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