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