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