]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/dialog.cpp
Fixed resizing of wxTextCtrl
[wxWidgets.git] / src / gtk1 / dialog.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: dialog.cpp
3// Purpose:
4// Author: Robert Roebling
a81258be 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
c801d85f
KB
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "dialog.h"
12#endif
13
14#include "wx/dialog.h"
15#include "wx/frame.h"
16#include "wx/app.h"
17#include "wx/gtk/win_gtk.h"
18
e2414cbe
RR
19//-----------------------------------------------------------------------------
20
21extern wxList wxPendingDelete;
22
c801d85f 23//-----------------------------------------------------------------------------
e1e955e1
RR
24// "delete_event"
25//-----------------------------------------------------------------------------
c801d85f
KB
26
27bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
28{
29/*
fb1585ae
RR
30 printf( "OnDelete from " );
31 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
32 printf( win->GetClassInfo()->GetClassName() );
33 printf( ".\n" );
c801d85f
KB
34*/
35
fb1585ae 36 win->Close();
c801d85f 37
fb1585ae 38 return TRUE;
c33c4050 39}
c801d85f
KB
40
41//-----------------------------------------------------------------------------
42// wxDialog
43//-----------------------------------------------------------------------------
44
a60c99e6 45BEGIN_EVENT_TABLE(wxDialog,wxPanel)
fb1585ae
RR
46 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
47 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
48 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
49 EVT_CLOSE (wxDialog::OnCloseWindow)
c801d85f
KB
50END_EVENT_TABLE()
51
a60c99e6 52IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
c801d85f 53
43a18898 54wxDialog::wxDialog()
c801d85f 55{
fb1585ae
RR
56 m_title = "";
57 m_modalShowing = FALSE;
58 wxTopLevelWindows.Insert( this );
c33c4050 59}
c801d85f
KB
60
61wxDialog::wxDialog( wxWindow *parent,
fb1585ae
RR
62 wxWindowID id, const wxString &title,
63 const wxPoint &pos, const wxSize &size,
64 long style, const wxString &name )
c801d85f 65{
fb1585ae
RR
66 m_modalShowing = FALSE;
67 wxTopLevelWindows.Insert( this );
68 Create( parent, id, title, pos, size, style, name );
c33c4050 69}
c801d85f
KB
70
71bool wxDialog::Create( wxWindow *parent,
fb1585ae
RR
72 wxWindowID id, const wxString &title,
73 const wxPoint &pos, const wxSize &size,
74 long style, const wxString &name )
c801d85f 75{
fb1585ae 76 m_needParent = FALSE;
c801d85f 77
fb1585ae 78 PreCreation( parent, id, pos, size, style, name );
c801d85f 79
fb1585ae
RR
80 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
81 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
c801d85f 82
fb1585ae 83 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL);
c801d85f 84
fb1585ae
RR
85 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
86 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
c801d85f 87
fb1585ae
RR
88 m_wxwindow = gtk_myfixed_new();
89 gtk_widget_show( m_wxwindow );
90 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
c801d85f 91
fb1585ae 92 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
c801d85f 93
fb1585ae 94 SetTitle( title );
c801d85f 95
fb1585ae
RR
96 if ((m_x != -1) || (m_y != -1))
97 gtk_widget_set_uposition( m_widget, m_x, m_y );
903f689b 98
fb1585ae 99 gtk_widget_set_usize( m_widget, m_width, m_height );
903f689b 100
fb1585ae 101 if (m_parent) m_parent->AddChild( this );
6ca41e57
RR
102
103
fb1585ae 104 PostCreation();
c801d85f 105
fb1585ae 106 return TRUE;
c33c4050 107}
c801d85f 108
43a18898 109wxDialog::~wxDialog()
c801d85f 110{
fb1585ae
RR
111 wxTopLevelWindows.DeleteObject( this );
112 if (wxTopLevelWindows.Number() == 0) wxTheApp->ExitMainLoop();
c33c4050 113}
c801d85f 114
43a18898 115void wxDialog::SetTitle( const wxString& title )
c801d85f 116{
fb1585ae
RR
117 m_title = title;
118 if (m_title.IsNull()) m_title = "";
119 gtk_window_set_title( GTK_WINDOW(m_widget), m_title );
c33c4050 120}
c801d85f 121
43a18898 122wxString wxDialog::GetTitle() const
c801d85f 123{
fb1585ae 124 return (wxString&)m_title;
c33c4050 125}
c801d85f
KB
126
127void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
128{
fb1585ae 129 if (Validate()) TransferDataFromWindow();
c33c4050 130}
c801d85f
KB
131
132void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
133{
fb1585ae
RR
134 if (IsModal())
135 {
136 EndModal(wxID_CANCEL);
137 }
138 else
139 {
140 SetReturnCode(wxID_CANCEL);
141 this->Show(FALSE);
142 }
c33c4050 143}
c801d85f 144
903f689b 145void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
c801d85f 146{
fb1585ae 147 if ( Validate() && TransferDataFromWindow())
1a6944fd 148 {
fb1585ae
RR
149 if (IsModal())
150 {
151 EndModal(wxID_OK);
152 }
153 else
154 {
155 SetReturnCode(wxID_OK);
156 this->Show(FALSE);
157 }
1a6944fd 158 }
c33c4050 159}
c801d85f
KB
160
161void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
162{
163 // yes
c33c4050 164}
c801d85f 165
43a18898 166bool wxDialog::OnClose()
c801d85f 167{
fb1585ae 168 static wxList closing;
c801d85f 169
fb1585ae 170 if (closing.Member(this)) return FALSE; // no loops
1a6944fd 171
fb1585ae 172 closing.Append(this);
c801d85f 173
fb1585ae
RR
174 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
175 cancelEvent.SetEventObject( this );
176 GetEventHandler()->ProcessEvent(cancelEvent);
177 closing.DeleteObject(this);
c801d85f 178
fb1585ae 179 return FALSE;
c801d85f
KB
180}
181
43a18898 182bool wxDialog::Destroy()
e2414cbe 183{
fb1585ae 184 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
e2414cbe 185
fb1585ae 186 return TRUE;
e2414cbe
RR
187}
188
43a18898 189void wxDialog::OnCloseWindow( wxCloseEvent& event )
c801d85f 190{
fb1585ae
RR
191 if (GetEventHandler()->OnClose() || event.GetForce())
192 {
193 this->Destroy();
194 }
c33c4050 195}
c801d85f 196
fb1585ae 197void wxDialog::SetSize( int x, int y, int width, int height, int sizeFlags )
903f689b 198{
fb1585ae
RR
199 wxASSERT_MSG( (m_widget != NULL), "invalid window" );
200
201 // Don't do anything for children of wxMDIChildFrame
202 if (!m_wxwindow) return;
203
204 if (m_resizing) return; // I don't like recursions
205 m_resizing = TRUE;
206
207 int old_x = m_x;
208 int old_y = m_y;
209 int old_width = m_width;
210 int old_height = m_height;
211
212 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
213 {
214 if (x != -1) m_x = x;
215 if (y != -1) m_y = y;
216 if (width != -1) m_width = width;
217 if (height != -1) m_height = height;
218 }
219 else
220 {
221 m_x = x;
222 m_y = y;
223 m_width = width;
224 m_height = height;
225 }
226
227 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
228 {
229 if (width == -1) m_width = 80;
230 }
231
232 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
233 {
234 if (height == -1) m_height = 26;
235 }
236
237 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
238 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
239 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_minWidth;
240 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_minHeight;
241
242 if ((m_x != -1) || (m_y != -1))
243 {
244 if ((m_x != old_x) || (m_y != old_y))
245 gtk_widget_set_uposition( m_widget, m_x, m_y );
246 }
247
248 if ((m_width != old_width) || (m_height != old_height))
249 {
250 gtk_widget_set_usize( m_widget, m_width, m_height );
251 }
252
253 m_sizeSet = TRUE;
254
255 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
256 event.SetEventObject( this );
257 ProcessEvent( event );
258
259 m_resizing = FALSE;
903f689b
RR
260}
261
43a18898
RR
262void wxDialog::SetSize( int width, int height )
263{
264 SetSize( -1, -1, width, height, wxSIZE_USE_EXISTING );
265}
266
903f689b
RR
267void wxDialog::Centre( int direction )
268{
fb1585ae
RR
269 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
270
43a18898
RR
271 int x = 0;
272 int y = 0;
fb1585ae
RR
273
274 if (direction & wxHORIZONTAL == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
275 if (direction & wxVERTICAL == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
276
277 Move( x, y );
903f689b
RR
278}
279
debe6624 280bool wxDialog::Show( bool show )
c801d85f 281{
fb1585ae
RR
282 if (!show && IsModal())
283 {
284 EndModal( wxID_CANCEL );
285 }
c801d85f 286
fb1585ae 287 wxWindow::Show( show );
c801d85f 288
fb1585ae 289 if (show) InitDialog();
c801d85f 290
fb1585ae 291 return TRUE;
c33c4050
RR
292}
293
43a18898 294bool wxDialog::IsModal() const
e1e955e1 295{
fb1585ae 296 return m_modalShowing;
e1e955e1
RR
297}
298
299void wxDialog::SetModal( bool WXUNUSED(flag) )
c33c4050 300{
e1e955e1 301/*
c33c4050
RR
302 if (flag)
303 m_windowStyle |= wxDIALOG_MODAL;
304 else
305 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
e1e955e1 306*/
fb1585ae 307 wxFAIL_MSG( "wxDialog:SetModal obsolete now" );
c33c4050 308}
c801d85f 309
43a18898 310int wxDialog::ShowModal()
c801d85f 311{
fb1585ae
RR
312 if (IsModal())
313 {
314 wxFAIL_MSG( "wxDialog:ShowModal called twice" );
315 return GetReturnCode();
316 }
d355d3fe 317
fb1585ae 318 Show( TRUE );
d355d3fe 319
fb1585ae 320 m_modalShowing = TRUE;
d355d3fe 321
fb1585ae
RR
322 gtk_grab_add( m_widget );
323 gtk_main();
324 gtk_grab_remove( m_widget );
d355d3fe 325
fb1585ae 326 return GetReturnCode();
c33c4050 327}
c801d85f
KB
328
329void wxDialog::EndModal( int retCode )
330{
fb1585ae 331 SetReturnCode( retCode );
d355d3fe 332
fb1585ae
RR
333 if (!IsModal())
334 {
335 wxFAIL_MSG( "wxDialog:EndModal called twice" );
336 return;
337 }
b6af8d80 338
fb1585ae 339 m_modalShowing = FALSE;
d355d3fe 340
fb1585ae 341 gtk_main_quit();
5b011451 342
fb1585ae 343 Show( FALSE );
c33c4050 344}
c801d85f 345
43a18898 346void wxDialog::InitDialog()
c801d85f 347{
fb1585ae 348 wxWindow::InitDialog();
c33c4050
RR
349}
350
c33c4050
RR
351void wxDialog::SetIcon( const wxIcon &icon )
352{
fb1585ae
RR
353 m_icon = icon;
354 if (!icon.Ok()) return;
c33c4050 355
fb1585ae
RR
356 wxMask *mask = icon.GetMask();
357 GdkBitmap *bm = (GdkBitmap *) NULL;
358 if (mask) bm = mask->GetBitmap();
c33c4050 359
fb1585ae 360 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
c33c4050 361}