1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "frame.h"
17 #include "wx/statusbr.h"
18 #include "wx/toolbar.h"
19 #include "wx/menuitem.h"
21 #include "wx/dcclient.h"
22 #include "wx/dialog.h"
23 #include "wx/settings.h"
26 extern wxList wxModelessWindows
;
27 extern wxList wxPendingDelete
;
29 BEGIN_EVENT_TABLE(wxFrame
, wxWindow
)
30 EVT_SIZE(wxFrame::OnSize
)
31 EVT_ACTIVATE(wxFrame::OnActivate
)
32 EVT_MENU_HIGHLIGHT_ALL(wxFrame::OnMenuHighlight
)
33 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
)
34 EVT_IDLE(wxFrame::OnIdle
)
35 EVT_CLOSE(wxFrame::OnCloseWindow
)
38 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxWindow
)
40 #if wxUSE_NATIVE_STATUSBAR
41 bool wxFrame::m_useNativeStatusBar
= TRUE
;
43 bool wxFrame::m_useNativeStatusBar
= FALSE
;
48 m_frameToolBar
= NULL
;
49 m_frameMenuBar
= NULL
;
50 m_frameStatusBar
= NULL
;
52 m_windowParent
= NULL
;
56 bool wxFrame::Create(wxWindow
*parent
,
58 const wxString
& title
,
65 wxTopLevelWindows
.Append(this);
68 m_windowStyle
= style
;
69 m_frameMenuBar
= NULL
;
70 m_frameToolBar
= NULL
;
71 m_frameStatusBar
= NULL
;
73 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
78 m_windowId
= (int)NewControlId();
80 if (parent
) parent
->AddChild(this);
82 wxModelessWindows
.Append(this);
84 // TODO: create frame.
91 wxTopLevelWindows
.DeleteObject(this);
94 delete m_frameStatusBar
;
96 delete m_frameMenuBar
;
98 /* Check if it's the last top-level window */
100 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
102 wxTheApp
->SetTopWindow(NULL
);
104 if (wxTheApp
->GetExitOnFrameDelete())
106 // TODO signal to the app that we're going to close
110 wxModelessWindows
.DeleteObject(this);
113 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
114 void wxFrame::GetClientSize(int *x
, int *y
) const
119 // Set the client size (i.e. leave the calculation of borders etc.
121 void wxFrame::SetClientSize(int width
, int height
)
126 void wxFrame::GetSize(int *width
, int *height
) const
131 void wxFrame::GetPosition(int *x
, int *y
) const
136 void wxFrame::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
141 bool wxFrame::Show(bool show
)
147 void wxFrame::Iconize(bool iconize
)
152 // Equivalent to maximize/restore in Windows
153 void wxFrame::Maximize(bool maximize
)
158 bool wxFrame::IsIconized() const
164 // Is the frame maximized?
165 bool wxFrame::IsMaximized(void) const
171 void wxFrame::SetTitle(const wxString
& title
)
176 wxString
wxFrame::GetTitle() const
182 void wxFrame::SetIcon(const wxIcon
& icon
)
188 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
, long style
, wxWindowID id
,
189 const wxString
& name
)
191 wxStatusBar
*statusBar
= NULL
;
193 statusBar
= new wxStatusBar(this, id
, wxPoint(0, 0), wxSize(100, 20),
196 // Set the height according to the font and the border size
197 wxClientDC
dc(statusBar
);
198 dc
.SetFont(statusBar
->GetFont());
201 dc
.GetTextExtent("X", &x
, &y
);
203 int height
= (int)( (y
* 1.1) + 2* statusBar
->GetBorderY());
205 statusBar
->SetSize(-1, -1, 100, height
);
207 statusBar
->SetFieldsCount(number
);
211 wxStatusBar
* wxFrame::CreateStatusBar(int number
, long style
, wxWindowID id
,
212 const wxString
& name
)
214 // Calling CreateStatusBar twice is an error.
215 wxCHECK_MSG( m_frameStatusBar
== NULL
, FALSE
,
216 "recreating status bar in wxFrame" );
218 m_frameStatusBar
= OnCreateStatusBar(number
, style
, id
,
220 if ( m_frameStatusBar
)
223 return m_frameStatusBar
;
229 void wxFrame::SetStatusText(const wxString
& text
, int number
)
231 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set text for" );
233 m_frameStatusBar
->SetStatusText(text
, number
);
236 void wxFrame::SetStatusWidths(int n
, const int widths_field
[])
238 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set widths for" );
240 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
244 void wxFrame::PositionStatusBar()
247 GetClientSize(&w
, &h
);
249 m_frameStatusBar
->GetSize(&sw
, &sh
);
251 // Since we wish the status bar to be directly under the client area,
252 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
253 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
256 void wxFrame::SetMenuBar(wxMenuBar
*menuBar
)
260 m_frameMenuBar
= NULL
;
264 m_frameMenuBar
= menuBar
;
271 // Work out max. size
272 wxNode
*node
= GetChildren().First();
277 // Find a child that's a subwindow, but not a dialog box.
278 wxWindow
*win
= (wxWindow
*)node
->Data();
280 if (!win
->IsKindOf(CLASSINFO(wxFrame
)) &&
281 !win
->IsKindOf(CLASSINFO(wxDialog
)))
285 win
->GetSize(&width
, &height
);
286 win
->GetPosition(&x
, &y
);
288 if ((x
+ width
) > max_width
)
289 max_width
= x
+ width
;
290 if ((y
+ height
) > max_height
)
291 max_height
= y
+ height
;
295 SetClientSize(max_width
, max_height
);
298 // Responds to colour changes, and passes event on to children.
299 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
301 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
304 if ( m_frameStatusBar
)
306 wxSysColourChangedEvent event2
;
307 event2
.SetEventObject( m_frameStatusBar
);
308 m_frameStatusBar
->ProcessEvent(event2
);
311 // Propagate the event to the non-top-level children
312 wxWindow::OnSysColourChanged(event
);
315 // Default resizing behaviour - if only ONE subwindow,
316 // resize to client rectangle size
317 void wxFrame::OnSize(wxSizeEvent
& event
)
319 // if we're using constraints - do use them
320 #if wxUSE_CONSTRAINTS
321 if ( GetAutoLayout() ) {
327 // do we have _exactly_ one child?
328 wxWindow
*child
= NULL
;
329 for ( wxNode
*node
= GetChildren().First(); node
; node
= node
->Next() )
331 wxWindow
*win
= (wxWindow
*)node
->Data();
332 if ( !win
->IsKindOf(CLASSINFO(wxFrame
)) &&
333 !win
->IsKindOf(CLASSINFO(wxDialog
)) &&
334 (win
!= GetStatusBar()) &&
335 (win
!= GetToolBar()) )
338 return; // it's our second subwindow - nothing to do
344 // we have exactly one child - set it's size to fill the whole frame
345 int clientW
, clientH
;
346 GetClientSize(&clientW
, &clientH
);
351 child
->SetSize(x
, y
, clientW
, clientH
);
355 // Default activation behaviour - set the focus for the first child
357 void wxFrame::OnActivate(wxActivateEvent
& event
)
359 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
361 // Find a child that's a subwindow, but not a dialog box.
362 wxWindow
*child
= (wxWindow
*)node
->Data();
363 if (!child
->IsKindOf(CLASSINFO(wxFrame
)) &&
364 !child
->IsKindOf(CLASSINFO(wxDialog
)))
372 // The default implementation for the close window event.
374 void wxFrame::OnCloseWindow(wxCloseEvent
& event
)
379 // Destroy the window (delayed, if a managed window)
380 bool wxFrame::Destroy()
382 if (!wxPendingDelete
.Member(this))
383 wxPendingDelete
.Append(this);
387 // Default menu selection behaviour - display a help string
388 void wxFrame::OnMenuHighlight(wxMenuEvent
& event
)
392 if (event
.GetMenuId() == -1)
396 wxMenuBar
*menuBar
= GetMenuBar();
399 wxString
helpString(menuBar
->GetHelpString(event
.GetMenuId()));
400 if (helpString
!= "")
401 SetStatusText(helpString
);
407 wxMenuBar
*wxFrame::GetMenuBar() const
409 return m_frameMenuBar
;
412 void wxFrame::Centre(int direction
)
414 int display_width
, display_height
, width
, height
, x
, y
;
415 wxDisplaySize(&display_width
, &display_height
);
417 GetSize(&width
, &height
);
420 if (direction
& wxHORIZONTAL
)
421 x
= (int)((display_width
- width
)/2);
422 if (direction
& wxVERTICAL
)
423 y
= (int)((display_height
- height
)/2);
425 SetSize(x
, y
, width
, height
);
428 // Call this to simulate a menu command
429 void wxFrame::Command(int id
)
434 void wxFrame::ProcessCommand(int id
)
436 wxCommandEvent
commandEvent(wxEVENT_TYPE_MENU_COMMAND
, id
);
437 commandEvent
.SetInt( id
);
438 commandEvent
.SetEventObject( this );
440 wxMenuBar
*bar
= GetMenuBar() ;
444 /* TODO: check the menu item if required
445 wxMenuItem *item = bar->FindItemForId(id) ;
446 if (item && item->IsCheckable())
448 bar->Check(id,!bar->Checked(id)) ;
452 wxEvtHandler
* evtHandler
= GetEventHandler();
453 evtHandler
->ProcessEvent(commandEvent
);
456 // Checks if there is a toolbar, and returns the first free client position
457 wxPoint
wxFrame::GetClientAreaOrigin() const
463 GetToolBar()->GetSize(& w
, & h
);
465 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
477 void wxFrame::ScreenToClient(int *x
, int *y
) const
479 wxWindow::ScreenToClient(x
, y
);
481 // We may be faking the client origin.
482 // So a window that's really at (0, 30) may appear
483 // (to wxWin apps) to be at (0, 0).
484 wxPoint
pt(GetClientAreaOrigin());
489 void wxFrame::ClientToScreen(int *x
, int *y
) const
491 // We may be faking the client origin.
492 // So a window that's really at (0, 30) may appear
493 // (to wxWin apps) to be at (0, 0).
494 wxPoint
pt1(GetClientAreaOrigin());
498 wxWindow::ClientToScreen(x
, y
);
501 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
503 wxCHECK_MSG( m_frameToolBar
== NULL
, FALSE
,
504 "recreating toolbar in wxFrame" );
506 wxToolBar
* toolBar
= OnCreateToolBar(style
, id
, name
);
519 wxToolBar
* wxFrame::OnCreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
521 return new wxToolBar(this, id
, wxDefaultPosition
, wxDefaultSize
, style
, name
);
524 void wxFrame::PositionToolBar()
528 // TODO: we actually need to use the low-level client size, before
529 // the toolbar/status bar were added.
530 // So DEFINITELY replace the line below with something appropriate.
532 GetClientSize(& cw
, &ch
);
534 if ( GetStatusBar() )
536 int statusX
, statusY
;
537 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
544 GetToolBar()->GetSize(& tw
, & th
);
546 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
548 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
549 // means, pretend we don't have toolbar/status bar, so we
550 // have the original client size.
551 GetToolBar()->SetSize(0, 0, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
555 // Use the 'real' position
556 GetToolBar()->SetSize(0, 0, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);