]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dialog.cpp
log messages given during program initialization are not discarded any more
[wxWidgets.git] / src / gtk / 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
ca35e608
RR
148 /* all this is for Motif Window Manager "hints" and is supposed to be
149 recognized by other WM as well. not tested. */
ab2b3dd4
RR
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;
ca35e608 154/*
ab2b3dd4 155 if ((m_windowStyle & wxMINIMIZE) == 0)
034be888 156 func |= GDK_FUNC_MINIMIZE;
ab2b3dd4 157 if ((m_windowStyle & wxMAXIMIZE) == 0)
034be888 158 func |= GDK_FUNC_MAXIMIZE;
ca35e608 159*/
ab2b3dd4 160 if ((m_windowStyle & wxSYSTEM_MENU) == 0)
034be888 161 decor |= GDK_DECOR_MENU;
ab2b3dd4 162 if ((m_windowStyle & wxMINIMIZE_BOX) == 0)
034be888 163 decor |= GDK_DECOR_MINIMIZE;
ab2b3dd4 164 if ((m_windowStyle & wxMAXIMIZE_BOX) == 0)
034be888 165 decor |= GDK_DECOR_MAXIMIZE;
ab2b3dd4 166 if ((m_windowStyle & wxRESIZE_BORDER) == 0)
034be888 167 func |= GDK_FUNC_RESIZE;
ab2b3dd4
RR
168 gdk_window_set_decorations(m_widget->window, (GdkWMDecoration)decor);
169 gdk_window_set_functions(m_widget->window, (GdkWMFunction)func);
034be888 170
2b854a32 171 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
36b3b54a 172 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
2b854a32 173
36b3b54a
RR
174 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
175 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this );
176
fb1585ae 177 return TRUE;
c33c4050 178}
c801d85f 179
43a18898 180wxDialog::~wxDialog()
c801d85f 181{
fb1585ae 182 wxTopLevelWindows.DeleteObject( this );
2b854a32 183
0d2a2b60
RR
184 if (wxTheApp->GetTopWindow() == this)
185 {
186 wxTheApp->SetTopWindow( (wxWindow*) NULL );
187 }
2b854a32 188
0d2a2b60 189 if (wxTopLevelWindows.Number() == 0)
2b854a32 190 {
0d2a2b60
RR
191 wxTheApp->ExitMainLoop();
192 }
c33c4050 193}
c801d85f 194
43a18898 195void wxDialog::SetTitle( const wxString& title )
c801d85f 196{
fb1585ae 197 m_title = title;
ed9b9841
OK
198 if (m_title.IsNull()) m_title = _T("");
199 gtk_window_set_title( GTK_WINDOW(m_widget), m_title.mbc_str() );
c33c4050 200}
c801d85f 201
43a18898 202wxString wxDialog::GetTitle() const
c801d85f 203{
fb1585ae 204 return (wxString&)m_title;
c33c4050 205}
c801d85f
KB
206
207void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
208{
fb1585ae 209 if (Validate()) TransferDataFromWindow();
c33c4050 210}
c801d85f
KB
211
212void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
213{
fb1585ae
RR
214 if (IsModal())
215 {
216 EndModal(wxID_CANCEL);
217 }
218 else
219 {
220 SetReturnCode(wxID_CANCEL);
221 this->Show(FALSE);
222 }
c33c4050 223}
c801d85f 224
903f689b 225void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
c801d85f 226{
fb1585ae 227 if ( Validate() && TransferDataFromWindow())
1a6944fd 228 {
2b854a32 229 if (IsModal())
fb1585ae
RR
230 {
231 EndModal(wxID_OK);
232 }
233 else
234 {
235 SetReturnCode(wxID_OK);
236 this->Show(FALSE);
237 }
1a6944fd 238 }
c33c4050 239}
c801d85f
KB
240
241void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
242{
2b854a32 243 // yes
c33c4050 244}
c801d85f 245
2b854a32 246void wxDialog::OnCloseWindow(wxCloseEvent& event)
c801d85f 247{
e3065973
JS
248 // We'll send a Cancel message by default,
249 // which may close the dialog.
250 // Check for looping if the Cancel event handler calls Close().
251
252 // Note that if a cancel button and handler aren't present in the dialog,
253 // nothing will happen when you close the dialog via the window manager, or
254 // via Close().
255 // We wouldn't want to destroy the dialog by default, since the dialog may have been
256 // created on the stack.
257 // However, this does mean that calling dialog->Close() won't delete the dialog
258 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
259 // sure to destroy the dialog.
260 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
261
ab2b3dd4 262 static wxList s_closing;
c801d85f 263
ab2b3dd4 264 if (s_closing.Member(this))
2b854a32
VZ
265 return; // no loops
266
ab2b3dd4 267 s_closing.Append(this);
c801d85f 268
fb1585ae
RR
269 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
270 cancelEvent.SetEventObject( this );
271 GetEventHandler()->ProcessEvent(cancelEvent);
ab2b3dd4 272 s_closing.DeleteObject(this);
c801d85f
KB
273}
274
43a18898 275bool wxDialog::Destroy()
e2414cbe 276{
fb1585ae 277 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
e2414cbe 278
fb1585ae 279 return TRUE;
e2414cbe
RR
280}
281
e52f60e6
RR
282void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
283{
ed9b9841 284 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
2b854a32 285
e52f60e6
RR
286 if (GetAutoLayout())
287 {
288 Layout();
289 }
2b854a32 290 else
e52f60e6 291 {
de8113d9 292 /* no child: go out ! */
db1b4961 293 if (!GetChildren().First()) return;
2b854a32 294
de8113d9 295 /* do we have exactly one child? */
e52f60e6 296 wxWindow *child = (wxWindow *) NULL;
db1b4961 297 for(wxNode *node = GetChildren().First(); node; node = node->Next())
e52f60e6
RR
298 {
299 wxWindow *win = (wxWindow *)node->Data();
de135918 300 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
e52f60e6 301 {
de8113d9 302 /* it's the second one: do nothing */
e52f60e6
RR
303 if (child) return;
304 child = win;
305 }
306 }
307
de8113d9 308 /* yes: set it's size to fill all the frame */
e52f60e6
RR
309 int client_x, client_y;
310 GetClientSize( &client_x, &client_y );
311 child->SetSize( 1, 1, client_x-2, client_y);
312 }
313}
314
bfc6fde4 315void wxDialog::DoSetSize( int x, int y, int width, int height, int sizeFlags )
903f689b 316{
ed9b9841
OK
317 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
318 wxASSERT_MSG( (m_wxwindow != NULL), _T("invalid dialog") );
fb1585ae 319
de8113d9 320 if (m_resizing) return; /* I don't like recursions */
fb1585ae
RR
321 m_resizing = TRUE;
322
323 int old_x = m_x;
324 int old_y = m_y;
325 int old_width = m_width;
326 int old_height = m_height;
2b854a32 327
fb1585ae
RR
328 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
329 {
330 if (x != -1) m_x = x;
331 if (y != -1) m_y = y;
332 if (width != -1) m_width = width;
333 if (height != -1) m_height = height;
334 }
335 else
336 {
337 m_x = x;
338 m_y = y;
339 m_width = width;
340 m_height = height;
341 }
342
343 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
344 {
345 if (width == -1) m_width = 80;
346 }
347
348 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
349 {
350 if (height == -1) m_height = 26;
351 }
2b854a32 352
fb1585ae
RR
353 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
354 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
0c77152e
RR
355 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
356 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
fb1585ae
RR
357
358 if ((m_x != -1) || (m_y != -1))
359 {
2b854a32 360 if ((m_x != old_x) || (m_y != old_y))
de8113d9
RR
361 {
362 /* m_sizeSet = FALSE; */
e146b8c8 363 gtk_widget_set_uposition( m_widget, m_x, m_y );
de8113d9 364 }
fb1585ae 365 }
2b854a32 366
fb1585ae
RR
367 if ((m_width != old_width) || (m_height != old_height))
368 {
de8113d9 369 m_sizeSet = FALSE;
fb1585ae 370 }
2b854a32 371
fb1585ae 372 m_resizing = FALSE;
903f689b
RR
373}
374
de8113d9
RR
375void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
376{
377 // due to a bug in gtk, x,y are always 0
378 // m_x = x;
379 // m_y = y;
380
381 if ((m_height == height) && (m_width == width) && (m_sizeSet)) return;
382 if (!m_wxwindow) return;
383
384 m_width = width;
385 m_height = height;
386
387 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
388 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
389 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
390 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
391
392 /* we actually set the size of a frame here and no-where else */
393 gtk_widget_set_usize( m_widget, m_width, m_height );
394
395 m_sizeSet = TRUE;
396
397 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
398 event.SetEventObject( this );
399 GetEventHandler()->ProcessEvent( event );
400}
401
903f689b
RR
402void wxDialog::Centre( int direction )
403{
ed9b9841 404 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
2b854a32 405
43a18898
RR
406 int x = 0;
407 int y = 0;
2b854a32 408
f5eafd0e
RR
409 if ((direction & wxHORIZONTAL) == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
410 if ((direction & wxVERTICAL) == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
2b854a32 411
fb1585ae 412 Move( x, y );
903f689b
RR
413}
414
de8113d9
RR
415void wxDialog::OnInternalIdle()
416{
417 if (!m_sizeSet)
418 GtkOnSize( m_x, m_y, m_width, m_height );
419}
420
debe6624 421bool wxDialog::Show( bool show )
c801d85f 422{
fb1585ae
RR
423 if (!show && IsModal())
424 {
de8113d9 425 EndModal( wxID_CANCEL );
fb1585ae 426 }
c801d85f 427
de8113d9
RR
428 if (show && !m_sizeSet)
429 {
430 /* by calling GtkOnSize here, we don't have to call
431 either after showing the frame, which would entail
432 much ugly flicker nor from within the size_allocate
433 handler, because GTK 1.1.X forbids that. */
434
435 GtkOnSize( m_x, m_y, m_width, m_height );
436 }
2b854a32 437
de8113d9 438 wxWindow::Show( show );
e146b8c8 439
fb1585ae 440 if (show) InitDialog();
2b854a32 441
fb1585ae 442 return TRUE;
c33c4050
RR
443}
444
43a18898 445bool wxDialog::IsModal() const
e1e955e1 446{
fb1585ae 447 return m_modalShowing;
e1e955e1
RR
448}
449
450void wxDialog::SetModal( bool WXUNUSED(flag) )
c33c4050 451{
e1e955e1 452/*
c33c4050
RR
453 if (flag)
454 m_windowStyle |= wxDIALOG_MODAL;
455 else
456 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
e1e955e1 457*/
ed9b9841 458 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
c33c4050 459}
c801d85f 460
43a18898 461int wxDialog::ShowModal()
c801d85f 462{
fb1585ae
RR
463 if (IsModal())
464 {
ed9b9841 465 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
fb1585ae
RR
466 return GetReturnCode();
467 }
e146b8c8 468
fb1585ae 469 Show( TRUE );
2b854a32 470
fb1585ae 471 m_modalShowing = TRUE;
2b854a32 472
fb1585ae
RR
473 gtk_grab_add( m_widget );
474 gtk_main();
475 gtk_grab_remove( m_widget );
2b854a32 476
fb1585ae 477 return GetReturnCode();
c33c4050 478}
c801d85f
KB
479
480void wxDialog::EndModal( int retCode )
481{
fb1585ae 482 SetReturnCode( retCode );
2b854a32 483
fb1585ae
RR
484 if (!IsModal())
485 {
ed9b9841 486 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
fb1585ae
RR
487 return;
488 }
2b854a32 489
fb1585ae 490 m_modalShowing = FALSE;
2b854a32 491
fb1585ae 492 gtk_main_quit();
2b854a32 493
fb1585ae 494 Show( FALSE );
c33c4050 495}
c801d85f 496
43a18898 497void wxDialog::InitDialog()
c801d85f 498{
fb1585ae 499 wxWindow::InitDialog();
c33c4050
RR
500}
501
c33c4050
RR
502void wxDialog::SetIcon( const wxIcon &icon )
503{
fb1585ae
RR
504 m_icon = icon;
505 if (!icon.Ok()) return;
2b854a32 506
fb1585ae
RR
507 wxMask *mask = icon.GetMask();
508 GdkBitmap *bm = (GdkBitmap *) NULL;
509 if (mask) bm = mask->GetBitmap();
2b854a32 510
fb1585ae 511 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
c33c4050 512}