]> git.saurik.com Git - wxWidgets.git/blobdiff - src/cocoa/frame.mm
osx regrouping
[wxWidgets.git] / src / cocoa / frame.mm
index 7c0387a644f91a7251d6e05c115fcf40957eff79..dd494c88dff6e4556a7d5712a236071e4127b14c 100644 (file)
@@ -1,27 +1,33 @@
 /////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////////////////////////////////
-// Name:        cocoa/frame.mm
+// Name:        src/cocoa/frame.mm
 // Purpose:     wxFrame
 // Author:      David Elliott
 // Modified by:
 // Created:     2003/03/16
 // Purpose:     wxFrame
 // Author:      David Elliott
 // Modified by:
 // Created:     2003/03/16
-// RCS-ID:      $Id:
+// RCS-ID:      $Id$
 // Copyright:   (c) 2003 David Elliott
 // Copyright:   (c) 2003 David Elliott
-// Licence:     wxWindows license
+// Licence:     wxWidgets licence
 /////////////////////////////////////////////////////////////////////////////
 
 /////////////////////////////////////////////////////////////////////////////
 
+#include "wx/wxprec.h"
+
 #include "wx/frame.h"
 #include "wx/frame.h"
-#include "wx/menu.h"
-#include "wx/menuitem.h"
-#include "wx/app.h"
-#include "wx/log.h"
-#include "wx/statusbr.h"
-#include "wx/toolbar.h"
+
+#ifndef WX_PRECOMP
+    #include "wx/log.h"
+    #include "wx/app.h"
+    #include "wx/menu.h"
+    #include "wx/toolbar.h"
+    #include "wx/statusbr.h"
+#endif // WX_PRECOMP
 
 #include "wx/cocoa/autorelease.h"
 
 #include "wx/cocoa/autorelease.h"
+#include "wx/cocoa/mbarman.h"
 
 #import <AppKit/NSWindow.h>
 #import <AppKit/NSApplication.h>
 #import <AppKit/NSView.h>
 
 #import <AppKit/NSWindow.h>
 #import <AppKit/NSApplication.h>
 #import <AppKit/NSView.h>
+#import <AppKit/NSMenuItem.h>
 
 // wxFrame
 
 
 // wxFrame
 
@@ -53,59 +59,101 @@ wxFrame::~wxFrame()
     [m_frameNSView release];
 }
 
     [m_frameNSView release];
 }
 
-void wxFrame::Cocoa_wxMenuItemAction(wxMenuItem& item)
-{
-    Command(item.GetId());
-}
-
+// -------------------------------------------------------------------
+// Menubar
 void wxFrame::AttachMenuBar(wxMenuBar *mbar)
 {
     wxFrameBase::AttachMenuBar(mbar);
 void wxFrame::AttachMenuBar(wxMenuBar *mbar)
 {
     wxFrameBase::AttachMenuBar(mbar);
-    if(m_frameMenuBar)
-    {
-        wxLogDebug("Attached menu");
-        [m_cocoaNSWindow setMenu:m_frameMenuBar->GetNSMenu()];
-    }
+    wxMenuBarManager::GetInstance()->UpdateMenuBar();
 }
 
 void wxFrame::DetachMenuBar()
 {
 }
 
 void wxFrame::DetachMenuBar()
 {
-    if(m_frameMenuBar)
+    wxFrameBase::DetachMenuBar();
+    wxMenuBarManager::GetInstance()->UpdateMenuBar();
+}
+
+void wxFrame::SetMenuBar(wxMenuBar *menubar)
+{
+    if ( menubar == GetMenuBar() )
     {
     {
-        [m_cocoaNSWindow setMenu:nil];
+        // nothing to do
+        return;
     }
     }
+
     wxFrameBase::DetachMenuBar();
     wxFrameBase::DetachMenuBar();
+    wxFrameBase::AttachMenuBar(menubar);
+    wxMenuBarManager::GetInstance()->UpdateMenuBar();
 }
 
 }
 
-bool wxFrame::Show(bool show)
+wxMenuBar* wxFrame::GetAppMenuBar(wxCocoaNSWindow *win)
 {
 {
-    wxAutoNSAutoreleasePool pool;
-    bool ret = wxFrameBase::Show(show);
-    if(show && GetMenuBar())
-        [wxTheApp->GetNSApplication() setMenu:GetMenuBar()->GetNSMenu() ];
-    return ret;
+    if(GetMenuBar())
+        return GetMenuBar();
+    return wxFrameBase::GetAppMenuBar(win);
 }
 
 }
 
-wxPoint wxFrame::GetClientAreaOrigin() const
+void wxFrame::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
 {
 {
-    return wxPoint(0,0);
+    wxLogTrace(wxTRACE_COCOA,wxT("wxFrame::wxMenuItemAction"));
+    wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
+    wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
+
+    wxMenu *menu = item->GetMenu();
+    wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu!"));
+
+    // Since we're handling the delegate messages there's a very good chance
+    // we'll receive a menu action from an item with a nil target.
+    wxMenuBar *menubar = menu->GetMenuBar();
+    if(menubar)
+    {
+        wxFrame *frame = menubar->GetFrame();
+        wxASSERT_MSG(frame==this, wxT("Received wxMenuItemAction in NSWindow delegate from a menu item attached to a different frame."));
+        frame->ProcessCommand(item->GetId());
+    }
+    else
+        wxLogDebug(wxT("Received wxMenuItemAction in NSWindow delegate from an unknown menu item."));
 }
 
 }
 
-void wxFrame::DoGetClientSize(int *width, int *height) const
+bool wxFrame::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
 {
 {
-    wxFrameBase::DoGetClientSize(width,height);
-    if(height)
+    SEL itemAction = [menuItem action];
+    if(itemAction == @selector(wxMenuItemAction:))
     {
     {
-        if(m_frameStatusBar && m_frameStatusBar->IsShown())
-            *height -= m_frameStatusBar->GetSize().y;
+        wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
+        wxCHECK_MSG(item,false,wxT("validateMenuItem received but no wxMenuItem exists!"));
+        // TODO: do more sanity checking
+        return item->IsEnabled();
     }
     }
+    // TODO: else if cut/copy/paste
+    wxLogDebug(wxT("Asked to validate an unknown menu item"));
+    return false;
 }
 
 }
 
-void wxFrame::DoSetClientSize(int width, int height)
+// -------------------------------------------------------------------
+// Origin/Size
+wxPoint wxFrame::GetClientAreaOrigin() const
+{
+    return wxPoint(0,0);
+}
+
+void wxFrame::CocoaSetWxWindowSize(int width, int height)
 {
 {
-    if(m_frameStatusBar && m_frameStatusBar->IsShown())
+    if(m_frameStatusBar)
         height += m_frameStatusBar->GetSize().y;
         height += m_frameStatusBar->GetSize().y;
-    wxFrameBase::DoSetClientSize(width,height);
+#if wxUSE_TOOLBAR
+    if(m_frameToolBar)
+        height += m_frameToolBar->GetSize().y;
+#endif //wxUSE_TOOLBAR
+    wxTopLevelWindow::CocoaSetWxWindowSize(width,height);
+}
+
+// -------------------------------------------------------------------
+WX_NSView wxFrame::GetNonClientNSView()
+{
+    if(m_frameNSView)
+        return m_frameNSView;
+    return GetNSViewForSuperview();
 }
 
 void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
 }
 
 void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
@@ -122,18 +170,23 @@ void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
 void wxFrame::UpdateFrameNSView()
 {
     if(!m_frameNSView)
 void wxFrame::UpdateFrameNSView()
 {
     if(!m_frameNSView)
-    {
+    {  // NOTE: We only need a plain NSView here since we don't associate it with ourselves.
         m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
         [m_cocoaNSWindow setContentView: m_frameNSView];
         [m_frameNSView addSubview:m_cocoaNSView];
     }
     NSRect frameRect = [m_frameNSView frame];
     float tbarheight = 0.0;
         m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
         [m_cocoaNSWindow setContentView: m_frameNSView];
         [m_frameNSView addSubview:m_cocoaNSView];
     }
     NSRect frameRect = [m_frameNSView frame];
     float tbarheight = 0.0;
+#if wxUSE_TOOLBAR
     if(m_frameToolBar)
     {
         NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
     if(m_frameToolBar)
     {
         NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
+        // If the toolbar doesn't have a superview then set it to our
+        // content view.
         if(![tbarNSView superview])
             [m_frameNSView addSubview: tbarNSView];
         if(![tbarNSView superview])
             [m_frameNSView addSubview: tbarNSView];
+        // Do this after addSubView so that SetSize can work
+        m_frameToolBar->SetSize(m_frameToolBar->DoGetBestSize());
         NSRect tbarRect = [tbarNSView frame];
         tbarRect.size.width = frameRect.size.width;
         tbarRect.origin.x = 0.0;
         NSRect tbarRect = [tbarNSView frame];
         tbarRect.size.width = frameRect.size.width;
         tbarRect.origin.x = 0.0;
@@ -143,6 +196,7 @@ void wxFrame::UpdateFrameNSView()
         [tbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMinYMargin];
         tbarheight = tbarRect.size.height;
     }
         [tbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMinYMargin];
         tbarheight = tbarRect.size.height;
     }
+#endif //wxUSE_TOOLBAR
     float sbarheight = 0.0;
     if(m_frameStatusBar)
     {
     float sbarheight = 0.0;
     if(m_frameStatusBar)
     {
@@ -158,7 +212,7 @@ void wxFrame::UpdateFrameNSView()
         [sbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMaxYMargin];
         sbarheight = sbarRect.size.height;
     }
         [sbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMaxYMargin];
         sbarheight = sbarRect.size.height;
     }
-    wxLogDebug("frame height=%f, tbar=%f, sbar=%f",frameRect.size.height,tbarheight,sbarheight);
+    wxLogTrace(wxTRACE_COCOA,wxT("frame height=%f, tbar=%f, sbar=%f"),frameRect.size.height,tbarheight,sbarheight);
     NSRect innerRect = [m_cocoaNSView frame];
     innerRect.size.height = frameRect.size.height - tbarheight - sbarheight;
     innerRect.origin.y = sbarheight;
     NSRect innerRect = [m_cocoaNSView frame];
     innerRect.size.height = frameRect.size.height - tbarheight - sbarheight;
     innerRect.origin.y = sbarheight;
@@ -176,9 +230,9 @@ void wxFrame::SetStatusBar(wxStatusBar *statusbar)
     if(m_frameStatusBar)
     {
         [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
     if(m_frameStatusBar)
     {
         [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
-        [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewNotSizable];
+        [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
         if(m_frameStatusBar->GetParent())
         if(m_frameStatusBar->GetParent())
-            m_frameStatusBar->GetParent()->CocoaAddChild(m_frameToolBar);
+            m_frameStatusBar->GetParent()->CocoaAddChild(m_frameStatusBar);
     }
     m_frameStatusBar = statusbar;
     if(m_frameStatusBar)
     }
     m_frameStatusBar = statusbar;
     if(m_frameStatusBar)
@@ -193,6 +247,7 @@ wxStatusBar* wxFrame::CreateStatusBar(int number,
                                           wxWindowID winid,
                                           const wxString& name)
 {
                                           wxWindowID winid,
                                           const wxString& name)
 {
+    wxAutoNSAutoreleasePool pool;
     wxFrameBase::CreateStatusBar(number,style,winid,name);
     if(m_frameStatusBar)
     {
     wxFrameBase::CreateStatusBar(number,style,winid,name);
     if(m_frameStatusBar)
     {
@@ -202,12 +257,14 @@ wxStatusBar* wxFrame::CreateStatusBar(int number,
     return m_frameStatusBar;
 }
 
     return m_frameStatusBar;
 }
 
+#if wxUSE_TOOLBAR
 void wxFrame::SetToolBar(wxToolBar *toolbar)
 {
     if(m_frameToolBar)
     {
 void wxFrame::SetToolBar(wxToolBar *toolbar)
 {
     if(m_frameToolBar)
     {
+        m_frameToolBar->SetOwningFrame(NULL);
         [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
         [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
-        [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewNotSizable];
+        [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
         if(m_frameToolBar->GetParent())
             m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
     }
         if(m_frameToolBar->GetParent())
             m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
     }
@@ -215,6 +272,7 @@ void wxFrame::SetToolBar(wxToolBar *toolbar)
     if(m_frameToolBar)
     {
         m_frameToolBar->CocoaRemoveFromParent();
     if(m_frameToolBar)
     {
         m_frameToolBar->CocoaRemoveFromParent();
+        m_frameToolBar->SetOwningFrame(this);
     }
     UpdateFrameNSView();
 }
     }
     UpdateFrameNSView();
 }
@@ -223,16 +281,11 @@ wxToolBar* wxFrame::CreateToolBar(long style,
                                       wxWindowID winid,
                                       const wxString& name)
 {
                                       wxWindowID winid,
                                       const wxString& name)
 {
-    wxFrameBase::CreateToolBar(style,winid,name);
-    if(m_frameToolBar)
-    {
-        m_frameToolBar->CocoaRemoveFromParent();
-    }
-    UpdateFrameNSView();
-    return m_frameToolBar;
+    wxAutoNSAutoreleasePool pool;
+    return wxFrameBase::CreateToolBar(style,winid,name);
 }
 }
+#endif // wxUSE_TOOLBAR
 
 void wxFrame::PositionStatusBar()
 {
 }
 
 void wxFrame::PositionStatusBar()
 {
 }
-