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