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