]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/frame.mm
Add virtual ~wxAnyScrollHelperBase() to fix compiler warning.
[wxWidgets.git] / src / cocoa / frame.mm
CommitLineData
fb896a32 1/////////////////////////////////////////////////////////////////////////////
76b49cf4 2// Name: src/cocoa/frame.mm
fb896a32
DE
3// Purpose: wxFrame
4// Author: David Elliott
5// Modified by:
6// Created: 2003/03/16
fb896a32 7// Copyright: (c) 2003 David Elliott
526954c5 8// Licence: wxWindows licence
fb896a32
DE
9/////////////////////////////////////////////////////////////////////////////
10
26785f01 11#include "wx/wxprec.h"
76b49cf4
WS
12
13#include "wx/frame.h"
14
26785f01
DE
15#ifndef WX_PRECOMP
16 #include "wx/log.h"
17 #include "wx/app.h"
26785f01
DE
18 #include "wx/menu.h"
19 #include "wx/toolbar.h"
20 #include "wx/statusbr.h"
21#endif // WX_PRECOMP
22
7fc77f30 23#include "wx/cocoa/autorelease.h"
24c7767f 24#include "wx/cocoa/mbarman.h"
7fc77f30 25
fb896a32
DE
26#import <AppKit/NSWindow.h>
27#import <AppKit/NSApplication.h>
448cbf1d 28#import <AppKit/NSView.h>
205fef01 29#import <AppKit/NSMenuItem.h>
fb896a32
DE
30
31// wxFrame
32
33BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
34END_EVENT_TABLE()
35
fb896a32
DE
36void wxFrame::Init()
37{
448cbf1d 38 m_frameNSView = nil;
fb896a32
DE
39}
40
41bool wxFrame::Create(wxWindow *parent,
42 wxWindowID winid,
43 const wxString& title,
44 const wxPoint& pos,
45 const wxSize& size,
46 long style,
47 const wxString& name)
48{
49 bool rt = wxTopLevelWindow::Create(parent,winid,title,pos,size,style,name);
50
51 return rt;
52}
53
54wxFrame::~wxFrame()
55{
448cbf1d 56 [m_frameNSView release];
fb896a32
DE
57}
58
205fef01
DE
59// -------------------------------------------------------------------
60// Menubar
fb896a32
DE
61void wxFrame::AttachMenuBar(wxMenuBar *mbar)
62{
63 wxFrameBase::AttachMenuBar(mbar);
243f5c2d 64 wxMenuBarManager::GetInstance()->UpdateMenuBar();
fb896a32
DE
65}
66
67void wxFrame::DetachMenuBar()
68{
fb896a32 69 wxFrameBase::DetachMenuBar();
243f5c2d 70 wxMenuBarManager::GetInstance()->UpdateMenuBar();
fb896a32
DE
71}
72
24c7767f 73void wxFrame::SetMenuBar(wxMenuBar *menubar)
fb896a32 74{
24c7767f
DE
75 if ( menubar == GetMenuBar() )
76 {
77 // nothing to do
78 return;
79 }
80
81 wxFrameBase::DetachMenuBar();
82 wxFrameBase::AttachMenuBar(menubar);
243f5c2d 83 wxMenuBarManager::GetInstance()->UpdateMenuBar();
fb896a32
DE
84}
85
8ded703d 86wxMenuBar* wxFrame::GetAppMenuBar(wxCocoaNSWindow *win)
3e84f98f
DE
87{
88 if(GetMenuBar())
89 return GetMenuBar();
8ded703d 90 return wxFrameBase::GetAppMenuBar(win);
3e84f98f
DE
91}
92
205fef01
DE
93void wxFrame::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
94{
95 wxLogTrace(wxTRACE_COCOA,wxT("wxFrame::wxMenuItemAction"));
96 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
97 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
98
99 wxMenu *menu = item->GetMenu();
100 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu!"));
101
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();
105 if(menubar)
106 {
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());
110 }
111 else
112 wxLogDebug(wxT("Received wxMenuItemAction in NSWindow delegate from an unknown menu item."));
113}
114
115bool wxFrame::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
116{
117 SEL itemAction = [menuItem action];
118 if(itemAction == @selector(wxMenuItemAction:))
119 {
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();
124 }
125 // TODO: else if cut/copy/paste
126 wxLogDebug(wxT("Asked to validate an unknown menu item"));
127 return false;
128}
129
130// -------------------------------------------------------------------
131// Origin/Size
fb896a32
DE
132wxPoint wxFrame::GetClientAreaOrigin() const
133{
134 return wxPoint(0,0);
135}
136
e08efb8d 137void wxFrame::CocoaSetWxWindowSize(int width, int height)
a58a6614 138{
e08efb8d 139 if(m_frameStatusBar)
a58a6614 140 height += m_frameStatusBar->GetSize().y;
26785f01 141#if wxUSE_TOOLBAR
e08efb8d
DE
142 if(m_frameToolBar)
143 height += m_frameToolBar->GetSize().y;
26785f01 144#endif //wxUSE_TOOLBAR
e08efb8d 145 wxTopLevelWindow::CocoaSetWxWindowSize(width,height);
a58a6614
DE
146}
147
205fef01 148// -------------------------------------------------------------------
514e7b7b
DE
149WX_NSView wxFrame::GetNonClientNSView()
150{
151 if(m_frameNSView)
152 return m_frameNSView;
153 return GetNSViewForSuperview();
154}
155
448cbf1d
DE
156void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
157{
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
161 if(m_frameNSView)
162 wxWindow::CocoaReplaceView(oldView, newView);
163 else
164 wxTopLevelWindow::CocoaReplaceView(oldView, newView);
165}
166
167void wxFrame::UpdateFrameNSView()
168{
169 if(!m_frameNSView)
829a2e95 170 { // NOTE: We only need a plain NSView here since we don't associate it with ourselves.
448cbf1d
DE
171 m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
172 [m_cocoaNSWindow setContentView: m_frameNSView];
173 [m_frameNSView addSubview:m_cocoaNSView];
174 }
175 NSRect frameRect = [m_frameNSView frame];
176 float tbarheight = 0.0;
26785f01 177#if wxUSE_TOOLBAR
448cbf1d
DE
178 if(m_frameToolBar)
179 {
180 NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
9de6df96
DE
181 // If the toolbar doesn't have a superview then set it to our
182 // content view.
448cbf1d
DE
183 if(![tbarNSView superview])
184 [m_frameNSView addSubview: tbarNSView];
6613dc3f
DE
185 // Do this after addSubView so that SetSize can work
186 m_frameToolBar->SetSize(m_frameToolBar->DoGetBestSize());
448cbf1d
DE
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;
195 }
26785f01 196#endif //wxUSE_TOOLBAR
448cbf1d
DE
197 float sbarheight = 0.0;
198 if(m_frameStatusBar)
199 {
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;
211 }
48580976 212 wxLogTrace(wxTRACE_COCOA,wxT("frame height=%f, tbar=%f, sbar=%f"),frameRect.size.height,tbarheight,sbarheight);
448cbf1d
DE
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];
223}
224
225void wxFrame::SetStatusBar(wxStatusBar *statusbar)
226{
227 if(m_frameStatusBar)
228 {
229 [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
c5bd9191 230 [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
448cbf1d 231 if(m_frameStatusBar->GetParent())
26785f01 232 m_frameStatusBar->GetParent()->CocoaAddChild(m_frameStatusBar);
448cbf1d
DE
233 }
234 m_frameStatusBar = statusbar;
235 if(m_frameStatusBar)
236 {
237 m_frameStatusBar->CocoaRemoveFromParent();
238 }
239 UpdateFrameNSView();
240}
241
242wxStatusBar* wxFrame::CreateStatusBar(int number,
243 long style,
244 wxWindowID winid,
245 const wxString& name)
a58a6614 246{
e1f4ff6d 247 wxAutoNSAutoreleasePool pool;
448cbf1d
DE
248 wxFrameBase::CreateStatusBar(number,style,winid,name);
249 if(m_frameStatusBar)
250 {
251 m_frameStatusBar->CocoaRemoveFromParent();
252 }
253 UpdateFrameNSView();
254 return m_frameStatusBar;
255}
a58a6614 256
26785f01 257#if wxUSE_TOOLBAR
448cbf1d
DE
258void wxFrame::SetToolBar(wxToolBar *toolbar)
259{
260 if(m_frameToolBar)
261 {
6613dc3f 262 m_frameToolBar->SetOwningFrame(NULL);
448cbf1d 263 [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
c5bd9191 264 [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
448cbf1d
DE
265 if(m_frameToolBar->GetParent())
266 m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
267 }
268 m_frameToolBar = toolbar;
269 if(m_frameToolBar)
270 {
271 m_frameToolBar->CocoaRemoveFromParent();
6613dc3f 272 m_frameToolBar->SetOwningFrame(this);
448cbf1d
DE
273 }
274 UpdateFrameNSView();
275}
a58a6614 276
448cbf1d
DE
277wxToolBar* wxFrame::CreateToolBar(long style,
278 wxWindowID winid,
279 const wxString& name)
280{
e1f4ff6d 281 wxAutoNSAutoreleasePool pool;
9de6df96 282 return wxFrameBase::CreateToolBar(style,winid,name);
448cbf1d 283}
26785f01 284#endif // wxUSE_TOOLBAR
a58a6614 285
448cbf1d
DE
286void wxFrame::PositionStatusBar()
287{
a58a6614 288}