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