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