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