]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/toplevel.mm
Change GetValue() and SetValue() to GetValueByRow() and SetValueByRow() in row based...
[wxWidgets.git] / src / cocoa / toplevel.mm
CommitLineData
fb896a32 1///////////////////////////////////////////////////////////////////////////////
8d8d3633 2// Name: src/cocoa/toplevel.mm
fb896a32 3// Purpose: implements wxTopLevelWindow for Cocoa
8d8d3633 4// Author: David Elliott
fb896a32
DE
5// Modified by:
6// Created: 2002/11/27
46cdffaf 7// RCS-ID: $Id$
fb896a32 8// Copyright: (c) 2002 David Elliott
065e208e 9// Licence: wxWidgets licence
fb896a32
DE
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
1832043f
WS
22
23#include "wx/toplevel.h"
24
fb896a32
DE
25#ifndef WX_PRECOMP
26 #include "wx/window.h"
fb896a32
DE
27 #include "wx/menuitem.h"
28 #include "wx/frame.h"
29 #include "wx/log.h"
30 #include "wx/app.h"
31#endif //WX_PRECOMP
32
7fc77f30 33#include "wx/cocoa/autorelease.h"
da026811 34#include "wx/cocoa/string.h"
6a5c31c2 35#include "wx/cocoa/ObjcRef.h"
7fc77f30 36
829a2e95
DE
37#include "wx/cocoa/objc/NSView.h"
38#include "wx/cocoa/objc/NSWindow.h"
080c7d56 39#import <AppKit/NSPanel.h>
e875c17d
DE
40#import <AppKit/NSButtonCell.h>
41#import <AppKit/NSControl.h>
a24aa427 42
fb896a32
DE
43// ----------------------------------------------------------------------------
44// globals
45// ----------------------------------------------------------------------------
46
47// list of all frames and modeless dialogs
48wxWindowList wxModelessWindows;
49
50// ============================================================================
51// wxTopLevelWindowCocoa implementation
52// ============================================================================
53
39050120
DE
54wxTopLevelWindowCocoa *wxTopLevelWindowCocoa::sm_cocoaDeactivateWindow = NULL;
55
fb896a32
DE
56// ----------------------------------------------------------------------------
57// wxTopLevelWindowCocoa creation
58// ----------------------------------------------------------------------------
fb896a32
DE
59BEGIN_EVENT_TABLE(wxTopLevelWindowCocoa,wxTopLevelWindowBase)
60 EVT_CLOSE(wxTopLevelWindowCocoa::OnCloseWindow)
61END_EVENT_TABLE()
62
63void wxTopLevelWindowCocoa::Init()
64{
65 m_iconized =
66 m_maximizeOnShow =
67 m_closed = false;
68}
69
080c7d56
DE
70unsigned int wxTopLevelWindowCocoa::NSWindowStyleForWxStyle(long style)
71{
72 unsigned int styleMask = 0;
73 if(style & wxCAPTION)
74 styleMask |= NSTitledWindowMask;
75 if(style & wxMINIMIZE_BOX)
76 styleMask |= NSMiniaturizableWindowMask;
77 #if 0
78 if(style & wxMAXIMIZE_BOX)
79 styleMask |= NSWindowMask;
80 #endif
81 if(style & wxCLOSE_BOX)
82 styleMask |= NSClosableWindowMask;
83 if(style & wxRESIZE_BORDER)
84 styleMask |= NSResizableWindowMask;
85 if(style & wxSIMPLE_BORDER)
86 styleMask |= NSBorderlessWindowMask;
87 return styleMask;
88}
89
15f37147
DE
90NSRect wxTopLevelWindowCocoa::MakeInitialNSWindowContentRect(const wxPoint& pos, const wxSize& size, unsigned int cocoaStyleMask)
91{
92 // Arbitrarily use (100,100) as the origin when default coords are given.
93 wxCoord x = pos.x!=wxDefaultCoord ? pos.x : 100;
94 wxCoord y = pos.y!=wxDefaultCoord ? pos.y : 100;
95
96 wxCoord w = WidthDefault(size.x);
97 wxCoord h = HeightDefault(size.y);
98
99 NSPoint cocoaOrigin = OriginInCocoaScreenCoordinatesForRectInWxDisplayCoordinates(x,y,w,h,true);
100
101 return [NSWindow
102 contentRectForFrameRect:NSMakeRect(cocoaOrigin.x,cocoaOrigin.y,w,h)
103 styleMask:cocoaStyleMask];
104
105}
106
fb896a32
DE
107bool wxTopLevelWindowCocoa::Create(wxWindow *parent,
108 wxWindowID winid,
109 const wxString& title,
110 const wxPoint& pos,
111 const wxSize& size,
112 long style,
113 const wxString& name)
114{
7fc77f30 115 wxAutoNSAutoreleasePool pool;
fb896a32
DE
116 wxTopLevelWindows.Append(this);
117
118 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
8d8d3633 119 return false;
fb896a32
DE
120
121 if ( parent )
122 parent->AddChild(this);
123
080c7d56
DE
124 unsigned int cocoaStyle = NSWindowStyleForWxStyle(style);
125 if(style & wxFRAME_TOOL_WINDOW)
126 cocoaStyle |= NSUtilityWindowMask;
fb896a32 127
15f37147 128 NSRect cocoaRect = MakeInitialNSWindowContentRect(pos,size,cocoaStyle);
fb896a32
DE
129
130 m_cocoaNSWindow = NULL;
131 m_cocoaNSView = NULL;
829a2e95
DE
132 // NOTE: We may need to deal with the contentView becoming a wx NSView as well.
133 NSWindow *newWindow;
134 // Create a WXNSPanel or a WXNSWindow depending on what type of window is desired.
080c7d56 135 if(style & wxFRAME_TOOL_WINDOW)
dc834029 136 newWindow = [[WX_GET_OBJC_CLASS(WXNSPanel) alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO];
080c7d56 137 else
dc834029 138 newWindow = [[WX_GET_OBJC_CLASS(WXNSWindow) alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO];
829a2e95 139 // Make sure the default content view is a WXNSView
a24aa427 140 [newWindow setContentView: [[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: [[newWindow contentView] frame]]];
829a2e95
DE
141 // Associate the window and view
142 SetNSWindow(newWindow);
143
fb896a32
DE
144 // NOTE: SetNSWindow has retained the Cocoa object for this object.
145 // Because we do not release on close, the following release matches the
146 // above alloc and thus the retain count will be 1.
147 [m_cocoaNSWindow release];
148
080c7d56
DE
149 if(style & wxFRAME_NO_TASKBAR)
150 [m_cocoaNSWindow setExcludedFromWindowsMenu: YES];
151 if(style & wxSTAY_ON_TOP)
152 [m_cocoaNSWindow setLevel:NSFloatingWindowLevel];
03cebc22 153 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
8d8d3633 154 return true;
fb896a32
DE
155}
156
157wxTopLevelWindowCocoa::~wxTopLevelWindowCocoa()
158{
39050120 159 wxASSERT(sm_cocoaDeactivateWindow!=this);
7fc77f30 160 wxAutoNSAutoreleasePool pool;
0b659b0a 161 DestroyChildren();
9c85202a
DE
162 if(m_cocoaNSView)
163 SendDestroyEvent();
fb896a32
DE
164 SetNSWindow(NULL);
165}
166
39050120
DE
167bool wxTopLevelWindowCocoa::Destroy()
168{
169 if(sm_cocoaDeactivateWindow==this)
170 {
171 sm_cocoaDeactivateWindow = NULL;
172 wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
173 }
174 return wxTopLevelWindowBase::Destroy();
175}
176
fb896a32
DE
177// ----------------------------------------------------------------------------
178// wxTopLevelWindowCocoa Cocoa Specifics
179// ----------------------------------------------------------------------------
180
8ded703d 181wxMenuBar* wxTopLevelWindowCocoa::GetAppMenuBar(wxCocoaNSWindow *win)
3e84f98f
DE
182{
183 wxTopLevelWindowCocoa *parent = wxDynamicCast(GetParent(),wxTopLevelWindow);
184 if(parent)
8ded703d 185 return parent->GetAppMenuBar(win);
3e84f98f
DE
186 return NULL;
187}
188
fb896a32
DE
189void wxTopLevelWindowCocoa::SetNSWindow(WX_NSWindow cocoaNSWindow)
190{
191 bool need_debug = cocoaNSWindow || m_cocoaNSWindow;
48580976 192 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [m_cocoaNSWindow=%p retainCount]=%d"),this,m_cocoaNSWindow,[m_cocoaNSWindow retainCount]);
fe169919 193 DisassociateNSWindow(m_cocoaNSWindow);
6a5c31c2
DE
194 wxGCSafeRetain(cocoaNSWindow);
195 wxGCSafeRelease(m_cocoaNSWindow);
fb896a32 196 m_cocoaNSWindow = cocoaNSWindow;
829a2e95
DE
197 // NOTE: We are no longer using posing so we won't get events on the
198 // window's view unless it was explicitly created as the wx view class.
fb896a32
DE
199 if(m_cocoaNSWindow)
200 SetNSView([m_cocoaNSWindow contentView]);
201 else
202 SetNSView(NULL);
fe169919 203 AssociateNSWindow(m_cocoaNSWindow);
48580976 204 if(need_debug) wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxTopLevelWindowCocoa=%p::SetNSWindow [cocoaNSWindow=%p retainCount]=%d"),this,cocoaNSWindow,[cocoaNSWindow retainCount]);
fb896a32
DE
205}
206
448cbf1d
DE
207void wxTopLevelWindowCocoa::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
208{
911e17c6 209 if([m_cocoaNSWindow contentView] == (id)oldView)
448cbf1d
DE
210 [m_cocoaNSWindow setContentView:newView];
211}
212
39050120
DE
213/*static*/ void wxTopLevelWindowCocoa::DeactivatePendingWindow()
214{
215 if(sm_cocoaDeactivateWindow)
216 sm_cocoaDeactivateWindow->wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey();
217 sm_cocoaDeactivateWindow = NULL;
218}
219
aa992c59 220void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeKey(void)
5aa417d5 221{
39050120 222 DeactivatePendingWindow();
48580976 223 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeKey"),this);
8d8d3633 224 wxActivateEvent event(wxEVT_ACTIVATE, true, GetId());
5aa417d5 225 event.SetEventObject(this);
937013e0 226 HandleWindowEvent(event);
5aa417d5
DE
227}
228
aa992c59 229void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignKey(void)
5aa417d5 230{
48580976 231 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignKey"),this);
8d8d3633 232 wxActivateEvent event(wxEVT_ACTIVATE, false, GetId());
5aa417d5 233 event.SetEventObject(this);
937013e0 234 HandleWindowEvent(event);
33faea0a
DE
235}
236
237void wxTopLevelWindowCocoa::CocoaDelegate_windowDidBecomeMain(void)
238{
48580976 239 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidBecomeMain"),this);
33faea0a
DE
240}
241
242void wxTopLevelWindowCocoa::CocoaDelegate_windowDidResignMain(void)
243{
48580976 244 wxLogTrace(wxTRACE_COCOA,wxT("wxTopLevelWindowCocoa=%p::CocoaDelegate_windowDidResignMain"),this);
5aa417d5
DE
245}
246
9692f42b 247void wxTopLevelWindowCocoa::CocoaDelegate_windowWillClose(void)
fb896a32
DE
248{
249 m_closed = true;
250 Destroy();
fb896a32
DE
251}
252
aa992c59 253bool wxTopLevelWindowCocoa::CocoaDelegate_windowShouldClose()
fb896a32
DE
254{
255 return wxWindowBase::Close(false);
256}
257
7dd8b1ea 258void wxTopLevelWindowCocoa::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
86adc758
DE
259{
260}
261
7dd8b1ea 262bool wxTopLevelWindowCocoa::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
86adc758
DE
263{
264 return false;
265}
266
fb896a32
DE
267// ----------------------------------------------------------------------------
268// wxTopLevelWindowCocoa maximize/minimize
269// ----------------------------------------------------------------------------
270
271void wxTopLevelWindowCocoa::Maximize(bool maximize)
272{
273}
274
275bool wxTopLevelWindowCocoa::IsMaximized() const
276{
8d8d3633 277 return false ;
fb896a32
DE
278}
279
280void wxTopLevelWindowCocoa::Iconize(bool iconize)
281{
282}
283
284bool wxTopLevelWindowCocoa::IsIconized() const
285{
8d8d3633 286 return false;
fb896a32
DE
287}
288
289void wxTopLevelWindowCocoa::Restore()
290{
291}
292
293bool wxTopLevelWindowCocoa::Show(bool show)
294{
cbb2499e
DE
295 if(m_isShown == show)
296 return false;
7fc77f30 297 wxAutoNSAutoreleasePool pool;
fb896a32 298 if(show)
275341c0 299 {
065e208e 300 // Send the window a size event because wxWidgets apps expect it
275341c0
DE
301 // NOTE: This should really only be done the first time a window
302 // is shown. I doubt this will cause any problems though.
303 wxSizeEvent event(GetSize(), GetId());
304 event.SetEventObject(this);
937013e0 305 HandleWindowEvent(event);
275341c0 306
fb896a32 307 [m_cocoaNSWindow makeKeyAndOrderFront:m_cocoaNSWindow];
275341c0 308 }
fb896a32
DE
309 else
310 [m_cocoaNSWindow orderOut:m_cocoaNSWindow];
cbb2499e 311 m_isShown = show;
fb896a32
DE
312 return true;
313}
314
315bool wxTopLevelWindowCocoa::Close(bool force)
316{
317 if(force)
318 return wxWindowBase::Close(force);
319 // performClose will fake the user clicking the close button which
320 // will invoke windowShouldClose which will call the base class version
321 // of Close() which will NOT Destroy() the window (see below) but
322 // if closing is not stopped, then performClose will go ahead and
9692f42b
DE
323 // close the window which will send the close notifications setting
324 // m_closed to true and Destroy()ing the window.
fb896a32
DE
325 [m_cocoaNSWindow performClose:m_cocoaNSWindow];
326 return m_closed;
327}
328
329void wxTopLevelWindowCocoa::OnCloseWindow(wxCloseEvent& event)
330{
331 // If the event was forced, close the window which will Destroy() it
332 if(!event.CanVeto())
333 [m_cocoaNSWindow close];
334 // if the event was not forced, it's probably because the user clicked
335 // the close button, or Close(false) was called which (see above) is
336 // redirected to performClose and thus Cocoa itself will close the window
337}
338
339// ----------------------------------------------------------------------------
340// wxTopLevelWindowCocoa misc
341// ----------------------------------------------------------------------------
342
8b6fd08a 343void wxTopLevelWindowCocoa::SetTitle(const wxString& title)
8d8d3633 344{
65aeaf19 345 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
8d8d3633
WS
346}
347
348wxString wxTopLevelWindowCocoa::GetTitle() const
349{
65aeaf19 350 return wxStringWithNSString([m_cocoaNSWindow title]);
8d8d3633
WS
351}
352
e875c17d
DE
353wxWindow* wxTopLevelWindowCocoa::SetDefaultItem(wxWindow *win)
354{
355 wxWindow *old = wxTopLevelWindowBase::SetDefaultItem(win);
e875c17d
DE
356
357 NSCell *newCell;
f3f56f2f
DE
358 if(win != NULL)
359 {
360 NSView *newView = win->GetNSView();
361 // newView does not have to be an NSControl, we only cast to NSControl*
362 // to silence the warning about cell not being implemented.
363 if(newView != nil && [newView respondsToSelector:@selector(cell)])
364 newCell = [(NSControl*)newView cell];
365 else
366 newCell = nil;
367
368 if(newCell != nil && ![newCell isKindOfClass:[NSButtonCell class]])
369 { // It's not an NSButtonCell, set the default to nil.
370 newCell = nil;
371 }
372 }
e875c17d
DE
373 else
374 newCell = nil;
375
e875c17d
DE
376 [GetNSWindow() setDefaultButtonCell:(NSButtonCell*)newCell];
377 return old;
378}
379
fb896a32
DE
380bool wxTopLevelWindowCocoa::ShowFullScreen(bool show, long style)
381{
8d8d3633 382 return false;
fb896a32
DE
383}
384
385bool wxTopLevelWindowCocoa::IsFullScreen() const
386{
8d8d3633 387 return false;
fb896a32
DE
388}
389
e08efb8d
DE
390void wxTopLevelWindowCocoa::CocoaSetWxWindowSize(int width, int height)
391{
392 // Set the NSView size by setting the frame size to enclose it
393 unsigned int styleMask = [m_cocoaNSWindow styleMask];
03859c91 394 NSRect oldFrameRect = [m_cocoaNSWindow frame];
e08efb8d 395 NSRect contentRect = [NSWindow
03859c91 396 contentRectForFrameRect: oldFrameRect
e08efb8d
DE
397 styleMask: styleMask];
398 contentRect.size.width = width;
399 contentRect.size.height = height;
03859c91 400 NSRect newFrameRect = [NSWindow
e08efb8d
DE
401 frameRectForContentRect: contentRect
402 styleMask: styleMask];
03859c91
DE
403
404 // Cocoa uses +y is up but wxWidgets uses +y is down. We want an increase/decrease in height
405 // to not effect where the top of the window is placed so we set the new y origin relative the
406 // old one taking the height change into account.
407 newFrameRect.origin.y = oldFrameRect.origin.y + oldFrameRect.size.height - newFrameRect.size.height;
408
409 [m_cocoaNSWindow setFrame: newFrameRect display: NO];
e08efb8d
DE
410}
411
fb896a32
DE
412void wxTopLevelWindowCocoa::DoMoveWindow(int x, int y, int width, int height)
413{
9879fa84 414 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoMoveWindow(%d,%d,%d,%d)"),this,x,y,width,height);
fb896a32 415
15f37147
DE
416 NSPoint cocoaOrigin = OriginInCocoaScreenCoordinatesForRectInWxDisplayCoordinates(x,y,width,height,false);
417
418 NSRect cocoaRect = NSMakeRect(cocoaOrigin.x,cocoaOrigin.y,width,height);
fb896a32
DE
419 [m_cocoaNSWindow setFrame: cocoaRect display:NO];
420}
421
422void wxTopLevelWindowCocoa::DoGetSize(int *w, int *h) const
423{
424 NSRect cocoaRect = [m_cocoaNSWindow frame];
425 if(w)
426 *w=(int)cocoaRect.size.width;
427 if(h)
428 *h=(int)cocoaRect.size.height;
9879fa84 429 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetSize = (%d,%d)"),this,(int)cocoaRect.size.width,(int)cocoaRect.size.height);
fb896a32
DE
430}
431
432void wxTopLevelWindowCocoa::DoGetPosition(int *x, int *y) const
433{
15f37147
DE
434 NSRect windowFrame = [m_cocoaNSWindow frame];
435
436 wxPoint theWxOrigin = OriginInWxDisplayCoordinatesForRectInCocoaScreenCoordinates(windowFrame);
437
438 if(*x)
439 *x = theWxOrigin.x;
440 if(*y)
441 *y = theWxOrigin.y;
442
443 wxLogTrace(wxTRACE_COCOA_TopLevelWindow_Size,wxT("wxTopLevelWindow=%p::DoGetPosition = (%d,%d)"),this,(int)theWxOrigin.x,(int)theWxOrigin.y);
fb896a32 444}