Add wxTEST_DIALOG for testing of modal dialogs.
[wxWidgets.git] / src / motif / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #include "wx/dialog.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/app.h"
19 #include "wx/utils.h"
20 #include "wx/settings.h"
21 #endif
22
23 #include "wx/evtloop.h"
24 #include "wx/testing.h"
25
26 #ifdef __VMS__
27 #pragma message disable nosimpint
28 #endif
29 #include <Xm/Xm.h>
30
31 #include <X11/Shell.h>
32 #if XmVersion >= 1002
33 #include <Xm/XmAll.h>
34 #endif
35 #include <Xm/MwmUtil.h>
36 #include <Xm/Label.h>
37 #include <Xm/BulletinB.h>
38 #include <Xm/Frame.h>
39 #include <Xm/Text.h>
40 #include <Xm/DialogS.h>
41 #include <Xm/FileSB.h>
42 #include <Xm/RowColumn.h>
43 #include <Xm/LabelG.h>
44 #include <Xm/AtomMgr.h>
45 #if XmVersion > 1000
46 #include <Xm/Protocols.h>
47 #endif
48 #ifdef __VMS__
49 #pragma message enable nosimpint
50 #endif
51
52 #include "wx/motif/private.h"
53
54 // A stack of modal_showing flags, since we can't rely
55 // on accessing wxDialog::m_modalShowing within
56 // wxDialog::Show in case a callback has deleted the wxDialog.
57 // static wxList wxModalShowingStack;
58
59 // Lists to keep track of windows, so we can disable/enable them
60 // for modal dialogs
61 wxList wxModalDialogs;
62 extern wxList wxModelessWindows; // Frames and modeless dialogs
63
64 #define wxUSE_INVISIBLE_RESIZE 1
65
66 wxDialog::wxDialog()
67 {
68 m_modalShowing = false;
69 m_eventLoop = NULL;
70 }
71
72 bool wxDialog::Create(wxWindow *parent, wxWindowID id,
73 const wxString& title,
74 const wxPoint& pos,
75 const wxSize& size,
76 long style,
77 const wxString& name)
78 {
79 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
80
81 if( !wxTopLevelWindow::Create( parent, id, title, pos, size, style,
82 name ) )
83 return false;
84
85 m_modalShowing = false;
86 m_eventLoop = NULL;
87
88 Widget dialogShell = (Widget) m_mainWidget;
89
90 SetTitle( title );
91
92 // Can't remember what this was about... but I think it's necessary.
93 #if wxUSE_INVISIBLE_RESIZE
94 if (pos.x > -1)
95 XtVaSetValues(dialogShell, XmNx, pos.x,
96 NULL);
97 if (pos.y > -1)
98 XtVaSetValues(dialogShell, XmNy, pos.y,
99 NULL);
100
101 if (size.x > -1)
102 XtVaSetValues(dialogShell, XmNwidth, size.x, NULL);
103 if (size.y > -1)
104 XtVaSetValues(dialogShell, XmNheight, size.y, NULL);
105 #endif
106
107 // Positioning of the dialog doesn't work properly unless the dialog
108 // is managed, so we manage without mapping to the screen.
109 // To show, we map the shell (actually it's parent).
110 #if !wxUSE_INVISIBLE_RESIZE
111 Widget shell = XtParent(dialogShell) ;
112 XtVaSetValues(shell, XmNmappedWhenManaged, False, NULL);
113 #endif
114
115 #if !wxUSE_INVISIBLE_RESIZE
116 XtManageChild(dialogShell);
117 SetSize(pos.x, pos.y, size.x, size.y);
118 #endif
119
120 XtAddEventHandler(dialogShell,ExposureMask,False,
121 wxUniversalRepaintProc, (XtPointer) this);
122
123 PostCreation();
124
125 return true;
126 }
127
128 bool wxDialog::XmDoCreateTLW(wxWindow* parent,
129 wxWindowID WXUNUSED(id),
130 const wxString& WXUNUSED(title),
131 const wxPoint& WXUNUSED(pos),
132 const wxSize& WXUNUSED(size),
133 long WXUNUSED(style),
134 const wxString& name)
135 {
136 Widget parentWidget = (Widget) 0;
137 if( parent )
138 parentWidget = (Widget) parent->GetTopWidget();
139 if( !parent )
140 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
141
142 wxASSERT_MSG( (parentWidget != (Widget) 0),
143 "Could not find a suitable parent shell for dialog." );
144
145 Arg args[2];
146 XtSetArg (args[0], XmNdefaultPosition, False);
147 XtSetArg (args[1], XmNautoUnmanage, False);
148 Widget dialogShell =
149 XmCreateBulletinBoardDialog( parentWidget,
150 name.char_str(),
151 args, 2);
152 m_mainWidget = (WXWidget) dialogShell;
153
154 // We don't want margins, since there is enough elsewhere.
155 XtVaSetValues( dialogShell,
156 XmNmarginHeight, 0,
157 XmNmarginWidth, 0,
158 XmNresizePolicy, XmRESIZE_NONE,
159 NULL ) ;
160
161 XtTranslations ptr ;
162 XtOverrideTranslations(dialogShell,
163 ptr = XtParseTranslationTable("<Configure>: resize()"));
164 XtFree((char *)ptr);
165
166 XtRealizeWidget(dialogShell);
167
168 wxAddWindowToTable( (Widget)m_mainWidget, this );
169
170 return true;
171 }
172
173 void wxDialog::SetModal(bool flag)
174 {
175 if ( flag )
176 wxModelessWindows.DeleteObject(this);
177 else
178 wxModelessWindows.Append(this);
179 }
180
181 wxDialog::~wxDialog()
182 {
183 SendDestroyEvent();
184
185 // if the dialog is modal, this will end its event loop
186 Show(false);
187
188 delete m_eventLoop;
189
190 if (m_mainWidget)
191 {
192 XtRemoveEventHandler((Widget) m_mainWidget, ExposureMask, False,
193 wxUniversalRepaintProc, (XtPointer) this);
194 }
195
196 m_modalShowing = false;
197
198 #if !wxUSE_INVISIBLE_RESIZE
199 if (m_mainWidget)
200 {
201 XtUnmapWidget((Widget) m_mainWidget);
202 }
203 #endif
204
205 PreDestroy();
206
207 if ( m_mainWidget )
208 {
209 wxDeleteWindowFromTable( (Widget)m_mainWidget );
210 XtDestroyWidget( (Widget)m_mainWidget );
211 }
212 }
213
214 void wxDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags)
215 {
216 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
217 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
218 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
219 }
220
221 void wxDialog::DoSetClientSize(int width, int height)
222 {
223 wxWindow::SetSize(-1, -1, width, height);
224 }
225
226 void wxDialog::SetTitle(const wxString& title)
227 {
228 wxTopLevelWindow::SetTitle( title );
229
230 if( !title.empty() )
231 {
232 wxXmString str( title );
233 XtVaSetValues( (Widget)m_mainWidget,
234 XmNtitle, (const char*)title.mb_str(),
235 XmNdialogTitle, str(),
236 XmNiconName, (const char*)title.mb_str(),
237 NULL );
238 }
239 }
240
241 bool wxDialog::Show( bool show )
242 {
243 if( !wxWindowBase::Show( show ) )
244 return false;
245
246 if ( !show && IsModal() )
247 EndModal(wxID_CANCEL);
248
249 m_isShown = show;
250
251 if (show)
252 {
253 if (CanDoLayoutAdaptation())
254 DoLayoutAdaptation();
255
256 // this usually will result in TransferDataToWindow() being called
257 // which will change the controls values so do it before showing as
258 // otherwise we could have some flicker
259 InitDialog();
260 }
261
262 if (show)
263 {
264 #if !wxUSE_INVISIBLE_RESIZE
265 XtMapWidget(XtParent((Widget) m_mainWidget));
266 #else
267 XtManageChild((Widget)m_mainWidget) ;
268 #endif
269
270 XRaiseWindow( XtDisplay( (Widget)m_mainWidget ),
271 XtWindow( (Widget)m_mainWidget) );
272
273 }
274 else
275 {
276 #if !wxUSE_INVISIBLE_RESIZE
277 XtUnmapWidget(XtParent((Widget) m_mainWidget));
278 #else
279 XtUnmanageChild((Widget)m_mainWidget) ;
280 #endif
281
282 XFlush(XtDisplay((Widget)m_mainWidget));
283 XSync(XtDisplay((Widget)m_mainWidget), False);
284 }
285
286 return true;
287 }
288
289 // Shows a dialog modally, returning a return code
290 int wxDialog::ShowModal()
291 {
292 WX_TESTING_SHOW_MODAL_HOOK();
293
294 Show(true);
295
296 // after the event loop ran, the widget might already have been destroyed
297 WXDisplay* display = (WXDisplay*)XtDisplay( (Widget)m_mainWidget );
298
299 if (m_modalShowing)
300 return 0;
301 m_eventLoop = new wxEventLoop;
302
303 m_modalShowing = true;
304 XtAddGrab((Widget) m_mainWidget, True, False);
305
306 m_eventLoop->Run();
307
308 // Now process all events in case they get sent to a destroyed dialog
309 wxFlushEvents( display );
310
311 wxDELETE(m_eventLoop);
312
313 // TODO: is it safe to call this, if the dialog may have been deleted
314 // by now? Probably only if we're using delayed deletion of dialogs.
315 return GetReturnCode();
316 }
317
318 void wxDialog::EndModal(int retCode)
319 {
320 if (!m_modalShowing)
321 return;
322
323 SetReturnCode(retCode);
324
325 // Strangely, we don't seem to need this now.
326 // XtRemoveGrab((Widget) m_mainWidget);
327
328 Show(false);
329
330 m_modalShowing = false;
331 m_eventLoop->Exit();
332
333 SetModal(false);
334 }
335
336 // Destroy the window (delayed, if a managed window)
337 bool wxDialog::Destroy()
338 {
339 if (!wxPendingDelete.Member(this))
340 wxPendingDelete.Append(this);
341 return true;
342 }
343
344 void wxDialog::ChangeFont(bool keepOriginalSize)
345 {
346 wxWindow::ChangeFont(keepOriginalSize);
347 }
348
349 void wxDialog::ChangeBackgroundColour()
350 {
351 if (GetMainWidget())
352 wxDoChangeBackgroundColour(GetMainWidget(), m_backgroundColour);
353 }
354
355 void wxDialog::ChangeForegroundColour()
356 {
357 if (GetMainWidget())
358 wxDoChangeForegroundColour(GetMainWidget(), m_foregroundColour);
359 }