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 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
89 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
90 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
91 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
92 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
93 EVT_SIZE (wxDialog::OnSize
)
94 EVT_CLOSE (wxDialog::OnCloseWindow
)
97 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
102 m_modalShowing
= FALSE
;
105 wxDialog::wxDialog( wxWindow
*parent
,
106 wxWindowID id
, const wxString
&title
,
107 const wxPoint
&pos
, const wxSize
&size
,
108 long style
, const wxString
&name
)
112 Create( parent
, id
, title
, pos
, size
, style
, name
);
115 bool wxDialog::Create( wxWindow
*parent
,
116 wxWindowID id
, const wxString
&title
,
117 const wxPoint
&pos
, const wxSize
&size
,
118 long style
, const wxString
&name
)
120 wxTopLevelWindows
.Append( this );
122 m_needParent
= FALSE
;
124 PreCreation( parent
, id
, pos
, size
, style
, name
);
126 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
127 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
129 gtk_widget_set( m_widget
, "GtkWindow::allow_shrink", TRUE
, NULL
);
131 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
132 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
134 m_wxwindow
= gtk_myfixed_new();
135 gtk_widget_show( m_wxwindow
);
136 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
138 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
142 if (m_parent
) m_parent
->AddChild( this );
146 gtk_widget_realize( m_widget
);
148 /* all this is for Motif Window Manager "hints" and is supposed to be
149 recognized by other WM as well. not tested. */
150 long decor
= (long) GDK_DECOR_ALL
;
151 long func
= (long) GDK_FUNC_ALL
;
152 if ((m_windowStyle
& wxCAPTION
) == 0)
153 decor
|= GDK_DECOR_TITLE
;
155 if ((m_windowStyle & wxMINIMIZE) == 0)
156 func |= GDK_FUNC_MINIMIZE;
157 if ((m_windowStyle & wxMAXIMIZE) == 0)
158 func |= GDK_FUNC_MAXIMIZE;
160 if ((m_windowStyle
& wxSYSTEM_MENU
) == 0)
161 decor
|= GDK_DECOR_MENU
;
162 if ((m_windowStyle
& wxMINIMIZE_BOX
) == 0)
163 decor
|= GDK_DECOR_MINIMIZE
;
164 if ((m_windowStyle
& wxMAXIMIZE_BOX
) == 0)
165 decor
|= GDK_DECOR_MAXIMIZE
;
166 if ((m_windowStyle
& wxRESIZE_BORDER
) == 0)
167 func
|= GDK_FUNC_RESIZE
;
168 gdk_window_set_decorations(m_widget
->window
, (GdkWMDecoration
)decor
);
169 gdk_window_set_functions(m_widget
->window
, (GdkWMFunction
)func
);
171 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
172 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
174 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
175 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
180 wxDialog::~wxDialog()
182 wxTopLevelWindows
.DeleteObject( this );
184 if (wxTheApp
->GetTopWindow() == this)
186 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
189 if (wxTopLevelWindows
.Number() == 0)
191 wxTheApp
->ExitMainLoop();
195 void wxDialog::SetTitle( const wxString
& title
)
198 if (m_title
.IsNull()) m_title
= _T("");
199 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
202 wxString
wxDialog::GetTitle() const
204 return (wxString
&)m_title
;
207 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
209 if (Validate()) TransferDataFromWindow();
212 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
216 EndModal(wxID_CANCEL
);
220 SetReturnCode(wxID_CANCEL
);
225 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
227 if ( Validate() && TransferDataFromWindow())
235 SetReturnCode(wxID_OK
);
241 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
246 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
248 // We'll send a Cancel message by default,
249 // which may close the dialog.
250 // Check for looping if the Cancel event handler calls Close().
252 // Note that if a cancel button and handler aren't present in the dialog,
253 // nothing will happen when you close the dialog via the window manager, or
255 // We wouldn't want to destroy the dialog by default, since the dialog may have been
256 // created on the stack.
257 // However, this does mean that calling dialog->Close() won't delete the dialog
258 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
259 // sure to destroy the dialog.
260 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
262 static wxList s_closing
;
264 if (s_closing
.Member(this))
267 s_closing
.Append(this);
269 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
270 cancelEvent
.SetEventObject( this );
271 GetEventHandler()->ProcessEvent(cancelEvent
);
272 s_closing
.DeleteObject(this);
275 bool wxDialog::Destroy()
277 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
282 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
284 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
292 /* no child: go out ! */
293 if (!GetChildren().First()) return;
295 /* do we have exactly one child? */
296 wxWindow
*child
= (wxWindow
*) NULL
;
297 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
299 wxWindow
*win
= (wxWindow
*)node
->Data();
300 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
302 /* it's the second one: do nothing */
308 /* yes: set it's size to fill all the frame */
309 int client_x
, client_y
;
310 GetClientSize( &client_x
, &client_y
);
311 child
->SetSize( 1, 1, client_x
-2, client_y
);
315 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
317 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
318 wxASSERT_MSG( (m_wxwindow
!= NULL
), _T("invalid dialog") );
320 if (m_resizing
) return; /* I don't like recursions */
325 int old_width
= m_width
;
326 int old_height
= m_height
;
328 if ((sizeFlags
& wxSIZE_USE_EXISTING
) == wxSIZE_USE_EXISTING
)
330 if (x
!= -1) m_x
= x
;
331 if (y
!= -1) m_y
= y
;
332 if (width
!= -1) m_width
= width
;
333 if (height
!= -1) m_height
= height
;
343 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
345 if (width
== -1) m_width
= 80;
348 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
350 if (height
== -1) m_height
= 26;
353 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
354 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
355 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
356 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
358 if ((m_x
!= -1) || (m_y
!= -1))
360 if ((m_x
!= old_x
) || (m_y
!= old_y
))
362 /* m_sizeSet = FALSE; */
363 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
367 if ((m_width
!= old_width
) || (m_height
!= old_height
))
375 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
377 // due to a bug in gtk, x,y are always 0
381 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
382 if (!m_wxwindow
) return;
387 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
388 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
389 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
390 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
392 /* we actually set the size of a frame here and no-where else */
393 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
397 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
398 event
.SetEventObject( this );
399 GetEventHandler()->ProcessEvent( event
);
402 void wxDialog::Centre( int direction
)
404 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
409 if ((direction
& wxHORIZONTAL
) == wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
410 if ((direction
& wxVERTICAL
) == wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
415 void wxDialog::OnInternalIdle()
418 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
421 bool wxDialog::Show( bool show
)
423 if (!show
&& IsModal())
425 EndModal( wxID_CANCEL
);
428 if (show
&& !m_sizeSet
)
430 /* by calling GtkOnSize here, we don't have to call
431 either after showing the frame, which would entail
432 much ugly flicker nor from within the size_allocate
433 handler, because GTK 1.1.X forbids that. */
435 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
438 wxWindow::Show( show
);
440 if (show
) InitDialog();
445 bool wxDialog::IsModal() const
447 return m_modalShowing
;
450 void wxDialog::SetModal( bool WXUNUSED(flag
) )
454 m_windowStyle |= wxDIALOG_MODAL;
456 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
458 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
461 int wxDialog::ShowModal()
465 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
466 return GetReturnCode();
471 m_modalShowing
= TRUE
;
473 gtk_grab_add( m_widget
);
475 gtk_grab_remove( m_widget
);
477 return GetReturnCode();
480 void wxDialog::EndModal( int retCode
)
482 SetReturnCode( retCode
);
486 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
490 m_modalShowing
= FALSE
;
497 void wxDialog::InitDialog()
499 wxWindow::InitDialog();
502 void wxDialog::SetIcon( const wxIcon
&icon
)
505 if (!icon
.Ok()) return;
507 wxMask
*mask
= icon
.GetMask();
508 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
509 if (mask
) bm
= mask
->GetBitmap();
511 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);