]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dialog.cpp
Backgrounds work again
[wxWidgets.git] / src / gtk1 / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dialog.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
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"
17
18 #include "gdk/gdk.h"
19 #include "gtk/gtk.h"
20 #include "wx/gtk/win_gtk.h"
21
22 //-----------------------------------------------------------------------------
23
24 extern wxList wxPendingDelete;
25
26 //-----------------------------------------------------------------------------
27 // "delete_event"
28 //-----------------------------------------------------------------------------
29
30 bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
31 {
32 /*
33 printf( "OnDelete from " );
34 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
35 printf( win->GetClassInfo()->GetClassName() );
36 printf( ".\n" );
37 */
38
39 win->Close();
40
41 return TRUE;
42 }
43
44 //-----------------------------------------------------------------------------
45 // "size_allocate"
46 //-----------------------------------------------------------------------------
47
48 static 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
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 }
65 }
66
67 //-----------------------------------------------------------------------------
68 // "configure_event"
69 //-----------------------------------------------------------------------------
70
71 static 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
85 //-----------------------------------------------------------------------------
86 // "realize" from m_widget
87 //-----------------------------------------------------------------------------
88
89 /* we cannot MWM hints and icons before the widget has been realized,
90 so we do this directly after realization */
91
92 static gint
93 gtk_dialog_realized_callback( GtkWidget *widget, wxDialog *win )
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
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
132 return FALSE;
133 }
134
135 //-----------------------------------------------------------------------------
136 // wxDialog
137 //-----------------------------------------------------------------------------
138
139 BEGIN_EVENT_TABLE(wxDialog,wxPanel)
140 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
141 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
142 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
143 EVT_SIZE (wxDialog::OnSize)
144 EVT_CLOSE (wxDialog::OnCloseWindow)
145 END_EVENT_TABLE()
146
147 IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
148
149 void wxDialog::Init()
150 {
151 m_sizeSet = FALSE;
152 m_modalShowing = FALSE;
153 }
154
155 wxDialog::wxDialog( wxWindow *parent,
156 wxWindowID id, const wxString &title,
157 const wxPoint &pos, const wxSize &size,
158 long style, const wxString &name )
159 {
160 Init();
161
162 Create( parent, id, title, pos, size, style, name );
163 }
164
165 bool wxDialog::Create( wxWindow *parent,
166 wxWindowID id, const wxString &title,
167 const wxPoint &pos, const wxSize &size,
168 long style, const wxString &name )
169 {
170 wxTopLevelWindows.Append( this );
171
172 m_needParent = FALSE;
173
174 PreCreation( parent, id, pos, size, style, name );
175
176 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
177 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
178
179 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL);
180
181 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
182 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
183
184 m_wxwindow = gtk_myfixed_new();
185 gtk_widget_show( m_wxwindow );
186 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
187
188 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
189
190 SetTitle( title );
191
192 if (m_parent) m_parent->AddChild( this );
193
194 PostCreation();
195
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 );
200
201 /* the user resized the frame by dragging etc. */
202 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
203 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
204
205 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
206 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this );
207
208 return TRUE;
209 }
210
211 wxDialog::~wxDialog()
212 {
213 wxTopLevelWindows.DeleteObject( this );
214
215 if (wxTheApp->GetTopWindow() == this)
216 {
217 wxTheApp->SetTopWindow( (wxWindow*) NULL );
218 }
219
220 if (wxTopLevelWindows.Number() == 0)
221 {
222 wxTheApp->ExitMainLoop();
223 }
224 }
225
226 void wxDialog::SetTitle( const wxString& title )
227 {
228 m_title = title;
229 if (m_title.IsNull()) m_title = _T("");
230 gtk_window_set_title( GTK_WINDOW(m_widget), m_title.mbc_str() );
231 }
232
233 wxString wxDialog::GetTitle() const
234 {
235 return (wxString&)m_title;
236 }
237
238 void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
239 {
240 if (Validate()) TransferDataFromWindow();
241 }
242
243 void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
244 {
245 if (IsModal())
246 {
247 EndModal(wxID_CANCEL);
248 }
249 else
250 {
251 SetReturnCode(wxID_CANCEL);
252 this->Show(FALSE);
253 }
254 }
255
256 void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
257 {
258 if ( Validate() && TransferDataFromWindow())
259 {
260 if (IsModal())
261 {
262 EndModal(wxID_OK);
263 }
264 else
265 {
266 SetReturnCode(wxID_OK);
267 this->Show(FALSE);
268 }
269 }
270 }
271
272 void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
273 {
274 // yes
275 }
276
277 void wxDialog::OnCloseWindow(wxCloseEvent& event)
278 {
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
293 static wxList s_closing;
294
295 if (s_closing.Member(this))
296 return; // no loops
297
298 s_closing.Append(this);
299
300 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
301 cancelEvent.SetEventObject( this );
302 GetEventHandler()->ProcessEvent(cancelEvent);
303 s_closing.DeleteObject(this);
304 }
305
306 bool wxDialog::Destroy()
307 {
308 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
309
310 return TRUE;
311 }
312
313 void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
314 {
315 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
316
317 if (GetAutoLayout())
318 {
319 Layout();
320 }
321 else
322 {
323 /* no child: go out ! */
324 if (!GetChildren().First()) return;
325
326 /* do we have exactly one child? */
327 wxWindow *child = (wxWindow *) NULL;
328 for(wxNode *node = GetChildren().First(); node; node = node->Next())
329 {
330 wxWindow *win = (wxWindow *)node->Data();
331 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
332 {
333 /* it's the second one: do nothing */
334 if (child) return;
335 child = win;
336 }
337 }
338
339 /* yes: set it's size to fill all the frame */
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
346 void wxDialog::DoSetSize( int x, int y, int width, int height, int sizeFlags )
347 {
348 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
349 wxASSERT_MSG( (m_wxwindow != NULL), _T("invalid dialog") );
350
351 if (m_resizing) return; /* I don't like recursions */
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;
358
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 }
383
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;
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;
388
389 if ((m_x != -1) || (m_y != -1))
390 {
391 if ((m_x != old_x) || (m_y != old_y))
392 {
393 /* m_sizeSet = FALSE; */
394 gtk_widget_set_uposition( m_widget, m_x, m_y );
395 }
396 }
397
398 if ((m_width != old_width) || (m_height != old_height))
399 {
400 m_sizeSet = FALSE;
401 }
402
403 m_resizing = FALSE;
404 }
405
406 void 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
433 void wxDialog::Centre( int direction )
434 {
435 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
436
437 int x = 0;
438 int y = 0;
439
440 if ((direction & wxHORIZONTAL) == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
441 if ((direction & wxVERTICAL) == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
442
443 Move( x, y );
444 }
445
446 void wxDialog::OnInternalIdle()
447 {
448 if (!m_sizeSet)
449 GtkOnSize( m_x, m_y, m_width, m_height );
450 }
451
452 bool wxDialog::Show( bool show )
453 {
454 if (!show && IsModal())
455 {
456 EndModal( wxID_CANCEL );
457 }
458
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 }
468
469 wxWindow::Show( show );
470
471 if (show) InitDialog();
472
473 return TRUE;
474 }
475
476 bool wxDialog::IsModal() const
477 {
478 return m_modalShowing;
479 }
480
481 void wxDialog::SetModal( bool WXUNUSED(flag) )
482 {
483 /*
484 if (flag)
485 m_windowStyle |= wxDIALOG_MODAL;
486 else
487 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
488 */
489 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
490 }
491
492 int wxDialog::ShowModal()
493 {
494 if (IsModal())
495 {
496 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
497 return GetReturnCode();
498 }
499
500 Show( TRUE );
501
502 m_modalShowing = TRUE;
503
504 gtk_grab_add( m_widget );
505 gtk_main();
506 gtk_grab_remove( m_widget );
507
508 return GetReturnCode();
509 }
510
511 void wxDialog::EndModal( int retCode )
512 {
513 SetReturnCode( retCode );
514
515 if (!IsModal())
516 {
517 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
518 return;
519 }
520
521 m_modalShowing = FALSE;
522
523 gtk_main_quit();
524
525 Show( FALSE );
526 }
527
528 void wxDialog::InitDialog()
529 {
530 wxWindow::InitDialog();
531 }
532
533 void wxDialog::SetIcon( const wxIcon &icon )
534 {
535 m_icon = icon;
536 if (!icon.Ok()) return;
537
538 wxMask *mask = icon.GetMask();
539 GdkBitmap *bm = (GdkBitmap *) NULL;
540 if (mask) bm = mask->GetBitmap();
541
542 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
543 }