]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/dialog.cpp
fixed compilation under Motif
[wxWidgets.git] / src / gtk1 / dialog.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: dialog.cpp
3// Purpose:
4// Author: Robert Roebling
a81258be 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
2b854a32 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "dialog.h"
12#endif
13
14#include "wx/dialog.h"
15#include "wx/frame.h"
16#include "wx/app.h"
83624f79
RR
17
18#include "gdk/gdk.h"
19#include "gtk/gtk.h"
c801d85f
KB
20#include "wx/gtk/win_gtk.h"
21
e2414cbe
RR
22//-----------------------------------------------------------------------------
23
24extern wxList wxPendingDelete;
25
c801d85f 26//-----------------------------------------------------------------------------
e1e955e1
RR
27// "delete_event"
28//-----------------------------------------------------------------------------
c801d85f
KB
29
30bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
2b854a32 31{
c801d85f 32/*
fb1585ae
RR
33 printf( "OnDelete from " );
34 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
35 printf( win->GetClassInfo()->GetClassName() );
36 printf( ".\n" );
c801d85f 37*/
2b854a32 38
fb1585ae 39 win->Close();
c801d85f 40
fb1585ae 41 return TRUE;
c33c4050 42}
c801d85f 43
e52f60e6
RR
44//-----------------------------------------------------------------------------
45// "size_allocate"
46//-----------------------------------------------------------------------------
47
48static void gtk_dialog_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxDialog *win )
49{
50 if (!win->HasVMT()) return;
51
52/*
53 printf( "OnDialogResize from " );
54 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
55 printf( win->GetClassInfo()->GetClassName() );
56 printf( ".\n" );
57*/
58
de8113d9
RR
59 if ((win->m_width != alloc->width) || (win->m_height != alloc->height))
60 {
61 win->m_sizeSet = FALSE;
62 win->m_width = alloc->width;
63 win->m_height = alloc->height;
64 }
e52f60e6
RR
65}
66
36b3b54a
RR
67//-----------------------------------------------------------------------------
68// "configure_event"
69//-----------------------------------------------------------------------------
70
71static gint gtk_dialog_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *event, wxDialog *win )
72{
73 if (!win->HasVMT()) return FALSE;
74
75 win->m_x = event->x;
76 win->m_y = event->y;
77
78 wxMoveEvent mevent( wxPoint(win->m_x,win->m_y), win->GetId() );
79 mevent.SetEventObject( win );
80 win->GetEventHandler()->ProcessEvent( mevent );
81
82 return FALSE;
83}
84
c801d85f
KB
85//-----------------------------------------------------------------------------
86// wxDialog
87//-----------------------------------------------------------------------------
88
a60c99e6 89BEGIN_EVENT_TABLE(wxDialog,wxPanel)
fb1585ae
RR
90 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
91 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
92 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
e52f60e6 93 EVT_SIZE (wxDialog::OnSize)
fb1585ae 94 EVT_CLOSE (wxDialog::OnCloseWindow)
c801d85f
KB
95END_EVENT_TABLE()
96
a60c99e6 97IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
c801d85f 98
68995f26 99void wxDialog::Init()
c801d85f 100{
de8113d9 101 m_sizeSet = FALSE;
fb1585ae 102 m_modalShowing = FALSE;
c33c4050 103}
c801d85f 104
2b854a32 105wxDialog::wxDialog( wxWindow *parent,
fb1585ae 106 wxWindowID id, const wxString &title,
2b854a32 107 const wxPoint &pos, const wxSize &size,
fb1585ae 108 long style, const wxString &name )
c801d85f 109{
68995f26
VZ
110 Init();
111
fb1585ae 112 Create( parent, id, title, pos, size, style, name );
c33c4050 113}
c801d85f
KB
114
115bool wxDialog::Create( wxWindow *parent,
fb1585ae 116 wxWindowID id, const wxString &title,
2b854a32 117 const wxPoint &pos, const wxSize &size,
fb1585ae 118 long style, const wxString &name )
c801d85f 119{
a802c3a1 120 wxTopLevelWindows.Append( this );
2b854a32 121
fb1585ae 122 m_needParent = FALSE;
2b854a32 123
fb1585ae 124 PreCreation( parent, id, pos, size, style, name );
2b854a32 125
fb1585ae
RR
126 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
127 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
2b854a32 128
fb1585ae 129 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL);
2b854a32
VZ
130
131 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
fb1585ae 132 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
2b854a32 133
fb1585ae
RR
134 m_wxwindow = gtk_myfixed_new();
135 gtk_widget_show( m_wxwindow );
136 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
2b854a32 137
fb1585ae 138 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
2b854a32 139
fb1585ae 140 SetTitle( title );
2b854a32 141
de8113d9 142 if (m_parent) m_parent->AddChild( this );
2b854a32 143
de8113d9 144 PostCreation();
e146b8c8 145
de8113d9 146 gtk_widget_realize( m_widget );
2b854a32 147
ab2b3dd4
RR
148 /* comments see wxFrame */
149
150 long decor = (long) GDK_DECOR_ALL;
151 long func = (long) GDK_FUNC_ALL;
152 if ((m_windowStyle & wxCAPTION) == 0)
034be888 153 decor |= GDK_DECOR_TITLE;
ab2b3dd4 154 if ((m_windowStyle & wxMINIMIZE) == 0)
034be888 155 func |= GDK_FUNC_MINIMIZE;
ab2b3dd4 156 if ((m_windowStyle & wxMAXIMIZE) == 0)
034be888 157 func |= GDK_FUNC_MAXIMIZE;
ab2b3dd4 158 if ((m_windowStyle & wxSYSTEM_MENU) == 0)
034be888 159 decor |= GDK_DECOR_MENU;
ab2b3dd4 160 if ((m_windowStyle & wxMINIMIZE_BOX) == 0)
034be888 161 decor |= GDK_DECOR_MINIMIZE;
ab2b3dd4 162 if ((m_windowStyle & wxMAXIMIZE_BOX) == 0)
034be888 163 decor |= GDK_DECOR_MAXIMIZE;
ab2b3dd4 164 if ((m_windowStyle & wxRESIZE_BORDER) == 0)
034be888 165 func |= GDK_FUNC_RESIZE;
ab2b3dd4
RR
166 gdk_window_set_decorations(m_widget->window, (GdkWMDecoration)decor);
167 gdk_window_set_functions(m_widget->window, (GdkWMFunction)func);
034be888 168
2b854a32 169 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
36b3b54a 170 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
2b854a32 171
36b3b54a
RR
172 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
173 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this );
174
fb1585ae 175 return TRUE;
c33c4050 176}
c801d85f 177
43a18898 178wxDialog::~wxDialog()
c801d85f 179{
fb1585ae 180 wxTopLevelWindows.DeleteObject( this );
2b854a32 181
0d2a2b60
RR
182 if (wxTheApp->GetTopWindow() == this)
183 {
184 wxTheApp->SetTopWindow( (wxWindow*) NULL );
185 }
2b854a32 186
0d2a2b60 187 if (wxTopLevelWindows.Number() == 0)
2b854a32 188 {
0d2a2b60
RR
189 wxTheApp->ExitMainLoop();
190 }
c33c4050 191}
c801d85f 192
43a18898 193void wxDialog::SetTitle( const wxString& title )
c801d85f 194{
fb1585ae
RR
195 m_title = title;
196 if (m_title.IsNull()) m_title = "";
197 gtk_window_set_title( GTK_WINDOW(m_widget), m_title );
c33c4050 198}
c801d85f 199
43a18898 200wxString wxDialog::GetTitle() const
c801d85f 201{
fb1585ae 202 return (wxString&)m_title;
c33c4050 203}
c801d85f
KB
204
205void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
206{
fb1585ae 207 if (Validate()) TransferDataFromWindow();
c33c4050 208}
c801d85f
KB
209
210void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
211{
fb1585ae
RR
212 if (IsModal())
213 {
214 EndModal(wxID_CANCEL);
215 }
216 else
217 {
218 SetReturnCode(wxID_CANCEL);
219 this->Show(FALSE);
220 }
c33c4050 221}
c801d85f 222
903f689b 223void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
c801d85f 224{
fb1585ae 225 if ( Validate() && TransferDataFromWindow())
1a6944fd 226 {
2b854a32 227 if (IsModal())
fb1585ae
RR
228 {
229 EndModal(wxID_OK);
230 }
231 else
232 {
233 SetReturnCode(wxID_OK);
234 this->Show(FALSE);
235 }
1a6944fd 236 }
c33c4050 237}
c801d85f
KB
238
239void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
240{
2b854a32 241 // yes
c33c4050 242}
c801d85f 243
2b854a32 244void wxDialog::OnCloseWindow(wxCloseEvent& event)
c801d85f 245{
e3065973
JS
246 // We'll send a Cancel message by default,
247 // which may close the dialog.
248 // Check for looping if the Cancel event handler calls Close().
249
250 // Note that if a cancel button and handler aren't present in the dialog,
251 // nothing will happen when you close the dialog via the window manager, or
252 // via Close().
253 // We wouldn't want to destroy the dialog by default, since the dialog may have been
254 // created on the stack.
255 // However, this does mean that calling dialog->Close() won't delete the dialog
256 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
257 // sure to destroy the dialog.
258 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
259
ab2b3dd4 260 static wxList s_closing;
c801d85f 261
ab2b3dd4 262 if (s_closing.Member(this))
2b854a32
VZ
263 return; // no loops
264
ab2b3dd4 265 s_closing.Append(this);
c801d85f 266
fb1585ae
RR
267 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
268 cancelEvent.SetEventObject( this );
269 GetEventHandler()->ProcessEvent(cancelEvent);
ab2b3dd4 270 s_closing.DeleteObject(this);
c801d85f
KB
271}
272
43a18898 273bool wxDialog::Destroy()
e2414cbe 274{
fb1585ae 275 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
e2414cbe 276
fb1585ae 277 return TRUE;
e2414cbe
RR
278}
279
e52f60e6
RR
280void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
281{
2830bf19 282 wxASSERT_MSG( (m_widget != NULL), "invalid dialog" );
2b854a32 283
e52f60e6
RR
284 if (GetAutoLayout())
285 {
286 Layout();
287 }
2b854a32 288 else
e52f60e6 289 {
de8113d9 290 /* no child: go out ! */
db1b4961 291 if (!GetChildren().First()) return;
2b854a32 292
de8113d9 293 /* do we have exactly one child? */
e52f60e6 294 wxWindow *child = (wxWindow *) NULL;
db1b4961 295 for(wxNode *node = GetChildren().First(); node; node = node->Next())
e52f60e6
RR
296 {
297 wxWindow *win = (wxWindow *)node->Data();
de135918 298 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
e52f60e6 299 {
de8113d9 300 /* it's the second one: do nothing */
e52f60e6
RR
301 if (child) return;
302 child = win;
303 }
304 }
305
de8113d9 306 /* yes: set it's size to fill all the frame */
e52f60e6
RR
307 int client_x, client_y;
308 GetClientSize( &client_x, &client_y );
309 child->SetSize( 1, 1, client_x-2, client_y);
310 }
311}
312
bfc6fde4 313void wxDialog::DoSetSize( int x, int y, int width, int height, int sizeFlags )
903f689b 314{
de8113d9
RR
315 wxASSERT_MSG( (m_widget != NULL), "invalid dialog" );
316 wxASSERT_MSG( (m_wxwindow != NULL), "invalid dialog" );
fb1585ae 317
de8113d9 318 if (m_resizing) return; /* I don't like recursions */
fb1585ae
RR
319 m_resizing = TRUE;
320
321 int old_x = m_x;
322 int old_y = m_y;
323 int old_width = m_width;
324 int old_height = m_height;
2b854a32 325
fb1585ae
RR
326 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
327 {
328 if (x != -1) m_x = x;
329 if (y != -1) m_y = y;
330 if (width != -1) m_width = width;
331 if (height != -1) m_height = height;
332 }
333 else
334 {
335 m_x = x;
336 m_y = y;
337 m_width = width;
338 m_height = height;
339 }
340
341 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
342 {
343 if (width == -1) m_width = 80;
344 }
345
346 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
347 {
348 if (height == -1) m_height = 26;
349 }
2b854a32 350
fb1585ae
RR
351 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
352 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
0c77152e
RR
353 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
354 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
fb1585ae
RR
355
356 if ((m_x != -1) || (m_y != -1))
357 {
2b854a32 358 if ((m_x != old_x) || (m_y != old_y))
de8113d9
RR
359 {
360 /* m_sizeSet = FALSE; */
e146b8c8 361 gtk_widget_set_uposition( m_widget, m_x, m_y );
de8113d9 362 }
fb1585ae 363 }
2b854a32 364
fb1585ae
RR
365 if ((m_width != old_width) || (m_height != old_height))
366 {
de8113d9 367 m_sizeSet = FALSE;
fb1585ae 368 }
2b854a32 369
fb1585ae 370 m_resizing = FALSE;
903f689b
RR
371}
372
de8113d9
RR
373void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
374{
375 // due to a bug in gtk, x,y are always 0
376 // m_x = x;
377 // m_y = y;
378
379 if ((m_height == height) && (m_width == width) && (m_sizeSet)) return;
380 if (!m_wxwindow) return;
381
382 m_width = width;
383 m_height = height;
384
385 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
386 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
387 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
388 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
389
390 /* we actually set the size of a frame here and no-where else */
391 gtk_widget_set_usize( m_widget, m_width, m_height );
392
393 m_sizeSet = TRUE;
394
395 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
396 event.SetEventObject( this );
397 GetEventHandler()->ProcessEvent( event );
398}
399
903f689b
RR
400void wxDialog::Centre( int direction )
401{
f5eafd0e 402 wxASSERT_MSG( (m_widget != NULL), "invalid dialog" );
2b854a32 403
43a18898
RR
404 int x = 0;
405 int y = 0;
2b854a32 406
f5eafd0e
RR
407 if ((direction & wxHORIZONTAL) == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
408 if ((direction & wxVERTICAL) == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
2b854a32 409
fb1585ae 410 Move( x, y );
903f689b
RR
411}
412
de8113d9
RR
413void wxDialog::OnInternalIdle()
414{
415 if (!m_sizeSet)
416 GtkOnSize( m_x, m_y, m_width, m_height );
417}
418
debe6624 419bool wxDialog::Show( bool show )
c801d85f 420{
fb1585ae
RR
421 if (!show && IsModal())
422 {
de8113d9 423 EndModal( wxID_CANCEL );
fb1585ae 424 }
c801d85f 425
de8113d9
RR
426 if (show && !m_sizeSet)
427 {
428 /* by calling GtkOnSize here, we don't have to call
429 either after showing the frame, which would entail
430 much ugly flicker nor from within the size_allocate
431 handler, because GTK 1.1.X forbids that. */
432
433 GtkOnSize( m_x, m_y, m_width, m_height );
434 }
2b854a32 435
de8113d9 436 wxWindow::Show( show );
e146b8c8 437
fb1585ae 438 if (show) InitDialog();
2b854a32 439
fb1585ae 440 return TRUE;
c33c4050
RR
441}
442
43a18898 443bool wxDialog::IsModal() const
e1e955e1 444{
fb1585ae 445 return m_modalShowing;
e1e955e1
RR
446}
447
448void wxDialog::SetModal( bool WXUNUSED(flag) )
c33c4050 449{
e1e955e1 450/*
c33c4050
RR
451 if (flag)
452 m_windowStyle |= wxDIALOG_MODAL;
453 else
454 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
e1e955e1 455*/
fb1585ae 456 wxFAIL_MSG( "wxDialog:SetModal obsolete now" );
c33c4050 457}
c801d85f 458
43a18898 459int wxDialog::ShowModal()
c801d85f 460{
fb1585ae
RR
461 if (IsModal())
462 {
463 wxFAIL_MSG( "wxDialog:ShowModal called twice" );
464 return GetReturnCode();
465 }
e146b8c8 466
fb1585ae 467 Show( TRUE );
2b854a32 468
fb1585ae 469 m_modalShowing = TRUE;
2b854a32 470
fb1585ae
RR
471 gtk_grab_add( m_widget );
472 gtk_main();
473 gtk_grab_remove( m_widget );
2b854a32 474
fb1585ae 475 return GetReturnCode();
c33c4050 476}
c801d85f
KB
477
478void wxDialog::EndModal( int retCode )
479{
fb1585ae 480 SetReturnCode( retCode );
2b854a32 481
fb1585ae
RR
482 if (!IsModal())
483 {
484 wxFAIL_MSG( "wxDialog:EndModal called twice" );
485 return;
486 }
2b854a32 487
fb1585ae 488 m_modalShowing = FALSE;
2b854a32 489
fb1585ae 490 gtk_main_quit();
2b854a32 491
fb1585ae 492 Show( FALSE );
c33c4050 493}
c801d85f 494
43a18898 495void wxDialog::InitDialog()
c801d85f 496{
fb1585ae 497 wxWindow::InitDialog();
c33c4050
RR
498}
499
c33c4050
RR
500void wxDialog::SetIcon( const wxIcon &icon )
501{
fb1585ae
RR
502 m_icon = icon;
503 if (!icon.Ok()) return;
2b854a32 504
fb1585ae
RR
505 wxMask *mask = icon.GetMask();
506 GdkBitmap *bm = (GdkBitmap *) NULL;
507 if (mask) bm = mask->GetBitmap();
2b854a32 508
fb1585ae 509 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
c33c4050 510}