]> git.saurik.com Git - wxWidgets.git/blame - src/osx/window_osx.cpp
avoid using deprecated gdk_cursor_unref() with GTK3
[wxWidgets.git] / src / osx / window_osx.cpp
CommitLineData
524c47aa 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/osx/window_osx.cpp
524c47aa
SC
3// Purpose: wxWindowMac
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
b5b208a1 7// RCS-ID: $Id$
524c47aa
SC
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#include "wx/window.h"
15
16#ifndef WX_PRECOMP
17 #include "wx/log.h"
18 #include "wx/app.h"
19 #include "wx/utils.h"
20 #include "wx/panel.h"
21 #include "wx/frame.h"
22 #include "wx/dc.h"
23 #include "wx/dcclient.h"
24 #include "wx/button.h"
25 #include "wx/menu.h"
26 #include "wx/dialog.h"
27 #include "wx/settings.h"
28 #include "wx/msgdlg.h"
29 #include "wx/scrolbar.h"
30 #include "wx/statbox.h"
31 #include "wx/textctrl.h"
32 #include "wx/toolbar.h"
33 #include "wx/layout.h"
34 #include "wx/statusbr.h"
35 #include "wx/menuitem.h"
36 #include "wx/treectrl.h"
37 #include "wx/listctrl.h"
38#endif
39
40#include "wx/tooltip.h"
41#include "wx/spinctrl.h"
42#include "wx/geometry.h"
43
44#if wxUSE_LISTCTRL
45 #include "wx/listctrl.h"
46#endif
47
48#if wxUSE_TREECTRL
49 #include "wx/treectrl.h"
50#endif
51
52#if wxUSE_CARET
53 #include "wx/caret.h"
54#endif
55
56#if wxUSE_POPUPWIN
57 #include "wx/popupwin.h"
58#endif
59
60#if wxUSE_DRAG_AND_DROP
0e1d0c53 61 #include "wx/dnd.h"
524c47aa
SC
62#endif
63
f55d9f74
SC
64#include "wx/graphics.h"
65
524c47aa 66#if wxOSX_USE_CARBON
0e1d0c53 67 #include "wx/osx/uma.h"
524c47aa 68#else
0e1d0c53 69 #include "wx/osx/private.h"
11fed901 70#endif
524c47aa
SC
71
72#define MAC_SCROLLBAR_SIZE 15
73#define MAC_SMALL_SCROLLBAR_SIZE 11
74
75#include <string.h>
76
77#ifdef __WXUNIVERSAL__
78 IMPLEMENT_ABSTRACT_CLASS(wxWindowMac, wxWindowBase)
524c47aa
SC
79#endif
80
81BEGIN_EVENT_TABLE(wxWindowMac, wxWindowBase)
524c47aa
SC
82 EVT_MOUSE_EVENTS(wxWindowMac::OnMouseEvent)
83END_EVENT_TABLE()
84
85#define wxMAC_DEBUG_REDRAW 0
86#ifndef wxMAC_DEBUG_REDRAW
87#define wxMAC_DEBUG_REDRAW 0
88#endif
89
d15694e8
SC
90wxWidgetImplType* kOSXNoWidgetImpl = (wxWidgetImplType*) -1L;
91
c1313b54
SC
92#if wxUSE_HOTKEY && wxOSX_USE_COCOA_OR_CARBON
93
94typedef struct {
95 EventHotKeyRef ref;
96 int keyId;
97 wxWindow* window;
98} wxHotKeyRec;
99
100wxVector<wxHotKeyRec> s_hotkeys;
101
102#endif
103
524c47aa
SC
104// ===========================================================================
105// implementation
106// ===========================================================================
107
82f8ea85
SC
108// the grow box has to be implemented as an inactive window, so that nothing can direct
109// the focus to it
110
111class WXDLLIMPEXP_CORE wxBlindPlateWindow : public wxWindow
112{
113public:
114 wxBlindPlateWindow() { Init(); }
c59aa14a 115
82f8ea85
SC
116 // Old-style constructor (no default values for coordinates to avoid
117 // ambiguity with the new one)
118 wxBlindPlateWindow(wxWindow *parent,
119 int x, int y, int width, int height,
120 long style = wxTAB_TRAVERSAL | wxNO_BORDER,
121 const wxString& name = wxPanelNameStr)
122 {
123 Init();
c59aa14a 124
82f8ea85
SC
125 Create(parent, wxID_ANY, wxPoint(x, y), wxSize(width, height), style, name);
126 }
c59aa14a 127
82f8ea85
SC
128 // Constructor
129 wxBlindPlateWindow(wxWindow *parent,
130 wxWindowID winid = wxID_ANY,
131 const wxPoint& pos = wxDefaultPosition,
132 const wxSize& size = wxDefaultSize,
133 long style = wxTAB_TRAVERSAL | wxNO_BORDER,
134 const wxString& name = wxPanelNameStr)
135 {
136 Init();
c59aa14a 137
82f8ea85
SC
138 Create(parent, winid, pos, size, style, name);
139 }
c59aa14a 140
82f8ea85
SC
141 // Pseudo ctor
142 bool Create(wxWindow *parent,
143 wxWindowID winid = wxID_ANY,
144 const wxPoint& pos = wxDefaultPosition,
145 const wxSize& size = wxDefaultSize,
146 long style = wxTAB_TRAVERSAL | wxNO_BORDER,
147 const wxString& name = wxPanelNameStr)
148 {
149 if ( !wxWindow::Create(parent, winid, pos, size, style, name) )
150 return false;
c59aa14a 151
82f8ea85
SC
152 // so that non-solid background renders correctly under GTK+:
153 SetThemeEnabled(true);
48159545 154 return true;
82f8ea85 155 }
c59aa14a 156
82f8ea85 157 virtual ~wxBlindPlateWindow();
c59aa14a 158
82f8ea85
SC
159 virtual bool AcceptsFocus() const
160 {
161 return false;
162 }
c59aa14a 163
82f8ea85
SC
164protected:
165 // common part of all ctors
166 void Init()
167 {
168 }
c59aa14a 169
82f8ea85
SC
170 DECLARE_DYNAMIC_CLASS_NO_COPY(wxBlindPlateWindow)
171 DECLARE_EVENT_TABLE()
172};
173
174wxBlindPlateWindow::~wxBlindPlateWindow()
175{
176}
177
178IMPLEMENT_DYNAMIC_CLASS(wxBlindPlateWindow, wxWindow)
179
180BEGIN_EVENT_TABLE(wxBlindPlateWindow, wxWindow)
181END_EVENT_TABLE()
182
183
524c47aa
SC
184// ----------------------------------------------------------------------------
185 // constructors and such
186// ----------------------------------------------------------------------------
187
188wxWindowMac::wxWindowMac()
189{
190 Init();
191}
192
193wxWindowMac::wxWindowMac(wxWindowMac *parent,
194 wxWindowID id,
195 const wxPoint& pos ,
196 const wxSize& size ,
197 long style ,
198 const wxString& name )
199{
200 Init();
201 Create(parent, id, pos, size, style, name);
202}
203
204void wxWindowMac::Init()
205{
206 m_peer = NULL ;
207 m_macAlpha = 255 ;
208 m_cgContextRef = NULL ;
209
210 // as all windows are created with WS_VISIBLE style...
211 m_isShown = true;
212
213 m_hScrollBar = NULL ;
214 m_vScrollBar = NULL ;
215 m_hScrollBarAlwaysShown = false;
216 m_vScrollBarAlwaysShown = false;
2ae3afa0 217 m_growBox = NULL ;
524c47aa 218
524c47aa
SC
219 m_clipChildren = false ;
220 m_cachedClippedRectValid = false ;
17e2694c 221 m_isNativeWindowWrapper = false;
524c47aa
SC
222}
223
224wxWindowMac::~wxWindowMac()
225{
226 SendDestroyEvent();
c1313b54
SC
227
228#if wxUSE_HOTKEY && wxOSX_USE_COCOA_OR_CARBON
229 for ( int i = s_hotkeys.size()-1; i>=0; -- i )
230 {
231 if ( s_hotkeys[i].window == this )
232 {
233 EventHotKeyRef ref = s_hotkeys[i].ref;
234 s_hotkeys.erase(s_hotkeys.begin() + i);
235 if ( UnregisterEventHotKey(ref) != noErr )
236 {
237 wxLogLastError(wxT("UnregisterHotKey"));
238 }
239 }
240 }
241#endif
524c47aa 242
524c47aa
SC
243 MacInvalidateBorders() ;
244
245#ifndef __WXUNIVERSAL__
246 // VS: make sure there's no wxFrame with last focus set to us:
247 for ( wxWindow *win = GetParent(); win; win = win->GetParent() )
248 {
249 wxFrame *frame = wxDynamicCast(win, wxFrame);
250 if ( frame )
251 {
252 if ( frame->GetLastFocus() == this )
d3b9f782 253 frame->SetLastFocus(NULL);
524c47aa
SC
254 break;
255 }
256 }
257#endif
258
259 // destroy children before destroying this window itself
260 DestroyChildren();
261
262 // wxRemoveMacControlAssociation( this ) ;
263 // If we delete an item, we should initialize the parent panel,
264 // because it could now be invalid.
265 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent((wxWindow*)this), wxTopLevelWindow);
266 if ( tlw )
267 {
268 if ( tlw->GetDefaultItem() == (wxButton*) this)
269 tlw->SetDefaultItem(NULL);
270 }
271
272 if ( g_MacLastWindow == this )
273 g_MacLastWindow = NULL ;
274
275#ifndef __WXUNIVERSAL__
276 wxFrame* frame = wxDynamicCast( wxGetTopLevelParent( (wxWindow*)this ) , wxFrame ) ;
277 if ( frame )
278 {
279 if ( frame->GetLastFocus() == this )
280 frame->SetLastFocus( NULL ) ;
281 }
282#endif
283
284 // delete our drop target if we've got one
285#if wxUSE_DRAG_AND_DROP
5276b0a5 286 wxDELETE(m_dropTarget);
524c47aa
SC
287#endif
288
d15694e8 289 delete GetPeer() ;
524c47aa
SC
290}
291
292WXWidget wxWindowMac::GetHandle() const
293{
d15694e8
SC
294 if ( GetPeer() )
295 return (WXWidget) GetPeer()->GetWXWidget() ;
d4e4ba48 296 return NULL;
524c47aa
SC
297}
298
d15694e8
SC
299wxOSXWidgetImpl* wxWindowMac::GetPeer() const
300{
301 return m_peer == kOSXNoWidgetImpl ? NULL : m_peer ;
302}
303
ce63f2e9
VZ
304bool wxWindowMac::ShouldCreatePeer() const
305{
306 return m_peer != kOSXNoWidgetImpl;
307}
308
d15694e8
SC
309void wxWindowMac::DontCreatePeer()
310{
311 m_peer = kOSXNoWidgetImpl;
312}
313
314void wxWindowMac::SetWrappingPeer(wxOSXWidgetImpl* wrapper)
315{
316 wxOSXWidgetImpl* inner = GetPeer();
317 wxASSERT_MSG( inner != NULL && inner->IsOk(), "missing or incomplete inner peer" );
318 wxASSERT_MSG( wrapper != NULL && wrapper->IsOk(), "missing or incomplete wrapper" );
319
320 if ( !(inner != NULL && inner->IsOk() && wrapper != NULL && wrapper->IsOk()) )
321 return;
322
323 inner->RemoveFromParent();
324 wrapper->InstallEventHandler();
325 wrapper->Embed(inner);
326 m_peer = wrapper;
327}
328
22756322
SC
329void wxWindowMac::SetPeer(wxOSXWidgetImpl* peer)
330{
d15694e8
SC
331 if ( GetPeer() )
332 {
869aa41b
SC
333 if ( !GetPeer()->IsRootControl() )
334 GetPeer()->RemoveFromParent();
d15694e8
SC
335 wxDELETE(m_peer);
336 }
337
22756322 338 m_peer = peer;
d15694e8
SC
339
340 if ( GetPeer() && !GetPeer()->IsRootControl())
341 {
342 wxASSERT_MSG( GetPeer()->IsOk() , wxT("The native control must exist already") ) ;
343
344 if (!GetParent()->GetChildren().Find((wxWindow*)this))
345 GetParent()->AddChild( this );
346
347 GetPeer()->InstallEventHandler();
348 GetPeer()->Embed(GetParent()->GetPeer());
349
350 GetParent()->MacChildAdded() ;
351
352 // adjust font, controlsize etc
353 DoSetWindowVariant( m_windowVariant ) ;
354
355 GetPeer()->SetLabel( wxStripMenuCodes(m_label, wxStrip_Mnemonics), GetFont().GetEncoding() ) ;
356
357 // for controls we want to use best size for wxDefaultSize params )
358 if ( !GetPeer()->IsUserPane() )
359 SetInitialSize(GetMinSize());
360
361 SetCursor( *wxSTANDARD_CURSOR ) ;
362 }
363}
364
365#if WXWIN_COMPATIBILITY_2_8
366
367bool wxWindowMac::MacIsUserPane()
368{
369 return GetPeer() == NULL || GetPeer()->IsUserPane();
370}
371
372#endif
373
374bool wxWindowMac::MacIsUserPane() const
375{
376 return GetPeer() == NULL || GetPeer()->IsUserPane();
22756322
SC
377}
378
524c47aa
SC
379// ---------------------------------------------------------------------------
380// Utility Routines to move between different coordinate systems
381// ---------------------------------------------------------------------------
382
383/*
384 * Right now we have the following setup :
385 * a border that is not part of the native control is always outside the
386 * control's border (otherwise we loose all native intelligence, future ways
387 * may be to have a second embedding control responsible for drawing borders
388 * and backgrounds eventually)
389 * so all this border calculations have to be taken into account when calling
390 * native methods or getting native oriented data
391 * so we have three coordinate systems here
392 * wx client coordinates
393 * wx window coordinates (including window frames)
394 * native coordinates
395 */
396
397//
398//
399
400// Constructor
401bool wxWindowMac::Create(wxWindowMac *parent,
402 wxWindowID id,
403 const wxPoint& pos,
404 const wxSize& size,
405 long style,
406 const wxString& name)
407{
408 wxCHECK_MSG( parent, false, wxT("can't create wxWindowMac without parent") );
409
410 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
411 return false;
412
413 m_windowVariant = parent->GetWindowVariant() ;
414
d15694e8 415 if ( m_peer != kOSXNoWidgetImpl )
524c47aa 416 {
d15694e8 417 SetPeer(wxWidgetImpl::CreateUserPane( this, parent, id, pos, size , style, GetExtraStyle() ));
524c47aa
SC
418 MacPostControlCreate(pos, size) ;
419 }
420
421#ifndef __WXUNIVERSAL__
422 // Don't give scrollbars to wxControls unless they ask for them
03647350 423 if ( (! IsKindOf(CLASSINFO(wxControl))
11fed901
SC
424#if wxUSE_STATUSBAR
425 && ! IsKindOf(CLASSINFO(wxStatusBar))
426#endif
427 )
524c47aa
SC
428 || (IsKindOf(CLASSINFO(wxControl)) && ((style & wxHSCROLL) || (style & wxVSCROLL))))
429 {
430 MacCreateScrollBars( style ) ;
431 }
432#endif
433
434 wxWindowCreateEvent event((wxWindow*)this);
435 GetEventHandler()->AddPendingEvent(event);
436
437 return true;
438}
439
440void wxWindowMac::MacChildAdded()
441{
11fed901 442#if wxUSE_SCROLLBAR
524c47aa
SC
443 if ( m_vScrollBar )
444 m_vScrollBar->Raise() ;
445 if ( m_hScrollBar )
446 m_hScrollBar->Raise() ;
2ae3afa0
JS
447 if ( m_growBox )
448 m_growBox->Raise() ;
11fed901 449#endif
524c47aa
SC
450}
451
6a06eecf
VZ
452void wxWindowMac::MacPostControlCreate(const wxPoint& WXUNUSED(pos),
453 const wxSize& WXUNUSED(size))
524c47aa 454{
d15694e8
SC
455 // todo remove if refactoring works correctly
456#if 0
457 wxASSERT_MSG( GetPeer() != NULL && GetPeer()->IsOk() , wxT("No valid mac control") ) ;
524c47aa 458
d15694e8
SC
459 if (!GetParent()->GetChildren().Find((wxWindow*)this))
460 GetParent()->AddChild( this );
524c47aa 461
d15694e8
SC
462 GetPeer()->InstallEventHandler();
463 GetPeer()->Embed(GetParent()->GetPeer());
524c47aa 464
524c47aa
SC
465 GetParent()->MacChildAdded() ;
466
467 // adjust font, controlsize etc
468 DoSetWindowVariant( m_windowVariant ) ;
469
d15694e8 470 GetPeer()->SetLabel( wxStripMenuCodes(m_label, wxStrip_Mnemonics), GetFont().GetEncoding() ) ;
524c47aa 471
f55d9f74 472 // for controls we want to use best size for wxDefaultSize params )
d15694e8 473 if ( !GetPeer()->IsUserPane() )
524c47aa
SC
474 SetInitialSize(size);
475
476 SetCursor( *wxSTANDARD_CURSOR ) ;
d15694e8 477#endif
524c47aa
SC
478}
479
480void wxWindowMac::DoSetWindowVariant( wxWindowVariant variant )
481{
482 // Don't assert, in case we set the window variant before
483 // the window is created
a1b806b9 484 // wxASSERT( GetPeer()->IsOk() ) ;
524c47aa
SC
485
486 m_windowVariant = variant ;
487
d15694e8 488 if (GetPeer() == NULL || !GetPeer()->IsOk())
524c47aa
SC
489 return;
490
d15694e8 491 GetPeer()->SetControlSize( variant );
c59aa14a 492
524c47aa
SC
493 switch ( variant )
494 {
495 case wxWINDOW_VARIANT_NORMAL :
7eb8aeb8
SC
496 static wxFont sysNormal(wxOSX_SYSTEM_FONT_NORMAL);
497 SetFont(sysNormal) ;
524c47aa
SC
498 break ;
499
500 case wxWINDOW_VARIANT_SMALL :
7eb8aeb8
SC
501 static wxFont sysSmall(wxOSX_SYSTEM_FONT_SMALL);
502 SetFont(sysSmall) ;
524c47aa
SC
503 break ;
504
505 case wxWINDOW_VARIANT_MINI :
7eb8aeb8
SC
506 static wxFont sysMini(wxOSX_SYSTEM_FONT_MINI);
507 SetFont(sysMini) ;
524c47aa
SC
508 break ;
509
510 case wxWINDOW_VARIANT_LARGE :
7eb8aeb8
SC
511 static wxFont sysLarge(wxOSX_SYSTEM_FONT_NORMAL);
512 SetFont(sysLarge) ;
524c47aa
SC
513 break ;
514
515 default:
9a83f860 516 wxFAIL_MSG(wxT("unexpected window variant"));
524c47aa
SC
517 break ;
518 }
524c47aa
SC
519}
520
521void wxWindowMac::MacUpdateControlFont()
522{
d15694e8
SC
523 if ( GetPeer() )
524 GetPeer()->SetFont( GetFont() , GetForegroundColour() , GetWindowStyle() ) ;
1e181c7a 525
524c47aa
SC
526 // do not trigger refreshes upon invisible and possible partly created objects
527 if ( IsShownOnScreen() )
528 Refresh() ;
529}
530
531bool wxWindowMac::SetFont(const wxFont& font)
532{
533 bool retval = wxWindowBase::SetFont( font );
534
535 MacUpdateControlFont() ;
536
537 return retval;
538}
539
540bool wxWindowMac::SetForegroundColour(const wxColour& col )
541{
542 bool retval = wxWindowBase::SetForegroundColour( col );
543
544 if (retval)
545 MacUpdateControlFont();
546
547 return retval;
548}
549
bc5c09a3
SC
550bool wxWindowMac::SetBackgroundStyle(wxBackgroundStyle style)
551{
552 if ( !wxWindowBase::SetBackgroundStyle(style) )
553 return false;
c59aa14a 554
d15694e8
SC
555 if ( GetPeer() )
556 GetPeer()->SetBackgroundStyle(style);
bc5c09a3
SC
557 return true;
558}
559
524c47aa
SC
560bool wxWindowMac::SetBackgroundColour(const wxColour& col )
561{
82f8ea85
SC
562 if (m_growBox)
563 {
a1b806b9 564 if ( m_backgroundColour.IsOk() )
82f8ea85
SC
565 m_growBox->SetBackgroundColour(m_backgroundColour);
566 else
567 m_growBox->SetBackgroundColour(*wxWHITE);
568 }
569
524c47aa
SC
570 if ( !wxWindowBase::SetBackgroundColour(col) && m_hasBgCol )
571 return false ;
572
d15694e8
SC
573 if ( GetPeer() )
574 GetPeer()->SetBackgroundColour( col ) ;
524c47aa
SC
575
576 return true ;
577}
578
77a246e1
JS
579static bool wxIsWindowOrParentDisabled(wxWindow* w)
580{
581 while (w && !w->IsTopLevel())
582 {
583 if (!w->IsEnabled())
584 return true;
585 w = w->GetParent();
586 }
587 return false;
588}
589
524c47aa
SC
590void wxWindowMac::SetFocus()
591{
592 if ( !AcceptsFocus() )
593 return ;
594
77a246e1
JS
595 if (wxIsWindowOrParentDisabled((wxWindow*) this))
596 return;
597
524c47aa
SC
598 wxWindow* former = FindFocus() ;
599 if ( former == this )
600 return ;
601
d15694e8 602 GetPeer()->SetFocus() ;
524c47aa
SC
603}
604
605void wxWindowMac::DoCaptureMouse()
606{
607 wxApp::s_captureWindow = (wxWindow*) this ;
d15694e8 608 GetPeer()->CaptureMouse() ;
524c47aa
SC
609}
610
611wxWindow * wxWindowBase::GetCapture()
612{
613 return wxApp::s_captureWindow ;
614}
615
616void wxWindowMac::DoReleaseMouse()
617{
618 wxApp::s_captureWindow = NULL ;
54f11060 619
d15694e8 620 GetPeer()->ReleaseMouse() ;
524c47aa
SC
621}
622
623#if wxUSE_DRAG_AND_DROP
624
625void wxWindowMac::SetDropTarget(wxDropTarget *pDropTarget)
626{
0b1ca117 627 delete m_dropTarget;
524c47aa
SC
628
629 m_dropTarget = pDropTarget;
5ce7b0b5
SC
630
631 GetPeer()->SetDropTarget(m_dropTarget) ;
524c47aa
SC
632}
633
634#endif
635
636// Old-style File Manager Drag & Drop
637void wxWindowMac::DragAcceptFiles(bool WXUNUSED(accept))
638{
639 // TODO:
640}
641
642// From a wx position / size calculate the appropriate size of the native control
643
644bool wxWindowMac::MacGetBoundsForControl(
645 const wxPoint& pos,
646 const wxSize& size,
647 int& x, int& y,
648 int& w, int& h , bool adjustOrigin ) const
649{
650 // the desired size, minus the border pixels gives the correct size of the control
651 x = (int)pos.x;
652 y = (int)pos.y;
653
69ce9cea
VZ
654 w = WidthDefault( size.x );
655 h = HeightDefault( size.y );
524c47aa
SC
656
657 x += MacGetLeftBorderSize() ;
658 y += MacGetTopBorderSize() ;
659 w -= MacGetLeftBorderSize() + MacGetRightBorderSize() ;
660 h -= MacGetTopBorderSize() + MacGetBottomBorderSize() ;
661
662 if ( adjustOrigin )
663 AdjustForParentClientOrigin( x , y ) ;
664
665 // this is in window relative coordinate, as this parent may have a border, its physical position is offset by this border
1a4e2d5b 666 if ( GetParent() && !GetParent()->IsTopLevel() )
524c47aa
SC
667 {
668 x -= GetParent()->MacGetLeftBorderSize() ;
669 y -= GetParent()->MacGetTopBorderSize() ;
670 }
671
672 return true ;
673}
674
675// Get window size (not client size)
676void wxWindowMac::DoGetSize(int *x, int *y) const
677{
678 int width, height;
d15694e8 679 GetPeer()->GetSize( width, height );
524c47aa
SC
680
681 if (x)
682 *x = width + MacGetLeftBorderSize() + MacGetRightBorderSize() ;
683 if (y)
684 *y = height + MacGetTopBorderSize() + MacGetBottomBorderSize() ;
685}
686
687// get the position of the bounds of this window in client coordinates of its parent
688void wxWindowMac::DoGetPosition(int *x, int *y) const
689{
690 int x1, y1;
69ce9cea 691
d15694e8 692 GetPeer()->GetPosition( x1, y1 ) ;
524c47aa
SC
693
694 // get the wx window position from the native one
695 x1 -= MacGetLeftBorderSize() ;
696 y1 -= MacGetTopBorderSize() ;
697
698 if ( !IsTopLevel() )
699 {
700 wxWindow *parent = GetParent();
701 if ( parent )
702 {
703 // we must first adjust it to be in window coordinates of the parent,
704 // as otherwise it gets lost by the ClientAreaOrigin fix
705 x1 += parent->MacGetLeftBorderSize() ;
706 y1 += parent->MacGetTopBorderSize() ;
707
708 // and now to client coordinates
709 wxPoint pt(parent->GetClientAreaOrigin());
710 x1 -= pt.x ;
711 y1 -= pt.y ;
712 }
713 }
714
715 if (x)
716 *x = x1 ;
717 if (y)
718 *y = y1 ;
719}
720
721void wxWindowMac::DoScreenToClient(int *x, int *y) const
722{
723 wxNonOwnedWindow* tlw = MacGetTopLevelWindow() ;
724 wxCHECK_RET( tlw , wxT("TopLevel Window missing") ) ;
725 tlw->GetNonOwnedPeer()->ScreenToWindow( x, y);
726 MacRootWindowToWindow( x , y ) ;
727
728 wxPoint origin = GetClientAreaOrigin() ;
729 if (x)
730 *x -= origin.x ;
731 if (y)
732 *y -= origin.y ;
733}
734
735void wxWindowMac::DoClientToScreen(int *x, int *y) const
736{
737 wxNonOwnedWindow* tlw = MacGetTopLevelWindow() ;
738 wxCHECK_RET( tlw , wxT("TopLevel window missing") ) ;
739
740 wxPoint origin = GetClientAreaOrigin() ;
741 if (x)
742 *x += origin.x ;
743 if (y)
744 *y += origin.y ;
745
746 MacWindowToRootWindow( x , y ) ;
747 tlw->GetNonOwnedPeer()->WindowToScreen( x , y );
748}
749
750void wxWindowMac::MacClientToRootWindow( int *x , int *y ) const
751{
752 wxPoint origin = GetClientAreaOrigin() ;
753 if (x)
754 *x += origin.x ;
755 if (y)
756 *y += origin.y ;
757
758 MacWindowToRootWindow( x , y ) ;
759}
760
761void wxWindowMac::MacWindowToRootWindow( int *x , int *y ) const
762{
763 wxPoint pt ;
764
765 if (x)
766 pt.x = *x ;
767 if (y)
768 pt.y = *y ;
769
770 if ( !IsTopLevel() )
771 {
772 wxNonOwnedWindow* top = MacGetTopLevelWindow();
773 if (top)
774 {
775 pt.x -= MacGetLeftBorderSize() ;
776 pt.y -= MacGetTopBorderSize() ;
d15694e8 777 wxWidgetImpl::Convert( &pt , GetPeer() , top->GetPeer() ) ;
524c47aa
SC
778 }
779 }
780
781 if (x)
782 *x = (int) pt.x ;
783 if (y)
784 *y = (int) pt.y ;
785}
786
787void wxWindowMac::MacRootWindowToWindow( int *x , int *y ) const
788{
789 wxPoint pt ;
790
791 if (x)
792 pt.x = *x ;
793 if (y)
794 pt.y = *y ;
795
796 if ( !IsTopLevel() )
797 {
798 wxNonOwnedWindow* top = MacGetTopLevelWindow();
799 if (top)
800 {
d15694e8 801 wxWidgetImpl::Convert( &pt , top->GetPeer() , GetPeer() ) ;
524c47aa
SC
802 pt.x += MacGetLeftBorderSize() ;
803 pt.y += MacGetTopBorderSize() ;
804 }
805 }
806
807 if (x)
808 *x = (int) pt.x ;
809 if (y)
810 *y = (int) pt.y ;
811}
812
813wxSize wxWindowMac::DoGetSizeFromClientSize( const wxSize & size ) const
814{
815 wxSize sizeTotal = size;
816
817 int innerwidth, innerheight;
818 int left, top;
819 int outerwidth, outerheight;
69ce9cea 820
d15694e8
SC
821 GetPeer()->GetContentArea( left, top, innerwidth, innerheight );
822 GetPeer()->GetSize( outerwidth, outerheight );
69ce9cea 823
0c530e5a
SC
824 sizeTotal.x += outerwidth-innerwidth;
825 sizeTotal.y += outerheight-innerheight;
69ce9cea 826
524c47aa
SC
827 sizeTotal.x += MacGetLeftBorderSize() + MacGetRightBorderSize() ;
828 sizeTotal.y += MacGetTopBorderSize() + MacGetBottomBorderSize() ;
829
830 return sizeTotal;
831}
832
833// Get size *available for subwindows* i.e. excluding menu bar etc.
834void wxWindowMac::DoGetClientSize( int *x, int *y ) const
835{
836 int ww, hh;
837
838 int left, top;
69ce9cea 839
d15694e8 840 GetPeer()->GetContentArea( left, top, ww, hh );
11fed901 841#if wxUSE_SCROLLBAR
524c47aa
SC
842 if (m_hScrollBar && m_hScrollBar->IsShown() )
843 hh -= m_hScrollBar->GetSize().y ;
844
845 if (m_vScrollBar && m_vScrollBar->IsShown() )
846 ww -= m_vScrollBar->GetSize().x ;
847
11fed901 848#endif
524c47aa
SC
849 if (x)
850 *x = ww;
851 if (y)
852 *y = hh;
853}
854
855bool wxWindowMac::SetCursor(const wxCursor& cursor)
856{
857 if (m_cursor.IsSameAs(cursor))
858 return false;
859
860 if (!cursor.IsOk())
861 {
862 if ( ! wxWindowBase::SetCursor( *wxSTANDARD_CURSOR ) )
863 return false ;
864 }
865 else
866 {
867 if ( ! wxWindowBase::SetCursor( cursor ) )
868 return false ;
869 }
870
a1b806b9 871 wxASSERT_MSG( m_cursor.IsOk(),
524c47aa
SC
872 wxT("cursor must be valid after call to the base version"));
873
54f11060
SC
874 if ( GetPeer() != NULL )
875 GetPeer()->SetCursor( m_cursor );
524c47aa
SC
876
877 return true ;
878}
879
880#if wxUSE_MENUS
881bool wxWindowMac::DoPopupMenu(wxMenu *menu, int x, int y)
882{
883#ifndef __WXUNIVERSAL__
524c47aa
SC
884 menu->UpdateUI();
885
886 if ( x == wxDefaultCoord && y == wxDefaultCoord )
887 {
888 wxPoint mouse = wxGetMousePosition();
889 x = mouse.x;
890 y = mouse.y;
891 }
892 else
893 {
894 ClientToScreen( &x , &y ) ;
895 }
2cb5d2d2 896 menu->GetPeer()->PopUp(this, x, y);
524c47aa
SC
897 return true;
898#else
899 // actually this shouldn't be called, because universal is having its own implementation
900 return false;
901#endif
902}
903#endif
904
905// ----------------------------------------------------------------------------
906// tooltips
907// ----------------------------------------------------------------------------
908
909#if wxUSE_TOOLTIPS
910
911void wxWindowMac::DoSetToolTip(wxToolTip *tooltip)
912{
913 wxWindowBase::DoSetToolTip(tooltip);
914
915 if ( m_tooltip )
916 m_tooltip->SetWindow(this);
c59aa14a 917
d15694e8
SC
918 if (GetPeer())
919 GetPeer()->SetToolTip(tooltip);
524c47aa
SC
920}
921
922#endif
923
924void wxWindowMac::MacInvalidateBorders()
925{
d15694e8 926 if ( GetPeer() == NULL )
524c47aa
SC
927 return ;
928
929 bool vis = IsShownOnScreen() ;
930 if ( !vis )
931 return ;
932
933 int outerBorder = MacGetLeftBorderSize() ;
b2088388 934
d15694e8 935 if ( GetPeer()->NeedsFocusRect() )
524c47aa 936 outerBorder += 4 ;
524c47aa
SC
937
938 if ( outerBorder == 0 )
939 return ;
940
941 // now we know that we have something to do at all
524c47aa
SC
942
943 int tx,ty,tw,th;
69ce9cea 944
d15694e8
SC
945 GetPeer()->GetSize( tw, th );
946 GetPeer()->GetPosition( tx, ty );
524c47aa
SC
947
948 wxRect leftupdate( tx-outerBorder,ty,outerBorder,th );
949 wxRect rightupdate( tx+tw, ty, outerBorder, th );
950 wxRect topupdate( tx-outerBorder, ty-outerBorder, tw + 2 * outerBorder, outerBorder );
951 wxRect bottomupdate( tx-outerBorder, ty + th, tw + 2 * outerBorder, outerBorder );
69ce9cea 952
1a4e2d5b 953 if (GetParent()) {
d15694e8
SC
954 GetParent()->GetPeer()->SetNeedsDisplay(&leftupdate);
955 GetParent()->GetPeer()->SetNeedsDisplay(&rightupdate);
956 GetParent()->GetPeer()->SetNeedsDisplay(&topupdate);
957 GetParent()->GetPeer()->SetNeedsDisplay(&bottomupdate);
1a4e2d5b 958 }
524c47aa
SC
959}
960
961void wxWindowMac::DoMoveWindow(int x, int y, int width, int height)
962{
963 // this is never called for a toplevel window, so we know we have a parent
964 int former_x , former_y , former_w, former_h ;
965
966 // Get true coordinates of former position
967 DoGetPosition( &former_x , &former_y ) ;
968 DoGetSize( &former_w , &former_h ) ;
969
970 wxWindow *parent = GetParent();
971 if ( parent )
972 {
973 wxPoint pt(parent->GetClientAreaOrigin());
974 former_x += pt.x ;
975 former_y += pt.y ;
976 }
977
978 int actualWidth = width ;
979 int actualHeight = height ;
980 int actualX = x;
981 int actualY = y;
982
f1b1c779
SC
983#if 0
984 // min and max sizes are only for sizers, not for explicit size setting
524c47aa
SC
985 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
986 actualWidth = m_minWidth;
987 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
988 actualHeight = m_minHeight;
989 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
990 actualWidth = m_maxWidth;
991 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
992 actualHeight = m_maxHeight;
f1b1c779 993#endif
524c47aa
SC
994
995 bool doMove = false, doResize = false ;
996
997 if ( actualX != former_x || actualY != former_y )
998 doMove = true ;
999
1000 if ( actualWidth != former_w || actualHeight != former_h )
1001 doResize = true ;
1002
1003 if ( doMove || doResize )
1004 {
1005 // as the borders are drawn outside the native control, we adjust now
1006
1007 wxRect bounds( wxPoint( actualX + MacGetLeftBorderSize() ,actualY + MacGetTopBorderSize() ),
1008 wxSize( actualWidth - (MacGetLeftBorderSize() + MacGetRightBorderSize()) ,
1009 actualHeight - (MacGetTopBorderSize() + MacGetBottomBorderSize()) ) ) ;
1010
1a4e2d5b 1011 if ( parent && !parent->IsTopLevel() )
524c47aa 1012 {
1a4e2d5b 1013 bounds.Offset( -parent->MacGetLeftBorderSize(), -parent->MacGetTopBorderSize() );
524c47aa
SC
1014 }
1015
1016 MacInvalidateBorders() ;
1017
1018 m_cachedClippedRectValid = false ;
69ce9cea 1019
d15694e8 1020 GetPeer()->Move( bounds.x, bounds.y, bounds.width, bounds.height);
524c47aa
SC
1021
1022 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
1023
1024 MacInvalidateBorders() ;
1025
1026 MacRepositionScrollBars() ;
1027 if ( doMove )
1028 {
1029 wxPoint point(actualX, actualY);
1030 wxMoveEvent event(point, m_windowId);
1031 event.SetEventObject(this);
1032 HandleWindowEvent(event) ;
1033 }
1034
1035 if ( doResize )
1036 {
1037 MacRepositionScrollBars() ;
6062fe5c 1038 MacOnInternalSize();
524c47aa
SC
1039 wxSize size(actualWidth, actualHeight);
1040 wxSizeEvent event(size, m_windowId);
1041 event.SetEventObject(this);
1042 HandleWindowEvent(event);
1043 }
1044 }
1045}
1046
1047wxSize wxWindowMac::DoGetBestSize() const
1048{
d15694e8 1049 if ( GetPeer() == NULL || GetPeer()->IsUserPane() || IsTopLevel() )
524c47aa
SC
1050 {
1051 return wxWindowBase::DoGetBestSize() ;
1052 }
1053 else
1054 {
1055 wxRect r ;
69ce9cea 1056
d15694e8 1057 GetPeer()->GetBestRect(&r);
524c47aa
SC
1058
1059 if ( r.GetWidth() == 0 && r.GetHeight() == 0 )
1060 {
1061 r.x =
1062 r.y = 0 ;
1063 r.width =
1064 r.height = 16 ;
1065
11fed901 1066#if wxUSE_SCROLLBAR
65391c8f 1067 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
524c47aa
SC
1068 {
1069 r.height = 16 ;
1070 }
65391c8f 1071 else
11fed901
SC
1072#endif
1073#if wxUSE_SPINBTN
65391c8f 1074 if ( IsKindOf( CLASSINFO( wxSpinButton ) ) )
524c47aa
SC
1075 {
1076 r.height = 24 ;
1077 }
524c47aa 1078 else
65391c8f 1079#endif
524c47aa
SC
1080 {
1081 // return wxWindowBase::DoGetBestSize() ;
1082 }
1083 }
1084
69ce9cea 1085 int bestWidth = r.width + MacGetLeftBorderSize() +
524c47aa 1086 MacGetRightBorderSize();
69ce9cea 1087 int bestHeight = r.height + MacGetTopBorderSize() +
524c47aa
SC
1088 MacGetBottomBorderSize();
1089 if ( bestHeight < 10 )
1090 bestHeight = 13 ;
1091
1092 return wxSize(bestWidth, bestHeight);
1093 }
1094}
1095
1096// set the size of the window: if the dimensions are positive, just use them,
1097// but if any of them is equal to -1, it means that we must find the value for
1098// it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
1099// which case -1 is a valid value for x and y)
1100//
1101// If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
1102// the width/height to best suit our contents, otherwise we reuse the current
1103// width/height
1104void wxWindowMac::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1105{
1106 // get the current size and position...
1107 int currentX, currentY;
1108 int currentW, currentH;
1109
1110 GetPosition(&currentX, &currentY);
1111 GetSize(&currentW, &currentH);
1112
1113 // ... and don't do anything (avoiding flicker) if it's already ok
1114 if ( x == currentX && y == currentY &&
1115 width == currentW && height == currentH && ( height != -1 && width != -1 ) )
1116 {
1117 // TODO: REMOVE
1118 MacRepositionScrollBars() ; // we might have a real position shift
1119
e47e063a
RR
1120 if (sizeFlags & wxSIZE_FORCE_EVENT)
1121 {
6062fe5c 1122 MacOnInternalSize();
e47e063a
RR
1123 wxSizeEvent event( wxSize(width,height), GetId() );
1124 event.SetEventObject( this );
1125 HandleWindowEvent( event );
1126 }
1127
524c47aa
SC
1128 return;
1129 }
1130
1131 if ( !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1132 {
1133 if ( x == wxDefaultCoord )
1134 x = currentX;
1135 if ( y == wxDefaultCoord )
1136 y = currentY;
1137 }
1138
1139 AdjustForParentClientOrigin( x, y, sizeFlags );
1140
1141 wxSize size = wxDefaultSize;
1142 if ( width == wxDefaultCoord )
1143 {
1144 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
1145 {
1146 size = DoGetBestSize();
1147 width = size.x;
1148 }
1149 else
1150 {
1151 // just take the current one
1152 width = currentW;
1153 }
1154 }
1155
1156 if ( height == wxDefaultCoord )
1157 {
1158 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
1159 {
1160 if ( size.x == wxDefaultCoord )
1161 size = DoGetBestSize();
1162 // else: already called DoGetBestSize() above
1163
1164 height = size.y;
1165 }
1166 else
1167 {
1168 // just take the current one
1169 height = currentH;
1170 }
1171 }
1172
1173 DoMoveWindow( x, y, width, height );
1174}
1175
1176wxPoint wxWindowMac::GetClientAreaOrigin() const
1177{
1178 int left,top,width,height;
d15694e8 1179 GetPeer()->GetContentArea( left , top , width , height);
524c47aa
SC
1180 return wxPoint( left + MacGetLeftBorderSize() , top + MacGetTopBorderSize() );
1181}
1182
1183void wxWindowMac::DoSetClientSize(int clientwidth, int clientheight)
1184{
1185 if ( clientwidth != wxDefaultCoord || clientheight != wxDefaultCoord )
1186 {
1187 int currentclientwidth , currentclientheight ;
1188 int currentwidth , currentheight ;
1189
1190 GetClientSize( &currentclientwidth , &currentclientheight ) ;
1191 GetSize( &currentwidth , &currentheight ) ;
1192
1193 DoSetSize( wxDefaultCoord , wxDefaultCoord , currentwidth + clientwidth - currentclientwidth ,
1194 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
1195 }
1196}
1197
f18b5ee7
SC
1198float wxWindowMac::GetContentScaleFactor() const
1199{
d15694e8 1200 return GetPeer()->GetContentScaleFactor();
f18b5ee7
SC
1201}
1202
524c47aa
SC
1203void wxWindowMac::SetLabel(const wxString& title)
1204{
8c6c5778
VZ
1205 if ( title == m_label )
1206 return;
1207
524c47aa
SC
1208 m_label = title ;
1209
8c6c5778
VZ
1210 InvalidateBestSize();
1211
d15694e8
SC
1212 if ( GetPeer() && GetPeer()->IsOk() )
1213 GetPeer()->SetLabel( wxStripMenuCodes(m_label, wxStrip_Mnemonics), GetFont().GetEncoding() ) ;
524c47aa
SC
1214
1215 // do not trigger refreshes upon invisible and possible partly created objects
1216 if ( IsShownOnScreen() )
1217 Refresh() ;
1218}
1219
1220wxString wxWindowMac::GetLabel() const
1221{
1222 return m_label ;
1223}
1224
1225bool wxWindowMac::Show(bool show)
1226{
91722d74
SC
1227 if ( !show )
1228 MacInvalidateBorders();
1229
524c47aa
SC
1230 if ( !wxWindowBase::Show(show) )
1231 return false;
1232
d15694e8
SC
1233 if ( GetPeer() )
1234 GetPeer()->SetVisibility( show ) ;
524c47aa 1235
2d0744aa
SC
1236 if ( show )
1237 MacInvalidateBorders();
1238
4cc16dcb 1239#ifdef __WXOSX_IPHONE__
c59aa14a 1240 // only when there's no native event support
4cc16dcb
SC
1241 if ( !IsTopLevel() )
1242#endif
1243 {
1244 wxShowEvent eventShow(GetId(), show);
1245 eventShow.SetEventObject(this);
c59aa14a 1246
4cc16dcb
SC
1247 HandleWindowEvent(eventShow);
1248 }
c59aa14a 1249
524c47aa
SC
1250 return true;
1251}
1252
ab9a0b84
VZ
1253bool wxWindowMac::OSXShowWithEffect(bool show,
1254 wxShowEffect effect,
1255 unsigned timeout)
1256{
1257 if ( effect == wxSHOW_EFFECT_NONE ||
d15694e8 1258 !GetPeer() || !GetPeer()->ShowWithEffect(show, effect, timeout) )
ab9a0b84
VZ
1259 return Show(show);
1260
1261 return true;
1262}
1263
524c47aa
SC
1264void wxWindowMac::DoEnable(bool enable)
1265{
d15694e8 1266 GetPeer()->Enable( enable ) ;
c4a7e284 1267 MacInvalidateBorders();
524c47aa
SC
1268}
1269
1270//
1271// status change notifications
1272//
1273
1274void wxWindowMac::MacVisibilityChanged()
1275{
1276}
1277
1278void wxWindowMac::MacHiliteChanged()
1279{
1280}
1281
1282void wxWindowMac::MacEnabledStateChanged()
1283{
d15694e8 1284 OnEnabled( GetPeer()->IsEnabled() );
524c47aa
SC
1285}
1286
1287//
1288// status queries on the inherited window's state
1289//
1290
1291bool wxWindowMac::MacIsReallyEnabled()
1292{
d15694e8 1293 return GetPeer()->IsEnabled() ;
524c47aa
SC
1294}
1295
1296bool wxWindowMac::MacIsReallyHilited()
1297{
1298#if wxOSX_USE_CARBON
d15694e8 1299 return GetPeer()->IsActive();
524c47aa
SC
1300#else
1301 return true; // TODO
1302#endif
1303}
1304
1305int wxWindowMac::GetCharHeight() const
1306{
f55d9f74
SC
1307 wxCoord height;
1308 GetTextExtent( wxT("g") , NULL , &height , NULL , NULL , NULL );
524c47aa 1309
f55d9f74 1310 return height;
524c47aa
SC
1311}
1312
1313int wxWindowMac::GetCharWidth() const
1314{
f55d9f74
SC
1315 wxCoord width;
1316 GetTextExtent( wxT("g") , &width , NULL , NULL , NULL , NULL );
524c47aa 1317
f55d9f74 1318 return width;
524c47aa
SC
1319}
1320
6de70470
VZ
1321void wxWindowMac::DoGetTextExtent(const wxString& str,
1322 int *x, int *y,
1323 int *descent,
1324 int *externalLeading,
1325 const wxFont *theFont) const
524c47aa
SC
1326{
1327 const wxFont *fontToUse = theFont;
1328 wxFont tempFont;
1329 if ( !fontToUse )
1330 {
1331 tempFont = GetFont();
1332 fontToUse = &tempFont;
1333 }
1334
f55d9f74
SC
1335 wxGraphicsContext* ctx = wxGraphicsContext::Create();
1336 ctx->SetFont( *fontToUse, *wxBLACK );
1337
1338 wxDouble h , d , e , w;
1339 ctx->GetTextExtent( str, &w, &h, &d, &e );
69ce9cea 1340
f55d9f74 1341 delete ctx;
69ce9cea 1342
524c47aa 1343 if ( externalLeading )
f55d9f74 1344 *externalLeading = (wxCoord)(e+0.5);
524c47aa 1345 if ( descent )
f55d9f74 1346 *descent = (wxCoord)(d+0.5);
524c47aa 1347 if ( x )
f55d9f74 1348 *x = (wxCoord)(w+0.5);
524c47aa 1349 if ( y )
f55d9f74 1350 *y = (wxCoord)(h+0.5);
524c47aa
SC
1351}
1352
1353/*
1354 * Rect is given in client coordinates, for further reading, read wxTopLevelWindowMac::InvalidateRect
1355 * we always intersect with the entire window, not only with the client area
1356 */
1357
1358void wxWindowMac::Refresh(bool WXUNUSED(eraseBack), const wxRect *rect)
1359{
d15694e8 1360 if ( GetPeer() == NULL )
524c47aa
SC
1361 return ;
1362
1363 if ( !IsShownOnScreen() )
1364 return ;
2ab38025
SC
1365
1366 if ( IsFrozen() )
1367 return;
69ce9cea 1368
d15694e8 1369 GetPeer()->SetNeedsDisplay( rect ) ;
524c47aa
SC
1370}
1371
1372void wxWindowMac::DoFreeze()
1373{
d15694e8
SC
1374 if ( GetPeer() && GetPeer()->IsOk() )
1375 GetPeer()->SetDrawingEnabled( false ) ;
524c47aa
SC
1376}
1377
1378void wxWindowMac::DoThaw()
1379{
d15694e8 1380 if ( GetPeer() && GetPeer()->IsOk() )
d15694e8 1381 GetPeer()->SetDrawingEnabled( true ) ;
524c47aa
SC
1382}
1383
1384wxWindow *wxGetActiveWindow()
1385{
1386 // actually this is a windows-only concept
1387 return NULL;
1388}
1389
1390// Coordinates relative to the window
1508fcac 1391void wxWindowMac::WarpPointer(int x_pos, int y_pos)
524c47aa 1392{
b2665b86 1393#if wxOSX_USE_COCOA_OR_CARBON
1508fcac
JS
1394 int x = x_pos;
1395 int y = y_pos;
1396 DoClientToScreen(&x, &y);
1397 CGPoint cgpoint = CGPointMake( x, y );
1398 CGWarpMouseCursorPosition( cgpoint );
1399
1400 // At least GTK sends a mouse moved event after WarpMouse
1401 wxMouseEvent event(wxEVT_MOTION);
1402 event.m_x = x_pos;
1403 event.m_y = y_pos;
1404 wxMouseState mState = ::wxGetMouseState();
1405
1406 event.m_altDown = mState.AltDown();
1407 event.m_controlDown = mState.ControlDown();
b2665b86
SC
1408 event.m_leftDown = mState.LeftIsDown();
1409 event.m_middleDown = mState.MiddleIsDown();
1410 event.m_rightDown = mState.RightIsDown();
1508fcac
JS
1411 event.m_metaDown = mState.MetaDown();
1412 event.m_shiftDown = mState.ShiftDown();
1413 event.SetId(GetId());
1414 event.SetEventObject(this);
1415 GetEventHandler()->ProcessEvent(event);
b2665b86 1416#endif
524c47aa
SC
1417}
1418
524c47aa
SC
1419int wxWindowMac::GetScrollPos(int orient) const
1420{
11fed901 1421#if wxUSE_SCROLLBAR
524c47aa
SC
1422 if ( orient == wxHORIZONTAL )
1423 {
1424 if ( m_hScrollBar )
1425 return m_hScrollBar->GetThumbPosition() ;
1426 }
1427 else
1428 {
1429 if ( m_vScrollBar )
1430 return m_vScrollBar->GetThumbPosition() ;
1431 }
11fed901 1432#endif
524c47aa
SC
1433 return 0;
1434}
1435
1436// This now returns the whole range, not just the number
1437// of positions that we can scroll.
1438int wxWindowMac::GetScrollRange(int orient) const
1439{
11fed901 1440#if wxUSE_SCROLLBAR
524c47aa
SC
1441 if ( orient == wxHORIZONTAL )
1442 {
1443 if ( m_hScrollBar )
1444 return m_hScrollBar->GetRange() ;
1445 }
1446 else
1447 {
1448 if ( m_vScrollBar )
1449 return m_vScrollBar->GetRange() ;
1450 }
11fed901 1451#endif
524c47aa
SC
1452 return 0;
1453}
1454
1455int wxWindowMac::GetScrollThumb(int orient) const
1456{
11fed901 1457#if wxUSE_SCROLLBAR
524c47aa
SC
1458 if ( orient == wxHORIZONTAL )
1459 {
1460 if ( m_hScrollBar )
1461 return m_hScrollBar->GetThumbSize() ;
1462 }
1463 else
1464 {
1465 if ( m_vScrollBar )
1466 return m_vScrollBar->GetThumbSize() ;
1467 }
11fed901 1468#endif
524c47aa
SC
1469 return 0;
1470}
1471
1472void wxWindowMac::SetScrollPos(int orient, int pos, bool WXUNUSED(refresh))
1473{
11fed901 1474#if wxUSE_SCROLLBAR
524c47aa
SC
1475 if ( orient == wxHORIZONTAL )
1476 {
1477 if ( m_hScrollBar )
1478 m_hScrollBar->SetThumbPosition( pos ) ;
1479 }
1480 else
1481 {
1482 if ( m_vScrollBar )
1483 m_vScrollBar->SetThumbPosition( pos ) ;
1484 }
11fed901 1485#endif
524c47aa
SC
1486}
1487
1488void
1489wxWindowMac::AlwaysShowScrollbars(bool hflag, bool vflag)
1490{
1491 bool needVisibilityUpdate = false;
1492
1493 if ( m_hScrollBarAlwaysShown != hflag )
1494 {
1495 m_hScrollBarAlwaysShown = hflag;
1496 needVisibilityUpdate = true;
1497 }
1498
1499 if ( m_vScrollBarAlwaysShown != vflag )
1500 {
1501 m_vScrollBarAlwaysShown = vflag;
1502 needVisibilityUpdate = true;
1503 }
1504
1505 if ( needVisibilityUpdate )
1506 DoUpdateScrollbarVisibility();
1507}
1508
1509//
1510// we draw borders and grow boxes, are already set up and clipped in the current port / cgContextRef
1511// our own window origin is at leftOrigin/rightOrigin
1512//
1513
1514void wxWindowMac::MacPaintGrowBox()
1515{
1516 if ( IsTopLevel() )
1517 return ;
1518
11fed901 1519#if wxUSE_SCROLLBAR
524c47aa
SC
1520 if ( MacHasScrollBarCorner() )
1521 {
2ae3afa0 1522#if 0
524c47aa
SC
1523 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef() ;
1524 wxASSERT( cgContext ) ;
1525
1526 int tx,ty,tw,th;
69ce9cea 1527
d15694e8
SC
1528 GetPeer()->GetSize( tw, th );
1529 GetPeer()->GetPosition( tx, ty );
524c47aa
SC
1530
1531 Rect rect = { ty,tx, ty+th, tx+tw };
1532
1533
1534 int size = m_hScrollBar ? m_hScrollBar->GetSize().y : ( m_vScrollBar ? m_vScrollBar->GetSize().x : MAC_SCROLLBAR_SIZE ) ;
1535 CGRect cgrect = CGRectMake( rect.right - size , rect.bottom - size , size , size ) ;
524c47aa
SC
1536 CGContextSaveGState( cgContext );
1537
a1b806b9 1538 if ( m_backgroundColour.IsOk() )
524c47aa
SC
1539 {
1540 CGContextSetFillColorWithColor( cgContext, m_backgroundColour.GetCGColor() );
1541 }
1542 else
1543 {
1544 CGContextSetRGBFillColor( cgContext, (CGFloat) 1.0, (CGFloat)1.0 ,(CGFloat) 1.0 , (CGFloat)1.0 );
1545 }
1546 CGContextFillRect( cgContext, cgrect );
1547 CGContextRestoreGState( cgContext );
2ae3afa0
JS
1548#else
1549 if (m_growBox)
1550 {
a1b806b9 1551 if ( m_backgroundColour.IsOk() )
2ae3afa0
JS
1552 m_growBox->SetBackgroundColour(m_backgroundColour);
1553 else
1554 m_growBox->SetBackgroundColour(*wxWHITE);
1555 }
1556#endif
524c47aa 1557 }
2ae3afa0 1558
11fed901 1559#endif
524c47aa
SC
1560}
1561
1562void wxWindowMac::MacPaintBorders( int WXUNUSED(leftOrigin) , int WXUNUSED(rightOrigin) )
1563{
1564 if ( IsTopLevel() )
1565 return ;
1566
d15694e8 1567 bool hasFocus = GetPeer()->NeedsFocusRect() && HasFocus();
524c47aa
SC
1568
1569 // back to the surrounding frame rectangle
1570 int tx,ty,tw,th;
69ce9cea 1571
d15694e8
SC
1572 GetPeer()->GetSize( tw, th );
1573 GetPeer()->GetPosition( tx, ty );
524c47aa 1574
524c47aa
SC
1575#if wxOSX_USE_COCOA_OR_CARBON
1576
524c47aa 1577 {
a6ac3d64
SC
1578 CGRect cgrect = CGRectMake( tx-1 , ty-1 , tw+2 ,
1579 th+2 ) ;
524c47aa 1580
524c47aa
SC
1581 CGContextRef cgContext = (CGContextRef) GetParent()->MacGetCGContextRef() ;
1582 wxASSERT( cgContext ) ;
1583
d15694e8 1584 if ( GetPeer()->NeedsFrame() )
524c47aa 1585 {
f2f6030e
SC
1586 HIThemeFrameDrawInfo info ;
1587 memset( &info, 0 , sizeof(info) ) ;
1588
1589 info.version = 0 ;
1590 info.kind = 0 ;
1591 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive ;
1592 info.isFocused = hasFocus ;
1593
1594 if ( HasFlag(wxRAISED_BORDER) || HasFlag(wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
1595 {
1596 info.kind = kHIThemeFrameTextFieldSquare ;
1597 HIThemeDrawFrame( &cgrect , &info , cgContext , kHIThemeOrientationNormal ) ;
1598 }
1599 else if ( HasFlag(wxSIMPLE_BORDER) )
1600 {
1601 info.kind = kHIThemeFrameListBox ;
1602 HIThemeDrawFrame( &cgrect , &info , cgContext , kHIThemeOrientationNormal ) ;
1603 }
524c47aa 1604 }
69ce9cea 1605
f2f6030e 1606 if ( hasFocus )
524c47aa
SC
1607 {
1608 HIThemeDrawFocusRect( &cgrect , true , cgContext , kHIThemeOrientationNormal ) ;
1609 }
524c47aa
SC
1610 }
1611#endif // wxOSX_USE_COCOA_OR_CARBON
1612}
1613
1614void wxWindowMac::RemoveChild( wxWindowBase *child )
1615{
11fed901 1616#if wxUSE_SCROLLBAR
524c47aa
SC
1617 if ( child == m_hScrollBar )
1618 m_hScrollBar = NULL ;
1619 if ( child == m_vScrollBar )
1620 m_vScrollBar = NULL ;
2ae3afa0
JS
1621 if ( child == m_growBox )
1622 m_growBox = NULL ;
11fed901 1623#endif
524c47aa
SC
1624 wxWindowBase::RemoveChild( child ) ;
1625}
1626
1627void wxWindowMac::DoUpdateScrollbarVisibility()
1628{
11fed901 1629#if wxUSE_SCROLLBAR
524c47aa
SC
1630 bool triggerSizeEvent = false;
1631
1632 if ( m_hScrollBar )
1633 {
1634 bool showHScrollBar = m_hScrollBarAlwaysShown || m_hScrollBar->IsNeeded();
1635
1636 if ( m_hScrollBar->IsShown() != showHScrollBar )
1637 {
1638 m_hScrollBar->Show( showHScrollBar );
1639 triggerSizeEvent = true;
1640 }
1641 }
1642
1643 if ( m_vScrollBar)
1644 {
1645 bool showVScrollBar = m_vScrollBarAlwaysShown || m_vScrollBar->IsNeeded();
1646
1647 if ( m_vScrollBar->IsShown() != showVScrollBar )
1648 {
1649 m_vScrollBar->Show( showVScrollBar ) ;
1650 triggerSizeEvent = true;
1651 }
1652 }
1653
1654 MacRepositionScrollBars() ;
1655 if ( triggerSizeEvent )
1656 {
6062fe5c 1657 MacOnInternalSize();
524c47aa
SC
1658 wxSizeEvent event(GetSize(), m_windowId);
1659 event.SetEventObject(this);
1660 HandleWindowEvent(event);
1661 }
11fed901 1662#endif
524c47aa
SC
1663}
1664
1665// New function that will replace some of the above.
1666void wxWindowMac::SetScrollbar(int orient, int pos, int thumb,
1667 int range, bool refresh)
1668{
11fed901 1669#if wxUSE_SCROLLBAR
209a9f58
VZ
1670 // Updating scrollbars when window is being deleted is useless and
1671 // currently results in asserts in client-to-screen coordinates conversion
1672 // code which is used by DoUpdateScrollbarVisibility() so just skip it.
1673 if ( m_isBeingDeleted )
1674 return;
1675
524c47aa
SC
1676 if ( orient == wxHORIZONTAL && m_hScrollBar )
1677 m_hScrollBar->SetScrollbar(pos, thumb, range, thumb, refresh);
1678 else if ( orient == wxVERTICAL && m_vScrollBar )
1679 m_vScrollBar->SetScrollbar(pos, thumb, range, thumb, refresh);
1680
1681 DoUpdateScrollbarVisibility();
11fed901 1682#endif
524c47aa
SC
1683}
1684
1685// Does a physical scroll
1686void wxWindowMac::ScrollWindow(int dx, int dy, const wxRect *rect)
1687{
1688 if ( dx == 0 && dy == 0 )
1689 return ;
1690
1691 int width , height ;
1692 GetClientSize( &width , &height ) ;
1693
1694 {
1695 wxRect scrollrect( MacGetLeftBorderSize() , MacGetTopBorderSize() , width , height ) ;
1696 if ( rect )
1697 scrollrect.Intersect( *rect ) ;
1698 // as the native control might be not a 0/0 wx window coordinates, we have to offset
1699 scrollrect.Offset( -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
1700
d15694e8 1701 GetPeer()->ScrollRect( &scrollrect, dx, dy );
524c47aa
SC
1702 }
1703
1704 wxWindowMac *child;
1705 int x, y, w, h;
1706 for (wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); node; node = node->GetNext())
1707 {
1708 child = node->GetData();
1709 if (child == NULL)
1710 continue;
82f8ea85 1711
524c47aa
SC
1712 if (child->IsTopLevel())
1713 continue;
c59aa14a 1714
82f8ea85
SC
1715 if ( !IsClientAreaChild(child) )
1716 continue;
524c47aa
SC
1717
1718 child->GetPosition( &x, &y );
1719 child->GetSize( &w, &h );
1720 if (rect)
1721 {
1722 wxRect rc( x, y, w, h );
1723 if (rect->Intersects( rc ))
1724 child->SetSize( x + dx, y + dy, w, h, wxSIZE_AUTO|wxSIZE_ALLOW_MINUS_ONE );
1725 }
1726 else
1727 {
1728 child->SetSize( x + dx, y + dy, w, h, wxSIZE_AUTO|wxSIZE_ALLOW_MINUS_ONE );
1729 }
1730 }
1731}
1732
1733void wxWindowMac::MacOnScroll( wxScrollEvent &event )
1734{
11fed901 1735#if wxUSE_SCROLLBAR
524c47aa
SC
1736 if ( event.GetEventObject() == m_vScrollBar || event.GetEventObject() == m_hScrollBar )
1737 {
1738 wxScrollWinEvent wevent;
1739 wevent.SetPosition(event.GetPosition());
1740 wevent.SetOrientation(event.GetOrientation());
1741 wevent.SetEventObject(this);
1742
1743 if (event.GetEventType() == wxEVT_SCROLL_TOP)
1744 wevent.SetEventType( wxEVT_SCROLLWIN_TOP );
1745 else if (event.GetEventType() == wxEVT_SCROLL_BOTTOM)
1746 wevent.SetEventType( wxEVT_SCROLLWIN_BOTTOM );
1747 else if (event.GetEventType() == wxEVT_SCROLL_LINEUP)
1748 wevent.SetEventType( wxEVT_SCROLLWIN_LINEUP );
1749 else if (event.GetEventType() == wxEVT_SCROLL_LINEDOWN)
1750 wevent.SetEventType( wxEVT_SCROLLWIN_LINEDOWN );
1751 else if (event.GetEventType() == wxEVT_SCROLL_PAGEUP)
1752 wevent.SetEventType( wxEVT_SCROLLWIN_PAGEUP );
1753 else if (event.GetEventType() == wxEVT_SCROLL_PAGEDOWN)
1754 wevent.SetEventType( wxEVT_SCROLLWIN_PAGEDOWN );
1755 else if (event.GetEventType() == wxEVT_SCROLL_THUMBTRACK)
1756 wevent.SetEventType( wxEVT_SCROLLWIN_THUMBTRACK );
1757 else if (event.GetEventType() == wxEVT_SCROLL_THUMBRELEASE)
1758 wevent.SetEventType( wxEVT_SCROLLWIN_THUMBRELEASE );
1759
1760 HandleWindowEvent(wevent);
1761 }
11fed901 1762#endif
524c47aa
SC
1763}
1764
524c47aa
SC
1765wxWindow *wxWindowBase::DoFindFocus()
1766{
f06e0fea 1767 return wxFindWindowFromWXWidget(wxWidgetImpl::FindFocus());
524c47aa
SC
1768}
1769
524c47aa
SC
1770// Raise the window to the top of the Z order
1771void wxWindowMac::Raise()
1772{
d15694e8 1773 GetPeer()->Raise();
524c47aa
SC
1774}
1775
1776// Lower the window to the bottom of the Z order
1777void wxWindowMac::Lower()
1778{
d15694e8 1779 GetPeer()->Lower();
524c47aa
SC
1780}
1781
1782// static wxWindow *gs_lastWhich = NULL;
1783
1784bool wxWindowMac::MacSetupCursor( const wxPoint& pt )
1785{
1786 // first trigger a set cursor event
1787
1788 wxPoint clientorigin = GetClientAreaOrigin() ;
1789 wxSize clientsize = GetClientSize() ;
1790 wxCursor cursor ;
1791 if ( wxRect2DInt( clientorigin.x , clientorigin.y , clientsize.x , clientsize.y ).Contains( wxPoint2DInt( pt ) ) )
1792 {
1793 wxSetCursorEvent event( pt.x , pt.y );
1794
1795 bool processedEvtSetCursor = HandleWindowEvent(event);
1796 if ( processedEvtSetCursor && event.HasCursor() )
1797 {
1798 cursor = event.GetCursor() ;
1799 }
1800 else
1801 {
1802 // the test for processedEvtSetCursor is here to prevent using m_cursor
1803 // if the user code caught EVT_SET_CURSOR() and returned nothing from
1804 // it - this is a way to say that our cursor shouldn't be used for this
1805 // point
a1b806b9 1806 if ( !processedEvtSetCursor && m_cursor.IsOk() )
524c47aa
SC
1807 cursor = m_cursor ;
1808
1809 if ( !wxIsBusy() && !GetParent() )
1810 cursor = *wxSTANDARD_CURSOR ;
1811 }
1812
a1b806b9 1813 if ( cursor.IsOk() )
524c47aa
SC
1814 cursor.MacInstall() ;
1815 }
1816
a1b806b9 1817 return cursor.IsOk() ;
524c47aa
SC
1818}
1819
1820wxString wxWindowMac::MacGetToolTipString( wxPoint &WXUNUSED(pt) )
1821{
1822#if wxUSE_TOOLTIPS
1823 if ( m_tooltip )
1824 return m_tooltip->GetTip() ;
1825#endif
1826
1827 return wxEmptyString ;
1828}
1829
1830void wxWindowMac::ClearBackground()
1831{
1832 Refresh() ;
1833 Update() ;
1834}
1835
1836void wxWindowMac::Update()
1837{
1838 wxNonOwnedWindow* top = MacGetTopLevelWindow();
1839 if (top)
1840 top->Update() ;
1841}
1842
1843wxNonOwnedWindow* wxWindowMac::MacGetTopLevelWindow() const
1844{
1845 wxWindowMac *iter = (wxWindowMac*)this ;
1846
1847 while ( iter )
1848 {
1849 if ( iter->IsTopLevel() )
1850 {
1851 wxTopLevelWindow* toplevel = wxDynamicCast(iter,wxTopLevelWindow);
1852 if ( toplevel )
1853 return toplevel;
1854#if wxUSE_POPUPWIN
1855 wxPopupWindow* popupwin = wxDynamicCast(iter,wxPopupWindow);
1856 if ( popupwin )
1857 return popupwin;
1858#endif
1859 }
1860 iter = iter->GetParent() ;
1861 }
1862
1863 return NULL ;
1864}
1865
1866const wxRect& wxWindowMac::MacGetClippedClientRect() const
1867{
1868 MacUpdateClippedRects() ;
1869
1870 return m_cachedClippedClientRect ;
1871}
1872
1873const wxRect& wxWindowMac::MacGetClippedRect() const
1874{
1875 MacUpdateClippedRects() ;
1876
1877 return m_cachedClippedRect ;
1878}
1879
1880const wxRect&wxWindowMac:: MacGetClippedRectWithOuterStructure() const
1881{
1882 MacUpdateClippedRects() ;
1883
1884 return m_cachedClippedRectWithOuterStructure ;
1885}
1886
1887const wxRegion& wxWindowMac::MacGetVisibleRegion( bool includeOuterStructures )
1888{
1889 static wxRegion emptyrgn ;
1890
1891 if ( !m_isBeingDeleted && IsShownOnScreen() )
1892 {
1893 MacUpdateClippedRects() ;
1894 if ( includeOuterStructures )
1895 return m_cachedClippedRegionWithOuterStructure ;
1896 else
1897 return m_cachedClippedRegion ;
1898 }
1899 else
1900 {
1901 return emptyrgn ;
1902 }
1903}
1904
1905void wxWindowMac::MacUpdateClippedRects() const
1906{
1907#if wxOSX_USE_CARBON
1908 if ( m_cachedClippedRectValid )
1909 return ;
1910
1911 // includeOuterStructures is true if we try to draw somthing like a focus ring etc.
1912 // also a window dc uses this, in this case we only clip in the hierarchy for hard
1913 // borders like a scrollwindow, splitter etc otherwise we end up in a paranoia having
1914 // to add focus borders everywhere
1915
1916 Rect rIncludingOuterStructures ;
1917
1918 int tx,ty,tw,th;
69ce9cea 1919
d15694e8
SC
1920 GetPeer()->GetSize( tw, th );
1921 GetPeer()->GetPosition( tx, ty );
524c47aa
SC
1922
1923 Rect r = { ty,tx, ty+th, tx+tw };
1924
1925 r.left -= MacGetLeftBorderSize() ;
1926 r.top -= MacGetTopBorderSize() ;
1927 r.bottom += MacGetBottomBorderSize() ;
1928 r.right += MacGetRightBorderSize() ;
1929
1930 r.right -= r.left ;
1931 r.bottom -= r.top ;
1932 r.left = 0 ;
1933 r.top = 0 ;
1934
1935 rIncludingOuterStructures = r ;
1936 InsetRect( &rIncludingOuterStructures , -4 , -4 ) ;
1937
1938 wxRect cl = GetClientRect() ;
1939 Rect rClient = { cl.y , cl.x , cl.y + cl.height , cl.x + cl.width } ;
1940
1941 int x , y ;
1942 wxSize size ;
1943 const wxWindow* child = (wxWindow*) this ;
1944 const wxWindow* parent = NULL ;
1945
1946 while ( !child->IsTopLevel() && ( parent = child->GetParent() ) != NULL )
1947 {
1948 if ( parent->MacIsChildOfClientArea(child) )
1949 {
1950 size = parent->GetClientSize() ;
1951 wxPoint origin = parent->GetClientAreaOrigin() ;
1952 x = origin.x ;
1953 y = origin.y ;
1954 }
1955 else
1956 {
1957 // this will be true for scrollbars, toolbars etc.
1958 size = parent->GetSize() ;
1959 y = parent->MacGetTopBorderSize() ;
1960 x = parent->MacGetLeftBorderSize() ;
1961 size.x -= parent->MacGetLeftBorderSize() + parent->MacGetRightBorderSize() ;
1962 size.y -= parent->MacGetTopBorderSize() + parent->MacGetBottomBorderSize() ;
1963 }
1964
1965 parent->MacWindowToRootWindow( &x, &y ) ;
1966 MacRootWindowToWindow( &x , &y ) ;
1967
1968 Rect rparent = { y , x , y + size.y , x + size.x } ;
1969
1970 // the wxwindow and client rects will always be clipped
1971 SectRect( &r , &rparent , &r ) ;
1972 SectRect( &rClient , &rparent , &rClient ) ;
1973
1974 // the structure only at 'hard' borders
1975 if ( parent->MacClipChildren() ||
1976 ( parent->GetParent() && parent->GetParent()->MacClipGrandChildren() ) )
1977 {
1978 SectRect( &rIncludingOuterStructures , &rparent , &rIncludingOuterStructures ) ;
1979 }
1980
1981 child = parent ;
1982 }
1983
1984 m_cachedClippedRect = wxRect( r.left , r.top , r.right - r.left , r.bottom - r.top ) ;
1985 m_cachedClippedClientRect = wxRect( rClient.left , rClient.top ,
1986 rClient.right - rClient.left , rClient.bottom - rClient.top ) ;
1987 m_cachedClippedRectWithOuterStructure = wxRect(
1988 rIncludingOuterStructures.left , rIncludingOuterStructures.top ,
1989 rIncludingOuterStructures.right - rIncludingOuterStructures.left ,
1990 rIncludingOuterStructures.bottom - rIncludingOuterStructures.top ) ;
1991
1992 m_cachedClippedRegionWithOuterStructure = wxRegion( m_cachedClippedRectWithOuterStructure ) ;
1993 m_cachedClippedRegion = wxRegion( m_cachedClippedRect ) ;
1994 m_cachedClippedClientRegion = wxRegion( m_cachedClippedClientRect ) ;
1995
1996 m_cachedClippedRectValid = true ;
1997#endif
1998}
1999
2000/*
2001 This function must not change the updatergn !
2002 */
5398a2e0 2003bool wxWindowMac::MacDoRedraw( long time )
524c47aa
SC
2004{
2005 bool handled = false ;
69ce9cea 2006
5398a2e0
SC
2007 wxRegion formerUpdateRgn = m_updateRegion;
2008 wxRegion clientUpdateRgn = formerUpdateRgn;
524c47aa 2009
69ce9cea
VZ
2010 const wxRect clientRect = GetClientRect();
2011
2012 clientUpdateRgn.Intersect(clientRect);
2013
5398a2e0 2014 // first send an erase event to the entire update area
69ce9cea 2015 const wxBackgroundStyle bgStyle = GetBackgroundStyle();
6a5594e0 2016 switch ( bgStyle )
524c47aa 2017 {
6a5594e0
VZ
2018 case wxBG_STYLE_ERASE:
2019 case wxBG_STYLE_SYSTEM:
8301a0e8 2020 case wxBG_STYLE_COLOUR:
69ce9cea 2021 {
6a5594e0
VZ
2022 // for the toplevel window this really is the entire area for
2023 // all the others only their client area, otherwise they might
2024 // be drawing with full alpha and eg put blue into the grow-box
2025 // area of a scrolled window (scroll sample)
2026 wxWindowDC dc(this);
2027 if ( IsTopLevel() )
2028 dc.SetDeviceClippingRegion(formerUpdateRgn);
2029 else
2030 dc.SetDeviceClippingRegion(clientUpdateRgn);
2031
2032 if ( bgStyle == wxBG_STYLE_ERASE )
2033 {
2034 wxEraseEvent eevent( GetId(), &dc );
2035 eevent.SetEventObject( this );
2036 if ( ProcessWindowEvent( eevent ) )
2037 break;
2038 }
2039
773809f5 2040 if ( UseBgCol() )
6a5594e0
VZ
2041 {
2042 dc.SetBackground(GetBackgroundColour());
2043 dc.Clear();
2044 }
69ce9cea 2045 }
6a5594e0
VZ
2046 break;
2047
2048 case wxBG_STYLE_PAINT:
4f6bcb83 2049 case wxBG_STYLE_TRANSPARENT:
6a5594e0
VZ
2050 // nothing to do, user-defined EVT_PAINT handler will overwrite the
2051 // entire window client area
2052 break;
2053
2054 default:
2055 wxFAIL_MSG( "unsupported background style" );
5398a2e0 2056 }
524c47aa 2057
82f8ea85
SC
2058 // as this is a full window, shouldn't be necessary anymore
2059 // MacPaintGrowBox();
524c47aa 2060
69ce9cea
VZ
2061 // calculate a client-origin version of the update rgn and set
2062 // m_updateRegion to that
2063 clientUpdateRgn.Offset(-clientRect.GetPosition());
5398a2e0 2064 m_updateRegion = clientUpdateRgn ;
524c47aa 2065
5398a2e0
SC
2066 if ( !m_updateRegion.Empty() )
2067 {
2068 // paint the window itself
524c47aa 2069
45f5bb03 2070 wxPaintEvent event(GetId());
5398a2e0
SC
2071 event.SetTimestamp(time);
2072 event.SetEventObject(this);
2073 handled = HandleWindowEvent(event);
2074 }
524c47aa 2075
5398a2e0 2076 m_updateRegion = formerUpdateRgn;
7dc941e3
SC
2077
2078 wxNonOwnedWindow* top = MacGetTopLevelWindow();
2079 if (top)
2080 top->WindowWasPainted() ;
2081
5398a2e0
SC
2082 return handled;
2083}
524c47aa 2084
5398a2e0
SC
2085void wxWindowMac::MacPaintChildrenBorders()
2086{
2087 // now we cannot rely on having its borders drawn by a window itself, as it does not
2088 // get the updateRgn wide enough to always do so, so we do it from the parent
2089 // this would also be the place to draw any custom backgrounds for native controls
2090 // in Composited windowing
2091 wxPoint clientOrigin = GetClientAreaOrigin() ;
524c47aa 2092
5398a2e0
SC
2093 wxWindowMac *child;
2094 int x, y, w, h;
2095 for (wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); node; node = node->GetNext())
2096 {
2097 child = node->GetData();
2098 if (child == NULL)
2099 continue;
11fed901 2100#if wxUSE_SCROLLBAR
5398a2e0
SC
2101 if (child == m_vScrollBar)
2102 continue;
2103 if (child == m_hScrollBar)
2104 continue;
2ae3afa0
JS
2105 if (child == m_growBox)
2106 continue;
11fed901 2107#endif
5398a2e0
SC
2108 if (child->IsTopLevel())
2109 continue;
2110 if (!child->IsShown())
2111 continue;
2112
2113 // only draw those in the update region (add a safety margin of 10 pixels for shadow effects
2114
2115 child->GetPosition( &x, &y );
2116 child->GetSize( &w, &h );
69ce9cea 2117
5398a2e0 2118 if ( m_updateRegion.Contains(clientOrigin.x+x-10, clientOrigin.y+y-10, w+20, h+20) )
524c47aa 2119 {
5398a2e0
SC
2120 // paint custom borders
2121 wxNcPaintEvent eventNc( child->GetId() );
2122 eventNc.SetEventObject( child );
2123 if ( !child->HandleWindowEvent( eventNc ) )
524c47aa 2124 {
5398a2e0 2125 child->MacPaintBorders(0, 0) ;
524c47aa
SC
2126 }
2127 }
2128 }
524c47aa
SC
2129}
2130
2131
2132WXWindow wxWindowMac::MacGetTopLevelWindowRef() const
2133{
69ce9cea 2134 wxNonOwnedWindow* tlw = MacGetTopLevelWindow();
524c47aa
SC
2135 return tlw ? tlw->GetWXWindow() : NULL ;
2136}
2137
2138bool wxWindowMac::MacHasScrollBarCorner() const
2139{
11fed901 2140#if wxUSE_SCROLLBAR
524c47aa
SC
2141 /* Returns whether the scroll bars in a wxScrolledWindow should be
2142 * shortened. Scroll bars should be shortened if either:
2143 *
2144 * - both scroll bars are visible, or
2145 *
2146 * - there is a resize box in the parent frame's corner and this
2147 * window shares the bottom and right edge with the parent
2148 * frame.
2149 */
2150
2151 if ( m_hScrollBar == NULL && m_vScrollBar == NULL )
2152 return false;
2153
2154 if ( ( m_hScrollBar && m_hScrollBar->IsShown() )
2155 && ( m_vScrollBar && m_vScrollBar->IsShown() ) )
2156 {
2157 // Both scroll bars visible
2158 return true;
2159 }
2160 else
2161 {
2162 wxPoint thisWindowBottomRight = GetScreenRect().GetBottomRight();
2163
2164 for ( const wxWindow *win = (wxWindow*)this; win; win = win->GetParent() )
2165 {
2166 const wxFrame *frame = wxDynamicCast( win, wxFrame ) ;
2167 if ( frame )
2168 {
2169 if ( frame->GetWindowStyleFlag() & wxRESIZE_BORDER )
2170 {
2171 // Parent frame has resize handle
2172 wxPoint frameBottomRight = frame->GetScreenRect().GetBottomRight();
2173
2174 // Note: allow for some wiggle room here as wxMac's
2175 // window rect calculations seem to be imprecise
2176 if ( abs( thisWindowBottomRight.x - frameBottomRight.x ) <= 2
2177 && abs( thisWindowBottomRight.y - frameBottomRight.y ) <= 2 )
2178 {
2179 // Parent frame has resize handle and shares
2180 // right bottom corner
2181 return true ;
2182 }
2183 else
2184 {
2185 // Parent frame has resize handle but doesn't
2186 // share right bottom corner
2187 return false ;
2188 }
2189 }
2190 else
2191 {
2192 // Parent frame doesn't have resize handle
2193 return false ;
2194 }
2195 }
2196 }
2197
2198 // No parent frame found
2199 return false ;
2200 }
11fed901
SC
2201#else
2202 return false;
2203#endif
524c47aa
SC
2204}
2205
2206void wxWindowMac::MacCreateScrollBars( long style )
2207{
11fed901 2208#if wxUSE_SCROLLBAR
524c47aa
SC
2209 wxASSERT_MSG( m_vScrollBar == NULL && m_hScrollBar == NULL , wxT("attempt to create window twice") ) ;
2210
2211 if ( style & ( wxVSCROLL | wxHSCROLL ) )
2212 {
2213 int scrlsize = MAC_SCROLLBAR_SIZE ;
2214 if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL || GetWindowVariant() == wxWINDOW_VARIANT_MINI )
2215 {
2216 scrlsize = MAC_SMALL_SCROLLBAR_SIZE ;
2217 }
2218
2219 int adjust = MacHasScrollBarCorner() ? scrlsize - 1: 0 ;
2220 int width, height ;
2221 GetClientSize( &width , &height ) ;
2222
2223 wxPoint vPoint(width - scrlsize, 0) ;
2224 wxSize vSize(scrlsize, height - adjust) ;
2225 wxPoint hPoint(0, height - scrlsize) ;
2226 wxSize hSize(width - adjust, scrlsize) ;
2227
2228 // we have to set the min size to a smaller value, otherwise they cannot get smaller (InitialSize sets MinSize)
2229 if ( style & wxVSCROLL )
2230 {
2231 m_vScrollBar = new wxScrollBar((wxWindow*)this, wxID_ANY, vPoint, vSize , wxVERTICAL);
2232 m_vScrollBar->SetMinSize( wxDefaultSize );
2233 }
2234
2235 if ( style & wxHSCROLL )
2236 {
2237 m_hScrollBar = new wxScrollBar((wxWindow*)this, wxID_ANY, hPoint, hSize , wxHORIZONTAL);
2238 m_hScrollBar->SetMinSize( wxDefaultSize );
2239 }
2ae3afa0
JS
2240
2241 wxPoint gPoint(width - scrlsize, height - scrlsize);
2242 wxSize gSize(scrlsize, scrlsize);
82f8ea85 2243 m_growBox = new wxBlindPlateWindow((wxWindow *)this, wxID_ANY, gPoint, gSize, 0);
524c47aa
SC
2244 }
2245
2246 // because the create does not take into account the client area origin
2247 // we might have a real position shift
2248 MacRepositionScrollBars() ;
11fed901 2249#endif
524c47aa
SC
2250}
2251
2252bool wxWindowMac::MacIsChildOfClientArea( const wxWindow* child ) const
2253{
11fed901
SC
2254 bool result = ((child == NULL)
2255#if wxUSE_SCROLLBAR
2ae3afa0 2256 || ((child != m_hScrollBar) && (child != m_vScrollBar) && (child != m_growBox))
11fed901
SC
2257#endif
2258 );
524c47aa
SC
2259
2260 return result ;
2261}
2262
2263void wxWindowMac::MacRepositionScrollBars()
2264{
11fed901 2265#if wxUSE_SCROLLBAR
524c47aa
SC
2266 if ( !m_hScrollBar && !m_vScrollBar )
2267 return ;
2268
2269 int scrlsize = m_hScrollBar ? m_hScrollBar->GetSize().y : ( m_vScrollBar ? m_vScrollBar->GetSize().x : MAC_SCROLLBAR_SIZE ) ;
2270 int adjust = MacHasScrollBarCorner() ? scrlsize - 1 : 0 ;
2271
2272 // get real client area
2273 int width, height ;
2274 GetSize( &width , &height );
2275
2276 width -= MacGetLeftBorderSize() + MacGetRightBorderSize();
2277 height -= MacGetTopBorderSize() + MacGetBottomBorderSize();
2278
2279 wxPoint vPoint( width - scrlsize, 0 ) ;
2280 wxSize vSize( scrlsize, height - adjust ) ;
2281 wxPoint hPoint( 0 , height - scrlsize ) ;
2282 wxSize hSize( width - adjust, scrlsize ) ;
2283
2284 if ( m_vScrollBar )
2285 m_vScrollBar->SetSize( vPoint.x , vPoint.y, vSize.x, vSize.y , wxSIZE_ALLOW_MINUS_ONE );
2286 if ( m_hScrollBar )
2287 m_hScrollBar->SetSize( hPoint.x , hPoint.y, hSize.x, hSize.y, wxSIZE_ALLOW_MINUS_ONE );
2ae3afa0
JS
2288 if ( m_growBox )
2289 {
2290 if ( MacHasScrollBarCorner() )
2291 {
2292 m_growBox->SetSize( width - scrlsize, height - scrlsize, wxDefaultCoord, wxDefaultCoord, wxSIZE_USE_EXISTING );
2293 if ( !m_growBox->IsShown() )
2294 m_growBox->Show();
2295 }
2296 else
2297 {
2298 if ( m_growBox->IsShown() )
2299 m_growBox->Hide();
2300 }
2301 }
11fed901 2302#endif
524c47aa
SC
2303}
2304
2305bool wxWindowMac::AcceptsFocus() const
2306{
d15694e8 2307 if ( GetPeer() == NULL || GetPeer()->IsUserPane() )
f06e0fea
SC
2308 return wxWindowBase::AcceptsFocus();
2309 else
d15694e8 2310 return GetPeer()->CanFocus();
524c47aa
SC
2311}
2312
2313void wxWindowMac::MacSuperChangedPosition()
2314{
2315 // only window-absolute structures have to be moved i.e. controls
2316
2317 m_cachedClippedRectValid = false ;
2318
2319 wxWindowMac *child;
2320 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2321 while ( node )
2322 {
2323 child = node->GetData();
2324 child->MacSuperChangedPosition() ;
2325
2326 node = node->GetNext();
2327 }
2328}
2329
2330void wxWindowMac::MacTopLevelWindowChangedPosition()
2331{
2332 // only screen-absolute structures have to be moved i.e. glcanvas
2333
2334 wxWindowMac *child;
2335 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2336 while ( node )
2337 {
2338 child = node->GetData();
2339 child->MacTopLevelWindowChangedPosition() ;
2340
2341 node = node->GetNext();
2342 }
2343}
2344
54ea2834 2345long wxWindowMac::MacGetWXBorderSize() const
524c47aa
SC
2346{
2347 if ( IsTopLevel() )
2348 return 0 ;
2349
2350 SInt32 border = 0 ;
2351
d15694e8 2352 if ( GetPeer() && GetPeer()->NeedsFrame() )
524c47aa 2353 {
f2f6030e
SC
2354 if (HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER))
2355 {
524c47aa 2356#if wxOSX_USE_COCOA_OR_CARBON
f2f6030e
SC
2357 // this metric is only the 'outset' outside the simple frame rect
2358 GetThemeMetric( kThemeMetricEditTextFrameOutset , &border ) ;
2359 border += 1;
524c47aa 2360#else
f2f6030e 2361 border += 2;
524c47aa 2362#endif
f2f6030e
SC
2363 }
2364 else if (HasFlag(wxSIMPLE_BORDER))
2365 {
524c47aa 2366#if wxOSX_USE_COCOA_OR_CARBON
f2f6030e
SC
2367 // this metric is only the 'outset' outside the simple frame rect
2368 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2369 border += 1;
524c47aa 2370#else
f2f6030e 2371 border += 1;
524c47aa 2372#endif
f2f6030e 2373 }
524c47aa
SC
2374 }
2375
2376 return border ;
2377}
2378
54ea2834
SC
2379long wxWindowMac::MacGetLeftBorderSize() const
2380{
2381 // the wx borders are all symmetric in mac themes
2382 long border = MacGetWXBorderSize() ;
2383
d15694e8 2384 if ( GetPeer() )
54ea2834
SC
2385 {
2386 int left, top, right, bottom;
d15694e8 2387 GetPeer()->GetLayoutInset( left, top, right, bottom );
54ea2834
SC
2388 border -= left;
2389 }
2390
2391 return border;
2392}
2393
2394
524c47aa
SC
2395long wxWindowMac::MacGetRightBorderSize() const
2396{
54ea2834
SC
2397 // the wx borders are all symmetric in mac themes
2398 long border = MacGetWXBorderSize() ;
2399
d15694e8 2400 if ( GetPeer() )
54ea2834
SC
2401 {
2402 int left, top, right, bottom;
d15694e8 2403 GetPeer()->GetLayoutInset( left, top, right, bottom );
54ea2834
SC
2404 border -= right;
2405 }
2406
2407 return border;
524c47aa
SC
2408}
2409
2410long wxWindowMac::MacGetTopBorderSize() const
2411{
54ea2834
SC
2412 // the wx borders are all symmetric in mac themes
2413 long border = MacGetWXBorderSize() ;
2414
d15694e8 2415 if ( GetPeer() )
54ea2834
SC
2416 {
2417 int left, top, right, bottom;
d15694e8 2418 GetPeer()->GetLayoutInset( left, top, right, bottom );
54ea2834
SC
2419 border -= top;
2420 }
2421
2422 return border;
524c47aa
SC
2423}
2424
2425long wxWindowMac::MacGetBottomBorderSize() const
2426{
54ea2834
SC
2427 // the wx borders are all symmetric in mac themes
2428 long border = MacGetWXBorderSize() ;
2429
d15694e8 2430 if ( GetPeer() )
54ea2834
SC
2431 {
2432 int left, top, right, bottom;
d15694e8 2433 GetPeer()->GetLayoutInset( left, top, right, bottom );
54ea2834
SC
2434 border -= bottom;
2435 }
2436
2437 return border;
524c47aa
SC
2438}
2439
2440long wxWindowMac::MacRemoveBordersFromStyle( long style )
2441{
2442 return style & ~wxBORDER_MASK ;
2443}
2444
2445// Find the wxWindowMac at the current mouse position, returning the mouse
2446// position.
2447wxWindow * wxFindWindowAtPointer( wxPoint& pt )
2448{
2449 pt = wxGetMousePosition();
2450 wxWindowMac* found = wxFindWindowAtPoint(pt);
2451
2452 return (wxWindow*) found;
2453}
2454
2455// Get the current mouse position.
2456wxPoint wxGetMousePosition()
2457{
2458 int x, y;
2459
2460 wxGetMousePosition( &x, &y );
2461
2462 return wxPoint(x, y);
2463}
2464
2465void wxWindowMac::OnMouseEvent( wxMouseEvent &event )
2466{
2467 if ( event.GetEventType() == wxEVT_RIGHT_DOWN )
2468 {
2469 // copied from wxGTK : CS
2470 // VZ: shouldn't we move this to base class then?
2471
2472 // generate a "context menu" event: this is similar to wxEVT_RIGHT_DOWN
2473 // except that:
2474 //
2475 // (a) it's a command event and so is propagated to the parent
2476 // (b) under MSW it can be generated from kbd too
2477 // (c) it uses screen coords (because of (a))
2478 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
2479 this->GetId(),
2480 this->ClientToScreen(event.GetPosition()));
2481 evtCtx.SetEventObject(this);
2482 if ( ! HandleWindowEvent(evtCtx) )
2483 event.Skip() ;
2484 }
2485 else
2486 {
2487 event.Skip() ;
2488 }
2489}
2490
19c7ac3d 2491void wxWindowMac::TriggerScrollEvent( wxEventType WXUNUSED(scrollEvent) )
524c47aa
SC
2492{
2493}
2494
2495Rect wxMacGetBoundsForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
2496{
2497 int x, y, w, h ;
2498
2499 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
0601befc 2500 Rect bounds = { static_cast<short>(y), static_cast<short>(x), static_cast<short>(y + h), static_cast<short>(x + w) };
524c47aa
SC
2501
2502 return bounds ;
2503}
2504
0faf03bf 2505bool wxWindowMac::OSXHandleClicked( double WXUNUSED(timestampsec) )
524c47aa
SC
2506{
2507 return false;
2508}
2509
2510wxInt32 wxWindowMac::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF event )
2511{
2512#if wxOSX_USE_COCOA_OR_CARBON
4eb5a0ec 2513 if ( OSXHandleClicked( GetEventTime((EventRef)event) ) )
524c47aa 2514 return noErr;
69ce9cea 2515
524c47aa
SC
2516 return eventNotHandledErr ;
2517#else
2518 return 0;
2519#endif
2520}
2521
2522bool wxWindowMac::Reparent(wxWindowBase *newParentBase)
2523{
2524 wxWindowMac *newParent = (wxWindowMac *)newParentBase;
2525 if ( !wxWindowBase::Reparent(newParent) )
2526 return false;
2527
d15694e8
SC
2528 GetPeer()->RemoveFromParent();
2529 GetPeer()->Embed( GetParent()->GetPeer() );
b80fdc02
JS
2530
2531 MacChildAdded();
524c47aa
SC
2532 return true;
2533}
2534
2535bool wxWindowMac::SetTransparent(wxByte alpha)
2536{
2537 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT);
2538
2539 if ( alpha != m_macAlpha )
2540 {
2541 m_macAlpha = alpha ;
2542 Refresh() ;
2543 }
2544 return true ;
2545}
2546
2547
2548bool wxWindowMac::CanSetTransparent()
2549{
2550 return true ;
2551}
2552
2553wxByte wxWindowMac::GetTransparent() const
2554{
2555 return m_macAlpha ;
2556}
2557
2558bool wxWindowMac::IsShownOnScreen() const
2559{
d15694e8 2560 if ( GetPeer() && GetPeer()->IsOk() )
524c47aa 2561 {
d15694e8 2562 bool peerVis = GetPeer()->IsVisible();
524c47aa
SC
2563 bool wxVis = wxWindowBase::IsShownOnScreen();
2564 if( peerVis != wxVis )
2565 {
2566 // CS : put a breakpoint here to investigate differences
2567 // between native an wx visibilities
2568 // the only place where I've encountered them until now
2569 // are the hiding/showing sequences where the vis-changed event is
2570 // first sent to the innermost control, while wx does things
2571 // from the outmost control
2572 wxVis = wxWindowBase::IsShownOnScreen();
2573 return wxVis;
2574 }
2575
d15694e8 2576 return GetPeer()->IsVisible();
524c47aa
SC
2577 }
2578 return wxWindowBase::IsShownOnScreen();
2579}
2580
c1313b54
SC
2581#if wxUSE_HOTKEY && wxOSX_USE_COCOA_OR_CARBON
2582
97d5c7a3
VZ
2583OSStatus
2584wxHotKeyHandler(EventHandlerCallRef WXUNUSED(nextHandler),
2585 EventRef event,
2586 void* WXUNUSED(userData))
c1313b54
SC
2587{
2588 EventHotKeyID hotKeyId;
2589
2590 GetEventParameter( event, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hotKeyId), NULL, &hotKeyId);
2591
97d5c7a3 2592 for ( unsigned i = 0; i < s_hotkeys.size(); ++i )
c1313b54 2593 {
97d5c7a3 2594 if ( s_hotkeys[i].keyId == static_cast<int>(hotKeyId.id) )
c1313b54
SC
2595 {
2596 unsigned char charCode ;
2597 UInt32 keyCode ;
2598 UInt32 modifiers ;
c1313b54
SC
2599 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
2600
2601 GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &charCode );
2602 GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
2603 GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers );
c1313b54
SC
2604
2605 UInt32 keymessage = (keyCode << 8) + charCode;
2606
2607 wxKeyEvent wxevent(wxEVT_HOTKEY);
2608 wxevent.SetId(hotKeyId.id);
2609 wxTheApp->MacCreateKeyEvent( wxevent, s_hotkeys[i].window , keymessage ,
723aa01a 2610 modifiers , when , 0 ) ;
c1313b54
SC
2611
2612 s_hotkeys[i].window->HandleWindowEvent(wxevent);
2613 }
2614 }
2615
2616 return noErr;
2617}
2618
2619bool wxWindowMac::RegisterHotKey(int hotkeyId, int modifiers, int keycode)
2620{
97d5c7a3 2621 for ( unsigned i = 0; i < s_hotkeys.size(); ++i )
c1313b54
SC
2622 {
2623 if ( s_hotkeys[i].keyId == hotkeyId )
2624 {
2625 wxLogLastError(wxT("hotkeyId already registered"));
2626
2627 return false;
2628 }
2629 }
2630
2631 static bool installed = false;
2632 if ( !installed )
2633 {
2634 EventTypeSpec eventType;
2635 eventType.eventClass=kEventClassKeyboard;
2636 eventType.eventKind=kEventHotKeyPressed;
2637
2638 InstallApplicationEventHandler(&wxHotKeyHandler, 1, &eventType, NULL, NULL);
2639 installed = true;
2640 }
2641
2642 UInt32 mac_modifiers=0;
2643 if ( modifiers & wxMOD_ALT )
2644 mac_modifiers |= optionKey;
2645 if ( modifiers & wxMOD_SHIFT )
2646 mac_modifiers |= shiftKey;
3c5f6264 2647 if ( modifiers & wxMOD_RAW_CONTROL )
c1313b54 2648 mac_modifiers |= controlKey;
3c5f6264 2649 if ( modifiers & wxMOD_CONTROL )
c1313b54
SC
2650 mac_modifiers |= cmdKey;
2651
2652 EventHotKeyRef hotKeyRef;
2653 EventHotKeyID hotKeyIDmac;
2654
2655 hotKeyIDmac.signature = 'WXMC';
2656 hotKeyIDmac.id = hotkeyId;
2657
2658 if ( RegisterEventHotKey(wxCharCodeWXToOSX((wxKeyCode)keycode), mac_modifiers, hotKeyIDmac,
2659 GetApplicationEventTarget(), 0, &hotKeyRef) != noErr )
2660 {
2661 wxLogLastError(wxT("RegisterHotKey"));
2662
2663 return false;
2664 }
2665 else
2666 {
2667 wxHotKeyRec v;
2668 v.ref = hotKeyRef;
2669 v.keyId = hotkeyId;
2670 v.window = this;
2671
2672 s_hotkeys.push_back(v);
2673 }
2674
2675 return true;
2676}
2677
2678bool wxWindowMac::UnregisterHotKey(int hotkeyId)
2679{
977b5d7d 2680 for ( int i = ((int)s_hotkeys.size())-1; i>=0; -- i )
c1313b54
SC
2681 {
2682 if ( s_hotkeys[i].keyId == hotkeyId )
2683 {
2684 EventHotKeyRef ref = s_hotkeys[i].ref;
2685 s_hotkeys.erase(s_hotkeys.begin() + i);
2686 if ( UnregisterEventHotKey(ref) != noErr )
2687 {
2688 wxLogLastError(wxT("UnregisterHotKey"));
2689
2690 return false;
2691 }
2692 else
2693 return true;
2694 }
2695 }
2696
2697 return false;
2698}
2699
2700#endif // wxUSE_HOTKEY
2701
4eb5a0ec 2702bool wxWindowMac::OSXHandleKeyEvent( wxKeyEvent& event )
19c7ac3d 2703{
ba950d44
SC
2704 bool handled = false;
2705
2706 // moved the ordinary key event sending AFTER the accel evaluation
19c7ac3d
SC
2707
2708#if wxUSE_ACCEL
2709 if ( !handled && event.GetEventType() == wxEVT_KEY_DOWN)
2710 {
2711 wxWindow *ancestor = this;
2712 while (ancestor)
2713 {
2714 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
2715 if (command != -1)
2716 {
2717 wxEvtHandler * const handler = ancestor->GetEventHandler();
2718
2719 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
2720 handled = handler->ProcessEvent( command_event );
2721
2722 if ( !handled )
2723 {
2724 // accelerators can also be used with buttons, try them too
2725 command_event.SetEventType(wxEVT_COMMAND_BUTTON_CLICKED);
2726 handled = handler->ProcessEvent( command_event );
2727 }
2728
2729 break;
2730 }
2731
2732 if (ancestor->IsTopLevel())
2733 break;
2734
2735 ancestor = ancestor->GetParent();
2736 }
2737 }
2738#endif // wxUSE_ACCEL
ba950d44
SC
2739
2740 if ( !handled )
2741 {
2742 handled = HandleWindowEvent( event ) ;
2743 if ( handled && event.GetSkipped() )
2744 handled = false ;
2745 }
19c7ac3d
SC
2746
2747 return handled ;
2748}
2749
524c47aa 2750//
69ce9cea 2751// wxWidgetImpl
524c47aa
SC
2752//
2753
f55d9f74
SC
2754WX_DECLARE_HASH_MAP(WXWidget, wxWidgetImpl*, wxPointerHash, wxPointerEqual, MacControlMap);
2755
2756static MacControlMap wxWinMacControlList;
2757
2758wxWindowMac *wxFindWindowFromWXWidget(WXWidget inControl )
2759{
2760 wxWidgetImpl* impl = wxWidgetImpl::FindFromWXWidget( inControl );
2761 if ( impl )
2762 return impl->GetWXPeer();
69ce9cea 2763
f55d9f74
SC
2764 return NULL;
2765}
2766
2767wxWidgetImpl *wxWidgetImpl::FindFromWXWidget(WXWidget inControl )
2768{
2769 MacControlMap::iterator node = wxWinMacControlList.find(inControl);
2770
2771 return (node == wxWinMacControlList.end()) ? NULL : node->second;
2772}
2773
2774void wxWidgetImpl::Associate(WXWidget inControl, wxWidgetImpl *impl)
2775{
2776 // adding NULL ControlRef is (first) surely a result of an error and
2777 // (secondly) breaks native event processing
2778 wxCHECK_RET( inControl != (WXWidget) NULL, wxT("attempt to add a NULL WXWidget to control map") );
2779
2780 wxWinMacControlList[inControl] = impl;
2781}
2782
2783void wxWidgetImpl::RemoveAssociations(wxWidgetImpl* impl)
2784{
2785 // iterate over all the elements in the class
2786 // is the iterator stable ? as we might have two associations pointing to the same wxWindow
2787 // we should go on...
2788
2789 bool found = true ;
2790 while ( found )
2791 {
2792 found = false ;
2793 MacControlMap::iterator it;
2794 for ( it = wxWinMacControlList.begin(); it != wxWinMacControlList.end(); ++it )
2795 {
2796 if ( it->second == impl )
2797 {
2798 wxWinMacControlList.erase(it);
2799 found = true ;
2800 break;
2801 }
2802 }
2803 }
2804}
2805
524c47aa
SC
2806IMPLEMENT_ABSTRACT_CLASS( wxWidgetImpl , wxObject )
2807
415f4a01 2808wxWidgetImpl::wxWidgetImpl( wxWindowMac* peer , bool isRootControl, bool isUserPane )
524c47aa
SC
2809{
2810 Init();
2811 m_isRootControl = isRootControl;
415f4a01 2812 m_isUserPane = isUserPane;
524c47aa 2813 m_wxPeer = peer;
809020fc 2814 m_shouldSendEvents = true;
524c47aa
SC
2815}
2816
2817wxWidgetImpl::wxWidgetImpl()
2818{
2819 Init();
2820}
2821
2822wxWidgetImpl::~wxWidgetImpl()
2823{
2824}
2825
2826void wxWidgetImpl::Init()
2827{
2828 m_isRootControl = false;
2829 m_wxPeer = NULL;
2830 m_needsFocusRect = false;
f2f6030e 2831 m_needsFrame = true;
524c47aa
SC
2832}
2833
2834void wxWidgetImpl::SetNeedsFocusRect( bool needs )
2835{
2836 m_needsFocusRect = needs;
2837}
2838
2839bool wxWidgetImpl::NeedsFocusRect() const
2840{
2841 return m_needsFocusRect;
2842}
2843
f2f6030e
SC
2844void wxWidgetImpl::SetNeedsFrame( bool needs )
2845{
2846 m_needsFrame = needs;
2847}
2848
2849bool wxWidgetImpl::NeedsFrame() const
2850{
2851 return m_needsFrame;
2852}
2ab38025
SC
2853
2854void wxWidgetImpl::SetDrawingEnabled(bool WXUNUSED(enabled))
2855{
2856}