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