1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/frame.mm
4 // Author: David Elliott
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
19 #include "wx/toolbar.h"
20 #include "wx/statusbr.h"
23 #include "wx/cocoa/autorelease.h"
24 #include "wx/cocoa/mbarman.h"
26 #import <AppKit/NSWindow.h>
27 #import <AppKit/NSApplication.h>
28 #import <AppKit/NSView.h>
29 #import <AppKit/NSMenuItem.h>
33 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
41 bool wxFrame::Create(wxWindow *parent,
43 const wxString& title,
49 bool rt = wxTopLevelWindow::Create(parent,winid,title,pos,size,style,name);
56 [m_frameNSView release];
59 // -------------------------------------------------------------------
61 void wxFrame::AttachMenuBar(wxMenuBar *mbar)
63 wxFrameBase::AttachMenuBar(mbar);
64 wxMenuBarManager::GetInstance()->UpdateMenuBar();
67 void wxFrame::DetachMenuBar()
69 wxFrameBase::DetachMenuBar();
70 wxMenuBarManager::GetInstance()->UpdateMenuBar();
73 void wxFrame::SetMenuBar(wxMenuBar *menubar)
75 if ( menubar == GetMenuBar() )
81 wxFrameBase::DetachMenuBar();
82 wxFrameBase::AttachMenuBar(menubar);
83 wxMenuBarManager::GetInstance()->UpdateMenuBar();
86 wxMenuBar* wxFrame::GetAppMenuBar(wxCocoaNSWindow *win)
90 return wxFrameBase::GetAppMenuBar(win);
93 void wxFrame::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
95 wxLogTrace(wxTRACE_COCOA,wxT("wxFrame::wxMenuItemAction"));
96 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
97 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
99 wxMenu *menu = item->GetMenu();
100 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu!"));
102 // Since we're handling the delegate messages there's a very good chance
103 // we'll receive a menu action from an item with a nil target.
104 wxMenuBar *menubar = menu->GetMenuBar();
107 wxFrame *frame = menubar->GetFrame();
108 wxASSERT_MSG(frame==this, wxT("Received wxMenuItemAction in NSWindow delegate from a menu item attached to a different frame."));
109 frame->ProcessCommand(item->GetId());
112 wxLogDebug(wxT("Received wxMenuItemAction in NSWindow delegate from an unknown menu item."));
115 bool wxFrame::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
117 SEL itemAction = [menuItem action];
118 if(itemAction == @selector(wxMenuItemAction:))
120 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
121 wxCHECK_MSG(item,false,wxT("validateMenuItem received but no wxMenuItem exists!"));
122 // TODO: do more sanity checking
123 return item->IsEnabled();
125 // TODO: else if cut/copy/paste
126 wxLogDebug(wxT("Asked to validate an unknown menu item"));
130 // -------------------------------------------------------------------
132 wxPoint wxFrame::GetClientAreaOrigin() const
137 void wxFrame::CocoaSetWxWindowSize(int width, int height)
140 height += m_frameStatusBar->GetSize().y;
143 height += m_frameToolBar->GetSize().y;
144 #endif //wxUSE_TOOLBAR
145 wxTopLevelWindow::CocoaSetWxWindowSize(width,height);
148 // -------------------------------------------------------------------
149 WX_NSView wxFrame::GetNonClientNSView()
152 return m_frameNSView;
153 return GetNSViewForSuperview();
156 void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
158 // If we have the additional toolbar/statbar view, then the
159 // default replaceSubview will work. Otherwise, the old view
160 // should be the content view and should be replaced that way
162 wxWindow::CocoaReplaceView(oldView, newView);
164 wxTopLevelWindow::CocoaReplaceView(oldView, newView);
167 void wxFrame::UpdateFrameNSView()
170 { // NOTE: We only need a plain NSView here since we don't associate it with ourselves.
171 m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
172 [m_cocoaNSWindow setContentView: m_frameNSView];
173 [m_frameNSView addSubview:m_cocoaNSView];
175 NSRect frameRect = [m_frameNSView frame];
176 float tbarheight = 0.0;
180 NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
181 // If the toolbar doesn't have a superview then set it to our
183 if(![tbarNSView superview])
184 [m_frameNSView addSubview: tbarNSView];
185 // Do this after addSubView so that SetSize can work
186 m_frameToolBar->SetSize(m_frameToolBar->DoGetBestSize());
187 NSRect tbarRect = [tbarNSView frame];
188 tbarRect.size.width = frameRect.size.width;
189 tbarRect.origin.x = 0.0;
190 tbarRect.origin.y = frameRect.size.height - tbarRect.size.height;
191 [tbarNSView setFrame:tbarRect];
192 // width expands, bottom margin expands
193 [tbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMinYMargin];
194 tbarheight = tbarRect.size.height;
196 #endif //wxUSE_TOOLBAR
197 float sbarheight = 0.0;
200 NSView *sbarNSView = m_frameStatusBar->GetNSViewForSuperview();
201 if(![sbarNSView superview])
202 [m_frameNSView addSubview: sbarNSView];
203 NSRect sbarRect = [sbarNSView frame];
204 sbarRect.size.width = frameRect.size.width;
205 sbarRect.origin.x = 0.0;
206 sbarRect.origin.y = 0.0;
207 [sbarNSView setFrame:sbarRect];
208 // width expands, top margin expands
209 [sbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMaxYMargin];
210 sbarheight = sbarRect.size.height;
212 wxLogTrace(wxTRACE_COCOA,wxT("frame height=%f, tbar=%f, sbar=%f"),frameRect.size.height,tbarheight,sbarheight);
213 NSRect innerRect = [m_cocoaNSView frame];
214 innerRect.size.height = frameRect.size.height - tbarheight - sbarheight;
215 innerRect.origin.y = sbarheight;
216 [m_cocoaNSView setFrame:innerRect];
217 [m_cocoaNSView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
218 // Don't let the frame get smaller than the toolbar+statusbar height
219 NSRect frameMinRect = [NSWindow frameRectForContentRect:
220 NSMakeRect(0.0,0.0,0.0,tbarheight+sbarheight)
221 styleMask: [m_cocoaNSWindow styleMask]];
222 [m_cocoaNSWindow setMinSize:frameMinRect.size];
225 void wxFrame::SetStatusBar(wxStatusBar *statusbar)
229 [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
230 [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
231 if(m_frameStatusBar->GetParent())
232 m_frameStatusBar->GetParent()->CocoaAddChild(m_frameStatusBar);
234 m_frameStatusBar = statusbar;
237 m_frameStatusBar->CocoaRemoveFromParent();
242 wxStatusBar* wxFrame::CreateStatusBar(int number,
245 const wxString& name)
247 wxAutoNSAutoreleasePool pool;
248 wxFrameBase::CreateStatusBar(number,style,winid,name);
251 m_frameStatusBar->CocoaRemoveFromParent();
254 return m_frameStatusBar;
258 void wxFrame::SetToolBar(wxToolBar *toolbar)
262 m_frameToolBar->SetOwningFrame(NULL);
263 [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
264 [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
265 if(m_frameToolBar->GetParent())
266 m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
268 m_frameToolBar = toolbar;
271 m_frameToolBar->CocoaRemoveFromParent();
272 m_frameToolBar->SetOwningFrame(this);
277 wxToolBar* wxFrame::CreateToolBar(long style,
279 const wxString& name)
281 wxAutoNSAutoreleasePool pool;
282 return wxFrameBase::CreateToolBar(style,winid,name);
284 #endif // wxUSE_TOOLBAR
286 void wxFrame::PositionStatusBar()