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 const int wxMENU_HEIGHT
= 28;
27 const int wxSTATUS_HEIGHT
= 25;
29 extern wxList wxTopLevelWindows
;
30 extern wxList wxPendingDelete
;
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
39 void gtk_frame_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxFrame
*win
)
41 if (!win
->HasVMT()) return;
44 printf( "OnFrameResize from " );
45 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
46 printf( win->GetClassInfo()->GetClassName() );
50 win
->GtkOnSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
53 //-----------------------------------------------------------------------------
56 bool gtk_frame_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxFrame
*win
)
59 printf( "OnDelete from " );
60 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
61 printf( win->GetClassInfo()->GetClassName() );
70 //-----------------------------------------------------------------------------
73 gint
gtk_frame_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxFrame
*win
)
75 if (!win
->HasVMT()) return FALSE
;
83 //-----------------------------------------------------------------------------
85 BEGIN_EVENT_TABLE(wxFrame
, wxWindow
)
86 EVT_SIZE(wxFrame::OnSize
)
87 EVT_CLOSE(wxFrame::OnCloseWindow
)
88 EVT_IDLE(wxFrame::OnIdle
)
91 IMPLEMENT_DYNAMIC_CLASS(wxFrame
,wxWindow
)
95 m_frameMenuBar
= (wxMenuBar
*) NULL
;
96 m_frameStatusBar
= (wxStatusBar
*) NULL
;
97 m_frameToolBar
= (wxToolBar
*) NULL
;
99 m_addPrivateChild
= FALSE
;
100 m_wxwindow
= (GtkWidget
*) NULL
;
101 m_mainWindow
= (GtkWidget
*) NULL
;
102 wxTopLevelWindows
.Insert( this );
105 wxFrame::wxFrame( wxWindow
*parent
, wxWindowID id
, const wxString
&title
,
106 const wxPoint
&pos
, const wxSize
&size
,
107 long style
, const wxString
&name
)
109 m_frameMenuBar
= (wxMenuBar
*) NULL
;
110 m_frameStatusBar
= (wxStatusBar
*) NULL
;
111 m_frameToolBar
= (wxToolBar
*) NULL
;
113 m_addPrivateChild
= FALSE
;
114 m_wxwindow
= (GtkWidget
*) NULL
;
115 m_mainWindow
= (GtkWidget
*) NULL
;
116 Create( parent
, id
, title
, pos
, size
, style
, name
);
117 wxTopLevelWindows
.Insert( this );
120 bool wxFrame::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&title
,
121 const wxPoint
&pos
, const wxSize
&size
,
122 long style
, const wxString
&name
)
124 m_needParent
= FALSE
;
126 PreCreation( parent
, id
, pos
, size
, style
, name
);
130 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
131 if ((size
.x
!= -1) && (size
.y
!= -1))
132 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
133 if ((pos
.x
!= -1) && (pos
.y
!= -1))
134 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
136 gtk_window_set_title( GTK_WINDOW(m_widget
), title
);
137 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
139 gtk_widget_set( m_widget
, "GtkWindow::allow_shrink", TRUE
, NULL
);
141 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
142 GTK_SIGNAL_FUNC(gtk_frame_delete_callback
), (gpointer
)this );
144 m_mainWindow
= gtk_myfixed_new();
145 gtk_widget_show( m_mainWindow
);
146 GTK_WIDGET_UNSET_FLAGS( m_mainWindow
, GTK_CAN_FOCUS
);
148 gtk_container_add( GTK_CONTAINER(m_widget
), m_mainWindow
);
149 gtk_widget_set_uposition( m_mainWindow
, 0, 0 );
151 m_wxwindow
= gtk_myfixed_new();
152 gtk_widget_show( m_wxwindow
);
153 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
155 gtk_container_add( GTK_CONTAINER(m_mainWindow
), m_wxwindow
);
157 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
158 GTK_SIGNAL_FUNC(gtk_frame_size_callback
), (gpointer
)this );
160 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
161 GTK_SIGNAL_FUNC(gtk_frame_configure_callback
), (gpointer
)this );
165 gtk_widget_realize( m_mainWindow
);
172 if (m_frameMenuBar
) delete m_frameMenuBar
;
173 if (m_frameStatusBar
) delete m_frameStatusBar
;
174 if (m_frameToolBar
) delete m_frameToolBar
;
176 // if (m_mainWindow) gtk_widget_destroy( m_mainWindow );
178 wxTopLevelWindows
.DeleteObject( this );
179 if (wxTopLevelWindows
.Number() == 0) wxTheApp
->ExitMainLoop();
182 bool wxFrame::Show( bool show
)
186 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
188 ProcessEvent( event
);
190 return wxWindow::Show( show
);
193 void wxFrame::Enable( bool enable
)
195 wxWindow::Enable( enable
);
196 gtk_widget_set_sensitive( m_mainWindow
, enable
);
199 void wxFrame::OnCloseWindow( wxCloseEvent
&event
)
201 if ( GetEventHandler()->OnClose() || event
.GetForce())
207 bool wxFrame::Destroy()
209 if (!wxPendingDelete
.Member(this))
210 wxPendingDelete
.Append(this);
215 void wxFrame::ImplementSetPosition(void)
217 if ((m_x
!= -1) || (m_y
!= -1))
218 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
221 void wxFrame::Centre( int direction
)
223 if (direction
& wxHORIZONTAL
== wxHORIZONTAL
) m_x
= (gdk_screen_width () - m_width
) / 2;
224 if (direction
& wxVERTICAL
== wxVERTICAL
) m_y
= (gdk_screen_height () - m_height
) / 2;
225 ImplementSetPosition();
228 void wxFrame::GetClientSize( int *width
, int *height
) const
230 wxWindow::GetClientSize( width
, height
);
233 if (m_frameMenuBar
) (*height
) -= wxMENU_HEIGHT
;
234 if (m_frameStatusBar
) (*height
) -= wxSTATUS_HEIGHT
;
238 m_frameToolBar
->GetSize( (int *) NULL
, &y
);
244 void wxFrame::SetClientSize( int const width
, int const height
)
247 if (m_frameMenuBar
) h
+= wxMENU_HEIGHT
;
248 if (m_frameStatusBar
) h
+= wxSTATUS_HEIGHT
;
252 m_frameToolBar
->GetSize( (int *) NULL
, &y
);
255 wxWindow::SetClientSize( width
, h
);
258 void wxFrame::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
260 // due to a bug in gtk, x,y are always 0
264 if ((m_height
== height
) && (m_width
== width
) &&
266 if (!m_mainWindow
) return;
267 if (!m_wxwindow
) return;
273 //gtk_widget_set_usize( m_widget, width, height );
277 int main_height
= height
;
278 int main_width
= width
;
280 // This emulates Windows behaviour:
281 // The menu bar is part of the main window, but the status bar
282 // is on the implementation side in the client area. The
283 // function GetClientSize returns the size of the client area
284 // minus the status bar height. Under wxGTK, the main window
285 // is represented by m_mainWindow. The menubar is inserted
286 // into m_mainWindow whereas the statusbar is insertes into
287 // m_wxwindow just like any other window.
290 // gtk_widget_set_usize( m_mainWindow, width, height );
294 main_y
= wxMENU_HEIGHT
;
295 main_height
-= wxMENU_HEIGHT
;
298 int toolbar_height
= 0;
299 if (m_frameToolBar
) m_frameToolBar
->GetSize( (int *) NULL
, &toolbar_height
);
301 main_y
+= toolbar_height
;
302 main_height
-= toolbar_height
;
304 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow
), m_wxwindow
, main_x
, main_y
);
305 gtk_widget_set_usize( m_wxwindow
, main_width
, main_height
);
309 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow
), m_frameMenuBar
->m_widget
, 1, 1 );
310 gtk_widget_set_usize( m_frameMenuBar
->m_widget
, width
-2, wxMENU_HEIGHT
-2 );
315 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow
), m_frameToolBar
->m_widget
, 1, wxMENU_HEIGHT
);
316 gtk_widget_set_usize( m_frameToolBar
->m_widget
, width
-2, toolbar_height
);
319 if (m_frameStatusBar
)
321 m_frameStatusBar
->SetSize( 0, main_height
-wxSTATUS_HEIGHT
, width
, wxSTATUS_HEIGHT
);
326 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
327 event
.SetEventObject( this );
328 ProcessEvent( event
);
331 void wxFrame::OnSize( wxSizeEvent
&WXUNUSED(event
) )
333 if ( GetAutoLayout() )
336 // no child: go out !
337 if (!GetChildren()->First())
340 // do we have exactly one child?
341 wxWindow
*child
= (wxWindow
*) NULL
;
342 for(wxNode
*node
= GetChildren()->First(); node
; node
= node
->Next())
344 wxWindow
*win
= (wxWindow
*)node
->Data();
345 if (!IS_KIND_OF(win
,wxFrame
) && !IS_KIND_OF(win
,wxDialog
)
346 #if 0 // not in m_children anyway
347 && (win
!= m_frameMenuBar
) &&
348 (win
!= m_frameToolBar
) &&
349 (win
!= m_frameStatusBar
)
353 if ( child
) // it's the second one: do nothing
360 // yes: set it's size to fill all the frame
361 int client_x
, client_y
;
362 GetClientSize(&client_x
, &client_y
);
363 child
->SetSize( 1, 1, client_x
-2, client_y
);
367 void wxFrame::AddChild( wxWindow
*child
)
369 // wxFrame and wxDialog as children aren't placed into the parents
371 if (IS_KIND_OF(child
,wxMDIChildFrame
)) printf( "wxFrame::AddChild error.\n" );
373 if ( IS_KIND_OF(child
,wxFrame
) || IS_KIND_OF(child
,wxDialog
))
375 m_children
.Append( child
);
377 if ((child
->m_x
!= -1) && (child
->m_y
!= -1))
378 gtk_widget_set_uposition( child
->m_widget
, child
->m_x
, child
->m_y
);
383 if (m_addPrivateChild
)
385 gtk_myfixed_put( GTK_MYFIXED(m_mainWindow
), child
->m_widget
, child
->m_x
, child
->m_y
);
387 gtk_widget_set_usize( child
->m_widget
, child
->m_width
, child
->m_height
);
391 m_children
.Append( child
);
394 gtk_myfixed_put( GTK_MYFIXED(m_wxwindow
), child
->m_widget
, child
->m_x
, child
->m_y
);
396 gtk_widget_set_usize( child
->m_widget
, child
->m_width
, child
->m_height
);
400 static void SetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
402 menu
->SetInvokingWindow( win
);
403 wxNode
*node
= menu
->m_items
.First();
406 wxMenuItem
*menuitem
= (wxMenuItem
*)node
->Data();
407 if (menuitem
->IsSubMenu())
408 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
413 void wxFrame::SetMenuBar( wxMenuBar
*menuBar
)
415 m_frameMenuBar
= menuBar
;
419 wxNode
*node
= m_frameMenuBar
->m_menus
.First();
422 wxMenu
*menu
= (wxMenu
*)node
->Data();
423 SetInvokingWindow( menu
, this );
427 if (m_frameMenuBar
->m_parent
!= this)
429 m_frameMenuBar
->m_parent
= this;
430 gtk_myfixed_put( GTK_MYFIXED(m_mainWindow
),
431 m_frameMenuBar
->m_widget
, m_frameMenuBar
->m_x
, m_frameMenuBar
->m_y
);
436 wxMenuBar
*wxFrame::GetMenuBar(void) const
438 return m_frameMenuBar
;
441 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
443 wxCHECK_MSG( m_frameToolBar
== NULL
, FALSE
, "recreating toolbar in wxFrame" );
445 m_addPrivateChild
= TRUE
;
446 m_frameToolBar
= OnCreateToolBar( style
, id
, name
);
447 m_addPrivateChild
= FALSE
;
449 return m_frameToolBar
;
452 wxToolBar
* wxFrame::OnCreateToolBar( long style
, wxWindowID id
, const wxString
& name
)
454 return new wxToolBar( this, id
, wxDefaultPosition
, wxDefaultSize
, style
, name
);
457 wxToolBar
*wxFrame::GetToolBar(void) const
459 return m_frameToolBar
;
462 wxStatusBar
* wxFrame::CreateStatusBar( int number
, long style
, wxWindowID id
, const wxString
& name
)
464 wxCHECK_MSG( m_frameStatusBar
== NULL
, FALSE
, "recreating status bar in wxFrame" );
466 m_frameStatusBar
= OnCreateStatusBar( number
, style
, id
, name
);
468 return m_frameStatusBar
;
471 wxStatusBar
*wxFrame::OnCreateStatusBar( int number
, long style
, wxWindowID id
, const wxString
& name
)
473 wxStatusBar
*statusBar
= (wxStatusBar
*) NULL
;
475 statusBar
= new wxStatusBar(this, id
, wxPoint(0, 0), wxSize(100, 20), style
, name
);
477 // Set the height according to the font and the border size
478 wxClientDC
dc(statusBar
);
479 dc
.SetFont( *statusBar
->GetFont() );
482 dc
.GetTextExtent( "X", &x
, &y
);
484 int height
= (int)( (y
* 1.1) + 2* statusBar
->GetBorderY());
486 statusBar
->SetSize( -1, -1, 100, height
);
488 statusBar
->SetFieldsCount( number
);
492 void wxFrame::SetStatusText(const wxString
& text
, int number
)
494 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set text for" );
496 m_frameStatusBar
->SetStatusText(text
, number
);
499 void wxFrame::SetStatusWidths(int n
, const int widths_field
[] )
501 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set widths for" );
503 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
506 wxStatusBar
*wxFrame::GetStatusBar(void) const
508 return m_frameStatusBar
;
511 void wxFrame::SetTitle( const wxString
&title
)
514 if (m_title
.IsNull()) m_title
= "";
515 gtk_window_set_title( GTK_WINDOW(m_widget
), title
);
518 void wxFrame::SetSizeHints(int minW
, int minH
,
519 int WXUNUSED(maxW
), int WXUNUSED(maxH
),
522 // gdk_window_set_hints alone doesn't really prevent the user from shrinking
523 // the window to the size smaller than (minW, minH)
524 gtk_widget_set_usize( GTK_WIDGET(m_widget
), minW
, minH
);
527 gdk_window_set_hints( m_widget->window, -1, -1,
528 minW, minH, maxW, maxH,
529 GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE );
533 void wxFrame::SetIcon( const wxIcon
&icon
)
536 if (!icon
.Ok()) return;
538 wxMask
*mask
= icon
.GetMask();
539 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
540 if (mask
) bm
= mask
->GetBitmap();
542 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);