wx/wxprec.h already includes wx/defs.h (with other minor cleaning).
[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 // ============================================================================
14 // declarations
15 // ============================================================================
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 #include "wx/frame.h"
22 #include "wx/dialog.h"
23 #include "wx/control.h"
24 #include "wx/app.h"
25 #include "wx/menu.h"
26 #if wxUSE_TOOLBAR
27 #include "wx/toolbar.h"
28 #endif
29 #if wxUSE_STATUSBAR
30 #include "wx/statusbr.h"
31 #endif
32 #include "wx/dcclient.h"
33
34 #include <glib.h>
35 #include "wx/gtk/private.h"
36
37 #include <gdk/gdkkeysyms.h>
38 #include <gdk/gdkx.h>
39
40 #include "wx/gtk/win_gtk.h"
41
42 // ----------------------------------------------------------------------------
43 // constants
44 // ----------------------------------------------------------------------------
45
46 const int wxSTATUS_HEIGHT = 25;
47 const int wxPLACE_HOLDER = 0;
48
49 // ----------------------------------------------------------------------------
50 // event tables
51 // ----------------------------------------------------------------------------
52
53 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
54
55 // ============================================================================
56 // implementation
57 // ============================================================================
58
59 // ----------------------------------------------------------------------------
60 // GTK callbacks
61 // ----------------------------------------------------------------------------
62
63 #if wxUSE_MENUS_NATIVE
64
65 //-----------------------------------------------------------------------------
66 // "child_attached" of menu bar
67 //-----------------------------------------------------------------------------
68
69 extern "C" {
70 static void gtk_menu_attached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
71 {
72 if (!win->m_hasVMT) return;
73
74 win->m_menuBarDetached = false;
75 win->GtkUpdateSize();
76 }
77 }
78
79 //-----------------------------------------------------------------------------
80 // "child_detached" of menu bar
81 //-----------------------------------------------------------------------------
82
83 extern "C" {
84 static void gtk_menu_detached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
85 {
86 if (g_isIdle)
87 wxapp_install_idle_handler();
88
89 if (!win->m_hasVMT) return;
90
91 // Raise the client area area
92 gdk_window_raise( win->m_wxwindow->window );
93
94 win->m_menuBarDetached = true;
95 win->GtkUpdateSize();
96 }
97 }
98
99 #endif // wxUSE_MENUS_NATIVE
100
101 #if wxUSE_TOOLBAR
102 //-----------------------------------------------------------------------------
103 // "child_attached" of tool bar
104 //-----------------------------------------------------------------------------
105
106 extern "C" {
107 static void gtk_toolbar_attached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
108 {
109 if (!win->m_hasVMT) return;
110
111 win->m_toolBarDetached = false;
112 win->GtkUpdateSize();
113 }
114 }
115
116 //-----------------------------------------------------------------------------
117 // "child_detached" of tool bar
118 //-----------------------------------------------------------------------------
119
120 extern "C" {
121 static void gtk_toolbar_detached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
122 {
123 if (g_isIdle)
124 wxapp_install_idle_handler();
125
126 if (!win->m_hasVMT) return;
127
128 // Raise the client area area
129 gdk_window_raise( win->m_wxwindow->window );
130
131 win->m_toolBarDetached = true;
132 win->GtkUpdateSize();
133 }
134 }
135 #endif // wxUSE_TOOLBAR
136
137
138 // ----------------------------------------------------------------------------
139 // wxFrame itself
140 // ----------------------------------------------------------------------------
141
142 //-----------------------------------------------------------------------------
143 // InsertChild for wxFrame
144 //-----------------------------------------------------------------------------
145
146 /* Callback for wxFrame. This very strange beast has to be used because
147 * C++ has no virtual methods in a constructor. We have to emulate a
148 * virtual function here as wxWidgets requires different ways to insert
149 * a child in container classes. */
150
151 static void wxInsertChildInFrame( wxFrame* parent, wxWindow* child )
152 {
153 wxASSERT( GTK_IS_WIDGET(child->m_widget) );
154
155 if (!parent->m_insertInClientArea)
156 {
157 // These are outside the client area
158 wxFrame* frame = (wxFrame*) parent;
159 gtk_pizza_put( GTK_PIZZA(frame->m_mainWidget),
160 GTK_WIDGET(child->m_widget),
161 child->m_x,
162 child->m_y,
163 child->m_width,
164 child->m_height );
165
166 #if wxUSE_TOOLBAR_NATIVE
167 // We connect to these events for recalculating the client area
168 // space when the toolbar is floating
169 if (wxIS_KIND_OF(child,wxToolBar))
170 {
171 wxToolBar *toolBar = (wxToolBar*) child;
172 if (toolBar->GetWindowStyle() & wxTB_DOCKABLE)
173 {
174 g_signal_connect (toolBar->m_widget, "child_attached",
175 G_CALLBACK (gtk_toolbar_attached_callback),
176 parent);
177 g_signal_connect (toolBar->m_widget, "child_detached",
178 G_CALLBACK (gtk_toolbar_detached_callback),
179 parent);
180 }
181 }
182 #endif // wxUSE_TOOLBAR
183 }
184 else
185 {
186 // These are inside the client area
187 gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow),
188 GTK_WIDGET(child->m_widget),
189 child->m_x,
190 child->m_y,
191 child->m_width,
192 child->m_height );
193 }
194
195 // Resize on OnInternalIdle
196 parent->GtkUpdateSize();
197 }
198
199 // ----------------------------------------------------------------------------
200 // wxFrame creation
201 // ----------------------------------------------------------------------------
202
203 void wxFrame::Init()
204 {
205 m_menuBarDetached = false;
206 m_toolBarDetached = false;
207 m_menuBarHeight = 2;
208 }
209
210 bool wxFrame::Create( wxWindow *parent,
211 wxWindowID id,
212 const wxString& title,
213 const wxPoint& pos,
214 const wxSize& sizeOrig,
215 long style,
216 const wxString &name )
217 {
218 bool rt = wxTopLevelWindow::Create(parent, id, title, pos, sizeOrig,
219 style, name);
220 m_insertCallback = (wxInsertChildFunction) wxInsertChildInFrame;
221
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) -= m_menuBarHeight;
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 += m_menuBarHeight;
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 = m_menuBarHeight;
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 += m_menuBarHeight;
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 if (GTK_WIDGET_DRAWABLE (m_frameStatusBar->m_widget))
485 {
486 gtk_widget_queue_draw (m_frameStatusBar->m_widget);
487 // FIXME: Do we really want to force an immediate redraw?
488 gdk_window_process_updates (m_frameStatusBar->m_widget->window, TRUE);
489 }
490 }
491 #endif // wxUSE_STATUSBAR
492
493 m_sizeSet = true;
494
495 // send size event to frame
496 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
497 event.SetEventObject( this );
498 GetEventHandler()->ProcessEvent( event );
499
500 #if wxUSE_STATUSBAR
501 // send size event to status bar
502 if (m_frameStatusBar)
503 {
504 wxSizeEvent event2( wxSize(m_frameStatusBar->m_width,m_frameStatusBar->m_height), m_frameStatusBar->GetId() );
505 event2.SetEventObject( m_frameStatusBar );
506 m_frameStatusBar->GetEventHandler()->ProcessEvent( event2 );
507 }
508 #endif // wxUSE_STATUSBAR
509
510 m_resizing = false;
511 }
512
513 void wxFrame::OnInternalIdle()
514 {
515 wxFrameBase::OnInternalIdle();
516
517 #if wxUSE_MENUS_NATIVE
518 if (m_frameMenuBar) m_frameMenuBar->OnInternalIdle();
519 #endif // wxUSE_MENUS_NATIVE
520 #if wxUSE_TOOLBAR
521 if (m_frameToolBar) m_frameToolBar->OnInternalIdle();
522 #endif
523 #if wxUSE_STATUSBAR
524 if (m_frameStatusBar)
525 {
526 m_frameStatusBar->OnInternalIdle();
527
528 // There may be controls in the status bar that
529 // need to be updated
530 for ( wxWindowList::compatibility_iterator node = m_frameStatusBar->GetChildren().GetFirst();
531 node;
532 node = node->GetNext() )
533 {
534 wxWindow *child = node->GetData();
535 child->OnInternalIdle();
536 }
537 }
538 #endif
539 }
540
541 // ----------------------------------------------------------------------------
542 // menu/tool/status bar stuff
543 // ----------------------------------------------------------------------------
544
545 #if wxUSE_MENUS_NATIVE
546
547 void wxFrame::DetachMenuBar()
548 {
549 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
550 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid frame") );
551
552 if ( m_frameMenuBar )
553 {
554 m_frameMenuBar->UnsetInvokingWindow( this );
555
556 if (m_frameMenuBar->GetWindowStyle() & wxMB_DOCKABLE)
557 {
558 g_signal_handlers_disconnect_by_func (m_frameMenuBar->m_widget,
559 (gpointer) gtk_menu_attached_callback,
560 this);
561
562 g_signal_handlers_disconnect_by_func (m_frameMenuBar->m_widget,
563 (gpointer) gtk_menu_detached_callback,
564 this);
565 }
566
567 gtk_widget_ref( m_frameMenuBar->m_widget );
568
569 gtk_container_remove( GTK_CONTAINER(m_mainWidget), m_frameMenuBar->m_widget );
570 }
571
572 wxFrameBase::DetachMenuBar();
573 }
574
575 void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
576 {
577 wxFrameBase::AttachMenuBar(menuBar);
578
579 if (m_frameMenuBar)
580 {
581 m_frameMenuBar->SetInvokingWindow( this );
582
583 m_frameMenuBar->SetParent(this);
584 gtk_pizza_put( GTK_PIZZA(m_mainWidget),
585 m_frameMenuBar->m_widget,
586 m_frameMenuBar->m_x,
587 m_frameMenuBar->m_y,
588 m_frameMenuBar->m_width,
589 m_frameMenuBar->m_height );
590
591 if (menuBar->GetWindowStyle() & wxMB_DOCKABLE)
592 {
593 g_signal_connect (menuBar->m_widget, "child_attached",
594 G_CALLBACK (gtk_menu_attached_callback),
595 this);
596 g_signal_connect (menuBar->m_widget, "child_detached",
597 G_CALLBACK (gtk_menu_detached_callback),
598 this);
599 }
600
601 gtk_widget_show( m_frameMenuBar->m_widget );
602
603 UpdateMenuBarSize();
604 }
605 else
606 {
607 m_menuBarHeight = 2;
608 GtkUpdateSize(); // resize window in OnInternalIdle
609 }
610 }
611
612 void wxFrame::UpdateMenuBarSize()
613 {
614 GtkRequisition req;
615
616 req.width = 2;
617 req.height = 2;
618
619 // this is called after Remove with a NULL m_frameMenuBar
620 if ( m_frameMenuBar )
621 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_frameMenuBar->m_widget) )->size_request )
622 (m_frameMenuBar->m_widget, &req );
623
624 m_menuBarHeight = req.height;
625
626 // resize window in OnInternalIdle
627
628 GtkUpdateSize();
629 }
630
631 #endif // wxUSE_MENUS_NATIVE
632
633 #if wxUSE_TOOLBAR
634
635 wxToolBar* wxFrame::CreateToolBar( long style, wxWindowID id, const wxString& name )
636 {
637 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
638
639 m_insertInClientArea = false;
640
641 m_frameToolBar = wxFrameBase::CreateToolBar( style, id, name );
642
643 m_insertInClientArea = true;
644
645 GtkUpdateSize();
646
647 return m_frameToolBar;
648 }
649
650 void wxFrame::SetToolBar(wxToolBar *toolbar)
651 {
652 bool hadTbar = m_frameToolBar != NULL;
653
654 wxFrameBase::SetToolBar(toolbar);
655
656 if ( m_frameToolBar )
657 {
658 // insert into toolbar area if not already there
659 if ((m_frameToolBar->m_widget->parent) &&
660 (m_frameToolBar->m_widget->parent != m_mainWidget))
661 {
662 GetChildren().DeleteObject( m_frameToolBar );
663
664 gtk_widget_reparent( m_frameToolBar->m_widget, m_mainWidget );
665 GtkUpdateSize();
666 }
667 }
668 else // toolbar unset
669 {
670 // still need to update size if it had been there before
671 if ( hadTbar )
672 {
673 GtkUpdateSize();
674 }
675 }
676 }
677
678 #endif // wxUSE_TOOLBAR
679
680 #if wxUSE_STATUSBAR
681
682 wxStatusBar* wxFrame::CreateStatusBar(int number,
683 long style,
684 wxWindowID id,
685 const wxString& name)
686 {
687 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
688
689 // because it will change when toolbar is added
690 GtkUpdateSize();
691
692 return wxFrameBase::CreateStatusBar( number, style, id, name );
693 }
694
695 void wxFrame::SetStatusBar(wxStatusBar *statbar)
696 {
697 bool hadStatBar = m_frameStatusBar != NULL;
698
699 wxFrameBase::SetStatusBar(statbar);
700
701 if (hadStatBar && !m_frameStatusBar)
702 GtkUpdateSize();
703 }
704
705 void wxFrame::PositionStatusBar()
706 {
707 if ( !m_frameStatusBar )
708 return;
709
710 GtkUpdateSize();
711 }
712 #endif // wxUSE_STATUSBAR