]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/frame.cpp
wxMenu code clean up
[wxWidgets.git] / src / gtk1 / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: frame.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "frame.h"
12 #endif
13
14 #include "wx/frame.h"
15 #include "wx/dialog.h"
16 #include "wx/control.h"
17 #include "wx/app.h"
18 #include "wx/menu.h"
19 #include "wx/toolbar.h"
20 #include "wx/statusbr.h"
21 #include "wx/dcclient.h"
22
23 #include "glib.h"
24 #include "gdk/gdk.h"
25 #include "gtk/gtk.h"
26 #include "wx/gtk/win_gtk.h"
27
28 //-----------------------------------------------------------------------------
29 // constants
30 //-----------------------------------------------------------------------------
31
32 const int wxMENU_HEIGHT = 27;
33 const int wxSTATUS_HEIGHT = 25;
34
35 //-----------------------------------------------------------------------------
36 // data
37 //-----------------------------------------------------------------------------
38
39 extern wxList wxTopLevelWindows;
40 extern wxList wxPendingDelete;
41
42 //-----------------------------------------------------------------------------
43 // "size_allocate"
44 //-----------------------------------------------------------------------------
45
46 static void gtk_frame_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxFrame *win )
47 {
48 if (!win->HasVMT()) return;
49
50 /*
51 printf( "OnFrameResize from " );
52 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
53 printf( win->GetClassInfo()->GetClassName() );
54 printf( ".\n" );
55 */
56
57 if ((win->m_width != alloc->width) || (win->m_height != alloc->height))
58 {
59 win->m_sizeSet = FALSE;
60 win->m_width = alloc->width;
61 win->m_height = alloc->height;
62 }
63 }
64
65 //-----------------------------------------------------------------------------
66 // "delete_event"
67 //-----------------------------------------------------------------------------
68
69 static gint gtk_frame_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxFrame *win )
70 {
71 /*
72 printf( "OnDelete from " );
73 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
74 printf( win->GetClassInfo()->GetClassName() );
75 printf( ".\n" );
76 */
77
78 win->Close();
79
80 return TRUE;
81 }
82
83 //-----------------------------------------------------------------------------
84 // "configure_event"
85 //-----------------------------------------------------------------------------
86
87 static gint gtk_frame_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *event, wxFrame *win )
88 {
89 if (!win->HasVMT()) return FALSE;
90
91 win->m_x = event->x;
92 win->m_y = event->y;
93
94 wxMoveEvent mevent( wxPoint(win->m_x,win->m_y), win->GetId() );
95 mevent.SetEventObject( win );
96 win->GetEventHandler()->ProcessEvent( mevent );
97
98 return FALSE;
99 }
100
101 //-----------------------------------------------------------------------------
102 // wxFrame
103 //-----------------------------------------------------------------------------
104
105 BEGIN_EVENT_TABLE(wxFrame, wxWindow)
106 EVT_SIZE(wxFrame::OnSize)
107 EVT_CLOSE(wxFrame::OnCloseWindow)
108 EVT_MENU_HIGHLIGHT_ALL(wxFrame::OnMenuHighlight)
109 END_EVENT_TABLE()
110
111 IMPLEMENT_DYNAMIC_CLASS(wxFrame,wxWindow)
112
113 wxFrame::wxFrame()
114 {
115 m_frameMenuBar = (wxMenuBar *) NULL;
116 m_mdiMenuBar = (wxMenuBar *) NULL;
117 m_frameStatusBar = (wxStatusBar *) NULL;
118 m_frameToolBar = (wxToolBar *) NULL;
119 m_sizeSet = FALSE;
120 m_miniEdge = 0;
121 m_miniTitle = 0;
122 }
123
124 wxFrame::wxFrame( wxWindow *parent, wxWindowID id, const wxString &title,
125 const wxPoint &pos, const wxSize &size,
126 long style, const wxString &name )
127 {
128 m_frameMenuBar = (wxMenuBar *) NULL;
129 m_mdiMenuBar = (wxMenuBar *) NULL;
130 m_frameStatusBar = (wxStatusBar *) NULL;
131 m_frameToolBar = (wxToolBar *) NULL;
132 m_sizeSet = FALSE;
133 m_miniEdge = 0;
134 m_miniTitle = 0;
135 Create( parent, id, title, pos, size, style, name );
136 }
137
138 bool wxFrame::Create( wxWindow *parent, wxWindowID id, const wxString &title,
139 const wxPoint &pos, const wxSize &size,
140 long style, const wxString &name )
141 {
142 wxTopLevelWindows.Append( this );
143
144 m_needParent = FALSE;
145
146 PreCreation( parent, id, pos, size, style, name );
147
148 m_title = title;
149
150 GtkWindowType win_type = GTK_WINDOW_TOPLEVEL;
151 if (style & wxSIMPLE_BORDER) win_type = GTK_WINDOW_POPUP;
152
153 m_widget = gtk_window_new( win_type );
154
155 gtk_window_set_title( GTK_WINDOW(m_widget), title );
156 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
157
158 gtk_window_set_policy( GTK_WINDOW(m_widget), 1, 1, 0 );
159
160 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
161 GTK_SIGNAL_FUNC(gtk_frame_delete_callback), (gpointer)this );
162
163 m_wxwindow = gtk_myfixed_new();
164 gtk_widget_show( m_wxwindow );
165 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
166
167 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
168
169 if (m_parent) m_parent->AddChild( this );
170
171 PostCreation();
172
173 gtk_widget_realize( m_widget );
174
175 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
176 GTK_SIGNAL_FUNC(gtk_frame_size_callback), (gpointer)this );
177
178 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
179 GTK_SIGNAL_FUNC(gtk_frame_configure_callback), (gpointer)this );
180
181 return TRUE;
182 }
183
184 wxFrame::~wxFrame()
185 {
186 if (m_frameMenuBar) delete m_frameMenuBar;
187 m_frameMenuBar = (wxMenuBar *) NULL;
188
189 if (m_frameStatusBar) delete m_frameStatusBar;
190 m_frameStatusBar = (wxStatusBar *) NULL;
191
192 if (m_frameToolBar) delete m_frameToolBar;
193 m_frameToolBar = (wxToolBar *) NULL;
194
195 wxTopLevelWindows.DeleteObject( this );
196
197 if (wxTheApp->GetTopWindow() == this)
198 {
199 wxTheApp->SetTopWindow( (wxWindow*) NULL );
200 }
201
202 if (wxTopLevelWindows.Number() == 0)
203 {
204 wxTheApp->ExitMainLoop();
205 }
206 }
207
208 bool wxFrame::Show( bool show )
209 {
210 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
211
212 if (show && !m_sizeSet)
213 {
214 /* by calling GtkOnSize here, we don't have to call
215 either after showing the frame, which would entail
216 much ugly flicker nor from within the size_allocate
217 handler, because GTK 1.1.X forbids that. */
218
219 GtkOnSize( m_x, m_y, m_width, m_height );
220 }
221
222 return wxWindow::Show( show );
223 }
224
225 bool wxFrame::Destroy()
226 {
227 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
228
229 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
230
231 return TRUE;
232 }
233
234 wxPoint wxFrame::GetClientAreaOrigin() const
235 {
236 wxPoint pt( m_miniEdge, m_miniEdge + m_miniTitle );
237 if (m_frameMenuBar)
238 {
239 int h = 0;
240 m_frameMenuBar->GetSize( (int*)NULL, &h );
241 pt.y += h;
242 }
243 if (m_frameToolBar)
244 {
245 int h = 0;
246 m_frameToolBar->GetSize( (int*)NULL, &h );
247 pt.y += h;
248 }
249 return pt;
250 }
251
252 void wxFrame::DoSetSize( int x, int y, int width, int height, int sizeFlags )
253 {
254 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
255
256 /* don't do anything for children of wxMDIChildFrame */
257 if (!m_wxwindow) return;
258
259 if (m_resizing) return; // I don't like recursions
260 m_resizing = TRUE;
261
262 int old_x = m_x;
263 int old_y = m_y;
264 int old_width = m_width;
265 int old_height = m_height;
266
267 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
268 {
269 if (x != -1) m_x = x;
270 if (y != -1) m_y = y;
271 if (width != -1) m_width = width;
272 if (height != -1) m_height = height;
273 }
274 else
275 {
276 m_x = x;
277 m_y = y;
278 m_width = width;
279 m_height = height;
280 }
281
282 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
283 {
284 if (width == -1) m_width = 80;
285 }
286
287 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
288 {
289 if (height == -1) m_height = 26;
290 }
291
292 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
293 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
294 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
295 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
296
297 if ((m_x != -1) || (m_y != -1))
298 {
299 if ((m_x != old_x) || (m_y != old_y))
300 {
301 /* m_sizeSet = FALSE; */
302 gtk_widget_set_uposition( m_widget, m_x, m_y );
303 }
304 }
305
306 if ((m_width != old_width) || (m_height != old_height))
307 {
308 /* we set the size in GtkOnSize */
309 m_sizeSet = FALSE;
310 }
311
312 m_resizing = FALSE;
313 }
314
315 void wxFrame::Centre( int direction )
316 {
317 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
318
319 int x = 0;
320 int y = 0;
321
322 if ((direction & wxHORIZONTAL) == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
323 if ((direction & wxVERTICAL) == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
324
325 Move( x, y );
326 }
327
328 void wxFrame::GetClientSize( int *width, int *height ) const
329 {
330 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
331
332 wxWindow::GetClientSize( width, height );
333 if (height)
334 {
335 if (m_frameMenuBar) (*height) -= wxMENU_HEIGHT;
336 if (m_frameStatusBar) (*height) -= wxSTATUS_HEIGHT;
337 if (m_frameToolBar)
338 {
339 int y = 0;
340 m_frameToolBar->GetSize( (int *) NULL, &y );
341 (*height) -= y;
342 }
343 (*height) -= m_miniEdge*2 + m_miniTitle;
344 }
345 if (width)
346 {
347 (*width) -= m_miniEdge*2;
348 }
349 }
350
351 void wxFrame::DoSetClientSize( int width, int height )
352 {
353 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
354
355 int h = height;
356 if (m_frameMenuBar) h += wxMENU_HEIGHT;
357 if (m_frameStatusBar) h += wxSTATUS_HEIGHT;
358 if (m_frameToolBar)
359 {
360 int y = 0;
361 m_frameToolBar->GetSize( (int *) NULL, &y );
362 h += y;
363 }
364 wxWindow::DoSetClientSize( width + m_miniEdge*2, h + m_miniEdge*2 + m_miniTitle );
365 }
366
367 void wxFrame::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
368 {
369 // due to a bug in gtk, x,y are always 0
370 // m_x = x;
371 // m_y = y;
372
373 if (m_resizing) return;
374 m_resizing = TRUE;
375
376 if (!m_wxwindow) return;
377
378 m_width = width;
379 m_height = height;
380
381 /* check if size is in legal range */
382 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
383 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
384 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
385 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
386
387 /* this emulates the new wxMSW behaviour of placing all
388 * frame-subwindows (menu, toolbar..) on one native window
389 * this hurts in the eye, but I don't want to call SetSize()
390 * because I don't want to call any non-native functions here. */
391
392 if (m_frameMenuBar)
393 {
394 int xx = m_miniEdge;
395 int yy = m_miniEdge + m_miniTitle;
396 int ww = m_width - 2*m_miniEdge;
397 int hh = wxMENU_HEIGHT;
398 m_frameMenuBar->m_x = xx;
399 m_frameMenuBar->m_y = yy;
400 m_frameMenuBar->m_width = ww;
401 m_frameMenuBar->m_height = hh;
402
403 gtk_myfixed_move( GTK_MYFIXED(m_wxwindow), m_frameMenuBar->m_widget, xx, yy );
404 gtk_widget_set_usize( m_frameMenuBar->m_widget, ww, hh );
405 }
406
407 if (m_frameToolBar)
408 {
409 int xx = m_miniEdge;
410 int yy = m_miniEdge + m_miniTitle;
411 if ((m_frameMenuBar) || (m_mdiMenuBar)) yy += wxMENU_HEIGHT;
412 int ww = m_width - 2*m_miniEdge;
413 int hh = m_frameToolBar->m_height;
414
415 m_frameToolBar->m_x = xx;
416 m_frameToolBar->m_y = yy;
417 m_frameToolBar->m_height = hh;
418 m_frameToolBar->m_width = ww;
419
420 gtk_myfixed_move( GTK_MYFIXED(m_wxwindow), m_frameToolBar->m_widget, xx, yy );
421 gtk_widget_set_usize( m_frameToolBar->m_widget, ww, hh );
422 }
423
424 if (m_frameStatusBar)
425 {
426 int xx = 0 + m_miniEdge;
427 int yy = m_height - wxSTATUS_HEIGHT - m_miniEdge;
428 int ww = m_width - 2*m_miniEdge;
429 int hh = wxSTATUS_HEIGHT;
430
431 m_frameStatusBar->m_x = xx;
432 m_frameStatusBar->m_y = yy;
433 m_frameStatusBar->m_width = ww;
434 m_frameStatusBar->m_height = hh;
435
436 gtk_myfixed_move( GTK_MYFIXED(m_wxwindow), m_frameStatusBar->m_widget, xx, yy );
437 gtk_widget_set_usize( m_frameStatusBar->m_widget, ww, hh );
438 }
439
440 /* we actually set the size of a frame here and no-where else */
441 gtk_widget_set_usize( m_widget, m_width, m_height );
442
443 m_sizeSet = TRUE;
444
445 /* send size event to frame */
446 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
447 event.SetEventObject( this );
448 GetEventHandler()->ProcessEvent( event );
449
450 /* send size event to status bar */
451 if (m_frameStatusBar)
452 {
453 wxSizeEvent event2( wxSize(m_frameStatusBar->m_width,m_frameStatusBar->m_height), m_frameStatusBar->GetId() );
454 event2.SetEventObject( m_frameStatusBar );
455 m_frameStatusBar->GetEventHandler()->ProcessEvent( event2 );
456 }
457
458 m_resizing = FALSE;
459 }
460
461 void wxFrame::OnInternalIdle()
462 {
463 if (!m_sizeSet)
464 GtkOnSize( m_x, m_y, m_width, m_height );
465
466 DoMenuUpdates();
467 }
468
469 void wxFrame::OnCloseWindow( wxCloseEvent& event )
470 {
471 // close the window if it wasn't vetoed by the application
472 // if ( !event.GetVeto() ) // No, this isn't the interpretation of GetVeto.
473 Destroy();
474 }
475
476 void wxFrame::OnSize( wxSizeEvent &WXUNUSED(event) )
477 {
478 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
479
480 if (GetAutoLayout())
481 {
482 Layout();
483 }
484 else
485 {
486 // do we have exactly one child?
487 wxWindow *child = (wxWindow *)NULL;
488 for ( wxNode *node = GetChildren().First(); node; node = node->Next() )
489 {
490 wxWindow *win = (wxWindow *)node->Data();
491 if ( !wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog) )
492 {
493 if ( child )
494 {
495 // it's the second one: do nothing
496 return;
497 }
498
499 child = win;
500 }
501 }
502
503 // no children at all?
504 if ( child )
505 {
506 // yes: set it's size to fill all the frame
507 int client_x, client_y;
508 GetClientSize( &client_x, &client_y );
509 child->SetSize( 1, 1, client_x-2, client_y-2 );
510 }
511 }
512 }
513
514 static void SetInvokingWindow( wxMenu *menu, wxWindow *win )
515 {
516 menu->SetInvokingWindow( win );
517 wxNode *node = menu->GetItems().First();
518 while (node)
519 {
520 wxMenuItem *menuitem = (wxMenuItem*)node->Data();
521 if (menuitem->IsSubMenu())
522 SetInvokingWindow( menuitem->GetSubMenu(), win );
523 node = node->Next();
524 }
525 }
526
527 void wxFrame::SetMenuBar( wxMenuBar *menuBar )
528 {
529 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
530 wxASSERT_MSG( (m_wxwindow != NULL), "invalid frame" );
531
532 m_frameMenuBar = menuBar;
533
534 if (m_frameMenuBar)
535 {
536 wxNode *node = m_frameMenuBar->GetMenus().First();
537 while (node)
538 {
539 wxMenu *menu = (wxMenu*)node->Data();
540 SetInvokingWindow( menu, this );
541 node = node->Next();
542 }
543
544 if (m_frameMenuBar->m_parent != this)
545 {
546 m_frameMenuBar->m_parent = this;
547 gtk_myfixed_put( GTK_MYFIXED(m_wxwindow),
548 m_frameMenuBar->m_widget, m_frameMenuBar->m_x, m_frameMenuBar->m_y );
549
550 /* an mdi child menu bar might be underneath */
551 if (m_mdiMenuBar)
552 m_frameMenuBar->Show( FALSE );
553 }
554 }
555
556 m_sizeSet = FALSE;
557 }
558
559 wxMenuBar *wxFrame::GetMenuBar() const
560 {
561 return m_frameMenuBar;
562 }
563
564 void wxFrame::OnMenuHighlight(wxMenuEvent& event)
565 {
566 if (GetStatusBar())
567 {
568 // if no help string found, we will clear the status bar text
569 wxString helpString;
570
571 int menuId = event.GetMenuId();
572 if ( menuId != -1 )
573 {
574 wxMenuBar *menuBar = GetMenuBar();
575 if (menuBar)
576 {
577 helpString = menuBar->GetHelpString(menuId);
578 }
579 }
580
581 SetStatusText(helpString);
582 }
583 }
584
585 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
586 {
587 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
588
589 wxCHECK_MSG( m_frameToolBar == NULL, FALSE, "recreating toolbar in wxFrame" );
590
591 m_frameToolBar = OnCreateToolBar( style, id, name );
592
593 GetChildren().DeleteObject( m_frameToolBar );
594
595 m_sizeSet = FALSE;
596
597 return m_frameToolBar;
598 }
599
600 wxToolBar* wxFrame::OnCreateToolBar( long style, wxWindowID id, const wxString& name )
601 {
602 return new wxToolBar( this, id, wxDefaultPosition, wxDefaultSize, style, name );
603 }
604
605 wxToolBar *wxFrame::GetToolBar() const
606 {
607 return m_frameToolBar;
608 }
609
610 wxStatusBar* wxFrame::CreateStatusBar( int number, long style, wxWindowID id, const wxString& name )
611 {
612 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
613
614 wxCHECK_MSG( m_frameStatusBar == NULL, FALSE, "recreating status bar in wxFrame" );
615
616 m_frameStatusBar = OnCreateStatusBar( number, style, id, name );
617
618 m_sizeSet = FALSE;
619
620 return m_frameStatusBar;
621 }
622
623 wxStatusBar *wxFrame::OnCreateStatusBar( int number, long style, wxWindowID id, const wxString& name )
624 {
625 wxStatusBar *statusBar = (wxStatusBar *) NULL;
626
627 statusBar = new wxStatusBar(this, id, wxPoint(0, 0), wxSize(100, 20), style, name);
628
629 // Set the height according to the font and the border size
630 wxClientDC dc(statusBar);
631 dc.SetFont( statusBar->GetFont() );
632
633 long x, y;
634 dc.GetTextExtent( "X", &x, &y );
635
636 int height = (int)( (y * 1.1) + 2* statusBar->GetBorderY());
637
638 statusBar->SetSize( -1, -1, 100, height );
639
640 statusBar->SetFieldsCount( number );
641 return statusBar;
642 }
643
644 void wxFrame::SetStatusText(const wxString& text, int number)
645 {
646 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
647
648 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set text for" );
649
650 m_frameStatusBar->SetStatusText(text, number);
651 }
652
653 void wxFrame::SetStatusWidths(int n, const int widths_field[] )
654 {
655 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
656
657 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set widths for" );
658
659 m_frameStatusBar->SetStatusWidths(n, widths_field);
660 }
661
662 wxStatusBar *wxFrame::GetStatusBar() const
663 {
664 return m_frameStatusBar;
665 }
666
667 void wxFrame::SetTitle( const wxString &title )
668 {
669 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
670
671 m_title = title;
672 if (m_title.IsNull()) m_title = "";
673 gtk_window_set_title( GTK_WINDOW(m_widget), title );
674 }
675
676 void wxFrame::SetIcon( const wxIcon &icon )
677 {
678 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
679
680 m_icon = icon;
681 if (!icon.Ok()) return;
682
683 wxMask *mask = icon.GetMask();
684 GdkBitmap *bm = (GdkBitmap *) NULL;
685 if (mask) bm = mask->GetBitmap();
686
687 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
688 }
689