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 #include <wx/mac/uma.h>
28 extern wxList wxModelessWindows
;
29 extern wxList wxPendingDelete
;
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
)
42 #if wxUSE_NATIVE_STATUSBAR
43 bool wxFrame::m_useNativeStatusBar
= TRUE
;
45 bool wxFrame::m_useNativeStatusBar
= FALSE
;
51 m_frameToolBar
= NULL
;
54 // in order to be able to give size events on show
55 m_frameMenuBar
= NULL
;
56 m_frameStatusBar
= NULL
;
61 bool wxFrame::Create(wxWindow
*parent
,
63 const wxString
& title
,
70 wxTopLevelWindows
.Append(this);
73 m_windowStyle
= style
;
74 m_frameMenuBar
= NULL
;
78 m_frameToolBar
= NULL
;
80 m_frameStatusBar
= NULL
;
82 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
87 m_windowId
= (int)NewControlId();
89 if (parent
) parent
->AddChild(this);
91 wxModelessWindows
.Append(this);
111 m_macWindowData
= new MacWindowData() ;
113 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
115 WindowClass wclass
= kDocumentWindowClass
;
116 WindowAttributes attr
= kWindowNoAttributes
;
118 if ( ( m_windowStyle
& wxMINIMIZE_BOX
) || ( m_windowStyle
& wxMAXIMIZE_BOX
) )
120 attr
|= kWindowFullZoomAttribute
;
121 attr
|= kWindowResizableAttribute
;
123 if ( m_windowStyle
& wxSTAY_ON_TOP
)
125 wclass
= kFloatingWindowClass
;
127 // if ( m_windowStyle & wxCAPTION )
128 // attr |= kHasPaletteTitlebarMask ;
133 if ( m_windowStyle
& wxSYSTEM_MENU
)
135 attr
|= kWindowCloseBoxAttribute
;
137 UMACreateNewWindow( wclass
, attr
, &theBoundsRect
, &m_macWindowData
->m_macWindow
) ;
138 wxAssociateWinWithMacWindow( m_macWindowData
->m_macWindow
, this ) ;
140 if( wxApp::s_macDefaultEncodingIsPC
)
141 label
= wxMacMakeMacStringFromPC( title
) ;
144 UMASetWTitleC( m_macWindowData
->m_macWindow
, label
) ;
145 UMACreateRootControl( m_macWindowData
->m_macWindow
, &m_macWindowData
->m_macRootControl
) ;
146 m_macWindowData
->m_macWindowBackgroundTheme
= kThemeBrushDocumentWindowBackground
;
147 m_macWindowData
->m_macFocus
= NULL
;
153 wxTopLevelWindows
.DeleteObject(this);
155 if (m_frameStatusBar
)
156 delete m_frameStatusBar
;
158 delete m_frameMenuBar
;
160 /* Check if it's the last top-level window */
162 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
164 wxTheApp
->SetTopWindow(NULL
);
166 if (wxTheApp
->GetExitOnFrameDelete())
168 wxTheApp
->ExitMainLoop() ;
172 wxModelessWindows
.DeleteObject(this);
176 void wxFrame::Iconize(bool iconize
)
181 // Equivalent to maximize/restore in Windows
182 void wxFrame::Maximize(bool maximize
)
187 bool wxFrame::IsIconized() const
193 // Is the frame maximized?
194 bool wxFrame::IsMaximized(void) const
200 void wxFrame::SetIcon(const wxIcon
& icon
)
206 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
, long style
, wxWindowID id
,
207 const wxString
& name
)
209 wxStatusBar
*statusBar
= NULL
;
211 statusBar
= new wxStatusBar(this, id
, wxPoint(0, 0), wxSize(100, 17),
214 // Set the height according to the font and the border size
215 // we shouldn't do this on the mac, because we have to fit the grow box
217 wxClientDC dc(statusBar);
218 dc.SetFont(statusBar->GetFont());
221 dc.GetTextExtent("X", &x, &y);
223 int height = (int)( (y * 1.1) + 2* statusBar->GetBorderY());
225 statusBar->SetSize(-1, -1, 100, height);
229 statusBar
->SetFieldsCount(number
);
233 wxStatusBar
* wxFrame::CreateStatusBar(int number
, long style
, wxWindowID id
,
234 const wxString
& name
)
236 // Calling CreateStatusBar twice is an error.
237 wxCHECK_MSG( m_frameStatusBar
== NULL
, FALSE
,
238 "recreating status bar in wxFrame" );
240 m_frameStatusBar
= OnCreateStatusBar(number
, style
, id
,
242 if ( m_frameStatusBar
)
245 return m_frameStatusBar
;
251 void wxFrame::SetStatusText(const wxString
& text
, int number
)
253 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set text for" );
255 m_frameStatusBar
->SetStatusText(text
, number
);
258 void wxFrame::SetStatusWidths(int n
, const int widths_field
[])
260 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set widths for" );
262 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
266 void wxFrame::PositionStatusBar()
268 if (m_frameStatusBar
)
271 GetClientSize(&w
, &h
);
273 m_frameStatusBar
->GetSize(&sw
, &sh
);
275 // Since we wish the status bar to be directly under the client area,
276 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
277 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
281 void wxFrame::SetMenuBar(wxMenuBar
*menuBar
)
285 m_frameMenuBar
= NULL
;
289 m_frameMenuBar
= menuBar
;
290 // TODO : we move this into the app code
291 m_frameMenuBar
->MacInstallMenuBar() ;
296 // Work out max. size
297 wxNode
*node
= GetChildren().First();
302 // Find a child that's a subwindow, but not a dialog box.
303 wxWindow
*win
= (wxWindow
*)node
->Data();
305 if (!win
->IsKindOf(CLASSINFO(wxFrame
)) &&
306 !win
->IsKindOf(CLASSINFO(wxDialog
)))
310 win
->GetSize(&width
, &height
);
311 win
->GetPosition(&x
, &y
);
313 if ((x
+ width
) > max_width
)
314 max_width
= x
+ width
;
315 if ((y
+ height
) > max_height
)
316 max_height
= y
+ height
;
320 SetClientSize(max_width
, max_height
);
323 // Responds to colour changes, and passes event on to children.
324 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
326 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
329 if ( m_frameStatusBar
)
331 wxSysColourChangedEvent event2
;
332 event2
.SetEventObject( m_frameStatusBar
);
333 m_frameStatusBar
->ProcessEvent(event2
);
336 // Propagate the event to the non-top-level children
337 wxWindow::OnSysColourChanged(event
);
340 // Default resizing behaviour - if only ONE subwindow,
341 // resize to client rectangle size
342 void wxFrame::OnIdle(wxIdleEvent
& WXUNUSED(event
) )
349 void wxFrame::DoMenuUpdates()
351 wxMenuBar
* bar
= GetMenuBar();
355 int nCount
= bar
->GetMenuCount();
356 for (int n
= 0; n
< nCount
; n
++)
357 DoMenuUpdates(bar
->GetMenu(n
), (wxWindow
*) NULL
);
361 // update a menu and all submenus recursively
362 void wxFrame::DoMenuUpdates(wxMenu
* menu
, wxWindow
* WXUNUSED(focusWin
))
364 wxEvtHandler
* evtHandler
= GetEventHandler();
365 wxMenuItemList::Node
* node
= menu
->GetMenuItems().GetFirst();
368 wxMenuItem
* item
= node
->GetData();
369 if ( !item
->IsSeparator() )
371 wxWindowID id
= item
->GetId();
372 wxUpdateUIEvent
event(id
);
373 event
.SetEventObject( this );
375 if (evtHandler
->ProcessEvent(event
))
377 if (event
.GetSetText())
378 menu
->SetLabel(id
, event
.GetText());
379 if (event
.GetSetChecked())
380 menu
->Check(id
, event
.GetChecked());
381 if (event
.GetSetEnabled())
382 menu
->Enable(id
, event
.GetEnabled());
385 if (item
->GetSubMenu())
386 DoMenuUpdates(item
->GetSubMenu(), (wxWindow
*) NULL
);
388 node
= node
->GetNext();
392 void wxFrame::OnSize(wxSizeEvent
& event
)
394 // if we're using constraints - do use them
395 #if wxUSE_CONSTRAINTS
396 if ( GetAutoLayout() ) {
402 // do we have _exactly_ one child?
403 wxWindow
*child
= NULL
;
404 for ( wxNode
*node
= GetChildren().First(); node
; node
= node
->Next() )
406 wxWindow
*win
= (wxWindow
*)node
->Data();
407 if ( !win
->IsKindOf(CLASSINFO(wxFrame
)) &&
408 !win
->IsKindOf(CLASSINFO(wxDialog
)) &&
409 (win
!= GetStatusBar())
412 (win
!= GetToolBar())
417 return; // it's our second subwindow - nothing to do
423 // we have exactly one child - set it's size to fill the whole frame
424 int clientW
, clientH
;
425 GetClientSize(&clientW
, &clientH
);
430 child
->SetSize(x
, y
, clientW
, clientH
);
434 // Default activation behaviour - set the focus for the first child
436 void wxFrame::OnActivate(wxActivateEvent
& event
)
438 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
440 // Find a child that's a subwindow, but not a dialog box.
441 wxWindow
*child
= (wxWindow
*)node
->Data();
442 if (!child
->IsKindOf(CLASSINFO(wxFrame
)) &&
443 !child
->IsKindOf(CLASSINFO(wxDialog
)))
451 // The default implementation for the close window event.
452 void wxFrame::OnCloseWindow(wxCloseEvent
& event
)
457 // Destroy the window (delayed, if a managed window)
458 bool wxFrame::Destroy()
460 if (!wxPendingDelete
.Member(this))
461 wxPendingDelete
.Append(this);
465 // Default menu selection behaviour - display a help string
466 void wxFrame::OnMenuHighlight(wxMenuEvent
& event
)
470 if (event
.GetMenuId() == -1)
474 wxMenuBar
*menuBar
= GetMenuBar();
477 wxString
helpString(menuBar
->GetHelpString(event
.GetMenuId()));
478 if (helpString
!= "")
479 SetStatusText(helpString
);
485 wxMenuBar
*wxFrame::GetMenuBar() const
487 return m_frameMenuBar
;
491 // Call this to simulate a menu command
492 void wxFrame::Command(int id
)
497 void wxFrame::ProcessCommand(int id
)
499 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
500 commandEvent
.SetInt( id
);
501 commandEvent
.SetEventObject( this );
503 wxMenuBar
*bar
= GetMenuBar() ;
507 /* TODO: check the menu item if required
508 wxMenuItem *item = bar->FindItemForId(id) ;
509 if (item && item->IsCheckable())
511 bar->Check(id,!bar->Checked(id)) ;
515 GetEventHandler()->ProcessEvent(commandEvent
);
518 // Checks if there is a toolbar, and returns the first free client position
519 wxPoint
wxFrame::GetClientAreaOrigin() const
526 GetToolBar()->GetSize(& w
, & h
);
528 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
541 void wxFrame::DoGetClientSize(int *x
, int *y
) const
543 wxWindow::DoGetClientSize( x
, y
) ;
545 if ( GetStatusBar() )
547 int statusX
, statusY
;
548 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
552 wxPoint
pt(GetClientAreaOrigin());
557 void wxFrame::DoSetClientSize(int clientwidth
, int clientheight
)
559 int currentclientwidth
, currentclientheight
;
560 int currentwidth
, currentheight
;
562 GetClientSize( ¤tclientwidth
, ¤tclientheight
) ;
563 GetSize( ¤twidth
, ¤theight
) ;
565 // find the current client size
567 // Find the difference between the entire window (title bar and all)
568 // and the client area; add this to the new client size to move the
571 DoSetSize( -1 , -1 , currentwidth
+ clientwidth
- currentclientwidth
,
572 currentheight
+ clientheight
- currentclientheight
, wxSIZE_USE_EXISTING
) ;
577 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
579 wxCHECK_MSG( m_frameToolBar
== NULL
, FALSE
,
580 "recreating toolbar in wxFrame" );
582 wxToolBar
* toolBar
= OnCreateToolBar(style
, id
, name
);
595 wxToolBar
* wxFrame::OnCreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
597 return new wxToolBar(this, id
, wxDefaultPosition
, wxDefaultSize
, style
, name
);
600 void wxFrame::PositionToolBar()
604 // TODO: we actually need to use the low-level client size, before
605 // the toolbar/status bar were added.
606 // So DEFINITELY replace the line below with something appropriate.
608 // GetClientSize(& cw, &ch);
613 if ( GetStatusBar() )
615 int statusX
, statusY
;
616 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
623 GetToolBar()->GetSize(& tw
, & th
);
625 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
627 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
628 // means, pretend we don't have toolbar/status bar, so we
629 // have the original client size.
630 GetToolBar()->SetSize(0, 0, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
634 // Use the 'real' position
635 GetToolBar()->SetSize(0, 0, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);