X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e702ff0f08a54622a6b0f1e2ec39537b082c3a8e..105fbe1ffa8968cb85fd2cac7192957e522d17ba:/src/mac/carbon/frame.cpp diff --git a/src/mac/carbon/frame.cpp b/src/mac/carbon/frame.cpp index 55f1e3b72c..b4da3e565f 100644 --- a/src/mac/carbon/frame.cpp +++ b/src/mac/carbon/frame.cpp @@ -1,58 +1,52 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: frame.cpp +// Name: src/mac/carbon/frame.cpp // Purpose: wxFrame -// Author: AUTHOR +// Author: Stefan Csomor // Modified by: -// Created: ??/??/98 +// Created: 1998-01-01 // RCS-ID: $Id$ -// Copyright: (c) AUTHOR +// Copyright: (c) Stefan Csomor // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ -#pragma implementation "frame.h" -#endif +#include "wx/wxprec.h" #include "wx/frame.h" -#include "wx/statusbr.h" -#include "wx/toolbar.h" -#include "wx/menuitem.h" -#include "wx/menu.h" -#include "wx/dcclient.h" -#include "wx/dialog.h" -#include "wx/settings.h" -#include "wx/app.h" - -extern wxList wxModelessWindows; -extern wxList wxPendingDelete; - -#if !USE_SHARED_LIBRARY -BEGIN_EVENT_TABLE(wxFrame, wxWindow) - EVT_SIZE(wxFrame::OnSize) + +#ifndef WX_PRECOMP + #include "wx/app.h" + #include "wx/dcclient.h" + #include "wx/menu.h" + #include "wx/dialog.h" + #include "wx/settings.h" + #include "wx/toolbar.h" + #include "wx/statusbr.h" + #include "wx/menuitem.h" +#endif // WX_PRECOMP + +#include "wx/mac/uma.h" + +extern wxWindowList wxModelessWindows; + +BEGIN_EVENT_TABLE(wxFrame, wxFrameBase) EVT_ACTIVATE(wxFrame::OnActivate) - EVT_MENU_HIGHLIGHT_ALL(wxFrame::OnMenuHighlight) + // EVT_MENU_HIGHLIGHT_ALL(wxFrame::OnMenuHighlight) EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged) - EVT_IDLE(wxFrame::OnIdle) - EVT_CLOSE(wxFrame::OnCloseWindow) +// EVT_IDLE(wxFrame::OnIdle) +// EVT_CLOSE(wxFrame::OnCloseWindow) END_EVENT_TABLE() -IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow) -#endif +IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow) -#if wxUSE_NATIVE_STATUSBAR -bool wxFrame::m_useNativeStatusBar = TRUE; -#else -bool wxFrame::m_useNativeStatusBar = FALSE; -#endif +#define WX_MAC_STATUSBAR_HEIGHT 18 -wxFrame::wxFrame() -{ - m_frameToolBar = NULL ; - m_frameMenuBar = NULL; - m_frameStatusBar = NULL; +// ---------------------------------------------------------------------------- +// creation/destruction +// ---------------------------------------------------------------------------- - m_windowParent = NULL; - m_iconized = FALSE; +void wxFrame::Init() +{ + m_winLastFocused = NULL; } bool wxFrame::Create(wxWindow *parent, @@ -63,503 +57,350 @@ bool wxFrame::Create(wxWindow *parent, long style, const wxString& name) { - if (!parent) - wxTopLevelWindows.Append(this); - - SetName(name); - m_windowStyle = style; - m_frameMenuBar = NULL; - m_frameToolBar = NULL ; - m_frameStatusBar = NULL; - - SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE)); - - if ( id > -1 ) - m_windowId = id; - else - m_windowId = (int)NewControlId(); - if (parent) parent->AddChild(this); + if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) ) + return false; - wxModelessWindows.Append(this); + wxModelessWindows.Append(this); - // TODO: create frame. - - return FALSE; + return true; } wxFrame::~wxFrame() { - wxTopLevelWindows.DeleteObject(this); - - if (m_frameStatusBar) - delete m_frameStatusBar; - if (m_frameMenuBar) - delete m_frameMenuBar; - -/* Check if it's the last top-level window */ - - if (wxTheApp && (wxTopLevelWindows.Number() == 0)) - { - wxTheApp->SetTopWindow(NULL); - - if (wxTheApp->GetExitOnFrameDelete()) - { - // TODO signal to the app that we're going to close - } - } - - wxModelessWindows.DeleteObject(this); -} - -// Get size *available for subwindows* i.e. excluding menu bar, toolbar etc. -void wxFrame::GetClientSize(int *x, int *y) const -{ - // TODO + m_isBeingDeleted = true; + DeleteAllBars(); } -// Set the client size (i.e. leave the calculation of borders etc. -// to wxWindows) -void wxFrame::SetClientSize(int width, int height) -{ - // TODO -} - -void wxFrame::GetSize(int *width, int *height) const -{ - // TODO -} - -void wxFrame::GetPosition(int *x, int *y) const -{ - // TODO -} - -void wxFrame::SetSize(int x, int y, int width, int height, int sizeFlags) -{ - // TODO -} - -bool wxFrame::Show(bool show) -{ - // TODO - return FALSE; -} - -void wxFrame::Iconize(bool iconize) +// get the origin of the client area in the client coordinates +wxPoint wxFrame::GetClientAreaOrigin() const { - // TODO -} + wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin(); -// Equivalent to maximize/restore in Windows -void wxFrame::Maximize(bool maximize) -{ - // TODO -} +#if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__) + wxToolBar *toolbar = GetToolBar(); + if ( toolbar && toolbar->IsShown() ) + { + int w, h; + toolbar->GetSize(&w, &h); -bool wxFrame::IsIconized() const -{ - // TODO - return FALSE; -} + if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL ) + { + pt.x += w; + } + else + { +#if !wxMAC_USE_NATIVE_TOOLBAR + pt.y += h; +#endif + } + } +#endif -// Is the frame maximized? -bool wxFrame::IsMaximized(void) const -{ - // TODO - return FALSE; + return pt; } -void wxFrame::SetTitle(const wxString& title) +bool wxFrame::Enable(bool enable) { - // TODO -} + if ( !wxWindow::Enable(enable) ) + return false; -wxString wxFrame::GetTitle() const -{ - // TODO - return wxString(""); -} + if ( m_frameMenuBar && m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar() ) + { + int iMaxMenu = m_frameMenuBar->GetMenuCount(); + for ( int i = 0 ; i < iMaxMenu ; ++ i ) + { + m_frameMenuBar->EnableTop( i , enable ) ; + } + } -void wxFrame::SetIcon(const wxIcon& icon) -{ - m_icon = icon; - // TODO + return true; } +#if wxUSE_STATUSBAR wxStatusBar *wxFrame::OnCreateStatusBar(int number, long style, wxWindowID id, const wxString& name) { - wxStatusBar *statusBar = NULL; - - statusBar = new wxStatusBar(this, id, wxPoint(0, 0), wxSize(100, 20), - style, name); - - // Set the height according to the font and the border size - wxClientDC dc(statusBar); - dc.SetFont(statusBar->GetFont()); - - long x, y; - dc.GetTextExtent("X", &x, &y); - - int height = (int)( (y * 1.1) + 2* statusBar->GetBorderY()); - - statusBar->SetSize(-1, -1, 100, height); + wxStatusBar *statusBar; + statusBar = new wxStatusBar(this, id, style, name); + statusBar->SetSize(100, WX_MAC_STATUSBAR_HEIGHT); statusBar->SetFieldsCount(number); - return statusBar; -} - -wxStatusBar* wxFrame::CreateStatusBar(int number, long style, wxWindowID id, - const wxString& name) -{ - // Calling CreateStatusBar twice is an error. - wxCHECK_MSG( m_frameStatusBar == NULL, FALSE, - "recreating status bar in wxFrame" ); - - m_frameStatusBar = OnCreateStatusBar(number, style, id, - name); - if ( m_frameStatusBar ) - { - PositionStatusBar(); - return m_frameStatusBar; - } - else - return NULL; -} -void wxFrame::SetStatusText(const wxString& text, int number) -{ - wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set text for" ); - - m_frameStatusBar->SetStatusText(text, number); -} - -void wxFrame::SetStatusWidths(int n, const int widths_field[]) -{ - wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set widths for" ); - - m_frameStatusBar->SetStatusWidths(n, widths_field); - PositionStatusBar(); + return statusBar; } void wxFrame::PositionStatusBar() { - int w, h; - GetClientSize(&w, &h); - int sw, sh; - m_frameStatusBar->GetSize(&sw, &sh); - - // Since we wish the status bar to be directly under the client area, - // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS. - m_frameStatusBar->SetSize(0, h, w, sh); -} - -void wxFrame::SetMenuBar(wxMenuBar *menuBar) -{ - if (!menuBar) + if (m_frameStatusBar && m_frameStatusBar->IsShown() ) { - m_frameMenuBar = NULL; - return; - } - - m_frameMenuBar = menuBar; - - // TODO -} + int w, h; + GetClientSize(&w, &h); -void wxFrame::Fit() -{ - // Work out max. size - wxNode *node = GetChildren().First(); - int max_width = 0; - int max_height = 0; - while (node) - { - // Find a child that's a subwindow, but not a dialog box. - wxWindow *win = (wxWindow *)node->Data(); - - if (!win->IsKindOf(CLASSINFO(wxFrame)) && - !win->IsKindOf(CLASSINFO(wxDialog))) - { - int width, height; - int x, y; - win->GetSize(&width, &height); - win->GetPosition(&x, &y); - - if ((x + width) > max_width) - max_width = x + width; - if ((y + height) > max_height) - max_height = y + height; + // Since we wish the status bar to be directly under the client area, + // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS. + m_frameStatusBar->SetSize(0, h, w, WX_MAC_STATUSBAR_HEIGHT); } - node = node->Next(); - } - SetClientSize(max_width, max_height); } +#endif // wxUSE_STATUSBAR // Responds to colour changes, and passes event on to children. void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event) { - SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE)); + SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE)); Refresh(); +#if wxUSE_STATUSBAR if ( m_frameStatusBar ) { wxSysColourChangedEvent event2; + event2.SetEventObject( m_frameStatusBar ); m_frameStatusBar->ProcessEvent(event2); } +#endif // wxUSE_STATUSBAR // Propagate the event to the non-top-level children wxWindow::OnSysColourChanged(event); } -// Default resizing behaviour - if only ONE subwindow, -// resize to client rectangle size -void wxFrame::OnSize(wxSizeEvent& event) -{ - // if we're using constraints - do use them - #if wxUSE_CONSTRAINTS - if ( GetAutoLayout() ) { - Layout(); - return; - } - #endif - - // do we have _exactly_ one child? - wxWindow *child = NULL; - for ( wxNode *node = GetChildren().First(); node; node = node->Next() ) - { - wxWindow *win = (wxWindow *)node->Data(); - if ( !win->IsKindOf(CLASSINFO(wxFrame)) && - !win->IsKindOf(CLASSINFO(wxDialog)) && - (win != GetStatusBar()) && - (win != GetToolBar()) ) - { - if ( child ) - return; // it's our second subwindow - nothing to do - child = win; - } - } - - if ( child ) { - // we have exactly one child - set it's size to fill the whole frame - int clientW, clientH; - GetClientSize(&clientW, &clientH); - - int x = 0; - int y = 0; - - child->SetSize(x, y, clientW, clientH); - } -} - // Default activation behaviour - set the focus for the first child // subwindow found. void wxFrame::OnActivate(wxActivateEvent& event) { - for(wxNode *node = GetChildren().First(); node; node = node->Next()) - { - // Find a child that's a subwindow, but not a dialog box. - wxWindow *child = (wxWindow *)node->Data(); - if (!child->IsKindOf(CLASSINFO(wxFrame)) && - !child->IsKindOf(CLASSINFO(wxDialog))) + if ( !event.GetActive() ) { - child->SetFocus(); - return; - } - } -} + // remember the last focused child if it is our child + m_winLastFocused = FindFocus(); -// The default implementation for the close window event. -void wxFrame::OnCloseWindow(wxCloseEvent& event) -{ - this->Destroy(); -} + // so we NULL it out if it's a child from some other frame + wxWindow *win = m_winLastFocused; + while ( win ) + { + if ( win->IsTopLevel() ) + { + if ( win != this ) + m_winLastFocused = NULL; -// Destroy the window (delayed, if a managed window) -bool wxFrame::Destroy() -{ - if (!wxPendingDelete.Member(this)) - wxPendingDelete.Append(this); - return TRUE; -} + break; + } -// Default menu selection behaviour - display a help string -void wxFrame::OnMenuHighlight(wxMenuEvent& event) -{ - if (GetStatusBar()) - { - if (event.GetMenuId() == -1) - SetStatusText(""); + win = win->GetParent(); + } + + event.Skip(); + } else { - wxMenuBar *menuBar = GetMenuBar(); - if (menuBar) - { - wxString helpString(menuBar->GetHelpString(event.GetMenuId())); - if (helpString != "") - SetStatusText(helpString); - } - } - } -} - -wxMenuBar *wxFrame::GetMenuBar() const -{ - return m_frameMenuBar; -} - -void wxFrame::Centre(int direction) -{ - int display_width, display_height, width, height, x, y; - wxDisplaySize(&display_width, &display_height); + // restore focus to the child which was last focused + wxWindow *parent = m_winLastFocused + ? m_winLastFocused->GetParent() + : NULL; - GetSize(&width, &height); - GetPosition(&x, &y); + if (parent == NULL) + parent = this; - if (direction & wxHORIZONTAL) - x = (int)((display_width - width)/2); - if (direction & wxVERTICAL) - y = (int)((display_height - height)/2); + wxSetFocusToChild(parent, &m_winLastFocused); - SetSize(x, y, width, height); + if (m_frameMenuBar != NULL) + { + m_frameMenuBar->MacInstallMenuBar(); + } + else + { + wxFrame *tlf = wxDynamicCast( wxTheApp->GetTopWindow(), wxFrame ); + if (tlf != NULL) + { + // Trying top-level frame membar + if (tlf->GetMenuBar()) + tlf->GetMenuBar()->MacInstallMenuBar(); + } + } + } } -// Call this to simulate a menu command -void wxFrame::Command(int id) +void wxFrame::DetachMenuBar() { - ProcessCommand(id); + if ( m_frameMenuBar ) + m_frameMenuBar->UnsetInvokingWindow(); + + wxFrameBase::DetachMenuBar(); } -void wxFrame::ProcessCommand(int id) +void wxFrame::AttachMenuBar( wxMenuBar *menuBar ) { - wxCommandEvent commandEvent(wxEVENT_TYPE_MENU_COMMAND, id); - commandEvent.SetInt( id ); - commandEvent.SetEventObject( this ); - - wxMenuBar *bar = GetMenuBar() ; - if (!bar) - return; - -/* TODO: check the menu item if required - wxMenuItem *item = bar->FindItemForId(id) ; - if (item && item->IsCheckable()) - { - bar->Check(id,!bar->Checked(id)) ; - } -*/ + wxFrame* tlf = wxDynamicCast( wxFindWinFromMacWindow( FrontNonFloatingWindow() ) , wxFrame ); + bool makeCurrent = false; - // Process events starting with the window with the focus, if any. - wxWindow* focusWin = wxFindFocusDescendant(this); + // if this is already the current menubar or we are the frontmost window + if ( (tlf == this) || (m_frameMenuBar == wxMenuBar::MacGetInstalledMenuBar()) ) + makeCurrent = true; + // or there is an app-level menubar like MDI + else if ( tlf && (tlf->GetMenuBar() == NULL) && (((wxFrame*)wxTheApp->GetTopWindow()) == this) ) + makeCurrent = true; - wxEvtHandler* evtHandler = focusWin ? focusWin->GetEventHandler() : GetEventHandler(); + wxFrameBase::AttachMenuBar( menuBar ); - evtHandler->ProcessEvent(commandEvent); + if (m_frameMenuBar) + { + m_frameMenuBar->SetInvokingWindow( this ); + if (makeCurrent) + m_frameMenuBar->MacInstallMenuBar(); + } } -// Checks if there is a toolbar, and returns the first free client position -wxPoint wxFrame::GetClientAreaOrigin() const +void wxFrame::DoGetClientSize(int *x, int *y) const { - wxPoint pt(0, 0); - if (GetToolBar()) + wxTopLevelWindow::DoGetClientSize( x , y ); + +#if wxUSE_STATUSBAR + if ( GetStatusBar() && GetStatusBar()->IsShown() && y ) + *y -= WX_MAC_STATUSBAR_HEIGHT; +#endif + +#if wxUSE_TOOLBAR + wxToolBar *toolbar = GetToolBar(); + if ( toolbar && toolbar->IsShown() ) { int w, h; - GetToolBar()->GetSize(& w, & h); + toolbar->GetSize(&w, &h); - if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL) + if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL ) { - pt.x += w; + if ( x ) + *x -= w; } else { - pt.y += h; +#if !wxMAC_USE_NATIVE_TOOLBAR + if ( y ) + *y -= h; +#endif } } - return pt; +#endif } -void wxFrame::ScreenToClient(int *x, int *y) const +bool wxFrame::MacIsChildOfClientArea( const wxWindow* child ) const { - wxWindow::ScreenToClient(x, y); - - // We may be faking the client origin. - // So a window that's really at (0, 30) may appear - // (to wxWin apps) to be at (0, 0). - wxPoint pt(GetClientAreaOrigin()); - *x -= pt.x; - *y -= pt.y; +#if wxUSE_STATUSBAR + if ( child == GetStatusBar() ) + return false ; +#endif + +#if wxUSE_TOOLBAR + if ( child == GetToolBar() ) + return false ; +#endif + + return wxFrameBase::MacIsChildOfClientArea( child ) ; } -void wxFrame::ClientToScreen(int *x, int *y) const +void wxFrame::DoSetClientSize(int clientwidth, int clientheight) { - // We may be faking the client origin. - // So a window that's really at (0, 30) may appear - // (to wxWin apps) to be at (0, 0). - wxPoint pt1(GetClientAreaOrigin()); - *x += pt1.x; - *y += pt1.y; - - wxWindow::ClientToScreen(x, y); + int currentclientwidth , currentclientheight ; + int currentwidth , currentheight ; + + GetClientSize( ¤tclientwidth , ¤tclientheight ) ; + if ( clientwidth == -1 ) + clientwidth = currentclientwidth ; + if ( clientheight == -1 ) + clientheight = currentclientheight ; + GetSize( ¤twidth , ¤theight ) ; + + // find the current client size + + // Find the difference between the entire window (title bar and all) and + // the client area; add this to the new client size to move the window + DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth , + currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ; } -wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name) +#if wxUSE_TOOLBAR +void wxFrame::SetToolBar(wxToolBar *toolbar) { - wxCHECK_MSG( m_frameToolBar == NULL, FALSE, - "recreating toolbar in wxFrame" ); + if ( m_frameToolBar == toolbar ) + return ; - wxToolBar* toolBar = OnCreateToolBar(style, id, name); - if (toolBar) - { - SetToolBar(toolBar); - PositionToolBar(); - return toolBar; - } - else - { - return NULL; - } +#if wxMAC_USE_NATIVE_TOOLBAR + if ( m_frameToolBar ) + m_frameToolBar->MacInstallNativeToolbar( false ) ; +#endif + + m_frameToolBar = toolbar ; + +#if wxMAC_USE_NATIVE_TOOLBAR + if ( toolbar ) + toolbar->MacInstallNativeToolbar( true ) ; +#endif } -wxToolBar* wxFrame::OnCreateToolBar(long style, wxWindowID id, const wxString& name) +wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name) { - return new wxToolBar(this, id, wxDefaultPosition, wxDefaultSize, style, name); + if ( wxFrameBase::CreateToolBar(style, id, name) ) + PositionToolBar(); + + return m_frameToolBar; } void wxFrame::PositionToolBar() { int cw, ch; - // TODO: we actually need to use the low-level client size, before - // the toolbar/status bar were added. - // So DEFINITELY replace the line below with something appropriate. - - GetClientSize(& cw, &ch); + GetSize( &cw , &ch ) ; + + int statusX = 0 ; + int statusY = 0 ; - if ( GetStatusBar() ) +#if wxUSE_STATUSBAR + if (GetStatusBar() && GetStatusBar()->IsShown()) { - int statusX, statusY; - GetStatusBar()->GetClientSize(&statusX, &statusY); - ch -= statusY; + GetStatusBar()->GetClientSize(&statusX, &statusY); + ch -= statusY; } +#endif if (GetToolBar()) { - int tw, th; - GetToolBar()->GetSize(& tw, & th); + int tx, ty, tw, th; + tx = ty = 0 ; + GetToolBar()->GetSize(&tw, &th); if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL) { // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS // means, pretend we don't have toolbar/status bar, so we // have the original client size. - GetToolBar()->SetSize(0, 0, tw, ch, wxSIZE_NO_ADJUSTMENTS); + GetToolBar()->SetSize(tx , ty , tw, ch , wxSIZE_NO_ADJUSTMENTS ); + } + else if (GetToolBar()->GetWindowStyleFlag() & wxTB_BOTTOM) + { + //FIXME: this positions the tool bar almost correctly, but still it doesn't work right yet, + //as 1) the space for the 'old' top toolbar is still taken up, and 2) the toolbar + //doesn't extend it's width to the width of the frame. + tx = 0; + ty = ch - (th + statusY); + GetToolBar()->SetSize(tx, ty, cw, th, wxSIZE_NO_ADJUSTMENTS ); } else { +#if !wxMAC_USE_NATIVE_TOOLBAR // Use the 'real' position - GetToolBar()->SetSize(0, 0, cw, th, wxSIZE_NO_ADJUSTMENTS); + GetToolBar()->SetSize(tx , ty , cw , th, wxSIZE_NO_ADJUSTMENTS ); +#endif } } } +#endif // wxUSE_TOOLBAR + +void wxFrame::PositionBars() +{ +#if wxUSE_STATUSBAR + PositionStatusBar(); +#endif +#if wxUSE_TOOLBAR + PositionToolBar(); +#endif +} +