]>
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 | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "dialog.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/dialog.h" | |
17 | #include "wx/utils.h" | |
18 | #include "wx/frame.h" | |
19 | #include "wx/app.h" | |
20 | #include "wx/settings.h" | |
21 | ||
dfc54541 JS |
22 | #include <Xm/Xm.h> |
23 | ||
24 | #include <X11/Shell.h> | |
25 | #if XmVersion >= 1002 | |
26 | #include <Xm/XmAll.h> | |
27 | #endif | |
28 | #include <Xm/MwmUtil.h> | |
29 | #include <Xm/Label.h> | |
30 | #include <Xm/BulletinB.h> | |
31 | #include <Xm/Frame.h> | |
32 | #include <Xm/Text.h> | |
33 | #include <Xm/DialogS.h> | |
34 | #include <Xm/FileSB.h> | |
35 | #include <Xm/RowColumn.h> | |
36 | #include <Xm/LabelG.h> | |
37 | #include <Xm/AtomMgr.h> | |
38 | #if XmVersion > 1000 | |
39 | #include <Xm/Protocols.h> | |
40 | #endif | |
41 | ||
42 | #include "wx/motif/private.h" | |
43 | ||
44 | static void wxCloseDialogCallback(Widget widget, XtPointer client_data, XmAnyCallbackStruct *cbs); | |
45 | static void wxDialogBoxRepaintProc(Widget w, XtPointer c_data, XEvent *event, char *); | |
46 | static void wxDialogBoxEventHandler (Widget wid, | |
2d120f83 JS |
47 | XtPointer client_data, |
48 | XEvent* event, | |
49 | Boolean *continueToDispatch); | |
dfc54541 JS |
50 | |
51 | static void wxUnmapBulletinBoard(Widget dialog, wxDialog *client,XtPointer call); | |
52 | ||
53 | // A stack of modal_showing flags, since we can't rely | |
54 | // on accessing wxDialog::m_modalShowing within | |
55 | // wxDialog::Show in case a callback has deleted the wxDialog. | |
56 | static wxList wxModalShowingStack; | |
57 | ||
4bb6408c JS |
58 | // Lists to keep track of windows, so we can disable/enable them |
59 | // for modal dialogs | |
60 | wxList wxModalDialogs; | |
61 | wxList wxModelessWindows; // Frames and modeless dialogs | |
62 | extern wxList wxPendingDelete; | |
63 | ||
47d67540 | 64 | #define wxUSE_INVISIBLE_RESIZE 1 |
dfc54541 | 65 | |
4bb6408c JS |
66 | #if !USE_SHARED_LIBRARY |
67 | IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxPanel) | |
68 | ||
69 | BEGIN_EVENT_TABLE(wxDialog, wxPanel) | |
2d120f83 JS |
70 | EVT_BUTTON(wxID_OK, wxDialog::OnOK) |
71 | EVT_BUTTON(wxID_APPLY, wxDialog::OnApply) | |
72 | EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel) | |
73 | EVT_CHAR_HOOK(wxDialog::OnCharHook) | |
74 | EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged) | |
75 | EVT_CLOSE(wxDialog::OnCloseWindow) | |
4bb6408c JS |
76 | END_EVENT_TABLE() |
77 | ||
78 | #endif | |
79 | ||
80 | wxDialog::wxDialog() | |
81 | { | |
dfc54541 | 82 | m_modalShowing = FALSE; |
0d57be45 | 83 | m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE); |
4bb6408c JS |
84 | } |
85 | ||
86 | bool wxDialog::Create(wxWindow *parent, wxWindowID id, | |
2d120f83 JS |
87 | const wxString& title, |
88 | const wxPoint& pos, | |
89 | const wxSize& size, | |
90 | long style, | |
91 | const wxString& name) | |
4bb6408c | 92 | { |
dfc54541 JS |
93 | m_windowStyle = style; |
94 | m_modalShowing = FALSE; | |
95 | m_dialogTitle = title; | |
2d120f83 | 96 | |
0d57be45 JS |
97 | m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE); |
98 | m_foregroundColour = *wxBLACK; | |
2d120f83 | 99 | |
dfc54541 | 100 | SetName(name); |
2d120f83 | 101 | |
dfc54541 JS |
102 | if (!parent) |
103 | wxTopLevelWindows.Append(this); | |
2d120f83 | 104 | |
dfc54541 | 105 | if (parent) parent->AddChild(this); |
2d120f83 | 106 | |
dfc54541 JS |
107 | if ( id == -1 ) |
108 | m_windowId = (int)NewControlId(); | |
109 | else | |
110 | m_windowId = id; | |
2d120f83 | 111 | |
dfc54541 JS |
112 | Widget parentWidget = (Widget) 0; |
113 | if (parent) | |
114 | parentWidget = (Widget) parent->GetTopWidget(); | |
115 | if (!parent) | |
116 | parentWidget = (Widget) wxTheApp->GetTopLevelWidget(); | |
2d120f83 | 117 | |
dfc54541 | 118 | wxASSERT_MSG( (parentWidget != (Widget) 0), "Could not find a suitable parent shell for dialog." ); |
2d120f83 | 119 | |
94b49b93 | 120 | Arg args[2]; |
dfc54541 | 121 | XtSetArg (args[0], XmNdefaultPosition, False); |
94b49b93 JS |
122 | XtSetArg (args[1], XmNautoUnmanage, False); |
123 | Widget dialogShell = XmCreateBulletinBoardDialog(parentWidget, (char*) (const char*) name, args, 2); | |
dfc54541 | 124 | m_mainWidget = (WXWidget) dialogShell; |
2d120f83 | 125 | |
dfc54541 JS |
126 | // We don't want margins, since there is enough elsewhere. |
127 | XtVaSetValues(dialogShell, | |
2d120f83 JS |
128 | XmNmarginHeight, 0, |
129 | XmNmarginWidth, 0, | |
130 | XmNresizePolicy, XmRESIZE_NONE, | |
131 | NULL) ; | |
132 | ||
dfc54541 JS |
133 | Widget shell = XtParent(dialogShell) ; |
134 | if (!title.IsNull()) | |
135 | { | |
136 | XmString str = XmStringCreateSimple((char*) (const char*)title); | |
137 | XtVaSetValues(dialogShell, | |
2d120f83 JS |
138 | XmNdialogTitle, str, |
139 | NULL); | |
dfc54541 JS |
140 | XmStringFree(str); |
141 | } | |
2d120f83 | 142 | |
4b5f3fe6 JS |
143 | m_windowFont = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT); |
144 | ChangeFont(FALSE); | |
2d120f83 | 145 | |
dfc54541 | 146 | wxAddWindowToTable(dialogShell, this); |
2d120f83 | 147 | |
dfc54541 JS |
148 | // Intercept CLOSE messages from the window manager |
149 | Atom WM_DELETE_WINDOW = XmInternAtom(XtDisplay(shell), "WM_DELETE_WINDOW", False); | |
2d120f83 | 150 | |
4b5f3fe6 | 151 | /* Remove and add WM_DELETE_WINDOW so ours is only handler */ |
dfc54541 JS |
152 | /* Why do we have to do this for wxDialog, but not wxFrame? */ |
153 | XmRemoveWMProtocols(shell, &WM_DELETE_WINDOW, 1); | |
154 | XmAddWMProtocols(shell, &WM_DELETE_WINDOW, 1); | |
155 | XmActivateWMProtocol(shell, WM_DELETE_WINDOW); | |
2d120f83 | 156 | |
dfc54541 JS |
157 | // Modified Steve Hammes for Motif 2.0 |
158 | #if (XmREVISION > 1 || XmVERSION > 1) | |
159 | XmAddWMProtocolCallback(shell, WM_DELETE_WINDOW, (XtCallbackProc) wxCloseDialogCallback, (XtPointer)this); | |
160 | #elif XmREVISION == 1 | |
161 | XmAddWMProtocolCallback(shell, WM_DELETE_WINDOW, (XtCallbackProc) wxCloseDialogCallback, (caddr_t)this); | |
162 | #else | |
163 | XmAddWMProtocolCallback(shell, WM_DELETE_WINDOW, (void (*)())wxCloseDialogCallback, (caddr_t)this); | |
164 | #endif | |
2d120f83 | 165 | |
dfc54541 JS |
166 | XtTranslations ptr ; |
167 | XtOverrideTranslations(dialogShell, | |
2d120f83 | 168 | ptr = XtParseTranslationTable("<Configure>: resize()")); |
dfc54541 | 169 | XtFree((char *)ptr); |
2d120f83 | 170 | |
dfc54541 | 171 | // Can't remember what this was about... but I think it's necessary. |
2d120f83 | 172 | |
dfad0599 | 173 | if (wxUSE_INVISIBLE_RESIZE) |
dfc54541 | 174 | { |
2d120f83 JS |
175 | if (pos.x > -1) |
176 | XtVaSetValues(dialogShell, XmNx, pos.x, | |
177 | NULL); | |
178 | if (pos.y > -1) | |
179 | XtVaSetValues(dialogShell, XmNy, pos.y, | |
180 | NULL); | |
181 | ||
182 | if (size.x > -1) | |
183 | XtVaSetValues(dialogShell, XmNwidth, size.x, NULL); | |
184 | if (size.y > -1) | |
185 | XtVaSetValues(dialogShell, XmNheight, size.y, NULL); | |
dfc54541 | 186 | } |
2d120f83 | 187 | |
dfc54541 JS |
188 | // This patch come from Torsten Liermann lier@lier1.muc.de |
189 | if (XmIsMotifWMRunning(shell)) | |
190 | { | |
191 | int decor = 0 ; | |
192 | if (m_windowStyle & wxRESIZE_BORDER) | |
2d120f83 | 193 | decor |= MWM_DECOR_RESIZEH ; |
dfc54541 | 194 | if (m_windowStyle & wxSYSTEM_MENU) |
2d120f83 | 195 | decor |= MWM_DECOR_MENU; |
dfc54541 JS |
196 | if ((m_windowStyle & wxCAPTION) || |
197 | (m_windowStyle & wxTINY_CAPTION_HORIZ) || | |
198 | (m_windowStyle & wxTINY_CAPTION_VERT)) | |
2d120f83 | 199 | decor |= MWM_DECOR_TITLE; |
dfc54541 | 200 | if (m_windowStyle & wxTHICK_FRAME) |
2d120f83 | 201 | decor |= MWM_DECOR_BORDER; |
dfc54541 | 202 | if (m_windowStyle & wxMINIMIZE_BOX) |
2d120f83 | 203 | decor |= MWM_DECOR_MINIMIZE; |
dfc54541 | 204 | if (m_windowStyle & wxMAXIMIZE_BOX) |
2d120f83 JS |
205 | decor |= MWM_DECOR_MAXIMIZE; |
206 | ||
dfc54541 JS |
207 | XtVaSetValues(shell,XmNmwmDecorations,decor,NULL) ; |
208 | } | |
209 | // This allows non-Motif window managers to support at least the | |
210 | // no-decorations case. | |
211 | else | |
212 | { | |
2d120f83 JS |
213 | if ((m_windowStyle & wxCAPTION) != wxCAPTION) |
214 | XtVaSetValues((Widget) shell,XmNoverrideRedirect,TRUE,NULL); | |
dfc54541 | 215 | } |
2d120f83 | 216 | |
dfc54541 | 217 | XtRealizeWidget(dialogShell); |
2d120f83 | 218 | |
dfc54541 | 219 | XtAddCallback(dialogShell,XmNunmapCallback, |
2d120f83 JS |
220 | (XtCallbackProc)wxUnmapBulletinBoard,this) ; |
221 | ||
dfc54541 JS |
222 | // Positioning of the dialog doesn't work properly unless the dialog |
223 | // is managed, so we manage without mapping to the screen. | |
224 | // To show, we map the shell (actually it's parent). | |
dfad0599 | 225 | if (!wxUSE_INVISIBLE_RESIZE) |
dfc54541 | 226 | XtVaSetValues(shell, XmNmappedWhenManaged, FALSE, NULL); |
2d120f83 | 227 | |
dfad0599 | 228 | if (!wxUSE_INVISIBLE_RESIZE) |
dfc54541 JS |
229 | { |
230 | XtManageChild(dialogShell); | |
231 | SetSize(pos.x, pos.y, size.x, size.y); | |
232 | } | |
233 | XtAddEventHandler(dialogShell,ExposureMask,FALSE, | |
2d120f83 JS |
234 | wxDialogBoxRepaintProc, (XtPointer) this); |
235 | ||
dfc54541 | 236 | XtAddEventHandler(dialogShell, |
2d120f83 JS |
237 | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | KeyPressMask, |
238 | FALSE, | |
239 | wxDialogBoxEventHandler, | |
240 | (XtPointer)this); | |
241 | ||
0d57be45 | 242 | ChangeBackgroundColour(); |
2d120f83 | 243 | |
dfc54541 | 244 | return TRUE; |
4bb6408c JS |
245 | } |
246 | ||
247 | void wxDialog::SetModal(bool flag) | |
248 | { | |
dfc54541 JS |
249 | if ( flag ) |
250 | m_windowStyle |= wxDIALOG_MODAL ; | |
251 | else | |
252 | if ( m_windowStyle & wxDIALOG_MODAL ) | |
2d120f83 JS |
253 | m_windowStyle -= wxDIALOG_MODAL ; |
254 | ||
255 | wxModelessWindows.DeleteObject(this); | |
256 | if (!flag) | |
257 | wxModelessWindows.Append(this); | |
4bb6408c JS |
258 | } |
259 | ||
260 | wxDialog::~wxDialog() | |
261 | { | |
dfc54541 | 262 | m_modalShowing = FALSE; |
dfad0599 | 263 | if (!wxUSE_INVISIBLE_RESIZE && m_mainWidget) |
dfc54541 | 264 | { |
2d120f83 | 265 | XtUnmapWidget((Widget) m_mainWidget); |
dfc54541 | 266 | } |
2d120f83 | 267 | |
4bb6408c | 268 | wxTopLevelWindows.DeleteObject(this); |
2d120f83 | 269 | |
4bb6408c | 270 | if ( (GetWindowStyleFlag() & wxDIALOG_MODAL) != wxDIALOG_MODAL ) |
2d120f83 JS |
271 | wxModelessWindows.DeleteObject(this); |
272 | ||
4bb6408c JS |
273 | // If this is the last top-level window, exit. |
274 | if (wxTheApp && (wxTopLevelWindows.Number() == 0)) | |
275 | { | |
2d120f83 JS |
276 | wxTheApp->SetTopWindow(NULL); |
277 | ||
278 | if (wxTheApp->GetExitOnFrameDelete()) | |
279 | { | |
280 | wxTheApp->ExitMainLoop(); | |
281 | } | |
4bb6408c | 282 | } |
2d120f83 | 283 | |
dfc54541 JS |
284 | // This event-flushing code used to be in wxWindow::PostDestroyChildren (wx_dialog.cpp) |
285 | // but I think this should work, if we destroy the children first. | |
286 | // Note that this might need to be done for wxFrame also. | |
287 | DestroyChildren(); | |
2d120f83 | 288 | |
b412f9be JS |
289 | // This causes a crash in e.g. the resource sample when closing |
290 | // the example dialog. TODO: Probably not necessary (?) | |
291 | #if 0 | |
dfc54541 JS |
292 | // Now process all events, because otherwise |
293 | // this might remain on the screen. | |
a4294b78 JS |
294 | Display* display; |
295 | if (m_mainWidget) | |
2d120f83 | 296 | display = XtDisplay((Widget) m_mainWidget); |
a4294b78 | 297 | else |
2d120f83 JS |
298 | display = (Display*) wxGetDisplay(); |
299 | ||
a4294b78 | 300 | XSync(display, FALSE); |
dfc54541 JS |
301 | XEvent event; |
302 | while (XtAppPending((XtAppContext) wxTheApp->GetAppContext())) { | |
2d120f83 JS |
303 | XFlush(display); |
304 | XtAppNextEvent((XtAppContext) wxTheApp->GetAppContext(), &event); | |
305 | XtDispatchEvent(&event); | |
dfc54541 | 306 | } |
b412f9be | 307 | #endif |
4bb6408c JS |
308 | } |
309 | ||
310 | // By default, pressing escape cancels the dialog | |
311 | void wxDialog::OnCharHook(wxKeyEvent& event) | |
312 | { | |
2d120f83 JS |
313 | if (event.m_keyCode == WXK_ESCAPE) |
314 | { | |
315 | // Behaviour changed in 2.0: we'll send a Cancel message | |
316 | // to the dialog instead of Close. | |
317 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); | |
318 | cancelEvent.SetEventObject( this ); | |
319 | GetEventHandler()->ProcessEvent(cancelEvent); | |
320 | ||
321 | return; | |
322 | } | |
323 | // We didn't process this event. | |
324 | event.Skip(); | |
4bb6408c JS |
325 | } |
326 | ||
327 | void wxDialog::Iconize(bool WXUNUSED(iconize)) | |
328 | { | |
2d120f83 JS |
329 | // Can't iconize a dialog in Motif, apparently |
330 | // TODO: try using the parent of m_mainShell. | |
331 | // XtVaSetValues((Widget) m_mainWidget, XmNiconic, iconize, NULL); | |
4bb6408c JS |
332 | } |
333 | ||
334 | bool wxDialog::IsIconized() const | |
335 | { | |
2d120f83 JS |
336 | /* |
337 | Boolean iconic; | |
338 | XtVaGetValues((Widget) m_mainWidget, XmNiconic, &iconic, NULL); | |
dfc54541 | 339 | |
2d120f83 JS |
340 | return iconic; |
341 | */ | |
4bb6408c JS |
342 | return FALSE; |
343 | } | |
344 | ||
dfc54541 | 345 | void wxDialog::SetSize(int x, int y, int width, int height, int sizeFlags) |
4bb6408c | 346 | { |
dfc54541 JS |
347 | XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_ANY, NULL); |
348 | wxWindow::SetSize(x, y, width, height, sizeFlags); | |
349 | XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_NONE, NULL); | |
4bb6408c JS |
350 | } |
351 | ||
dfc54541 | 352 | void wxDialog::SetClientSize(int width, int height) |
4bb6408c | 353 | { |
dfc54541 | 354 | SetSize(-1, -1, width, height); |
4bb6408c JS |
355 | } |
356 | ||
4bb6408c JS |
357 | |
358 | void wxDialog::SetTitle(const wxString& title) | |
359 | { | |
dfc54541 JS |
360 | m_dialogTitle = title; |
361 | if (!title.IsNull()) | |
362 | { | |
363 | XmString str = XmStringCreateSimple((char*) (const char*) title); | |
364 | XtVaSetValues((Widget) m_mainWidget, | |
2d120f83 JS |
365 | XmNtitle, (char*) (const char*) title, |
366 | XmNdialogTitle, str, // Roberto Cocchi | |
367 | XmNiconName, (char*) (const char*) title, | |
368 | NULL); | |
dfc54541 JS |
369 | XmStringFree(str); |
370 | } | |
4bb6408c JS |
371 | } |
372 | ||
373 | wxString wxDialog::GetTitle() const | |
374 | { | |
2d120f83 | 375 | return m_dialogTitle; |
4bb6408c JS |
376 | } |
377 | ||
378 | void wxDialog::Centre(int direction) | |
379 | { | |
2d120f83 JS |
380 | int x_offset,y_offset ; |
381 | int display_width, display_height; | |
382 | int width, height, x, y; | |
383 | wxWindow *parent = GetParent(); | |
384 | if ((direction & wxCENTER_FRAME) && parent) | |
385 | { | |
386 | parent->GetPosition(&x_offset,&y_offset) ; | |
387 | parent->GetSize(&display_width,&display_height) ; | |
388 | } | |
389 | else | |
390 | { | |
391 | wxDisplaySize(&display_width, &display_height); | |
392 | x_offset = 0 ; | |
393 | y_offset = 0 ; | |
394 | } | |
395 | ||
396 | GetSize(&width, &height); | |
397 | GetPosition(&x, &y); | |
398 | ||
399 | if (direction & wxHORIZONTAL) | |
400 | x = (int)((display_width - width)/2); | |
401 | if (direction & wxVERTICAL) | |
402 | y = (int)((display_height - height)/2); | |
403 | ||
404 | SetSize(x+x_offset, y+y_offset, width, height); | |
4bb6408c JS |
405 | } |
406 | ||
dfc54541 JS |
407 | void wxDialog::Raise() |
408 | { | |
409 | Window parent_window = XtWindow((Widget) m_mainWidget), | |
2d120f83 JS |
410 | next_parent = XtWindow((Widget) m_mainWidget), |
411 | root = RootWindowOfScreen(XtScreen((Widget) m_mainWidget)); | |
dfc54541 JS |
412 | // search for the parent that is child of ROOT, because the WM may |
413 | // reparent twice and notify only the next parent (like FVWM) | |
414 | while (next_parent != root) { | |
2d120f83 JS |
415 | Window *theChildren; unsigned int n; |
416 | parent_window = next_parent; | |
417 | XQueryTree(XtDisplay((Widget) m_mainWidget), parent_window, &root, | |
418 | &next_parent, &theChildren, &n); | |
419 | XFree(theChildren); // not needed | |
dfc54541 JS |
420 | } |
421 | XRaiseWindow(XtDisplay((Widget) m_mainWidget), parent_window); | |
422 | } | |
423 | ||
424 | void wxDialog::Lower() | |
425 | { | |
426 | Window parent_window = XtWindow((Widget) m_mainWidget), | |
2d120f83 JS |
427 | next_parent = XtWindow((Widget) m_mainWidget), |
428 | root = RootWindowOfScreen(XtScreen((Widget) m_mainWidget)); | |
dfc54541 JS |
429 | // search for the parent that is child of ROOT, because the WM may |
430 | // reparent twice and notify only the next parent (like FVWM) | |
431 | while (next_parent != root) { | |
2d120f83 JS |
432 | Window *theChildren; unsigned int n; |
433 | parent_window = next_parent; | |
434 | XQueryTree(XtDisplay((Widget) m_mainWidget), parent_window, &root, | |
435 | &next_parent, &theChildren, &n); | |
436 | XFree(theChildren); // not needed | |
dfc54541 JS |
437 | } |
438 | XLowerWindow(XtDisplay((Widget) m_mainWidget), parent_window); | |
439 | } | |
440 | ||
441 | bool wxDialog::Show(bool show) | |
442 | { | |
443 | m_isShown = show; | |
2d120f83 | 444 | |
dfc54541 JS |
445 | if (show) |
446 | { | |
dfad0599 | 447 | if (!wxUSE_INVISIBLE_RESIZE) |
2d120f83 | 448 | XtMapWidget(XtParent((Widget) m_mainWidget)); |
dfc54541 | 449 | else |
2d120f83 JS |
450 | XtManageChild((Widget) m_mainWidget) ; |
451 | ||
dfc54541 | 452 | XRaiseWindow(XtDisplay((Widget) m_mainWidget), XtWindow((Widget) m_mainWidget)); |
2d120f83 | 453 | |
dfc54541 JS |
454 | } |
455 | else | |
456 | { | |
dfad0599 | 457 | if (!wxUSE_INVISIBLE_RESIZE) |
dfc54541 JS |
458 | XtUnmapWidget(XtParent((Widget) m_mainWidget)); |
459 | else | |
460 | XtUnmanageChild((Widget) m_mainWidget) ; | |
2d120f83 JS |
461 | |
462 | XFlush(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())); | |
463 | XSync(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), FALSE); | |
dfc54541 | 464 | } |
2d120f83 | 465 | |
dfc54541 JS |
466 | return TRUE; |
467 | } | |
468 | ||
469 | // Shows a dialog modally, returning a return code | |
4bb6408c JS |
470 | int wxDialog::ShowModal() |
471 | { | |
472 | m_windowStyle |= wxDIALOG_MODAL; | |
2d120f83 | 473 | |
dfc54541 | 474 | Show(TRUE); |
2d120f83 | 475 | |
dfc54541 JS |
476 | if (m_modalShowing) |
477 | return 0; | |
2d120f83 | 478 | |
dfc54541 | 479 | wxModalShowingStack.Insert((wxObject *)TRUE); |
2d120f83 | 480 | |
dfc54541 | 481 | m_modalShowing = TRUE; |
793f619f | 482 | XtAddGrab((Widget) m_mainWidget, TRUE, FALSE); |
2d120f83 | 483 | |
dfc54541 | 484 | XEvent event; |
2d120f83 | 485 | |
dfc54541 | 486 | // Loop until we signal that the dialog should be closed |
5dcf05ae | 487 | while ((wxModalShowingStack.Number() > 0) && ((int)(wxModalShowingStack.First()->Data()) != 0)) |
dfc54541 | 488 | { |
2d120f83 JS |
489 | // XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll); |
490 | ||
8aa04e8b JS |
491 | XtAppNextEvent((XtAppContext) wxTheApp->GetAppContext(), &event); |
492 | wxTheApp->ProcessXEvent((WXEvent*) &event); | |
dfc54541 | 493 | } |
2d120f83 | 494 | |
dfc54541 JS |
495 | // Remove modal dialog flag from stack |
496 | wxNode *node = wxModalShowingStack.First(); | |
497 | if (node) | |
2d120f83 JS |
498 | delete node; |
499 | ||
dfc54541 JS |
500 | // Now process all events in case they get sent to a destroyed dialog |
501 | XSync(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), FALSE); | |
502 | while (XtAppPending((XtAppContext) wxTheApp->GetAppContext())) | |
503 | { | |
504 | XFlush(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())); | |
505 | XtAppNextEvent((XtAppContext) wxTheApp->GetAppContext(), &event); | |
2d120f83 | 506 | |
8aa04e8b | 507 | wxTheApp->ProcessXEvent((WXEvent*) &event); |
dfc54541 | 508 | } |
2d120f83 | 509 | |
dfc54541 JS |
510 | // TODO: is it safe to call this, if the dialog may have been deleted |
511 | // by now? Probably only if we're using delayed deletion of dialogs. | |
512 | return GetReturnCode(); | |
4bb6408c JS |
513 | } |
514 | ||
515 | void wxDialog::EndModal(int retCode) | |
516 | { | |
dfc54541 JS |
517 | if (!m_modalShowing) |
518 | return; | |
2d120f83 | 519 | |
dfc54541 | 520 | SetReturnCode(retCode); |
2d120f83 | 521 | |
88150e60 JS |
522 | // Strangely, we don't seem to need this now. |
523 | // XtRemoveGrab((Widget) m_mainWidget); | |
2d120f83 | 524 | |
dfc54541 | 525 | Show(FALSE); |
2d120f83 | 526 | |
dfc54541 | 527 | m_modalShowing = FALSE; |
2d120f83 | 528 | |
dfc54541 JS |
529 | wxNode *node = wxModalShowingStack.First(); |
530 | if (node) | |
2d120f83 | 531 | node->SetData((wxObject *)FALSE); |
4bb6408c JS |
532 | } |
533 | ||
534 | // Standard buttons | |
f9e02ac7 | 535 | void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event)) |
4bb6408c | 536 | { |
2d120f83 JS |
537 | if ( Validate() && TransferDataFromWindow() ) |
538 | { | |
4bb6408c JS |
539 | if ( IsModal() ) |
540 | EndModal(wxID_OK); | |
541 | else | |
542 | { | |
2d120f83 JS |
543 | SetReturnCode(wxID_OK); |
544 | this->Show(FALSE); | |
4bb6408c | 545 | } |
2d120f83 | 546 | } |
4bb6408c JS |
547 | } |
548 | ||
f9e02ac7 | 549 | void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event)) |
4bb6408c | 550 | { |
2d120f83 JS |
551 | if (Validate()) |
552 | TransferDataFromWindow(); | |
553 | // TODO probably need to disable the Apply button until things change again | |
4bb6408c JS |
554 | } |
555 | ||
f9e02ac7 | 556 | void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) |
4bb6408c JS |
557 | { |
558 | if ( IsModal() ) | |
559 | EndModal(wxID_CANCEL); | |
560 | else | |
561 | { | |
562 | SetReturnCode(wxID_CANCEL); | |
2d120f83 | 563 | this->Show(FALSE); |
4bb6408c JS |
564 | } |
565 | } | |
566 | ||
567 | bool wxDialog::OnClose() | |
568 | { | |
2d120f83 | 569 | // Behaviour changed in 2.0: we'll send a Cancel message by default, |
4bb6408c JS |
570 | // which may close the dialog. |
571 | // Check for looping if the Cancel event handler calls Close() | |
2d120f83 | 572 | |
4bb6408c | 573 | static wxList closing; |
2d120f83 | 574 | |
4bb6408c JS |
575 | if ( closing.Member(this) ) |
576 | return FALSE; | |
2d120f83 | 577 | |
4bb6408c | 578 | closing.Append(this); |
2d120f83 JS |
579 | |
580 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); | |
581 | cancelEvent.SetEventObject( this ); | |
582 | GetEventHandler()->ProcessEvent(cancelEvent); | |
583 | ||
4bb6408c | 584 | closing.DeleteObject(this); |
2d120f83 JS |
585 | |
586 | return FALSE; | |
4bb6408c JS |
587 | } |
588 | ||
589 | void wxDialog::OnCloseWindow(wxCloseEvent& event) | |
590 | { | |
591 | // Compatibility | |
592 | if ( GetEventHandler()->OnClose() || event.GetForce()) | |
593 | { | |
594 | this->Destroy(); | |
595 | } | |
596 | } | |
597 | ||
598 | // Destroy the window (delayed, if a managed window) | |
599 | bool wxDialog::Destroy() | |
600 | { | |
2d120f83 JS |
601 | if (!wxPendingDelete.Member(this)) |
602 | wxPendingDelete.Append(this); | |
603 | return TRUE; | |
4bb6408c JS |
604 | } |
605 | ||
f9e02ac7 | 606 | void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) |
4bb6408c | 607 | { |
2d120f83 JS |
608 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE)); |
609 | Refresh(); | |
4bb6408c JS |
610 | } |
611 | ||
612 | void wxDialog::Fit() | |
613 | { | |
9838df2c | 614 | wxWindow::Fit(); |
4bb6408c | 615 | } |
dfc54541 JS |
616 | |
617 | // Handle a close event from the window manager | |
f9e02ac7 | 618 | static void wxCloseDialogCallback( Widget WXUNUSED(widget), XtPointer client_data, |
2d120f83 | 619 | XmAnyCallbackStruct *WXUNUSED(cbs)) |
dfc54541 | 620 | { |
2d120f83 JS |
621 | wxDialog *dialog = (wxDialog *)client_data; |
622 | wxCloseEvent closeEvent(wxEVT_CLOSE_WINDOW, dialog->GetId()); | |
623 | closeEvent.SetEventObject(dialog); | |
624 | ||
625 | // May delete the dialog (with delayed deletion) | |
626 | dialog->GetEventHandler()->ProcessEvent(closeEvent); | |
dfc54541 JS |
627 | } |
628 | ||
629 | // TODO: Preferably, we should have a universal repaint proc. | |
630 | // Meanwhile, use a special one for dialogs. | |
f9e02ac7 | 631 | static void wxDialogBoxRepaintProc(Widget w, XtPointer WXUNUSED(c_data), XEvent *event, char *) |
55acd85e | 632 | { |
2d120f83 JS |
633 | Window window; |
634 | Display *display; | |
635 | ||
636 | wxWindow* win = (wxWindow *)wxWidgetHashTable->Get((long)w); | |
637 | if (!win) | |
638 | return; | |
639 | ||
640 | switch(event -> type) | |
641 | { | |
642 | case Expose : | |
643 | { | |
644 | window = (Window) win -> GetXWindow(); | |
645 | display = (Display *) win -> GetXDisplay(); | |
646 | ||
647 | wxRect* rect = new wxRect(event->xexpose.x, event->xexpose.y, | |
648 | event->xexpose.width, event->xexpose.height); | |
649 | win->m_updateRects.Append((wxObject*) rect); | |
650 | ||
651 | if (event -> xexpose.count == 0) | |
652 | { | |
653 | wxPaintEvent event(win->GetId()); | |
654 | event.SetEventObject(win); | |
655 | win->GetEventHandler()->ProcessEvent(event); | |
656 | ||
657 | win->ClearUpdateRects(); | |
658 | } | |
659 | break; | |
660 | } | |
661 | default : | |
662 | { | |
663 | cout << "\n\nNew Event ! is = " << event -> type << "\n"; | |
664 | break; | |
665 | } | |
666 | } | |
55acd85e | 667 | } |
dfc54541 JS |
668 | |
669 | static void wxDialogBoxEventHandler (Widget wid, | |
2d120f83 JS |
670 | XtPointer WXUNUSED(client_data), |
671 | XEvent* event, | |
672 | Boolean *continueToDispatch) | |
673 | { | |
674 | wxDialog *dialog = (wxDialog *)wxWidgetHashTable->Get((long)wid); | |
675 | if (dialog) | |
dfc54541 | 676 | { |
2d120f83 JS |
677 | wxMouseEvent wxevent(wxEVT_NULL); |
678 | if (wxTranslateMouseEvent(wxevent, dialog, wid, event)) | |
dfc54541 | 679 | { |
2d120f83 JS |
680 | wxevent.SetEventObject(dialog); |
681 | wxevent.SetId(dialog->GetId()); | |
682 | dialog->GetEventHandler()->ProcessEvent(wxevent); | |
dfc54541 JS |
683 | } |
684 | else | |
685 | { | |
2d120f83 JS |
686 | // An attempt to implement OnCharHook by calling OnCharHook first; |
687 | // if this returns TRUE, set continueToDispatch to False | |
688 | // (don't continue processing). | |
689 | // Otherwise set it to True and call OnChar. | |
c75e6695 | 690 | wxKeyEvent keyEvent(wxEVT_CHAR); |
2d120f83 JS |
691 | if (wxTranslateKeyEvent(keyEvent, dialog, wid, event)) |
692 | { | |
693 | keyEvent.SetEventObject(dialog); | |
694 | keyEvent.SetId(dialog->GetId()); | |
695 | keyEvent.SetEventType(wxEVT_CHAR_HOOK); | |
696 | if (dialog->GetEventHandler()->ProcessEvent(keyEvent)) | |
697 | { | |
698 | *continueToDispatch = False; | |
699 | return; | |
700 | } | |
701 | else | |
702 | { | |
703 | keyEvent.SetEventType(wxEVT_CHAR); | |
704 | dialog->GetEventHandler()->ProcessEvent(keyEvent); | |
705 | } | |
706 | } | |
707 | } | |
dfc54541 | 708 | } |
2d120f83 | 709 | *continueToDispatch = True; |
dfc54541 JS |
710 | } |
711 | ||
f9e02ac7 | 712 | static void wxUnmapBulletinBoard(Widget WXUNUSED(dialog), wxDialog *WXUNUSED(client), XtPointer WXUNUSED(call) ) |
dfc54541 | 713 | { |
2d120f83 JS |
714 | /* This gets called when the dialog is being shown, which |
715 | * defeats modal showing. | |
716 | client->m_modalShowing = FALSE ; | |
717 | client->m_isShown = FALSE; | |
718 | */ | |
dfc54541 | 719 | } |
0d57be45 | 720 | |
94b49b93 | 721 | void wxDialog::ChangeFont(bool keepOriginalSize) |
0d57be45 | 722 | { |
94b49b93 | 723 | wxWindow::ChangeFont(keepOriginalSize); |
0d57be45 JS |
724 | } |
725 | ||
726 | void wxDialog::ChangeBackgroundColour() | |
727 | { | |
94b49b93 JS |
728 | if (GetMainWidget()) |
729 | DoChangeBackgroundColour(GetMainWidget(), m_backgroundColour); | |
0d57be45 JS |
730 | } |
731 | ||
732 | void wxDialog::ChangeForegroundColour() | |
733 | { | |
94b49b93 JS |
734 | if (GetMainWidget()) |
735 | DoChangeForegroundColour(GetMainWidget(), m_foregroundColour); | |
0d57be45 JS |
736 | } |
737 |