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