]> ncurses.scripts.mit.edu Git - ncurses.git/blob - c++/cursslk.cc
ncurses 6.2 - patch 20201031
[ncurses.git] / c++ / cursslk.cc
1 // * this is for making emacs happy: -*-Mode: C++;-*-
2 /****************************************************************************
3  * Copyright 2019,2020 Thomas E. Dickey                                     *
4  * Copyright 1998-2005,2012 Free Software Foundation, Inc.                  *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30
31 /****************************************************************************
32  *   Author: Juergen Pfeifer, 1997                                          *
33  ****************************************************************************/
34
35 #include "internal.h"
36 #include "cursslk.h"
37 #include "cursesapp.h"
38
39 MODULE_ID("$Id: cursslk.cc,v 1.19 2020/07/18 19:57:11 anonymous.maarten Exp $")
40
41 Soft_Label_Key_Set::Soft_Label_Key&
42   Soft_Label_Key_Set::Soft_Label_Key::operator=(char *text)
43 {
44   delete[] label;
45   size_t need = 1 + ::strlen(text);
46   label = new char[need];
47   ::_nc_STRCPY(label,text,need);
48   return *this;
49 }
50
51 long Soft_Label_Key_Set::count      = 0L;
52 int  Soft_Label_Key_Set::num_labels = 0;
53
54 Soft_Label_Key_Set::Label_Layout
55   Soft_Label_Key_Set::format = None;
56
57 void Soft_Label_Key_Set::init()
58 {
59   slk_array = new Soft_Label_Key[num_labels];
60   for(int i=0; i < num_labels; i++) {
61     slk_array[i].num = i+1;
62   }
63   b_attrInit = FALSE;
64 }
65
66 Soft_Label_Key_Set::Soft_Label_Key_Set()
67   : b_attrInit(FALSE),
68     slk_array(NULL)
69 {
70   if (format==None)
71     Error("No default SLK layout");
72   init();
73 }
74
75 Soft_Label_Key_Set::Soft_Label_Key_Set(Soft_Label_Key_Set::Label_Layout fmt)
76   : b_attrInit(FALSE),
77     slk_array(NULL)
78 {
79   if (fmt==None)
80     Error("Invalid SLK Layout");
81   if (count++==0) {
82     format = fmt;
83     if (ERR == ::slk_init(static_cast<int>(fmt)))
84       Error("slk_init");
85     num_labels = (fmt>=PC_Style?12:8);
86   }
87   else if (fmt!=format)
88     Error("All SLKs must have same layout");
89   init();
90 }
91
92 Soft_Label_Key_Set::~Soft_Label_Key_Set() THROWS(NCursesException) {
93   if (!::isendwin())
94     clear();
95   delete[] slk_array;
96   count--;
97 }
98
99 Soft_Label_Key_Set::Soft_Label_Key& Soft_Label_Key_Set::operator[](int i) {
100   if (i<1 || i>num_labels)
101     Error("Invalid Label index");
102   return slk_array[i-1];
103 }
104
105 int Soft_Label_Key_Set::labels() const {
106   return num_labels;
107 }
108
109 void Soft_Label_Key_Set::activate_label(int i, bool bf) {
110   if (!b_attrInit) {
111     NCursesApplication* A = NCursesApplication::getApplication();
112     if (A) attrset(A->labels());
113     b_attrInit = TRUE;
114   }
115   Soft_Label_Key& K = (*this)[i];
116   if (ERR==::slk_set(K.num,bf?K.label:"",K.format))
117     Error("slk_set");
118   noutrefresh();
119 }
120
121 void Soft_Label_Key_Set::activate_labels(bool bf)
122 {
123   if (!b_attrInit) {
124     NCursesApplication* A = NCursesApplication::getApplication();
125     if (A) attrset(A->labels());
126     b_attrInit = TRUE;
127   }
128   for(int i=1; i <= num_labels; i++) {
129     Soft_Label_Key& K = (*this)[i];
130     if (ERR==::slk_set(K.num,bf?K.label:"",K.format))
131       Error("slk_set");
132   }
133   if (bf)
134     restore();
135   else
136     clear();
137   noutrefresh();
138 }