]> git.saurik.com Git - wxWidgets.git/blob - src/motif/dialog.cpp
centralized Esc key handling for closing the dialogs in wxDialogBase:
[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 #ifdef __VMS
16 #define XtDisplay XTDISPLAY
17 #define XtWindow XTWINDOW
18 #define XtParent XTPARENT
19 #define XtScreen XTSCREEN
20 #endif
21
22 #include "wx/dialog.h"
23
24 #ifndef WX_PRECOMP
25 #include "wx/app.h"
26 #include "wx/utils.h"
27 #include "wx/settings.h"
28 #endif
29
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
70 #define wxUSE_INVISIBLE_RESIZE 1
71
72 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
73
74 BEGIN_EVENT_TABLE(wxDialog, wxTopLevelWindow)
75 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
76 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
77 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
78 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
79 EVT_CLOSE(wxDialog::OnCloseWindow)
80 END_EVENT_TABLE()
81
82
83 wxDialog::wxDialog()
84 {
85 m_modalShowing = false;
86 m_eventLoop = NULL;
87 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
88 }
89
90 bool wxDialog::Create(wxWindow *parent, wxWindowID id,
91 const wxString& title,
92 const wxPoint& pos,
93 const wxSize& size,
94 long style,
95 const wxString& name)
96 {
97 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
98
99 if( !wxTopLevelWindow::Create( parent, id, title, pos, size, style,
100 name ) )
101 return false;
102
103 m_modalShowing = false;
104 m_eventLoop = NULL;
105
106 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
107 m_foregroundColour = *wxBLACK;
108
109 Widget dialogShell = (Widget) m_mainWidget;
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 if (pos.x > -1)
119 XtVaSetValues(dialogShell, XmNx, pos.x,
120 NULL);
121 if (pos.y > -1)
122 XtVaSetValues(dialogShell, XmNy, pos.y,
123 NULL);
124
125 if (size.x > -1)
126 XtVaSetValues(dialogShell, XmNwidth, size.x, NULL);
127 if (size.y > -1)
128 XtVaSetValues(dialogShell, XmNheight, size.y, NULL);
129 #endif
130
131 // Positioning of the dialog doesn't work properly unless the dialog
132 // is managed, so we manage without mapping to the screen.
133 // To show, we map the shell (actually it's parent).
134 #if !wxUSE_INVISIBLE_RESIZE
135 Widget shell = XtParent(dialogShell) ;
136 XtVaSetValues(shell, XmNmappedWhenManaged, False, NULL);
137 #endif
138
139 #if !wxUSE_INVISIBLE_RESIZE
140 XtManageChild(dialogShell);
141 SetSize(pos.x, pos.y, size.x, size.y);
142 #endif
143
144 XtAddEventHandler(dialogShell,ExposureMask,False,
145 wxUniversalRepaintProc, (XtPointer) this);
146
147 ChangeBackgroundColour();
148
149 return true;
150 }
151
152 bool wxDialog::XmDoCreateTLW(wxWindow* parent,
153 wxWindowID WXUNUSED(id),
154 const wxString& WXUNUSED(title),
155 const wxPoint& WXUNUSED(pos),
156 const wxSize& WXUNUSED(size),
157 long WXUNUSED(style),
158 const wxString& name)
159 {
160 Widget parentWidget = (Widget) 0;
161 if( parent )
162 parentWidget = (Widget) parent->GetTopWidget();
163 if( !parent )
164 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
165
166 wxASSERT_MSG( (parentWidget != (Widget) 0),
167 "Could not find a suitable parent shell for dialog." );
168
169 Arg args[2];
170 XtSetArg (args[0], XmNdefaultPosition, False);
171 XtSetArg (args[1], XmNautoUnmanage, False);
172 Widget dialogShell =
173 XmCreateBulletinBoardDialog( parentWidget,
174 wxConstCast(name.c_str(), char),
175 args, 2);
176 m_mainWidget = (WXWidget) dialogShell;
177
178 // We don't want margins, since there is enough elsewhere.
179 XtVaSetValues( dialogShell,
180 XmNmarginHeight, 0,
181 XmNmarginWidth, 0,
182 XmNresizePolicy, XmRESIZE_NONE,
183 NULL ) ;
184
185 XtTranslations ptr ;
186 XtOverrideTranslations(dialogShell,
187 ptr = XtParseTranslationTable("<Configure>: resize()"));
188 XtFree((char *)ptr);
189
190 XtRealizeWidget(dialogShell);
191
192 wxAddWindowToTable( (Widget)m_mainWidget, this );
193
194 return true;
195 }
196
197 void wxDialog::SetModal(bool flag)
198 {
199 if ( flag )
200 wxModelessWindows.DeleteObject(this);
201 else
202 wxModelessWindows.Append(this);
203 }
204
205 wxDialog::~wxDialog()
206 {
207 m_isBeingDeleted = true;
208
209 delete m_eventLoop;
210
211 if (m_mainWidget)
212 {
213 XtRemoveEventHandler((Widget) m_mainWidget, ExposureMask, False,
214 wxUniversalRepaintProc, (XtPointer) this);
215 }
216
217 m_modalShowing = false;
218
219 #if !wxUSE_INVISIBLE_RESIZE
220 if (m_mainWidget)
221 {
222 XtUnmapWidget((Widget) m_mainWidget);
223 }
224 #endif
225
226 PreDestroy();
227
228 if ( m_mainWidget )
229 {
230 wxDeleteWindowFromTable( (Widget)m_mainWidget );
231 XtDestroyWidget( (Widget)m_mainWidget );
232 }
233 }
234
235 void wxDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags)
236 {
237 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
238 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
239 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
240 }
241
242 void wxDialog::DoSetClientSize(int width, int height)
243 {
244 wxWindow::SetSize(-1, -1, width, height);
245 }
246
247 void wxDialog::SetTitle(const wxString& title)
248 {
249 wxTopLevelWindow::SetTitle( title );
250
251 if( !title.empty() )
252 {
253 wxXmString str( title );
254 XtVaSetValues( (Widget)m_mainWidget,
255 XmNtitle, title.c_str(),
256 XmNdialogTitle, str(), // Roberto Cocchi
257 XmNiconName, title.c_str(),
258 NULL );
259 }
260 }
261
262 bool wxDialog::Show( bool show )
263 {
264 if( !wxWindowBase::Show( show ) )
265 return false;
266
267 m_isShown = show;
268
269 if (show)
270 {
271 // this usually will result in TransferDataToWindow() being called
272 // which will change the controls values so do it before showing as
273 // otherwise we could have some flicker
274 InitDialog();
275 }
276
277 if (show)
278 {
279 #if !wxUSE_INVISIBLE_RESIZE
280 XtMapWidget(XtParent((Widget) m_mainWidget));
281 #else
282 XtManageChild((Widget)m_mainWidget) ;
283 #endif
284
285 XRaiseWindow( XtDisplay( (Widget)m_mainWidget ),
286 XtWindow( (Widget)m_mainWidget) );
287
288 }
289 else
290 {
291 #if !wxUSE_INVISIBLE_RESIZE
292 XtUnmapWidget(XtParent((Widget) m_mainWidget));
293 #else
294 XtUnmanageChild((Widget)m_mainWidget) ;
295 #endif
296
297 XFlush(XtDisplay((Widget)m_mainWidget));
298 XSync(XtDisplay((Widget)m_mainWidget), False);
299 }
300
301 return true;
302 }
303
304 // Shows a dialog modally, returning a return code
305 int wxDialog::ShowModal()
306 {
307 Show(true);
308
309 // after the event loop ran, the widget might already have been destroyed
310 WXDisplay* display = (WXDisplay*)XtDisplay( (Widget)m_mainWidget );
311
312 if (m_modalShowing)
313 return 0;
314 m_eventLoop = new wxEventLoop;
315
316 m_modalShowing = true;
317 XtAddGrab((Widget) m_mainWidget, True, False);
318
319 m_eventLoop->Run();
320
321 // Now process all events in case they get sent to a destroyed dialog
322 wxFlushEvents( display );
323
324 delete m_eventLoop;
325 m_eventLoop = NULL;
326
327 // TODO: is it safe to call this, if the dialog may have been deleted
328 // by now? Probably only if we're using delayed deletion of dialogs.
329 return GetReturnCode();
330 }
331
332 void wxDialog::EndModal(int retCode)
333 {
334 if (!m_modalShowing)
335 return;
336
337 SetReturnCode(retCode);
338
339 // Strangely, we don't seem to need this now.
340 // XtRemoveGrab((Widget) m_mainWidget);
341
342 Show(false);
343
344 m_modalShowing = false;
345 m_eventLoop->Exit();
346
347 SetModal(false);
348 }
349
350 // Standard buttons
351 void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
352 {
353 if ( Validate() && TransferDataFromWindow() )
354 {
355 if ( IsModal() )
356 EndModal(wxID_OK);
357 else
358 {
359 SetReturnCode(wxID_OK);
360 this->Show(false);
361 }
362 }
363 }
364
365 void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
366 {
367 if (Validate())
368 TransferDataFromWindow();
369 // TODO probably need to disable the Apply button until things change again
370 }
371
372 void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
373 {
374 if ( IsModal() )
375 EndModal(wxID_CANCEL);
376 else
377 {
378 SetReturnCode(wxID_CANCEL);
379 this->Show(false);
380 }
381 }
382
383 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
384 {
385 // We'll send a Cancel message by default,
386 // which may close the dialog.
387 // Check for looping if the Cancel event handler calls Close().
388
389 // Note that if a cancel button and handler aren't present in the dialog,
390 // nothing will happen when you close the dialog via the window manager, or
391 // via Close().
392 // We wouldn't want to destroy the dialog by default, since the dialog may have been
393 // created on the stack.
394 // However, this does mean that calling dialog->Close() won't delete the dialog
395 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
396 // sure to destroy the dialog.
397 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
398
399 static wxList closing;
400
401 if ( closing.Member(this) )
402 return;
403
404 closing.Append(this);
405
406 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
407 cancelEvent.SetEventObject( this );
408 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
409
410 closing.DeleteObject(this);
411 }
412
413 // Destroy the window (delayed, if a managed window)
414 bool wxDialog::Destroy()
415 {
416 if (!wxPendingDelete.Member(this))
417 wxPendingDelete.Append(this);
418 return true;
419 }
420
421 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
422 {
423 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
424 Refresh();
425 }
426
427 void wxDialog::ChangeFont(bool keepOriginalSize)
428 {
429 wxWindow::ChangeFont(keepOriginalSize);
430 }
431
432 void wxDialog::ChangeBackgroundColour()
433 {
434 if (GetMainWidget())
435 wxDoChangeBackgroundColour(GetMainWidget(), m_backgroundColour);
436 }
437
438 void wxDialog::ChangeForegroundColour()
439 {
440 if (GetMainWidget())
441 wxDoChangeForegroundColour(GetMainWidget(), m_foregroundColour);
442 }