]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/frame.cpp
The alignment controls are now left-aligned if the floating controls are not shown.
[wxWidgets.git] / src / gtk1 / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/frame.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/frame.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/app.h"
24 #include "wx/dcclient.h"
25 #include "wx/menu.h"
26 #include "wx/dialog.h"
27 #include "wx/control.h"
28 #include "wx/toolbar.h"
29 #include "wx/statusbr.h"
30 #endif // WX_PRECOMP
31
32 #include <glib.h>
33 #include "wx/gtk1/private.h"
34
35 #include <gdk/gdkkeysyms.h>
36 #include <gdk/gdkx.h>
37
38 #include "wx/gtk1/win_gtk.h"
39
40 // ----------------------------------------------------------------------------
41 // constants
42 // ----------------------------------------------------------------------------
43
44 const int wxSTATUS_HEIGHT = 25;
45 const int wxPLACE_HOLDER = 0;
46
47 // ----------------------------------------------------------------------------
48 // idle system
49 // ----------------------------------------------------------------------------
50
51 extern void wxapp_install_idle_handler();
52 extern bool g_isIdle;
53
54 // ----------------------------------------------------------------------------
55 // event tables
56 // ----------------------------------------------------------------------------
57
58 // ============================================================================
59 // implementation
60 // ============================================================================
61
62 // ----------------------------------------------------------------------------
63 // GTK callbacks
64 // ----------------------------------------------------------------------------
65
66 #if wxUSE_MENUS_NATIVE
67
68 //-----------------------------------------------------------------------------
69 // "child_attached" of menu bar
70 //-----------------------------------------------------------------------------
71
72 extern "C" {
73 static void gtk_menu_attached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
74 {
75 if (!win->m_hasVMT) return;
76
77 win->m_menuBarDetached = false;
78 win->GtkUpdateSize();
79 }
80 }
81
82 //-----------------------------------------------------------------------------
83 // "child_detached" of menu bar
84 //-----------------------------------------------------------------------------
85
86 extern "C" {
87 static void gtk_menu_detached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
88 {
89 if (g_isIdle)
90 wxapp_install_idle_handler();
91
92 if (!win->m_hasVMT) return;
93
94 // Raise the client area area
95 gdk_window_raise( win->m_wxwindow->window );
96
97 win->m_menuBarDetached = true;
98 win->GtkUpdateSize();
99 }
100 }
101
102 #endif // wxUSE_MENUS_NATIVE
103
104 #if wxUSE_TOOLBAR
105 //-----------------------------------------------------------------------------
106 // "child_attached" of tool bar
107 //-----------------------------------------------------------------------------
108
109 extern "C" {
110 static void gtk_toolbar_attached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
111 {
112 if (!win->m_hasVMT) return;
113
114 win->m_toolBarDetached = false;
115 win->GtkUpdateSize();
116 }
117 }
118
119 //-----------------------------------------------------------------------------
120 // "child_detached" of tool bar
121 //-----------------------------------------------------------------------------
122
123 extern "C" {
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 }
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 wxWidgets 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 m_menuBarHeight = 2;
210 }
211
212 bool wxFrame::Create( wxWindow *parent,
213 wxWindowID id,
214 const wxString& title,
215 const wxPoint& pos,
216 const wxSize& sizeOrig,
217 long style,
218 const wxString &name )
219 {
220 bool rt = wxTopLevelWindow::Create(parent, id, title, pos, sizeOrig,
221 style, name);
222 m_insertCallback = (wxInsertChildFunction) wxInsertChildInFrame;
223
224 return rt;
225 }
226
227 wxFrame::~wxFrame()
228 {
229 SendDestroyEvent();
230
231 DeleteAllBars();
232 }
233
234 // ----------------------------------------------------------------------------
235 // overridden wxWindow methods
236 // ----------------------------------------------------------------------------
237
238 void wxFrame::DoGetClientSize( int *width, int *height ) const
239 {
240 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
241
242 wxTopLevelWindow::DoGetClientSize( width, height );
243
244 if (height)
245 {
246 #if wxUSE_MENUS_NATIVE
247 // menu bar
248 if (m_frameMenuBar)
249 {
250 if (!m_menuBarDetached)
251 (*height) -= m_menuBarHeight;
252 else
253 (*height) -= wxPLACE_HOLDER;
254 }
255 #endif // wxUSE_MENUS_NATIVE
256
257 #if wxUSE_STATUSBAR
258 // status bar
259 if (m_frameStatusBar && m_frameStatusBar->IsShown())
260 (*height) -= wxSTATUS_HEIGHT;
261 #endif // wxUSE_STATUSBAR
262
263 #if wxUSE_TOOLBAR
264 // tool bar
265 if (m_frameToolBar && m_frameToolBar->IsShown())
266 {
267 if (m_toolBarDetached)
268 {
269 *height -= wxPLACE_HOLDER;
270 }
271 else
272 {
273 int x, y;
274 m_frameToolBar->GetSize( &x, &y );
275 if ( m_frameToolBar->GetWindowStyle() & wxTB_VERTICAL )
276 {
277 *width -= x;
278 }
279 else
280 {
281 *height -= y;
282 }
283 }
284 }
285 #endif // wxUSE_TOOLBAR
286 }
287 }
288
289 void wxFrame::DoSetClientSize( int width, int height )
290 {
291 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
292
293 #if wxUSE_MENUS_NATIVE
294 // menu bar
295 if (m_frameMenuBar)
296 {
297 if (!m_menuBarDetached)
298 height += m_menuBarHeight;
299 else
300 height += wxPLACE_HOLDER;
301 }
302 #endif // wxUSE_MENUS_NATIVE
303
304 #if wxUSE_STATUSBAR
305 // status bar
306 if (m_frameStatusBar && m_frameStatusBar->IsShown()) height += wxSTATUS_HEIGHT;
307 #endif
308
309 #if wxUSE_TOOLBAR
310 // tool bar
311 if (m_frameToolBar && m_frameToolBar->IsShown())
312 {
313 if (m_toolBarDetached)
314 {
315 height += wxPLACE_HOLDER;
316 }
317 else
318 {
319 int x, y;
320 m_frameToolBar->GetSize( &x, &y );
321 if ( m_frameToolBar->GetWindowStyle() & wxTB_VERTICAL )
322 {
323 width += x;
324 }
325 else
326 {
327 height += y;
328 }
329 }
330 }
331 #endif
332
333 wxTopLevelWindow::DoSetClientSize( width, height );
334 }
335
336 void wxFrame::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y),
337 int width, int height )
338 {
339 // due to a bug in gtk, x,y are always 0
340 // m_x = x;
341 // m_y = y;
342
343 // avoid recursions
344 if (m_resizing) return;
345 m_resizing = true;
346
347 // this shouldn't happen: wxFrame, wxMDIParentFrame and wxMDIChildFrame have m_wxwindow
348 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid frame") );
349
350 m_width = width;
351 m_height = height;
352
353 // space occupied by m_frameToolBar and m_frameMenuBar
354 int client_area_x_offset = 0,
355 client_area_y_offset = 0;
356
357 /* wxMDIChildFrame derives from wxFrame but it _is_ a wxWindow as it uses
358 wxWindow::Create to create it's GTK equivalent. m_mainWidget is only
359 set in wxFrame::Create so it is used to check what kind of frame we
360 have here. if m_mainWidget is NULL it is a wxMDIChildFrame and so we
361 skip the part which handles m_frameMenuBar, m_frameToolBar and (most
362 importantly) m_mainWidget */
363
364 int minWidth = GetMinWidth(),
365 minHeight = GetMinHeight(),
366 maxWidth = GetMaxWidth(),
367 maxHeight = GetMaxHeight();
368
369 if ((minWidth != -1) && (m_width < minWidth)) m_width = minWidth;
370 if ((minHeight != -1) && (m_height < minHeight)) m_height = minHeight;
371 if ((maxWidth != -1) && (m_width > maxWidth)) m_width = maxWidth;
372 if ((maxHeight != -1) && (m_height > maxHeight)) m_height = maxHeight;
373
374 if (m_mainWidget)
375 {
376 // set size hints
377 gint flag = 0; // GDK_HINT_POS;
378 if ((minWidth != -1) || (minHeight != -1)) flag |= GDK_HINT_MIN_SIZE;
379 if ((maxWidth != -1) || (maxHeight != -1)) flag |= GDK_HINT_MAX_SIZE;
380 GdkGeometry geom;
381 geom.min_width = minWidth;
382 geom.min_height = minHeight;
383 geom.max_width = maxWidth;
384 geom.max_height = maxHeight;
385 gtk_window_set_geometry_hints( GTK_WINDOW(m_widget),
386 NULL,
387 &geom,
388 (GdkWindowHints) flag );
389
390 // I revert back to wxGTK's original behaviour. m_mainWidget holds
391 // the menubar, the toolbar and the client area, which is represented
392 // by m_wxwindow.
393 // This hurts in the eye, but I don't want to call SetSize()
394 // because I don't want to call any non-native functions here.
395
396 #if wxUSE_MENUS_NATIVE
397 if (m_frameMenuBar)
398 {
399 int xx = m_miniEdge;
400 int yy = m_miniEdge + m_miniTitle;
401 int ww = m_width - 2*m_miniEdge;
402 int hh = m_menuBarHeight;
403 if (m_menuBarDetached) hh = wxPLACE_HOLDER;
404 m_frameMenuBar->m_x = xx;
405 m_frameMenuBar->m_y = yy;
406 m_frameMenuBar->m_width = ww;
407 m_frameMenuBar->m_height = hh;
408 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
409 m_frameMenuBar->m_widget,
410 xx, yy, ww, hh );
411 client_area_y_offset += hh;
412 }
413 #endif // wxUSE_MENUS_NATIVE
414
415 #if wxUSE_TOOLBAR
416 if ((m_frameToolBar) && m_frameToolBar->IsShown() &&
417 (m_frameToolBar->m_widget->parent == m_mainWidget))
418 {
419 int xx = m_miniEdge;
420 int yy = m_miniEdge + m_miniTitle;
421 #if wxUSE_MENUS_NATIVE
422 if (m_frameMenuBar)
423 {
424 if (!m_menuBarDetached)
425 yy += m_menuBarHeight;
426 else
427 yy += wxPLACE_HOLDER;
428 }
429 #endif // wxUSE_MENUS_NATIVE
430
431 m_frameToolBar->m_x = xx;
432 m_frameToolBar->m_y = yy;
433
434 // don't change the toolbar's reported height/width
435 int ww, hh;
436 if ( m_frameToolBar->GetWindowStyle() & wxTB_VERTICAL )
437 {
438 ww = m_toolBarDetached ? wxPLACE_HOLDER
439 : m_frameToolBar->m_width;
440 hh = m_height - 2*m_miniEdge;
441
442 client_area_x_offset += ww;
443 }
444 else
445 {
446 ww = m_width - 2*m_miniEdge;
447 hh = m_toolBarDetached ? wxPLACE_HOLDER
448 : m_frameToolBar->m_height;
449
450 client_area_y_offset += hh;
451 }
452
453 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
454 m_frameToolBar->m_widget,
455 xx, yy, ww, hh );
456 }
457 #endif // wxUSE_TOOLBAR
458
459 int client_x = client_area_x_offset + m_miniEdge;
460 int client_y = client_area_y_offset + m_miniEdge + m_miniTitle;
461 int client_w = m_width - client_area_x_offset - 2*m_miniEdge;
462 int client_h = m_height - client_area_y_offset- 2*m_miniEdge - m_miniTitle;
463 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
464 m_wxwindow,
465 client_x, client_y, client_w, client_h );
466 }
467 else
468 {
469 // If there is no m_mainWidget between m_widget and m_wxwindow there
470 // is no need to set the size or position of m_wxwindow.
471 }
472
473 #if wxUSE_STATUSBAR
474 if (m_frameStatusBar && m_frameStatusBar->IsShown())
475 {
476 int xx = 0 + m_miniEdge;
477 int yy = m_height - wxSTATUS_HEIGHT - m_miniEdge - client_area_y_offset;
478 int ww = m_width - 2*m_miniEdge;
479 int hh = wxSTATUS_HEIGHT;
480 m_frameStatusBar->m_x = xx;
481 m_frameStatusBar->m_y = yy;
482 m_frameStatusBar->m_width = ww;
483 m_frameStatusBar->m_height = hh;
484 gtk_pizza_set_size( GTK_PIZZA(m_wxwindow),
485 m_frameStatusBar->m_widget,
486 xx, yy, ww, hh );
487 gtk_widget_draw( m_frameStatusBar->m_widget, NULL );
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 HandleWindowEvent( 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->HandleWindowEvent( 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->Attach( this );
553
554 if (m_frameMenuBar->GetWindowStyle() & wxMB_DOCKABLE)
555 {
556 gtk_signal_disconnect_by_func( GTK_OBJECT(m_frameMenuBar->m_widget),
557 GTK_SIGNAL_FUNC(gtk_menu_attached_callback), (gpointer)this );
558
559 gtk_signal_disconnect_by_func( GTK_OBJECT(m_frameMenuBar->m_widget),
560 GTK_SIGNAL_FUNC(gtk_menu_detached_callback), (gpointer)this );
561 }
562
563 gtk_widget_ref( m_frameMenuBar->m_widget );
564
565 gtk_container_remove( GTK_CONTAINER(m_mainWidget), m_frameMenuBar->m_widget );
566 }
567
568 wxFrameBase::DetachMenuBar();
569 }
570
571 void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
572 {
573 wxFrameBase::AttachMenuBar(menuBar);
574
575 if (m_frameMenuBar)
576 {
577 m_frameMenuBar->SetParent(this);
578 gtk_pizza_put( GTK_PIZZA(m_mainWidget),
579 m_frameMenuBar->m_widget,
580 m_frameMenuBar->m_x,
581 m_frameMenuBar->m_y,
582 m_frameMenuBar->m_width,
583 m_frameMenuBar->m_height );
584
585 if (menuBar->GetWindowStyle() & wxMB_DOCKABLE)
586 {
587 gtk_signal_connect( GTK_OBJECT(menuBar->m_widget), "child_attached",
588 GTK_SIGNAL_FUNC(gtk_menu_attached_callback), (gpointer)this );
589
590 gtk_signal_connect( GTK_OBJECT(menuBar->m_widget), "child_detached",
591 GTK_SIGNAL_FUNC(gtk_menu_detached_callback), (gpointer)this );
592 }
593
594 gtk_widget_show( m_frameMenuBar->m_widget );
595
596 UpdateMenuBarSize();
597 }
598 else
599 {
600 m_menuBarHeight = 2;
601 GtkUpdateSize(); // resize window in OnInternalIdle
602 }
603 }
604
605 void wxFrame::UpdateMenuBarSize()
606 {
607 GtkRequisition req;
608
609 req.width = 2;
610 req.height = 2;
611
612 // this is called after Remove with a NULL m_frameMenuBar
613 if ( m_frameMenuBar )
614 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_frameMenuBar->m_widget) )->size_request )
615 (m_frameMenuBar->m_widget, &req );
616
617 m_menuBarHeight = req.height;
618
619 // resize window in OnInternalIdle
620
621 GtkUpdateSize();
622 }
623
624 #endif // wxUSE_MENUS_NATIVE
625
626 #if wxUSE_TOOLBAR
627
628 wxToolBar* wxFrame::CreateToolBar( long style, wxWindowID id, const wxString& name )
629 {
630 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
631
632 m_insertInClientArea = false;
633
634 m_frameToolBar = wxFrameBase::CreateToolBar( style, id, name );
635
636 m_insertInClientArea = true;
637
638 GtkUpdateSize();
639
640 return m_frameToolBar;
641 }
642
643 void wxFrame::SetToolBar(wxToolBar *toolbar)
644 {
645 bool hadTbar = m_frameToolBar != NULL;
646
647 wxFrameBase::SetToolBar(toolbar);
648
649 if ( m_frameToolBar )
650 {
651 // insert into toolbar area if not already there
652 if ((m_frameToolBar->m_widget->parent) &&
653 (m_frameToolBar->m_widget->parent != m_mainWidget))
654 {
655 GetChildren().DeleteObject( m_frameToolBar );
656
657 gtk_widget_reparent( m_frameToolBar->m_widget, m_mainWidget );
658 GtkUpdateSize();
659 }
660 }
661 else // toolbar unset
662 {
663 // still need to update size if it had been there before
664 if ( hadTbar )
665 {
666 GtkUpdateSize();
667 }
668 }
669 }
670
671 #endif // wxUSE_TOOLBAR
672
673 #if wxUSE_STATUSBAR
674
675 wxStatusBar* wxFrame::CreateStatusBar(int number,
676 long style,
677 wxWindowID id,
678 const wxString& name)
679 {
680 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
681
682 // because it will change when toolbar is added
683 GtkUpdateSize();
684
685 return wxFrameBase::CreateStatusBar( number, style, id, name );
686 }
687
688 void wxFrame::SetStatusBar(wxStatusBar *statbar)
689 {
690 bool hadStatBar = m_frameStatusBar != NULL;
691
692 wxFrameBase::SetStatusBar(statbar);
693
694 if (hadStatBar && !m_frameStatusBar)
695 GtkUpdateSize();
696 }
697
698 void wxFrame::PositionStatusBar()
699 {
700 if ( !m_frameStatusBar )
701 return;
702
703 GtkUpdateSize();
704 }
705 #endif // wxUSE_STATUSBAR