]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dialog.cpp
Restored the ability to scroll.
[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 #include "wx/cursor.h"
22
23 //-----------------------------------------------------------------------------
24 // idle system
25 //-----------------------------------------------------------------------------
26
27 extern void wxapp_install_idle_handler();
28 extern bool g_isIdle;
29
30 //-----------------------------------------------------------------------------
31 // data
32 //-----------------------------------------------------------------------------
33
34 extern wxList wxPendingDelete;
35
36 //-----------------------------------------------------------------------------
37 // "delete_event"
38 //-----------------------------------------------------------------------------
39
40 bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
41 {
42 if (g_isIdle)
43 wxapp_install_idle_handler();
44
45 win->Close();
46
47 return TRUE;
48 }
49
50 //-----------------------------------------------------------------------------
51 // "size_allocate"
52 //-----------------------------------------------------------------------------
53
54 static void gtk_dialog_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxDialog *win )
55 {
56 if (g_isIdle)
57 wxapp_install_idle_handler();
58
59 if (!win->m_hasVMT) return;
60
61 if ((win->m_width != alloc->width) || (win->m_height != alloc->height))
62 {
63 win->m_width = alloc->width;
64 win->m_height = alloc->height;
65 win->UpdateSize();
66 }
67 }
68
69 //-----------------------------------------------------------------------------
70 // "configure_event"
71 //-----------------------------------------------------------------------------
72
73 static gint
74 #if (GTK_MINOR_VERSON > 0)
75 gtk_dialog_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDialog *win )
76 #else
77 gtk_dialog_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *event, wxDialog *win )
78 #endif
79 {
80 if (g_isIdle)
81 wxapp_install_idle_handler();
82
83 if (!win->m_hasVMT) return FALSE;
84
85 #if (GTK_MINOR_VERSON > 0)
86 int x = 0;
87 int y = 0;
88 gdk_window_get_root_origin( win->m_widget->window, &x, &y );
89 win->m_x = x;
90 win->m_y = y;
91 #else
92 win->m_x = event->x;
93 win->m_y = event->y;
94 #endif
95
96 wxMoveEvent mevent( wxPoint(win->m_x,win->m_y), win->GetId() );
97 mevent.SetEventObject( win );
98 win->GetEventHandler()->ProcessEvent( mevent );
99
100 return FALSE;
101 }
102
103 //-----------------------------------------------------------------------------
104 // "realize" from m_widget
105 //-----------------------------------------------------------------------------
106
107 /* we cannot MWM hints and icons before the widget has been realized,
108 so we do this directly after realization */
109
110 static gint
111 gtk_dialog_realized_callback( GtkWidget *widget, wxDialog *win )
112 {
113 if (g_isIdle)
114 wxapp_install_idle_handler();
115
116 /* I haven''t been able to set the position of
117 the dialog before it is shown, so I set the
118 position in "realize" and "map" */
119 gtk_widget_set_uposition( widget, win->m_x, win->m_y );
120
121 /* reset the icon */
122 if (win->m_icon != wxNullIcon)
123 {
124 wxIcon icon( win->m_icon );
125 win->m_icon = wxNullIcon;
126 win->SetIcon( icon );
127 }
128
129 return FALSE;
130 }
131
132 //-----------------------------------------------------------------------------
133 // "map" from m_widget
134 //-----------------------------------------------------------------------------
135
136 static gint
137 gtk_dialog_map_callback( GtkWidget *widget, wxDialog *win )
138 {
139 if (g_isIdle)
140 wxapp_install_idle_handler();
141
142 /* I haven''t been able to set the position of
143 the dialog before it is shown, so I set the
144 position in "realize" and "map" */
145 gtk_widget_set_uposition( widget, win->m_x, win->m_y );
146
147 /* all this is for Motif Window Manager "hints" and is supposed to be
148 recognized by other WM as well. not tested. */
149 long decor = (long) GDK_DECOR_BORDER;
150 long func = (long) GDK_FUNC_MOVE ;
151
152 if ((win->GetWindowStyle() & wxCAPTION) != 0)
153 decor |= GDK_DECOR_TITLE;
154 if ((win->GetWindowStyle() & wxSYSTEM_MENU) != 0)
155 {
156 decor |= GDK_DECOR_MENU;
157 func |= GDK_FUNC_CLOSE;
158 }
159 if ((win->GetWindowStyle() & wxMINIMIZE_BOX) != 0)
160 {
161 func |= GDK_FUNC_MINIMIZE;
162 decor |= GDK_DECOR_MINIMIZE;
163 }
164 if ((win->GetWindowStyle() & wxMAXIMIZE_BOX) != 0)
165 {
166 decor |= GDK_DECOR_MAXIMIZE;
167 func |= GDK_FUNC_MAXIMIZE;
168 }
169 if ((win->GetWindowStyle() & wxRESIZE_BORDER) != 0)
170 {
171 func |= GDK_FUNC_RESIZE;
172 decor |= GDK_DECOR_RESIZEH;
173 }
174 gdk_window_set_decorations( win->m_widget->window, (GdkWMDecoration)decor);
175 gdk_window_set_functions( win->m_widget->window, (GdkWMFunction)func);
176
177 /* GTK's shrinking/growing policy */
178 if ((win->GetWindowStyle() & wxRESIZE_BORDER) == 0)
179 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 0, 0, 1);
180 else
181 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 1, 1, 1);
182
183 return FALSE;
184 }
185
186 //-----------------------------------------------------------------------------
187 // InsertChild for wxDialog
188 //-----------------------------------------------------------------------------
189
190 /* Callback for wxFrame. This very strange beast has to be used because
191 * C++ has no virtual methods in a constructor. We have to emulate a
192 * virtual function here as wxWindows requires different ways to insert
193 * a child in container classes. */
194
195 static void wxInsertChildInDialog( wxDialog* parent, wxWindow* child )
196 {
197 gtk_myfixed_put( GTK_MYFIXED(parent->m_wxwindow),
198 GTK_WIDGET(child->m_widget),
199 child->m_x,
200 child->m_y,
201 child->m_width,
202 child->m_height );
203
204 if (parent->HasFlag(wxTAB_TRAVERSAL))
205 {
206 /* we now allow a window to get the focus as long as it
207 doesn't have any children. */
208 GTK_WIDGET_UNSET_FLAGS( parent->m_wxwindow, GTK_CAN_FOCUS );
209 }
210 }
211
212 //-----------------------------------------------------------------------------
213 // wxDialog
214 //-----------------------------------------------------------------------------
215
216 BEGIN_EVENT_TABLE(wxDialog,wxPanel)
217 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
218 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
219 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
220 EVT_SIZE (wxDialog::OnSize)
221 EVT_CLOSE (wxDialog::OnCloseWindow)
222 END_EVENT_TABLE()
223
224 IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
225
226 void wxDialog::Init()
227 {
228 m_returnCode = 0;
229 m_sizeSet = FALSE;
230 m_modalShowing = FALSE;
231 }
232
233 wxDialog::wxDialog( wxWindow *parent,
234 wxWindowID id, const wxString &title,
235 const wxPoint &pos, const wxSize &size,
236 long style, const wxString &name )
237 {
238 Init();
239
240 Create( parent, id, title, pos, size, style, name );
241 }
242
243 bool wxDialog::Create( wxWindow *parent,
244 wxWindowID id, const wxString &title,
245 const wxPoint &pos, const wxSize &size,
246 long style, const wxString &name )
247 {
248 wxTopLevelWindows.Append( this );
249
250 m_needParent = FALSE;
251
252 if (!PreCreation( parent, pos, size ) ||
253 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
254 {
255 wxFAIL_MSG( _T("wxDialog creation failed") );
256 return FALSE;
257 }
258
259 m_insertCallback = (wxInsertChildFunction) wxInsertChildInDialog;
260
261 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
262
263 if (!name.IsEmpty())
264 gtk_window_set_wmclass( GTK_WINDOW(m_widget), name.mb_str(), name.mb_str() );
265
266 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
267
268 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
269 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
270
271 m_wxwindow = gtk_myfixed_new();
272 gtk_widget_show( m_wxwindow );
273 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
274
275 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
276
277 SetTitle( title );
278
279 if (m_parent) m_parent->AddChild( this );
280
281 PostCreation();
282
283 /* we cannot set MWM hints before the widget has
284 been realized, so we do this directly after realization */
285 gtk_signal_connect( GTK_OBJECT(m_widget), "realize",
286 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback), (gpointer) this );
287
288 /* we set the position of the window after the map event. setting it
289 before has no effect (with KWM) */
290 gtk_signal_connect( GTK_OBJECT(m_widget), "map",
291 GTK_SIGNAL_FUNC(gtk_dialog_map_callback), (gpointer) this );
292
293 /* the user resized the frame by dragging etc. */
294 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
295 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
296
297 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
298 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this );
299
300 return TRUE;
301 }
302
303 wxDialog::~wxDialog()
304 {
305 m_isBeingDeleted = TRUE;
306
307 wxTopLevelWindows.DeleteObject( this );
308
309 if (wxTheApp->GetTopWindow() == this)
310 {
311 wxTheApp->SetTopWindow( (wxWindow*) NULL );
312 }
313
314 if (wxTopLevelWindows.Number() == 0)
315 {
316 wxTheApp->ExitMainLoop();
317 }
318 }
319
320 void wxDialog::SetTitle( const wxString& title )
321 {
322 m_title = title;
323 if (m_title.IsNull()) m_title = _T("");
324 gtk_window_set_title( GTK_WINDOW(m_widget), m_title.mbc_str() );
325 }
326
327 wxString wxDialog::GetTitle() const
328 {
329 return (wxString&)m_title;
330 }
331
332 void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
333 {
334 if (Validate()) TransferDataFromWindow();
335 }
336
337 void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
338 {
339 if (IsModal())
340 {
341 EndModal(wxID_CANCEL);
342 }
343 else
344 {
345 SetReturnCode(wxID_CANCEL);
346 Show(FALSE);
347 }
348 }
349
350 void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
351 {
352 if (Validate() && TransferDataFromWindow())
353 {
354 if (IsModal())
355 {
356 EndModal(wxID_OK);
357 }
358 else
359 {
360 SetReturnCode(wxID_OK);
361 this->Show(FALSE);
362 }
363 }
364 }
365
366 void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
367 {
368 // yes
369 }
370
371 void wxDialog::OnCloseWindow(wxCloseEvent& event)
372 {
373 // We'll send a Cancel message by default,
374 // which may close the dialog.
375 // Check for looping if the Cancel event handler calls Close().
376
377 // Note that if a cancel button and handler aren't present in the dialog,
378 // nothing will happen when you close the dialog via the window manager, or
379 // via Close().
380 // We wouldn't want to destroy the dialog by default, since the dialog may have been
381 // created on the stack.
382 // However, this does mean that calling dialog->Close() won't delete the dialog
383 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
384 // sure to destroy the dialog.
385 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
386
387 static wxList s_closing;
388
389 if (s_closing.Member(this))
390 return; // no loops
391
392 s_closing.Append(this);
393
394 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
395 cancelEvent.SetEventObject( this );
396 GetEventHandler()->ProcessEvent(cancelEvent);
397 s_closing.DeleteObject(this);
398 }
399
400 bool wxDialog::Destroy()
401 {
402 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
403
404 return TRUE;
405 }
406
407 void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
408 {
409 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
410
411 #if wxUSE_CONSTRAINTS
412 if (GetAutoLayout())
413 {
414 Layout();
415 }
416 else
417 #endif // wxUSE_CONSTRAINTS
418 {
419 /* no child: go out ! */
420 if (!GetChildren().First()) return;
421
422 /* do we have exactly one child? */
423 wxWindow *child = (wxWindow *) NULL;
424 for(wxNode *node = GetChildren().First(); node; node = node->Next())
425 {
426 wxWindow *win = (wxWindow *)node->Data();
427 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
428 {
429 /* it's the second one: do nothing */
430 if (child) return;
431 child = win;
432 }
433 }
434
435 /* yes: set it's size to fill all the frame */
436 int client_x, client_y;
437 GetClientSize( &client_x, &client_y );
438 child->SetSize( 1, 1, client_x-2, client_y);
439 }
440 }
441
442 void wxDialog::DoSetSize( int x, int y, int width, int height, int sizeFlags )
443 {
444 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
445 wxASSERT_MSG( (m_wxwindow != NULL), _T("invalid dialog") );
446
447 if (m_resizing) return; /* I don't like recursions */
448 m_resizing = TRUE;
449
450 int old_x = m_x;
451 int old_y = m_y;
452 int old_width = m_width;
453 int old_height = m_height;
454
455 if ((sizeFlags & wxSIZE_ALLOW_MINUS_ONE) == 0)
456 {
457 if (x != -1) m_x = x;
458 if (y != -1) m_y = y;
459 if (width != -1) m_width = width;
460 if (height != -1) m_height = height;
461 }
462 else
463 {
464 m_x = x;
465 m_y = y;
466 m_width = width;
467 m_height = height;
468 }
469
470 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
471 {
472 if (width == -1) m_width = 80;
473 }
474
475 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
476 {
477 if (height == -1) m_height = 26;
478 }
479
480 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
481 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
482 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
483 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
484
485 if ((m_x != -1) || (m_y != -1))
486 {
487 if ((m_x != old_x) || (m_y != old_y))
488 {
489 /* we set the position here and when showing the dialog
490 for the first time in idle time */
491 gtk_widget_set_uposition( m_widget, m_x, m_y );
492 }
493 }
494
495 if ((m_width != old_width) || (m_height != old_height))
496 {
497 /* actual resizing is deferred to GtkOnSize in idle time and
498 when showing the dialog */
499 m_sizeSet = FALSE;
500 }
501
502 m_resizing = FALSE;
503 }
504
505 void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
506 {
507 // due to a bug in gtk, x,y are always 0
508 // m_x = x;
509 // m_y = y;
510
511 if ((m_height == height) && (m_width == width) && (m_sizeSet)) return;
512 if (!m_wxwindow) return;
513
514 m_width = width;
515 m_height = height;
516
517 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
518 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
519 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
520 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
521
522 /* we actually set the size of a frame here and no-where else */
523 gtk_widget_set_usize( m_widget, m_width, m_height );
524
525 m_sizeSet = TRUE;
526
527 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
528 event.SetEventObject( this );
529 GetEventHandler()->ProcessEvent( event );
530 }
531
532 void wxDialog::Centre( int direction )
533 {
534 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
535
536 int x = 0;
537 int y = 0;
538
539 if ((direction & wxHORIZONTAL) == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
540 if ((direction & wxVERTICAL) == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
541
542 Move( x, y );
543 }
544
545 void wxDialog::OnInternalIdle()
546 {
547 if (!m_sizeSet && GTK_WIDGET_REALIZED(m_wxwindow))
548 GtkOnSize( m_x, m_y, m_width, m_height );
549 }
550
551 bool wxDialog::Show( bool show )
552 {
553 if (!show && IsModal())
554 {
555 EndModal( wxID_CANCEL );
556 }
557
558 if (show && !m_sizeSet)
559 {
560 /* by calling GtkOnSize here, we don't have to call
561 either after showing the frame, which would entail
562 much ugly flicker nor from within the size_allocate
563 handler, because GTK 1.1.X forbids that. */
564
565 GtkOnSize( m_x, m_y, m_width, m_height );
566 }
567
568 bool ret = wxWindow::Show( show );
569
570 if (show) InitDialog();
571
572 return ret;
573 }
574
575 bool wxDialog::IsModal() const
576 {
577 return m_modalShowing;
578 }
579
580 void wxDialog::SetModal( bool WXUNUSED(flag) )
581 {
582 /*
583 if (flag)
584 m_windowStyle |= wxDIALOG_MODAL;
585 else
586 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
587 */
588 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
589 }
590
591 int wxDialog::ShowModal()
592 {
593 if (IsModal())
594 {
595 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
596 return GetReturnCode();
597 }
598
599 wxBusyCursorSuspender cs; // temporarily suppress the busy cursor
600
601 Show( TRUE );
602
603 m_modalShowing = TRUE;
604
605 gtk_grab_add( m_widget );
606 gtk_main();
607 gtk_grab_remove( m_widget );
608
609 return GetReturnCode();
610 }
611
612 void wxDialog::EndModal( int retCode )
613 {
614 SetReturnCode( retCode );
615
616 if (!IsModal())
617 {
618 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
619 return;
620 }
621
622 m_modalShowing = FALSE;
623
624 gtk_main_quit();
625
626 Show( FALSE );
627 }
628
629 void wxDialog::InitDialog()
630 {
631 wxWindow::InitDialog();
632 }
633
634 void wxDialog::SetIcon( const wxIcon &icon )
635 {
636 m_icon = icon;
637 if (!icon.Ok()) return;
638
639 if (!m_widget->window) return;
640
641 wxMask *mask = icon.GetMask();
642 GdkBitmap *bm = (GdkBitmap *) NULL;
643 if (mask) bm = mask->GetBitmap();
644
645 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
646 }