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