IsModal() now has the newer meaning (equivalent of old IsModalShowing())
[wxWidgets.git] / src / motif / dialog.cpp
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 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __VMS
20 #define XtDisplay XTDISPLAY
21 #define XtWindow XTWINDOW
22 #define XtParent XTPARENT
23 #define XtScreen XTSCREEN
24 #endif
25
26 #include "wx/dialog.h"
27 #include "wx/utils.h"
28 #include "wx/app.h"
29 #include "wx/settings.h"
30 #include "wx/evtloop.h"
31
32 #ifdef __VMS__
33 #pragma message disable nosimpint
34 #endif
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
54 #ifdef __VMS__
55 #pragma message enable nosimpint
56 #endif
57
58 #include "wx/motif/private.h"
59
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.
63 // static wxList wxModalShowingStack;
64
65 // Lists to keep track of windows, so we can disable/enable them
66 // for modal dialogs
67 wxList wxModalDialogs;
68 extern wxList wxModelessWindows; // Frames and modeless dialogs
69 extern wxList wxPendingDelete;
70
71 #define wxUSE_INVISIBLE_RESIZE 1
72
73 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
74
75 BEGIN_EVENT_TABLE(wxDialog, wxTopLevelWindow)
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)
82 END_EVENT_TABLE()
83
84
85 wxDialog::wxDialog()
86 {
87 m_modalShowing = false;
88 m_eventLoop = NULL;
89 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
90 }
91
92 bool wxDialog::Create(wxWindow *parent, wxWindowID id,
93 const wxString& title,
94 const wxPoint& pos,
95 const wxSize& size,
96 long style,
97 const wxString& name)
98 {
99 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
100
101 if( !wxTopLevelWindow::Create( parent, id, title, pos, size, style,
102 name ) )
103 return false;
104
105 m_modalShowing = false;
106 m_eventLoop = NULL;
107
108 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
109 m_foregroundColour = *wxBLACK;
110
111 Widget dialogShell = (Widget) m_mainWidget;
112 Widget shell = XtParent(dialogShell) ;
113
114 SetTitle( title );
115
116 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
117 ChangeFont(false);
118
119 // Can't remember what this was about... but I think it's necessary.
120 if (wxUSE_INVISIBLE_RESIZE)
121 {
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);
128
129 if (size.x > -1)
130 XtVaSetValues(dialogShell, XmNwidth, size.x, NULL);
131 if (size.y > -1)
132 XtVaSetValues(dialogShell, XmNheight, size.y, NULL);
133 }
134
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).
138 if (!wxUSE_INVISIBLE_RESIZE)
139 XtVaSetValues(shell, XmNmappedWhenManaged, False, NULL);
140
141 if (!wxUSE_INVISIBLE_RESIZE)
142 {
143 XtManageChild(dialogShell);
144 SetSize(pos.x, pos.y, size.x, size.y);
145 }
146 XtAddEventHandler(dialogShell,ExposureMask,False,
147 wxUniversalRepaintProc, (XtPointer) this);
148
149 ChangeBackgroundColour();
150
151 return true;
152 }
153
154 bool wxDialog::DoCreate(wxWindow* parent,
155 wxWindowID id,
156 const wxString& title,
157 const wxPoint& pos,
158 const wxSize& size,
159 long style,
160 const wxString& name)
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 =
175 XmCreateBulletinBoardDialog( parentWidget,
176 wxConstCast(name.c_str(), char),
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
196 return true;
197 }
198
199 void wxDialog::SetModal(bool flag)
200 {
201 #ifdef __VMS
202 #pragma message disable codcauunr
203 #endif
204 if ( flag )
205 m_windowStyle |= wxDIALOG_MODAL ;
206 else
207 if ( m_windowStyle & wxDIALOG_MODAL )
208 m_windowStyle -= wxDIALOG_MODAL ;
209
210 wxModelessWindows.DeleteObject(this);
211 if (!flag)
212 wxModelessWindows.Append(this);
213 #ifdef __VMS
214 #pragma message enable codcauunr
215 #endif
216 }
217
218 wxDialog::~wxDialog()
219 {
220 m_isBeingDeleted = true;
221
222 delete m_eventLoop;
223
224 if (m_mainWidget)
225 {
226 XtRemoveEventHandler((Widget) m_mainWidget, ExposureMask, False,
227 wxUniversalRepaintProc, (XtPointer) this);
228 }
229
230 m_modalShowing = false;
231 if (!wxUSE_INVISIBLE_RESIZE && m_mainWidget)
232 {
233 XtUnmapWidget((Widget) m_mainWidget);
234 }
235
236 PreDestroy();
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( !wxWindowBase::Show( show ) )
292 return false;
293
294 m_isShown = show;
295
296 if (show)
297 {
298 // this usually will result in TransferDataToWindow() being called
299 // which will change the controls values so do it before showing as
300 // otherwise we could have some flicker
301 InitDialog();
302 }
303
304 if (show)
305 {
306 if (!wxUSE_INVISIBLE_RESIZE)
307 XtMapWidget(XtParent((Widget) m_mainWidget));
308 else
309 XtManageChild((Widget)m_mainWidget) ;
310
311 XRaiseWindow( XtDisplay( (Widget)m_mainWidget ),
312 XtWindow( (Widget)m_mainWidget) );
313
314 }
315 else
316 {
317 if (!wxUSE_INVISIBLE_RESIZE)
318 XtUnmapWidget(XtParent((Widget) m_mainWidget));
319 else
320 XtUnmanageChild((Widget)m_mainWidget) ;
321
322 XFlush(XtDisplay((Widget)m_mainWidget));
323 XSync(XtDisplay((Widget)m_mainWidget), False);
324 }
325
326 return true;
327 }
328
329 // Shows a dialog modally, returning a return code
330 int wxDialog::ShowModal()
331 {
332 m_windowStyle |= wxDIALOG_MODAL;
333
334 Show(true);
335
336 // after the event loop ran, the widget might already have been destroyed
337 WXDisplay* display = (WXDisplay*)XtDisplay( (Widget)m_mainWidget );
338
339 if (m_modalShowing)
340 return 0;
341 m_eventLoop = new wxEventLoop;
342
343 m_modalShowing = true;
344 XtAddGrab((Widget) m_mainWidget, True, False);
345
346 m_eventLoop->Run();
347
348 // Now process all events in case they get sent to a destroyed dialog
349 wxFlushEvents( display );
350
351 delete m_eventLoop;
352 m_eventLoop = NULL;
353
354 // TODO: is it safe to call this, if the dialog may have been deleted
355 // by now? Probably only if we're using delayed deletion of dialogs.
356 return GetReturnCode();
357 }
358
359 void wxDialog::EndModal(int retCode)
360 {
361 if (!m_modalShowing)
362 return;
363
364 SetReturnCode(retCode);
365
366 // Strangely, we don't seem to need this now.
367 // XtRemoveGrab((Widget) m_mainWidget);
368
369 Show(false);
370
371 m_modalShowing = false;
372 m_eventLoop->Exit();
373
374 SetModal(false);
375 }
376
377 // Standard buttons
378 void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
379 {
380 if ( Validate() && TransferDataFromWindow() )
381 {
382 if ( IsModal() )
383 EndModal(wxID_OK);
384 else
385 {
386 SetReturnCode(wxID_OK);
387 this->Show(false);
388 }
389 }
390 }
391
392 void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
393 {
394 if (Validate())
395 TransferDataFromWindow();
396 // TODO probably need to disable the Apply button until things change again
397 }
398
399 void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
400 {
401 if ( IsModal() )
402 EndModal(wxID_CANCEL);
403 else
404 {
405 SetReturnCode(wxID_CANCEL);
406 this->Show(false);
407 }
408 }
409
410 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
411 {
412 // We'll send a Cancel message by default,
413 // which may close the dialog.
414 // Check for looping if the Cancel event handler calls Close().
415
416 // Note that if a cancel button and handler aren't present in the dialog,
417 // nothing will happen when you close the dialog via the window manager, or
418 // via Close().
419 // We wouldn't want to destroy the dialog by default, since the dialog may have been
420 // created on the stack.
421 // However, this does mean that calling dialog->Close() won't delete the dialog
422 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
423 // sure to destroy the dialog.
424 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
425
426 static wxList closing;
427
428 if ( closing.Member(this) )
429 return;
430
431 closing.Append(this);
432
433 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
434 cancelEvent.SetEventObject( this );
435 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
436
437 closing.DeleteObject(this);
438 }
439
440 // Destroy the window (delayed, if a managed window)
441 bool wxDialog::Destroy()
442 {
443 if (!wxPendingDelete.Member(this))
444 wxPendingDelete.Append(this);
445 return true;
446 }
447
448 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
449 {
450 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
451 Refresh();
452 }
453
454 void wxDialog::ChangeFont(bool keepOriginalSize)
455 {
456 wxWindow::ChangeFont(keepOriginalSize);
457 }
458
459 void wxDialog::ChangeBackgroundColour()
460 {
461 if (GetMainWidget())
462 wxDoChangeBackgroundColour(GetMainWidget(), m_backgroundColour);
463 }
464
465 void wxDialog::ChangeForegroundColour()
466 {
467 if (GetMainWidget())
468 wxDoChangeForegroundColour(GetMainWidget(), m_foregroundColour);
469 }