wxMotif for OS/2 adjustements. Source cleaning.
[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 #include "wx/utils.h"
24 #include "wx/app.h"
25 #include "wx/settings.h"
26 #include "wx/evtloop.h"
27
28 #ifdef __VMS__
29 #pragma message disable nosimpint
30 #endif
31 #include <Xm/Xm.h>
32
33 #include <X11/Shell.h>
34 #if XmVersion >= 1002
35 #include <Xm/XmAll.h>
36 #endif
37 #include <Xm/MwmUtil.h>
38 #include <Xm/Label.h>
39 #include <Xm/BulletinB.h>
40 #include <Xm/Frame.h>
41 #include <Xm/Text.h>
42 #include <Xm/DialogS.h>
43 #include <Xm/FileSB.h>
44 #include <Xm/RowColumn.h>
45 #include <Xm/LabelG.h>
46 #include <Xm/AtomMgr.h>
47 #if XmVersion > 1000
48 #include <Xm/Protocols.h>
49 #endif
50 #ifdef __VMS__
51 #pragma message enable nosimpint
52 #endif
53
54 #include "wx/motif/private.h"
55
56 // A stack of modal_showing flags, since we can't rely
57 // on accessing wxDialog::m_modalShowing within
58 // wxDialog::Show in case a callback has deleted the wxDialog.
59 // static wxList wxModalShowingStack;
60
61 // Lists to keep track of windows, so we can disable/enable them
62 // for modal dialogs
63 wxList wxModalDialogs;
64 extern wxList wxModelessWindows; // Frames and modeless dialogs
65 extern wxList wxPendingDelete;
66
67 #define wxUSE_INVISIBLE_RESIZE 1
68
69 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
70
71 BEGIN_EVENT_TABLE(wxDialog, wxTopLevelWindow)
72 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
73 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
74 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
75 EVT_CHAR_HOOK(wxDialog::OnCharHook)
76 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
77 EVT_CLOSE(wxDialog::OnCloseWindow)
78 END_EVENT_TABLE()
79
80
81 wxDialog::wxDialog()
82 {
83 m_modalShowing = false;
84 m_eventLoop = NULL;
85 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
86 }
87
88 bool wxDialog::Create(wxWindow *parent, wxWindowID id,
89 const wxString& title,
90 const wxPoint& pos,
91 const wxSize& size,
92 long style,
93 const wxString& name)
94 {
95 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
96
97 if( !wxTopLevelWindow::Create( parent, id, title, pos, size, style,
98 name ) )
99 return false;
100
101 m_modalShowing = false;
102 m_eventLoop = NULL;
103
104 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
105 m_foregroundColour = *wxBLACK;
106
107 Widget dialogShell = (Widget) m_mainWidget;
108 Widget shell = XtParent(dialogShell) ;
109
110 SetTitle( title );
111
112 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
113 ChangeFont(false);
114
115 // Can't remember what this was about... but I think it's necessary.
116 #if wxUSE_INVISIBLE_RESIZE
117 if (pos.x > -1)
118 XtVaSetValues(dialogShell, XmNx, pos.x,
119 NULL);
120 if (pos.y > -1)
121 XtVaSetValues(dialogShell, XmNy, pos.y,
122 NULL);
123
124 if (size.x > -1)
125 XtVaSetValues(dialogShell, XmNwidth, size.x, NULL);
126 if (size.y > -1)
127 XtVaSetValues(dialogShell, XmNheight, size.y, NULL);
128 #endif
129
130 // Positioning of the dialog doesn't work properly unless the dialog
131 // is managed, so we manage without mapping to the screen.
132 // To show, we map the shell (actually it's parent).
133 #if !wxUSE_INVISIBLE_RESIZE
134 XtVaSetValues(shell, XmNmappedWhenManaged, False, NULL);
135 #endif
136
137 #if !wxUSE_INVISIBLE_RESIZE
138 XtManageChild(dialogShell);
139 SetSize(pos.x, pos.y, size.x, size.y);
140 #endif
141
142 XtAddEventHandler(dialogShell,ExposureMask,False,
143 wxUniversalRepaintProc, (XtPointer) this);
144
145 ChangeBackgroundColour();
146
147 return true;
148 }
149
150 bool wxDialog::XmDoCreateTLW(wxWindow* parent,
151 wxWindowID WXUNUSED(id),
152 const wxString& WXUNUSED(title),
153 const wxPoint& WXUNUSED(pos),
154 const wxSize& WXUNUSED(size),
155 long WXUNUSED(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
228 #if !wxUSE_INVISIBLE_RESIZE
229 if (m_mainWidget)
230 {
231 XtUnmapWidget((Widget) m_mainWidget);
232 }
233 #endif
234
235 PreDestroy();
236
237 if ( m_mainWidget )
238 {
239 wxDeleteWindowFromTable( (Widget)m_mainWidget );
240 XtDestroyWidget( (Widget)m_mainWidget );
241 }
242 }
243
244 // By default, pressing escape cancels the dialog
245 void wxDialog::OnCharHook(wxKeyEvent& event)
246 {
247 if (event.m_keyCode == WXK_ESCAPE)
248 {
249 // Behaviour changed in 2.0: we'll send a Cancel message
250 // to the dialog instead of Close.
251 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
252 cancelEvent.SetEventObject( this );
253 GetEventHandler()->ProcessEvent(cancelEvent);
254
255 return;
256 }
257 // We didn't process this event.
258 event.Skip();
259 }
260
261 void wxDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags)
262 {
263 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
264 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
265 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
266 }
267
268 void wxDialog::DoSetClientSize(int width, int height)
269 {
270 wxWindow::SetSize(-1, -1, width, height);
271 }
272
273 void wxDialog::SetTitle(const wxString& title)
274 {
275 wxTopLevelWindow::SetTitle( title );
276
277 if( !title.empty() )
278 {
279 wxXmString str( title );
280 XtVaSetValues( (Widget)m_mainWidget,
281 XmNtitle, title.c_str(),
282 XmNdialogTitle, str(), // Roberto Cocchi
283 XmNiconName, title.c_str(),
284 NULL );
285 }
286 }
287
288 bool wxDialog::Show( bool show )
289 {
290 if( !wxWindowBase::Show( show ) )
291 return false;
292
293 m_isShown = show;
294
295 if (show)
296 {
297 // this usually will result in TransferDataToWindow() being called
298 // which will change the controls values so do it before showing as
299 // otherwise we could have some flicker
300 InitDialog();
301 }
302
303 if (show)
304 {
305 #if !wxUSE_INVISIBLE_RESIZE
306 XtMapWidget(XtParent((Widget) m_mainWidget));
307 #else
308 XtManageChild((Widget)m_mainWidget) ;
309 #endif
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 #endif
322
323 XFlush(XtDisplay((Widget)m_mainWidget));
324 XSync(XtDisplay((Widget)m_mainWidget), False);
325 }
326
327 return true;
328 }
329
330 // Shows a dialog modally, returning a return code
331 int wxDialog::ShowModal()
332 {
333 m_windowStyle |= wxDIALOG_MODAL;
334
335 Show(true);
336
337 // after the event loop ran, the widget might already have been destroyed
338 WXDisplay* display = (WXDisplay*)XtDisplay( (Widget)m_mainWidget );
339
340 if (m_modalShowing)
341 return 0;
342 m_eventLoop = new wxEventLoop;
343
344 m_modalShowing = true;
345 XtAddGrab((Widget) m_mainWidget, True, False);
346
347 m_eventLoop->Run();
348
349 // Now process all events in case they get sent to a destroyed dialog
350 wxFlushEvents( display );
351
352 delete m_eventLoop;
353 m_eventLoop = NULL;
354
355 // TODO: is it safe to call this, if the dialog may have been deleted
356 // by now? Probably only if we're using delayed deletion of dialogs.
357 return GetReturnCode();
358 }
359
360 void wxDialog::EndModal(int retCode)
361 {
362 if (!m_modalShowing)
363 return;
364
365 SetReturnCode(retCode);
366
367 // Strangely, we don't seem to need this now.
368 // XtRemoveGrab((Widget) m_mainWidget);
369
370 Show(false);
371
372 m_modalShowing = false;
373 m_eventLoop->Exit();
374
375 SetModal(false);
376 }
377
378 // Standard buttons
379 void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
380 {
381 if ( Validate() && TransferDataFromWindow() )
382 {
383 if ( IsModal() )
384 EndModal(wxID_OK);
385 else
386 {
387 SetReturnCode(wxID_OK);
388 this->Show(false);
389 }
390 }
391 }
392
393 void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
394 {
395 if (Validate())
396 TransferDataFromWindow();
397 // TODO probably need to disable the Apply button until things change again
398 }
399
400 void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
401 {
402 if ( IsModal() )
403 EndModal(wxID_CANCEL);
404 else
405 {
406 SetReturnCode(wxID_CANCEL);
407 this->Show(false);
408 }
409 }
410
411 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
412 {
413 // We'll send a Cancel message by default,
414 // which may close the dialog.
415 // Check for looping if the Cancel event handler calls Close().
416
417 // Note that if a cancel button and handler aren't present in the dialog,
418 // nothing will happen when you close the dialog via the window manager, or
419 // via Close().
420 // We wouldn't want to destroy the dialog by default, since the dialog may have been
421 // created on the stack.
422 // However, this does mean that calling dialog->Close() won't delete the dialog
423 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
424 // sure to destroy the dialog.
425 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
426
427 static wxList closing;
428
429 if ( closing.Member(this) )
430 return;
431
432 closing.Append(this);
433
434 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
435 cancelEvent.SetEventObject( this );
436 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
437
438 closing.DeleteObject(this);
439 }
440
441 // Destroy the window (delayed, if a managed window)
442 bool wxDialog::Destroy()
443 {
444 if (!wxPendingDelete.Member(this))
445 wxPendingDelete.Append(this);
446 return true;
447 }
448
449 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
450 {
451 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
452 Refresh();
453 }
454
455 void wxDialog::ChangeFont(bool keepOriginalSize)
456 {
457 wxWindow::ChangeFont(keepOriginalSize);
458 }
459
460 void wxDialog::ChangeBackgroundColour()
461 {
462 if (GetMainWidget())
463 wxDoChangeBackgroundColour(GetMainWidget(), m_backgroundColour);
464 }
465
466 void wxDialog::ChangeForegroundColour()
467 {
468 if (GetMainWidget())
469 wxDoChangeForegroundColour(GetMainWidget(), m_foregroundColour);
470 }