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