Return wxFONTFAMILY_DEFAULT, not UNKNOWN, from wxFont::GetFamily().
[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
23 #ifndef WX_PRECOMP
24 #include "wx/app.h"
25 #include "wx/dcclient.h"
26 #include "wx/menu.h"
27 #include "wx/dialog.h"
28 #include "wx/control.h"
29 #include "wx/toolbar.h"
30 #include "wx/statusbr.h"
31 #endif // WX_PRECOMP
32
33 #include <glib.h>
34 #include "wx/gtk1/private.h"
35
36 #include <gdk/gdkkeysyms.h>
37 #include <gdk/gdkx.h>
38
39 #include "wx/gtk1/win_gtk.h"
40
41 // ----------------------------------------------------------------------------
42 // constants
43 // ----------------------------------------------------------------------------
44
45 const int wxSTATUS_HEIGHT = 25;
46 const int wxPLACE_HOLDER = 0;
47
48 // ----------------------------------------------------------------------------
49 // idle system
50 // ----------------------------------------------------------------------------
51
52 extern void wxapp_install_idle_handler();
53 extern bool g_isIdle;
54
55 // ----------------------------------------------------------------------------
56 // event tables
57 // ----------------------------------------------------------------------------
58
59 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
60
61 // ============================================================================
62 // implementation
63 // ============================================================================
64
65 // ----------------------------------------------------------------------------
66 // GTK callbacks
67 // ----------------------------------------------------------------------------
68
69 #if wxUSE_MENUS_NATIVE
70
71 //-----------------------------------------------------------------------------
72 // "child_attached" of menu bar
73 //-----------------------------------------------------------------------------
74
75 extern "C" {
76 static void gtk_menu_attached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
77 {
78 if (!win->m_hasVMT) return;
79
80 win->m_menuBarDetached = false;
81 win->GtkUpdateSize();
82 }
83 }
84
85 //-----------------------------------------------------------------------------
86 // "child_detached" of menu bar
87 //-----------------------------------------------------------------------------
88
89 extern "C" {
90 static void gtk_menu_detached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
91 {
92 if (g_isIdle)
93 wxapp_install_idle_handler();
94
95 if (!win->m_hasVMT) return;
96
97 // Raise the client area area
98 gdk_window_raise( win->m_wxwindow->window );
99
100 win->m_menuBarDetached = true;
101 win->GtkUpdateSize();
102 }
103 }
104
105 #endif // wxUSE_MENUS_NATIVE
106
107 #if wxUSE_TOOLBAR
108 //-----------------------------------------------------------------------------
109 // "child_attached" of tool bar
110 //-----------------------------------------------------------------------------
111
112 extern "C" {
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 //-----------------------------------------------------------------------------
123 // "child_detached" of tool bar
124 //-----------------------------------------------------------------------------
125
126 extern "C" {
127 static void gtk_toolbar_detached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
128 {
129 if (g_isIdle)
130 wxapp_install_idle_handler();
131
132 if (!win->m_hasVMT) return;
133
134 // Raise the client area area
135 gdk_window_raise( win->m_wxwindow->window );
136
137 win->m_toolBarDetached = true;
138 win->GtkUpdateSize();
139 }
140 }
141 #endif // wxUSE_TOOLBAR
142
143
144 // ----------------------------------------------------------------------------
145 // wxFrame itself
146 // ----------------------------------------------------------------------------
147
148 //-----------------------------------------------------------------------------
149 // InsertChild for wxFrame
150 //-----------------------------------------------------------------------------
151
152 /* Callback for wxFrame. This very strange beast has to be used because
153 * C++ has no virtual methods in a constructor. We have to emulate a
154 * virtual function here as wxWidgets requires different ways to insert
155 * a child in container classes. */
156
157 static void wxInsertChildInFrame( wxFrame* parent, wxWindow* child )
158 {
159 wxASSERT( GTK_IS_WIDGET(child->m_widget) );
160
161 if (!parent->m_insertInClientArea)
162 {
163 // These are outside the client area
164 wxFrame* frame = (wxFrame*) parent;
165 gtk_pizza_put( GTK_PIZZA(frame->m_mainWidget),
166 GTK_WIDGET(child->m_widget),
167 child->m_x,
168 child->m_y,
169 child->m_width,
170 child->m_height );
171
172 #if wxUSE_TOOLBAR_NATIVE
173 // We connect to these events for recalculating the client area
174 // space when the toolbar is floating
175 if (wxIS_KIND_OF(child,wxToolBar))
176 {
177 wxToolBar *toolBar = (wxToolBar*) child;
178 if (toolBar->GetWindowStyle() & wxTB_DOCKABLE)
179 {
180 gtk_signal_connect( GTK_OBJECT(toolBar->m_widget), "child_attached",
181 GTK_SIGNAL_FUNC(gtk_toolbar_attached_callback), (gpointer)parent );
182
183 gtk_signal_connect( GTK_OBJECT(toolBar->m_widget), "child_detached",
184 GTK_SIGNAL_FUNC(gtk_toolbar_detached_callback), (gpointer)parent );
185 }
186 }
187 #endif // wxUSE_TOOLBAR
188 }
189 else
190 {
191 // These are inside the client area
192 gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow),
193 GTK_WIDGET(child->m_widget),
194 child->m_x,
195 child->m_y,
196 child->m_width,
197 child->m_height );
198 }
199
200 // Resize on OnInternalIdle
201 parent->GtkUpdateSize();
202 }
203
204 // ----------------------------------------------------------------------------
205 // wxFrame creation
206 // ----------------------------------------------------------------------------
207
208 void wxFrame::Init()
209 {
210 m_menuBarDetached = false;
211 m_toolBarDetached = false;
212 m_menuBarHeight = 2;
213 }
214
215 bool wxFrame::Create( wxWindow *parent,
216 wxWindowID id,
217 const wxString& title,
218 const wxPoint& pos,
219 const wxSize& sizeOrig,
220 long style,
221 const wxString &name )
222 {
223 bool rt = wxTopLevelWindow::Create(parent, id, title, pos, sizeOrig,
224 style, name);
225 m_insertCallback = (wxInsertChildFunction) wxInsertChildInFrame;
226
227 return rt;
228 }
229
230 wxFrame::~wxFrame()
231 {
232 SendDestroyEvent();
233
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 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, 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 HandleWindowEvent( 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->HandleWindowEvent( 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->Attach( 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->SetParent(this);
581 gtk_pizza_put( GTK_PIZZA(m_mainWidget),
582 m_frameMenuBar->m_widget,
583 m_frameMenuBar->m_x,
584 m_frameMenuBar->m_y,
585 m_frameMenuBar->m_width,
586 m_frameMenuBar->m_height );
587
588 if (menuBar->GetWindowStyle() & wxMB_DOCKABLE)
589 {
590 gtk_signal_connect( GTK_OBJECT(menuBar->m_widget), "child_attached",
591 GTK_SIGNAL_FUNC(gtk_menu_attached_callback), (gpointer)this );
592
593 gtk_signal_connect( GTK_OBJECT(menuBar->m_widget), "child_detached",
594 GTK_SIGNAL_FUNC(gtk_menu_detached_callback), (gpointer)this );
595 }
596
597 gtk_widget_show( m_frameMenuBar->m_widget );
598
599 UpdateMenuBarSize();
600 }
601 else
602 {
603 m_menuBarHeight = 2;
604 GtkUpdateSize(); // resize window in OnInternalIdle
605 }
606 }
607
608 void wxFrame::UpdateMenuBarSize()
609 {
610 GtkRequisition req;
611
612 req.width = 2;
613 req.height = 2;
614
615 // this is called after Remove with a NULL m_frameMenuBar
616 if ( m_frameMenuBar )
617 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_frameMenuBar->m_widget) )->size_request )
618 (m_frameMenuBar->m_widget, &req );
619
620 m_menuBarHeight = req.height;
621
622 // resize window in OnInternalIdle
623
624 GtkUpdateSize();
625 }
626
627 #endif // wxUSE_MENUS_NATIVE
628
629 #if wxUSE_TOOLBAR
630
631 wxToolBar* wxFrame::CreateToolBar( long style, wxWindowID id, const wxString& name )
632 {
633 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
634
635 m_insertInClientArea = false;
636
637 m_frameToolBar = wxFrameBase::CreateToolBar( style, id, name );
638
639 m_insertInClientArea = true;
640
641 GtkUpdateSize();
642
643 return m_frameToolBar;
644 }
645
646 void wxFrame::SetToolBar(wxToolBar *toolbar)
647 {
648 bool hadTbar = m_frameToolBar != NULL;
649
650 wxFrameBase::SetToolBar(toolbar);
651
652 if ( m_frameToolBar )
653 {
654 // insert into toolbar area if not already there
655 if ((m_frameToolBar->m_widget->parent) &&
656 (m_frameToolBar->m_widget->parent != m_mainWidget))
657 {
658 GetChildren().DeleteObject( m_frameToolBar );
659
660 gtk_widget_reparent( m_frameToolBar->m_widget, m_mainWidget );
661 GtkUpdateSize();
662 }
663 }
664 else // toolbar unset
665 {
666 // still need to update size if it had been there before
667 if ( hadTbar )
668 {
669 GtkUpdateSize();
670 }
671 }
672 }
673
674 #endif // wxUSE_TOOLBAR
675
676 #if wxUSE_STATUSBAR
677
678 wxStatusBar* wxFrame::CreateStatusBar(int number,
679 long style,
680 wxWindowID id,
681 const wxString& name)
682 {
683 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
684
685 // because it will change when toolbar is added
686 GtkUpdateSize();
687
688 return wxFrameBase::CreateStatusBar( number, style, id, name );
689 }
690
691 void wxFrame::SetStatusBar(wxStatusBar *statbar)
692 {
693 bool hadStatBar = m_frameStatusBar != NULL;
694
695 wxFrameBase::SetStatusBar(statbar);
696
697 if (hadStatBar && !m_frameStatusBar)
698 GtkUpdateSize();
699 }
700
701 void wxFrame::PositionStatusBar()
702 {
703 if ( !m_frameStatusBar )
704 return;
705
706 GtkUpdateSize();
707 }
708 #endif // wxUSE_STATUSBAR