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