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