]> git.saurik.com Git - wxWidgets.git/blame - src/motif/dialog.cpp
Always link with expat in monolithic build.
[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
4bb6408c 15#include "wx/dialog.h"
670f9935
WS
16
17#ifndef WX_PRECOMP
18 #include "wx/app.h"
de6185e2 19 #include "wx/utils.h"
9eddec69 20 #include "wx/settings.h"
670f9935
WS
21#endif
22
7e1bcfa8 23#include "wx/evtloop.h"
4bb6408c 24
338dd992
JJ
25#ifdef __VMS__
26#pragma message disable nosimpint
27#endif
dfc54541
JS
28#include <Xm/Xm.h>
29
30#include <X11/Shell.h>
31#if XmVersion >= 1002
32#include <Xm/XmAll.h>
33#endif
34#include <Xm/MwmUtil.h>
35#include <Xm/Label.h>
36#include <Xm/BulletinB.h>
37#include <Xm/Frame.h>
38#include <Xm/Text.h>
39#include <Xm/DialogS.h>
40#include <Xm/FileSB.h>
41#include <Xm/RowColumn.h>
42#include <Xm/LabelG.h>
43#include <Xm/AtomMgr.h>
44#if XmVersion > 1000
45#include <Xm/Protocols.h>
46#endif
338dd992
JJ
47#ifdef __VMS__
48#pragma message enable nosimpint
49#endif
dfc54541
JS
50
51#include "wx/motif/private.h"
52
dfc54541
JS
53// A stack of modal_showing flags, since we can't rely
54// on accessing wxDialog::m_modalShowing within
55// wxDialog::Show in case a callback has deleted the wxDialog.
7e1bcfa8 56// static wxList wxModalShowingStack;
dfc54541 57
4bb6408c
JS
58// Lists to keep track of windows, so we can disable/enable them
59// for modal dialogs
60wxList wxModalDialogs;
798a4529 61extern wxList wxModelessWindows; // Frames and modeless dialogs
4bb6408c 62
47d67540 63#define wxUSE_INVISIBLE_RESIZE 1
dfc54541 64
4bb6408c
JS
65wxDialog::wxDialog()
66{
96be256b 67 m_modalShowing = false;
7e1bcfa8 68 m_eventLoop = NULL;
4bb6408c
JS
69}
70
71bool wxDialog::Create(wxWindow *parent, wxWindowID id,
2d120f83
JS
72 const wxString& title,
73 const wxPoint& pos,
74 const wxSize& size,
75 long style,
76 const wxString& name)
4bb6408c 77{
798a4529
MB
78 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
79
80 if( !wxTopLevelWindow::Create( parent, id, title, pos, size, style,
81 name ) )
96be256b 82 return false;
798a4529 83
96be256b 84 m_modalShowing = false;
7e1bcfa8 85 m_eventLoop = NULL;
dfe1eee3 86
798a4529 87 Widget dialogShell = (Widget) m_mainWidget;
798a4529
MB
88
89 SetTitle( title );
dfe1eee3 90
dfc54541 91 // Can't remember what this was about... but I think it's necessary.
355b4d3d
WS
92#if wxUSE_INVISIBLE_RESIZE
93 if (pos.x > -1)
94 XtVaSetValues(dialogShell, XmNx, pos.x,
95 NULL);
96 if (pos.y > -1)
97 XtVaSetValues(dialogShell, XmNy, pos.y,
98 NULL);
99
100 if (size.x > -1)
101 XtVaSetValues(dialogShell, XmNwidth, size.x, NULL);
102 if (size.y > -1)
103 XtVaSetValues(dialogShell, XmNheight, size.y, NULL);
104#endif
dfe1eee3 105
dfc54541
JS
106 // Positioning of the dialog doesn't work properly unless the dialog
107 // is managed, so we manage without mapping to the screen.
108 // To show, we map the shell (actually it's parent).
355b4d3d 109#if !wxUSE_INVISIBLE_RESIZE
31df756d 110 Widget shell = XtParent(dialogShell) ;
355b4d3d
WS
111 XtVaSetValues(shell, XmNmappedWhenManaged, False, NULL);
112#endif
113
114#if !wxUSE_INVISIBLE_RESIZE
115 XtManageChild(dialogShell);
116 SetSize(pos.x, pos.y, size.x, size.y);
117#endif
dfe1eee3 118
96be256b 119 XtAddEventHandler(dialogShell,ExposureMask,False,
2e35f56f 120 wxUniversalRepaintProc, (XtPointer) this);
dfe1eee3 121
105fbe1f 122 PostCreation();
dfe1eee3 123
96be256b 124 return true;
4bb6408c
JS
125}
126
02bcd285 127bool wxDialog::XmDoCreateTLW(wxWindow* parent,
355b4d3d
WS
128 wxWindowID WXUNUSED(id),
129 const wxString& WXUNUSED(title),
130 const wxPoint& WXUNUSED(pos),
131 const wxSize& WXUNUSED(size),
132 long WXUNUSED(style),
f58585c0 133 const wxString& name)
798a4529
MB
134{
135 Widget parentWidget = (Widget) 0;
136 if( parent )
137 parentWidget = (Widget) parent->GetTopWidget();
138 if( !parent )
139 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
140
141 wxASSERT_MSG( (parentWidget != (Widget) 0),
142 "Could not find a suitable parent shell for dialog." );
143
144 Arg args[2];
145 XtSetArg (args[0], XmNdefaultPosition, False);
146 XtSetArg (args[1], XmNautoUnmanage, False);
147 Widget dialogShell =
d3a80c92 148 XmCreateBulletinBoardDialog( parentWidget,
6991087b 149 name.char_str(),
798a4529
MB
150 args, 2);
151 m_mainWidget = (WXWidget) dialogShell;
152
153 // We don't want margins, since there is enough elsewhere.
154 XtVaSetValues( dialogShell,
155 XmNmarginHeight, 0,
156 XmNmarginWidth, 0,
157 XmNresizePolicy, XmRESIZE_NONE,
158 NULL ) ;
159
160 XtTranslations ptr ;
161 XtOverrideTranslations(dialogShell,
162 ptr = XtParseTranslationTable("<Configure>: resize()"));
163 XtFree((char *)ptr);
164
165 XtRealizeWidget(dialogShell);
166
167 wxAddWindowToTable( (Widget)m_mainWidget, this );
168
96be256b 169 return true;
798a4529
MB
170}
171
4bb6408c
JS
172void wxDialog::SetModal(bool flag)
173{
5f9a153c 174 if ( flag )
31df756d
VZ
175 wxModelessWindows.DeleteObject(this);
176 else
177 wxModelessWindows.Append(this);
4bb6408c
JS
178}
179
180wxDialog::~wxDialog()
181{
c6212a0c 182 SendDestroyEvent();
2187eef5 183
a9efc294
VS
184 // if the dialog is modal, this will end its event loop
185 Show(false);
186
7e1bcfa8 187 delete m_eventLoop;
798a4529 188
2e35f56f 189 if (m_mainWidget)
798a4529 190 {
96be256b 191 XtRemoveEventHandler((Widget) m_mainWidget, ExposureMask, False,
798a4529
MB
192 wxUniversalRepaintProc, (XtPointer) this);
193 }
dfe1eee3 194
96be256b 195 m_modalShowing = false;
355b4d3d
WS
196
197#if !wxUSE_INVISIBLE_RESIZE
198 if (m_mainWidget)
dfc54541 199 {
2d120f83 200 XtUnmapWidget((Widget) m_mainWidget);
dfc54541 201 }
355b4d3d 202#endif
2187eef5
MB
203
204 PreDestroy();
dfe1eee3 205
f58585c0 206 if ( m_mainWidget )
cba2db0c 207 {
798a4529
MB
208 wxDeleteWindowFromTable( (Widget)m_mainWidget );
209 XtDestroyWidget( (Widget)m_mainWidget );
dfc54541 210 }
4bb6408c
JS
211}
212
bfc6fde4 213void wxDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 214{
dfc54541 215 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
bfc6fde4 216 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
dfc54541 217 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
4bb6408c
JS
218}
219
ad813b00
JS
220void wxDialog::DoSetClientSize(int width, int height)
221{
222 wxWindow::SetSize(-1, -1, width, height);
223}
224
4bb6408c
JS
225void wxDialog::SetTitle(const wxString& title)
226{
798a4529 227 wxTopLevelWindow::SetTitle( title );
4bb6408c 228
798a4529
MB
229 if( !title.empty() )
230 {
231 wxXmString str( title );
232 XtVaSetValues( (Widget)m_mainWidget,
6991087b 233 XmNtitle, (const char*)title.mb_str(),
9b135950 234 XmNdialogTitle, str(),
6991087b 235 XmNiconName, (const char*)title.mb_str(),
798a4529 236 NULL );
dfc54541 237 }
dfc54541
JS
238}
239
798a4529 240bool wxDialog::Show( bool show )
dfc54541 241{
5a2e3d8c
MB
242 if( !wxWindowBase::Show( show ) )
243 return false;
dfc54541 244
a9efc294
VS
245 if ( !show && IsModal() )
246 EndModal(wxID_CANCEL);
247
dfc54541 248 m_isShown = show;
dfe1eee3 249
b3090d48
MB
250 if (show)
251 {
3aa8e4ea
JS
252 if (CanDoLayoutAdaptation())
253 DoLayoutAdaptation();
254
b3090d48
MB
255 // this usually will result in TransferDataToWindow() being called
256 // which will change the controls values so do it before showing as
257 // otherwise we could have some flicker
258 InitDialog();
259 }
260
dfc54541
JS
261 if (show)
262 {
355b4d3d
WS
263#if !wxUSE_INVISIBLE_RESIZE
264 XtMapWidget(XtParent((Widget) m_mainWidget));
265#else
266 XtManageChild((Widget)m_mainWidget) ;
267#endif
dfe1eee3 268
55034339 269 XRaiseWindow( XtDisplay( (Widget)m_mainWidget ),
798a4529 270 XtWindow( (Widget)m_mainWidget) );
dfe1eee3 271
dfc54541
JS
272 }
273 else
274 {
355b4d3d
WS
275#if !wxUSE_INVISIBLE_RESIZE
276 XtUnmapWidget(XtParent((Widget) m_mainWidget));
277#else
278 XtUnmanageChild((Widget)m_mainWidget) ;
279#endif
dfe1eee3 280
eb6fa4b4 281 XFlush(XtDisplay((Widget)m_mainWidget));
96be256b 282 XSync(XtDisplay((Widget)m_mainWidget), False);
dfc54541 283 }
dfe1eee3 284
96be256b 285 return true;
dfc54541
JS
286}
287
288// Shows a dialog modally, returning a return code
4bb6408c
JS
289int wxDialog::ShowModal()
290{
96be256b 291 Show(true);
dfe1eee3 292
eb6fa4b4
MB
293 // after the event loop ran, the widget might already have been destroyed
294 WXDisplay* display = (WXDisplay*)XtDisplay( (Widget)m_mainWidget );
295
dfc54541
JS
296 if (m_modalShowing)
297 return 0;
7e1bcfa8 298 m_eventLoop = new wxEventLoop;
dfe1eee3 299
96be256b
MB
300 m_modalShowing = true;
301 XtAddGrab((Widget) m_mainWidget, True, False);
dfe1eee3 302
7e1bcfa8 303 m_eventLoop->Run();
dfe1eee3 304
dfc54541 305 // Now process all events in case they get sent to a destroyed dialog
eb6fa4b4 306 wxFlushEvents( display );
dfe1eee3 307
5276b0a5 308 wxDELETE(m_eventLoop);
7e1bcfa8 309
dfc54541
JS
310 // TODO: is it safe to call this, if the dialog may have been deleted
311 // by now? Probably only if we're using delayed deletion of dialogs.
312 return GetReturnCode();
4bb6408c
JS
313}
314
315void wxDialog::EndModal(int retCode)
316{
dfc54541
JS
317 if (!m_modalShowing)
318 return;
dfe1eee3 319
dfc54541 320 SetReturnCode(retCode);
dfe1eee3 321
88150e60
JS
322 // Strangely, we don't seem to need this now.
323 // XtRemoveGrab((Widget) m_mainWidget);
dfe1eee3 324
96be256b 325 Show(false);
dfe1eee3 326
96be256b 327 m_modalShowing = false;
7e1bcfa8 328 m_eventLoop->Exit();
e95b8d5a
JS
329
330 SetModal(false);
4bb6408c
JS
331}
332
4bb6408c
JS
333// Destroy the window (delayed, if a managed window)
334bool wxDialog::Destroy()
335{
2d120f83
JS
336 if (!wxPendingDelete.Member(this))
337 wxPendingDelete.Append(this);
96be256b 338 return true;
4bb6408c
JS
339}
340
94b49b93 341void wxDialog::ChangeFont(bool keepOriginalSize)
0d57be45 342{
94b49b93 343 wxWindow::ChangeFont(keepOriginalSize);
0d57be45
JS
344}
345
346void wxDialog::ChangeBackgroundColour()
347{
94b49b93 348 if (GetMainWidget())
a8680e3e 349 wxDoChangeBackgroundColour(GetMainWidget(), m_backgroundColour);
0d57be45
JS
350}
351
352void wxDialog::ChangeForegroundColour()
353{
94b49b93 354 if (GetMainWidget())
a8680e3e 355 wxDoChangeForegroundColour(GetMainWidget(), m_foregroundColour);
0d57be45 356}