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