Document domain parameter of wxTranslations::GetTranslatedString().
[wxWidgets.git] / src / cocoa / frame.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/frame.mm
3 // Purpose:     wxFrame
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2003/03/16
7 // Copyright:   (c) 2003 David Elliott
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #include "wx/frame.h"
14
15 #ifndef WX_PRECOMP
16     #include "wx/log.h"
17     #include "wx/app.h"
18     #include "wx/menu.h"
19     #include "wx/toolbar.h"
20     #include "wx/statusbr.h"
21 #endif // WX_PRECOMP
22
23 #include "wx/cocoa/autorelease.h"
24 #include "wx/cocoa/mbarman.h"
25
26 #import <AppKit/NSWindow.h>
27 #import <AppKit/NSApplication.h>
28 #import <AppKit/NSView.h>
29 #import <AppKit/NSMenuItem.h>
30
31 // wxFrame
32
33 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
34 END_EVENT_TABLE()
35
36 void wxFrame::Init()
37 {
38     m_frameNSView = nil;
39 }
40
41 bool 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
54 wxFrame::~wxFrame()
55 {
56     [m_frameNSView release];
57 }
58
59 // -------------------------------------------------------------------
60 // Menubar
61 void wxFrame::AttachMenuBar(wxMenuBar *mbar)
62 {
63     wxFrameBase::AttachMenuBar(mbar);
64     wxMenuBarManager::GetInstance()->UpdateMenuBar();
65 }
66
67 void wxFrame::DetachMenuBar()
68 {
69     wxFrameBase::DetachMenuBar();
70     wxMenuBarManager::GetInstance()->UpdateMenuBar();
71 }
72
73 void wxFrame::SetMenuBar(wxMenuBar *menubar)
74 {
75     if ( menubar == GetMenuBar() )
76     {
77         // nothing to do
78         return;
79     }
80
81     wxFrameBase::DetachMenuBar();
82     wxFrameBase::AttachMenuBar(menubar);
83     wxMenuBarManager::GetInstance()->UpdateMenuBar();
84 }
85
86 wxMenuBar* wxFrame::GetAppMenuBar(wxCocoaNSWindow *win)
87 {
88     if(GetMenuBar())
89         return GetMenuBar();
90     return wxFrameBase::GetAppMenuBar(win);
91 }
92
93 void 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
115 bool 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
132 wxPoint wxFrame::GetClientAreaOrigin() const
133 {
134     return wxPoint(0,0);
135 }
136
137 void wxFrame::CocoaSetWxWindowSize(int width, int height)
138 {
139     if(m_frameStatusBar)
140         height += m_frameStatusBar->GetSize().y;
141 #if wxUSE_TOOLBAR
142     if(m_frameToolBar)
143         height += m_frameToolBar->GetSize().y;
144 #endif //wxUSE_TOOLBAR
145     wxTopLevelWindow::CocoaSetWxWindowSize(width,height);
146 }
147
148 // -------------------------------------------------------------------
149 WX_NSView wxFrame::GetNonClientNSView()
150 {
151     if(m_frameNSView)
152         return m_frameNSView;
153     return GetNSViewForSuperview();
154 }
155
156 void 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
167 void wxFrame::UpdateFrameNSView()
168 {
169     if(!m_frameNSView)
170     {   // NOTE: We only need a plain NSView here since we don't associate it with ourselves.
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;
177 #if wxUSE_TOOLBAR
178     if(m_frameToolBar)
179     {
180         NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
181         // If the toolbar doesn't have a superview then set it to our
182         // content view.
183         if(![tbarNSView superview])
184             [m_frameNSView addSubview: tbarNSView];
185         // Do this after addSubView so that SetSize can work
186         m_frameToolBar->SetSize(m_frameToolBar->DoGetBestSize());
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     }
196 #endif //wxUSE_TOOLBAR
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     }
212     wxLogTrace(wxTRACE_COCOA,wxT("frame height=%f, tbar=%f, sbar=%f"),frameRect.size.height,tbarheight,sbarheight);
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
225 void wxFrame::SetStatusBar(wxStatusBar *statusbar)
226 {
227     if(m_frameStatusBar)
228     {
229         [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
230         [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
231         if(m_frameStatusBar->GetParent())
232             m_frameStatusBar->GetParent()->CocoaAddChild(m_frameStatusBar);
233     }
234     m_frameStatusBar = statusbar;
235     if(m_frameStatusBar)
236     {
237         m_frameStatusBar->CocoaRemoveFromParent();
238     }
239     UpdateFrameNSView();
240 }
241
242 wxStatusBar* wxFrame::CreateStatusBar(int number,
243                                           long style,
244                                           wxWindowID winid,
245                                           const wxString& name)
246 {
247     wxAutoNSAutoreleasePool pool;
248     wxFrameBase::CreateStatusBar(number,style,winid,name);
249     if(m_frameStatusBar)
250     {
251         m_frameStatusBar->CocoaRemoveFromParent();
252     }
253     UpdateFrameNSView();
254     return m_frameStatusBar;
255 }
256
257 #if wxUSE_TOOLBAR
258 void wxFrame::SetToolBar(wxToolBar *toolbar)
259 {
260     if(m_frameToolBar)
261     {
262         m_frameToolBar->SetOwningFrame(NULL);
263         [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
264         [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
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();
272         m_frameToolBar->SetOwningFrame(this);
273     }
274     UpdateFrameNSView();
275 }
276
277 wxToolBar* wxFrame::CreateToolBar(long style,
278                                       wxWindowID winid,
279                                       const wxString& name)
280 {
281     wxAutoNSAutoreleasePool pool;
282     return wxFrameBase::CreateToolBar(style,winid,name);
283 }
284 #endif // wxUSE_TOOLBAR
285
286 void wxFrame::PositionStatusBar()
287 {
288 }