]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dialog.cpp | |
3 | // Purpose: wxDialog class | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
4bb6408c JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
4bb6408c JS |
13 | #pragma implementation "dialog.h" |
14 | #endif | |
15 | ||
1248b41f MB |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
bcd055ae JJ |
19 | #ifdef __VMS |
20 | #define XtDisplay XTDISPLAY | |
21 | #define XtWindow XTWINDOW | |
22 | #define XtParent XTPARENT | |
23 | #define XtScreen XTSCREEN | |
24 | #endif | |
25 | ||
4bb6408c JS |
26 | #include "wx/dialog.h" |
27 | #include "wx/utils.h" | |
4bb6408c JS |
28 | #include "wx/app.h" |
29 | #include "wx/settings.h" | |
7e1bcfa8 | 30 | #include "wx/evtloop.h" |
4bb6408c | 31 | |
338dd992 JJ |
32 | #ifdef __VMS__ |
33 | #pragma message disable nosimpint | |
34 | #endif | |
dfc54541 JS |
35 | #include <Xm/Xm.h> |
36 | ||
37 | #include <X11/Shell.h> | |
38 | #if XmVersion >= 1002 | |
39 | #include <Xm/XmAll.h> | |
40 | #endif | |
41 | #include <Xm/MwmUtil.h> | |
42 | #include <Xm/Label.h> | |
43 | #include <Xm/BulletinB.h> | |
44 | #include <Xm/Frame.h> | |
45 | #include <Xm/Text.h> | |
46 | #include <Xm/DialogS.h> | |
47 | #include <Xm/FileSB.h> | |
48 | #include <Xm/RowColumn.h> | |
49 | #include <Xm/LabelG.h> | |
50 | #include <Xm/AtomMgr.h> | |
51 | #if XmVersion > 1000 | |
52 | #include <Xm/Protocols.h> | |
53 | #endif | |
338dd992 JJ |
54 | #ifdef __VMS__ |
55 | #pragma message enable nosimpint | |
56 | #endif | |
dfc54541 JS |
57 | |
58 | #include "wx/motif/private.h" | |
59 | ||
dfc54541 JS |
60 | // A stack of modal_showing flags, since we can't rely |
61 | // on accessing wxDialog::m_modalShowing within | |
62 | // wxDialog::Show in case a callback has deleted the wxDialog. | |
7e1bcfa8 | 63 | // static wxList wxModalShowingStack; |
dfc54541 | 64 | |
4bb6408c JS |
65 | // Lists to keep track of windows, so we can disable/enable them |
66 | // for modal dialogs | |
67 | wxList wxModalDialogs; | |
798a4529 | 68 | extern wxList wxModelessWindows; // Frames and modeless dialogs |
4bb6408c JS |
69 | extern wxList wxPendingDelete; |
70 | ||
47d67540 | 71 | #define wxUSE_INVISIBLE_RESIZE 1 |
dfc54541 | 72 | |
798a4529 | 73 | IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow) |
4bb6408c | 74 | |
798a4529 | 75 | BEGIN_EVENT_TABLE(wxDialog, wxTopLevelWindow) |
8e877c19 RR |
76 | EVT_BUTTON(wxID_OK, wxDialog::OnOK) |
77 | EVT_BUTTON(wxID_APPLY, wxDialog::OnApply) | |
78 | EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel) | |
79 | EVT_CHAR_HOOK(wxDialog::OnCharHook) | |
80 | EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged) | |
81 | EVT_CLOSE(wxDialog::OnCloseWindow) | |
4bb6408c JS |
82 | END_EVENT_TABLE() |
83 | ||
4bb6408c JS |
84 | |
85 | wxDialog::wxDialog() | |
86 | { | |
96be256b | 87 | m_modalShowing = false; |
7e1bcfa8 | 88 | m_eventLoop = NULL; |
a756f210 | 89 | m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
4bb6408c JS |
90 | } |
91 | ||
92 | bool wxDialog::Create(wxWindow *parent, wxWindowID id, | |
2d120f83 JS |
93 | const wxString& title, |
94 | const wxPoint& pos, | |
95 | const wxSize& size, | |
96 | long style, | |
97 | const wxString& name) | |
4bb6408c | 98 | { |
798a4529 MB |
99 | SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG); |
100 | ||
101 | if( !wxTopLevelWindow::Create( parent, id, title, pos, size, style, | |
102 | name ) ) | |
96be256b | 103 | return false; |
798a4529 | 104 | |
96be256b | 105 | m_modalShowing = false; |
7e1bcfa8 | 106 | m_eventLoop = NULL; |
dfe1eee3 | 107 | |
a756f210 | 108 | m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
0d57be45 | 109 | m_foregroundColour = *wxBLACK; |
dfe1eee3 | 110 | |
798a4529 | 111 | Widget dialogShell = (Widget) m_mainWidget; |
dfc54541 | 112 | Widget shell = XtParent(dialogShell) ; |
798a4529 MB |
113 | |
114 | SetTitle( title ); | |
dfe1eee3 | 115 | |
a756f210 | 116 | m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); |
96be256b | 117 | ChangeFont(false); |
dfe1eee3 | 118 | |
dfc54541 | 119 | // Can't remember what this was about... but I think it's necessary. |
dfad0599 | 120 | if (wxUSE_INVISIBLE_RESIZE) |
dfc54541 | 121 | { |
2d120f83 JS |
122 | if (pos.x > -1) |
123 | XtVaSetValues(dialogShell, XmNx, pos.x, | |
124 | NULL); | |
125 | if (pos.y > -1) | |
126 | XtVaSetValues(dialogShell, XmNy, pos.y, | |
127 | NULL); | |
dfe1eee3 | 128 | |
2d120f83 JS |
129 | if (size.x > -1) |
130 | XtVaSetValues(dialogShell, XmNwidth, size.x, NULL); | |
131 | if (size.y > -1) | |
132 | XtVaSetValues(dialogShell, XmNheight, size.y, NULL); | |
dfc54541 | 133 | } |
dfe1eee3 | 134 | |
dfc54541 JS |
135 | // Positioning of the dialog doesn't work properly unless the dialog |
136 | // is managed, so we manage without mapping to the screen. | |
137 | // To show, we map the shell (actually it's parent). | |
dfad0599 | 138 | if (!wxUSE_INVISIBLE_RESIZE) |
96be256b | 139 | XtVaSetValues(shell, XmNmappedWhenManaged, False, NULL); |
dfe1eee3 | 140 | |
dfad0599 | 141 | if (!wxUSE_INVISIBLE_RESIZE) |
dfc54541 JS |
142 | { |
143 | XtManageChild(dialogShell); | |
144 | SetSize(pos.x, pos.y, size.x, size.y); | |
145 | } | |
96be256b | 146 | XtAddEventHandler(dialogShell,ExposureMask,False, |
2e35f56f | 147 | wxUniversalRepaintProc, (XtPointer) this); |
dfe1eee3 | 148 | |
0d57be45 | 149 | ChangeBackgroundColour(); |
dfe1eee3 | 150 | |
96be256b | 151 | return true; |
4bb6408c JS |
152 | } |
153 | ||
e6b915ed | 154 | bool wxDialog::DoCreate(wxWindow* parent, |
f58585c0 VZ |
155 | wxWindowID id, |
156 | const wxString& title, | |
157 | const wxPoint& pos, | |
158 | const wxSize& size, | |
159 | long style, | |
160 | const wxString& name) | |
798a4529 MB |
161 | { |
162 | Widget parentWidget = (Widget) 0; | |
163 | if( parent ) | |
164 | parentWidget = (Widget) parent->GetTopWidget(); | |
165 | if( !parent ) | |
166 | parentWidget = (Widget) wxTheApp->GetTopLevelWidget(); | |
167 | ||
168 | wxASSERT_MSG( (parentWidget != (Widget) 0), | |
169 | "Could not find a suitable parent shell for dialog." ); | |
170 | ||
171 | Arg args[2]; | |
172 | XtSetArg (args[0], XmNdefaultPosition, False); | |
173 | XtSetArg (args[1], XmNautoUnmanage, False); | |
174 | Widget dialogShell = | |
d3a80c92 MB |
175 | XmCreateBulletinBoardDialog( parentWidget, |
176 | wxConstCast(name.c_str(), char), | |
798a4529 MB |
177 | args, 2); |
178 | m_mainWidget = (WXWidget) dialogShell; | |
179 | ||
180 | // We don't want margins, since there is enough elsewhere. | |
181 | XtVaSetValues( dialogShell, | |
182 | XmNmarginHeight, 0, | |
183 | XmNmarginWidth, 0, | |
184 | XmNresizePolicy, XmRESIZE_NONE, | |
185 | NULL ) ; | |
186 | ||
187 | XtTranslations ptr ; | |
188 | XtOverrideTranslations(dialogShell, | |
189 | ptr = XtParseTranslationTable("<Configure>: resize()")); | |
190 | XtFree((char *)ptr); | |
191 | ||
192 | XtRealizeWidget(dialogShell); | |
193 | ||
194 | wxAddWindowToTable( (Widget)m_mainWidget, this ); | |
195 | ||
96be256b | 196 | return true; |
798a4529 MB |
197 | } |
198 | ||
219ee9ba VZ |
199 | void wxDialog::DoDestroy() |
200 | { | |
201 | } | |
202 | ||
4bb6408c JS |
203 | void wxDialog::SetModal(bool flag) |
204 | { | |
5f9a153c JJ |
205 | #ifdef __VMS |
206 | #pragma message disable codcauunr | |
207 | #endif | |
208 | if ( flag ) | |
dfc54541 JS |
209 | m_windowStyle |= wxDIALOG_MODAL ; |
210 | else | |
211 | if ( m_windowStyle & wxDIALOG_MODAL ) | |
2d120f83 | 212 | m_windowStyle -= wxDIALOG_MODAL ; |
dfe1eee3 | 213 | |
2d120f83 JS |
214 | wxModelessWindows.DeleteObject(this); |
215 | if (!flag) | |
216 | wxModelessWindows.Append(this); | |
5f9a153c JJ |
217 | #ifdef __VMS |
218 | #pragma message enable codcauunr | |
219 | #endif | |
4bb6408c JS |
220 | } |
221 | ||
222 | wxDialog::~wxDialog() | |
223 | { | |
96be256b | 224 | m_isBeingDeleted = true; |
2187eef5 | 225 | |
7e1bcfa8 | 226 | delete m_eventLoop; |
798a4529 | 227 | |
2e35f56f | 228 | if (m_mainWidget) |
798a4529 | 229 | { |
96be256b | 230 | XtRemoveEventHandler((Widget) m_mainWidget, ExposureMask, False, |
798a4529 MB |
231 | wxUniversalRepaintProc, (XtPointer) this); |
232 | } | |
dfe1eee3 | 233 | |
96be256b | 234 | m_modalShowing = false; |
dfad0599 | 235 | if (!wxUSE_INVISIBLE_RESIZE && m_mainWidget) |
dfc54541 | 236 | { |
2d120f83 | 237 | XtUnmapWidget((Widget) m_mainWidget); |
dfc54541 | 238 | } |
2187eef5 MB |
239 | |
240 | PreDestroy(); | |
dfe1eee3 | 241 | |
f58585c0 | 242 | if ( m_mainWidget ) |
cba2db0c | 243 | { |
798a4529 MB |
244 | wxDeleteWindowFromTable( (Widget)m_mainWidget ); |
245 | XtDestroyWidget( (Widget)m_mainWidget ); | |
dfc54541 | 246 | } |
4bb6408c JS |
247 | } |
248 | ||
249 | // By default, pressing escape cancels the dialog | |
250 | void wxDialog::OnCharHook(wxKeyEvent& event) | |
251 | { | |
2d120f83 JS |
252 | if (event.m_keyCode == WXK_ESCAPE) |
253 | { | |
254 | // Behaviour changed in 2.0: we'll send a Cancel message | |
255 | // to the dialog instead of Close. | |
256 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); | |
257 | cancelEvent.SetEventObject( this ); | |
258 | GetEventHandler()->ProcessEvent(cancelEvent); | |
dfe1eee3 | 259 | |
2d120f83 JS |
260 | return; |
261 | } | |
262 | // We didn't process this event. | |
263 | event.Skip(); | |
4bb6408c JS |
264 | } |
265 | ||
bfc6fde4 | 266 | void wxDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
4bb6408c | 267 | { |
dfc54541 | 268 | XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_ANY, NULL); |
bfc6fde4 | 269 | wxWindow::DoSetSize(x, y, width, height, sizeFlags); |
dfc54541 | 270 | XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_NONE, NULL); |
4bb6408c JS |
271 | } |
272 | ||
ad813b00 JS |
273 | void wxDialog::DoSetClientSize(int width, int height) |
274 | { | |
275 | wxWindow::SetSize(-1, -1, width, height); | |
276 | } | |
277 | ||
4bb6408c JS |
278 | void wxDialog::SetTitle(const wxString& title) |
279 | { | |
798a4529 | 280 | wxTopLevelWindow::SetTitle( title ); |
4bb6408c | 281 | |
798a4529 MB |
282 | if( !title.empty() ) |
283 | { | |
284 | wxXmString str( title ); | |
285 | XtVaSetValues( (Widget)m_mainWidget, | |
286 | XmNtitle, title.c_str(), | |
287 | XmNdialogTitle, str(), // Roberto Cocchi | |
288 | XmNiconName, title.c_str(), | |
289 | NULL ); | |
dfc54541 | 290 | } |
dfc54541 JS |
291 | } |
292 | ||
798a4529 | 293 | bool wxDialog::Show( bool show ) |
dfc54541 | 294 | { |
5a2e3d8c MB |
295 | if( !wxWindowBase::Show( show ) ) |
296 | return false; | |
dfc54541 | 297 | |
dfc54541 | 298 | m_isShown = show; |
dfe1eee3 | 299 | |
b3090d48 MB |
300 | if (show) |
301 | { | |
302 | // this usually will result in TransferDataToWindow() being called | |
303 | // which will change the controls values so do it before showing as | |
304 | // otherwise we could have some flicker | |
305 | InitDialog(); | |
306 | } | |
307 | ||
dfc54541 JS |
308 | if (show) |
309 | { | |
dfad0599 | 310 | if (!wxUSE_INVISIBLE_RESIZE) |
2d120f83 | 311 | XtMapWidget(XtParent((Widget) m_mainWidget)); |
dfc54541 | 312 | else |
798a4529 | 313 | XtManageChild((Widget)m_mainWidget) ; |
dfe1eee3 | 314 | |
798a4529 MB |
315 | XRaiseWindow( XtDisplay( (Widget)m_mainWidget ), |
316 | XtWindow( (Widget)m_mainWidget) ); | |
dfe1eee3 | 317 | |
dfc54541 JS |
318 | } |
319 | else | |
320 | { | |
dfad0599 | 321 | if (!wxUSE_INVISIBLE_RESIZE) |
dfc54541 JS |
322 | XtUnmapWidget(XtParent((Widget) m_mainWidget)); |
323 | else | |
798a4529 | 324 | XtUnmanageChild((Widget)m_mainWidget) ; |
dfe1eee3 | 325 | |
eb6fa4b4 | 326 | XFlush(XtDisplay((Widget)m_mainWidget)); |
96be256b | 327 | XSync(XtDisplay((Widget)m_mainWidget), False); |
dfc54541 | 328 | } |
dfe1eee3 | 329 | |
96be256b | 330 | return true; |
dfc54541 JS |
331 | } |
332 | ||
333 | // Shows a dialog modally, returning a return code | |
4bb6408c JS |
334 | int wxDialog::ShowModal() |
335 | { | |
336 | m_windowStyle |= wxDIALOG_MODAL; | |
dfe1eee3 | 337 | |
96be256b | 338 | Show(true); |
dfe1eee3 | 339 | |
eb6fa4b4 MB |
340 | // after the event loop ran, the widget might already have been destroyed |
341 | WXDisplay* display = (WXDisplay*)XtDisplay( (Widget)m_mainWidget ); | |
342 | ||
dfc54541 JS |
343 | if (m_modalShowing) |
344 | return 0; | |
7e1bcfa8 | 345 | m_eventLoop = new wxEventLoop; |
dfe1eee3 | 346 | |
96be256b MB |
347 | m_modalShowing = true; |
348 | XtAddGrab((Widget) m_mainWidget, True, False); | |
dfe1eee3 | 349 | |
7e1bcfa8 | 350 | m_eventLoop->Run(); |
dfe1eee3 | 351 | |
dfc54541 | 352 | // Now process all events in case they get sent to a destroyed dialog |
eb6fa4b4 | 353 | wxFlushEvents( display ); |
dfe1eee3 | 354 | |
7e1bcfa8 MB |
355 | delete m_eventLoop; |
356 | m_eventLoop = NULL; | |
357 | ||
dfc54541 JS |
358 | // TODO: is it safe to call this, if the dialog may have been deleted |
359 | // by now? Probably only if we're using delayed deletion of dialogs. | |
360 | return GetReturnCode(); | |
4bb6408c JS |
361 | } |
362 | ||
363 | void wxDialog::EndModal(int retCode) | |
364 | { | |
dfc54541 JS |
365 | if (!m_modalShowing) |
366 | return; | |
dfe1eee3 | 367 | |
dfc54541 | 368 | SetReturnCode(retCode); |
dfe1eee3 | 369 | |
88150e60 JS |
370 | // Strangely, we don't seem to need this now. |
371 | // XtRemoveGrab((Widget) m_mainWidget); | |
dfe1eee3 | 372 | |
96be256b | 373 | Show(false); |
dfe1eee3 | 374 | |
96be256b | 375 | m_modalShowing = false; |
7e1bcfa8 | 376 | m_eventLoop->Exit(); |
e95b8d5a JS |
377 | |
378 | SetModal(false); | |
4bb6408c JS |
379 | } |
380 | ||
381 | // Standard buttons | |
f9e02ac7 | 382 | void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event)) |
4bb6408c | 383 | { |
2d120f83 JS |
384 | if ( Validate() && TransferDataFromWindow() ) |
385 | { | |
4bb6408c JS |
386 | if ( IsModal() ) |
387 | EndModal(wxID_OK); | |
388 | else | |
389 | { | |
2d120f83 | 390 | SetReturnCode(wxID_OK); |
96be256b | 391 | this->Show(false); |
4bb6408c | 392 | } |
2d120f83 | 393 | } |
4bb6408c JS |
394 | } |
395 | ||
f9e02ac7 | 396 | void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event)) |
4bb6408c | 397 | { |
2d120f83 JS |
398 | if (Validate()) |
399 | TransferDataFromWindow(); | |
400 | // TODO probably need to disable the Apply button until things change again | |
4bb6408c JS |
401 | } |
402 | ||
f9e02ac7 | 403 | void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) |
4bb6408c JS |
404 | { |
405 | if ( IsModal() ) | |
406 | EndModal(wxID_CANCEL); | |
407 | else | |
408 | { | |
409 | SetReturnCode(wxID_CANCEL); | |
96be256b | 410 | this->Show(false); |
4bb6408c JS |
411 | } |
412 | } | |
413 | ||
af111fc3 | 414 | void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
4bb6408c | 415 | { |
e3065973 | 416 | // We'll send a Cancel message by default, |
4bb6408c | 417 | // which may close the dialog. |
e3065973 JS |
418 | // Check for looping if the Cancel event handler calls Close(). |
419 | ||
420 | // Note that if a cancel button and handler aren't present in the dialog, | |
421 | // nothing will happen when you close the dialog via the window manager, or | |
422 | // via Close(). | |
423 | // We wouldn't want to destroy the dialog by default, since the dialog may have been | |
424 | // created on the stack. | |
425 | // However, this does mean that calling dialog->Close() won't delete the dialog | |
426 | // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be | |
427 | // sure to destroy the dialog. | |
428 | // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog. | |
429 | ||
4bb6408c | 430 | static wxList closing; |
dfe1eee3 | 431 | |
4bb6408c | 432 | if ( closing.Member(this) ) |
e3065973 | 433 | return; |
dfe1eee3 | 434 | |
4bb6408c | 435 | closing.Append(this); |
dfe1eee3 | 436 | |
2d120f83 JS |
437 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
438 | cancelEvent.SetEventObject( this ); | |
e3065973 | 439 | GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog |
4bb6408c | 440 | |
e3065973 | 441 | closing.DeleteObject(this); |
4bb6408c JS |
442 | } |
443 | ||
444 | // Destroy the window (delayed, if a managed window) | |
445 | bool wxDialog::Destroy() | |
446 | { | |
2d120f83 JS |
447 | if (!wxPendingDelete.Member(this)) |
448 | wxPendingDelete.Append(this); | |
96be256b | 449 | return true; |
4bb6408c JS |
450 | } |
451 | ||
f9e02ac7 | 452 | void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) |
4bb6408c | 453 | { |
a756f210 | 454 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); |
2d120f83 | 455 | Refresh(); |
4bb6408c JS |
456 | } |
457 | ||
94b49b93 | 458 | void wxDialog::ChangeFont(bool keepOriginalSize) |
0d57be45 | 459 | { |
94b49b93 | 460 | wxWindow::ChangeFont(keepOriginalSize); |
0d57be45 JS |
461 | } |
462 | ||
463 | void wxDialog::ChangeBackgroundColour() | |
464 | { | |
94b49b93 | 465 | if (GetMainWidget()) |
a8680e3e | 466 | wxDoChangeBackgroundColour(GetMainWidget(), m_backgroundColour); |
0d57be45 JS |
467 | } |
468 | ||
469 | void wxDialog::ChangeForegroundColour() | |
470 | { | |
94b49b93 | 471 | if (GetMainWidget()) |
a8680e3e | 472 | wxDoChangeForegroundColour(GetMainWidget(), m_foregroundColour); |
0d57be45 | 473 | } |