]> git.saurik.com Git - wxWidgets.git/blob - src/osx/window_osx.cpp
fix for GTK assert after r72696, a draw/expose_event signal may also be attached...
[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 if ( IsFrozen() )
1367 return;
1368
1369 GetPeer()->SetNeedsDisplay( rect ) ;
1370 }
1371
1372 void wxWindowMac::DoFreeze()
1373 {
1374 if ( GetPeer() && GetPeer()->IsOk() )
1375 GetPeer()->SetDrawingEnabled( false ) ;
1376 }
1377
1378 void wxWindowMac::DoThaw()
1379 {
1380 if ( GetPeer() && GetPeer()->IsOk() )
1381 GetPeer()->SetDrawingEnabled( true ) ;
1382 }
1383
1384 wxWindow *wxGetActiveWindow()
1385 {
1386 // actually this is a windows-only concept
1387 return NULL;
1388 }
1389
1390 // Coordinates relative to the window
1391 void wxWindowMac::WarpPointer(int x_pos, int y_pos)
1392 {
1393 #if wxOSX_USE_COCOA_OR_CARBON
1394 int x = x_pos;
1395 int y = y_pos;
1396 DoClientToScreen(&x, &y);
1397 CGPoint cgpoint = CGPointMake( x, y );
1398 CGWarpMouseCursorPosition( cgpoint );
1399
1400 // At least GTK sends a mouse moved event after WarpMouse
1401 wxMouseEvent event(wxEVT_MOTION);
1402 event.m_x = x_pos;
1403 event.m_y = y_pos;
1404 wxMouseState mState = ::wxGetMouseState();
1405
1406 event.m_altDown = mState.AltDown();
1407 event.m_controlDown = mState.ControlDown();
1408 event.m_leftDown = mState.LeftIsDown();
1409 event.m_middleDown = mState.MiddleIsDown();
1410 event.m_rightDown = mState.RightIsDown();
1411 event.m_metaDown = mState.MetaDown();
1412 event.m_shiftDown = mState.ShiftDown();
1413 event.SetId(GetId());
1414 event.SetEventObject(this);
1415 GetEventHandler()->ProcessEvent(event);
1416 #endif
1417 }
1418
1419 int wxWindowMac::GetScrollPos(int orient) const
1420 {
1421 #if wxUSE_SCROLLBAR
1422 if ( orient == wxHORIZONTAL )
1423 {
1424 if ( m_hScrollBar )
1425 return m_hScrollBar->GetThumbPosition() ;
1426 }
1427 else
1428 {
1429 if ( m_vScrollBar )
1430 return m_vScrollBar->GetThumbPosition() ;
1431 }
1432 #endif
1433 return 0;
1434 }
1435
1436 // This now returns the whole range, not just the number
1437 // of positions that we can scroll.
1438 int wxWindowMac::GetScrollRange(int orient) const
1439 {
1440 #if wxUSE_SCROLLBAR
1441 if ( orient == wxHORIZONTAL )
1442 {
1443 if ( m_hScrollBar )
1444 return m_hScrollBar->GetRange() ;
1445 }
1446 else
1447 {
1448 if ( m_vScrollBar )
1449 return m_vScrollBar->GetRange() ;
1450 }
1451 #endif
1452 return 0;
1453 }
1454
1455 int wxWindowMac::GetScrollThumb(int orient) const
1456 {
1457 #if wxUSE_SCROLLBAR
1458 if ( orient == wxHORIZONTAL )
1459 {
1460 if ( m_hScrollBar )
1461 return m_hScrollBar->GetThumbSize() ;
1462 }
1463 else
1464 {
1465 if ( m_vScrollBar )
1466 return m_vScrollBar->GetThumbSize() ;
1467 }
1468 #endif
1469 return 0;
1470 }
1471
1472 void wxWindowMac::SetScrollPos(int orient, int pos, bool WXUNUSED(refresh))
1473 {
1474 #if wxUSE_SCROLLBAR
1475 if ( orient == wxHORIZONTAL )
1476 {
1477 if ( m_hScrollBar )
1478 m_hScrollBar->SetThumbPosition( pos ) ;
1479 }
1480 else
1481 {
1482 if ( m_vScrollBar )
1483 m_vScrollBar->SetThumbPosition( pos ) ;
1484 }
1485 #endif
1486 }
1487
1488 void
1489 wxWindowMac::AlwaysShowScrollbars(bool hflag, bool vflag)
1490 {
1491 bool needVisibilityUpdate = false;
1492
1493 if ( m_hScrollBarAlwaysShown != hflag )
1494 {
1495 m_hScrollBarAlwaysShown = hflag;
1496 needVisibilityUpdate = true;
1497 }
1498
1499 if ( m_vScrollBarAlwaysShown != vflag )
1500 {
1501 m_vScrollBarAlwaysShown = vflag;
1502 needVisibilityUpdate = true;
1503 }
1504
1505 if ( needVisibilityUpdate )
1506 DoUpdateScrollbarVisibility();
1507 }
1508
1509 //
1510 // we draw borders and grow boxes, are already set up and clipped in the current port / cgContextRef
1511 // our own window origin is at leftOrigin/rightOrigin
1512 //
1513
1514 void wxWindowMac::MacPaintGrowBox()
1515 {
1516 if ( IsTopLevel() )
1517 return ;
1518
1519 #if wxUSE_SCROLLBAR
1520 if ( MacHasScrollBarCorner() )
1521 {
1522 #if 0
1523 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef() ;
1524 wxASSERT( cgContext ) ;
1525
1526 int tx,ty,tw,th;
1527
1528 GetPeer()->GetSize( tw, th );
1529 GetPeer()->GetPosition( tx, ty );
1530
1531 Rect rect = { ty,tx, ty+th, tx+tw };
1532
1533
1534 int size = m_hScrollBar ? m_hScrollBar->GetSize().y : ( m_vScrollBar ? m_vScrollBar->GetSize().x : MAC_SCROLLBAR_SIZE ) ;
1535 CGRect cgrect = CGRectMake( rect.right - size , rect.bottom - size , size , size ) ;
1536 CGContextSaveGState( cgContext );
1537
1538 if ( m_backgroundColour.IsOk() )
1539 {
1540 CGContextSetFillColorWithColor( cgContext, m_backgroundColour.GetCGColor() );
1541 }
1542 else
1543 {
1544 CGContextSetRGBFillColor( cgContext, (CGFloat) 1.0, (CGFloat)1.0 ,(CGFloat) 1.0 , (CGFloat)1.0 );
1545 }
1546 CGContextFillRect( cgContext, cgrect );
1547 CGContextRestoreGState( cgContext );
1548 #else
1549 if (m_growBox)
1550 {
1551 if ( m_backgroundColour.IsOk() )
1552 m_growBox->SetBackgroundColour(m_backgroundColour);
1553 else
1554 m_growBox->SetBackgroundColour(*wxWHITE);
1555 }
1556 #endif
1557 }
1558
1559 #endif
1560 }
1561
1562 void wxWindowMac::MacPaintBorders( int WXUNUSED(leftOrigin) , int WXUNUSED(rightOrigin) )
1563 {
1564 if ( IsTopLevel() )
1565 return ;
1566
1567 bool hasFocus = GetPeer()->NeedsFocusRect() && HasFocus();
1568
1569 // back to the surrounding frame rectangle
1570 int tx,ty,tw,th;
1571
1572 GetPeer()->GetSize( tw, th );
1573 GetPeer()->GetPosition( tx, ty );
1574
1575 #if wxOSX_USE_COCOA_OR_CARBON
1576
1577 {
1578 CGRect cgrect = CGRectMake( tx-1 , ty-1 , tw+2 ,
1579 th+2 ) ;
1580
1581 CGContextRef cgContext = (CGContextRef) GetParent()->MacGetCGContextRef() ;
1582 wxASSERT( cgContext ) ;
1583
1584 if ( GetPeer()->NeedsFrame() )
1585 {
1586 HIThemeFrameDrawInfo info ;
1587 memset( &info, 0 , sizeof(info) ) ;
1588
1589 info.version = 0 ;
1590 info.kind = 0 ;
1591 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive ;
1592 info.isFocused = hasFocus ;
1593
1594 if ( HasFlag(wxRAISED_BORDER) || HasFlag(wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
1595 {
1596 info.kind = kHIThemeFrameTextFieldSquare ;
1597 HIThemeDrawFrame( &cgrect , &info , cgContext , kHIThemeOrientationNormal ) ;
1598 }
1599 else if ( HasFlag(wxSIMPLE_BORDER) )
1600 {
1601 info.kind = kHIThemeFrameListBox ;
1602 HIThemeDrawFrame( &cgrect , &info , cgContext , kHIThemeOrientationNormal ) ;
1603 }
1604 }
1605
1606 if ( hasFocus )
1607 {
1608 HIThemeDrawFocusRect( &cgrect , true , cgContext , kHIThemeOrientationNormal ) ;
1609 }
1610 }
1611 #endif // wxOSX_USE_COCOA_OR_CARBON
1612 }
1613
1614 void wxWindowMac::RemoveChild( wxWindowBase *child )
1615 {
1616 #if wxUSE_SCROLLBAR
1617 if ( child == m_hScrollBar )
1618 m_hScrollBar = NULL ;
1619 if ( child == m_vScrollBar )
1620 m_vScrollBar = NULL ;
1621 if ( child == m_growBox )
1622 m_growBox = NULL ;
1623 #endif
1624 wxWindowBase::RemoveChild( child ) ;
1625 }
1626
1627 void wxWindowMac::DoUpdateScrollbarVisibility()
1628 {
1629 #if wxUSE_SCROLLBAR
1630 bool triggerSizeEvent = false;
1631
1632 if ( m_hScrollBar )
1633 {
1634 bool showHScrollBar = m_hScrollBarAlwaysShown || m_hScrollBar->IsNeeded();
1635
1636 if ( m_hScrollBar->IsShown() != showHScrollBar )
1637 {
1638 m_hScrollBar->Show( showHScrollBar );
1639 triggerSizeEvent = true;
1640 }
1641 }
1642
1643 if ( m_vScrollBar)
1644 {
1645 bool showVScrollBar = m_vScrollBarAlwaysShown || m_vScrollBar->IsNeeded();
1646
1647 if ( m_vScrollBar->IsShown() != showVScrollBar )
1648 {
1649 m_vScrollBar->Show( showVScrollBar ) ;
1650 triggerSizeEvent = true;
1651 }
1652 }
1653
1654 MacRepositionScrollBars() ;
1655 if ( triggerSizeEvent )
1656 {
1657 MacOnInternalSize();
1658 wxSizeEvent event(GetSize(), m_windowId);
1659 event.SetEventObject(this);
1660 HandleWindowEvent(event);
1661 }
1662 #endif
1663 }
1664
1665 // New function that will replace some of the above.
1666 void wxWindowMac::SetScrollbar(int orient, int pos, int thumb,
1667 int range, bool refresh)
1668 {
1669 #if wxUSE_SCROLLBAR
1670 // Updating scrollbars when window is being deleted is useless and
1671 // currently results in asserts in client-to-screen coordinates conversion
1672 // code which is used by DoUpdateScrollbarVisibility() so just skip it.
1673 if ( m_isBeingDeleted )
1674 return;
1675
1676 if ( orient == wxHORIZONTAL && m_hScrollBar )
1677 m_hScrollBar->SetScrollbar(pos, thumb, range, thumb, refresh);
1678 else if ( orient == wxVERTICAL && m_vScrollBar )
1679 m_vScrollBar->SetScrollbar(pos, thumb, range, thumb, refresh);
1680
1681 DoUpdateScrollbarVisibility();
1682 #endif
1683 }
1684
1685 // Does a physical scroll
1686 void wxWindowMac::ScrollWindow(int dx, int dy, const wxRect *rect)
1687 {
1688 if ( dx == 0 && dy == 0 )
1689 return ;
1690
1691 int width , height ;
1692 GetClientSize( &width , &height ) ;
1693
1694 {
1695 wxRect scrollrect( MacGetLeftBorderSize() , MacGetTopBorderSize() , width , height ) ;
1696 if ( rect )
1697 scrollrect.Intersect( *rect ) ;
1698 // as the native control might be not a 0/0 wx window coordinates, we have to offset
1699 scrollrect.Offset( -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
1700
1701 GetPeer()->ScrollRect( &scrollrect, dx, dy );
1702 }
1703
1704 wxWindowMac *child;
1705 int x, y, w, h;
1706 for (wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); node; node = node->GetNext())
1707 {
1708 child = node->GetData();
1709 if (child == NULL)
1710 continue;
1711
1712 if (child->IsTopLevel())
1713 continue;
1714
1715 if ( !IsClientAreaChild(child) )
1716 continue;
1717
1718 child->GetPosition( &x, &y );
1719 child->GetSize( &w, &h );
1720 if (rect)
1721 {
1722 wxRect rc( x, y, w, h );
1723 if (rect->Intersects( rc ))
1724 child->SetSize( x + dx, y + dy, w, h, wxSIZE_AUTO|wxSIZE_ALLOW_MINUS_ONE );
1725 }
1726 else
1727 {
1728 child->SetSize( x + dx, y + dy, w, h, wxSIZE_AUTO|wxSIZE_ALLOW_MINUS_ONE );
1729 }
1730 }
1731 }
1732
1733 void wxWindowMac::MacOnScroll( wxScrollEvent &event )
1734 {
1735 #if wxUSE_SCROLLBAR
1736 if ( event.GetEventObject() == m_vScrollBar || event.GetEventObject() == m_hScrollBar )
1737 {
1738 wxScrollWinEvent wevent;
1739 wevent.SetPosition(event.GetPosition());
1740 wevent.SetOrientation(event.GetOrientation());
1741 wevent.SetEventObject(this);
1742
1743 if (event.GetEventType() == wxEVT_SCROLL_TOP)
1744 wevent.SetEventType( wxEVT_SCROLLWIN_TOP );
1745 else if (event.GetEventType() == wxEVT_SCROLL_BOTTOM)
1746 wevent.SetEventType( wxEVT_SCROLLWIN_BOTTOM );
1747 else if (event.GetEventType() == wxEVT_SCROLL_LINEUP)
1748 wevent.SetEventType( wxEVT_SCROLLWIN_LINEUP );
1749 else if (event.GetEventType() == wxEVT_SCROLL_LINEDOWN)
1750 wevent.SetEventType( wxEVT_SCROLLWIN_LINEDOWN );
1751 else if (event.GetEventType() == wxEVT_SCROLL_PAGEUP)
1752 wevent.SetEventType( wxEVT_SCROLLWIN_PAGEUP );
1753 else if (event.GetEventType() == wxEVT_SCROLL_PAGEDOWN)
1754 wevent.SetEventType( wxEVT_SCROLLWIN_PAGEDOWN );
1755 else if (event.GetEventType() == wxEVT_SCROLL_THUMBTRACK)
1756 wevent.SetEventType( wxEVT_SCROLLWIN_THUMBTRACK );
1757 else if (event.GetEventType() == wxEVT_SCROLL_THUMBRELEASE)
1758 wevent.SetEventType( wxEVT_SCROLLWIN_THUMBRELEASE );
1759
1760 HandleWindowEvent(wevent);
1761 }
1762 #endif
1763 }
1764
1765 wxWindow *wxWindowBase::DoFindFocus()
1766 {
1767 return wxFindWindowFromWXWidget(wxWidgetImpl::FindFocus());
1768 }
1769
1770 // Raise the window to the top of the Z order
1771 void wxWindowMac::Raise()
1772 {
1773 GetPeer()->Raise();
1774 }
1775
1776 // Lower the window to the bottom of the Z order
1777 void wxWindowMac::Lower()
1778 {
1779 GetPeer()->Lower();
1780 }
1781
1782 // static wxWindow *gs_lastWhich = NULL;
1783
1784 bool wxWindowMac::MacSetupCursor( const wxPoint& pt )
1785 {
1786 // first trigger a set cursor event
1787
1788 wxPoint clientorigin = GetClientAreaOrigin() ;
1789 wxSize clientsize = GetClientSize() ;
1790 wxCursor cursor ;
1791 if ( wxRect2DInt( clientorigin.x , clientorigin.y , clientsize.x , clientsize.y ).Contains( wxPoint2DInt( pt ) ) )
1792 {
1793 wxSetCursorEvent event( pt.x , pt.y );
1794
1795 bool processedEvtSetCursor = HandleWindowEvent(event);
1796 if ( processedEvtSetCursor && event.HasCursor() )
1797 {
1798 cursor = event.GetCursor() ;
1799 }
1800 else
1801 {
1802 // the test for processedEvtSetCursor is here to prevent using m_cursor
1803 // if the user code caught EVT_SET_CURSOR() and returned nothing from
1804 // it - this is a way to say that our cursor shouldn't be used for this
1805 // point
1806 if ( !processedEvtSetCursor && m_cursor.IsOk() )
1807 cursor = m_cursor ;
1808
1809 if ( !wxIsBusy() && !GetParent() )
1810 cursor = *wxSTANDARD_CURSOR ;
1811 }
1812
1813 if ( cursor.IsOk() )
1814 cursor.MacInstall() ;
1815 }
1816
1817 return cursor.IsOk() ;
1818 }
1819
1820 wxString wxWindowMac::MacGetToolTipString( wxPoint &WXUNUSED(pt) )
1821 {
1822 #if wxUSE_TOOLTIPS
1823 if ( m_tooltip )
1824 return m_tooltip->GetTip() ;
1825 #endif
1826
1827 return wxEmptyString ;
1828 }
1829
1830 void wxWindowMac::ClearBackground()
1831 {
1832 Refresh() ;
1833 Update() ;
1834 }
1835
1836 void wxWindowMac::Update()
1837 {
1838 wxNonOwnedWindow* top = MacGetTopLevelWindow();
1839 if (top)
1840 top->Update() ;
1841 }
1842
1843 wxNonOwnedWindow* wxWindowMac::MacGetTopLevelWindow() const
1844 {
1845 wxWindowMac *iter = (wxWindowMac*)this ;
1846
1847 while ( iter )
1848 {
1849 if ( iter->IsTopLevel() )
1850 {
1851 wxTopLevelWindow* toplevel = wxDynamicCast(iter,wxTopLevelWindow);
1852 if ( toplevel )
1853 return toplevel;
1854 #if wxUSE_POPUPWIN
1855 wxPopupWindow* popupwin = wxDynamicCast(iter,wxPopupWindow);
1856 if ( popupwin )
1857 return popupwin;
1858 #endif
1859 }
1860 iter = iter->GetParent() ;
1861 }
1862
1863 return NULL ;
1864 }
1865
1866 const wxRect& wxWindowMac::MacGetClippedClientRect() const
1867 {
1868 MacUpdateClippedRects() ;
1869
1870 return m_cachedClippedClientRect ;
1871 }
1872
1873 const wxRect& wxWindowMac::MacGetClippedRect() const
1874 {
1875 MacUpdateClippedRects() ;
1876
1877 return m_cachedClippedRect ;
1878 }
1879
1880 const wxRect&wxWindowMac:: MacGetClippedRectWithOuterStructure() const
1881 {
1882 MacUpdateClippedRects() ;
1883
1884 return m_cachedClippedRectWithOuterStructure ;
1885 }
1886
1887 const wxRegion& wxWindowMac::MacGetVisibleRegion( bool includeOuterStructures )
1888 {
1889 static wxRegion emptyrgn ;
1890
1891 if ( !m_isBeingDeleted && IsShownOnScreen() )
1892 {
1893 MacUpdateClippedRects() ;
1894 if ( includeOuterStructures )
1895 return m_cachedClippedRegionWithOuterStructure ;
1896 else
1897 return m_cachedClippedRegion ;
1898 }
1899 else
1900 {
1901 return emptyrgn ;
1902 }
1903 }
1904
1905 void wxWindowMac::MacUpdateClippedRects() const
1906 {
1907 #if wxOSX_USE_CARBON
1908 if ( m_cachedClippedRectValid )
1909 return ;
1910
1911 // includeOuterStructures is true if we try to draw somthing like a focus ring etc.
1912 // also a window dc uses this, in this case we only clip in the hierarchy for hard
1913 // borders like a scrollwindow, splitter etc otherwise we end up in a paranoia having
1914 // to add focus borders everywhere
1915
1916 Rect rIncludingOuterStructures ;
1917
1918 int tx,ty,tw,th;
1919
1920 GetPeer()->GetSize( tw, th );
1921 GetPeer()->GetPosition( tx, ty );
1922
1923 Rect r = { ty,tx, ty+th, tx+tw };
1924
1925 r.left -= MacGetLeftBorderSize() ;
1926 r.top -= MacGetTopBorderSize() ;
1927 r.bottom += MacGetBottomBorderSize() ;
1928 r.right += MacGetRightBorderSize() ;
1929
1930 r.right -= r.left ;
1931 r.bottom -= r.top ;
1932 r.left = 0 ;
1933 r.top = 0 ;
1934
1935 rIncludingOuterStructures = r ;
1936 InsetRect( &rIncludingOuterStructures , -4 , -4 ) ;
1937
1938 wxRect cl = GetClientRect() ;
1939 Rect rClient = { cl.y , cl.x , cl.y + cl.height , cl.x + cl.width } ;
1940
1941 int x , y ;
1942 wxSize size ;
1943 const wxWindow* child = (wxWindow*) this ;
1944 const wxWindow* parent = NULL ;
1945
1946 while ( !child->IsTopLevel() && ( parent = child->GetParent() ) != NULL )
1947 {
1948 if ( parent->MacIsChildOfClientArea(child) )
1949 {
1950 size = parent->GetClientSize() ;
1951 wxPoint origin = parent->GetClientAreaOrigin() ;
1952 x = origin.x ;
1953 y = origin.y ;
1954 }
1955 else
1956 {
1957 // this will be true for scrollbars, toolbars etc.
1958 size = parent->GetSize() ;
1959 y = parent->MacGetTopBorderSize() ;
1960 x = parent->MacGetLeftBorderSize() ;
1961 size.x -= parent->MacGetLeftBorderSize() + parent->MacGetRightBorderSize() ;
1962 size.y -= parent->MacGetTopBorderSize() + parent->MacGetBottomBorderSize() ;
1963 }
1964
1965 parent->MacWindowToRootWindow( &x, &y ) ;
1966 MacRootWindowToWindow( &x , &y ) ;
1967
1968 Rect rparent = { y , x , y + size.y , x + size.x } ;
1969
1970 // the wxwindow and client rects will always be clipped
1971 SectRect( &r , &rparent , &r ) ;
1972 SectRect( &rClient , &rparent , &rClient ) ;
1973
1974 // the structure only at 'hard' borders
1975 if ( parent->MacClipChildren() ||
1976 ( parent->GetParent() && parent->GetParent()->MacClipGrandChildren() ) )
1977 {
1978 SectRect( &rIncludingOuterStructures , &rparent , &rIncludingOuterStructures ) ;
1979 }
1980
1981 child = parent ;
1982 }
1983
1984 m_cachedClippedRect = wxRect( r.left , r.top , r.right - r.left , r.bottom - r.top ) ;
1985 m_cachedClippedClientRect = wxRect( rClient.left , rClient.top ,
1986 rClient.right - rClient.left , rClient.bottom - rClient.top ) ;
1987 m_cachedClippedRectWithOuterStructure = wxRect(
1988 rIncludingOuterStructures.left , rIncludingOuterStructures.top ,
1989 rIncludingOuterStructures.right - rIncludingOuterStructures.left ,
1990 rIncludingOuterStructures.bottom - rIncludingOuterStructures.top ) ;
1991
1992 m_cachedClippedRegionWithOuterStructure = wxRegion( m_cachedClippedRectWithOuterStructure ) ;
1993 m_cachedClippedRegion = wxRegion( m_cachedClippedRect ) ;
1994 m_cachedClippedClientRegion = wxRegion( m_cachedClippedClientRect ) ;
1995
1996 m_cachedClippedRectValid = true ;
1997 #endif
1998 }
1999
2000 /*
2001 This function must not change the updatergn !
2002 */
2003 bool wxWindowMac::MacDoRedraw( long time )
2004 {
2005 bool handled = false ;
2006
2007 wxRegion formerUpdateRgn = m_updateRegion;
2008 wxRegion clientUpdateRgn = formerUpdateRgn;
2009
2010 const wxRect clientRect = GetClientRect();
2011
2012 clientUpdateRgn.Intersect(clientRect);
2013
2014 // first send an erase event to the entire update area
2015 const wxBackgroundStyle bgStyle = GetBackgroundStyle();
2016 switch ( bgStyle )
2017 {
2018 case wxBG_STYLE_ERASE:
2019 case wxBG_STYLE_SYSTEM:
2020 case wxBG_STYLE_COLOUR:
2021 {
2022 // for the toplevel window this really is the entire area for
2023 // all the others only their client area, otherwise they might
2024 // be drawing with full alpha and eg put blue into the grow-box
2025 // area of a scrolled window (scroll sample)
2026 wxWindowDC dc(this);
2027 if ( IsTopLevel() )
2028 dc.SetDeviceClippingRegion(formerUpdateRgn);
2029 else
2030 dc.SetDeviceClippingRegion(clientUpdateRgn);
2031
2032 if ( bgStyle == wxBG_STYLE_ERASE )
2033 {
2034 wxEraseEvent eevent( GetId(), &dc );
2035 eevent.SetEventObject( this );
2036 if ( ProcessWindowEvent( eevent ) )
2037 break;
2038 }
2039
2040 if ( UseBgCol() )
2041 {
2042 dc.SetBackground(GetBackgroundColour());
2043 dc.Clear();
2044 }
2045 }
2046 break;
2047
2048 case wxBG_STYLE_PAINT:
2049 case wxBG_STYLE_TRANSPARENT:
2050 // nothing to do, user-defined EVT_PAINT handler will overwrite the
2051 // entire window client area
2052 break;
2053
2054 default:
2055 wxFAIL_MSG( "unsupported background style" );
2056 }
2057
2058 // as this is a full window, shouldn't be necessary anymore
2059 // MacPaintGrowBox();
2060
2061 // calculate a client-origin version of the update rgn and set
2062 // m_updateRegion to that
2063 clientUpdateRgn.Offset(-clientRect.GetPosition());
2064 m_updateRegion = clientUpdateRgn ;
2065
2066 if ( !m_updateRegion.Empty() )
2067 {
2068 // paint the window itself
2069
2070 wxPaintEvent event(GetId());
2071 event.SetTimestamp(time);
2072 event.SetEventObject(this);
2073 handled = HandleWindowEvent(event);
2074 }
2075
2076 m_updateRegion = formerUpdateRgn;
2077
2078 wxNonOwnedWindow* top = MacGetTopLevelWindow();
2079 if (top)
2080 top->WindowWasPainted() ;
2081
2082 return handled;
2083 }
2084
2085 void wxWindowMac::MacPaintChildrenBorders()
2086 {
2087 // now we cannot rely on having its borders drawn by a window itself, as it does not
2088 // get the updateRgn wide enough to always do so, so we do it from the parent
2089 // this would also be the place to draw any custom backgrounds for native controls
2090 // in Composited windowing
2091 wxPoint clientOrigin = GetClientAreaOrigin() ;
2092
2093 wxWindowMac *child;
2094 int x, y, w, h;
2095 for (wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); node; node = node->GetNext())
2096 {
2097 child = node->GetData();
2098 if (child == NULL)
2099 continue;
2100 #if wxUSE_SCROLLBAR
2101 if (child == m_vScrollBar)
2102 continue;
2103 if (child == m_hScrollBar)
2104 continue;
2105 if (child == m_growBox)
2106 continue;
2107 #endif
2108 if (child->IsTopLevel())
2109 continue;
2110 if (!child->IsShown())
2111 continue;
2112
2113 // only draw those in the update region (add a safety margin of 10 pixels for shadow effects
2114
2115 child->GetPosition( &x, &y );
2116 child->GetSize( &w, &h );
2117
2118 if ( m_updateRegion.Contains(clientOrigin.x+x-10, clientOrigin.y+y-10, w+20, h+20) )
2119 {
2120 // paint custom borders
2121 wxNcPaintEvent eventNc( child->GetId() );
2122 eventNc.SetEventObject( child );
2123 if ( !child->HandleWindowEvent( eventNc ) )
2124 {
2125 child->MacPaintBorders(0, 0) ;
2126 }
2127 }
2128 }
2129 }
2130
2131
2132 WXWindow wxWindowMac::MacGetTopLevelWindowRef() const
2133 {
2134 wxNonOwnedWindow* tlw = MacGetTopLevelWindow();
2135 return tlw ? tlw->GetWXWindow() : NULL ;
2136 }
2137
2138 bool wxWindowMac::MacHasScrollBarCorner() const
2139 {
2140 #if wxUSE_SCROLLBAR
2141 /* Returns whether the scroll bars in a wxScrolledWindow should be
2142 * shortened. Scroll bars should be shortened if either:
2143 *
2144 * - both scroll bars are visible, or
2145 *
2146 * - there is a resize box in the parent frame's corner and this
2147 * window shares the bottom and right edge with the parent
2148 * frame.
2149 */
2150
2151 if ( m_hScrollBar == NULL && m_vScrollBar == NULL )
2152 return false;
2153
2154 if ( ( m_hScrollBar && m_hScrollBar->IsShown() )
2155 && ( m_vScrollBar && m_vScrollBar->IsShown() ) )
2156 {
2157 // Both scroll bars visible
2158 return true;
2159 }
2160 else
2161 {
2162 wxPoint thisWindowBottomRight = GetScreenRect().GetBottomRight();
2163
2164 for ( const wxWindow *win = (wxWindow*)this; win; win = win->GetParent() )
2165 {
2166 const wxFrame *frame = wxDynamicCast( win, wxFrame ) ;
2167 if ( frame )
2168 {
2169 if ( frame->GetWindowStyleFlag() & wxRESIZE_BORDER )
2170 {
2171 // Parent frame has resize handle
2172 wxPoint frameBottomRight = frame->GetScreenRect().GetBottomRight();
2173
2174 // Note: allow for some wiggle room here as wxMac's
2175 // window rect calculations seem to be imprecise
2176 if ( abs( thisWindowBottomRight.x - frameBottomRight.x ) <= 2
2177 && abs( thisWindowBottomRight.y - frameBottomRight.y ) <= 2 )
2178 {
2179 // Parent frame has resize handle and shares
2180 // right bottom corner
2181 return true ;
2182 }
2183 else
2184 {
2185 // Parent frame has resize handle but doesn't
2186 // share right bottom corner
2187 return false ;
2188 }
2189 }
2190 else
2191 {
2192 // Parent frame doesn't have resize handle
2193 return false ;
2194 }
2195 }
2196 }
2197
2198 // No parent frame found
2199 return false ;
2200 }
2201 #else
2202 return false;
2203 #endif
2204 }
2205
2206 void wxWindowMac::MacCreateScrollBars( long style )
2207 {
2208 #if wxUSE_SCROLLBAR
2209 wxASSERT_MSG( m_vScrollBar == NULL && m_hScrollBar == NULL , wxT("attempt to create window twice") ) ;
2210
2211 if ( style & ( wxVSCROLL | wxHSCROLL ) )
2212 {
2213 int scrlsize = MAC_SCROLLBAR_SIZE ;
2214 if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL || GetWindowVariant() == wxWINDOW_VARIANT_MINI )
2215 {
2216 scrlsize = MAC_SMALL_SCROLLBAR_SIZE ;
2217 }
2218
2219 int adjust = MacHasScrollBarCorner() ? scrlsize - 1: 0 ;
2220 int width, height ;
2221 GetClientSize( &width , &height ) ;
2222
2223 wxPoint vPoint(width - scrlsize, 0) ;
2224 wxSize vSize(scrlsize, height - adjust) ;
2225 wxPoint hPoint(0, height - scrlsize) ;
2226 wxSize hSize(width - adjust, scrlsize) ;
2227
2228 // we have to set the min size to a smaller value, otherwise they cannot get smaller (InitialSize sets MinSize)
2229 if ( style & wxVSCROLL )
2230 {
2231 m_vScrollBar = new wxScrollBar((wxWindow*)this, wxID_ANY, vPoint, vSize , wxVERTICAL);
2232 m_vScrollBar->SetMinSize( wxDefaultSize );
2233 }
2234
2235 if ( style & wxHSCROLL )
2236 {
2237 m_hScrollBar = new wxScrollBar((wxWindow*)this, wxID_ANY, hPoint, hSize , wxHORIZONTAL);
2238 m_hScrollBar->SetMinSize( wxDefaultSize );
2239 }
2240
2241 wxPoint gPoint(width - scrlsize, height - scrlsize);
2242 wxSize gSize(scrlsize, scrlsize);
2243 m_growBox = new wxBlindPlateWindow((wxWindow *)this, wxID_ANY, gPoint, gSize, 0);
2244 }
2245
2246 // because the create does not take into account the client area origin
2247 // we might have a real position shift
2248 MacRepositionScrollBars() ;
2249 #endif
2250 }
2251
2252 bool wxWindowMac::MacIsChildOfClientArea( const wxWindow* child ) const
2253 {
2254 bool result = ((child == NULL)
2255 #if wxUSE_SCROLLBAR
2256 || ((child != m_hScrollBar) && (child != m_vScrollBar) && (child != m_growBox))
2257 #endif
2258 );
2259
2260 return result ;
2261 }
2262
2263 void wxWindowMac::MacRepositionScrollBars()
2264 {
2265 #if wxUSE_SCROLLBAR
2266 if ( !m_hScrollBar && !m_vScrollBar )
2267 return ;
2268
2269 int scrlsize = m_hScrollBar ? m_hScrollBar->GetSize().y : ( m_vScrollBar ? m_vScrollBar->GetSize().x : MAC_SCROLLBAR_SIZE ) ;
2270 int adjust = MacHasScrollBarCorner() ? scrlsize - 1 : 0 ;
2271
2272 // get real client area
2273 int width, height ;
2274 GetSize( &width , &height );
2275
2276 width -= MacGetLeftBorderSize() + MacGetRightBorderSize();
2277 height -= MacGetTopBorderSize() + MacGetBottomBorderSize();
2278
2279 wxPoint vPoint( width - scrlsize, 0 ) ;
2280 wxSize vSize( scrlsize, height - adjust ) ;
2281 wxPoint hPoint( 0 , height - scrlsize ) ;
2282 wxSize hSize( width - adjust, scrlsize ) ;
2283
2284 if ( m_vScrollBar )
2285 m_vScrollBar->SetSize( vPoint.x , vPoint.y, vSize.x, vSize.y , wxSIZE_ALLOW_MINUS_ONE );
2286 if ( m_hScrollBar )
2287 m_hScrollBar->SetSize( hPoint.x , hPoint.y, hSize.x, hSize.y, wxSIZE_ALLOW_MINUS_ONE );
2288 if ( m_growBox )
2289 {
2290 if ( MacHasScrollBarCorner() )
2291 {
2292 m_growBox->SetSize( width - scrlsize, height - scrlsize, wxDefaultCoord, wxDefaultCoord, wxSIZE_USE_EXISTING );
2293 if ( !m_growBox->IsShown() )
2294 m_growBox->Show();
2295 }
2296 else
2297 {
2298 if ( m_growBox->IsShown() )
2299 m_growBox->Hide();
2300 }
2301 }
2302 #endif
2303 }
2304
2305 bool wxWindowMac::AcceptsFocus() const
2306 {
2307 if ( GetPeer() == NULL || GetPeer()->IsUserPane() )
2308 return wxWindowBase::AcceptsFocus();
2309 else
2310 return GetPeer()->CanFocus();
2311 }
2312
2313 void wxWindowMac::MacSuperChangedPosition()
2314 {
2315 // only window-absolute structures have to be moved i.e. controls
2316
2317 m_cachedClippedRectValid = false ;
2318
2319 wxWindowMac *child;
2320 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2321 while ( node )
2322 {
2323 child = node->GetData();
2324 child->MacSuperChangedPosition() ;
2325
2326 node = node->GetNext();
2327 }
2328 }
2329
2330 void wxWindowMac::MacTopLevelWindowChangedPosition()
2331 {
2332 // only screen-absolute structures have to be moved i.e. glcanvas
2333
2334 wxWindowMac *child;
2335 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2336 while ( node )
2337 {
2338 child = node->GetData();
2339 child->MacTopLevelWindowChangedPosition() ;
2340
2341 node = node->GetNext();
2342 }
2343 }
2344
2345 long wxWindowMac::MacGetWXBorderSize() const
2346 {
2347 if ( IsTopLevel() )
2348 return 0 ;
2349
2350 SInt32 border = 0 ;
2351
2352 if ( GetPeer() && GetPeer()->NeedsFrame() )
2353 {
2354 if (HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER))
2355 {
2356 #if wxOSX_USE_COCOA_OR_CARBON
2357 // this metric is only the 'outset' outside the simple frame rect
2358 GetThemeMetric( kThemeMetricEditTextFrameOutset , &border ) ;
2359 border += 1;
2360 #else
2361 border += 2;
2362 #endif
2363 }
2364 else if (HasFlag(wxSIMPLE_BORDER))
2365 {
2366 #if wxOSX_USE_COCOA_OR_CARBON
2367 // this metric is only the 'outset' outside the simple frame rect
2368 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2369 border += 1;
2370 #else
2371 border += 1;
2372 #endif
2373 }
2374 }
2375
2376 return border ;
2377 }
2378
2379 long wxWindowMac::MacGetLeftBorderSize() const
2380 {
2381 // the wx borders are all symmetric in mac themes
2382 long border = MacGetWXBorderSize() ;
2383
2384 if ( GetPeer() )
2385 {
2386 int left, top, right, bottom;
2387 GetPeer()->GetLayoutInset( left, top, right, bottom );
2388 border -= left;
2389 }
2390
2391 return border;
2392 }
2393
2394
2395 long wxWindowMac::MacGetRightBorderSize() const
2396 {
2397 // the wx borders are all symmetric in mac themes
2398 long border = MacGetWXBorderSize() ;
2399
2400 if ( GetPeer() )
2401 {
2402 int left, top, right, bottom;
2403 GetPeer()->GetLayoutInset( left, top, right, bottom );
2404 border -= right;
2405 }
2406
2407 return border;
2408 }
2409
2410 long wxWindowMac::MacGetTopBorderSize() const
2411 {
2412 // the wx borders are all symmetric in mac themes
2413 long border = MacGetWXBorderSize() ;
2414
2415 if ( GetPeer() )
2416 {
2417 int left, top, right, bottom;
2418 GetPeer()->GetLayoutInset( left, top, right, bottom );
2419 border -= top;
2420 }
2421
2422 return border;
2423 }
2424
2425 long wxWindowMac::MacGetBottomBorderSize() const
2426 {
2427 // the wx borders are all symmetric in mac themes
2428 long border = MacGetWXBorderSize() ;
2429
2430 if ( GetPeer() )
2431 {
2432 int left, top, right, bottom;
2433 GetPeer()->GetLayoutInset( left, top, right, bottom );
2434 border -= bottom;
2435 }
2436
2437 return border;
2438 }
2439
2440 long wxWindowMac::MacRemoveBordersFromStyle( long style )
2441 {
2442 return style & ~wxBORDER_MASK ;
2443 }
2444
2445 // Find the wxWindowMac at the current mouse position, returning the mouse
2446 // position.
2447 wxWindow * wxFindWindowAtPointer( wxPoint& pt )
2448 {
2449 pt = wxGetMousePosition();
2450 wxWindowMac* found = wxFindWindowAtPoint(pt);
2451
2452 return (wxWindow*) found;
2453 }
2454
2455 // Get the current mouse position.
2456 wxPoint wxGetMousePosition()
2457 {
2458 int x, y;
2459
2460 wxGetMousePosition( &x, &y );
2461
2462 return wxPoint(x, y);
2463 }
2464
2465 void wxWindowMac::OnMouseEvent( wxMouseEvent &event )
2466 {
2467 if ( event.GetEventType() == wxEVT_RIGHT_DOWN )
2468 {
2469 // copied from wxGTK : CS
2470 // VZ: shouldn't we move this to base class then?
2471
2472 // generate a "context menu" event: this is similar to wxEVT_RIGHT_DOWN
2473 // except that:
2474 //
2475 // (a) it's a command event and so is propagated to the parent
2476 // (b) under MSW it can be generated from kbd too
2477 // (c) it uses screen coords (because of (a))
2478 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
2479 this->GetId(),
2480 this->ClientToScreen(event.GetPosition()));
2481 evtCtx.SetEventObject(this);
2482 if ( ! HandleWindowEvent(evtCtx) )
2483 event.Skip() ;
2484 }
2485 else
2486 {
2487 event.Skip() ;
2488 }
2489 }
2490
2491 void wxWindowMac::TriggerScrollEvent( wxEventType WXUNUSED(scrollEvent) )
2492 {
2493 }
2494
2495 Rect wxMacGetBoundsForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
2496 {
2497 int x, y, w, h ;
2498
2499 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
2500 Rect bounds = { static_cast<short>(y), static_cast<short>(x), static_cast<short>(y + h), static_cast<short>(x + w) };
2501
2502 return bounds ;
2503 }
2504
2505 bool wxWindowMac::OSXHandleClicked( double WXUNUSED(timestampsec) )
2506 {
2507 return false;
2508 }
2509
2510 wxInt32 wxWindowMac::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF event )
2511 {
2512 #if wxOSX_USE_COCOA_OR_CARBON
2513 if ( OSXHandleClicked( GetEventTime((EventRef)event) ) )
2514 return noErr;
2515
2516 return eventNotHandledErr ;
2517 #else
2518 return 0;
2519 #endif
2520 }
2521
2522 bool wxWindowMac::Reparent(wxWindowBase *newParentBase)
2523 {
2524 wxWindowMac *newParent = (wxWindowMac *)newParentBase;
2525 if ( !wxWindowBase::Reparent(newParent) )
2526 return false;
2527
2528 GetPeer()->RemoveFromParent();
2529 GetPeer()->Embed( GetParent()->GetPeer() );
2530
2531 MacChildAdded();
2532 return true;
2533 }
2534
2535 bool wxWindowMac::SetTransparent(wxByte alpha)
2536 {
2537 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT);
2538
2539 if ( alpha != m_macAlpha )
2540 {
2541 m_macAlpha = alpha ;
2542 Refresh() ;
2543 }
2544 return true ;
2545 }
2546
2547
2548 bool wxWindowMac::CanSetTransparent()
2549 {
2550 return true ;
2551 }
2552
2553 wxByte wxWindowMac::GetTransparent() const
2554 {
2555 return m_macAlpha ;
2556 }
2557
2558 bool wxWindowMac::IsShownOnScreen() const
2559 {
2560 if ( GetPeer() && GetPeer()->IsOk() )
2561 {
2562 bool peerVis = GetPeer()->IsVisible();
2563 bool wxVis = wxWindowBase::IsShownOnScreen();
2564 if( peerVis != wxVis )
2565 {
2566 // CS : put a breakpoint here to investigate differences
2567 // between native an wx visibilities
2568 // the only place where I've encountered them until now
2569 // are the hiding/showing sequences where the vis-changed event is
2570 // first sent to the innermost control, while wx does things
2571 // from the outmost control
2572 wxVis = wxWindowBase::IsShownOnScreen();
2573 return wxVis;
2574 }
2575
2576 return GetPeer()->IsVisible();
2577 }
2578 return wxWindowBase::IsShownOnScreen();
2579 }
2580
2581 #if wxUSE_HOTKEY && wxOSX_USE_COCOA_OR_CARBON
2582
2583 OSStatus
2584 wxHotKeyHandler(EventHandlerCallRef WXUNUSED(nextHandler),
2585 EventRef event,
2586 void* WXUNUSED(userData))
2587 {
2588 EventHotKeyID hotKeyId;
2589
2590 GetEventParameter( event, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hotKeyId), NULL, &hotKeyId);
2591
2592 for ( unsigned i = 0; i < s_hotkeys.size(); ++i )
2593 {
2594 if ( s_hotkeys[i].keyId == static_cast<int>(hotKeyId.id) )
2595 {
2596 unsigned char charCode ;
2597 UInt32 keyCode ;
2598 UInt32 modifiers ;
2599 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
2600
2601 GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &charCode );
2602 GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
2603 GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers );
2604
2605 UInt32 keymessage = (keyCode << 8) + charCode;
2606
2607 wxKeyEvent wxevent(wxEVT_HOTKEY);
2608 wxevent.SetId(hotKeyId.id);
2609 wxTheApp->MacCreateKeyEvent( wxevent, s_hotkeys[i].window , keymessage ,
2610 modifiers , when , 0 ) ;
2611
2612 s_hotkeys[i].window->HandleWindowEvent(wxevent);
2613 }
2614 }
2615
2616 return noErr;
2617 }
2618
2619 bool wxWindowMac::RegisterHotKey(int hotkeyId, int modifiers, int keycode)
2620 {
2621 for ( unsigned i = 0; i < s_hotkeys.size(); ++i )
2622 {
2623 if ( s_hotkeys[i].keyId == hotkeyId )
2624 {
2625 wxLogLastError(wxT("hotkeyId already registered"));
2626
2627 return false;
2628 }
2629 }
2630
2631 static bool installed = false;
2632 if ( !installed )
2633 {
2634 EventTypeSpec eventType;
2635 eventType.eventClass=kEventClassKeyboard;
2636 eventType.eventKind=kEventHotKeyPressed;
2637
2638 InstallApplicationEventHandler(&wxHotKeyHandler, 1, &eventType, NULL, NULL);
2639 installed = true;
2640 }
2641
2642 UInt32 mac_modifiers=0;
2643 if ( modifiers & wxMOD_ALT )
2644 mac_modifiers |= optionKey;
2645 if ( modifiers & wxMOD_SHIFT )
2646 mac_modifiers |= shiftKey;
2647 if ( modifiers & wxMOD_RAW_CONTROL )
2648 mac_modifiers |= controlKey;
2649 if ( modifiers & wxMOD_CONTROL )
2650 mac_modifiers |= cmdKey;
2651
2652 EventHotKeyRef hotKeyRef;
2653 EventHotKeyID hotKeyIDmac;
2654
2655 hotKeyIDmac.signature = 'WXMC';
2656 hotKeyIDmac.id = hotkeyId;
2657
2658 if ( RegisterEventHotKey(wxCharCodeWXToOSX((wxKeyCode)keycode), mac_modifiers, hotKeyIDmac,
2659 GetApplicationEventTarget(), 0, &hotKeyRef) != noErr )
2660 {
2661 wxLogLastError(wxT("RegisterHotKey"));
2662
2663 return false;
2664 }
2665 else
2666 {
2667 wxHotKeyRec v;
2668 v.ref = hotKeyRef;
2669 v.keyId = hotkeyId;
2670 v.window = this;
2671
2672 s_hotkeys.push_back(v);
2673 }
2674
2675 return true;
2676 }
2677
2678 bool wxWindowMac::UnregisterHotKey(int hotkeyId)
2679 {
2680 for ( int i = ((int)s_hotkeys.size())-1; i>=0; -- i )
2681 {
2682 if ( s_hotkeys[i].keyId == hotkeyId )
2683 {
2684 EventHotKeyRef ref = s_hotkeys[i].ref;
2685 s_hotkeys.erase(s_hotkeys.begin() + i);
2686 if ( UnregisterEventHotKey(ref) != noErr )
2687 {
2688 wxLogLastError(wxT("UnregisterHotKey"));
2689
2690 return false;
2691 }
2692 else
2693 return true;
2694 }
2695 }
2696
2697 return false;
2698 }
2699
2700 #endif // wxUSE_HOTKEY
2701
2702 bool wxWindowMac::OSXHandleKeyEvent( wxKeyEvent& event )
2703 {
2704 bool handled = false;
2705
2706 // moved the ordinary key event sending AFTER the accel evaluation
2707
2708 #if wxUSE_ACCEL
2709 if ( !handled && event.GetEventType() == wxEVT_KEY_DOWN)
2710 {
2711 wxWindow *ancestor = this;
2712 while (ancestor)
2713 {
2714 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
2715 if (command != -1)
2716 {
2717 wxEvtHandler * const handler = ancestor->GetEventHandler();
2718
2719 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
2720 handled = handler->ProcessEvent( command_event );
2721
2722 if ( !handled )
2723 {
2724 // accelerators can also be used with buttons, try them too
2725 command_event.SetEventType(wxEVT_COMMAND_BUTTON_CLICKED);
2726 handled = handler->ProcessEvent( command_event );
2727 }
2728
2729 break;
2730 }
2731
2732 if (ancestor->IsTopLevel())
2733 break;
2734
2735 ancestor = ancestor->GetParent();
2736 }
2737 }
2738 #endif // wxUSE_ACCEL
2739
2740 if ( !handled )
2741 {
2742 handled = HandleWindowEvent( event ) ;
2743 if ( handled && event.GetSkipped() )
2744 handled = false ;
2745 }
2746
2747 return handled ;
2748 }
2749
2750 //
2751 // wxWidgetImpl
2752 //
2753
2754 WX_DECLARE_HASH_MAP(WXWidget, wxWidgetImpl*, wxPointerHash, wxPointerEqual, MacControlMap);
2755
2756 static MacControlMap wxWinMacControlList;
2757
2758 wxWindowMac *wxFindWindowFromWXWidget(WXWidget inControl )
2759 {
2760 wxWidgetImpl* impl = wxWidgetImpl::FindFromWXWidget( inControl );
2761 if ( impl )
2762 return impl->GetWXPeer();
2763
2764 return NULL;
2765 }
2766
2767 wxWidgetImpl *wxWidgetImpl::FindFromWXWidget(WXWidget inControl )
2768 {
2769 MacControlMap::iterator node = wxWinMacControlList.find(inControl);
2770
2771 return (node == wxWinMacControlList.end()) ? NULL : node->second;
2772 }
2773
2774 void wxWidgetImpl::Associate(WXWidget inControl, wxWidgetImpl *impl)
2775 {
2776 // adding NULL ControlRef is (first) surely a result of an error and
2777 // (secondly) breaks native event processing
2778 wxCHECK_RET( inControl != (WXWidget) NULL, wxT("attempt to add a NULL WXWidget to control map") );
2779
2780 wxWinMacControlList[inControl] = impl;
2781 }
2782
2783 void wxWidgetImpl::RemoveAssociations(wxWidgetImpl* impl)
2784 {
2785 // iterate over all the elements in the class
2786 // is the iterator stable ? as we might have two associations pointing to the same wxWindow
2787 // we should go on...
2788
2789 bool found = true ;
2790 while ( found )
2791 {
2792 found = false ;
2793 MacControlMap::iterator it;
2794 for ( it = wxWinMacControlList.begin(); it != wxWinMacControlList.end(); ++it )
2795 {
2796 if ( it->second == impl )
2797 {
2798 wxWinMacControlList.erase(it);
2799 found = true ;
2800 break;
2801 }
2802 }
2803 }
2804 }
2805
2806 IMPLEMENT_ABSTRACT_CLASS( wxWidgetImpl , wxObject )
2807
2808 wxWidgetImpl::wxWidgetImpl( wxWindowMac* peer , bool isRootControl, bool isUserPane )
2809 {
2810 Init();
2811 m_isRootControl = isRootControl;
2812 m_isUserPane = isUserPane;
2813 m_wxPeer = peer;
2814 m_shouldSendEvents = true;
2815 }
2816
2817 wxWidgetImpl::wxWidgetImpl()
2818 {
2819 Init();
2820 }
2821
2822 wxWidgetImpl::~wxWidgetImpl()
2823 {
2824 }
2825
2826 void wxWidgetImpl::Init()
2827 {
2828 m_isRootControl = false;
2829 m_wxPeer = NULL;
2830 m_needsFocusRect = false;
2831 m_needsFrame = true;
2832 }
2833
2834 void wxWidgetImpl::SetNeedsFocusRect( bool needs )
2835 {
2836 m_needsFocusRect = needs;
2837 }
2838
2839 bool wxWidgetImpl::NeedsFocusRect() const
2840 {
2841 return m_needsFocusRect;
2842 }
2843
2844 void wxWidgetImpl::SetNeedsFrame( bool needs )
2845 {
2846 m_needsFrame = needs;
2847 }
2848
2849 bool wxWidgetImpl::NeedsFrame() const
2850 {
2851 return m_needsFrame;
2852 }
2853
2854 void wxWidgetImpl::SetDrawingEnabled(bool WXUNUSED(enabled))
2855 {
2856 }