]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/frame.mm
check that the version of __sync_sub_and_fetch that returns a value is supported...
[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
1c4e8f38 7// RCS-ID: $Id$
fb896a32 8// Copyright: (c) 2003 David Elliott
065e208e 9// Licence: wxWidgets licence
fb896a32
DE
10/////////////////////////////////////////////////////////////////////////////
11
26785f01 12#include "wx/wxprec.h"
76b49cf4
WS
13
14#include "wx/frame.h"
15
26785f01
DE
16#ifndef WX_PRECOMP
17 #include "wx/log.h"
18 #include "wx/app.h"
26785f01
DE
19 #include "wx/menu.h"
20 #include "wx/toolbar.h"
21 #include "wx/statusbr.h"
22#endif // WX_PRECOMP
23
7fc77f30 24#include "wx/cocoa/autorelease.h"
24c7767f 25#include "wx/cocoa/mbarman.h"
7fc77f30 26
fb896a32
DE
27#import <AppKit/NSWindow.h>
28#import <AppKit/NSApplication.h>
448cbf1d 29#import <AppKit/NSView.h>
205fef01 30#import <AppKit/NSMenuItem.h>
fb896a32
DE
31
32// wxFrame
33
34BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
35END_EVENT_TABLE()
36
37IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
38
39void wxFrame::Init()
40{
448cbf1d 41 m_frameNSView = nil;
fb896a32
DE
42}
43
44bool wxFrame::Create(wxWindow *parent,
45 wxWindowID winid,
46 const wxString& title,
47 const wxPoint& pos,
48 const wxSize& size,
49 long style,
50 const wxString& name)
51{
52 bool rt = wxTopLevelWindow::Create(parent,winid,title,pos,size,style,name);
53
54 return rt;
55}
56
57wxFrame::~wxFrame()
58{
448cbf1d 59 [m_frameNSView release];
fb896a32
DE
60}
61
205fef01
DE
62// -------------------------------------------------------------------
63// Menubar
fb896a32
DE
64void wxFrame::AttachMenuBar(wxMenuBar *mbar)
65{
66 wxFrameBase::AttachMenuBar(mbar);
243f5c2d 67 wxMenuBarManager::GetInstance()->UpdateMenuBar();
fb896a32
DE
68}
69
70void wxFrame::DetachMenuBar()
71{
fb896a32 72 wxFrameBase::DetachMenuBar();
243f5c2d 73 wxMenuBarManager::GetInstance()->UpdateMenuBar();
fb896a32
DE
74}
75
24c7767f 76void wxFrame::SetMenuBar(wxMenuBar *menubar)
fb896a32 77{
24c7767f
DE
78 if ( menubar == GetMenuBar() )
79 {
80 // nothing to do
81 return;
82 }
83
84 wxFrameBase::DetachMenuBar();
85 wxFrameBase::AttachMenuBar(menubar);
243f5c2d 86 wxMenuBarManager::GetInstance()->UpdateMenuBar();
fb896a32
DE
87}
88
8ded703d 89wxMenuBar* wxFrame::GetAppMenuBar(wxCocoaNSWindow *win)
3e84f98f
DE
90{
91 if(GetMenuBar())
92 return GetMenuBar();
8ded703d 93 return wxFrameBase::GetAppMenuBar(win);
3e84f98f
DE
94}
95
205fef01
DE
96void wxFrame::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
97{
98 wxLogTrace(wxTRACE_COCOA,wxT("wxFrame::wxMenuItemAction"));
99 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
100 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
101
102 wxMenu *menu = item->GetMenu();
103 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu!"));
104
105 // Since we're handling the delegate messages there's a very good chance
106 // we'll receive a menu action from an item with a nil target.
107 wxMenuBar *menubar = menu->GetMenuBar();
108 if(menubar)
109 {
110 wxFrame *frame = menubar->GetFrame();
111 wxASSERT_MSG(frame==this, wxT("Received wxMenuItemAction in NSWindow delegate from a menu item attached to a different frame."));
112 frame->ProcessCommand(item->GetId());
113 }
114 else
115 wxLogDebug(wxT("Received wxMenuItemAction in NSWindow delegate from an unknown menu item."));
116}
117
118bool wxFrame::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
119{
120 SEL itemAction = [menuItem action];
121 if(itemAction == @selector(wxMenuItemAction:))
122 {
123 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
124 wxCHECK_MSG(item,false,wxT("validateMenuItem received but no wxMenuItem exists!"));
125 // TODO: do more sanity checking
126 return item->IsEnabled();
127 }
128 // TODO: else if cut/copy/paste
129 wxLogDebug(wxT("Asked to validate an unknown menu item"));
130 return false;
131}
132
133// -------------------------------------------------------------------
134// Origin/Size
fb896a32
DE
135wxPoint wxFrame::GetClientAreaOrigin() const
136{
137 return wxPoint(0,0);
138}
139
e08efb8d 140void wxFrame::CocoaSetWxWindowSize(int width, int height)
a58a6614 141{
e08efb8d 142 if(m_frameStatusBar)
a58a6614 143 height += m_frameStatusBar->GetSize().y;
26785f01 144#if wxUSE_TOOLBAR
e08efb8d
DE
145 if(m_frameToolBar)
146 height += m_frameToolBar->GetSize().y;
26785f01 147#endif //wxUSE_TOOLBAR
e08efb8d 148 wxTopLevelWindow::CocoaSetWxWindowSize(width,height);
a58a6614
DE
149}
150
205fef01 151// -------------------------------------------------------------------
514e7b7b
DE
152WX_NSView wxFrame::GetNonClientNSView()
153{
154 if(m_frameNSView)
155 return m_frameNSView;
156 return GetNSViewForSuperview();
157}
158
448cbf1d
DE
159void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
160{
161 // If we have the additional toolbar/statbar view, then the
162 // default replaceSubview will work. Otherwise, the old view
163 // should be the content view and should be replaced that way
164 if(m_frameNSView)
165 wxWindow::CocoaReplaceView(oldView, newView);
166 else
167 wxTopLevelWindow::CocoaReplaceView(oldView, newView);
168}
169
170void wxFrame::UpdateFrameNSView()
171{
172 if(!m_frameNSView)
829a2e95 173 { // NOTE: We only need a plain NSView here since we don't associate it with ourselves.
448cbf1d
DE
174 m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
175 [m_cocoaNSWindow setContentView: m_frameNSView];
176 [m_frameNSView addSubview:m_cocoaNSView];
177 }
178 NSRect frameRect = [m_frameNSView frame];
179 float tbarheight = 0.0;
26785f01 180#if wxUSE_TOOLBAR
448cbf1d
DE
181 if(m_frameToolBar)
182 {
183 NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
9de6df96
DE
184 // If the toolbar doesn't have a superview then set it to our
185 // content view.
448cbf1d
DE
186 if(![tbarNSView superview])
187 [m_frameNSView addSubview: tbarNSView];
6613dc3f
DE
188 // Do this after addSubView so that SetSize can work
189 m_frameToolBar->SetSize(m_frameToolBar->DoGetBestSize());
448cbf1d
DE
190 NSRect tbarRect = [tbarNSView frame];
191 tbarRect.size.width = frameRect.size.width;
192 tbarRect.origin.x = 0.0;
193 tbarRect.origin.y = frameRect.size.height - tbarRect.size.height;
194 [tbarNSView setFrame:tbarRect];
195 // width expands, bottom margin expands
196 [tbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMinYMargin];
197 tbarheight = tbarRect.size.height;
198 }
26785f01 199#endif //wxUSE_TOOLBAR
448cbf1d
DE
200 float sbarheight = 0.0;
201 if(m_frameStatusBar)
202 {
203 NSView *sbarNSView = m_frameStatusBar->GetNSViewForSuperview();
204 if(![sbarNSView superview])
205 [m_frameNSView addSubview: sbarNSView];
206 NSRect sbarRect = [sbarNSView frame];
207 sbarRect.size.width = frameRect.size.width;
208 sbarRect.origin.x = 0.0;
209 sbarRect.origin.y = 0.0;
210 [sbarNSView setFrame:sbarRect];
211 // width expands, top margin expands
212 [sbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMaxYMargin];
213 sbarheight = sbarRect.size.height;
214 }
48580976 215 wxLogTrace(wxTRACE_COCOA,wxT("frame height=%f, tbar=%f, sbar=%f"),frameRect.size.height,tbarheight,sbarheight);
448cbf1d
DE
216 NSRect innerRect = [m_cocoaNSView frame];
217 innerRect.size.height = frameRect.size.height - tbarheight - sbarheight;
218 innerRect.origin.y = sbarheight;
219 [m_cocoaNSView setFrame:innerRect];
220 [m_cocoaNSView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
221 // Don't let the frame get smaller than the toolbar+statusbar height
222 NSRect frameMinRect = [NSWindow frameRectForContentRect:
223 NSMakeRect(0.0,0.0,0.0,tbarheight+sbarheight)
224 styleMask: [m_cocoaNSWindow styleMask]];
225 [m_cocoaNSWindow setMinSize:frameMinRect.size];
226}
227
228void wxFrame::SetStatusBar(wxStatusBar *statusbar)
229{
230 if(m_frameStatusBar)
231 {
232 [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
c5bd9191 233 [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
448cbf1d 234 if(m_frameStatusBar->GetParent())
26785f01 235 m_frameStatusBar->GetParent()->CocoaAddChild(m_frameStatusBar);
448cbf1d
DE
236 }
237 m_frameStatusBar = statusbar;
238 if(m_frameStatusBar)
239 {
240 m_frameStatusBar->CocoaRemoveFromParent();
241 }
242 UpdateFrameNSView();
243}
244
245wxStatusBar* wxFrame::CreateStatusBar(int number,
246 long style,
247 wxWindowID winid,
248 const wxString& name)
a58a6614 249{
e1f4ff6d 250 wxAutoNSAutoreleasePool pool;
448cbf1d
DE
251 wxFrameBase::CreateStatusBar(number,style,winid,name);
252 if(m_frameStatusBar)
253 {
254 m_frameStatusBar->CocoaRemoveFromParent();
255 }
256 UpdateFrameNSView();
257 return m_frameStatusBar;
258}
a58a6614 259
26785f01 260#if wxUSE_TOOLBAR
448cbf1d
DE
261void wxFrame::SetToolBar(wxToolBar *toolbar)
262{
263 if(m_frameToolBar)
264 {
6613dc3f 265 m_frameToolBar->SetOwningFrame(NULL);
448cbf1d 266 [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
c5bd9191 267 [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
448cbf1d
DE
268 if(m_frameToolBar->GetParent())
269 m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
270 }
271 m_frameToolBar = toolbar;
272 if(m_frameToolBar)
273 {
274 m_frameToolBar->CocoaRemoveFromParent();
6613dc3f 275 m_frameToolBar->SetOwningFrame(this);
448cbf1d
DE
276 }
277 UpdateFrameNSView();
278}
a58a6614 279
448cbf1d
DE
280wxToolBar* wxFrame::CreateToolBar(long style,
281 wxWindowID winid,
282 const wxString& name)
283{
e1f4ff6d 284 wxAutoNSAutoreleasePool pool;
9de6df96 285 return wxFrameBase::CreateToolBar(style,winid,name);
448cbf1d 286}
26785f01 287#endif // wxUSE_TOOLBAR
a58a6614 288
448cbf1d
DE
289void wxFrame::PositionStatusBar()
290{
a58a6614 291}