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 #if !USE_SHARED_LIBRARY
32 BEGIN_EVENT_TABLE(wxFrame
, wxWindow
)
33 EVT_SIZE(wxFrame::OnSize
)
34 EVT_ACTIVATE(wxFrame::OnActivate
)
35 EVT_MENU_HIGHLIGHT_ALL(wxFrame::OnMenuHighlight
)
36 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
)
37 EVT_IDLE(wxFrame::OnIdle
)
38 EVT_CLOSE(wxFrame::OnCloseWindow
)
41 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxWindow
)
44 #if wxUSE_NATIVE_STATUSBAR
45 bool wxFrame::m_useNativeStatusBar
= TRUE
;
47 bool wxFrame::m_useNativeStatusBar
= FALSE
;
53 m_frameToolBar
= NULL
;
56 // in order to be able to give size events on show
57 m_frameMenuBar
= NULL
;
58 m_frameStatusBar
= NULL
;
62 bool wxFrame::Create(wxWindow
*parent
,
64 const wxString
& title
,
71 wxTopLevelWindows
.Append(this);
74 m_windowStyle
= style
;
75 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::OnSize(wxSizeEvent
& event
)
344 // if we're using constraints - do use them
345 #if wxUSE_CONSTRAINTS
346 if ( GetAutoLayout() ) {
352 // do we have _exactly_ one child?
353 wxWindow
*child
= NULL
;
354 for ( wxNode
*node
= GetChildren().First(); node
; node
= node
->Next() )
356 wxWindow
*win
= (wxWindow
*)node
->Data();
357 if ( !win
->IsKindOf(CLASSINFO(wxFrame
)) &&
358 !win
->IsKindOf(CLASSINFO(wxDialog
)) &&
359 (win
!= GetStatusBar())
362 (win
!= GetToolBar())
367 return; // it's our second subwindow - nothing to do
373 // we have exactly one child - set it's size to fill the whole frame
374 int clientW
, clientH
;
375 GetClientSize(&clientW
, &clientH
);
380 child
->SetSize(x
, y
, clientW
, clientH
);
384 // Default activation behaviour - set the focus for the first child
386 void wxFrame::OnActivate(wxActivateEvent
& event
)
388 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
390 // Find a child that's a subwindow, but not a dialog box.
391 wxWindow
*child
= (wxWindow
*)node
->Data();
392 if (!child
->IsKindOf(CLASSINFO(wxFrame
)) &&
393 !child
->IsKindOf(CLASSINFO(wxDialog
)))
401 // The default implementation for the close window event.
402 void wxFrame::OnCloseWindow(wxCloseEvent
& event
)
407 // Destroy the window (delayed, if a managed window)
408 bool wxFrame::Destroy()
410 if (!wxPendingDelete
.Member(this))
411 wxPendingDelete
.Append(this);
415 // Default menu selection behaviour - display a help string
416 void wxFrame::OnMenuHighlight(wxMenuEvent
& event
)
420 if (event
.GetMenuId() == -1)
424 wxMenuBar
*menuBar
= GetMenuBar();
427 wxString
helpString(menuBar
->GetHelpString(event
.GetMenuId()));
428 if (helpString
!= "")
429 SetStatusText(helpString
);
435 wxMenuBar
*wxFrame::GetMenuBar() const
437 return m_frameMenuBar
;
441 // Call this to simulate a menu command
442 void wxFrame::Command(int id
)
447 void wxFrame::ProcessCommand(int id
)
449 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
450 commandEvent
.SetInt( id
);
451 commandEvent
.SetEventObject( this );
453 wxMenuBar
*bar
= GetMenuBar() ;
457 /* TODO: check the menu item if required
458 wxMenuItem *item = bar->FindItemForId(id) ;
459 if (item && item->IsCheckable())
461 bar->Check(id,!bar->Checked(id)) ;
465 GetEventHandler()->ProcessEvent(commandEvent
);
468 // Checks if there is a toolbar, and returns the first free client position
469 wxPoint
wxFrame::GetClientAreaOrigin() const
476 GetToolBar()->GetSize(& w
, & h
);
478 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
491 void wxFrame::GetClientSize(int *x
, int *y
) const
493 wxWindow::GetClientSize( x
, y
) ;
495 if ( GetStatusBar() )
497 int statusX
, statusY
;
498 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
502 wxPoint
pt(GetClientAreaOrigin());
507 void wxFrame::DoSetClientSize(int clientwidth
, int clientheight
)
509 int currentclientwidth
, currentclientheight
;
510 int currentwidth
, currentheight
;
512 GetClientSize( ¤tclientwidth
, ¤tclientheight
) ;
513 GetSize( ¤twidth
, ¤theight
) ;
515 // find the current client size
517 // Find the difference between the entire window (title bar and all)
518 // and the client area; add this to the new client size to move the
521 DoSetSize( -1 , -1 , currentwidth
+ clientwidth
- currentclientwidth
,
522 currentheight
+ clientheight
- currentclientheight
, wxSIZE_USE_EXISTING
) ;
527 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
529 wxCHECK_MSG( m_frameToolBar
== NULL
, FALSE
,
530 "recreating toolbar in wxFrame" );
532 wxToolBar
* toolBar
= OnCreateToolBar(style
, id
, name
);
545 wxToolBar
* wxFrame::OnCreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
547 return new wxToolBar(this, id
, wxDefaultPosition
, wxDefaultSize
, style
, name
);
550 void wxFrame::PositionToolBar()
554 // TODO: we actually need to use the low-level client size, before
555 // the toolbar/status bar were added.
556 // So DEFINITELY replace the line below with something appropriate.
558 // GetClientSize(& cw, &ch);
563 if ( GetStatusBar() )
565 int statusX
, statusY
;
566 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
573 GetToolBar()->GetSize(& tw
, & th
);
575 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
577 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
578 // means, pretend we don't have toolbar/status bar, so we
579 // have the original client size.
580 GetToolBar()->SetSize(0, 0, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
584 // Use the 'real' position
585 GetToolBar()->SetSize(0, 0, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);