]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/dialog.cpp
Backgrounds work again
[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
2b07d713
RR
85//-----------------------------------------------------------------------------
86// "realize" from m_widget
87//-----------------------------------------------------------------------------
88
58dea4b0 89/* we cannot MWM hints and icons before the widget has been realized,
2b07d713
RR
90 so we do this directly after realization */
91
92static gint
58dea4b0 93gtk_dialog_realized_callback( GtkWidget *widget, wxDialog *win )
2b07d713
RR
94{
95 /* all this is for Motif Window Manager "hints" and is supposed to be
96 recognized by other WM as well. not tested. */
97 long decor = (long) GDK_DECOR_ALL;
98 long func = (long) GDK_FUNC_ALL;
99
100 if ((win->m_windowStyle & wxCAPTION) == 0)
101 decor |= GDK_DECOR_TITLE;
102/* if ((win->m_windowStyle & wxMINIMIZE) == 0)
103 func |= GDK_FUNC_MINIMIZE;
104 if ((win->m_windowStyle & wxMAXIMIZE) == 0)
105 func |= GDK_FUNC_MAXIMIZE; */
106 if ((win->m_windowStyle & wxSYSTEM_MENU) == 0)
107 decor |= GDK_DECOR_MENU;
108 if ((win->m_windowStyle & wxMINIMIZE_BOX) == 0)
109 decor |= GDK_DECOR_MINIMIZE;
110 if ((win->m_windowStyle & wxMAXIMIZE_BOX) == 0)
111 decor |= GDK_DECOR_MAXIMIZE;
112 if ((win->m_windowStyle & wxRESIZE_BORDER) == 0)
113 func |= GDK_FUNC_RESIZE;
114
115 gdk_window_set_decorations( win->m_widget->window, (GdkWMDecoration)decor);
116 gdk_window_set_functions( win->m_widget->window, (GdkWMFunction)func);
117
118 /* GTK's shrinking/growing policy */
119 if ((win->m_windowStyle & wxRESIZE_BORDER) == 0)
120 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 0, 0, 1);
121 else
122 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 1, 1, 1);
123
58dea4b0
RR
124 /* reset the icon */
125 if (win->m_icon != wxNullIcon)
126 {
127 wxIcon icon( win->m_icon );
128 win->m_icon = wxNullIcon;
129 win->SetIcon( icon );
130 }
131
2b07d713
RR
132 return FALSE;
133}
134
c801d85f
KB
135//-----------------------------------------------------------------------------
136// wxDialog
137//-----------------------------------------------------------------------------
138
a60c99e6 139BEGIN_EVENT_TABLE(wxDialog,wxPanel)
fb1585ae
RR
140 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
141 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
142 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
e52f60e6 143 EVT_SIZE (wxDialog::OnSize)
fb1585ae 144 EVT_CLOSE (wxDialog::OnCloseWindow)
c801d85f
KB
145END_EVENT_TABLE()
146
a60c99e6 147IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
c801d85f 148
68995f26 149void wxDialog::Init()
c801d85f 150{
de8113d9 151 m_sizeSet = FALSE;
fb1585ae 152 m_modalShowing = FALSE;
c33c4050 153}
c801d85f 154
2b854a32 155wxDialog::wxDialog( wxWindow *parent,
fb1585ae 156 wxWindowID id, const wxString &title,
2b854a32 157 const wxPoint &pos, const wxSize &size,
fb1585ae 158 long style, const wxString &name )
c801d85f 159{
68995f26
VZ
160 Init();
161
fb1585ae 162 Create( parent, id, title, pos, size, style, name );
c33c4050 163}
c801d85f
KB
164
165bool wxDialog::Create( wxWindow *parent,
fb1585ae 166 wxWindowID id, const wxString &title,
2b854a32 167 const wxPoint &pos, const wxSize &size,
fb1585ae 168 long style, const wxString &name )
c801d85f 169{
a802c3a1 170 wxTopLevelWindows.Append( this );
2b854a32 171
fb1585ae 172 m_needParent = FALSE;
2b854a32 173
fb1585ae 174 PreCreation( parent, id, pos, size, style, name );
2b854a32 175
fb1585ae
RR
176 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
177 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
2b854a32 178
fb1585ae 179 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL);
2b854a32
VZ
180
181 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
fb1585ae 182 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
2b854a32 183
fb1585ae
RR
184 m_wxwindow = gtk_myfixed_new();
185 gtk_widget_show( m_wxwindow );
186 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
2b854a32 187
fb1585ae 188 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
2b854a32 189
fb1585ae 190 SetTitle( title );
2b854a32 191
de8113d9 192 if (m_parent) m_parent->AddChild( this );
2b854a32 193
de8113d9 194 PostCreation();
e146b8c8 195
2b07d713
RR
196 /* we cannot set MWM hints before the widget has
197 been realized, so we do this directly after realization */
198 gtk_signal_connect( GTK_OBJECT(m_widget), "realize",
199 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback), (gpointer) this );
8a126fcc 200
2b07d713 201 /* the user resized the frame by dragging etc. */
2b854a32 202 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
36b3b54a 203 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
2b854a32 204
36b3b54a
RR
205 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
206 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this );
207
fb1585ae 208 return TRUE;
c33c4050 209}
c801d85f 210
43a18898 211wxDialog::~wxDialog()
c801d85f 212{
fb1585ae 213 wxTopLevelWindows.DeleteObject( this );
2b854a32 214
0d2a2b60
RR
215 if (wxTheApp->GetTopWindow() == this)
216 {
217 wxTheApp->SetTopWindow( (wxWindow*) NULL );
218 }
2b854a32 219
0d2a2b60 220 if (wxTopLevelWindows.Number() == 0)
2b854a32 221 {
0d2a2b60
RR
222 wxTheApp->ExitMainLoop();
223 }
c33c4050 224}
c801d85f 225
43a18898 226void wxDialog::SetTitle( const wxString& title )
c801d85f 227{
fb1585ae 228 m_title = title;
ed9b9841
OK
229 if (m_title.IsNull()) m_title = _T("");
230 gtk_window_set_title( GTK_WINDOW(m_widget), m_title.mbc_str() );
c33c4050 231}
c801d85f 232
43a18898 233wxString wxDialog::GetTitle() const
c801d85f 234{
fb1585ae 235 return (wxString&)m_title;
c33c4050 236}
c801d85f
KB
237
238void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
239{
fb1585ae 240 if (Validate()) TransferDataFromWindow();
c33c4050 241}
c801d85f
KB
242
243void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
244{
fb1585ae
RR
245 if (IsModal())
246 {
247 EndModal(wxID_CANCEL);
248 }
249 else
250 {
251 SetReturnCode(wxID_CANCEL);
252 this->Show(FALSE);
253 }
c33c4050 254}
c801d85f 255
903f689b 256void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
c801d85f 257{
fb1585ae 258 if ( Validate() && TransferDataFromWindow())
1a6944fd 259 {
2b854a32 260 if (IsModal())
fb1585ae
RR
261 {
262 EndModal(wxID_OK);
263 }
264 else
265 {
266 SetReturnCode(wxID_OK);
267 this->Show(FALSE);
268 }
1a6944fd 269 }
c33c4050 270}
c801d85f
KB
271
272void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
273{
2b854a32 274 // yes
c33c4050 275}
c801d85f 276
2b854a32 277void wxDialog::OnCloseWindow(wxCloseEvent& event)
c801d85f 278{
e3065973
JS
279 // We'll send a Cancel message by default,
280 // which may close the dialog.
281 // Check for looping if the Cancel event handler calls Close().
282
283 // Note that if a cancel button and handler aren't present in the dialog,
284 // nothing will happen when you close the dialog via the window manager, or
285 // via Close().
286 // We wouldn't want to destroy the dialog by default, since the dialog may have been
287 // created on the stack.
288 // However, this does mean that calling dialog->Close() won't delete the dialog
289 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
290 // sure to destroy the dialog.
291 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
292
ab2b3dd4 293 static wxList s_closing;
c801d85f 294
ab2b3dd4 295 if (s_closing.Member(this))
2b854a32
VZ
296 return; // no loops
297
ab2b3dd4 298 s_closing.Append(this);
c801d85f 299
fb1585ae
RR
300 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
301 cancelEvent.SetEventObject( this );
302 GetEventHandler()->ProcessEvent(cancelEvent);
ab2b3dd4 303 s_closing.DeleteObject(this);
c801d85f
KB
304}
305
43a18898 306bool wxDialog::Destroy()
e2414cbe 307{
fb1585ae 308 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
e2414cbe 309
fb1585ae 310 return TRUE;
e2414cbe
RR
311}
312
e52f60e6
RR
313void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
314{
ed9b9841 315 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
2b854a32 316
e52f60e6
RR
317 if (GetAutoLayout())
318 {
319 Layout();
320 }
2b854a32 321 else
e52f60e6 322 {
de8113d9 323 /* no child: go out ! */
db1b4961 324 if (!GetChildren().First()) return;
2b854a32 325
de8113d9 326 /* do we have exactly one child? */
e52f60e6 327 wxWindow *child = (wxWindow *) NULL;
db1b4961 328 for(wxNode *node = GetChildren().First(); node; node = node->Next())
e52f60e6
RR
329 {
330 wxWindow *win = (wxWindow *)node->Data();
de135918 331 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
e52f60e6 332 {
de8113d9 333 /* it's the second one: do nothing */
e52f60e6
RR
334 if (child) return;
335 child = win;
336 }
337 }
338
de8113d9 339 /* yes: set it's size to fill all the frame */
e52f60e6
RR
340 int client_x, client_y;
341 GetClientSize( &client_x, &client_y );
342 child->SetSize( 1, 1, client_x-2, client_y);
343 }
344}
345
bfc6fde4 346void wxDialog::DoSetSize( int x, int y, int width, int height, int sizeFlags )
903f689b 347{
ed9b9841
OK
348 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
349 wxASSERT_MSG( (m_wxwindow != NULL), _T("invalid dialog") );
fb1585ae 350
de8113d9 351 if (m_resizing) return; /* I don't like recursions */
fb1585ae
RR
352 m_resizing = TRUE;
353
354 int old_x = m_x;
355 int old_y = m_y;
356 int old_width = m_width;
357 int old_height = m_height;
2b854a32 358
fb1585ae
RR
359 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
360 {
361 if (x != -1) m_x = x;
362 if (y != -1) m_y = y;
363 if (width != -1) m_width = width;
364 if (height != -1) m_height = height;
365 }
366 else
367 {
368 m_x = x;
369 m_y = y;
370 m_width = width;
371 m_height = height;
372 }
373
374 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
375 {
376 if (width == -1) m_width = 80;
377 }
378
379 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
380 {
381 if (height == -1) m_height = 26;
382 }
2b854a32 383
fb1585ae
RR
384 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
385 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
0c77152e
RR
386 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
387 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
fb1585ae
RR
388
389 if ((m_x != -1) || (m_y != -1))
390 {
2b854a32 391 if ((m_x != old_x) || (m_y != old_y))
de8113d9
RR
392 {
393 /* m_sizeSet = FALSE; */
e146b8c8 394 gtk_widget_set_uposition( m_widget, m_x, m_y );
de8113d9 395 }
fb1585ae 396 }
2b854a32 397
fb1585ae
RR
398 if ((m_width != old_width) || (m_height != old_height))
399 {
de8113d9 400 m_sizeSet = FALSE;
fb1585ae 401 }
2b854a32 402
fb1585ae 403 m_resizing = FALSE;
903f689b
RR
404}
405
de8113d9
RR
406void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
407{
408 // due to a bug in gtk, x,y are always 0
409 // m_x = x;
410 // m_y = y;
411
412 if ((m_height == height) && (m_width == width) && (m_sizeSet)) return;
413 if (!m_wxwindow) return;
414
415 m_width = width;
416 m_height = height;
417
418 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
419 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
420 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
421 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
422
423 /* we actually set the size of a frame here and no-where else */
424 gtk_widget_set_usize( m_widget, m_width, m_height );
425
426 m_sizeSet = TRUE;
427
428 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
429 event.SetEventObject( this );
430 GetEventHandler()->ProcessEvent( event );
431}
432
903f689b
RR
433void wxDialog::Centre( int direction )
434{
ed9b9841 435 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
2b854a32 436
43a18898
RR
437 int x = 0;
438 int y = 0;
2b854a32 439
f5eafd0e
RR
440 if ((direction & wxHORIZONTAL) == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
441 if ((direction & wxVERTICAL) == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
2b854a32 442
fb1585ae 443 Move( x, y );
903f689b
RR
444}
445
de8113d9
RR
446void wxDialog::OnInternalIdle()
447{
448 if (!m_sizeSet)
449 GtkOnSize( m_x, m_y, m_width, m_height );
450}
451
debe6624 452bool wxDialog::Show( bool show )
c801d85f 453{
fb1585ae
RR
454 if (!show && IsModal())
455 {
de8113d9 456 EndModal( wxID_CANCEL );
fb1585ae 457 }
c801d85f 458
de8113d9
RR
459 if (show && !m_sizeSet)
460 {
461 /* by calling GtkOnSize here, we don't have to call
462 either after showing the frame, which would entail
463 much ugly flicker nor from within the size_allocate
464 handler, because GTK 1.1.X forbids that. */
465
466 GtkOnSize( m_x, m_y, m_width, m_height );
467 }
2b854a32 468
de8113d9 469 wxWindow::Show( show );
e146b8c8 470
fb1585ae 471 if (show) InitDialog();
2b854a32 472
fb1585ae 473 return TRUE;
c33c4050
RR
474}
475
43a18898 476bool wxDialog::IsModal() const
e1e955e1 477{
fb1585ae 478 return m_modalShowing;
e1e955e1
RR
479}
480
481void wxDialog::SetModal( bool WXUNUSED(flag) )
c33c4050 482{
e1e955e1 483/*
c33c4050
RR
484 if (flag)
485 m_windowStyle |= wxDIALOG_MODAL;
486 else
487 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
e1e955e1 488*/
ed9b9841 489 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
c33c4050 490}
c801d85f 491
43a18898 492int wxDialog::ShowModal()
c801d85f 493{
fb1585ae
RR
494 if (IsModal())
495 {
ed9b9841 496 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
fb1585ae
RR
497 return GetReturnCode();
498 }
e146b8c8 499
fb1585ae 500 Show( TRUE );
2b854a32 501
fb1585ae 502 m_modalShowing = TRUE;
2b854a32 503
fb1585ae
RR
504 gtk_grab_add( m_widget );
505 gtk_main();
506 gtk_grab_remove( m_widget );
2b854a32 507
fb1585ae 508 return GetReturnCode();
c33c4050 509}
c801d85f
KB
510
511void wxDialog::EndModal( int retCode )
512{
fb1585ae 513 SetReturnCode( retCode );
2b854a32 514
fb1585ae
RR
515 if (!IsModal())
516 {
ed9b9841 517 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
fb1585ae
RR
518 return;
519 }
2b854a32 520
fb1585ae 521 m_modalShowing = FALSE;
2b854a32 522
fb1585ae 523 gtk_main_quit();
2b854a32 524
fb1585ae 525 Show( FALSE );
c33c4050 526}
c801d85f 527
43a18898 528void wxDialog::InitDialog()
c801d85f 529{
fb1585ae 530 wxWindow::InitDialog();
c33c4050
RR
531}
532
c33c4050
RR
533void wxDialog::SetIcon( const wxIcon &icon )
534{
fb1585ae
RR
535 m_icon = icon;
536 if (!icon.Ok()) return;
2b854a32 537
fb1585ae
RR
538 wxMask *mask = icon.GetMask();
539 GdkBitmap *bm = (GdkBitmap *) NULL;
540 if (mask) bm = mask->GetBitmap();
2b854a32 541
fb1585ae 542 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
c33c4050 543}