]> git.saurik.com Git - wxWidgets.git/blame - src/motif/dialog.cpp
Added InitMouseEvent helper method (like wxMSW)
[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
dfe1eee3 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "dialog.h"
14#endif
15
bcd055ae
JJ
16#ifdef __VMS
17#define XtDisplay XTDISPLAY
18#define XtWindow XTWINDOW
19#define XtParent XTPARENT
20#define XtScreen XTSCREEN
21#endif
22
4bb6408c
JS
23#include "wx/dialog.h"
24#include "wx/utils.h"
4bb6408c
JS
25#include "wx/app.h"
26#include "wx/settings.h"
7e1bcfa8 27#include "wx/evtloop.h"
4bb6408c 28
338dd992
JJ
29#ifdef __VMS__
30#pragma message disable nosimpint
31#endif
dfc54541
JS
32#include <Xm/Xm.h>
33
34#include <X11/Shell.h>
35#if XmVersion >= 1002
36#include <Xm/XmAll.h>
37#endif
38#include <Xm/MwmUtil.h>
39#include <Xm/Label.h>
40#include <Xm/BulletinB.h>
41#include <Xm/Frame.h>
42#include <Xm/Text.h>
43#include <Xm/DialogS.h>
44#include <Xm/FileSB.h>
45#include <Xm/RowColumn.h>
46#include <Xm/LabelG.h>
47#include <Xm/AtomMgr.h>
48#if XmVersion > 1000
49#include <Xm/Protocols.h>
50#endif
338dd992
JJ
51#ifdef __VMS__
52#pragma message enable nosimpint
53#endif
dfc54541
JS
54
55#include "wx/motif/private.h"
56
dfc54541
JS
57// A stack of modal_showing flags, since we can't rely
58// on accessing wxDialog::m_modalShowing within
59// wxDialog::Show in case a callback has deleted the wxDialog.
7e1bcfa8 60// static wxList wxModalShowingStack;
dfc54541 61
4bb6408c
JS
62// Lists to keep track of windows, so we can disable/enable them
63// for modal dialogs
64wxList wxModalDialogs;
798a4529 65extern wxList wxModelessWindows; // Frames and modeless dialogs
4bb6408c
JS
66extern wxList wxPendingDelete;
67
47d67540 68#define wxUSE_INVISIBLE_RESIZE 1
dfc54541 69
798a4529 70IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
4bb6408c 71
798a4529 72BEGIN_EVENT_TABLE(wxDialog, wxTopLevelWindow)
8e877c19
RR
73 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
74 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
75 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
76 EVT_CHAR_HOOK(wxDialog::OnCharHook)
77 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
78 EVT_CLOSE(wxDialog::OnCloseWindow)
4bb6408c
JS
79END_EVENT_TABLE()
80
4bb6408c
JS
81
82wxDialog::wxDialog()
83{
dfc54541 84 m_modalShowing = FALSE;
7e1bcfa8 85 m_eventLoop = NULL;
a756f210 86 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
4bb6408c
JS
87}
88
89bool wxDialog::Create(wxWindow *parent, wxWindowID id,
2d120f83
JS
90 const wxString& title,
91 const wxPoint& pos,
92 const wxSize& size,
93 long style,
94 const wxString& name)
4bb6408c 95{
798a4529
MB
96 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
97
98 if( !wxTopLevelWindow::Create( parent, id, title, pos, size, style,
99 name ) )
100 return FALSE;
101
dfc54541 102 m_modalShowing = FALSE;
7e1bcfa8 103 m_eventLoop = NULL;
dfe1eee3 104
a756f210 105 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
0d57be45 106 m_foregroundColour = *wxBLACK;
dfe1eee3 107
798a4529 108 Widget dialogShell = (Widget) m_mainWidget;
dfc54541 109 Widget shell = XtParent(dialogShell) ;
798a4529
MB
110
111 SetTitle( title );
dfe1eee3 112
a756f210 113 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
4b5f3fe6 114 ChangeFont(FALSE);
dfe1eee3 115
dfc54541 116 // Can't remember what this was about... but I think it's necessary.
dfad0599 117 if (wxUSE_INVISIBLE_RESIZE)
dfc54541 118 {
2d120f83
JS
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);
dfe1eee3 125
2d120f83
JS
126 if (size.x > -1)
127 XtVaSetValues(dialogShell, XmNwidth, size.x, NULL);
128 if (size.y > -1)
129 XtVaSetValues(dialogShell, XmNheight, size.y, NULL);
dfc54541 130 }
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).
dfad0599 135 if (!wxUSE_INVISIBLE_RESIZE)
dfc54541 136 XtVaSetValues(shell, XmNmappedWhenManaged, FALSE, NULL);
dfe1eee3 137
dfad0599 138 if (!wxUSE_INVISIBLE_RESIZE)
dfc54541
JS
139 {
140 XtManageChild(dialogShell);
141 SetSize(pos.x, pos.y, size.x, size.y);
142 }
143 XtAddEventHandler(dialogShell,ExposureMask,FALSE,
2e35f56f 144 wxUniversalRepaintProc, (XtPointer) this);
dfe1eee3 145
0d57be45 146 ChangeBackgroundColour();
dfe1eee3 147
dfc54541 148 return TRUE;
4bb6408c
JS
149}
150
798a4529
MB
151bool wxDialog::DoCreate( wxWindow* parent, wxWindowID id,
152 const wxString& title,
153 const wxPoint& pos,
154 const wxSize& size,
155 long 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 =
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
192 return TRUE;
193}
194
4bb6408c
JS
195void wxDialog::SetModal(bool flag)
196{
dfc54541
JS
197 if ( flag )
198 m_windowStyle |= wxDIALOG_MODAL ;
199 else
200 if ( m_windowStyle & wxDIALOG_MODAL )
2d120f83 201 m_windowStyle -= wxDIALOG_MODAL ;
dfe1eee3 202
2d120f83
JS
203 wxModelessWindows.DeleteObject(this);
204 if (!flag)
205 wxModelessWindows.Append(this);
4bb6408c
JS
206}
207
208wxDialog::~wxDialog()
209{
3ab377bd 210 m_isBeingDeleted = TRUE;
2187eef5 211
7e1bcfa8 212 delete m_eventLoop;
798a4529 213
2e35f56f 214 if (m_mainWidget)
798a4529
MB
215 {
216 XtRemoveEventHandler((Widget) m_mainWidget, ExposureMask, FALSE,
217 wxUniversalRepaintProc, (XtPointer) this);
218 }
dfe1eee3 219
dfc54541 220 m_modalShowing = FALSE;
dfad0599 221 if (!wxUSE_INVISIBLE_RESIZE && m_mainWidget)
dfc54541 222 {
2d120f83 223 XtUnmapWidget((Widget) m_mainWidget);
dfc54541 224 }
2187eef5
MB
225
226 PreDestroy();
227 DoDestroy();
798a4529 228}
dfe1eee3 229
798a4529
MB
230void wxDialog::DoDestroy()
231{
232 if( m_mainWidget )
cba2db0c 233 {
798a4529
MB
234 wxDeleteWindowFromTable( (Widget)m_mainWidget );
235 XtDestroyWidget( (Widget)m_mainWidget );
dfc54541 236 }
4bb6408c
JS
237}
238
239// By default, pressing escape cancels the dialog
240void wxDialog::OnCharHook(wxKeyEvent& event)
241{
2d120f83
JS
242 if (event.m_keyCode == WXK_ESCAPE)
243 {
244 // Behaviour changed in 2.0: we'll send a Cancel message
245 // to the dialog instead of Close.
246 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
247 cancelEvent.SetEventObject( this );
248 GetEventHandler()->ProcessEvent(cancelEvent);
dfe1eee3 249
2d120f83
JS
250 return;
251 }
252 // We didn't process this event.
253 event.Skip();
4bb6408c
JS
254}
255
bfc6fde4 256void wxDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 257{
dfc54541 258 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
bfc6fde4 259 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
dfc54541 260 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
4bb6408c
JS
261}
262
ad813b00
JS
263void wxDialog::DoSetClientSize(int width, int height)
264{
265 wxWindow::SetSize(-1, -1, width, height);
266}
267
4bb6408c
JS
268void wxDialog::SetTitle(const wxString& title)
269{
798a4529 270 wxTopLevelWindow::SetTitle( title );
4bb6408c 271
798a4529
MB
272 if( !title.empty() )
273 {
274 wxXmString str( title );
275 XtVaSetValues( (Widget)m_mainWidget,
276 XmNtitle, title.c_str(),
277 XmNdialogTitle, str(), // Roberto Cocchi
278 XmNiconName, title.c_str(),
279 NULL );
dfc54541 280 }
dfc54541
JS
281}
282
798a4529 283bool wxDialog::Show( bool show )
dfc54541 284{
798a4529
MB
285 if( !wxTopLevelWindowMotif::Show( show ) )
286 return FALSE;
dfc54541 287
dfc54541 288 m_isShown = show;
dfe1eee3 289
dfc54541
JS
290 if (show)
291 {
dfad0599 292 if (!wxUSE_INVISIBLE_RESIZE)
2d120f83 293 XtMapWidget(XtParent((Widget) m_mainWidget));
dfc54541 294 else
798a4529 295 XtManageChild((Widget)m_mainWidget) ;
dfe1eee3 296
798a4529
MB
297 XRaiseWindow( XtDisplay( (Widget)m_mainWidget ),
298 XtWindow( (Widget)m_mainWidget) );
dfe1eee3 299
dfc54541
JS
300 }
301 else
302 {
dfad0599 303 if (!wxUSE_INVISIBLE_RESIZE)
dfc54541
JS
304 XtUnmapWidget(XtParent((Widget) m_mainWidget));
305 else
798a4529 306 XtUnmanageChild((Widget)m_mainWidget) ;
dfe1eee3 307
2d120f83
JS
308 XFlush(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()));
309 XSync(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), FALSE);
dfc54541 310 }
dfe1eee3 311
dfc54541
JS
312 return TRUE;
313}
314
315// Shows a dialog modally, returning a return code
4bb6408c
JS
316int wxDialog::ShowModal()
317{
318 m_windowStyle |= wxDIALOG_MODAL;
dfe1eee3 319
dfc54541 320 Show(TRUE);
dfe1eee3 321
dfc54541
JS
322 if (m_modalShowing)
323 return 0;
7e1bcfa8 324 m_eventLoop = new wxEventLoop;
dfe1eee3 325
dfc54541 326 m_modalShowing = TRUE;
793f619f 327 XtAddGrab((Widget) m_mainWidget, TRUE, FALSE);
dfe1eee3 328
7e1bcfa8 329 m_eventLoop->Run();
dfe1eee3 330
dfc54541
JS
331 // Now process all events in case they get sent to a destroyed dialog
332 XSync(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), FALSE);
7e1bcfa8 333 while (m_eventLoop->Pending())
dfc54541 334 {
3d870472 335 XFlush(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()));
7e1bcfa8 336 m_eventLoop->Dispatch();
dfc54541 337 }
dfe1eee3 338
7e1bcfa8
MB
339 delete m_eventLoop;
340 m_eventLoop = NULL;
341
dfc54541
JS
342 // TODO: is it safe to call this, if the dialog may have been deleted
343 // by now? Probably only if we're using delayed deletion of dialogs.
344 return GetReturnCode();
4bb6408c
JS
345}
346
347void wxDialog::EndModal(int retCode)
348{
dfc54541
JS
349 if (!m_modalShowing)
350 return;
dfe1eee3 351
dfc54541 352 SetReturnCode(retCode);
dfe1eee3 353
88150e60
JS
354 // Strangely, we don't seem to need this now.
355 // XtRemoveGrab((Widget) m_mainWidget);
dfe1eee3 356
dfc54541 357 Show(FALSE);
dfe1eee3 358
dfc54541 359 m_modalShowing = FALSE;
7e1bcfa8 360 m_eventLoop->Exit();
4bb6408c
JS
361}
362
363// Standard buttons
f9e02ac7 364void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
4bb6408c 365{
2d120f83
JS
366 if ( Validate() && TransferDataFromWindow() )
367 {
4bb6408c
JS
368 if ( IsModal() )
369 EndModal(wxID_OK);
370 else
371 {
2d120f83
JS
372 SetReturnCode(wxID_OK);
373 this->Show(FALSE);
4bb6408c 374 }
2d120f83 375 }
4bb6408c
JS
376}
377
f9e02ac7 378void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
4bb6408c 379{
2d120f83
JS
380 if (Validate())
381 TransferDataFromWindow();
382 // TODO probably need to disable the Apply button until things change again
4bb6408c
JS
383}
384
f9e02ac7 385void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
4bb6408c
JS
386{
387 if ( IsModal() )
388 EndModal(wxID_CANCEL);
389 else
390 {
391 SetReturnCode(wxID_CANCEL);
2d120f83 392 this->Show(FALSE);
4bb6408c
JS
393 }
394}
395
af111fc3 396void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
4bb6408c 397{
e3065973 398 // We'll send a Cancel message by default,
4bb6408c 399 // which may close the dialog.
e3065973
JS
400 // Check for looping if the Cancel event handler calls Close().
401
402 // Note that if a cancel button and handler aren't present in the dialog,
403 // nothing will happen when you close the dialog via the window manager, or
404 // via Close().
405 // We wouldn't want to destroy the dialog by default, since the dialog may have been
406 // created on the stack.
407 // However, this does mean that calling dialog->Close() won't delete the dialog
408 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
409 // sure to destroy the dialog.
410 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
411
4bb6408c 412 static wxList closing;
dfe1eee3 413
4bb6408c 414 if ( closing.Member(this) )
e3065973 415 return;
dfe1eee3 416
4bb6408c 417 closing.Append(this);
dfe1eee3 418
2d120f83
JS
419 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
420 cancelEvent.SetEventObject( this );
e3065973 421 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
4bb6408c 422
e3065973 423 closing.DeleteObject(this);
4bb6408c
JS
424}
425
426// Destroy the window (delayed, if a managed window)
427bool wxDialog::Destroy()
428{
2d120f83
JS
429 if (!wxPendingDelete.Member(this))
430 wxPendingDelete.Append(this);
431 return TRUE;
4bb6408c
JS
432}
433
f9e02ac7 434void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
4bb6408c 435{
a756f210 436 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
2d120f83 437 Refresh();
4bb6408c
JS
438}
439
94b49b93 440void wxDialog::ChangeFont(bool keepOriginalSize)
0d57be45 441{
94b49b93 442 wxWindow::ChangeFont(keepOriginalSize);
0d57be45
JS
443}
444
445void wxDialog::ChangeBackgroundColour()
446{
94b49b93
JS
447 if (GetMainWidget())
448 DoChangeBackgroundColour(GetMainWidget(), m_backgroundColour);
0d57be45
JS
449}
450
451void wxDialog::ChangeForegroundColour()
452{
94b49b93
JS
453 if (GetMainWidget())
454 DoChangeForegroundColour(GetMainWidget(), m_foregroundColour);
0d57be45 455}