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