1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "dialog.h"
14 #include "wx/dialog.h"
17 #include "wx/cursor.h"
21 #include <gdk/gdkkeysyms.h>
23 #include "wx/gtk/win_gtk.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
31 extern int g_openDialogs
;
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern wxList wxPendingDelete
;
39 //-----------------------------------------------------------------------------
40 // "focus" from m_window
41 //-----------------------------------------------------------------------------
43 static gint
gtk_dialog_focus_callback( GtkWidget
*widget
, GtkDirectionType
WXUNUSED(d
), wxWindow
*WXUNUSED(win
) )
46 wxapp_install_idle_handler();
48 // This disables GTK's tab traversal
49 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "focus" );
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxDialog
*win
)
60 wxapp_install_idle_handler();
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxDialog
*win
)
74 wxapp_install_idle_handler();
76 if (!win
->m_hasVMT
) return;
78 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
80 win
->m_width
= alloc
->width
;
81 win
->m_height
= alloc
->height
;
86 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
91 #if (GTK_MINOR_VERSION > 0)
92 gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDialog
*win
)
94 gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxDialog
*win
)
98 wxapp_install_idle_handler();
100 if (!win
->m_hasVMT
) return FALSE
;
102 #if (GTK_MINOR_VERSION > 0)
105 gdk_window_get_root_origin( win
->m_widget
->window
, &x
, &y
);
113 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
114 mevent
.SetEventObject( win
);
115 win
->GetEventHandler()->ProcessEvent( mevent
);
120 //-----------------------------------------------------------------------------
121 // "realize" from m_widget
122 //-----------------------------------------------------------------------------
124 /* we cannot MWM hints and icons before the widget has been realized,
125 so we do this directly after realization */
128 gtk_dialog_realized_callback( GtkWidget
*widget
, wxDialog
*win
)
131 wxapp_install_idle_handler();
133 /* I haven't been able to set the position of
134 the dialog before it is shown, so I set the
135 position in "realize" */
136 gtk_widget_set_uposition( widget
, win
->m_x
, win
->m_y
);
138 /* all this is for Motif Window Manager "hints" and is supposed to be
139 recognized by other WM as well. not tested. */
140 long decor
= (long) GDK_DECOR_BORDER
;
141 long func
= (long) GDK_FUNC_MOVE
;
143 /* Some WM don't display any border around the frame contents if
144 used with these hints, so we add a resize border around it,
145 without automatically allowinng it to be resized though.
147 This avoids the problem, but looks odd. What shall we do?
149 decor
|= GDK_DECOR_RESIZEH
;
151 if ((win
->GetWindowStyle() & wxCAPTION
) != 0)
152 decor
|= GDK_DECOR_TITLE
;
153 if ((win
->GetWindowStyle() & wxSYSTEM_MENU
) != 0)
155 decor
|= GDK_DECOR_MENU
;
156 func
|= GDK_FUNC_CLOSE
;
158 if ((win
->GetWindowStyle() & wxMINIMIZE_BOX
) != 0)
160 func
|= GDK_FUNC_MINIMIZE
;
161 decor
|= GDK_DECOR_MINIMIZE
;
163 if ((win
->GetWindowStyle() & wxMAXIMIZE_BOX
) != 0)
165 decor
|= GDK_DECOR_MAXIMIZE
;
166 func
|= GDK_FUNC_MAXIMIZE
;
168 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) != 0)
170 func
|= GDK_FUNC_RESIZE
;
171 decor
|= GDK_DECOR_RESIZEH
;
173 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
174 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
176 /* GTK's shrinking/growing policy */
177 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) == 0)
178 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
180 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
183 gint flag
= 0; // GDK_HINT_POS;
184 if ((win
->GetMinWidth() != -1) || (win
->GetMinHeight() != -1)) flag
|= GDK_HINT_MIN_SIZE
;
185 if ((win
->GetMaxWidth() != -1) || (win
->GetMaxHeight() != -1)) flag
|= GDK_HINT_MAX_SIZE
;
188 gdk_window_set_hints( win
->m_widget
->window
,
190 win
->GetMinWidth(), win
->GetMinHeight(),
191 win
->GetMaxWidth(), win
->GetMaxHeight(),
196 if (win
->m_icon
!= wxNullIcon
)
198 wxIcon
icon( win
->m_icon
);
199 win
->m_icon
= wxNullIcon
;
200 win
->SetIcon( icon
);
206 //-----------------------------------------------------------------------------
207 // InsertChild for wxDialog
208 //-----------------------------------------------------------------------------
210 /* Callback for wxFrame. This very strange beast has to be used because
211 * C++ has no virtual methods in a constructor. We have to emulate a
212 * virtual function here as wxWindows requires different ways to insert
213 * a child in container classes. */
215 static void wxInsertChildInDialog( wxDialog
* parent
, wxWindow
* child
)
217 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
218 GTK_WIDGET(child
->m_widget
),
224 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
226 /* we now allow a window to get the focus as long as it
227 doesn't have any children. */
228 GTK_WIDGET_UNSET_FLAGS( parent
->m_wxwindow
, GTK_CAN_FOCUS
);
232 //-----------------------------------------------------------------------------
234 //-----------------------------------------------------------------------------
236 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
237 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
238 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
239 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
240 EVT_SIZE (wxDialog::OnSize
)
241 EVT_CLOSE (wxDialog::OnCloseWindow
)
244 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
246 void wxDialog::Init()
250 m_modalShowing
= FALSE
;
254 wxDialog::wxDialog( wxWindow
*parent
,
255 wxWindowID id
, const wxString
&title
,
256 const wxPoint
&pos
, const wxSize
&size
,
257 long style
, const wxString
&name
)
261 Create( parent
, id
, title
, pos
, size
, style
, name
);
264 bool wxDialog::Create( wxWindow
*parent
,
265 wxWindowID id
, const wxString
&title
,
266 const wxPoint
&pos
, const wxSize
&size
,
267 long style
, const wxString
&name
)
271 wxTopLevelWindows
.Append( this );
273 m_needParent
= FALSE
;
275 if (!PreCreation( parent
, pos
, size
) ||
276 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
278 wxFAIL_MSG( wxT("wxDialog creation failed") );
282 // All dialogs should really have this style
283 m_windowStyle
|= wxTAB_TRAVERSAL
;
285 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
287 m_widget
= gtk_window_new( GTK_WINDOW_DIALOG
);
289 if ((m_parent
) && (GTK_IS_WINDOW(m_parent
->m_widget
)))
290 gtk_window_set_transient_for( GTK_WINDOW(m_widget
), GTK_WINDOW(m_parent
->m_widget
) );
293 gtk_window_set_wmclass( GTK_WINDOW(m_widget
), name
.mb_str(), name
.mb_str() );
295 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
297 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
298 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
300 m_wxwindow
= gtk_pizza_new();
301 gtk_widget_show( m_wxwindow
);
302 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
304 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
308 if (m_parent
) m_parent
->AddChild( this );
312 /* we cannot set MWM hints before the widget has
313 been realized, so we do this directly after realization */
314 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
315 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
317 /* the user resized the frame by dragging etc. */
318 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
319 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
321 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
322 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
324 /* disable native tab traversal */
325 gtk_signal_connect( GTK_OBJECT(m_widget
), "focus",
326 GTK_SIGNAL_FUNC(gtk_dialog_focus_callback
), (gpointer
)this );
331 wxDialog::~wxDialog()
333 m_isBeingDeleted
= TRUE
;
335 wxTopLevelWindows
.DeleteObject( this );
337 if (wxTheApp
->GetTopWindow() == this)
339 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
342 if (wxTopLevelWindows
.Number() == 0)
344 wxTheApp
->ExitMainLoop();
350 void wxDialog::SetTitle( const wxString
& title
)
353 if (m_title
.IsNull()) m_title
= wxT("");
354 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
357 wxString
wxDialog::GetTitle() const
359 return (wxString
&)m_title
;
362 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
364 if (Validate()) TransferDataFromWindow();
367 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
371 EndModal(wxID_CANCEL
);
375 SetReturnCode(wxID_CANCEL
);
380 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
382 if (Validate() && TransferDataFromWindow())
390 SetReturnCode(wxID_OK
);
396 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
401 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
403 // We'll send a Cancel message by default,
404 // which may close the dialog.
405 // Check for looping if the Cancel event handler calls Close().
407 // Note that if a cancel button and handler aren't present in the dialog,
408 // nothing will happen when you close the dialog via the window manager, or
410 // We wouldn't want to destroy the dialog by default, since the dialog may have been
411 // created on the stack.
412 // However, this does mean that calling dialog->Close() won't delete the dialog
413 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
414 // sure to destroy the dialog.
415 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
417 static wxList s_closing
;
419 if (s_closing
.Member(this))
422 s_closing
.Append(this);
424 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
425 cancelEvent
.SetEventObject( this );
426 GetEventHandler()->ProcessEvent(cancelEvent
);
427 s_closing
.DeleteObject(this);
430 bool wxDialog::Destroy()
432 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
437 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
439 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
441 #if wxUSE_CONSTRAINTS
447 #endif // wxUSE_CONSTRAINTS
449 /* no child: go out ! */
450 if (!GetChildren().First()) return;
452 /* do we have exactly one child? */
453 wxWindow
*child
= (wxWindow
*) NULL
;
454 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
456 wxWindow
*win
= (wxWindow
*)node
->Data();
457 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
459 /* it's the second one: do nothing */
465 /* yes: set it's size to fill all the frame */
466 int client_x
, client_y
;
467 GetClientSize( &client_x
, &client_y
);
468 child
->SetSize( 1, 1, client_x
-2, client_y
);
472 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
474 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
475 wxASSERT_MSG( (m_wxwindow
!= NULL
), wxT("invalid dialog") );
477 if (m_resizing
) return; /* I don't like recursions */
483 int old_width
= m_width
;
484 int old_height
= m_height
;
486 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
488 if (x
!= -1) m_x
= x
;
489 if (y
!= -1) m_y
= y
;
490 if (width
!= -1) m_width
= width
;
491 if (height
!= -1) m_height
= height
;
502 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
504 if (width == -1) m_width = 80;
507 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
509 if (height == -1) m_height = 26;
513 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
514 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
515 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
516 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
518 if ((m_x
!= -1) || (m_y
!= -1))
520 if ((m_x
!= old_x
) || (m_y
!= old_y
))
522 /* we set the position here and when showing the dialog
523 for the first time in idle time */
524 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
528 if ((m_width
!= old_width
) || (m_height
!= old_height
))
530 /* actual resizing is deferred to GtkOnSize in idle time and
531 when showing the dialog */
538 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
540 // due to a bug in gtk, x,y are always 0
544 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
545 if (!m_wxwindow
) return;
550 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
551 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
552 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
553 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
555 /* we actually set the size of a frame here and no-where else */
556 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
560 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
561 event
.SetEventObject( this );
562 GetEventHandler()->ProcessEvent( event
);
565 void wxDialog::OnInternalIdle()
567 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
568 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
570 wxWindow::OnInternalIdle();
573 bool wxDialog::Show( bool show
)
575 if (!show
&& IsModal())
577 EndModal( wxID_CANCEL
);
580 if (show
&& !m_sizeSet
)
582 /* by calling GtkOnSize here, we don't have to call
583 either after showing the frame, which would entail
584 much ugly flicker nor from within the size_allocate
585 handler, because GTK 1.1.X forbids that. */
587 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
590 bool ret
= wxWindow::Show( show
);
592 if (show
) InitDialog();
597 bool wxDialog::IsModal() const
599 return m_modalShowing
;
602 void wxDialog::SetModal( bool WXUNUSED(flag
) )
606 m_windowStyle |= wxDIALOG_MODAL;
608 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
610 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
613 int wxDialog::ShowModal()
617 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
618 return GetReturnCode();
621 wxBusyCursorSuspender cs
; // temporarily suppress the busy cursor
625 m_modalShowing
= TRUE
;
627 gtk_grab_add( m_widget
);
629 gtk_grab_remove( m_widget
);
631 return GetReturnCode();
634 void wxDialog::EndModal( int retCode
)
636 SetReturnCode( retCode
);
640 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
644 m_modalShowing
= FALSE
;
651 void wxDialog::InitDialog()
653 wxWindow::InitDialog();
656 void wxDialog::SetIcon( const wxIcon
&icon
)
659 if (!icon
.Ok()) return;
661 if (!m_widget
->window
) return;
663 wxMask
*mask
= icon
.GetMask();
664 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
665 if (mask
) bm
= mask
->GetBitmap();
667 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);