]> git.saurik.com Git - wxWidgets.git/blame - src/motif/dialog.cpp
wxGTK1 compilation fix after last commit
[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
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;
dfc54541 108 Widget shell = XtParent(dialogShell) ;
798a4529
MB
109
110 SetTitle( title );
dfe1eee3 111
a756f210 112 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
96be256b 113 ChangeFont(false);
dfe1eee3 114
dfc54541 115 // Can't remember what this was about... but I think it's necessary.
dfad0599 116 if (wxUSE_INVISIBLE_RESIZE)
dfc54541 117 {
2d120f83
JS
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);
dfe1eee3 124
2d120f83
JS
125 if (size.x > -1)
126 XtVaSetValues(dialogShell, XmNwidth, size.x, NULL);
127 if (size.y > -1)
128 XtVaSetValues(dialogShell, XmNheight, size.y, NULL);
dfc54541 129 }
dfe1eee3 130
dfc54541
JS
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).
dfad0599 134 if (!wxUSE_INVISIBLE_RESIZE)
96be256b 135 XtVaSetValues(shell, XmNmappedWhenManaged, False, NULL);
dfe1eee3 136
dfad0599 137 if (!wxUSE_INVISIBLE_RESIZE)
dfc54541
JS
138 {
139 XtManageChild(dialogShell);
140 SetSize(pos.x, pos.y, size.x, size.y);
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,
f58585c0
VZ
151 wxWindowID id,
152 const wxString& title,
153 const wxPoint& pos,
154 const wxSize& size,
155 long style,
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
JJ
197#ifdef __VMS
198#pragma message disable codcauunr
199#endif
200 if ( flag )
dfc54541
JS
201 m_windowStyle |= wxDIALOG_MODAL ;
202 else
203 if ( m_windowStyle & wxDIALOG_MODAL )
2d120f83 204 m_windowStyle -= wxDIALOG_MODAL ;
dfe1eee3 205
2d120f83
JS
206 wxModelessWindows.DeleteObject(this);
207 if (!flag)
208 wxModelessWindows.Append(this);
5f9a153c
JJ
209#ifdef __VMS
210#pragma message enable codcauunr
211#endif
4bb6408c
JS
212}
213
214wxDialog::~wxDialog()
215{
96be256b 216 m_isBeingDeleted = true;
2187eef5 217
7e1bcfa8 218 delete m_eventLoop;
798a4529 219
2e35f56f 220 if (m_mainWidget)
798a4529 221 {
96be256b 222 XtRemoveEventHandler((Widget) m_mainWidget, ExposureMask, False,
798a4529
MB
223 wxUniversalRepaintProc, (XtPointer) this);
224 }
dfe1eee3 225
96be256b 226 m_modalShowing = false;
dfad0599 227 if (!wxUSE_INVISIBLE_RESIZE && m_mainWidget)
dfc54541 228 {
2d120f83 229 XtUnmapWidget((Widget) m_mainWidget);
dfc54541 230 }
2187eef5
MB
231
232 PreDestroy();
dfe1eee3 233
f58585c0 234 if ( m_mainWidget )
cba2db0c 235 {
798a4529
MB
236 wxDeleteWindowFromTable( (Widget)m_mainWidget );
237 XtDestroyWidget( (Widget)m_mainWidget );
dfc54541 238 }
4bb6408c
JS
239}
240
241// By default, pressing escape cancels the dialog
242void wxDialog::OnCharHook(wxKeyEvent& event)
243{
2d120f83
JS
244 if (event.m_keyCode == WXK_ESCAPE)
245 {
246 // Behaviour changed in 2.0: we'll send a Cancel message
247 // to the dialog instead of Close.
248 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
249 cancelEvent.SetEventObject( this );
250 GetEventHandler()->ProcessEvent(cancelEvent);
dfe1eee3 251
2d120f83
JS
252 return;
253 }
254 // We didn't process this event.
255 event.Skip();
4bb6408c
JS
256}
257
bfc6fde4 258void wxDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 259{
dfc54541 260 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
bfc6fde4 261 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
dfc54541 262 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
4bb6408c
JS
263}
264
ad813b00
JS
265void wxDialog::DoSetClientSize(int width, int height)
266{
267 wxWindow::SetSize(-1, -1, width, height);
268}
269
4bb6408c
JS
270void wxDialog::SetTitle(const wxString& title)
271{
798a4529 272 wxTopLevelWindow::SetTitle( title );
4bb6408c 273
798a4529
MB
274 if( !title.empty() )
275 {
276 wxXmString str( title );
277 XtVaSetValues( (Widget)m_mainWidget,
278 XmNtitle, title.c_str(),
279 XmNdialogTitle, str(), // Roberto Cocchi
280 XmNiconName, title.c_str(),
281 NULL );
dfc54541 282 }
dfc54541
JS
283}
284
798a4529 285bool wxDialog::Show( bool show )
dfc54541 286{
5a2e3d8c
MB
287 if( !wxWindowBase::Show( show ) )
288 return false;
dfc54541 289
dfc54541 290 m_isShown = show;
dfe1eee3 291
b3090d48
MB
292 if (show)
293 {
294 // this usually will result in TransferDataToWindow() being called
295 // which will change the controls values so do it before showing as
296 // otherwise we could have some flicker
297 InitDialog();
298 }
299
dfc54541
JS
300 if (show)
301 {
dfad0599 302 if (!wxUSE_INVISIBLE_RESIZE)
2d120f83 303 XtMapWidget(XtParent((Widget) m_mainWidget));
dfc54541 304 else
798a4529 305 XtManageChild((Widget)m_mainWidget) ;
dfe1eee3 306
798a4529
MB
307 XRaiseWindow( XtDisplay( (Widget)m_mainWidget ),
308 XtWindow( (Widget)m_mainWidget) );
dfe1eee3 309
dfc54541
JS
310 }
311 else
312 {
dfad0599 313 if (!wxUSE_INVISIBLE_RESIZE)
dfc54541
JS
314 XtUnmapWidget(XtParent((Widget) m_mainWidget));
315 else
798a4529 316 XtUnmanageChild((Widget)m_mainWidget) ;
dfe1eee3 317
eb6fa4b4 318 XFlush(XtDisplay((Widget)m_mainWidget));
96be256b 319 XSync(XtDisplay((Widget)m_mainWidget), False);
dfc54541 320 }
dfe1eee3 321
96be256b 322 return true;
dfc54541
JS
323}
324
325// Shows a dialog modally, returning a return code
4bb6408c
JS
326int wxDialog::ShowModal()
327{
328 m_windowStyle |= wxDIALOG_MODAL;
dfe1eee3 329
96be256b 330 Show(true);
dfe1eee3 331
eb6fa4b4
MB
332 // after the event loop ran, the widget might already have been destroyed
333 WXDisplay* display = (WXDisplay*)XtDisplay( (Widget)m_mainWidget );
334
dfc54541
JS
335 if (m_modalShowing)
336 return 0;
7e1bcfa8 337 m_eventLoop = new wxEventLoop;
dfe1eee3 338
96be256b
MB
339 m_modalShowing = true;
340 XtAddGrab((Widget) m_mainWidget, True, False);
dfe1eee3 341
7e1bcfa8 342 m_eventLoop->Run();
dfe1eee3 343
dfc54541 344 // Now process all events in case they get sent to a destroyed dialog
eb6fa4b4 345 wxFlushEvents( display );
dfe1eee3 346
7e1bcfa8
MB
347 delete m_eventLoop;
348 m_eventLoop = NULL;
349
dfc54541
JS
350 // TODO: is it safe to call this, if the dialog may have been deleted
351 // by now? Probably only if we're using delayed deletion of dialogs.
352 return GetReturnCode();
4bb6408c
JS
353}
354
355void wxDialog::EndModal(int retCode)
356{
dfc54541
JS
357 if (!m_modalShowing)
358 return;
dfe1eee3 359
dfc54541 360 SetReturnCode(retCode);
dfe1eee3 361
88150e60
JS
362 // Strangely, we don't seem to need this now.
363 // XtRemoveGrab((Widget) m_mainWidget);
dfe1eee3 364
96be256b 365 Show(false);
dfe1eee3 366
96be256b 367 m_modalShowing = false;
7e1bcfa8 368 m_eventLoop->Exit();
e95b8d5a
JS
369
370 SetModal(false);
4bb6408c
JS
371}
372
373// Standard buttons
f9e02ac7 374void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
4bb6408c 375{
2d120f83
JS
376 if ( Validate() && TransferDataFromWindow() )
377 {
4bb6408c
JS
378 if ( IsModal() )
379 EndModal(wxID_OK);
380 else
381 {
2d120f83 382 SetReturnCode(wxID_OK);
96be256b 383 this->Show(false);
4bb6408c 384 }
2d120f83 385 }
4bb6408c
JS
386}
387
f9e02ac7 388void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
4bb6408c 389{
2d120f83
JS
390 if (Validate())
391 TransferDataFromWindow();
392 // TODO probably need to disable the Apply button until things change again
4bb6408c
JS
393}
394
f9e02ac7 395void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
4bb6408c
JS
396{
397 if ( IsModal() )
398 EndModal(wxID_CANCEL);
399 else
400 {
401 SetReturnCode(wxID_CANCEL);
96be256b 402 this->Show(false);
4bb6408c
JS
403 }
404}
405
af111fc3 406void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
4bb6408c 407{
e3065973 408 // We'll send a Cancel message by default,
4bb6408c 409 // which may close the dialog.
e3065973
JS
410 // Check for looping if the Cancel event handler calls Close().
411
412 // Note that if a cancel button and handler aren't present in the dialog,
413 // nothing will happen when you close the dialog via the window manager, or
414 // via Close().
415 // We wouldn't want to destroy the dialog by default, since the dialog may have been
416 // created on the stack.
417 // However, this does mean that calling dialog->Close() won't delete the dialog
418 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
419 // sure to destroy the dialog.
420 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
421
4bb6408c 422 static wxList closing;
dfe1eee3 423
4bb6408c 424 if ( closing.Member(this) )
e3065973 425 return;
dfe1eee3 426
4bb6408c 427 closing.Append(this);
dfe1eee3 428
2d120f83
JS
429 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
430 cancelEvent.SetEventObject( this );
e3065973 431 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
4bb6408c 432
e3065973 433 closing.DeleteObject(this);
4bb6408c
JS
434}
435
436// Destroy the window (delayed, if a managed window)
437bool wxDialog::Destroy()
438{
2d120f83
JS
439 if (!wxPendingDelete.Member(this))
440 wxPendingDelete.Append(this);
96be256b 441 return true;
4bb6408c
JS
442}
443
f9e02ac7 444void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
4bb6408c 445{
a756f210 446 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
2d120f83 447 Refresh();
4bb6408c
JS
448}
449
94b49b93 450void wxDialog::ChangeFont(bool keepOriginalSize)
0d57be45 451{
94b49b93 452 wxWindow::ChangeFont(keepOriginalSize);
0d57be45
JS
453}
454
455void wxDialog::ChangeBackgroundColour()
456{
94b49b93 457 if (GetMainWidget())
a8680e3e 458 wxDoChangeBackgroundColour(GetMainWidget(), m_backgroundColour);
0d57be45
JS
459}
460
461void wxDialog::ChangeForegroundColour()
462{
94b49b93 463 if (GetMainWidget())
a8680e3e 464 wxDoChangeForegroundColour(GetMainWidget(), m_foregroundColour);
0d57be45 465}