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