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