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
;
153 if ((m_windowStyle
& wxCAPTION
) == 0)
154 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; */
159 if ((m_windowStyle
& wxSYSTEM_MENU
) == 0)
160 decor
|= GDK_DECOR_MENU
;
161 if ((m_windowStyle
& wxMINIMIZE_BOX
) == 0)
162 decor
|= GDK_DECOR_MINIMIZE
;
163 if ((m_windowStyle
& wxMAXIMIZE_BOX
) == 0)
164 decor
|= GDK_DECOR_MAXIMIZE
;
165 if ((m_windowStyle
& wxRESIZE_BORDER
) == 0)
166 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's shrinking/growing policy */
172 if ((m_windowStyle
& wxRESIZE_BORDER
) == 0)
173 gtk_window_set_policy(GTK_WINDOW(m_widget
), 0, 0, 1);
175 gtk_window_set_policy(GTK_WINDOW(m_widget
), 1, 1, 1);
177 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
178 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
180 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
181 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
186 wxDialog::~wxDialog()
188 wxTopLevelWindows
.DeleteObject( this );
190 if (wxTheApp
->GetTopWindow() == this)
192 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
195 if (wxTopLevelWindows
.Number() == 0)
197 wxTheApp
->ExitMainLoop();
201 void wxDialog::SetTitle( const wxString
& title
)
204 if (m_title
.IsNull()) m_title
= _T("");
205 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
208 wxString
wxDialog::GetTitle() const
210 return (wxString
&)m_title
;
213 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
215 if (Validate()) TransferDataFromWindow();
218 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
222 EndModal(wxID_CANCEL
);
226 SetReturnCode(wxID_CANCEL
);
231 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
233 if ( Validate() && TransferDataFromWindow())
241 SetReturnCode(wxID_OK
);
247 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
252 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
254 // We'll send a Cancel message by default,
255 // which may close the dialog.
256 // Check for looping if the Cancel event handler calls Close().
258 // Note that if a cancel button and handler aren't present in the dialog,
259 // nothing will happen when you close the dialog via the window manager, or
261 // We wouldn't want to destroy the dialog by default, since the dialog may have been
262 // created on the stack.
263 // However, this does mean that calling dialog->Close() won't delete the dialog
264 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
265 // sure to destroy the dialog.
266 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
268 static wxList s_closing
;
270 if (s_closing
.Member(this))
273 s_closing
.Append(this);
275 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
276 cancelEvent
.SetEventObject( this );
277 GetEventHandler()->ProcessEvent(cancelEvent
);
278 s_closing
.DeleteObject(this);
281 bool wxDialog::Destroy()
283 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
288 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
290 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
298 /* no child: go out ! */
299 if (!GetChildren().First()) return;
301 /* do we have exactly one child? */
302 wxWindow
*child
= (wxWindow
*) NULL
;
303 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
305 wxWindow
*win
= (wxWindow
*)node
->Data();
306 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
308 /* it's the second one: do nothing */
314 /* yes: set it's size to fill all the frame */
315 int client_x
, client_y
;
316 GetClientSize( &client_x
, &client_y
);
317 child
->SetSize( 1, 1, client_x
-2, client_y
);
321 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
323 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
324 wxASSERT_MSG( (m_wxwindow
!= NULL
), _T("invalid dialog") );
326 if (m_resizing
) return; /* I don't like recursions */
331 int old_width
= m_width
;
332 int old_height
= m_height
;
334 if ((sizeFlags
& wxSIZE_USE_EXISTING
) == wxSIZE_USE_EXISTING
)
336 if (x
!= -1) m_x
= x
;
337 if (y
!= -1) m_y
= y
;
338 if (width
!= -1) m_width
= width
;
339 if (height
!= -1) m_height
= height
;
349 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
351 if (width
== -1) m_width
= 80;
354 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
356 if (height
== -1) m_height
= 26;
359 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
360 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
361 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
362 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
364 if ((m_x
!= -1) || (m_y
!= -1))
366 if ((m_x
!= old_x
) || (m_y
!= old_y
))
368 /* m_sizeSet = FALSE; */
369 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
373 if ((m_width
!= old_width
) || (m_height
!= old_height
))
381 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
383 // due to a bug in gtk, x,y are always 0
387 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
388 if (!m_wxwindow
) return;
393 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
394 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
395 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
396 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
398 /* we actually set the size of a frame here and no-where else */
399 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
403 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
404 event
.SetEventObject( this );
405 GetEventHandler()->ProcessEvent( event
);
408 void wxDialog::Centre( int direction
)
410 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
415 if ((direction
& wxHORIZONTAL
) == wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
416 if ((direction
& wxVERTICAL
) == wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
421 void wxDialog::OnInternalIdle()
424 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
427 bool wxDialog::Show( bool show
)
429 if (!show
&& IsModal())
431 EndModal( wxID_CANCEL
);
434 if (show
&& !m_sizeSet
)
436 /* by calling GtkOnSize here, we don't have to call
437 either after showing the frame, which would entail
438 much ugly flicker nor from within the size_allocate
439 handler, because GTK 1.1.X forbids that. */
441 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
444 wxWindow::Show( show
);
446 if (show
) InitDialog();
451 bool wxDialog::IsModal() const
453 return m_modalShowing
;
456 void wxDialog::SetModal( bool WXUNUSED(flag
) )
460 m_windowStyle |= wxDIALOG_MODAL;
462 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
464 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
467 int wxDialog::ShowModal()
471 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
472 return GetReturnCode();
477 m_modalShowing
= TRUE
;
479 gtk_grab_add( m_widget
);
481 gtk_grab_remove( m_widget
);
483 return GetReturnCode();
486 void wxDialog::EndModal( int retCode
)
488 SetReturnCode( retCode
);
492 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
496 m_modalShowing
= FALSE
;
503 void wxDialog::InitDialog()
505 wxWindow::InitDialog();
508 void wxDialog::SetIcon( const wxIcon
&icon
)
511 if (!icon
.Ok()) return;
513 wxMask
*mask
= icon
.GetMask();
514 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
515 if (mask
) bm
= mask
->GetBitmap();
517 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);