]> git.saurik.com Git - wxWidgets.git/blobdiff - src/cocoa/frame.mm
cocoa doesn't need system framework - string conversion for cocoa - focus for cocoa...
[wxWidgets.git] / src / cocoa / frame.mm
index 3c40bc0f6637f5e66556dde1cc13e10e828445bc..7c21de34f4a022bdda3ac979c663b8f8a22ef3a2 100644 (file)
@@ -6,7 +6,7 @@
 // Created:     2003/03/16
 // RCS-ID:      $Id:
 // Copyright:   (c) 2003 David Elliott
-// Licence:     wxWindows license
+// Licence:     wxWidgets licence
 /////////////////////////////////////////////////////////////////////////////
 
 #include "wx/wxprec.h"
 #endif // WX_PRECOMP
 
 #include "wx/cocoa/autorelease.h"
+#include "wx/cocoa/mbarman.h"
 
 #import <AppKit/NSWindow.h>
 #import <AppKit/NSApplication.h>
 #import <AppKit/NSView.h>
+#import <AppKit/NSMenuItem.h>
 
 // wxFrame
 
@@ -55,34 +57,79 @@ wxFrame::~wxFrame()
     [m_frameNSView release];
 }
 
+// -------------------------------------------------------------------
+// Menubar
 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()
 {
-    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::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);
 }
 
+void wxFrame::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
+{
+    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."));
+}
+
+bool wxFrame::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
+{
+    SEL itemAction = [menuItem action];
+    if(itemAction == @selector(wxMenuItemAction:))
+    {
+        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;
+}
+
+// -------------------------------------------------------------------
+// Origin/Size
 wxPoint wxFrame::GetClientAreaOrigin() const
 {
     return wxPoint(0,0);
@@ -99,6 +146,14 @@ void wxFrame::CocoaSetWxWindowSize(int width, int height)
     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)
 {
     // If we have the additional toolbar/statbar view, then the
@@ -126,6 +181,8 @@ void wxFrame::UpdateFrameNSView()
         NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
         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;
@@ -151,7 +208,7 @@ void wxFrame::UpdateFrameNSView()
         [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;
@@ -186,6 +243,7 @@ wxStatusBar* wxFrame::CreateStatusBar(int number,
                                           wxWindowID winid,
                                           const wxString& name)
 {
+    wxAutoNSAutoreleasePool pool;
     wxFrameBase::CreateStatusBar(number,style,winid,name);
     if(m_frameStatusBar)
     {
@@ -200,6 +258,7 @@ void wxFrame::SetToolBar(wxToolBar *toolbar)
 {
     if(m_frameToolBar)
     {
+        m_frameToolBar->SetOwningFrame(NULL);
         [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
         [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
         if(m_frameToolBar->GetParent())
@@ -209,6 +268,7 @@ void wxFrame::SetToolBar(wxToolBar *toolbar)
     if(m_frameToolBar)
     {
         m_frameToolBar->CocoaRemoveFromParent();
+        m_frameToolBar->SetOwningFrame(this);
     }
     UpdateFrameNSView();
 }
@@ -217,10 +277,12 @@ wxToolBar* wxFrame::CreateToolBar(long style,
                                       wxWindowID winid,
                                       const wxString& name)
 {
+    wxAutoNSAutoreleasePool pool;
     wxFrameBase::CreateToolBar(style,winid,name);
     if(m_frameToolBar)
     {
         m_frameToolBar->CocoaRemoveFromParent();
+        m_frameToolBar->SetOwningFrame(this);
     }
     UpdateFrameNSView();
     return m_frameToolBar;