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