]> ncurses.scripts.mit.edu Git - ncurses.git/blob - c++/cursslk.cc
ncurses 6.4 - patch 20240420
[ncurses.git] / c++ / cursslk.cc
1 // * this is for making emacs happy: -*-Mode: C++;-*-
2 /****************************************************************************
3  * Copyright 2019-2020,2022 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.20 2022/02/26 17:57:23 tom 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   if (num_labels > 12)
60       num_labels = 12;
61   slk_array = new Soft_Label_Key[num_labels];
62   for(int i=0; i < num_labels; i++) {
63     slk_array[i].num = i+1;
64   }
65   b_attrInit = FALSE;
66 }
67
68 Soft_Label_Key_Set::Soft_Label_Key_Set()
69   : b_attrInit(FALSE),
70     slk_array(NULL)
71 {
72   if (format==None)
73     Error("No default SLK layout");
74   init();
75 }
76
77 Soft_Label_Key_Set::Soft_Label_Key_Set(Soft_Label_Key_Set::Label_Layout fmt)
78   : b_attrInit(FALSE),
79     slk_array(NULL)
80 {
81   if (fmt==None)
82     Error("Invalid SLK Layout");
83   if (count++==0) {
84     format = fmt;
85     if (ERR == ::slk_init(static_cast<int>(fmt)))
86       Error("slk_init");
87     num_labels = (fmt>=PC_Style?12:8);
88   }
89   else if (fmt!=format)
90     Error("All SLKs must have same layout");
91   init();
92 }
93
94 Soft_Label_Key_Set::~Soft_Label_Key_Set() THROWS(NCursesException) {
95   if (!::isendwin())
96     clear();
97   delete[] slk_array;
98   count--;
99 }
100
101 Soft_Label_Key_Set::Soft_Label_Key& Soft_Label_Key_Set::operator[](int i) {
102   if (i<1 || i>num_labels)
103     Error("Invalid Label index");
104   return slk_array[i-1];
105 }
106
107 int Soft_Label_Key_Set::labels() const {
108   return num_labels;
109 }
110
111 void Soft_Label_Key_Set::activate_label(int i, bool bf) {
112   if (!b_attrInit) {
113     NCursesApplication* A = NCursesApplication::getApplication();
114     if (A) attrset(A->labels());
115     b_attrInit = TRUE;
116   }
117   Soft_Label_Key& K = (*this)[i];
118   if (ERR==::slk_set(K.num,bf?K.label:"",K.format))
119     Error("slk_set");
120   noutrefresh();
121 }
122
123 void Soft_Label_Key_Set::activate_labels(bool bf)
124 {
125   if (!b_attrInit) {
126     NCursesApplication* A = NCursesApplication::getApplication();
127     if (A) attrset(A->labels());
128     b_attrInit = TRUE;
129   }
130   for(int i=1; i <= num_labels; i++) {
131     Soft_Label_Key& K = (*this)[i];
132     if (ERR==::slk_set(K.num,bf?K.label:"",K.format))
133       Error("slk_set");
134   }
135   if (bf)
136     restore();
137   else
138     clear();
139   noutrefresh();
140 }