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