-
Notifications
You must be signed in to change notification settings - Fork 3
/
vtkKWComboBox.cxx
259 lines (212 loc) · 7.13 KB
/
vtkKWComboBox.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*=========================================================================
Module: $RCSfile: vtkKWComboBox.cxx,v $
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkKWComboBox.h"
#include "vtkObjectFactory.h"
#include "vtkKWApplication.h"
#include "Utilities/BWidgets/vtkKWBWidgetsInit.h"
//----------------------------------------------------------------------------
vtkStandardNewMacro( vtkKWComboBox);
vtkCxxRevisionMacro(vtkKWComboBox, "$Revision: 1.22 $");
//----------------------------------------------------------------------------
vtkKWComboBox::vtkKWComboBox()
{
this->Width = 20;
}
//----------------------------------------------------------------------------
vtkKWComboBox::~vtkKWComboBox()
{
}
//----------------------------------------------------------------------------
void vtkKWComboBox::CreateWidget()
{
// Use BWidget's ComboBox class:
// http://aspn.activestate.com/ASPN/docs/ActiveTcl/bwidget/contents.html
vtkKWApplication *app = this->GetApplication();
vtkKWBWidgetsInit::Initialize(app ? app->GetMainInterp() : NULL);
// Call the superclass to set the appropriate flags then create manually
if (!vtkKWWidget::CreateSpecificTkWidget(this,
"ComboBox", "-highlightthickness 0 -entrybg #ffffff"))
{
vtkErrorMacro("Failed creating widget " << this->GetClassName());
return;
}
// Design choice: we assume a keypress is meant for this widget only
this->SetGenericBinding(
"BwEntry",
"<KeyPress>", NULL, "::tk::CancelRepeat ; ::tk::EntryInsert %W %A; break");
this->Configure();
}
//----------------------------------------------------------------------------
void vtkKWComboBox::SetValue(const char *s)
{
if (!this->IsAlive())
{
return;
}
int old_state = this->GetState();
this->SetStateToNormal();
this->SetTextOption("-text", s);
this->SetState(old_state);
}
//----------------------------------------------------------------------------
void vtkKWComboBox::AddValue(const char* value)
{
if (!this->IsCreated() || !value || this->HasValue(value))
{
return;
}
this->Script("%s configure -values [concat [%s cget -values] {\"%s\"}]",
this->GetWidgetName(), this->GetWidgetName(), value);
}
//----------------------------------------------------------------------------
void vtkKWComboBox::AddValueAsInt(int value)
{
char buffer[256];
sprintf(buffer, "%d", value);
this->AddValue(buffer);
}
//----------------------------------------------------------------------------
void vtkKWComboBox::ReplaceNthValue( int idx, const char *value )
{
if (!this->IsCreated() || !value || this->HasValue(value))
{
return;
}
if (idx < 0 || idx >= this->GetNumberOfValues())
{
vtkErrorMacro(
"This combobox has only " << this->GetNumberOfValues()
<< " elements. Index " << idx << " is out of range");
return;
}
this->Script("%s configure -values [lreplace [%s cget -values] %d %d %s]",
this->GetWidgetName(), this->GetWidgetName(), idx, idx, value);
}
//----------------------------------------------------------------------------
int vtkKWComboBox::GetNumberOfValues()
{
if (!this->IsCreated())
{
return 0;
}
return atoi(
this->Script("llength [%s cget -values]", this->GetWidgetName()));
}
//----------------------------------------------------------------------------
void vtkKWComboBox::DeleteAllValues()
{
if (!this->IsCreated())
{
return;
}
this->Script("%s configure -values {}", this->GetWidgetName());
}
//----------------------------------------------------------------------------
void vtkKWComboBox::DeleteValue(int idx)
{
if (!this->IsCreated())
{
return;
}
if (idx < 0 || idx >= this->GetNumberOfValues())
{
vtkErrorMacro(
"This combobox has only " << this->GetNumberOfValues()
<< " elements. Index " << idx << " is out of range");
return;
}
this->Script("%s configure -values [lreplace [%s cget -values] %d %d]",
this->GetWidgetName(), this->GetWidgetName(), idx, idx);
}
//----------------------------------------------------------------------------
const char* vtkKWComboBox::GetValueFromIndex(int idx)
{
if (!this->IsCreated())
{
return NULL;
}
if (idx < 0 || idx >= this->GetNumberOfValues())
{
vtkErrorMacro(
"This combobox has only " << this->GetNumberOfValues()
<< " elements. Index " << idx << " is out of range");
return NULL;
}
return this->Script("lindex [%s cget -values] %d",
this->GetWidgetName(), idx);
}
//----------------------------------------------------------------------------
int vtkKWComboBox::HasValue(const char* value)
{
return this->GetValueIndex(value) < 0 ? 0 : 1;
}
//----------------------------------------------------------------------------
int vtkKWComboBox::GetValueIndex(const char* value)
{
if (!this->IsCreated() || !value)
{
return -1;
}
return atoi(this->Script("lsearch [%s cget -values] {%s}",
this->GetWidgetName(), value));
}
//----------------------------------------------------------------------------
void vtkKWComboBox::SetCommand(vtkObject *object, const char *method)
{
this->Superclass::SetCommand(object, method);
if (this->IsCreated())
{
char *command = NULL;
this->SetObjectMethodCommand(&command, this, "ValueCallback");
this->SetConfigurationOption("-command", command);
this->SetConfigurationOption("-modifycmd", command);
delete [] command;
}
}
//----------------------------------------------------------------------------
void vtkKWComboBox::UpdateEnableState()
{
// We need to bypass the superclass (vtkKWEntry) UpdateEnableState
// because it sets up Tk options that are not supported by this Tk widget.
this->vtkKWCoreWidget::UpdateEnableState();
this->SetState(this->GetEnabled());
if (this->IsCreated())
{
this->SetConfigurationOptionAsInt("-editable", this->ReadOnly ? 0 : 1);
}
}
//----------------------------------------------------------------------------
void vtkKWComboBox::SetListboxWidth(int n)
{
if (this->IsCreated())
{
this->SetConfigurationOptionAsInt("-listboxwidth", n);
}
}
//----------------------------------------------------------------------------
int vtkKWComboBox::GetListboxWidth()
{
if (this->IsCreated())
{
return this->GetConfigurationOptionAsInt("-listboxwidth");
}
return 0;
}
//----------------------------------------------------------------------------
void vtkKWComboBox::SetBackgroundColor(double r, double g, double b)
{
this->Superclass::SetBackgroundColor(r, g, b);
this->SetConfigurationOptionAsColor("-entrybg", r, g, b);
}
//----------------------------------------------------------------------------
void vtkKWComboBox::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}