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