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