]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/frame.cpp
Fixed bug [ 657949 ] Segmentation fault in ~wxFileHistory
[wxWidgets.git] / src / gtk1 / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: frame.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ============================================================================
11 // declarations
12 // ============================================================================
13
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17
18 #ifdef __GNUG__
19 #pragma implementation "frame.h"
20 #endif
21
22 #include "wx/defs.h"
23
24 #include "wx/dialog.h"
25 #include "wx/control.h"
26 #include "wx/app.h"
27 #include "wx/menu.h"
28 #if wxUSE_TOOLBAR
29 #include "wx/toolbar.h"
30 #endif
31 #if wxUSE_STATUSBAR
32 #include "wx/statusbr.h"
33 #endif
34 #include "wx/dcclient.h"
35
36 #include <glib.h>
37 #include "wx/gtk/private.h"
38
39 #include <gdk/gdkkeysyms.h>
40 #include <gdk/gdkx.h>
41
42 #include "wx/gtk/win_gtk.h"
43
44 // ----------------------------------------------------------------------------
45 // constants
46 // ----------------------------------------------------------------------------
47
48 const int wxSTATUS_HEIGHT = 25;
49 const int wxPLACE_HOLDER = 0;
50
51 // ----------------------------------------------------------------------------
52 // idle system
53 // ----------------------------------------------------------------------------
54
55 extern void wxapp_install_idle_handler();
56 extern bool g_isIdle;
57 extern int g_openDialogs;
58
59 // ----------------------------------------------------------------------------
60 // event tables
61 // ----------------------------------------------------------------------------
62
63 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
64
65 // ============================================================================
66 // implementation
67 // ============================================================================
68
69 // ----------------------------------------------------------------------------
70 // GTK callbacks
71 // ----------------------------------------------------------------------------
72
73 #if wxUSE_MENUS_NATIVE
74
75 //-----------------------------------------------------------------------------
76 // "child_attached" of menu bar
77 //-----------------------------------------------------------------------------
78
79 static void gtk_menu_attached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
80 {
81 if (!win->m_hasVMT) return;
82
83 win->m_menuBarDetached = FALSE;
84 win->GtkUpdateSize();
85 }
86
87 //-----------------------------------------------------------------------------
88 // "child_detached" of menu bar
89 //-----------------------------------------------------------------------------
90
91 static void gtk_menu_detached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
92 {
93 if (g_isIdle)
94 wxapp_install_idle_handler();
95
96 if (!win->m_hasVMT) return;
97
98 // Raise the client area area
99 gdk_window_raise( win->m_wxwindow->window );
100
101 win->m_menuBarDetached = TRUE;
102 win->GtkUpdateSize();
103 }
104
105 #endif // wxUSE_MENUS_NATIVE
106
107 #if wxUSE_TOOLBAR
108 //-----------------------------------------------------------------------------
109 // "child_attached" of tool bar
110 //-----------------------------------------------------------------------------
111
112 static void gtk_toolbar_attached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
113 {
114 if (!win->m_hasVMT) return;
115
116 win->m_toolBarDetached = FALSE;
117 win->GtkUpdateSize();
118 }
119
120 //-----------------------------------------------------------------------------
121 // "child_detached" of tool bar
122 //-----------------------------------------------------------------------------
123
124 static void gtk_toolbar_detached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
125 {
126 if (g_isIdle)
127 wxapp_install_idle_handler();
128
129 if (!win->m_hasVMT) return;
130
131 // Raise the client area area
132 gdk_window_raise( win->m_wxwindow->window );
133
134 win->m_toolBarDetached = TRUE;
135 win->GtkUpdateSize();
136 }
137 #endif // wxUSE_TOOLBAR
138
139
140 // ----------------------------------------------------------------------------
141 // wxFrame itself
142 // ----------------------------------------------------------------------------
143
144 //-----------------------------------------------------------------------------
145 // InsertChild for wxFrame
146 //-----------------------------------------------------------------------------
147
148 /* Callback for wxFrame. This very strange beast has to be used because
149 * C++ has no virtual methods in a constructor. We have to emulate a
150 * virtual function here as wxWindows requires different ways to insert
151 * a child in container classes. */
152
153 static void wxInsertChildInFrame( wxFrame* parent, wxWindow* child )
154 {
155 wxASSERT( GTK_IS_WIDGET(child->m_widget) );
156
157 if (!parent->m_insertInClientArea)
158 {
159 // These are outside the client area
160 wxFrame* frame = (wxFrame*) parent;
161 gtk_pizza_put( GTK_PIZZA(frame->m_mainWidget),
162 GTK_WIDGET(child->m_widget),
163 child->m_x,
164 child->m_y,
165 child->m_width,
166 child->m_height );
167
168 #if wxUSE_TOOLBAR_NATIVE
169 // We connect to these events for recalculating the client area
170 // space when the toolbar is floating
171 if (wxIS_KIND_OF(child,wxToolBar))
172 {
173 wxToolBar *toolBar = (wxToolBar*) child;
174 if (toolBar->GetWindowStyle() & wxTB_DOCKABLE)
175 {
176 gtk_signal_connect( GTK_OBJECT(toolBar->m_widget), "child_attached",
177 GTK_SIGNAL_FUNC(gtk_toolbar_attached_callback), (gpointer)parent );
178
179 gtk_signal_connect( GTK_OBJECT(toolBar->m_widget), "child_detached",
180 GTK_SIGNAL_FUNC(gtk_toolbar_detached_callback), (gpointer)parent );
181 }
182 }
183 #endif // wxUSE_TOOLBAR
184 }
185 else
186 {
187 // These are inside the client area
188 gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow),
189 GTK_WIDGET(child->m_widget),
190 child->m_x,
191 child->m_y,
192 child->m_width,
193 child->m_height );
194 }
195
196 // Resize on OnInternalIdle
197 parent->GtkUpdateSize();
198 }
199
200 // ----------------------------------------------------------------------------
201 // wxFrame creation
202 // ----------------------------------------------------------------------------
203
204 void wxFrame::Init()
205 {
206 m_menuBarDetached = FALSE;
207 m_toolBarDetached = FALSE;
208 m_menuBarHeight = 2;
209 }
210
211 bool wxFrame::Create( wxWindow *parent,
212 wxWindowID id,
213 const wxString& title,
214 const wxPoint& pos,
215 const wxSize& sizeOrig,
216 long style,
217 const wxString &name )
218 {
219 bool rt = wxTopLevelWindow::Create(parent, id, title, pos, sizeOrig,
220 style, name);
221 m_insertCallback = (wxInsertChildFunction) wxInsertChildInFrame;
222
223 return rt;
224 }
225
226 wxFrame::~wxFrame()
227 {
228 m_isBeingDeleted = TRUE;
229 DeleteAllBars();
230 }
231
232 // ----------------------------------------------------------------------------
233 // overridden wxWindow methods
234 // ----------------------------------------------------------------------------
235
236 void wxFrame::DoGetClientSize( int *width, int *height ) const
237 {
238 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
239
240 wxTopLevelWindow::DoGetClientSize( width, height );
241
242 if (height)
243 {
244 #if wxUSE_MENUS_NATIVE
245 // menu bar
246 if (m_frameMenuBar)
247 {
248 if (!m_menuBarDetached)
249 (*height) -= m_menuBarHeight;
250 else
251 (*height) -= wxPLACE_HOLDER;
252 }
253 #endif // wxUSE_MENUS_NATIVE
254
255 #if wxUSE_STATUSBAR
256 // status bar
257 if (m_frameStatusBar && m_frameStatusBar->IsShown())
258 (*height) -= wxSTATUS_HEIGHT;
259 #endif // wxUSE_STATUSBAR
260
261 #if wxUSE_TOOLBAR
262 // tool bar
263 if (m_frameToolBar && m_frameToolBar->IsShown())
264 {
265 if (m_toolBarDetached)
266 {
267 *height -= wxPLACE_HOLDER;
268 }
269 else
270 {
271 int x, y;
272 m_frameToolBar->GetSize( &x, &y );
273 if ( m_frameToolBar->GetWindowStyle() & wxTB_VERTICAL )
274 {
275 *width -= x;
276 }
277 else
278 {
279 *height -= y;
280 }
281 }
282 }
283 #endif // wxUSE_TOOLBAR
284 }
285 }
286
287 void wxFrame::DoSetClientSize( int width, int height )
288 {
289 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
290
291 #if wxUSE_MENUS_NATIVE
292 // menu bar
293 if (m_frameMenuBar)
294 {
295 if (!m_menuBarDetached)
296 height += m_menuBarHeight;
297 else
298 height += wxPLACE_HOLDER;
299 }
300 #endif // wxUSE_MENUS_NATIVE
301
302 #if wxUSE_STATUSBAR
303 // status bar
304 if (m_frameStatusBar && m_frameStatusBar->IsShown()) height += wxSTATUS_HEIGHT;
305 #endif
306
307 #if wxUSE_TOOLBAR
308 // tool bar
309 if (m_frameToolBar && m_frameToolBar->IsShown())
310 {
311 if (m_toolBarDetached)
312 {
313 height += wxPLACE_HOLDER;
314 }
315 else
316 {
317 int x, y;
318 m_frameToolBar->GetSize( &x, &y );
319 if ( m_frameToolBar->GetWindowStyle() & wxTB_VERTICAL )
320 {
321 width += x;
322 }
323 else
324 {
325 height += y;
326 }
327 }
328 }
329 #endif
330
331 wxTopLevelWindow::DoSetClientSize( width, height );
332 }
333
334 void wxFrame::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y),
335 int width, int height )
336 {
337 // due to a bug in gtk, x,y are always 0
338 // m_x = x;
339 // m_y = y;
340
341 // avoid recursions
342 if (m_resizing) return;
343 m_resizing = TRUE;
344
345 // this shouldn't happen: wxFrame, wxMDIParentFrame and wxMDIChildFrame have m_wxwindow
346 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid frame") );
347
348 m_width = width;
349 m_height = height;
350
351 // space occupied by m_frameToolBar and m_frameMenuBar
352 int client_area_x_offset = 0,
353 client_area_y_offset = 0;
354
355 /* wxMDIChildFrame derives from wxFrame but it _is_ a wxWindow as it uses
356 wxWindow::Create to create it's GTK equivalent. m_mainWidget is only
357 set in wxFrame::Create so it is used to check what kind of frame we
358 have here. if m_mainWidget is NULL it is a wxMDIChildFrame and so we
359 skip the part which handles m_frameMenuBar, m_frameToolBar and (most
360 importantly) m_mainWidget */
361
362 int minWidth = GetMinWidth(),
363 minHeight = GetMinHeight(),
364 maxWidth = GetMaxWidth(),
365 maxHeight = GetMaxHeight();
366
367 if ((minWidth != -1) && (m_width < minWidth)) m_width = minWidth;
368 if ((minHeight != -1) && (m_height < minHeight)) m_height = minHeight;
369 if ((maxWidth != -1) && (m_width > maxWidth)) m_width = maxWidth;
370 if ((maxHeight != -1) && (m_height > maxHeight)) m_height = maxHeight;
371
372 if (m_mainWidget)
373 {
374 // set size hints
375 gint flag = 0; // GDK_HINT_POS;
376 if ((minWidth != -1) || (minHeight != -1)) flag |= GDK_HINT_MIN_SIZE;
377 if ((maxWidth != -1) || (maxHeight != -1)) flag |= GDK_HINT_MAX_SIZE;
378 GdkGeometry geom;
379 geom.min_width = minWidth;
380 geom.min_height = minHeight;
381 geom.max_width = maxWidth;
382 geom.max_height = maxHeight;
383 gtk_window_set_geometry_hints( GTK_WINDOW(m_widget),
384 (GtkWidget*) NULL,
385 &geom,
386 (GdkWindowHints) flag );
387
388 // I revert back to wxGTK's original behaviour. m_mainWidget holds
389 // the menubar, the toolbar and the client area, which is represented
390 // by m_wxwindow.
391 // This hurts in the eye, but I don't want to call SetSize()
392 // because I don't want to call any non-native functions here.
393
394 #if wxUSE_MENUS_NATIVE
395 if (m_frameMenuBar)
396 {
397 int xx = m_miniEdge;
398 int yy = m_miniEdge + m_miniTitle;
399 int ww = m_width - 2*m_miniEdge;
400 int hh = m_menuBarHeight;
401 if (m_menuBarDetached) hh = wxPLACE_HOLDER;
402 m_frameMenuBar->m_x = xx;
403 m_frameMenuBar->m_y = yy;
404 m_frameMenuBar->m_width = ww;
405 m_frameMenuBar->m_height = hh;
406 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
407 m_frameMenuBar->m_widget,
408 xx, yy, ww, hh );
409 client_area_y_offset += hh;
410 }
411 #endif // wxUSE_MENUS_NATIVE
412
413 #if wxUSE_TOOLBAR
414 if ((m_frameToolBar) && m_frameToolBar->IsShown() &&
415 (m_frameToolBar->m_widget->parent == m_mainWidget))
416 {
417 int xx = m_miniEdge;
418 int yy = m_miniEdge + m_miniTitle;
419 #if wxUSE_MENUS_NATIVE
420 if (m_frameMenuBar)
421 {
422 if (!m_menuBarDetached)
423 yy += m_menuBarHeight;
424 else
425 yy += wxPLACE_HOLDER;
426 }
427 #endif // wxUSE_MENUS_NATIVE
428
429 m_frameToolBar->m_x = xx;
430 m_frameToolBar->m_y = yy;
431
432 // don't change the toolbar's reported height/width
433 int ww, hh;
434 if ( m_frameToolBar->GetWindowStyle() & wxTB_VERTICAL )
435 {
436 ww = m_toolBarDetached ? wxPLACE_HOLDER
437 : m_frameToolBar->m_width;
438 hh = m_height - 2*m_miniEdge;
439
440 client_area_x_offset += ww;
441 }
442 else
443 {
444 ww = m_width - 2*m_miniEdge;
445 hh = m_toolBarDetached ? wxPLACE_HOLDER
446 : m_frameToolBar->m_height;
447
448 client_area_y_offset += hh;
449 }
450
451 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
452 m_frameToolBar->m_widget,
453 xx, yy, ww, hh );
454 }
455 #endif // wxUSE_TOOLBAR
456
457 int client_x = client_area_x_offset + m_miniEdge;
458 int client_y = client_area_y_offset + m_miniEdge + m_miniTitle;
459 int client_w = m_width - client_area_x_offset - 2*m_miniEdge;
460 int client_h = m_height - client_area_y_offset- 2*m_miniEdge - m_miniTitle;
461 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
462 m_wxwindow,
463 client_x, client_y, client_w, client_h );
464 }
465 else
466 {
467 // If there is no m_mainWidget between m_widget and m_wxwindow there
468 // is no need to set the size or position of m_wxwindow.
469 }
470
471 #if wxUSE_STATUSBAR
472 if (m_frameStatusBar && m_frameStatusBar->IsShown())
473 {
474 int xx = 0 + m_miniEdge;
475 int yy = m_height - wxSTATUS_HEIGHT - m_miniEdge - client_area_y_offset;
476 int ww = m_width - 2*m_miniEdge;
477 int hh = wxSTATUS_HEIGHT;
478 m_frameStatusBar->m_x = xx;
479 m_frameStatusBar->m_y = yy;
480 m_frameStatusBar->m_width = ww;
481 m_frameStatusBar->m_height = hh;
482 gtk_pizza_set_size( GTK_PIZZA(m_wxwindow),
483 m_frameStatusBar->m_widget,
484 xx, yy, ww, hh );
485 gtk_widget_draw( m_frameStatusBar->m_widget, (GdkRectangle*) NULL );
486 }
487 #endif // wxUSE_STATUSBAR
488
489 m_sizeSet = TRUE;
490
491 // send size event to frame
492 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
493 event.SetEventObject( this );
494 GetEventHandler()->ProcessEvent( event );
495
496 #if wxUSE_STATUSBAR
497 // send size event to status bar
498 if (m_frameStatusBar)
499 {
500 wxSizeEvent event2( wxSize(m_frameStatusBar->m_width,m_frameStatusBar->m_height), m_frameStatusBar->GetId() );
501 event2.SetEventObject( m_frameStatusBar );
502 m_frameStatusBar->GetEventHandler()->ProcessEvent( event2 );
503 }
504 #endif // wxUSE_STATUSBAR
505
506 m_resizing = FALSE;
507 }
508
509 void wxFrame::OnInternalIdle()
510 {
511 wxTopLevelWindow::OnInternalIdle();
512
513 #if wxUSE_MENUS_NATIVE
514 if (m_frameMenuBar) m_frameMenuBar->OnInternalIdle();
515 #endif // wxUSE_MENUS_NATIVE
516 #if wxUSE_TOOLBAR
517 if (m_frameToolBar) m_frameToolBar->OnInternalIdle();
518 #endif
519 #if wxUSE_STATUSBAR
520 if (m_frameStatusBar) m_frameStatusBar->OnInternalIdle();
521 #endif
522 }
523
524 // ----------------------------------------------------------------------------
525 // menu/tool/status bar stuff
526 // ----------------------------------------------------------------------------
527
528 #if wxUSE_MENUS_NATIVE
529
530 void wxFrame::DetachMenuBar()
531 {
532 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
533 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid frame") );
534
535 if ( m_frameMenuBar )
536 {
537 m_frameMenuBar->UnsetInvokingWindow( this );
538
539 if (m_frameMenuBar->GetWindowStyle() & wxMB_DOCKABLE)
540 {
541 gtk_signal_disconnect_by_func( GTK_OBJECT(m_frameMenuBar->m_widget),
542 GTK_SIGNAL_FUNC(gtk_menu_attached_callback), (gpointer)this );
543
544 gtk_signal_disconnect_by_func( GTK_OBJECT(m_frameMenuBar->m_widget),
545 GTK_SIGNAL_FUNC(gtk_menu_detached_callback), (gpointer)this );
546 }
547
548 gtk_widget_ref( m_frameMenuBar->m_widget );
549
550 gtk_container_remove( GTK_CONTAINER(m_mainWidget), m_frameMenuBar->m_widget );
551 }
552
553 wxFrameBase::DetachMenuBar();
554 }
555
556 void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
557 {
558 wxFrameBase::AttachMenuBar(menuBar);
559
560 if (m_frameMenuBar)
561 {
562 m_frameMenuBar->SetInvokingWindow( this );
563
564 m_frameMenuBar->SetParent(this);
565 gtk_pizza_put( GTK_PIZZA(m_mainWidget),
566 m_frameMenuBar->m_widget,
567 m_frameMenuBar->m_x,
568 m_frameMenuBar->m_y,
569 m_frameMenuBar->m_width,
570 m_frameMenuBar->m_height );
571
572 if (menuBar->GetWindowStyle() & wxMB_DOCKABLE)
573 {
574 gtk_signal_connect( GTK_OBJECT(menuBar->m_widget), "child_attached",
575 GTK_SIGNAL_FUNC(gtk_menu_attached_callback), (gpointer)this );
576
577 gtk_signal_connect( GTK_OBJECT(menuBar->m_widget), "child_detached",
578 GTK_SIGNAL_FUNC(gtk_menu_detached_callback), (gpointer)this );
579 }
580
581 m_frameMenuBar->Show( TRUE );
582
583 UpdateMenuBarSize();
584 }
585 else
586 {
587 m_menuBarHeight = 2;
588 GtkUpdateSize(); // resize window in OnInternalIdle
589 }
590 }
591
592 void wxFrame::UpdateMenuBarSize()
593 {
594 wxASSERT_MSG( m_frameMenuBar, _T("Updating non existant menubar?") );
595
596 GtkRequisition req;
597
598 req.width = 2;
599 req.height = 2;
600
601 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_frameMenuBar->m_widget) )->size_request )
602 (m_frameMenuBar->m_widget, &req );
603
604 m_menuBarHeight = req.height;
605
606 // resize window in OnInternalIdle
607
608 GtkUpdateSize();
609 }
610
611 #endif // wxUSE_MENUS_NATIVE
612
613 #if wxUSE_TOOLBAR
614
615 wxToolBar* wxFrame::CreateToolBar( long style, wxWindowID id, const wxString& name )
616 {
617 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
618
619 m_insertInClientArea = FALSE;
620
621 m_frameToolBar = wxFrameBase::CreateToolBar( style, id, name );
622
623 m_insertInClientArea = TRUE;
624
625 GtkUpdateSize();
626
627 return m_frameToolBar;
628 }
629
630 void wxFrame::SetToolBar(wxToolBar *toolbar)
631 {
632 bool hadTbar = m_frameToolBar != NULL;
633
634 wxFrameBase::SetToolBar(toolbar);
635
636 if ( m_frameToolBar )
637 {
638 // insert into toolbar area if not already there
639 if ((m_frameToolBar->m_widget->parent) &&
640 (m_frameToolBar->m_widget->parent != m_mainWidget))
641 {
642 GetChildren().DeleteObject( m_frameToolBar );
643
644 gtk_widget_reparent( m_frameToolBar->m_widget, m_mainWidget );
645 GtkUpdateSize();
646 }
647 }
648 else // toolbar unset
649 {
650 // still need to update size if it had been there before
651 if ( hadTbar )
652 {
653 GtkUpdateSize();
654 }
655 }
656 }
657
658 #endif // wxUSE_TOOLBAR
659
660 #if wxUSE_STATUSBAR
661
662 wxStatusBar* wxFrame::CreateStatusBar(int number,
663 long style,
664 wxWindowID id,
665 const wxString& name)
666 {
667 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
668
669 // because it will change when toolbar is added
670 GtkUpdateSize();
671
672 return wxFrameBase::CreateStatusBar( number, style, id, name );
673 }
674
675 void wxFrame::PositionStatusBar()
676 {
677 if ( !m_frameStatusBar )
678 return;
679
680 GtkUpdateSize();
681 }
682 #endif // wxUSE_STATUSBAR
683