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