]> git.saurik.com Git - wxWidgets.git/blame - src/motif/dialog.cpp
Crash fix under VC++
[wxWidgets.git] / src / motif / dialog.cpp
CommitLineData
4bb6408c
JS
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
65571936 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
4bb6408c
JS
13#pragma implementation "dialog.h"
14#endif
15
1248b41f
MB
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
bcd055ae
JJ
19#ifdef __VMS
20#define XtDisplay XTDISPLAY
21#define XtWindow XTWINDOW
22#define XtParent XTPARENT
23#define XtScreen XTSCREEN
24#endif
25
4bb6408c
JS
26#include "wx/dialog.h"
27#include "wx/utils.h"
4bb6408c
JS
28#include "wx/app.h"
29#include "wx/settings.h"
7e1bcfa8 30#include "wx/evtloop.h"
4bb6408c 31
338dd992
JJ
32#ifdef __VMS__
33#pragma message disable nosimpint
34#endif
dfc54541
JS
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
338dd992
JJ
54#ifdef __VMS__
55#pragma message enable nosimpint
56#endif
dfc54541
JS
57
58#include "wx/motif/private.h"
59
dfc54541
JS
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.
7e1bcfa8 63// static wxList wxModalShowingStack;
dfc54541 64
4bb6408c
JS
65// Lists to keep track of windows, so we can disable/enable them
66// for modal dialogs
67wxList wxModalDialogs;
798a4529 68extern wxList wxModelessWindows; // Frames and modeless dialogs
4bb6408c
JS
69extern wxList wxPendingDelete;
70
47d67540 71#define wxUSE_INVISIBLE_RESIZE 1
dfc54541 72
798a4529 73IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
4bb6408c 74
798a4529 75BEGIN_EVENT_TABLE(wxDialog, wxTopLevelWindow)
8e877c19
RR
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)
4bb6408c
JS
82END_EVENT_TABLE()
83
4bb6408c
JS
84
85wxDialog::wxDialog()
86{
dfc54541 87 m_modalShowing = FALSE;
7e1bcfa8 88 m_eventLoop = NULL;
a756f210 89 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
4bb6408c
JS
90}
91
92bool wxDialog::Create(wxWindow *parent, wxWindowID id,
2d120f83
JS
93 const wxString& title,
94 const wxPoint& pos,
95 const wxSize& size,
96 long style,
97 const wxString& name)
4bb6408c 98{
798a4529
MB
99 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
100
101 if( !wxTopLevelWindow::Create( parent, id, title, pos, size, style,
102 name ) )
103 return FALSE;
104
dfc54541 105 m_modalShowing = FALSE;
7e1bcfa8 106 m_eventLoop = NULL;
dfe1eee3 107
a756f210 108 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
0d57be45 109 m_foregroundColour = *wxBLACK;
dfe1eee3 110
798a4529 111 Widget dialogShell = (Widget) m_mainWidget;
dfc54541 112 Widget shell = XtParent(dialogShell) ;
798a4529
MB
113
114 SetTitle( title );
dfe1eee3 115
a756f210 116 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
4b5f3fe6 117 ChangeFont(FALSE);
dfe1eee3 118
dfc54541 119 // Can't remember what this was about... but I think it's necessary.
dfad0599 120 if (wxUSE_INVISIBLE_RESIZE)
dfc54541 121 {
2d120f83
JS
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);
dfe1eee3 128
2d120f83
JS
129 if (size.x > -1)
130 XtVaSetValues(dialogShell, XmNwidth, size.x, NULL);
131 if (size.y > -1)
132 XtVaSetValues(dialogShell, XmNheight, size.y, NULL);
dfc54541 133 }
dfe1eee3 134
dfc54541
JS
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).
dfad0599 138 if (!wxUSE_INVISIBLE_RESIZE)
dfc54541 139 XtVaSetValues(shell, XmNmappedWhenManaged, FALSE, NULL);
dfe1eee3 140
dfad0599 141 if (!wxUSE_INVISIBLE_RESIZE)
dfc54541
JS
142 {
143 XtManageChild(dialogShell);
144 SetSize(pos.x, pos.y, size.x, size.y);
145 }
146 XtAddEventHandler(dialogShell,ExposureMask,FALSE,
2e35f56f 147 wxUniversalRepaintProc, (XtPointer) this);
dfe1eee3 148
0d57be45 149 ChangeBackgroundColour();
dfe1eee3 150
dfc54541 151 return TRUE;
4bb6408c
JS
152}
153
798a4529
MB
154bool wxDialog::DoCreate( wxWindow* parent, wxWindowID id,
155 const wxString& title,
156 const wxPoint& pos,
157 const wxSize& size,
158 long style,
159 const wxString& name )
160{
161 Widget parentWidget = (Widget) 0;
162 if( parent )
163 parentWidget = (Widget) parent->GetTopWidget();
164 if( !parent )
165 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
166
167 wxASSERT_MSG( (parentWidget != (Widget) 0),
168 "Could not find a suitable parent shell for dialog." );
169
170 Arg args[2];
171 XtSetArg (args[0], XmNdefaultPosition, False);
172 XtSetArg (args[1], XmNautoUnmanage, False);
173 Widget dialogShell =
d3a80c92
MB
174 XmCreateBulletinBoardDialog( parentWidget,
175 wxConstCast(name.c_str(), char),
798a4529
MB
176 args, 2);
177 m_mainWidget = (WXWidget) dialogShell;
178
179 // We don't want margins, since there is enough elsewhere.
180 XtVaSetValues( dialogShell,
181 XmNmarginHeight, 0,
182 XmNmarginWidth, 0,
183 XmNresizePolicy, XmRESIZE_NONE,
184 NULL ) ;
185
186 XtTranslations ptr ;
187 XtOverrideTranslations(dialogShell,
188 ptr = XtParseTranslationTable("<Configure>: resize()"));
189 XtFree((char *)ptr);
190
191 XtRealizeWidget(dialogShell);
192
193 wxAddWindowToTable( (Widget)m_mainWidget, this );
194
195 return TRUE;
196}
197
4bb6408c
JS
198void wxDialog::SetModal(bool flag)
199{
5f9a153c
JJ
200#ifdef __VMS
201#pragma message disable codcauunr
202#endif
203 if ( flag )
dfc54541
JS
204 m_windowStyle |= wxDIALOG_MODAL ;
205 else
206 if ( m_windowStyle & wxDIALOG_MODAL )
2d120f83 207 m_windowStyle -= wxDIALOG_MODAL ;
dfe1eee3 208
2d120f83
JS
209 wxModelessWindows.DeleteObject(this);
210 if (!flag)
211 wxModelessWindows.Append(this);
5f9a153c
JJ
212#ifdef __VMS
213#pragma message enable codcauunr
214#endif
4bb6408c
JS
215}
216
217wxDialog::~wxDialog()
218{
3ab377bd 219 m_isBeingDeleted = TRUE;
2187eef5 220
7e1bcfa8 221 delete m_eventLoop;
798a4529 222
2e35f56f 223 if (m_mainWidget)
798a4529
MB
224 {
225 XtRemoveEventHandler((Widget) m_mainWidget, ExposureMask, FALSE,
226 wxUniversalRepaintProc, (XtPointer) this);
227 }
dfe1eee3 228
dfc54541 229 m_modalShowing = FALSE;
dfad0599 230 if (!wxUSE_INVISIBLE_RESIZE && m_mainWidget)
dfc54541 231 {
2d120f83 232 XtUnmapWidget((Widget) m_mainWidget);
dfc54541 233 }
2187eef5
MB
234
235 PreDestroy();
236 DoDestroy();
798a4529 237}
dfe1eee3 238
798a4529
MB
239void wxDialog::DoDestroy()
240{
241 if( m_mainWidget )
cba2db0c 242 {
798a4529
MB
243 wxDeleteWindowFromTable( (Widget)m_mainWidget );
244 XtDestroyWidget( (Widget)m_mainWidget );
dfc54541 245 }
4bb6408c
JS
246}
247
248// By default, pressing escape cancels the dialog
249void wxDialog::OnCharHook(wxKeyEvent& event)
250{
2d120f83
JS
251 if (event.m_keyCode == WXK_ESCAPE)
252 {
253 // Behaviour changed in 2.0: we'll send a Cancel message
254 // to the dialog instead of Close.
255 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
256 cancelEvent.SetEventObject( this );
257 GetEventHandler()->ProcessEvent(cancelEvent);
dfe1eee3 258
2d120f83
JS
259 return;
260 }
261 // We didn't process this event.
262 event.Skip();
4bb6408c
JS
263}
264
bfc6fde4 265void wxDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 266{
dfc54541 267 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
bfc6fde4 268 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
dfc54541 269 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
4bb6408c
JS
270}
271
ad813b00
JS
272void wxDialog::DoSetClientSize(int width, int height)
273{
274 wxWindow::SetSize(-1, -1, width, height);
275}
276
4bb6408c
JS
277void wxDialog::SetTitle(const wxString& title)
278{
798a4529 279 wxTopLevelWindow::SetTitle( title );
4bb6408c 280
798a4529
MB
281 if( !title.empty() )
282 {
283 wxXmString str( title );
284 XtVaSetValues( (Widget)m_mainWidget,
285 XmNtitle, title.c_str(),
286 XmNdialogTitle, str(), // Roberto Cocchi
287 XmNiconName, title.c_str(),
288 NULL );
dfc54541 289 }
dfc54541
JS
290}
291
798a4529 292bool wxDialog::Show( bool show )
dfc54541 293{
5a2e3d8c
MB
294 if( !wxWindowBase::Show( show ) )
295 return false;
dfc54541 296
dfc54541 297 m_isShown = show;
dfe1eee3 298
dfc54541
JS
299 if (show)
300 {
dfad0599 301 if (!wxUSE_INVISIBLE_RESIZE)
2d120f83 302 XtMapWidget(XtParent((Widget) m_mainWidget));
dfc54541 303 else
798a4529 304 XtManageChild((Widget)m_mainWidget) ;
dfe1eee3 305
798a4529
MB
306 XRaiseWindow( XtDisplay( (Widget)m_mainWidget ),
307 XtWindow( (Widget)m_mainWidget) );
dfe1eee3 308
dfc54541
JS
309 }
310 else
311 {
dfad0599 312 if (!wxUSE_INVISIBLE_RESIZE)
dfc54541
JS
313 XtUnmapWidget(XtParent((Widget) m_mainWidget));
314 else
798a4529 315 XtUnmanageChild((Widget)m_mainWidget) ;
dfe1eee3 316
eb6fa4b4
MB
317 XFlush(XtDisplay((Widget)m_mainWidget));
318 XSync(XtDisplay((Widget)m_mainWidget), FALSE);
dfc54541 319 }
dfe1eee3 320
dfc54541
JS
321 return TRUE;
322}
323
324// Shows a dialog modally, returning a return code
4bb6408c
JS
325int wxDialog::ShowModal()
326{
327 m_windowStyle |= wxDIALOG_MODAL;
dfe1eee3 328
dfc54541 329 Show(TRUE);
dfe1eee3 330
eb6fa4b4
MB
331 // after the event loop ran, the widget might already have been destroyed
332 WXDisplay* display = (WXDisplay*)XtDisplay( (Widget)m_mainWidget );
333
dfc54541
JS
334 if (m_modalShowing)
335 return 0;
7e1bcfa8 336 m_eventLoop = new wxEventLoop;
dfe1eee3 337
dfc54541 338 m_modalShowing = TRUE;
793f619f 339 XtAddGrab((Widget) m_mainWidget, TRUE, FALSE);
dfe1eee3 340
7e1bcfa8 341 m_eventLoop->Run();
dfe1eee3 342
dfc54541 343 // Now process all events in case they get sent to a destroyed dialog
eb6fa4b4 344 wxFlushEvents( display );
dfe1eee3 345
7e1bcfa8
MB
346 delete m_eventLoop;
347 m_eventLoop = NULL;
348
dfc54541
JS
349 // TODO: is it safe to call this, if the dialog may have been deleted
350 // by now? Probably only if we're using delayed deletion of dialogs.
351 return GetReturnCode();
4bb6408c
JS
352}
353
354void wxDialog::EndModal(int retCode)
355{
dfc54541
JS
356 if (!m_modalShowing)
357 return;
dfe1eee3 358
dfc54541 359 SetReturnCode(retCode);
dfe1eee3 360
88150e60
JS
361 // Strangely, we don't seem to need this now.
362 // XtRemoveGrab((Widget) m_mainWidget);
dfe1eee3 363
dfc54541 364 Show(FALSE);
dfe1eee3 365
dfc54541 366 m_modalShowing = FALSE;
7e1bcfa8 367 m_eventLoop->Exit();
4bb6408c
JS
368}
369
370// Standard buttons
f9e02ac7 371void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
4bb6408c 372{
2d120f83
JS
373 if ( Validate() && TransferDataFromWindow() )
374 {
4bb6408c
JS
375 if ( IsModal() )
376 EndModal(wxID_OK);
377 else
378 {
2d120f83
JS
379 SetReturnCode(wxID_OK);
380 this->Show(FALSE);
4bb6408c 381 }
2d120f83 382 }
4bb6408c
JS
383}
384
f9e02ac7 385void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
4bb6408c 386{
2d120f83
JS
387 if (Validate())
388 TransferDataFromWindow();
389 // TODO probably need to disable the Apply button until things change again
4bb6408c
JS
390}
391
f9e02ac7 392void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
4bb6408c
JS
393{
394 if ( IsModal() )
395 EndModal(wxID_CANCEL);
396 else
397 {
398 SetReturnCode(wxID_CANCEL);
2d120f83 399 this->Show(FALSE);
4bb6408c
JS
400 }
401}
402
af111fc3 403void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
4bb6408c 404{
e3065973 405 // We'll send a Cancel message by default,
4bb6408c 406 // which may close the dialog.
e3065973
JS
407 // Check for looping if the Cancel event handler calls Close().
408
409 // Note that if a cancel button and handler aren't present in the dialog,
410 // nothing will happen when you close the dialog via the window manager, or
411 // via Close().
412 // We wouldn't want to destroy the dialog by default, since the dialog may have been
413 // created on the stack.
414 // However, this does mean that calling dialog->Close() won't delete the dialog
415 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
416 // sure to destroy the dialog.
417 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
418
4bb6408c 419 static wxList closing;
dfe1eee3 420
4bb6408c 421 if ( closing.Member(this) )
e3065973 422 return;
dfe1eee3 423
4bb6408c 424 closing.Append(this);
dfe1eee3 425
2d120f83
JS
426 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
427 cancelEvent.SetEventObject( this );
e3065973 428 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
4bb6408c 429
e3065973 430 closing.DeleteObject(this);
4bb6408c
JS
431}
432
433// Destroy the window (delayed, if a managed window)
434bool wxDialog::Destroy()
435{
2d120f83
JS
436 if (!wxPendingDelete.Member(this))
437 wxPendingDelete.Append(this);
438 return TRUE;
4bb6408c
JS
439}
440
f9e02ac7 441void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
4bb6408c 442{
a756f210 443 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
2d120f83 444 Refresh();
4bb6408c
JS
445}
446
94b49b93 447void wxDialog::ChangeFont(bool keepOriginalSize)
0d57be45 448{
94b49b93 449 wxWindow::ChangeFont(keepOriginalSize);
0d57be45
JS
450}
451
452void wxDialog::ChangeBackgroundColour()
453{
94b49b93 454 if (GetMainWidget())
a8680e3e 455 wxDoChangeBackgroundColour(GetMainWidget(), m_backgroundColour);
0d57be45
JS
456}
457
458void wxDialog::ChangeForegroundColour()
459{
94b49b93 460 if (GetMainWidget())
a8680e3e 461 wxDoChangeForegroundColour(GetMainWidget(), m_foregroundColour);
0d57be45 462}