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