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