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