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