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"
22 #include "wx/dcclient.h"
23 #include "wx/gtk/win_gtk.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 const int wxMENU_HEIGHT
= 28;
30 const int wxSTATUS_HEIGHT
= 25;
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern wxList wxTopLevelWindows
;
37 extern wxList wxPendingDelete
;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 static void gtk_frame_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxFrame
*win
)
45 if (!win
->HasVMT()) return;
48 printf( "OnFrameResize from " );
49 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
50 printf( win->GetClassInfo()->GetClassName() );
54 win
->GtkOnSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 static gint
gtk_frame_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxFrame
*win
)
64 printf( "OnDelete from " );
65 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
66 printf( win->GetClassInfo()->GetClassName() );
75 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
79 static gint
gtk_frame_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxFrame
*win
)
81 if (!win
->HasVMT()) return FALSE
;
89 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
93 BEGIN_EVENT_TABLE(wxFrame
, wxWindow
)
94 EVT_SIZE(wxFrame::OnSize
)
95 EVT_CLOSE(wxFrame::OnCloseWindow
)
96 EVT_IDLE(wxFrame::OnIdle
)
99 IMPLEMENT_DYNAMIC_CLASS(wxFrame
,wxWindow
)
103 m_frameMenuBar
= (wxMenuBar
*) NULL
;
104 m_frameStatusBar
= (wxStatusBar
*) NULL
;
105 m_frameToolBar
= (wxToolBar
*) NULL
;
107 m_addPrivateChild
= FALSE
;
108 m_wxwindow
= (GtkWidget
*) NULL
;
109 m_mainWindow
= (GtkWidget
*) NULL
;
110 wxTopLevelWindows
.Insert( this );
113 wxFrame::wxFrame( wxWindow
*parent
, wxWindowID id
, const wxString
&title
,
114 const wxPoint
&pos
, const wxSize
&size
,
115 long style
, const wxString
&name
)
117 m_frameMenuBar
= (wxMenuBar
*) NULL
;
118 m_frameStatusBar
= (wxStatusBar
*) NULL
;
119 m_frameToolBar
= (wxToolBar
*) NULL
;
121 m_addPrivateChild
= FALSE
;
122 m_wxwindow
= (GtkWidget
*) NULL
;
123 m_mainWindow
= (GtkWidget
*) NULL
;
124 Create( parent
, id
, title
, pos
, size
, style
, name
);
125 wxTopLevelWindows
.Insert( this );
128 bool wxFrame::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&title
,
129 const wxPoint
&pos
, const wxSize
&size
,
130 long style
, const wxString
&name
)
132 m_needParent
= FALSE
;
134 PreCreation( parent
, id
, pos
, size
, style
, name
);
138 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
139 if ((size
.x
!= -1) && (size
.y
!= -1))
140 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
141 if ((pos
.x
!= -1) && (pos
.y
!= -1))
142 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
144 gtk_window_set_title( GTK_WINDOW(m_widget
), title
);
145 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
147 gtk_widget_set( m_widget
, "GtkWindow::allow_shrink", TRUE
, NULL
);
149 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
150 GTK_SIGNAL_FUNC(gtk_frame_delete_callback
), (gpointer
)this );
152 m_mainWindow
= gtk_myfixed_new();
153 gtk_widget_show( m_mainWindow
);
154 GTK_WIDGET_UNSET_FLAGS( m_mainWindow
, GTK_CAN_FOCUS
);
156 gtk_container_add( GTK_CONTAINER(m_widget
), m_mainWindow
);
157 gtk_widget_set_uposition( m_mainWindow
, 0, 0 );
159 m_wxwindow
= gtk_myfixed_new();
160 gtk_widget_show( m_wxwindow
);
161 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
163 gtk_container_add( GTK_CONTAINER(m_mainWindow
), m_wxwindow
);
165 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
166 GTK_SIGNAL_FUNC(gtk_frame_size_callback
), (gpointer
)this );
168 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
169 GTK_SIGNAL_FUNC(gtk_frame_configure_callback
), (gpointer
)this );
173 gtk_widget_realize( m_mainWindow
);
180 if (m_frameMenuBar
) delete m_frameMenuBar
;
181 if (m_frameStatusBar
) delete m_frameStatusBar
;
182 if (m_frameToolBar
) delete m_frameToolBar
;
184 // if (m_mainWindow) gtk_widget_destroy( m_mainWindow );
186 wxTopLevelWindows
.DeleteObject( this );
187 if (wxTopLevelWindows
.Number() == 0) wxTheApp
->ExitMainLoop();
190 bool wxFrame::Show( bool show
)
192 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
196 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
198 ProcessEvent( event
);
200 return wxWindow::Show( show
);
203 void wxFrame::Enable( bool enable
)
205 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
207 if (!m_mainWindow
) return;
209 wxWindow::Enable( enable
);
210 gtk_widget_set_sensitive( m_mainWindow
, enable
);
213 void wxFrame::OnCloseWindow( wxCloseEvent
&event
)
215 if (GetEventHandler()->OnClose() || event
.GetForce()) this->Destroy();
218 bool wxFrame::Destroy()
220 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
222 if (!wxPendingDelete
.Member(this))
223 wxPendingDelete
.Append(this);
228 void wxFrame::ImplementSetPosition(void)
230 if ((m_x
!= -1) || (m_y
!= -1))
231 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
234 void wxFrame::Centre( int direction
)
236 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
238 if (direction
& wxHORIZONTAL
== wxHORIZONTAL
) m_x
= (gdk_screen_width () - m_width
) / 2;
239 if (direction
& wxVERTICAL
== wxVERTICAL
) m_y
= (gdk_screen_height () - m_height
) / 2;
240 ImplementSetPosition();
243 void wxFrame::GetClientSize( int *width
, int *height
) const
245 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
247 wxWindow::GetClientSize( width
, height
);
250 if (m_frameMenuBar
) (*height
) -= wxMENU_HEIGHT
;
251 if (m_frameStatusBar
) (*height
) -= wxSTATUS_HEIGHT
;
255 m_frameToolBar
->GetSize( (int *) NULL
, &y
);
261 void wxFrame::SetClientSize( int const width
, int const height
)
263 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
266 if (m_frameMenuBar
) h
+= wxMENU_HEIGHT
;
267 if (m_frameStatusBar
) h
+= wxSTATUS_HEIGHT
;
271 m_frameToolBar
->GetSize( (int *) NULL
, &y
);
274 wxWindow::SetClientSize( width
, h
);
277 void wxFrame::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
279 // due to a bug in gtk, x,y are always 0
283 if ((m_height
== height
) && (m_width
== width
) &&
285 if (!m_mainWindow
) return;
286 if (!m_wxwindow
) return;
290 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
291 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
292 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_minWidth
;
293 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_minHeight
;
295 gtk_widget_set_usize( m_widget
, width
, height
);
299 int main_height
= height
;
300 int main_width
= width
;
302 // This emulates Windows behaviour:
303 // The menu bar is part of the main window, but the status bar
304 // is on the implementation side in the client area. The
305 // function GetClientSize returns the size of the client area
306 // minus the status bar height. Under wxGTK, the main window
307 // is represented by m_mainWindow. The menubar is inserted
308 // into m_mainWindow whereas the statusbar is insertes into
309 // m_wxwindow just like any other window.
312 // gtk_widget_set_usize( m_mainWindow, width, height );
316 main_y
= wxMENU_HEIGHT
;
317 main_height
-= wxMENU_HEIGHT
;
320 int toolbar_height
= 0;
321 if (m_frameToolBar
) m_frameToolBar
->GetSize( (int *) NULL
, &toolbar_height
);
323 main_y
+= toolbar_height
;
324 main_height
-= toolbar_height
;
326 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow
), m_wxwindow
, main_x
, main_y
);
327 gtk_widget_set_usize( m_wxwindow
, main_width
, main_height
);
331 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow
), m_frameMenuBar
->m_widget
, 1, 1 );
332 gtk_widget_set_usize( m_frameMenuBar
->m_widget
, width
-2, wxMENU_HEIGHT
-2 );
337 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow
), m_frameToolBar
->m_widget
, 1, wxMENU_HEIGHT
);
338 gtk_widget_set_usize( m_frameToolBar
->m_widget
, width
-2, toolbar_height
);
341 if (m_frameStatusBar
)
343 m_frameStatusBar
->SetSize( 0, main_height
-wxSTATUS_HEIGHT
, width
, wxSTATUS_HEIGHT
);
348 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
349 event
.SetEventObject( this );
350 ProcessEvent( event
);
353 void wxFrame::OnSize( wxSizeEvent
&WXUNUSED(event
) )
355 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
357 if ( GetAutoLayout() )
360 // no child: go out !
361 if (!GetChildren()->First())
364 // do we have exactly one child?
365 wxWindow
*child
= (wxWindow
*) NULL
;
366 for(wxNode
*node
= GetChildren()->First(); node
; node
= node
->Next())
368 wxWindow
*win
= (wxWindow
*)node
->Data();
369 if (!IS_KIND_OF(win
,wxFrame
) && !IS_KIND_OF(win
,wxDialog
)
370 #if 0 // not in m_children anyway
371 && (win
!= m_frameMenuBar
) &&
372 (win
!= m_frameToolBar
) &&
373 (win
!= m_frameStatusBar
)
377 if ( child
) // it's the second one: do nothing
384 // yes: set it's size to fill all the frame
385 int client_x
, client_y
;
386 GetClientSize(&client_x
, &client_y
);
387 child
->SetSize( 1, 1, client_x
-2, client_y
);
391 void wxFrame::AddChild( wxWindow
*child
)
393 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
394 wxASSERT_MSG( (m_wxwindow
!= NULL
), "invalid frame" );
395 wxASSERT_MSG( (m_mainWindow
!= NULL
), "invalid frame" );
396 wxASSERT_MSG( (child
!= NULL
), "invalid child" );
397 wxASSERT_MSG( (child
->m_widget
!= NULL
), "invalid child" );
399 // wxFrame and wxDialog as children aren't placed into the parents
401 if (IS_KIND_OF(child
,wxMDIChildFrame
)) wxFAIL_MSG( "wxFrame::AddChild error.\n" );
403 if ( IS_KIND_OF(child
,wxFrame
) || IS_KIND_OF(child
,wxDialog
))
405 m_children
.Append( child
);
407 if ((child
->m_x
!= -1) && (child
->m_y
!= -1))
408 gtk_widget_set_uposition( child
->m_widget
, child
->m_x
, child
->m_y
);
413 if (m_addPrivateChild
)
415 gtk_myfixed_put( GTK_MYFIXED(m_mainWindow
), child
->m_widget
, child
->m_x
, child
->m_y
);
417 gtk_widget_set_usize( child
->m_widget
, child
->m_width
, child
->m_height
);
421 m_children
.Append( child
);
424 gtk_myfixed_put( GTK_MYFIXED(m_wxwindow
), child
->m_widget
, child
->m_x
, child
->m_y
);
426 gtk_widget_set_usize( child
->m_widget
, child
->m_width
, child
->m_height
);
430 static void SetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
432 menu
->SetInvokingWindow( win
);
433 wxNode
*node
= menu
->m_items
.First();
436 wxMenuItem
*menuitem
= (wxMenuItem
*)node
->Data();
437 if (menuitem
->IsSubMenu())
438 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
443 void wxFrame::SetMenuBar( wxMenuBar
*menuBar
)
445 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
446 wxASSERT_MSG( (m_wxwindow
!= NULL
), "invalid frame" );
447 wxASSERT_MSG( (m_mainWindow
!= NULL
), "invalid frame" );
449 m_frameMenuBar
= menuBar
;
453 wxNode
*node
= m_frameMenuBar
->m_menus
.First();
456 wxMenu
*menu
= (wxMenu
*)node
->Data();
457 SetInvokingWindow( menu
, this );
461 if (m_frameMenuBar
->m_parent
!= this)
463 m_frameMenuBar
->m_parent
= this;
464 gtk_myfixed_put( GTK_MYFIXED(m_mainWindow
),
465 m_frameMenuBar
->m_widget
, m_frameMenuBar
->m_x
, m_frameMenuBar
->m_y
);
470 wxMenuBar
*wxFrame::GetMenuBar(void) const
472 return m_frameMenuBar
;
475 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
477 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
479 wxCHECK_MSG( m_frameToolBar
== NULL
, FALSE
, "recreating toolbar in wxFrame" );
481 m_addPrivateChild
= TRUE
;
482 m_frameToolBar
= OnCreateToolBar( style
, id
, name
);
483 m_addPrivateChild
= FALSE
;
485 return m_frameToolBar
;
488 wxToolBar
* wxFrame::OnCreateToolBar( long style
, wxWindowID id
, const wxString
& name
)
490 return new wxToolBar( this, id
, wxDefaultPosition
, wxDefaultSize
, style
, name
);
493 wxToolBar
*wxFrame::GetToolBar(void) const
495 return m_frameToolBar
;
498 wxStatusBar
* wxFrame::CreateStatusBar( int number
, long style
, wxWindowID id
, const wxString
& name
)
500 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
502 wxCHECK_MSG( m_frameStatusBar
== NULL
, FALSE
, "recreating status bar in wxFrame" );
504 m_frameStatusBar
= OnCreateStatusBar( number
, style
, id
, name
);
506 return m_frameStatusBar
;
509 wxStatusBar
*wxFrame::OnCreateStatusBar( int number
, long style
, wxWindowID id
, const wxString
& name
)
511 wxStatusBar
*statusBar
= (wxStatusBar
*) NULL
;
513 statusBar
= new wxStatusBar(this, id
, wxPoint(0, 0), wxSize(100, 20), style
, name
);
515 // Set the height according to the font and the border size
516 wxClientDC
dc(statusBar
);
517 dc
.SetFont( *statusBar
->GetFont() );
520 dc
.GetTextExtent( "X", &x
, &y
);
522 int height
= (int)( (y
* 1.1) + 2* statusBar
->GetBorderY());
524 statusBar
->SetSize( -1, -1, 100, height
);
526 statusBar
->SetFieldsCount( number
);
530 void wxFrame::SetStatusText(const wxString
& text
, int number
)
532 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
534 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set text for" );
536 m_frameStatusBar
->SetStatusText(text
, number
);
539 void wxFrame::SetStatusWidths(int n
, const int widths_field
[] )
541 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
543 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set widths for" );
545 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
548 wxStatusBar
*wxFrame::GetStatusBar(void) const
550 return m_frameStatusBar
;
553 void wxFrame::SetTitle( const wxString
&title
)
555 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
558 if (m_title
.IsNull()) m_title
= "";
559 gtk_window_set_title( GTK_WINDOW(m_widget
), title
);
562 void wxFrame::SetIcon( const wxIcon
&icon
)
564 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
567 if (!icon
.Ok()) return;
569 wxMask
*mask
= icon
.GetMask();
570 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
571 if (mask
) bm
= mask
->GetBitmap();
573 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);