]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dialog.cpp
listbox kbd handling buglet corrected (event.Skip() called even when no
[wxWidgets.git] / src / gtk / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dialog.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
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
19 //-----------------------------------------------------------------------------
20
21 extern wxList wxPendingDelete;
22
23 //-----------------------------------------------------------------------------
24 // "delete_event"
25 //-----------------------------------------------------------------------------
26
27 bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
28 {
29 /*
30 printf( "OnDelete from " );
31 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
32 printf( win->GetClassInfo()->GetClassName() );
33 printf( ".\n" );
34 */
35
36 win->Close();
37
38 return TRUE;
39 }
40
41 //-----------------------------------------------------------------------------
42 // "size_allocate"
43 //-----------------------------------------------------------------------------
44
45 static void gtk_dialog_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxDialog *win )
46 {
47 if (!win->HasVMT()) return;
48
49 /*
50 printf( "OnDialogResize from " );
51 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
52 printf( win->GetClassInfo()->GetClassName() );
53 printf( ".\n" );
54 */
55
56 win->GtkOnSize( alloc->x, alloc->y, alloc->width, alloc->height );
57 }
58
59 //-----------------------------------------------------------------------------
60 // wxDialog
61 //-----------------------------------------------------------------------------
62
63 BEGIN_EVENT_TABLE(wxDialog,wxPanel)
64 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
65 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
66 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
67 EVT_SIZE (wxDialog::OnSize)
68 EVT_CLOSE (wxDialog::OnCloseWindow)
69 END_EVENT_TABLE()
70
71 IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
72
73 wxDialog::wxDialog()
74 {
75 m_title = "";
76 m_modalShowing = FALSE;
77 }
78
79 wxDialog::wxDialog( wxWindow *parent,
80 wxWindowID id, const wxString &title,
81 const wxPoint &pos, const wxSize &size,
82 long style, const wxString &name )
83 {
84 m_modalShowing = FALSE;
85 Create( parent, id, title, pos, size, style, name );
86 }
87
88 bool wxDialog::Create( wxWindow *parent,
89 wxWindowID id, const wxString &title,
90 const wxPoint &pos, const wxSize &size,
91 long style, const wxString &name )
92 {
93 wxTopLevelWindows.Append( this );
94
95 m_needParent = FALSE;
96
97 PreCreation( parent, id, pos, size, style, name );
98
99 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
100 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
101
102 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL);
103
104 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
105 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
106
107 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
108 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
109
110 m_wxwindow = gtk_myfixed_new();
111 gtk_widget_show( m_wxwindow );
112 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
113
114 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
115
116 SetTitle( title );
117
118 if ((m_x != -1) || (m_y != -1))
119 gtk_widget_set_uposition( m_widget, m_x, m_y );
120
121 gtk_widget_set_usize( m_widget, m_width, m_height );
122
123 if (m_parent) m_parent->AddChild( this );
124
125
126 PostCreation();
127
128 return TRUE;
129 }
130
131 wxDialog::~wxDialog()
132 {
133 wxTopLevelWindows.DeleteObject( this );
134 if (wxTopLevelWindows.Number() == 0) wxTheApp->ExitMainLoop();
135 }
136
137 void wxDialog::SetTitle( const wxString& title )
138 {
139 m_title = title;
140 if (m_title.IsNull()) m_title = "";
141 gtk_window_set_title( GTK_WINDOW(m_widget), m_title );
142 }
143
144 wxString wxDialog::GetTitle() const
145 {
146 return (wxString&)m_title;
147 }
148
149 void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
150 {
151 if (Validate()) TransferDataFromWindow();
152 }
153
154 void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
155 {
156 if (IsModal())
157 {
158 EndModal(wxID_CANCEL);
159 }
160 else
161 {
162 SetReturnCode(wxID_CANCEL);
163 this->Show(FALSE);
164 }
165 }
166
167 void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
168 {
169 if ( Validate() && TransferDataFromWindow())
170 {
171 if (IsModal())
172 {
173 EndModal(wxID_OK);
174 }
175 else
176 {
177 SetReturnCode(wxID_OK);
178 this->Show(FALSE);
179 }
180 }
181 }
182
183 void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
184 {
185 // yes
186 }
187
188 bool wxDialog::OnClose()
189 {
190 static wxList closing;
191
192 if (closing.Member(this)) return FALSE; // no loops
193
194 closing.Append(this);
195
196 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
197 cancelEvent.SetEventObject( this );
198 GetEventHandler()->ProcessEvent(cancelEvent);
199 closing.DeleteObject(this);
200
201 return FALSE;
202 }
203
204 bool wxDialog::Destroy()
205 {
206 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
207
208 return TRUE;
209 }
210
211 void wxDialog::OnCloseWindow( wxCloseEvent& event )
212 {
213 if (GetEventHandler()->OnClose() || event.GetForce())
214 {
215 this->Destroy();
216 }
217 }
218
219 void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
220 {
221 // due to a bug in gtk, x,y are always 0
222 // m_x = x;
223 // m_y = y;
224
225 if ((m_height == height) && (m_width == width) &&
226 (m_sizeSet)) return;
227 if (!m_wxwindow) return;
228
229 m_width = width;
230 m_height = height;
231
232 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
233 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
234 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_minWidth;
235 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_minHeight;
236
237 gtk_widget_set_usize( m_widget, m_width, m_height );
238
239 m_sizeSet = TRUE;
240
241 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
242 event.SetEventObject( this );
243 GetEventHandler()->ProcessEvent( event );
244 }
245
246 void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
247 {
248 wxASSERT_MSG( (m_widget != NULL), "invalid dialog" );
249
250 if (GetAutoLayout())
251 {
252 Layout();
253 }
254 else
255 {
256 // no child: go out !
257 if (!GetChildren().First()) return;
258
259 // do we have exactly one child?
260 wxWindow *child = (wxWindow *) NULL;
261 for(wxNode *node = GetChildren().First(); node; node = node->Next())
262 {
263 wxWindow *win = (wxWindow *)node->Data();
264 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
265 {
266 // it's the second one: do nothing
267 if (child) return;
268 child = win;
269 }
270 }
271
272 // yes: set it's size to fill all the frame
273 int client_x, client_y;
274 GetClientSize( &client_x, &client_y );
275 child->SetSize( 1, 1, client_x-2, client_y);
276 }
277 }
278
279 void wxDialog::SetSize( int x, int y, int width, int height, int sizeFlags )
280 {
281 wxASSERT_MSG( (m_widget != NULL), "invalid window" );
282
283 // Don't do anything for children of wxMDIChildFrame
284 if (!m_wxwindow) return;
285
286 if (m_resizing) return; // I don't like recursions
287 m_resizing = TRUE;
288
289 int old_x = m_x;
290 int old_y = m_y;
291 int old_width = m_width;
292 int old_height = m_height;
293
294 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
295 {
296 if (x != -1) m_x = x;
297 if (y != -1) m_y = y;
298 if (width != -1) m_width = width;
299 if (height != -1) m_height = height;
300 }
301 else
302 {
303 m_x = x;
304 m_y = y;
305 m_width = width;
306 m_height = height;
307 }
308
309 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
310 {
311 if (width == -1) m_width = 80;
312 }
313
314 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
315 {
316 if (height == -1) m_height = 26;
317 }
318
319 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
320 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
321 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_minWidth;
322 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_minHeight;
323
324 if ((m_x != -1) || (m_y != -1))
325 {
326 if ((m_x != old_x) || (m_y != old_y))
327 gtk_widget_set_uposition( m_widget, m_x, m_y );
328 }
329
330 if ((m_width != old_width) || (m_height != old_height))
331 {
332 gtk_widget_set_usize( m_widget, m_width, m_height );
333 }
334
335 m_sizeSet = TRUE;
336
337 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
338 event.SetEventObject( this );
339 GetEventHandler()->ProcessEvent( event );
340
341 m_resizing = FALSE;
342 }
343
344 void wxDialog::SetSize( int width, int height )
345 {
346 SetSize( -1, -1, width, height, wxSIZE_USE_EXISTING );
347 }
348
349 void wxDialog::Centre( int direction )
350 {
351 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
352
353 int x = 0;
354 int y = 0;
355
356 if (direction & wxHORIZONTAL == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
357 if (direction & wxVERTICAL == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
358
359 Move( x, y );
360 }
361
362 bool wxDialog::Show( bool show )
363 {
364 if (!show && IsModal())
365 {
366 EndModal( wxID_CANCEL );
367 }
368
369 wxWindow::Show( show );
370
371 if (show) InitDialog();
372
373 return TRUE;
374 }
375
376 bool wxDialog::IsModal() const
377 {
378 return m_modalShowing;
379 }
380
381 void wxDialog::SetModal( bool WXUNUSED(flag) )
382 {
383 /*
384 if (flag)
385 m_windowStyle |= wxDIALOG_MODAL;
386 else
387 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
388 */
389 wxFAIL_MSG( "wxDialog:SetModal obsolete now" );
390 }
391
392 int wxDialog::ShowModal()
393 {
394 if (IsModal())
395 {
396 wxFAIL_MSG( "wxDialog:ShowModal called twice" );
397 return GetReturnCode();
398 }
399
400 Show( TRUE );
401
402 m_modalShowing = TRUE;
403
404 gtk_grab_add( m_widget );
405 gtk_main();
406 gtk_grab_remove( m_widget );
407
408 return GetReturnCode();
409 }
410
411 void wxDialog::EndModal( int retCode )
412 {
413 SetReturnCode( retCode );
414
415 if (!IsModal())
416 {
417 wxFAIL_MSG( "wxDialog:EndModal called twice" );
418 return;
419 }
420
421 m_modalShowing = FALSE;
422
423 gtk_main_quit();
424
425 Show( FALSE );
426 }
427
428 void wxDialog::InitDialog()
429 {
430 wxWindow::InitDialog();
431 }
432
433 void wxDialog::SetIcon( const wxIcon &icon )
434 {
435 m_icon = icon;
436 if (!icon.Ok()) return;
437
438 wxMask *mask = icon.GetMask();
439 GdkBitmap *bm = (GdkBitmap *) NULL;
440 if (mask) bm = mask->GetBitmap();
441
442 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
443 }