]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/frame.mm
use SetClientSize(), not SetSize() in Fit()
[wxWidgets.git] / src / cocoa / frame.mm
CommitLineData
fb896a32
DE
1/////////////////////////////////////////////////////////////////////////////
2// Name: cocoa/frame.mm
3// Purpose: wxFrame
4// Author: David Elliott
5// Modified by:
6// Created: 2003/03/16
7// RCS-ID: $Id:
8// Copyright: (c) 2003 David Elliott
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
26785f01
DE
12#include "wx/wxprec.h"
13#ifndef WX_PRECOMP
14 #include "wx/log.h"
15 #include "wx/app.h"
16 #include "wx/frame.h"
17 #include "wx/menu.h"
18 #include "wx/toolbar.h"
19 #include "wx/statusbr.h"
20#endif // WX_PRECOMP
21
7fc77f30
DE
22#include "wx/cocoa/autorelease.h"
23
fb896a32
DE
24#import <AppKit/NSWindow.h>
25#import <AppKit/NSApplication.h>
448cbf1d 26#import <AppKit/NSView.h>
fb896a32
DE
27
28// wxFrame
29
30BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
31END_EVENT_TABLE()
32
33IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
34
35void wxFrame::Init()
36{
448cbf1d 37 m_frameNSView = nil;
fb896a32
DE
38}
39
40bool wxFrame::Create(wxWindow *parent,
41 wxWindowID winid,
42 const wxString& title,
43 const wxPoint& pos,
44 const wxSize& size,
45 long style,
46 const wxString& name)
47{
48 bool rt = wxTopLevelWindow::Create(parent,winid,title,pos,size,style,name);
49
50 return rt;
51}
52
53wxFrame::~wxFrame()
54{
448cbf1d 55 [m_frameNSView release];
fb896a32
DE
56}
57
fb896a32
DE
58void wxFrame::AttachMenuBar(wxMenuBar *mbar)
59{
60 wxFrameBase::AttachMenuBar(mbar);
61 if(m_frameMenuBar)
62 {
63 wxLogDebug("Attached menu");
64 [m_cocoaNSWindow setMenu:m_frameMenuBar->GetNSMenu()];
65 }
66}
67
68void wxFrame::DetachMenuBar()
69{
70 if(m_frameMenuBar)
71 {
72 [m_cocoaNSWindow setMenu:nil];
73 }
74 wxFrameBase::DetachMenuBar();
75}
76
77bool wxFrame::Show(bool show)
78{
7fc77f30 79 wxAutoNSAutoreleasePool pool;
fb896a32
DE
80 bool ret = wxFrameBase::Show(show);
81 if(show && GetMenuBar())
82 [wxTheApp->GetNSApplication() setMenu:GetMenuBar()->GetNSMenu() ];
83 return ret;
84}
85
86wxPoint wxFrame::GetClientAreaOrigin() const
87{
88 return wxPoint(0,0);
89}
90
e08efb8d 91void wxFrame::CocoaSetWxWindowSize(int width, int height)
a58a6614 92{
e08efb8d 93 if(m_frameStatusBar)
a58a6614 94 height += m_frameStatusBar->GetSize().y;
26785f01 95#if wxUSE_TOOLBAR
e08efb8d
DE
96 if(m_frameToolBar)
97 height += m_frameToolBar->GetSize().y;
26785f01 98#endif //wxUSE_TOOLBAR
e08efb8d 99 wxTopLevelWindow::CocoaSetWxWindowSize(width,height);
a58a6614
DE
100}
101
448cbf1d
DE
102void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
103{
104 // If we have the additional toolbar/statbar view, then the
105 // default replaceSubview will work. Otherwise, the old view
106 // should be the content view and should be replaced that way
107 if(m_frameNSView)
108 wxWindow::CocoaReplaceView(oldView, newView);
109 else
110 wxTopLevelWindow::CocoaReplaceView(oldView, newView);
111}
112
113void wxFrame::UpdateFrameNSView()
114{
115 if(!m_frameNSView)
116 {
117 m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
118 [m_cocoaNSWindow setContentView: m_frameNSView];
119 [m_frameNSView addSubview:m_cocoaNSView];
120 }
121 NSRect frameRect = [m_frameNSView frame];
122 float tbarheight = 0.0;
26785f01 123#if wxUSE_TOOLBAR
448cbf1d
DE
124 if(m_frameToolBar)
125 {
126 NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
127 if(![tbarNSView superview])
128 [m_frameNSView addSubview: tbarNSView];
129 NSRect tbarRect = [tbarNSView frame];
130 tbarRect.size.width = frameRect.size.width;
131 tbarRect.origin.x = 0.0;
132 tbarRect.origin.y = frameRect.size.height - tbarRect.size.height;
133 [tbarNSView setFrame:tbarRect];
134 // width expands, bottom margin expands
135 [tbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMinYMargin];
136 tbarheight = tbarRect.size.height;
137 }
26785f01 138#endif //wxUSE_TOOLBAR
448cbf1d
DE
139 float sbarheight = 0.0;
140 if(m_frameStatusBar)
141 {
142 NSView *sbarNSView = m_frameStatusBar->GetNSViewForSuperview();
143 if(![sbarNSView superview])
144 [m_frameNSView addSubview: sbarNSView];
145 NSRect sbarRect = [sbarNSView frame];
146 sbarRect.size.width = frameRect.size.width;
147 sbarRect.origin.x = 0.0;
148 sbarRect.origin.y = 0.0;
149 [sbarNSView setFrame:sbarRect];
150 // width expands, top margin expands
151 [sbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMaxYMargin];
152 sbarheight = sbarRect.size.height;
153 }
154 wxLogDebug("frame height=%f, tbar=%f, sbar=%f",frameRect.size.height,tbarheight,sbarheight);
155 NSRect innerRect = [m_cocoaNSView frame];
156 innerRect.size.height = frameRect.size.height - tbarheight - sbarheight;
157 innerRect.origin.y = sbarheight;
158 [m_cocoaNSView setFrame:innerRect];
159 [m_cocoaNSView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
160 // Don't let the frame get smaller than the toolbar+statusbar height
161 NSRect frameMinRect = [NSWindow frameRectForContentRect:
162 NSMakeRect(0.0,0.0,0.0,tbarheight+sbarheight)
163 styleMask: [m_cocoaNSWindow styleMask]];
164 [m_cocoaNSWindow setMinSize:frameMinRect.size];
165}
166
167void wxFrame::SetStatusBar(wxStatusBar *statusbar)
168{
169 if(m_frameStatusBar)
170 {
171 [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
c5bd9191 172 [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
448cbf1d 173 if(m_frameStatusBar->GetParent())
26785f01 174 m_frameStatusBar->GetParent()->CocoaAddChild(m_frameStatusBar);
448cbf1d
DE
175 }
176 m_frameStatusBar = statusbar;
177 if(m_frameStatusBar)
178 {
179 m_frameStatusBar->CocoaRemoveFromParent();
180 }
181 UpdateFrameNSView();
182}
183
184wxStatusBar* wxFrame::CreateStatusBar(int number,
185 long style,
186 wxWindowID winid,
187 const wxString& name)
a58a6614 188{
448cbf1d
DE
189 wxFrameBase::CreateStatusBar(number,style,winid,name);
190 if(m_frameStatusBar)
191 {
192 m_frameStatusBar->CocoaRemoveFromParent();
193 }
194 UpdateFrameNSView();
195 return m_frameStatusBar;
196}
a58a6614 197
26785f01 198#if wxUSE_TOOLBAR
448cbf1d
DE
199void wxFrame::SetToolBar(wxToolBar *toolbar)
200{
201 if(m_frameToolBar)
202 {
203 [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
c5bd9191 204 [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
448cbf1d
DE
205 if(m_frameToolBar->GetParent())
206 m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
207 }
208 m_frameToolBar = toolbar;
209 if(m_frameToolBar)
210 {
211 m_frameToolBar->CocoaRemoveFromParent();
212 }
213 UpdateFrameNSView();
214}
a58a6614 215
448cbf1d
DE
216wxToolBar* wxFrame::CreateToolBar(long style,
217 wxWindowID winid,
218 const wxString& name)
219{
220 wxFrameBase::CreateToolBar(style,winid,name);
221 if(m_frameToolBar)
222 {
223 m_frameToolBar->CocoaRemoveFromParent();
224 }
225 UpdateFrameNSView();
226 return m_frameToolBar;
227}
26785f01 228#endif // wxUSE_TOOLBAR
a58a6614 229
448cbf1d
DE
230void wxFrame::PositionStatusBar()
231{
a58a6614
DE
232}
233