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