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