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