1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "frame.h"
16 #include "wx/dialog.h"
17 #include "wx/control.h"
20 #include "wx/toolbar.h"
21 #include "wx/statusbr.h"
23 #include "wx/dcclient.h"
24 #include "wx/gtk/win_gtk.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 const int wxMENU_HEIGHT
= 28;
31 const int wxSTATUS_HEIGHT
= 25;
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern wxList wxTopLevelWindows
;
38 extern wxList wxPendingDelete
;
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 static void gtk_frame_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxFrame
*win
)
46 if (!win
->HasVMT()) return;
49 printf( "OnFrameResize from " );
50 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
51 printf( win->GetClassInfo()->GetClassName() );
55 win
->GtkOnSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 static gint
gtk_frame_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxFrame
*win
)
65 printf( "OnDelete from " );
66 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
67 printf( win->GetClassInfo()->GetClassName() );
76 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
80 static gint
gtk_frame_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxFrame
*win
)
82 if (!win
->HasVMT()) return FALSE
;
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
94 BEGIN_EVENT_TABLE(wxFrame
, wxWindow
)
95 EVT_SIZE(wxFrame::OnSize
)
96 EVT_CLOSE(wxFrame::OnCloseWindow
)
97 EVT_IDLE(wxFrame::OnIdle
)
100 IMPLEMENT_DYNAMIC_CLASS(wxFrame
,wxWindow
)
104 m_frameMenuBar
= (wxMenuBar
*) NULL
;
105 m_frameStatusBar
= (wxStatusBar
*) NULL
;
106 m_frameToolBar
= (wxToolBar
*) NULL
;
108 m_addPrivateChild
= FALSE
;
109 m_wxwindow
= (GtkWidget
*) NULL
;
110 m_mainWindow
= (GtkWidget
*) NULL
;
111 wxTopLevelWindows
.Insert( this );
114 wxFrame::wxFrame( wxWindow
*parent
, wxWindowID id
, const wxString
&title
,
115 const wxPoint
&pos
, const wxSize
&size
,
116 long style
, const wxString
&name
)
118 m_frameMenuBar
= (wxMenuBar
*) NULL
;
119 m_frameStatusBar
= (wxStatusBar
*) NULL
;
120 m_frameToolBar
= (wxToolBar
*) NULL
;
122 m_addPrivateChild
= FALSE
;
123 m_wxwindow
= (GtkWidget
*) NULL
;
124 m_mainWindow
= (GtkWidget
*) NULL
;
125 Create( parent
, id
, title
, pos
, size
, style
, name
);
126 wxTopLevelWindows
.Insert( this );
129 bool wxFrame::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&title
,
130 const wxPoint
&pos
, const wxSize
&size
,
131 long style
, const wxString
&name
)
133 m_needParent
= FALSE
;
135 PreCreation( parent
, id
, pos
, size
, style
, name
);
139 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
140 if ((size
.x
!= -1) && (size
.y
!= -1))
141 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
142 if ((pos
.x
!= -1) && (pos
.y
!= -1))
143 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
145 gtk_window_set_title( GTK_WINDOW(m_widget
), title
);
146 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
148 gtk_widget_set( m_widget
, "GtkWindow::allow_shrink", TRUE
, NULL
);
150 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
151 GTK_SIGNAL_FUNC(gtk_frame_delete_callback
), (gpointer
)this );
153 m_mainWindow
= gtk_myfixed_new();
154 gtk_widget_show( m_mainWindow
);
155 GTK_WIDGET_UNSET_FLAGS( m_mainWindow
, GTK_CAN_FOCUS
);
157 gtk_container_add( GTK_CONTAINER(m_widget
), m_mainWindow
);
158 gtk_widget_set_uposition( m_mainWindow
, 0, 0 );
160 m_wxwindow
= gtk_myfixed_new();
161 gtk_widget_show( m_wxwindow
);
162 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
164 gtk_container_add( GTK_CONTAINER(m_mainWindow
), m_wxwindow
);
166 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
167 GTK_SIGNAL_FUNC(gtk_frame_size_callback
), (gpointer
)this );
169 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
170 GTK_SIGNAL_FUNC(gtk_frame_configure_callback
), (gpointer
)this );
174 gtk_widget_realize( m_mainWindow
);
181 if (m_frameMenuBar
) delete m_frameMenuBar
;
182 if (m_frameStatusBar
) delete m_frameStatusBar
;
183 if (m_frameToolBar
) delete m_frameToolBar
;
185 // if (m_mainWindow) gtk_widget_destroy( m_mainWindow );
187 wxTopLevelWindows
.DeleteObject( this );
188 if (wxTopLevelWindows
.Number() == 0) wxTheApp
->ExitMainLoop();
191 bool wxFrame::Show( bool show
)
193 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
197 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
199 ProcessEvent( event
);
201 return wxWindow::Show( show
);
204 void wxFrame::Enable( bool enable
)
206 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
208 if (!m_mainWindow
) return;
210 wxWindow::Enable( enable
);
211 gtk_widget_set_sensitive( m_mainWindow
, enable
);
214 void wxFrame::OnCloseWindow( wxCloseEvent
&event
)
216 if (GetEventHandler()->OnClose() || event
.GetForce()) this->Destroy();
219 bool wxFrame::Destroy()
221 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
223 if (!wxPendingDelete
.Member(this))
224 wxPendingDelete
.Append(this);
229 void wxFrame::ImplementSetPosition(void)
231 if ((m_x
!= -1) || (m_y
!= -1))
232 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
235 void wxFrame::Centre( int direction
)
237 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
239 if (direction
& wxHORIZONTAL
== wxHORIZONTAL
) m_x
= (gdk_screen_width () - m_width
) / 2;
240 if (direction
& wxVERTICAL
== wxVERTICAL
) m_y
= (gdk_screen_height () - m_height
) / 2;
241 ImplementSetPosition();
244 void wxFrame::GetClientSize( int *width
, int *height
) const
246 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
248 wxWindow::GetClientSize( width
, height
);
251 if (m_frameMenuBar
) (*height
) -= wxMENU_HEIGHT
;
252 if (m_frameStatusBar
) (*height
) -= wxSTATUS_HEIGHT
;
256 m_frameToolBar
->GetSize( (int *) NULL
, &y
);
262 void wxFrame::SetClientSize( int const width
, int const height
)
264 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
267 if (m_frameMenuBar
) h
+= wxMENU_HEIGHT
;
268 if (m_frameStatusBar
) h
+= wxSTATUS_HEIGHT
;
272 m_frameToolBar
->GetSize( (int *) NULL
, &y
);
275 wxWindow::SetClientSize( width
, h
);
278 void wxFrame::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
280 // due to a bug in gtk, x,y are always 0
284 if ((m_height
== height
) && (m_width
== width
) &&
286 if (!m_mainWindow
) return;
287 if (!m_wxwindow
) return;
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_minWidth
;
294 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_minHeight
;
296 gtk_widget_set_usize( m_widget
, width
, height
);
300 int main_height
= height
;
301 int main_width
= width
;
303 // This emulates Windows behaviour:
304 // The menu bar is part of the main window, but the status bar
305 // is on the implementation side in the client area. The
306 // function GetClientSize returns the size of the client area
307 // minus the status bar height. Under wxGTK, the main window
308 // is represented by m_mainWindow. The menubar is inserted
309 // into m_mainWindow whereas the statusbar is insertes into
310 // m_wxwindow just like any other window.
313 // gtk_widget_set_usize( m_mainWindow, width, height );
317 main_y
= wxMENU_HEIGHT
;
318 main_height
-= wxMENU_HEIGHT
;
321 int toolbar_height
= 0;
322 if (m_frameToolBar
) m_frameToolBar
->GetSize( (int *) NULL
, &toolbar_height
);
324 main_y
+= toolbar_height
;
325 main_height
-= toolbar_height
;
327 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow
), m_wxwindow
, main_x
, main_y
);
328 gtk_widget_set_usize( m_wxwindow
, main_width
, main_height
);
332 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow
), m_frameMenuBar
->m_widget
, 1, 1 );
333 gtk_widget_set_usize( m_frameMenuBar
->m_widget
, width
-2, wxMENU_HEIGHT
-2 );
338 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow
), m_frameToolBar
->m_widget
, 1, wxMENU_HEIGHT
);
339 gtk_widget_set_usize( m_frameToolBar
->m_widget
, width
-2, toolbar_height
);
342 if (m_frameStatusBar
)
344 m_frameStatusBar
->SetSize( 0, main_height
-wxSTATUS_HEIGHT
, width
, wxSTATUS_HEIGHT
);
349 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
350 event
.SetEventObject( this );
351 ProcessEvent( event
);
354 void wxFrame::OnSize( wxSizeEvent
&WXUNUSED(event
) )
356 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
358 if ( GetAutoLayout() )
361 // no child: go out !
362 if (!GetChildren()->First())
365 // do we have exactly one child?
366 wxWindow
*child
= (wxWindow
*) NULL
;
367 for(wxNode
*node
= GetChildren()->First(); node
; node
= node
->Next())
369 wxWindow
*win
= (wxWindow
*)node
->Data();
370 if (!IS_KIND_OF(win
,wxFrame
) && !IS_KIND_OF(win
,wxDialog
)
371 #if 0 // not in m_children anyway
372 && (win
!= m_frameMenuBar
) &&
373 (win
!= m_frameToolBar
) &&
374 (win
!= m_frameStatusBar
)
378 if ( child
) // it's the second one: do nothing
385 // yes: set it's size to fill all the frame
386 int client_x
, client_y
;
387 GetClientSize(&client_x
, &client_y
);
388 child
->SetSize( 1, 1, client_x
-2, client_y
);
392 void wxFrame::AddChild( wxWindow
*child
)
394 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
395 wxASSERT_MSG( (m_wxwindow
!= NULL
), "invalid frame" );
396 wxASSERT_MSG( (m_mainWindow
!= NULL
), "invalid frame" );
397 wxASSERT_MSG( (child
!= NULL
), "invalid child" );
398 wxASSERT_MSG( (child
->m_widget
!= NULL
), "invalid child" );
400 // wxFrame and wxDialog as children aren't placed into the parents
402 if (IS_KIND_OF(child
,wxMDIChildFrame
)) wxFAIL_MSG( "wxFrame::AddChild error.\n" );
404 if ( IS_KIND_OF(child
,wxFrame
) || IS_KIND_OF(child
,wxDialog
))
406 m_children
.Append( child
);
408 if ((child
->m_x
!= -1) && (child
->m_y
!= -1))
409 gtk_widget_set_uposition( child
->m_widget
, child
->m_x
, child
->m_y
);
414 if (m_addPrivateChild
)
416 gtk_myfixed_put( GTK_MYFIXED(m_mainWindow
), child
->m_widget
, child
->m_x
, child
->m_y
);
418 gtk_widget_set_usize( child
->m_widget
, child
->m_width
, child
->m_height
);
422 m_children
.Append( child
);
425 gtk_myfixed_put( GTK_MYFIXED(m_wxwindow
), child
->m_widget
, child
->m_x
, child
->m_y
);
427 gtk_widget_set_usize( child
->m_widget
, child
->m_width
, child
->m_height
);
431 static void SetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
433 menu
->SetInvokingWindow( win
);
434 wxNode
*node
= menu
->m_items
.First();
437 wxMenuItem
*menuitem
= (wxMenuItem
*)node
->Data();
438 if (menuitem
->IsSubMenu())
439 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
444 void wxFrame::SetMenuBar( wxMenuBar
*menuBar
)
446 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
447 wxASSERT_MSG( (m_wxwindow
!= NULL
), "invalid frame" );
448 wxASSERT_MSG( (m_mainWindow
!= NULL
), "invalid frame" );
450 m_frameMenuBar
= menuBar
;
454 wxNode
*node
= m_frameMenuBar
->m_menus
.First();
457 wxMenu
*menu
= (wxMenu
*)node
->Data();
458 SetInvokingWindow( menu
, this );
462 if (m_frameMenuBar
->m_parent
!= this)
464 m_frameMenuBar
->m_parent
= this;
465 gtk_myfixed_put( GTK_MYFIXED(m_mainWindow
),
466 m_frameMenuBar
->m_widget
, m_frameMenuBar
->m_x
, m_frameMenuBar
->m_y
);
471 wxMenuBar
*wxFrame::GetMenuBar(void) const
473 return m_frameMenuBar
;
476 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
478 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
480 wxCHECK_MSG( m_frameToolBar
== NULL
, FALSE
, "recreating toolbar in wxFrame" );
482 m_addPrivateChild
= TRUE
;
483 m_frameToolBar
= OnCreateToolBar( style
, id
, name
);
484 m_addPrivateChild
= FALSE
;
486 return m_frameToolBar
;
489 wxToolBar
* wxFrame::OnCreateToolBar( long style
, wxWindowID id
, const wxString
& name
)
491 return new wxToolBar( this, id
, wxDefaultPosition
, wxDefaultSize
, style
, name
);
494 wxToolBar
*wxFrame::GetToolBar(void) const
496 return m_frameToolBar
;
499 wxStatusBar
* wxFrame::CreateStatusBar( int number
, long style
, wxWindowID id
, const wxString
& name
)
501 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
503 wxCHECK_MSG( m_frameStatusBar
== NULL
, FALSE
, "recreating status bar in wxFrame" );
505 m_frameStatusBar
= OnCreateStatusBar( number
, style
, id
, name
);
507 return m_frameStatusBar
;
510 wxStatusBar
*wxFrame::OnCreateStatusBar( int number
, long style
, wxWindowID id
, const wxString
& name
)
512 wxStatusBar
*statusBar
= (wxStatusBar
*) NULL
;
514 statusBar
= new wxStatusBar(this, id
, wxPoint(0, 0), wxSize(100, 20), style
, name
);
516 // Set the height according to the font and the border size
517 wxClientDC
dc(statusBar
);
518 dc
.SetFont( *statusBar
->GetFont() );
521 dc
.GetTextExtent( "X", &x
, &y
);
523 int height
= (int)( (y
* 1.1) + 2* statusBar
->GetBorderY());
525 statusBar
->SetSize( -1, -1, 100, height
);
527 statusBar
->SetFieldsCount( number
);
531 void wxFrame::SetStatusText(const wxString
& text
, int number
)
533 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
535 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set text for" );
537 m_frameStatusBar
->SetStatusText(text
, number
);
540 void wxFrame::SetStatusWidths(int n
, const int widths_field
[] )
542 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
544 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set widths for" );
546 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
549 wxStatusBar
*wxFrame::GetStatusBar(void) const
551 return m_frameStatusBar
;
554 void wxFrame::SetTitle( const wxString
&title
)
556 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
559 if (m_title
.IsNull()) m_title
= "";
560 gtk_window_set_title( GTK_WINDOW(m_widget
), title
);
563 void wxFrame::SetIcon( const wxIcon
&icon
)
565 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
568 if (!icon
.Ok()) return;
570 wxMask
*mask
= icon
.GetMask();
571 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
572 if (mask
) bm
= mask
->GetBitmap();
574 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);