forked from FNNDSC/KWWidgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkKWBalloonHelpManager.cxx
404 lines (324 loc) · 10.5 KB
/
vtkKWBalloonHelpManager.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*=========================================================================
Module: $RCSfile: vtkKWBalloonHelpManager.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 "vtkKWBalloonHelpManager.h"
#include "vtkKWLabel.h"
#include "vtkKWWidget.h"
#include "vtkKWTopLevel.h"
#include "vtkKWApplication.h"
#include "vtkKWTkUtilities.h"
#include "vtkKWWindowBase.h"
#include "vtkObjectFactory.h"
#include <vtksys/stl/string>
//----------------------------------------------------------------------------
vtkStandardNewMacro( vtkKWBalloonHelpManager );
//vtkCxxRevisionMacro(vtkKWBalloonHelpManager, "$Revision: 1.15 $");
//----------------------------------------------------------------------------
vtkKWBalloonHelpManager::vtkKWBalloonHelpManager()
{
this->CurrentWidget = NULL;
this->AfterTimerId = NULL;
this->TopLevel = NULL;
this->Label = NULL;
this->Delay = 1200;
this->Visibility = 1;
this->IgnoreIfNotEnabled = 0;
}
//----------------------------------------------------------------------------
vtkKWBalloonHelpManager::~vtkKWBalloonHelpManager()
{
this->SetCurrentWidget(NULL);
this->SetAfterTimerId(NULL);
if (this->TopLevel)
{
this->TopLevel->Delete();
this->TopLevel = NULL;
}
if (this->Label)
{
this->Label->Delete();
this->Label = NULL;
}
}
//----------------------------------------------------------------------------
void vtkKWBalloonHelpManager::CreateBalloon()
{
if (!this->TopLevel)
{
this->TopLevel = vtkKWTopLevel::New();
}
if (!this->Label)
{
this->Label = vtkKWLabel::New();
}
vtkKWApplication *app = this->GetApplication();
if (!app && this->CurrentWidget)
{
app = this->CurrentWidget->GetApplication();
}
if (app)
{
if (!this->TopLevel->IsCreated())
{
this->TopLevel->HideDecorationOn();
this->TopLevel->SetApplication(app);
this->TopLevel->Create();
this->TopLevel->SetBackgroundColor(0.0, 0.0, 0.0);
this->TopLevel->SetBorderWidth(1);
this->TopLevel->SetReliefToFlat();
}
if (!this->Label->IsCreated() && this->TopLevel)
{
this->Label->SetParent(this->TopLevel);
this->Label->Create();
this->Label->SetBackgroundColor(1.0, 1.0, 0.88);
this->Label->SetForegroundColor(0.0, 0.0, 0.0);
this->Label->SetJustificationToLeft();
this->Label->SetWrapLength("2i");
app->Script("pack %s", this->Label->GetWidgetName());
}
}
}
//----------------------------------------------------------------------------
void vtkKWBalloonHelpManager::TriggerCallback(vtkKWWidget *widget)
{
if (!widget || !widget->IsAlive() || this->ApplicationInExit())
{
return;
}
// If there is no help string, do not bother
if (!this->Visibility ||
(this->IgnoreIfNotEnabled && !widget->GetEnabled()) ||
(!widget->GetBalloonHelpString() && !widget->GetBalloonHelpIcon()))
{
this->SetAfterTimerId(NULL);
return;
}
// Cancel the previous balloon help, and setup the new one
this->CancelCallback();
this->SetCurrentWidget(widget);
this->SetAfterTimerId(
widget->Script("after %d {catch {%s DisplayCallback %s}}",
this->Delay,
this->GetTclName(), widget->GetTclName()));
}
//----------------------------------------------------------------------------
void vtkKWBalloonHelpManager::CancelCallback()
{
if (!this->GetApplication() || this->ApplicationInExit())
{
return;
}
// Cancel the previous timer
if (this->AfterTimerId)
{
vtkKWTkUtilities::CancelTimerHandler(
this->GetApplication(), this->AfterTimerId);
this->SetAfterTimerId(NULL);
}
this->SetCurrentWidget(NULL);
// Hide the balloon help
if (this->TopLevel)
{
this->TopLevel->Withdraw();
}
}
//----------------------------------------------------------------------------
void vtkKWBalloonHelpManager::WithdrawCallback()
{
if (!this->GetApplication() || this->ApplicationInExit())
{
return;
}
// Hide the balloon help
if (this->TopLevel)
{
this->TopLevel->Withdraw();
}
// Re-schedule the balloon help for the current widget (if any)
if (this->CurrentWidget)
{
this->TriggerCallback(this->CurrentWidget);
}
}
//----------------------------------------------------------------------------
void vtkKWBalloonHelpManager::DisplayCallback(vtkKWWidget *widget)
{
if (!this->GetApplication() || this->ApplicationInExit() ||
!this->Visibility ||
!widget || !widget->IsAlive() ||
(this->IgnoreIfNotEnabled && !widget->GetEnabled()))
{
return;
}
// If there is no help string, return
if (!widget->GetBalloonHelpString() && !widget->GetBalloonHelpIcon())
{
this->SetAfterTimerId(NULL);
return;
}
// Create the UI, set the help string
this->CreateBalloon();
if (widget->GetBalloonHelpIcon())
{
this->Label->SetImageToIcon(widget->GetBalloonHelpIcon());
}
else
{
this->Label->SetText(widget->GetBalloonHelpString());
}
// Get the position of the mouse
int pointer_x, pointer_y;
vtkKWTkUtilities::GetMousePointerCoordinates(widget, &pointer_x, &pointer_y);
// Get the position of the upper-left corner of the parent in its screen
int parent_x;
vtkKWTkUtilities::GetWidgetCoordinates(widget->GetParent(), &parent_x, NULL);
// Get the size of the parent
int parent_width;
vtkKWTkUtilities::GetWidgetSize(widget->GetParent(), &parent_width, NULL);
// Get the size of the balloon window
int balloon_width, balloon_height;
vtkKWTkUtilities::GetWidgetRequestedSize(
this->Label, &balloon_width, &balloon_height);
// Set the position of the widget relative to the mouse
// Try to keep the help from going past the right edge of the widget
// If it goes too far right, move it to the left,
// but not past the left edge of the parent widget
if (pointer_x + balloon_width > parent_x + parent_width)
{
pointer_x = parent_x + parent_width - balloon_width;
if (pointer_x < parent_x)
{
pointer_x = parent_x;
}
}
// Try to put the balloon under the mouse cursor, unless we found a
// vtkKWRenderWidget. This is done for convenience, to avoid popping up
// something on top of a render window; once the balloon disappears, this
// would trigger a re-render...
int distance_y_to_widget = 15;
int balloon_bottom_y = pointer_y + distance_y_to_widget + balloon_height;
vtkKWWindowBase *win = vtkKWWindowBase::SafeDownCast(
widget->GetParentTopLevel());
vtkKWWidget *found_rw = vtkKWTkUtilities::ContainsCoordinatesForSpecificType(
win, pointer_x, balloon_bottom_y, "vtkKWRenderWidget");
if (!found_rw)
{
found_rw = vtkKWTkUtilities::ContainsCoordinatesForSpecificType(
win, pointer_x + balloon_width, balloon_bottom_y, "vtkKWRenderWidget");
}
if (found_rw)
{
distance_y_to_widget = -distance_y_to_widget - balloon_height;
if (pointer_y + distance_y_to_widget < 0)
{
distance_y_to_widget = -pointer_y;
}
}
// Place the balloon
this->TopLevel->SetPosition(pointer_x, pointer_y + distance_y_to_widget);
this->GetApplication()->ProcessPendingEvents();
// Map the balloon
if (this->AfterTimerId)
{
this->TopLevel->DeIconify();
this->TopLevel->Raise();
}
this->SetAfterTimerId(NULL);
}
//----------------------------------------------------------------------------
void vtkKWBalloonHelpManager::SetVisibility(int v)
{
if (this->Visibility == v)
{
return;
}
this->Visibility = v;
this->Modified();
if (!this->Visibility)
{
this->CancelCallback();
}
}
//----------------------------------------------------------------------------
void vtkKWBalloonHelpManager::SetCurrentWidget(vtkKWWidget *widget)
{
if (this->CurrentWidget == widget ||
(this->ApplicationInExit() && widget))
{
return;
}
#if 0
if (this->CurrentWidget)
{
this->CurrentWidget->UnRegister(this);
this->CurrentWidget = NULL;
}
if (widget)
{
this->CurrentWidget = widget;
this->CurrentWidget->Register(this);
}
#else
this->CurrentWidget = widget;
#endif
this->Modified();
}
//----------------------------------------------------------------------------
int vtkKWBalloonHelpManager::ApplicationInExit()
{
vtkKWApplication *app = this->GetApplication();
if (!app && this->CurrentWidget)
{
app = this->CurrentWidget->GetApplication();
}
return (app && app->GetInExit()) ? 1 : 0;
}
//----------------------------------------------------------------------------
void vtkKWBalloonHelpManager::AddBindings(vtkKWWidget *widget)
{
if (!widget || !widget->IsAlive())
{
return;
}
if (strstr(widget->GetBinding("<Enter>"), "TriggerCallback"))
{
return;
}
vtksys_stl::string command("TriggerCallback ");
command += widget->GetTclName();
widget->AddBinding("<Enter>", this, command.c_str());
widget->AddBinding("<ButtonPress>", this, "WithdrawCallback");
widget->AddBinding("<KeyPress>", this, "WithdrawCallback");
widget->AddBinding("<B1-Motion>", this, "WithdrawCallback");
widget->AddBinding("<Leave>", this, "CancelCallback");
}
//----------------------------------------------------------------------------
void vtkKWBalloonHelpManager::RemoveBindings(vtkKWWidget *widget)
{
if (!widget || !widget->IsAlive())
{
return;
}
vtksys_stl::string command("TriggerCallback ");
command += widget->GetTclName();
widget->RemoveBinding("<Enter>", this, command.c_str());
widget->RemoveBinding("<ButtonPress>", this, "WithdrawCallback");
widget->RemoveBinding("<KeyPress>", this, "WithdrawCallback");
widget->RemoveBinding("<B1-Motion>", this, "WithdrawCallback");
widget->RemoveBinding("<Leave>", this, "CancelCallback");
}
//----------------------------------------------------------------------------
void vtkKWBalloonHelpManager::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Visibility: " << (this->Visibility ? "on":"off") << endl;
os << indent << "IgnoreIfNotEnabled: " << (this->IgnoreIfNotEnabled ? "on":"off") << endl;
os << indent << "Delay: " << this->GetDelay() << endl;
}