blob: 7b898cefff7d78e88c9d7d1ec7ad2d191c9137ae [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301// * this is for making emacs happy: -*-Mode: C++;-*-
2/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04003 * Copyright 2019-2022,2023 Thomas E. Dickey *
4 * Copyright 1998-2005,2012 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05305 * *
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
micky3879b9f5e72025-07-08 18:04:53 -040039MODULE_ID("$Id: cursslk.cc,v 1.21 2023/02/25 23:36:06 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053040
41Soft_Label_Key_Set::Soft_Label_Key&
42 Soft_Label_Key_Set::Soft_Label_Key::operator=(char *text)
43{
44 delete[] label;
Steve Kondikae271bc2015-11-15 02:50:53 +010045 size_t need = 1 + ::strlen(text);
46 label = new char[need];
47 ::_nc_STRCPY(label,text,need);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053048 return *this;
49}
50
51long Soft_Label_Key_Set::count = 0L;
52int Soft_Label_Key_Set::num_labels = 0;
53
54Soft_Label_Key_Set::Label_Layout
55 Soft_Label_Key_Set::format = None;
56
57void Soft_Label_Key_Set::init()
58{
micky3879b9f5e72025-07-08 18:04:53 -040059 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];
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053064 for(int i=0; i < num_labels; i++) {
65 slk_array[i].num = i+1;
66 }
67 b_attrInit = FALSE;
68}
69
70Soft_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
79Soft_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
micky3879b9f5e72025-07-08 18:04:53 -040096Soft_Label_Key_Set::~Soft_Label_Key_Set() THROWS(NCursesException) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053097 if (!::isendwin())
98 clear();
99 delete[] slk_array;
100 count--;
101}
102
103Soft_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
micky3879b9f5e72025-07-08 18:04:53 -0400109int Soft_Label_Key_Set::labels() const {
110 return num_labels;
111}
112
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530113void 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
125void 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}