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 //-----------------------------------------------------------------------------
136 // "map" from m_widget
137 //-----------------------------------------------------------------------------
140 gtk_dialog_map_callback( GtkWidget
*widget
, wxDialog
*win
)
142 gtk_widget_set_uposition( widget
, win
->m_x
, win
->m_y
);
147 //-----------------------------------------------------------------------------
149 //-----------------------------------------------------------------------------
151 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
152 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
153 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
154 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
155 EVT_SIZE (wxDialog::OnSize
)
156 EVT_CLOSE (wxDialog::OnCloseWindow
)
159 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
161 void wxDialog::Init()
164 m_modalShowing
= FALSE
;
167 wxDialog::wxDialog( wxWindow
*parent
,
168 wxWindowID id
, const wxString
&title
,
169 const wxPoint
&pos
, const wxSize
&size
,
170 long style
, const wxString
&name
)
174 Create( parent
, id
, title
, pos
, size
, style
, name
);
177 bool wxDialog::Create( wxWindow
*parent
,
178 wxWindowID id
, const wxString
&title
,
179 const wxPoint
&pos
, const wxSize
&size
,
180 long style
, const wxString
&name
)
182 wxTopLevelWindows
.Append( this );
184 m_needParent
= FALSE
;
186 PreCreation( parent
, id
, pos
, size
, style
, name
);
188 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
189 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
191 gtk_widget_set( m_widget
, "GtkWindow::allow_shrink", TRUE
, NULL
);
193 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
194 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
196 m_wxwindow
= gtk_myfixed_new();
197 gtk_widget_show( m_wxwindow
);
198 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
200 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
204 if (m_parent
) m_parent
->AddChild( this );
208 /* we cannot set MWM hints before the widget has
209 been realized, so we do this directly after realization */
210 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
211 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
213 /* we set the position of the window after the map event. setting it
214 before has no effect (with KWM) */
215 gtk_signal_connect( GTK_OBJECT(m_widget
), "map",
216 GTK_SIGNAL_FUNC(gtk_dialog_map_callback
), (gpointer
) this );
218 /* the user resized the frame by dragging etc. */
219 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
220 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
222 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
223 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
228 wxDialog::~wxDialog()
230 wxTopLevelWindows
.DeleteObject( this );
232 if (wxTheApp
->GetTopWindow() == this)
234 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
237 if (wxTopLevelWindows
.Number() == 0)
239 wxTheApp
->ExitMainLoop();
243 void wxDialog::SetTitle( const wxString
& title
)
246 if (m_title
.IsNull()) m_title
= _T("");
247 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
250 wxString
wxDialog::GetTitle() const
252 return (wxString
&)m_title
;
255 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
257 if (Validate()) TransferDataFromWindow();
260 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
264 EndModal(wxID_CANCEL
);
268 SetReturnCode(wxID_CANCEL
);
273 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
275 if ( Validate() && TransferDataFromWindow())
283 SetReturnCode(wxID_OK
);
289 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
294 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
296 // We'll send a Cancel message by default,
297 // which may close the dialog.
298 // Check for looping if the Cancel event handler calls Close().
300 // Note that if a cancel button and handler aren't present in the dialog,
301 // nothing will happen when you close the dialog via the window manager, or
303 // We wouldn't want to destroy the dialog by default, since the dialog may have been
304 // created on the stack.
305 // However, this does mean that calling dialog->Close() won't delete the dialog
306 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
307 // sure to destroy the dialog.
308 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
310 static wxList s_closing
;
312 if (s_closing
.Member(this))
315 s_closing
.Append(this);
317 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
318 cancelEvent
.SetEventObject( this );
319 GetEventHandler()->ProcessEvent(cancelEvent
);
320 s_closing
.DeleteObject(this);
323 bool wxDialog::Destroy()
325 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
330 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
332 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
340 /* no child: go out ! */
341 if (!GetChildren().First()) return;
343 /* do we have exactly one child? */
344 wxWindow
*child
= (wxWindow
*) NULL
;
345 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
347 wxWindow
*win
= (wxWindow
*)node
->Data();
348 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
350 /* it's the second one: do nothing */
356 /* yes: set it's size to fill all the frame */
357 int client_x
, client_y
;
358 GetClientSize( &client_x
, &client_y
);
359 child
->SetSize( 1, 1, client_x
-2, client_y
);
363 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
365 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
366 wxASSERT_MSG( (m_wxwindow
!= NULL
), _T("invalid dialog") );
368 if (m_resizing
) return; /* I don't like recursions */
373 int old_width
= m_width
;
374 int old_height
= m_height
;
376 if ((sizeFlags
& wxSIZE_USE_EXISTING
) == wxSIZE_USE_EXISTING
)
378 if (x
!= -1) m_x
= x
;
379 if (y
!= -1) m_y
= y
;
380 if (width
!= -1) m_width
= width
;
381 if (height
!= -1) m_height
= height
;
391 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
393 if (width
== -1) m_width
= 80;
396 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
398 if (height
== -1) m_height
= 26;
401 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
402 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
403 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
404 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
406 if ((m_x
!= -1) || (m_y
!= -1))
408 if ((m_x
!= old_x
) || (m_y
!= old_y
))
410 /* we set the position here and when showing the dialog
411 for the first time in idle time */
412 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
416 if ((m_width
!= old_width
) || (m_height
!= old_height
))
418 /* actual resizing is deferred to GtkOnSize in idle time and
419 when showing the dialog */
426 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
428 // due to a bug in gtk, x,y are always 0
432 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
433 if (!m_wxwindow
) return;
438 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
439 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
440 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
441 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
443 /* we actually set the size of a frame here and no-where else */
444 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
448 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
449 event
.SetEventObject( this );
450 GetEventHandler()->ProcessEvent( event
);
453 void wxDialog::Centre( int direction
)
455 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
460 if ((direction
& wxHORIZONTAL
) == wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
461 if ((direction
& wxVERTICAL
) == wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
466 void wxDialog::OnInternalIdle()
468 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
469 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
472 bool wxDialog::Show( bool show
)
474 if (!show
&& IsModal())
476 EndModal( wxID_CANCEL
);
479 if (show
&& !m_sizeSet
)
481 /* by calling GtkOnSize here, we don't have to call
482 either after showing the frame, which would entail
483 much ugly flicker nor from within the size_allocate
484 handler, because GTK 1.1.X forbids that. */
486 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
489 if (show
!= m_isShown
)
493 gtk_widget_show( m_widget
);
496 gtk_widget_hide( m_widget
);
501 if (show
) InitDialog();
506 bool wxDialog::IsModal() const
508 return m_modalShowing
;
511 void wxDialog::SetModal( bool WXUNUSED(flag
) )
515 m_windowStyle |= wxDIALOG_MODAL;
517 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
519 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
522 int wxDialog::ShowModal()
526 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
527 return GetReturnCode();
532 m_modalShowing
= TRUE
;
534 gtk_grab_add( m_widget
);
536 gtk_grab_remove( m_widget
);
538 return GetReturnCode();
541 void wxDialog::EndModal( int retCode
)
543 SetReturnCode( retCode
);
547 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
551 m_modalShowing
= FALSE
;
558 void wxDialog::InitDialog()
560 wxWindow::InitDialog();
563 void wxDialog::SetIcon( const wxIcon
&icon
)
566 if (!icon
.Ok()) return;
568 wxMask
*mask
= icon
.GetMask();
569 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
570 if (mask
) bm
= mask
->GetBitmap();
572 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);