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"
20 #include "wx/gtk/win_gtk.h"
22 //-----------------------------------------------------------------------------
24 extern wxList wxPendingDelete
;
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxDialog
*win
)
33 printf( "OnDelete from " );
34 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
35 printf( win->GetClassInfo()->GetClassName() );
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxDialog
*win
)
50 if (!win
->HasVMT()) return;
53 printf( "OnDialogResize from " );
54 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
55 printf( win->GetClassInfo()->GetClassName() );
59 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
61 win
->m_sizeSet
= FALSE
;
62 win
->m_width
= alloc
->width
;
63 win
->m_height
= alloc
->height
;
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 static gint
gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxDialog
*win
)
73 if (!win
->HasVMT()) return FALSE
;
78 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
79 mevent
.SetEventObject( win
);
80 win
->GetEventHandler()->ProcessEvent( mevent
);
85 //-----------------------------------------------------------------------------
86 // "realize" from m_widget
87 //-----------------------------------------------------------------------------
89 /* we cannot MWM hints and icons before the widget has been realized,
90 so we do this directly after realization */
93 gtk_dialog_realized_callback( GtkWidget
*widget
, wxDialog
*win
)
95 /* all this is for Motif Window Manager "hints" and is supposed to be
96 recognized by other WM as well. not tested. */
97 long decor
= (long) GDK_DECOR_ALL
;
98 long func
= (long) GDK_FUNC_ALL
;
100 if ((win
->m_windowStyle
& wxCAPTION
) == 0)
101 decor
|= GDK_DECOR_TITLE
;
102 /* if ((win->m_windowStyle & wxMINIMIZE) == 0)
103 func |= GDK_FUNC_MINIMIZE;
104 if ((win->m_windowStyle & wxMAXIMIZE) == 0)
105 func |= GDK_FUNC_MAXIMIZE; */
106 if ((win
->m_windowStyle
& wxSYSTEM_MENU
) == 0)
107 decor
|= GDK_DECOR_MENU
;
108 if ((win
->m_windowStyle
& wxMINIMIZE_BOX
) == 0)
109 decor
|= GDK_DECOR_MINIMIZE
;
110 if ((win
->m_windowStyle
& wxMAXIMIZE_BOX
) == 0)
111 decor
|= GDK_DECOR_MAXIMIZE
;
112 if ((win
->m_windowStyle
& wxRESIZE_BORDER
) == 0)
113 func
|= GDK_FUNC_RESIZE
;
115 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
116 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
118 /* GTK's shrinking/growing policy */
119 if ((win
->m_windowStyle
& wxRESIZE_BORDER
) == 0)
120 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
122 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
125 if (win
->m_icon
!= wxNullIcon
)
127 wxIcon
icon( win
->m_icon
);
128 win
->m_icon
= wxNullIcon
;
129 win
->SetIcon( icon
);
135 //-----------------------------------------------------------------------------
137 //-----------------------------------------------------------------------------
139 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
140 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
141 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
142 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
143 EVT_SIZE (wxDialog::OnSize
)
144 EVT_CLOSE (wxDialog::OnCloseWindow
)
147 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
149 void wxDialog::Init()
152 m_modalShowing
= FALSE
;
155 wxDialog::wxDialog( wxWindow
*parent
,
156 wxWindowID id
, const wxString
&title
,
157 const wxPoint
&pos
, const wxSize
&size
,
158 long style
, const wxString
&name
)
162 Create( parent
, id
, title
, pos
, size
, style
, name
);
165 bool wxDialog::Create( wxWindow
*parent
,
166 wxWindowID id
, const wxString
&title
,
167 const wxPoint
&pos
, const wxSize
&size
,
168 long style
, const wxString
&name
)
170 wxTopLevelWindows
.Append( this );
172 m_needParent
= FALSE
;
174 PreCreation( parent
, id
, pos
, size
, style
, name
);
176 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
177 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
179 gtk_widget_set( m_widget
, "GtkWindow::allow_shrink", TRUE
, NULL
);
181 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
182 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
184 m_wxwindow
= gtk_myfixed_new();
185 gtk_widget_show( m_wxwindow
);
186 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
188 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
192 if (m_parent
) m_parent
->AddChild( this );
196 /* we cannot set MWM hints before the widget has
197 been realized, so we do this directly after realization */
198 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
199 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
201 /* the user resized the frame by dragging etc. */
202 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
203 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
205 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
206 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
211 wxDialog::~wxDialog()
213 wxTopLevelWindows
.DeleteObject( this );
215 if (wxTheApp
->GetTopWindow() == this)
217 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
220 if (wxTopLevelWindows
.Number() == 0)
222 wxTheApp
->ExitMainLoop();
226 void wxDialog::SetTitle( const wxString
& title
)
229 if (m_title
.IsNull()) m_title
= _T("");
230 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
233 wxString
wxDialog::GetTitle() const
235 return (wxString
&)m_title
;
238 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
240 if (Validate()) TransferDataFromWindow();
243 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
247 EndModal(wxID_CANCEL
);
251 SetReturnCode(wxID_CANCEL
);
256 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
258 if ( Validate() && TransferDataFromWindow())
266 SetReturnCode(wxID_OK
);
272 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
277 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
279 // We'll send a Cancel message by default,
280 // which may close the dialog.
281 // Check for looping if the Cancel event handler calls Close().
283 // Note that if a cancel button and handler aren't present in the dialog,
284 // nothing will happen when you close the dialog via the window manager, or
286 // We wouldn't want to destroy the dialog by default, since the dialog may have been
287 // created on the stack.
288 // However, this does mean that calling dialog->Close() won't delete the dialog
289 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
290 // sure to destroy the dialog.
291 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
293 static wxList s_closing
;
295 if (s_closing
.Member(this))
298 s_closing
.Append(this);
300 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
301 cancelEvent
.SetEventObject( this );
302 GetEventHandler()->ProcessEvent(cancelEvent
);
303 s_closing
.DeleteObject(this);
306 bool wxDialog::Destroy()
308 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
313 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
315 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
323 /* no child: go out ! */
324 if (!GetChildren().First()) return;
326 /* do we have exactly one child? */
327 wxWindow
*child
= (wxWindow
*) NULL
;
328 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
330 wxWindow
*win
= (wxWindow
*)node
->Data();
331 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
333 /* it's the second one: do nothing */
339 /* yes: set it's size to fill all the frame */
340 int client_x
, client_y
;
341 GetClientSize( &client_x
, &client_y
);
342 child
->SetSize( 1, 1, client_x
-2, client_y
);
346 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
348 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
349 wxASSERT_MSG( (m_wxwindow
!= NULL
), _T("invalid dialog") );
351 if (m_resizing
) return; /* I don't like recursions */
356 int old_width
= m_width
;
357 int old_height
= m_height
;
359 if ((sizeFlags
& wxSIZE_USE_EXISTING
) == wxSIZE_USE_EXISTING
)
361 if (x
!= -1) m_x
= x
;
362 if (y
!= -1) m_y
= y
;
363 if (width
!= -1) m_width
= width
;
364 if (height
!= -1) m_height
= height
;
374 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
376 if (width
== -1) m_width
= 80;
379 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
381 if (height
== -1) m_height
= 26;
384 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
385 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
386 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
387 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
389 if ((m_x
!= -1) || (m_y
!= -1))
391 if ((m_x
!= old_x
) || (m_y
!= old_y
))
393 /* m_sizeSet = FALSE; */
394 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
398 if ((m_width
!= old_width
) || (m_height
!= old_height
))
406 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
408 // due to a bug in gtk, x,y are always 0
412 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
413 if (!m_wxwindow
) return;
418 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
419 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
420 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
421 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
423 /* we actually set the size of a frame here and no-where else */
424 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
428 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
429 event
.SetEventObject( this );
430 GetEventHandler()->ProcessEvent( event
);
433 void wxDialog::Centre( int direction
)
435 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
440 if ((direction
& wxHORIZONTAL
) == wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
441 if ((direction
& wxVERTICAL
) == wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
446 void wxDialog::OnInternalIdle()
449 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
452 bool wxDialog::Show( bool show
)
454 if (!show
&& IsModal())
456 EndModal( wxID_CANCEL
);
459 if (show
&& !m_sizeSet
)
461 /* by calling GtkOnSize here, we don't have to call
462 either after showing the frame, which would entail
463 much ugly flicker nor from within the size_allocate
464 handler, because GTK 1.1.X forbids that. */
466 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
469 wxWindow::Show( show
);
471 if (show
) InitDialog();
476 bool wxDialog::IsModal() const
478 return m_modalShowing
;
481 void wxDialog::SetModal( bool WXUNUSED(flag
) )
485 m_windowStyle |= wxDIALOG_MODAL;
487 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
489 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
492 int wxDialog::ShowModal()
496 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
497 return GetReturnCode();
502 m_modalShowing
= TRUE
;
504 gtk_grab_add( m_widget
);
506 gtk_grab_remove( m_widget
);
508 return GetReturnCode();
511 void wxDialog::EndModal( int retCode
)
513 SetReturnCode( retCode
);
517 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
521 m_modalShowing
= FALSE
;
528 void wxDialog::InitDialog()
530 wxWindow::InitDialog();
533 void wxDialog::SetIcon( const wxIcon
&icon
)
536 if (!icon
.Ok()) return;
538 wxMask
*mask
= icon
.GetMask();
539 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
540 if (mask
) bm
= mask
->GetBitmap();
542 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);