1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "frame.h"
15 #include "wx/dialog.h"
16 #include "wx/control.h"
19 #include "wx/toolbar.h"
20 #include "wx/statusbr.h"
21 #include "wx/dcclient.h"
26 #include "wx/gtk/win_gtk.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 const int wxMENU_HEIGHT
= 27;
33 const int wxSTATUS_HEIGHT
= 25;
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 extern wxList wxPendingDelete
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 static void gtk_frame_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxFrame
*win
)
47 if (!win
->HasVMT()) return;
50 printf( "OnFrameResize from " );
51 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
52 printf( win->GetClassInfo()->GetClassName() );
56 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
58 win
->m_sizeSet
= FALSE
;
59 win
->m_width
= alloc
->width
;
60 win
->m_height
= alloc
->height
;
64 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
68 static gint
gtk_frame_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxFrame
*win
)
71 printf( "OnDelete from " );
72 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
73 printf( win->GetClassInfo()->GetClassName() );
82 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
86 static gint
gtk_frame_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxFrame
*win
)
88 if (!win
->HasVMT()) return FALSE
;
93 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
94 mevent
.SetEventObject( win
);
95 win
->GetEventHandler()->ProcessEvent( mevent
);
100 //-----------------------------------------------------------------------------
102 //-----------------------------------------------------------------------------
104 BEGIN_EVENT_TABLE(wxFrame
, wxWindow
)
105 EVT_SIZE(wxFrame::OnSize
)
106 EVT_CLOSE(wxFrame::OnCloseWindow
)
107 EVT_MENU_HIGHLIGHT_ALL(wxFrame::OnMenuHighlight
)
110 IMPLEMENT_DYNAMIC_CLASS(wxFrame
,wxWindow
)
114 m_frameMenuBar
= (wxMenuBar
*) NULL
;
115 m_mdiMenuBar
= (wxMenuBar
*) NULL
;
116 m_frameStatusBar
= (wxStatusBar
*) NULL
;
117 m_frameToolBar
= (wxToolBar
*) NULL
;
123 wxFrame::wxFrame( wxWindow
*parent
, wxWindowID id
, const wxString
&title
,
124 const wxPoint
&pos
, const wxSize
&size
,
125 long style
, const wxString
&name
)
127 m_frameMenuBar
= (wxMenuBar
*) NULL
;
128 m_mdiMenuBar
= (wxMenuBar
*) NULL
;
129 m_frameStatusBar
= (wxStatusBar
*) NULL
;
130 m_frameToolBar
= (wxToolBar
*) NULL
;
134 Create( parent
, id
, title
, pos
, size
, style
, name
);
137 bool wxFrame::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&title
,
138 const wxPoint
&pos
, const wxSize
&size
,
139 long style
, const wxString
&name
)
141 wxTopLevelWindows
.Append( this );
143 m_needParent
= FALSE
;
145 PreCreation( parent
, id
, pos
, size
, style
, name
);
149 GtkWindowType win_type
= GTK_WINDOW_TOPLEVEL
;
150 if (style
& wxSIMPLE_BORDER
) win_type
= GTK_WINDOW_POPUP
;
152 m_widget
= gtk_window_new( win_type
);
154 gtk_window_set_title( GTK_WINDOW(m_widget
), title
);
155 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
157 gtk_window_set_policy( GTK_WINDOW(m_widget
), 1, 1, 0 );
159 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
160 GTK_SIGNAL_FUNC(gtk_frame_delete_callback
), (gpointer
)this );
162 m_wxwindow
= gtk_myfixed_new();
163 gtk_widget_show( m_wxwindow
);
164 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
166 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
168 if (m_parent
) m_parent
->AddChild( this );
172 gtk_widget_realize( m_widget
);
174 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
175 GTK_SIGNAL_FUNC(gtk_frame_size_callback
), (gpointer
)this );
177 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
178 GTK_SIGNAL_FUNC(gtk_frame_configure_callback
), (gpointer
)this );
185 if (m_frameMenuBar
) delete m_frameMenuBar
;
186 m_frameMenuBar
= (wxMenuBar
*) NULL
;
188 if (m_frameStatusBar
) delete m_frameStatusBar
;
189 m_frameStatusBar
= (wxStatusBar
*) NULL
;
191 if (m_frameToolBar
) delete m_frameToolBar
;
192 m_frameToolBar
= (wxToolBar
*) NULL
;
194 wxTopLevelWindows
.DeleteObject( this );
196 if (wxTheApp
->GetTopWindow() == this)
198 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
201 if (wxTopLevelWindows
.Number() == 0)
203 wxTheApp
->ExitMainLoop();
207 bool wxFrame::Show( bool show
)
209 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
211 if (show
&& !m_sizeSet
)
213 /* by calling GtkOnSize here, we don't have to call
214 either after showing the frame, which would entail
215 much ugly flicker nor from within the size_allocate
216 handler, because GTK 1.1.X forbids that. */
218 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
221 return wxWindow::Show( show
);
224 bool wxFrame::Destroy()
226 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
228 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
233 wxPoint
wxFrame::GetClientAreaOrigin() const
235 wxPoint
pt( m_miniEdge
, m_miniEdge
+ m_miniTitle
);
239 m_frameMenuBar
->GetSize( (int*)NULL
, &h
);
245 m_frameToolBar
->GetSize( (int*)NULL
, &h
);
251 void wxFrame::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
253 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
255 /* don't do anything for children of wxMDIChildFrame */
256 if (!m_wxwindow
) return;
258 if (m_resizing
) return; // I don't like recursions
263 int old_width
= m_width
;
264 int old_height
= m_height
;
266 if ((sizeFlags
& wxSIZE_USE_EXISTING
) == wxSIZE_USE_EXISTING
)
268 if (x
!= -1) m_x
= x
;
269 if (y
!= -1) m_y
= y
;
270 if (width
!= -1) m_width
= width
;
271 if (height
!= -1) m_height
= height
;
281 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
283 if (width
== -1) m_width
= 80;
286 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
288 if (height
== -1) m_height
= 26;
291 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
292 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
293 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
294 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
296 if ((m_x
!= -1) || (m_y
!= -1))
298 if ((m_x
!= old_x
) || (m_y
!= old_y
))
300 /* m_sizeSet = FALSE; */
301 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
305 if ((m_width
!= old_width
) || (m_height
!= old_height
))
307 /* we set the size in GtkOnSize */
314 void wxFrame::Centre( int direction
)
316 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
321 if ((direction
& wxHORIZONTAL
) == wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
322 if ((direction
& wxVERTICAL
) == wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
327 void wxFrame::GetClientSize( int *width
, int *height
) const
329 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
331 wxWindow::GetClientSize( width
, height
);
334 if (m_frameMenuBar
) (*height
) -= wxMENU_HEIGHT
;
335 if (m_frameStatusBar
) (*height
) -= wxSTATUS_HEIGHT
;
339 m_frameToolBar
->GetSize( (int *) NULL
, &y
);
342 (*height
) -= m_miniEdge
*2 + m_miniTitle
;
346 (*width
) -= m_miniEdge
*2;
350 void wxFrame::DoSetClientSize( int width
, int height
)
352 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
355 if (m_frameMenuBar
) h
+= wxMENU_HEIGHT
;
356 if (m_frameStatusBar
) h
+= wxSTATUS_HEIGHT
;
360 m_frameToolBar
->GetSize( (int *) NULL
, &y
);
363 wxWindow::DoSetClientSize( width
+ m_miniEdge
*2, h
+ m_miniEdge
*2 + m_miniTitle
);
366 void wxFrame::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
368 // due to a bug in gtk, x,y are always 0
372 if (m_resizing
) return;
375 if (!m_wxwindow
) return;
380 /* check if size is in legal range */
381 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
382 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
383 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
384 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
386 /* this emulates the new wxMSW behaviour of placing all
387 * frame-subwindows (menu, toolbar..) on one native window
388 * this hurts in the eye, but I don't want to call SetSize()
389 * because I don't want to call any non-native functions here. */
394 int yy
= m_miniEdge
+ m_miniTitle
;
395 int ww
= m_width
- 2*m_miniEdge
;
396 int hh
= wxMENU_HEIGHT
;
397 m_frameMenuBar
->m_x
= xx
;
398 m_frameMenuBar
->m_y
= yy
;
399 m_frameMenuBar
->m_width
= ww
;
400 m_frameMenuBar
->m_height
= hh
;
402 gtk_myfixed_move( GTK_MYFIXED(m_wxwindow
), m_frameMenuBar
->m_widget
, xx
, yy
);
403 gtk_widget_set_usize( m_frameMenuBar
->m_widget
, ww
, hh
);
409 int yy
= m_miniEdge
+ m_miniTitle
;
410 if ((m_frameMenuBar
) || (m_mdiMenuBar
)) yy
+= wxMENU_HEIGHT
;
411 int ww
= m_width
- 2*m_miniEdge
;
412 int hh
= m_frameToolBar
->m_height
;
414 m_frameToolBar
->m_x
= xx
;
415 m_frameToolBar
->m_y
= yy
;
416 m_frameToolBar
->m_height
= hh
;
417 m_frameToolBar
->m_width
= ww
;
419 gtk_myfixed_move( GTK_MYFIXED(m_wxwindow
), m_frameToolBar
->m_widget
, xx
, yy
);
420 gtk_widget_set_usize( m_frameToolBar
->m_widget
, ww
, hh
);
423 if (m_frameStatusBar
)
425 int xx
= 0 + m_miniEdge
;
426 int yy
= m_height
- wxSTATUS_HEIGHT
- m_miniEdge
;
427 int ww
= m_width
- 2*m_miniEdge
;
428 int hh
= wxSTATUS_HEIGHT
;
430 m_frameStatusBar
->m_x
= xx
;
431 m_frameStatusBar
->m_y
= yy
;
432 m_frameStatusBar
->m_width
= ww
;
433 m_frameStatusBar
->m_height
= hh
;
435 gtk_myfixed_move( GTK_MYFIXED(m_wxwindow
), m_frameStatusBar
->m_widget
, xx
, yy
);
436 gtk_widget_set_usize( m_frameStatusBar
->m_widget
, ww
, hh
);
439 /* we actually set the size of a frame here and no-where else */
440 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
444 /* send size event to frame */
445 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
446 event
.SetEventObject( this );
447 GetEventHandler()->ProcessEvent( event
);
449 /* send size event to status bar */
450 if (m_frameStatusBar
)
452 wxSizeEvent
event2( wxSize(m_frameStatusBar
->m_width
,m_frameStatusBar
->m_height
), m_frameStatusBar
->GetId() );
453 event2
.SetEventObject( m_frameStatusBar
);
454 m_frameStatusBar
->GetEventHandler()->ProcessEvent( event2
);
460 void wxFrame::OnInternalIdle()
463 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
468 void wxFrame::OnCloseWindow( wxCloseEvent
& event
)
470 // close the window if it wasn't vetoed by the application
471 // if ( !event.GetVeto() ) // No, this isn't the interpretation of GetVeto.
475 void wxFrame::OnSize( wxSizeEvent
&WXUNUSED(event
) )
477 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
485 // do we have exactly one child?
486 wxWindow
*child
= (wxWindow
*)NULL
;
487 for ( wxNode
*node
= GetChildren().First(); node
; node
= node
->Next() )
489 wxWindow
*win
= (wxWindow
*)node
->Data();
490 if ( !wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
) )
494 // it's the second one: do nothing
502 // no children at all?
505 // yes: set it's size to fill all the frame
506 int client_x
, client_y
;
507 GetClientSize( &client_x
, &client_y
);
508 child
->SetSize( 1, 1, client_x
-2, client_y
-2 );
513 static void SetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
515 menu
->SetInvokingWindow( win
);
516 wxNode
*node
= menu
->GetItems().First();
519 wxMenuItem
*menuitem
= (wxMenuItem
*)node
->Data();
520 if (menuitem
->IsSubMenu())
521 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
526 void wxFrame::SetMenuBar( wxMenuBar
*menuBar
)
528 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
529 wxASSERT_MSG( (m_wxwindow
!= NULL
), "invalid frame" );
531 m_frameMenuBar
= menuBar
;
535 wxNode
*node
= m_frameMenuBar
->GetMenus().First();
538 wxMenu
*menu
= (wxMenu
*)node
->Data();
539 SetInvokingWindow( menu
, this );
543 if (m_frameMenuBar
->m_parent
!= this)
545 m_frameMenuBar
->m_parent
= this;
546 gtk_myfixed_put( GTK_MYFIXED(m_wxwindow
),
547 m_frameMenuBar
->m_widget
, m_frameMenuBar
->m_x
, m_frameMenuBar
->m_y
);
549 /* an mdi child menu bar might be underneath */
551 m_frameMenuBar
->Show( FALSE
);
558 wxMenuBar
*wxFrame::GetMenuBar() const
560 return m_frameMenuBar
;
563 void wxFrame::OnMenuHighlight(wxMenuEvent
& event
)
567 // if no help string found, we will clear the status bar text
570 int menuId
= event
.GetMenuId();
573 wxMenuBar
*menuBar
= GetMenuBar();
576 helpString
= menuBar
->GetHelpString(menuId
);
580 SetStatusText(helpString
);
584 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
586 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
588 wxCHECK_MSG( m_frameToolBar
== NULL
, FALSE
, "recreating toolbar in wxFrame" );
590 m_frameToolBar
= OnCreateToolBar( style
, id
, name
);
592 GetChildren().DeleteObject( m_frameToolBar
);
596 return m_frameToolBar
;
599 wxToolBar
* wxFrame::OnCreateToolBar( long style
, wxWindowID id
, const wxString
& name
)
601 return new wxToolBar( this, id
, wxDefaultPosition
, wxDefaultSize
, style
, name
);
604 wxToolBar
*wxFrame::GetToolBar() const
606 return m_frameToolBar
;
609 wxStatusBar
* wxFrame::CreateStatusBar( int number
, long style
, wxWindowID id
, const wxString
& name
)
611 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
613 wxCHECK_MSG( m_frameStatusBar
== NULL
, FALSE
, "recreating status bar in wxFrame" );
615 m_frameStatusBar
= OnCreateStatusBar( number
, style
, id
, name
);
619 return m_frameStatusBar
;
622 wxStatusBar
*wxFrame::OnCreateStatusBar( int number
, long style
, wxWindowID id
, const wxString
& name
)
624 wxStatusBar
*statusBar
= (wxStatusBar
*) NULL
;
626 statusBar
= new wxStatusBar(this, id
, wxPoint(0, 0), wxSize(100, 20), style
, name
);
628 // Set the height according to the font and the border size
629 wxClientDC
dc(statusBar
);
630 dc
.SetFont( statusBar
->GetFont() );
633 dc
.GetTextExtent( "X", &x
, &y
);
635 int height
= (int)( (y
* 1.1) + 2* statusBar
->GetBorderY());
637 statusBar
->SetSize( -1, -1, 100, height
);
639 statusBar
->SetFieldsCount( number
);
643 void wxFrame::Command( int id
)
645 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
646 commandEvent
.SetInt( id
);
647 commandEvent
.SetEventObject( this );
649 wxMenuBar
*bar
= GetMenuBar();
652 wxMenuItem
*item
= bar
->FindItemForId(id
) ;
653 if (item
&& item
->IsCheckable())
655 bar
->Check(id
,!bar
->Checked(id
)) ;
658 // Process events starting with the window with the focus, if any.
659 wxWindow
* focusWin
= wxFindFocusDescendant(this);
661 wxEvtHandler
* evtHandler
= focusWin
? focusWin
->GetEventHandler() : GetEventHandler();
663 evtHandler
->ProcessEvent(commandEvent
);
666 void wxFrame::SetStatusText(const wxString
& text
, int number
)
668 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
670 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set text for" );
672 m_frameStatusBar
->SetStatusText(text
, number
);
675 void wxFrame::SetStatusWidths(int n
, const int widths_field
[] )
677 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
679 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set widths for" );
681 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
684 wxStatusBar
*wxFrame::GetStatusBar() const
686 return m_frameStatusBar
;
689 void wxFrame::SetTitle( const wxString
&title
)
691 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
694 if (m_title
.IsNull()) m_title
= "";
695 gtk_window_set_title( GTK_WINDOW(m_widget
), title
);
698 void wxFrame::SetIcon( const wxIcon
&icon
)
700 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
703 if (!icon
.Ok()) return;
705 wxMask
*mask
= icon
.GetMask();
706 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
707 if (mask
) bm
= mask
->GetBitmap();
709 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);