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();
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
72 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxDialog
*win
)
75 wxapp_install_idle_handler();
77 if (!win
->m_hasVMT
) return;
79 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
81 win
->m_width
= alloc
->width
;
82 win
->m_height
= alloc
->height
;
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
92 #if (GTK_MINOR_VERSION > 0)
93 gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDialog
*win
)
95 gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxDialog
*win
)
99 wxapp_install_idle_handler();
101 if (!win
->m_hasVMT
) return FALSE
;
103 #if (GTK_MINOR_VERSION > 0)
106 gdk_window_get_root_origin( win
->m_widget
->window
, &x
, &y
);
114 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
115 mevent
.SetEventObject( win
);
116 win
->GetEventHandler()->ProcessEvent( mevent
);
121 //-----------------------------------------------------------------------------
122 // "realize" from m_widget
123 //-----------------------------------------------------------------------------
125 /* we cannot MWM hints and icons before the widget has been realized,
126 so we do this directly after realization */
129 gtk_dialog_realized_callback( GtkWidget
*widget
, wxDialog
*win
)
132 wxapp_install_idle_handler();
134 /* I haven't been able to set the position of
135 the dialog before it is shown, so I set the
136 position in "realize" */
137 gtk_widget_set_uposition( widget
, win
->m_x
, win
->m_y
);
139 /* all this is for Motif Window Manager "hints" and is supposed to be
140 recognized by other WM as well. not tested. */
141 long decor
= (long) GDK_DECOR_BORDER
;
142 long func
= (long) GDK_FUNC_MOVE
;
144 /* Some WM don't display any border around the frame contents if
145 used with these hints, so we add a resize border around it,
146 without automatically allowinng it to be resized though.
148 This avoids the problem, but looks odd. What shall we do?
150 decor
|= GDK_DECOR_RESIZEH
;
152 if ((win
->GetWindowStyle() & wxCAPTION
) != 0)
153 decor
|= GDK_DECOR_TITLE
;
154 if ((win
->GetWindowStyle() & wxSYSTEM_MENU
) != 0)
156 decor
|= GDK_DECOR_MENU
;
157 func
|= GDK_FUNC_CLOSE
;
159 if ((win
->GetWindowStyle() & wxMINIMIZE_BOX
) != 0)
161 func
|= GDK_FUNC_MINIMIZE
;
162 decor
|= GDK_DECOR_MINIMIZE
;
164 if ((win
->GetWindowStyle() & wxMAXIMIZE_BOX
) != 0)
166 decor
|= GDK_DECOR_MAXIMIZE
;
167 func
|= GDK_FUNC_MAXIMIZE
;
169 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) != 0)
171 func
|= GDK_FUNC_RESIZE
;
172 decor
|= GDK_DECOR_RESIZEH
;
174 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
175 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
177 /* GTK's shrinking/growing policy */
178 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) == 0)
179 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
181 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
184 gint flag
= 0; // GDK_HINT_POS;
185 if ((win
->GetMinWidth() != -1) || (win
->GetMinHeight() != -1)) flag
|= GDK_HINT_MIN_SIZE
;
186 if ((win
->GetMaxWidth() != -1) || (win
->GetMaxHeight() != -1)) flag
|= GDK_HINT_MAX_SIZE
;
189 gdk_window_set_hints( win
->m_widget
->window
,
191 win
->GetMinWidth(), win
->GetMinHeight(),
192 win
->GetMaxWidth(), win
->GetMaxHeight(),
197 if (win
->m_icon
!= wxNullIcon
)
199 wxIcon
icon( win
->m_icon
);
200 win
->m_icon
= wxNullIcon
;
201 win
->SetIcon( icon
);
207 //-----------------------------------------------------------------------------
208 // InsertChild for wxDialog
209 //-----------------------------------------------------------------------------
211 /* Callback for wxFrame. This very strange beast has to be used because
212 * C++ has no virtual methods in a constructor. We have to emulate a
213 * virtual function here as wxWindows requires different ways to insert
214 * a child in container classes. */
216 static void wxInsertChildInDialog( wxDialog
* parent
, wxWindow
* child
)
218 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
219 GTK_WIDGET(child
->m_widget
),
225 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
227 /* we now allow a window to get the focus as long as it
228 doesn't have any children. */
229 GTK_WIDGET_UNSET_FLAGS( parent
->m_wxwindow
, GTK_CAN_FOCUS
);
233 //-----------------------------------------------------------------------------
235 //-----------------------------------------------------------------------------
237 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
238 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
239 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
240 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
241 EVT_SIZE (wxDialog::OnSize
)
242 EVT_CLOSE (wxDialog::OnCloseWindow
)
245 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
247 void wxDialog::Init()
251 m_modalShowing
= FALSE
;
255 wxDialog::wxDialog( wxWindow
*parent
,
256 wxWindowID id
, const wxString
&title
,
257 const wxPoint
&pos
, const wxSize
&size
,
258 long style
, const wxString
&name
)
262 Create( parent
, id
, title
, pos
, size
, style
, name
);
265 bool wxDialog::Create( wxWindow
*parent
,
266 wxWindowID id
, const wxString
&title
,
267 const wxPoint
&pos
, const wxSize
&size
,
268 long style
, const wxString
&name
)
270 wxTopLevelWindows
.Append( this );
272 m_needParent
= FALSE
;
274 if (!PreCreation( parent
, pos
, size
) ||
275 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
277 wxFAIL_MSG( wxT("wxDialog creation failed") );
281 // All dialogs should really have this style
282 m_windowStyle
|= wxTAB_TRAVERSAL
;
284 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
286 m_widget
= gtk_window_new( GTK_WINDOW_DIALOG
);
288 if ((m_parent
) && (GTK_IS_WINDOW(m_parent
->m_widget
)))
289 gtk_window_set_transient_for( GTK_WINDOW(m_widget
), GTK_WINDOW(m_parent
->m_widget
) );
292 gtk_window_set_wmclass( GTK_WINDOW(m_widget
), name
.mb_str(), name
.mb_str() );
294 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
296 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
297 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
299 m_wxwindow
= gtk_pizza_new();
300 gtk_widget_show( m_wxwindow
);
301 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
303 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
307 if (m_parent
) m_parent
->AddChild( this );
311 /* we cannot set MWM hints before the widget has
312 been realized, so we do this directly after realization */
313 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
314 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
316 /* the user resized the frame by dragging etc. */
317 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
318 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
320 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
321 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
323 /* disable native tab traversal */
324 gtk_signal_connect( GTK_OBJECT(m_widget
), "focus",
325 GTK_SIGNAL_FUNC(gtk_dialog_focus_callback
), (gpointer
)this );
330 wxDialog::~wxDialog()
332 m_isBeingDeleted
= TRUE
;
334 wxTopLevelWindows
.DeleteObject( this );
336 if (wxTheApp
->GetTopWindow() == this)
338 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
341 if ((wxTopLevelWindows
.Number() == 0) &&
342 (wxTheApp
->GetExitOnFrameDelete()))
344 wxTheApp
->ExitMainLoop();
348 void wxDialog::SetTitle( const wxString
& title
)
351 if (m_title
.IsNull()) m_title
= wxT("");
352 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
355 wxString
wxDialog::GetTitle() const
357 return (wxString
&)m_title
;
360 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
362 if (Validate()) TransferDataFromWindow();
365 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
369 EndModal(wxID_CANCEL
);
373 SetReturnCode(wxID_CANCEL
);
378 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
380 if (Validate() && TransferDataFromWindow())
388 SetReturnCode(wxID_OK
);
394 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
399 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
401 // We'll send a Cancel message by default,
402 // which may close the dialog.
403 // Check for looping if the Cancel event handler calls Close().
405 // Note that if a cancel button and handler aren't present in the dialog,
406 // nothing will happen when you close the dialog via the window manager, or
408 // We wouldn't want to destroy the dialog by default, since the dialog may have been
409 // created on the stack.
410 // However, this does mean that calling dialog->Close() won't delete the dialog
411 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
412 // sure to destroy the dialog.
413 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
415 static wxList s_closing
;
417 if (s_closing
.Member(this))
420 s_closing
.Append(this);
422 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
423 cancelEvent
.SetEventObject( this );
424 GetEventHandler()->ProcessEvent(cancelEvent
);
425 s_closing
.DeleteObject(this);
428 bool wxDialog::Destroy()
430 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
435 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
437 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
439 #if wxUSE_CONSTRAINTS
445 #endif // wxUSE_CONSTRAINTS
447 /* no child: go out ! */
448 if (!GetChildren().First()) return;
450 /* do we have exactly one child? */
451 wxWindow
*child
= (wxWindow
*) NULL
;
452 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
454 wxWindow
*win
= (wxWindow
*)node
->Data();
455 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
457 /* it's the second one: do nothing */
463 /* yes: set it's size to fill all the frame */
464 int client_x
, client_y
;
465 GetClientSize( &client_x
, &client_y
);
466 child
->SetSize( 1, 1, client_x
-2, client_y
);
470 void wxDialog::DoMoveWindow(int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(width
), int WXUNUSED(height
) )
472 wxFAIL_MSG( wxT("DoMoveWindow called for wxDialog") );
475 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
477 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
478 wxASSERT_MSG( (m_wxwindow
!= NULL
), wxT("invalid dialog") );
480 if (m_resizing
) return; /* I don't like recursions */
486 int old_width
= m_width
;
487 int old_height
= m_height
;
489 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
491 if (x
!= -1) m_x
= x
;
492 if (y
!= -1) m_y
= y
;
493 if (width
!= -1) m_width
= width
;
494 if (height
!= -1) m_height
= height
;
505 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
507 if (width == -1) m_width = 80;
510 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
512 if (height == -1) m_height = 26;
516 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
517 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
518 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
519 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
521 if ((m_x
!= -1) || (m_y
!= -1))
523 if ((m_x
!= old_x
) || (m_y
!= old_y
))
525 /* we set the position here and when showing the dialog
526 for the first time in idle time */
527 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
531 if ((m_width
!= old_width
) || (m_height
!= old_height
))
533 /* actual resizing is deferred to GtkOnSize in idle time and
534 when showing the dialog */
541 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
543 // due to a bug in gtk, x,y are always 0
547 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
548 if (!m_wxwindow
) return;
553 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
554 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
555 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
556 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
558 /* we actually set the size of a frame here and no-where else */
559 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
563 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
564 event
.SetEventObject( this );
565 GetEventHandler()->ProcessEvent( event
);
568 void wxDialog::OnInternalIdle()
570 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
571 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
573 wxWindow::OnInternalIdle();
576 bool wxDialog::Show( bool show
)
578 if (!show
&& IsModal())
580 EndModal( wxID_CANCEL
);
583 if (show
&& !m_sizeSet
)
585 /* by calling GtkOnSize here, we don't have to call
586 either after showing the frame, which would entail
587 much ugly flicker nor from within the size_allocate
588 handler, because GTK 1.1.X forbids that. */
590 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
593 bool ret
= wxWindow::Show( show
);
595 if (show
) InitDialog();
600 bool wxDialog::IsModal() const
602 return m_modalShowing
;
605 void wxDialog::SetModal( bool WXUNUSED(flag
) )
609 m_windowStyle |= wxDIALOG_MODAL;
611 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
613 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
616 int wxDialog::ShowModal()
620 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
621 return GetReturnCode();
624 wxBusyCursorSuspender cs
; // temporarily suppress the busy cursor
628 m_modalShowing
= TRUE
;
632 gtk_grab_add( m_widget
);
634 gtk_grab_remove( m_widget
);
638 return GetReturnCode();
641 void wxDialog::EndModal( int retCode
)
643 SetReturnCode( retCode
);
647 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
651 m_modalShowing
= FALSE
;
658 void wxDialog::InitDialog()
660 wxWindow::InitDialog();
663 void wxDialog::SetIcon( const wxIcon
&icon
)
666 if (!icon
.Ok()) return;
668 if (!m_widget
->window
) return;
670 wxMask
*mask
= icon
.GetMask();
671 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
672 if (mask
) bm
= mask
->GetBitmap();
674 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);