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