1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "frame.h"
18 #include "wx/statusbr.h"
19 #include "wx/toolbar.h"
20 #include "wx/menuitem.h"
22 #include "wx/dcclient.h"
23 #include "wx/dialog.h"
24 #include "wx/settings.h"
27 extern wxList wxModelessWindows
;
28 extern wxList wxPendingDelete
;
30 #if !USE_SHARED_LIBRARY
31 BEGIN_EVENT_TABLE(wxFrame
, wxWindow
)
32 EVT_SIZE(wxFrame::OnSize
)
33 EVT_ACTIVATE(wxFrame::OnActivate
)
34 EVT_MENU_HIGHLIGHT_ALL(wxFrame::OnMenuHighlight
)
35 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
)
36 EVT_IDLE(wxFrame::OnIdle
)
37 EVT_CLOSE(wxFrame::OnCloseWindow
)
40 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxWindow
)
43 #if wxUSE_NATIVE_STATUSBAR
44 bool wxFrame::m_useNativeStatusBar
= TRUE
;
46 bool wxFrame::m_useNativeStatusBar
= FALSE
;
51 m_frameMenuBar
= NULL
;
52 m_frameStatusBar
= NULL
;
56 m_frameToolBar
= NULL
;
59 bool wxFrame::Create(wxWindow
*parent
,
61 const wxString
& title
,
68 wxTopLevelWindows
.Append(this);
71 m_windowStyle
= style
;
72 m_frameMenuBar
= NULL
;
73 m_frameToolBar
= NULL
;
74 m_frameStatusBar
= NULL
;
76 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
81 m_windowId
= (int)NewControlId();
83 if (parent
) parent
->AddChild(this);
85 wxModelessWindows
.Append(this);
87 // TODO: create frame.
94 wxTopLevelWindows
.DeleteObject(this);
97 delete m_frameStatusBar
;
99 delete m_frameMenuBar
;
101 /* Check if it's the last top-level window */
103 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
105 wxTheApp
->SetTopWindow(NULL
);
107 if (wxTheApp
->GetExitOnFrameDelete())
109 // TODO signal to the app that we're going to close
113 wxModelessWindows
.DeleteObject(this);
116 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
117 void wxFrame::GetClientSize(int *x
, int *y
) const
122 // Set the client size (i.e. leave the calculation of borders etc.
124 void wxFrame::SetClientSize(int width
, int height
)
129 void wxFrame::GetSize(int *width
, int *height
) const
134 void wxFrame::GetPosition(int *x
, int *y
) const
139 void wxFrame::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
144 bool wxFrame::Show(bool show
)
150 void wxFrame::Iconize(bool iconize
)
155 // Equivalent to maximize/restore in Windows
156 void wxFrame::Maximize(bool maximize
)
161 bool wxFrame::IsIconized() const
167 // Is the frame maximized?
168 bool wxFrame::IsMaximized(void) const
174 void wxFrame::SetTitle(const wxString
& title
)
179 wxString
wxFrame::GetTitle() const
185 void wxFrame::SetIcon(const wxIcon
& icon
)
191 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
, long style
, wxWindowID id
,
192 const wxString
& name
)
194 wxStatusBar
*statusBar
= NULL
;
196 statusBar
= new wxStatusBar(this, id
, wxPoint(0, 0), wxSize(100, 20),
199 // Set the height according to the font and the border size
200 wxClientDC
dc(statusBar
);
201 dc
.SetFont(statusBar
->GetFont());
204 dc
.GetTextExtent("X", &x
, &y
, NULL
, NULL
, NULL
, FALSE
);
206 int height
= (int)( (y
* 1.1) + 2* statusBar
->GetBorderY());
208 statusBar
->SetSize(-1, -1, 100, height
);
210 statusBar
->SetFieldsCount(number
);
214 wxStatusBar
* wxFrame::CreateStatusBar(int number
, long style
, wxWindowID id
,
215 const wxString
& name
)
217 // Calling CreateStatusBar twice is an error.
218 wxCHECK_MSG( m_frameStatusBar
== NULL
, FALSE
,
219 "recreating status bar in wxFrame" );
221 m_frameStatusBar
= OnCreateStatusBar(number
, style
, id
,
223 if ( m_frameStatusBar
)
226 return m_frameStatusBar
;
232 void wxFrame::SetStatusText(const wxString
& text
, int number
)
234 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set text for" );
236 m_frameStatusBar
->SetStatusText(text
, number
);
239 void wxFrame::SetStatusWidths(int n
, const int widths_field
[])
241 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set widths for" );
243 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
247 void wxFrame::PositionStatusBar()
250 GetClientSize(&w
, &h
);
252 m_frameStatusBar
->GetSize(&sw
, &sh
);
254 // Since we wish the status bar to be directly under the client area,
255 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
256 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
259 void wxFrame::SetMenuBar(wxMenuBar
*menuBar
)
263 m_frameMenuBar
= NULL
;
267 m_frameMenuBar
= menuBar
;
274 // Work out max. size
275 wxNode
*node
= GetChildren().First();
280 // Find a child that's a subwindow, but not a dialog box.
281 wxWindow
*win
= (wxWindow
*)node
->Data();
283 if (!win
->IsKindOf(CLASSINFO(wxFrame
)) &&
284 !win
->IsKindOf(CLASSINFO(wxDialog
)))
288 win
->GetSize(&width
, &height
);
289 win
->GetPosition(&x
, &y
);
291 if ((x
+ width
) > max_width
)
292 max_width
= x
+ width
;
293 if ((y
+ height
) > max_height
)
294 max_height
= y
+ height
;
298 SetClientSize(max_width
, max_height
);
301 // Responds to colour changes, and passes event on to children.
302 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
304 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
307 if ( m_frameStatusBar
)
309 wxSysColourChangedEvent event2
;
310 event2
.SetEventObject( m_frameStatusBar
);
311 m_frameStatusBar
->ProcessEvent(event2
);
314 // Propagate the event to the non-top-level children
315 wxWindow::OnSysColourChanged(event
);
318 // Default resizing behaviour - if only ONE subwindow,
319 // resize to client rectangle size
320 void wxFrame::OnSize(wxSizeEvent
& event
)
322 // if we're using constraints - do use them
323 #if wxUSE_CONSTRAINTS
324 if ( GetAutoLayout() ) {
330 // do we have _exactly_ one child?
331 wxWindow
*child
= NULL
;
332 for ( wxNode
*node
= GetChildren().First(); node
; node
= node
->Next() )
334 wxWindow
*win
= (wxWindow
*)node
->Data();
335 if ( !win
->IsKindOf(CLASSINFO(wxFrame
)) &&
336 !win
->IsKindOf(CLASSINFO(wxDialog
)) &&
337 (win
!= GetStatusBar()) &&
338 (win
!= GetToolBar()) )
341 return; // it's our second subwindow - nothing to do
347 // we have exactly one child - set it's size to fill the whole frame
348 int clientW
, clientH
;
349 GetClientSize(&clientW
, &clientH
);
354 child
->SetSize(x
, y
, clientW
, clientH
);
358 // Default activation behaviour - set the focus for the first child
360 void wxFrame::OnActivate(wxActivateEvent
& event
)
362 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
364 // Find a child that's a subwindow, but not a dialog box.
365 wxWindow
*child
= (wxWindow
*)node
->Data();
366 if (!child
->IsKindOf(CLASSINFO(wxFrame
)) &&
367 !child
->IsKindOf(CLASSINFO(wxDialog
)))
375 // The default implementation for the close window event.
377 void wxFrame::OnCloseWindow(wxCloseEvent
& event
)
382 // Destroy the window (delayed, if a managed window)
383 bool wxFrame::Destroy()
385 if (!wxPendingDelete
.Member(this))
386 wxPendingDelete
.Append(this);
390 // Default menu selection behaviour - display a help string
391 void wxFrame::OnMenuHighlight(wxMenuEvent
& event
)
395 if (event
.GetMenuId() == -1)
399 wxMenuBar
*menuBar
= GetMenuBar();
402 wxString
helpString(menuBar
->GetHelpString(event
.GetMenuId()));
403 if (helpString
!= "")
404 SetStatusText(helpString
);
410 wxMenuBar
*wxFrame::GetMenuBar() const
412 return m_frameMenuBar
;
415 void wxFrame::Centre(int direction
)
417 int display_width
, display_height
, width
, height
, x
, y
;
418 wxDisplaySize(&display_width
, &display_height
);
420 GetSize(&width
, &height
);
423 if (direction
& wxHORIZONTAL
)
424 x
= (int)((display_width
- width
)/2);
425 if (direction
& wxVERTICAL
)
426 y
= (int)((display_height
- height
)/2);
428 SetSize(x
, y
, width
, height
);
431 // Call this to simulate a menu command
432 void wxFrame::Command(int id
)
437 void wxFrame::ProcessCommand(int id
)
439 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
440 commandEvent
.SetInt( id
);
441 commandEvent
.SetEventObject( this );
443 wxMenuBar
*bar
= GetMenuBar() ;
447 /* TODO: check the menu item if required
448 wxMenuItem *item = bar->FindItemForId(id) ;
449 if (item && item->IsCheckable())
451 bar->Check(id,!bar->Checked(id)) ;
455 wxEvtHandler
* evtHandler
= GetEventHandler();
456 evtHandler
->ProcessEvent(commandEvent
);
459 // Checks if there is a toolbar, and returns the first free client position
460 wxPoint
wxFrame::GetClientAreaOrigin() const
466 GetToolBar()->GetSize(& w
, & h
);
468 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
480 void wxFrame::ScreenToClient(int *x
, int *y
) const
482 wxWindow::ScreenToClient(x
, y
);
484 // We may be faking the client origin.
485 // So a window that's really at (0, 30) may appear
486 // (to wxWin apps) to be at (0, 0).
487 wxPoint
pt(GetClientAreaOrigin());
492 void wxFrame::ClientToScreen(int *x
, int *y
) const
494 // We may be faking the client origin.
495 // So a window that's really at (0, 30) may appear
496 // (to wxWin apps) to be at (0, 0).
497 wxPoint
pt1(GetClientAreaOrigin());
501 wxWindow::ClientToScreen(x
, y
);
504 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
506 wxCHECK_MSG( m_frameToolBar
== NULL
, FALSE
,
507 "recreating toolbar in wxFrame" );
509 wxToolBar
* toolBar
= OnCreateToolBar(style
, id
, name
);
522 wxToolBar
* wxFrame::OnCreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
524 return new wxToolBar(this, id
, wxDefaultPosition
, wxDefaultSize
, style
, name
);
527 void wxFrame::PositionToolBar()
531 // TODO: we actually need to use the low-level client size, before
532 // the toolbar/status bar were added.
533 // So DEFINITELY replace the line below with something appropriate.
535 GetClientSize(& cw
, &ch
);
537 if ( GetStatusBar() )
539 int statusX
, statusY
;
540 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
547 GetToolBar()->GetSize(& tw
, & th
);
549 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
551 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
552 // means, pretend we don't have toolbar/status bar, so we
553 // have the original client size.
554 GetToolBar()->SetSize(0, 0, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
558 // Use the 'real' position
559 GetToolBar()->SetSize(0, 0, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);