Implemented toolbar/statusbar positioning
[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::DoGetClientSize(int *width, int *height) const
95 {
96     wxFrameBase::DoGetClientSize(width,height);
97     if(height)
98     {
99         if(m_frameStatusBar && m_frameStatusBar->IsShown())
100             *height -= m_frameStatusBar->GetSize().y;
101     }
102 }
103
104 void wxFrame::DoSetClientSize(int width, int height)
105 {
106     if(m_frameStatusBar && m_frameStatusBar->IsShown())
107         height += m_frameStatusBar->GetSize().y;
108     wxFrameBase::DoSetClientSize(width,height);
109 }
110
111 void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
112 {
113     // If we have the additional toolbar/statbar view, then the
114     // default replaceSubview will work. Otherwise, the old view
115     // should be the content view and should be replaced that way
116     if(m_frameNSView)
117         wxWindow::CocoaReplaceView(oldView, newView);
118     else
119         wxTopLevelWindow::CocoaReplaceView(oldView, newView);
120 }
121
122 void wxFrame::UpdateFrameNSView()
123 {
124     if(!m_frameNSView)
125     {
126         m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
127         [m_cocoaNSWindow setContentView: m_frameNSView];
128         [m_frameNSView addSubview:m_cocoaNSView];
129     }
130     NSRect frameRect = [m_frameNSView frame];
131     float tbarheight = 0.0;
132     if(m_frameToolBar)
133     {
134         NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
135         if(![tbarNSView superview])
136             [m_frameNSView addSubview: tbarNSView];
137         NSRect tbarRect = [tbarNSView frame];
138         tbarRect.size.width = frameRect.size.width;
139         tbarRect.origin.x = 0.0;
140         tbarRect.origin.y = frameRect.size.height - tbarRect.size.height;
141         [tbarNSView setFrame:tbarRect];
142         // width expands, bottom margin expands
143         [tbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMinYMargin];
144         tbarheight = tbarRect.size.height;
145     }
146     float sbarheight = 0.0;
147     if(m_frameStatusBar)
148     {
149         NSView *sbarNSView = m_frameStatusBar->GetNSViewForSuperview();
150         if(![sbarNSView superview])
151             [m_frameNSView addSubview: sbarNSView];
152         NSRect sbarRect = [sbarNSView frame];
153         sbarRect.size.width = frameRect.size.width;
154         sbarRect.origin.x = 0.0;
155         sbarRect.origin.y = 0.0;
156         [sbarNSView setFrame:sbarRect];
157         // width expands, top margin expands
158         [sbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMaxYMargin];
159         sbarheight = sbarRect.size.height;
160     }
161     wxLogDebug("frame height=%f, tbar=%f, sbar=%f",frameRect.size.height,tbarheight,sbarheight);
162     NSRect innerRect = [m_cocoaNSView frame];
163     innerRect.size.height = frameRect.size.height - tbarheight - sbarheight;
164     innerRect.origin.y = sbarheight;
165     [m_cocoaNSView setFrame:innerRect];
166     [m_cocoaNSView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
167     // Don't let the frame get smaller than the toolbar+statusbar height
168     NSRect frameMinRect = [NSWindow frameRectForContentRect:
169             NSMakeRect(0.0,0.0,0.0,tbarheight+sbarheight)
170         styleMask: [m_cocoaNSWindow styleMask]];
171     [m_cocoaNSWindow setMinSize:frameMinRect.size];
172 }
173
174 void wxFrame::SetStatusBar(wxStatusBar *statusbar)
175 {
176     if(m_frameStatusBar)
177     {
178         [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
179         [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewNotSizable];
180         if(m_frameStatusBar->GetParent())
181             m_frameStatusBar->GetParent()->CocoaAddChild(m_frameToolBar);
182     }
183     m_frameStatusBar = statusbar;
184     if(m_frameStatusBar)
185     {
186         m_frameStatusBar->CocoaRemoveFromParent();
187     }
188     UpdateFrameNSView();
189 }
190
191 wxStatusBar* wxFrame::CreateStatusBar(int number,
192                                           long style,
193                                           wxWindowID winid,
194                                           const wxString& name)
195 {
196     wxFrameBase::CreateStatusBar(number,style,winid,name);
197     if(m_frameStatusBar)
198     {
199         m_frameStatusBar->CocoaRemoveFromParent();
200     }
201     UpdateFrameNSView();
202     return m_frameStatusBar;
203 }
204
205 void wxFrame::SetToolBar(wxToolBar *toolbar)
206 {
207     if(m_frameToolBar)
208     {
209         [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
210         [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewNotSizable];
211         if(m_frameToolBar->GetParent())
212             m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
213     }
214     m_frameToolBar = toolbar;
215     if(m_frameToolBar)
216     {
217         m_frameToolBar->CocoaRemoveFromParent();
218     }
219     UpdateFrameNSView();
220 }
221
222 wxToolBar* wxFrame::CreateToolBar(long style,
223                                       wxWindowID winid,
224                                       const wxString& name)
225 {
226     wxFrameBase::CreateToolBar(style,winid,name);
227     if(m_frameToolBar)
228     {
229         m_frameToolBar->CocoaRemoveFromParent();
230     }
231     UpdateFrameNSView();
232     return m_frameToolBar;
233 }
234
235 void wxFrame::PositionStatusBar()
236 {
237 }
238