-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDialogBox.cpp
More file actions
223 lines (175 loc) · 4.52 KB
/
DialogBox.cpp
File metadata and controls
223 lines (175 loc) · 4.52 KB
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
// Dialog Box with additional toggle option
#include "config.h"
#include "i18n.h"
#include <fx.h>
#include <fxkeys.h>
#include "DialogBox.h"
// Map
FXDEFMAP(DialogBox) DialogBoxMap[] =
{
FXMAPFUNC(SEL_KEYPRESS, 0, DialogBox::onKeyPress),
FXMAPFUNC(SEL_KEYRELEASE,0,DialogBox::onKeyRelease),
FXMAPFUNC(SEL_CLOSE, 0, DialogBox::onClose),
FXMAPFUNC(SEL_COMMAND, DialogBox::ID_CANCEL, DialogBox::onCmdCancel),
FXMAPFUNC(SEL_COMMAND, DialogBox::ID_ACCEPT, DialogBox::onCmdAccept),
FXMAPFUNC(SEL_COMMAND, DialogBox::ID_TOGGLE_OPTION, DialogBox::onCmdToggleOption)
};
// Object implementation
FXIMPLEMENT(DialogBox, FXTopWindow, DialogBoxMap, ARRAYNUMBER(DialogBoxMap))
// Contruct dialog which will stay on top of owner
DialogBox::DialogBox(FXWindow* win, const FXString& name, FXuint opts, int x, int y, int w, int h, int pl, int pr, int pt, int pb, int hs, int vs) :
FXTopWindow(win, name, NULL, NULL, opts, x, y, w, h, pl, pr, pt, pb, hs, vs)
{
_option = 0;
}
// Contruct free floating dialog
DialogBox::DialogBox(FXApp* a, const FXString& name, FXuint opts, int x, int y, int w, int h, int pl, int pr, int pt, int pb, int hs, int vs) :
FXTopWindow(a, name, NULL, NULL, opts, x, y, w, h, pl, pr, pt, pb, hs, vs)
{
_option = 0;
}
// Close window & cancel out of dialog
long DialogBox::onClose(FXObject*, FXSelector, void*)
{
if (target && target->handle(this, FXSEL(SEL_CLOSE, message), NULL))
{
return(1);
}
handle(this, FXSEL(SEL_COMMAND, DialogBox::ID_CANCEL), NULL);
return(1);
}
// Close dialog with an accept
long DialogBox::onCmdAccept(FXObject*, FXSelector, void*)
{
getApp()->stopModal(this, true);
hide();
return(1);
}
// Close dialog with a cancel
long DialogBox::onCmdCancel(FXObject*, FXSelector, void*)
{
getApp()->stopModal(this, false);
hide();
return(1);
}
// Toggle option
long DialogBox::onCmdToggleOption(FXObject*, FXSelector, void*)
{
_option = !_option;
return(1);
}
// Get option state
FXuint DialogBox::getOption()
{
return(_option);
}
// Create window
void DialogBox::create()
{
FXTopWindow::create();
}
// Show window such that the cursor is in it
void DialogBox::show(FXuint placement)
{
int rw, rh, wx, wy, ww, wh, x, y;
FXuint state;
// Get dialog size
translateCoordinatesTo(wx, wy, getRoot(), 0, 0);
ww = getWidth();
wh = getHeight();
// Where's the mouse?
getRoot()->getCursorPosition(x, y, state);
// Place such that mouse in the middle
if ((x < wx) || (y < wy) || (wx+ww <= x) || (wy+wh <= y))
{
// Get root window size
rw = getRoot()->getWidth();
rh = getRoot()->getHeight();
// Move by the minimal amount
if (x < wx)
{
wx = x-20;
}
else if (wx+ww <= x)
{
wx = x-ww+20;
}
if (y < wy)
{
wy = y-20;
}
else if (wy+wh <= y)
{
wy = y-wh+20;
}
// Adjust so dialog is fully visible
if (wx < 0)
{
wx = 10;
}
if (wy < 0)
{
wy = 10;
}
if (wx+ww > rw)
{
wx = rw-ww-10;
}
if (wy+wh > rh)
{
wy = rh-wh-10;
}
move(wx, wy);
}
// Pop the window
FXTopWindow::show(placement);
}
// Keyboard press; handle escape and return to close the dialog
long DialogBox::onKeyPress(FXObject* sender, FXSelector sel, void* ptr)
{
if (FXTopWindow::onKeyPress(sender, sel, ptr))
{
return(1);
}
if (((FXEvent*)ptr)->code == KEY_Escape)
{
handle(this, FXSEL(SEL_COMMAND, ID_CANCEL), NULL);
return(1);
}
FXEvent* event = (FXEvent*)ptr;
switch (event->code)
{
case KEY_Escape:
handle(this, FXSEL(SEL_COMMAND, ID_CANCEL), NULL);
return(1);
case KEY_KP_Enter:
case KEY_Return:
handle(this, FXSEL(SEL_COMMAND, ID_ACCEPT), NULL);
return(1);
default:
FXTopWindow::onKeyPress(sender, sel, ptr);
return(1);
}
return(0);
}
// Keyboard release; handle escape to close the dialog
long DialogBox::onKeyRelease(FXObject* sender,FXSelector sel,void* ptr)
{
if(FXTopWindow::onKeyRelease(sender,sel,ptr))
{
return 1;
}
if(((FXEvent*)ptr)->code==KEY_Escape)
{
return 1;
}
return 0;
}
// Execute dialog box modally
FXuint DialogBox::execute(FXuint placement)
{
create();
show(placement);
getApp()->refresh();
return(getApp()->runModalFor(this));
}