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