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