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