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