1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/frame.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #include "wx/toolbar.h"
19 #include "wx/statusbr.h"
22 #include "wx/cocoa/autorelease.h"
23 #include "wx/cocoa/mbarman.h"
25 #import <AppKit/NSWindow.h>
26 #import <AppKit/NSApplication.h>
27 #import <AppKit/NSView.h>
28 #import <AppKit/NSMenuItem.h>
32 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
35 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
42 bool wxFrame::Create(wxWindow *parent,
44 const wxString& title,
50 bool rt = wxTopLevelWindow::Create(parent,winid,title,pos,size,style,name);
57 [m_frameNSView release];
60 // -------------------------------------------------------------------
62 void wxFrame::AttachMenuBar(wxMenuBar *mbar)
64 wxFrameBase::AttachMenuBar(mbar);
65 wxMenuBarManager::GetInstance()->UpdateMenuBar();
68 void wxFrame::DetachMenuBar()
70 wxFrameBase::DetachMenuBar();
71 wxMenuBarManager::GetInstance()->UpdateMenuBar();
74 void wxFrame::SetMenuBar(wxMenuBar *menubar)
76 if ( menubar == GetMenuBar() )
82 wxFrameBase::DetachMenuBar();
83 wxFrameBase::AttachMenuBar(menubar);
84 wxMenuBarManager::GetInstance()->UpdateMenuBar();
87 wxMenuBar* wxFrame::GetAppMenuBar(wxCocoaNSWindow *win)
91 return wxFrameBase::GetAppMenuBar(win);
94 void wxFrame::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
96 wxLogTrace(wxTRACE_COCOA,wxT("wxFrame::wxMenuItemAction"));
97 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
98 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
100 wxMenu *menu = item->GetMenu();
101 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu!"));
103 // Since we're handling the delegate messages there's a very good chance
104 // we'll receive a menu action from an item with a nil target.
105 wxMenuBar *menubar = menu->GetMenuBar();
108 wxFrame *frame = menubar->GetFrame();
109 wxASSERT_MSG(frame==this, wxT("Received wxMenuItemAction in NSWindow delegate from a menu item attached to a different frame."));
110 frame->ProcessCommand(item->GetId());
113 wxLogDebug(wxT("Received wxMenuItemAction in NSWindow delegate from an unknown menu item."));
116 bool wxFrame::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
118 SEL itemAction = [menuItem action];
119 if(itemAction == @selector(wxMenuItemAction:))
121 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
122 wxCHECK_MSG(item,false,wxT("validateMenuItem received but no wxMenuItem exists!"));
123 // TODO: do more sanity checking
124 return item->IsEnabled();
126 // TODO: else if cut/copy/paste
127 wxLogDebug(wxT("Asked to validate an unknown menu item"));
131 // -------------------------------------------------------------------
133 wxPoint wxFrame::GetClientAreaOrigin() const
138 void wxFrame::CocoaSetWxWindowSize(int width, int height)
141 height += m_frameStatusBar->GetSize().y;
144 height += m_frameToolBar->GetSize().y;
145 #endif //wxUSE_TOOLBAR
146 wxTopLevelWindow::CocoaSetWxWindowSize(width,height);
149 // -------------------------------------------------------------------
150 WX_NSView wxFrame::GetNonClientNSView()
153 return m_frameNSView;
154 return GetNSViewForSuperview();
157 void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
159 // If we have the additional toolbar/statbar view, then the
160 // default replaceSubview will work. Otherwise, the old view
161 // should be the content view and should be replaced that way
163 wxWindow::CocoaReplaceView(oldView, newView);
165 wxTopLevelWindow::CocoaReplaceView(oldView, newView);
168 void wxFrame::UpdateFrameNSView()
172 m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
173 [m_cocoaNSWindow setContentView: m_frameNSView];
174 [m_frameNSView addSubview:m_cocoaNSView];
176 NSRect frameRect = [m_frameNSView frame];
177 float tbarheight = 0.0;
181 NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
182 if(![tbarNSView superview])
183 [m_frameNSView addSubview: tbarNSView];
184 // Do this after addSubView so that SetSize can work
185 m_frameToolBar->SetSize(m_frameToolBar->DoGetBestSize());
186 NSRect tbarRect = [tbarNSView frame];
187 tbarRect.size.width = frameRect.size.width;
188 tbarRect.origin.x = 0.0;
189 tbarRect.origin.y = frameRect.size.height - tbarRect.size.height;
190 [tbarNSView setFrame:tbarRect];
191 // width expands, bottom margin expands
192 [tbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMinYMargin];
193 tbarheight = tbarRect.size.height;
195 #endif //wxUSE_TOOLBAR
196 float sbarheight = 0.0;
199 NSView *sbarNSView = m_frameStatusBar->GetNSViewForSuperview();
200 if(![sbarNSView superview])
201 [m_frameNSView addSubview: sbarNSView];
202 NSRect sbarRect = [sbarNSView frame];
203 sbarRect.size.width = frameRect.size.width;
204 sbarRect.origin.x = 0.0;
205 sbarRect.origin.y = 0.0;
206 [sbarNSView setFrame:sbarRect];
207 // width expands, top margin expands
208 [sbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMaxYMargin];
209 sbarheight = sbarRect.size.height;
211 wxLogTrace(wxTRACE_COCOA,wxT("frame height=%f, tbar=%f, sbar=%f"),frameRect.size.height,tbarheight,sbarheight);
212 NSRect innerRect = [m_cocoaNSView frame];
213 innerRect.size.height = frameRect.size.height - tbarheight - sbarheight;
214 innerRect.origin.y = sbarheight;
215 [m_cocoaNSView setFrame:innerRect];
216 [m_cocoaNSView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
217 // Don't let the frame get smaller than the toolbar+statusbar height
218 NSRect frameMinRect = [NSWindow frameRectForContentRect:
219 NSMakeRect(0.0,0.0,0.0,tbarheight+sbarheight)
220 styleMask: [m_cocoaNSWindow styleMask]];
221 [m_cocoaNSWindow setMinSize:frameMinRect.size];
224 void wxFrame::SetStatusBar(wxStatusBar *statusbar)
228 [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
229 [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
230 if(m_frameStatusBar->GetParent())
231 m_frameStatusBar->GetParent()->CocoaAddChild(m_frameStatusBar);
233 m_frameStatusBar = statusbar;
236 m_frameStatusBar->CocoaRemoveFromParent();
241 wxStatusBar* wxFrame::CreateStatusBar(int number,
244 const wxString& name)
246 wxAutoNSAutoreleasePool pool;
247 wxFrameBase::CreateStatusBar(number,style,winid,name);
250 m_frameStatusBar->CocoaRemoveFromParent();
253 return m_frameStatusBar;
257 void wxFrame::SetToolBar(wxToolBar *toolbar)
261 m_frameToolBar->SetOwningFrame(NULL);
262 [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
263 [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
264 if(m_frameToolBar->GetParent())
265 m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
267 m_frameToolBar = toolbar;
270 m_frameToolBar->CocoaRemoveFromParent();
271 m_frameToolBar->SetOwningFrame(this);
276 wxToolBar* wxFrame::CreateToolBar(long style,
278 const wxString& name)
280 wxAutoNSAutoreleasePool pool;
281 wxFrameBase::CreateToolBar(style,winid,name);
284 m_frameToolBar->CocoaRemoveFromParent();
285 m_frameToolBar->SetOwningFrame(this);
288 return m_frameToolBar;
290 #endif // wxUSE_TOOLBAR
292 void wxFrame::PositionStatusBar()