]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/carbon/window.cpp
Fix client data handling: delete client data for all items when a
[wxWidgets.git] / src / mac / carbon / window.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: windows.cpp
3// Purpose: wxWindowMac
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "window.h"
14#endif
15
16#include "wx/setup.h"
17#include "wx/menu.h"
18#include "wx/window.h"
19#include "wx/dc.h"
20#include "wx/dcclient.h"
21#include "wx/utils.h"
22#include "wx/app.h"
23#include "wx/panel.h"
24#include "wx/layout.h"
25#include "wx/dialog.h"
26#include "wx/listbox.h"
27#include "wx/scrolbar.h"
28#include "wx/statbox.h"
29#include "wx/button.h"
30#include "wx/settings.h"
31#include "wx/msgdlg.h"
32#include "wx/frame.h"
33#include "wx/notebook.h"
34#include "wx/tabctrl.h"
35#include "wx/tooltip.h"
36#include "wx/statusbr.h"
37#include "wx/menuitem.h"
38#include "wx/spinctrl.h"
39#include "wx/log.h"
40#include "wx/geometry.h"
41
42#if wxUSE_CARET
43 #include "wx/caret.h"
44#endif // wxUSE_CARET
45
46#define wxWINDOW_HSCROLL 5998
47#define wxWINDOW_VSCROLL 5997
48#define MAC_SCROLLBAR_SIZE 16
49
50#include "wx/mac/uma.h"
51#ifndef __DARWIN__
52#include <Windows.h>
53#include <ToolUtils.h>
54#endif
55
56#if wxUSE_DRAG_AND_DROP
57#include "wx/dnd.h"
58#endif
59
60#include <string.h>
61
62extern wxList wxPendingDelete;
63wxWindowMac* gFocusWindow = NULL ;
64
65#ifdef __WXUNIVERSAL__
66 IMPLEMENT_ABSTRACT_CLASS(wxWindowMac, wxWindowBase)
67#else // __WXMAC__
68 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
69#endif // __WXUNIVERSAL__/__WXMAC__
70
71#if !USE_SHARED_LIBRARY
72
73BEGIN_EVENT_TABLE(wxWindowMac, wxWindowBase)
74 EVT_NC_PAINT(wxWindowMac::OnNcPaint)
75 EVT_ERASE_BACKGROUND(wxWindowMac::OnEraseBackground)
76 EVT_SYS_COLOUR_CHANGED(wxWindowMac::OnSysColourChanged)
77 EVT_INIT_DIALOG(wxWindowMac::OnInitDialog)
78 EVT_IDLE(wxWindowMac::OnIdle)
79 EVT_SET_FOCUS(wxWindowMac::OnSetFocus)
80END_EVENT_TABLE()
81
82#endif
83
84#define wxMAC_DEBUG_REDRAW 0
85#ifndef wxMAC_DEBUG_REDRAW
86#define wxMAC_DEBUG_REDRAW 0
87#endif
88
89#define wxMAC_USE_THEME_BORDER 0
90
91
92// ===========================================================================
93// implementation
94// ===========================================================================
95
96
97// ----------------------------------------------------------------------------
98// constructors and such
99// ----------------------------------------------------------------------------
100
101void wxWindowMac::Init()
102{
103 // generic
104 InitBase();
105
106 // MSW specific
107 m_doubleClickAllowed = 0;
108 m_winCaptured = FALSE;
109
110 m_isBeingDeleted = FALSE;
111
112 m_useCtl3D = FALSE;
113 m_mouseInWindow = FALSE;
114
115 m_xThumbSize = 0;
116 m_yThumbSize = 0;
117 m_backgroundTransparent = FALSE;
118
119 // as all windows are created with WS_VISIBLE style...
120 m_isShown = TRUE;
121
122 m_x = 0;
123 m_y = 0 ;
124 m_width = 0 ;
125 m_height = 0 ;
126
127 m_hScrollBar = NULL ;
128 m_vScrollBar = NULL ;
129
130 m_label = wxEmptyString;
131}
132
133// Destructor
134wxWindowMac::~wxWindowMac()
135{
136 // deleting a window while it is shown invalidates the region
137 if ( IsShown() ) {
138 wxWindowMac* iter = this ;
139 while( iter ) {
140 if ( iter->IsTopLevel() )
141 {
142 Refresh() ;
143 break ;
144 }
145 iter = iter->GetParent() ;
146
147 }
148 }
149
150 m_isBeingDeleted = TRUE;
151
152#ifndef __WXUNIVERSAL__
153 // VS: make sure there's no wxFrame with last focus set to us:
154 for ( wxWindow *win = GetParent(); win; win = win->GetParent() )
155 {
156 wxFrame *frame = wxDynamicCast(win, wxFrame);
157 if ( frame )
158 {
159 if ( frame->GetLastFocus() == this )
160 {
161 frame->SetLastFocus((wxWindow*)NULL);
162 }
163 break;
164 }
165 }
166#endif // __WXUNIVERSAL__
167
168 if ( s_lastMouseWindow == this )
169 {
170 s_lastMouseWindow = NULL ;
171 }
172
173 wxFrame* frame = wxDynamicCast( wxGetTopLevelParent( this ) , wxFrame ) ;
174 if ( frame )
175 {
176 if ( frame->GetLastFocus() == this )
177 frame->SetLastFocus( NULL ) ;
178 }
179
180 if ( gFocusWindow == this )
181 {
182 gFocusWindow = NULL ;
183 }
184
185 if ( m_parent )
186 m_parent->RemoveChild(this);
187
188 DestroyChildren();
189}
190
191// Constructor
192bool wxWindowMac::Create(wxWindowMac *parent, wxWindowID id,
193 const wxPoint& pos,
194 const wxSize& size,
195 long style,
196 const wxString& name)
197{
198 wxCHECK_MSG( parent, FALSE, wxT("can't create wxWindowMac without parent") );
199
200#if wxUSE_STATBOX
201 // wxGTK doesn't allow to create controls with static box as the parent so
202 // this will result in a crash when the program is ported to wxGTK - warn
203 // about it
204 //
205 // the correct solution is to create the controls as siblings of the
206 // static box
207 wxASSERT_MSG( !wxDynamicCast(parent, wxStaticBox),
208 _T("wxStaticBox can't be used as a window parent!") );
209#endif // wxUSE_STATBOX
210
211 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
212 return FALSE;
213
214 parent->AddChild(this);
215
216 m_x = (int)pos.x;
217 m_y = (int)pos.y;
218 AdjustForParentClientOrigin(m_x, m_y, wxSIZE_USE_EXISTING);
219 m_width = WidthDefault( size.x );
220 m_height = HeightDefault( size.y ) ;
221#ifndef __WXUNIVERSAL__
222 // Don't give scrollbars to wxControls unless they ask for them
223 if ( (! IsKindOf(CLASSINFO(wxControl)) && ! IsKindOf(CLASSINFO(wxStatusBar))) ||
224 (IsKindOf(CLASSINFO(wxControl)) && ( style & wxHSCROLL || style & wxVSCROLL)))
225 {
226 MacCreateScrollBars( style ) ;
227 }
228#endif
229 return TRUE;
230}
231
232void wxWindowMac::SetFocus()
233{
234 if ( gFocusWindow == this )
235 return ;
236
237 if ( AcceptsFocus() )
238 {
239 if (gFocusWindow )
240 {
241#if wxUSE_CARET
242 // Deal with caret
243 if ( gFocusWindow->m_caret )
244 {
245 gFocusWindow->m_caret->OnKillFocus();
246 }
247#endif // wxUSE_CARET
248#ifndef __WXUNIVERSAL__
249 wxControl* control = wxDynamicCast( gFocusWindow , wxControl ) ;
250 if ( control && control->GetMacControl() )
251 {
252 UMASetKeyboardFocus( (WindowRef) gFocusWindow->MacGetRootWindow() , (ControlHandle) control->GetMacControl() , kControlFocusNoPart ) ;
253 control->MacRedrawControl() ;
254 }
255#endif
256 // Without testing the window id, for some reason
257 // a kill focus event can still be sent to
258 // the control just being focussed.
259 int thisId = this->m_windowId;
260 int gFocusWindowId = gFocusWindow->m_windowId;
261 if (gFocusWindowId != thisId)
262 {
263 wxFocusEvent event(wxEVT_KILL_FOCUS, gFocusWindow->m_windowId);
264 event.SetEventObject(gFocusWindow);
265 gFocusWindow->GetEventHandler()->ProcessEvent(event) ;
266 }
267 }
268 gFocusWindow = this ;
269 {
270 #if wxUSE_CARET
271 // Deal with caret
272 if ( m_caret )
273 {
274 m_caret->OnSetFocus();
275 }
276 #endif // wxUSE_CARET
277 // panel wants to track the window which was the last to have focus in it
278 wxChildFocusEvent eventFocus(this);
279 GetEventHandler()->ProcessEvent(eventFocus);
280
281 #ifndef __WXUNIVERSAL__
282 wxControl* control = wxDynamicCast( gFocusWindow , wxControl ) ;
283 if ( control && control->GetMacControl() )
284 {
285 UMASetKeyboardFocus( (WindowRef) gFocusWindow->MacGetRootWindow() , (ControlHandle) control->GetMacControl() , kControlFocusNextPart ) ;
286 }
287 #endif
288 wxFocusEvent event(wxEVT_SET_FOCUS, m_windowId);
289 event.SetEventObject(this);
290 GetEventHandler()->ProcessEvent(event) ;
291 }
292 }
293}
294
295bool wxWindowMac::Enable(bool enable)
296{
297 if ( !wxWindowBase::Enable(enable) )
298 return FALSE;
299
300 MacSuperEnabled( enable ) ;
301
302 return TRUE;
303}
304
305void wxWindowMac::DoCaptureMouse()
306{
307 wxTheApp->s_captureWindow = this ;
308}
309
310wxWindow* wxWindowBase::GetCapture()
311{
312 return wxTheApp->s_captureWindow ;
313}
314
315void wxWindowMac::DoReleaseMouse()
316{
317 wxTheApp->s_captureWindow = NULL ;
318}
319
320#if wxUSE_DRAG_AND_DROP
321
322void wxWindowMac::SetDropTarget(wxDropTarget *pDropTarget)
323{
324 if ( m_dropTarget != 0 ) {
325 delete m_dropTarget;
326 }
327
328 m_dropTarget = pDropTarget;
329 if ( m_dropTarget != 0 )
330 {
331 // TODO
332 }
333}
334
335#endif
336
337// Old style file-manager drag&drop
338void wxWindowMac::DragAcceptFiles(bool accept)
339{
340 // TODO
341}
342
343// Get total size
344void wxWindowMac::DoGetSize(int *x, int *y) const
345{
346 if(x) *x = m_width ;
347 if(y) *y = m_height ;
348}
349
350void wxWindowMac::DoGetPosition(int *x, int *y) const
351{
352 int xx,yy;
353
354 xx = m_x ;
355 yy = m_y ;
356 if ( !IsTopLevel() && GetParent())
357 {
358 wxPoint pt(GetParent()->GetClientAreaOrigin());
359 xx -= pt.x;
360 yy -= pt.y;
361 }
362 if(x) *x = xx;
363 if(y) *y = yy;
364}
365
366#if wxUSE_MENUS
367bool wxWindowMac::DoPopupMenu(wxMenu *menu, int x, int y)
368{
369 menu->SetInvokingWindow(this);
370 menu->UpdateUI();
371 ClientToScreen( &x , &y ) ;
372
373 ::InsertMenu( (MenuHandle) menu->GetHMenu() , -1 ) ;
374 long menuResult = ::PopUpMenuSelect((MenuHandle) menu->GetHMenu() ,y,x, 0) ;
375 if ( HiWord(menuResult) != 0 )
376 {
377 MenuCommand id ;
378 GetMenuItemCommandID( GetMenuHandle(HiWord(menuResult)) , LoWord(menuResult) , &id ) ;
379
380 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, id );
381 event.m_timeStamp = TickCount() ;
382 event.SetEventObject(this->GetEventHandler());
383 event.SetInt( id );
384 GetEventHandler()->ProcessEvent(event);
385 }
386 ::DeleteMenu( menu->MacGetMenuId() ) ;
387 menu->SetInvokingWindow(NULL);
388
389 return TRUE;
390}
391#endif
392
393void wxWindowMac::DoScreenToClient(int *x, int *y) const
394{
395 WindowRef window = (WindowRef) MacGetRootWindow() ;
396
397 Point localwhere = {0,0} ;
398
399 if(x) localwhere.h = * x ;
400 if(y) localwhere.v = * y ;
401
402 GrafPtr port ;
403 ::GetPort( &port ) ;
404 ::SetPort( UMAGetWindowPort( window ) ) ;
405 ::GlobalToLocal( &localwhere ) ;
406 ::SetPort( port ) ;
407
408 if(x) *x = localwhere.h ;
409 if(y) *y = localwhere.v ;
410
411 MacRootWindowToWindow( x , y ) ;
412 if ( x )
413 *x -= MacGetLeftBorderSize() ;
414 if ( y )
415 *y -= MacGetTopBorderSize() ;
416}
417
418void wxWindowMac::DoClientToScreen(int *x, int *y) const
419{
420 WindowRef window = (WindowRef) MacGetRootWindow() ;
421
422 if ( x )
423 *x += MacGetLeftBorderSize() ;
424 if ( y )
425 *y += MacGetTopBorderSize() ;
426
427 MacWindowToRootWindow( x , y ) ;
428
429 Point localwhere = { 0,0 };
430 if(x) localwhere.h = * x ;
431 if(y) localwhere.v = * y ;
432
433 GrafPtr port ;
434 ::GetPort( &port ) ;
435 ::SetPort( UMAGetWindowPort( window ) ) ;
436
437 ::LocalToGlobal( &localwhere ) ;
438 ::SetPort( port ) ;
439 if(x) *x = localwhere.h ;
440 if(y) *y = localwhere.v ;
441}
442
443void wxWindowMac::MacClientToRootWindow( int *x , int *y ) const
444{
445 wxPoint origin = GetClientAreaOrigin() ;
446 if(x) *x += origin.x ;
447 if(y) *y += origin.y ;
448
449 MacWindowToRootWindow( x , y ) ;
450}
451
452void wxWindowMac::MacRootWindowToClient( int *x , int *y ) const
453{
454 wxPoint origin = GetClientAreaOrigin() ;
455 MacRootWindowToWindow( x , y ) ;
456 if(x) *x -= origin.x ;
457 if(y) *y -= origin.y ;
458}
459
460void wxWindowMac::MacWindowToRootWindow( int *x , int *y ) const
461{
462 if ( !IsTopLevel() )
463 {
464 if(x) *x += m_x ;
465 if(y) *y += m_y ;
466 GetParent()->MacWindowToRootWindow( x , y ) ;
467 }
468}
469
470void wxWindowMac::MacRootWindowToWindow( int *x , int *y ) const
471{
472 if ( !IsTopLevel() )
473 {
474 if(x) *x -= m_x ;
475 if(y) *y -= m_y ;
476 GetParent()->MacRootWindowToWindow( x , y ) ;
477 }
478}
479
480bool wxWindowMac::SetCursor(const wxCursor& cursor)
481{
482 if (m_cursor == cursor)
483 return FALSE;
484
485 if (wxNullCursor == cursor)
486 {
487 if ( ! wxWindowBase::SetCursor( *wxSTANDARD_CURSOR ) )
488 return FALSE ;
489 }
490 else
491 {
492 if ( ! wxWindowBase::SetCursor( cursor ) )
493 return FALSE ;
494 }
495
496 wxASSERT_MSG( m_cursor.Ok(),
497 wxT("cursor must be valid after call to the base version"));
498
499 Point pt ;
500 wxWindowMac *mouseWin ;
501 GetMouse( &pt ) ;
502
503 // Change the cursor NOW if we're within the correct window
504
505 if ( MacGetWindowFromPoint( wxPoint( pt.h , pt.v ) , &mouseWin ) )
506 {
507 if ( mouseWin == this && !wxIsBusy() )
508 {
509 m_cursor.MacInstall() ;
510 }
511 }
512
513 return TRUE ;
514}
515
516
517// Get size *available for subwindows* i.e. excluding menu bar etc.
518void wxWindowMac::DoGetClientSize(int *x, int *y) const
519{
520 int ww, hh;
521 ww = m_width ;
522 hh = m_height ;
523
524 ww -= MacGetLeftBorderSize( ) + MacGetRightBorderSize( ) ;
525 hh -= MacGetTopBorderSize( ) + MacGetBottomBorderSize( );
526
527 if ( (m_vScrollBar && m_vScrollBar->IsShown()) || (m_hScrollBar && m_hScrollBar->IsShown()) )
528 {
529 int x1 = 0 ;
530 int y1 = 0 ;
531 int w = m_width ;
532 int h = m_height ;
533
534 MacClientToRootWindow( &x1 , &y1 ) ;
535 MacClientToRootWindow( &w , &h ) ;
536
537 wxWindowMac *iter = (wxWindowMac*)this ;
538
539 int totW = 10000 , totH = 10000;
540 while( iter )
541 {
542 if ( iter->IsTopLevel() )
543 {
544 totW = iter->m_width ;
545 totH = iter->m_height ;
546 break ;
547 }
548
549 iter = iter->GetParent() ;
550 }
551
552 if (m_hScrollBar && m_hScrollBar->IsShown() )
553 {
554 hh -= MAC_SCROLLBAR_SIZE;
555 if ( h-y1 >= totH )
556 {
557 hh += 1 ;
558 }
559 }
560 if (m_vScrollBar && m_vScrollBar->IsShown() )
561 {
562 ww -= MAC_SCROLLBAR_SIZE;
563 if ( w-x1 >= totW )
564 {
565 ww += 1 ;
566 }
567 }
568 }
569 if(x) *x = ww;
570 if(y) *y = hh;
571}
572
573
574// ----------------------------------------------------------------------------
575// tooltips
576// ----------------------------------------------------------------------------
577
578#if wxUSE_TOOLTIPS
579
580void wxWindowMac::DoSetToolTip(wxToolTip *tooltip)
581{
582 wxWindowBase::DoSetToolTip(tooltip);
583
584 if ( m_tooltip )
585 m_tooltip->SetWindow(this);
586}
587
588#endif // wxUSE_TOOLTIPS
589
590void wxWindowMac::DoMoveWindow(int x, int y, int width, int height)
591{
592 int former_x = m_x ;
593 int former_y = m_y ;
594 int former_w = m_width ;
595 int former_h = m_height ;
596
597 int actualWidth = width;
598 int actualHeight = height;
599 int actualX = x;
600 int actualY = y;
601
602 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
603 actualWidth = m_minWidth;
604 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
605 actualHeight = m_minHeight;
606 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
607 actualWidth = m_maxWidth;
608 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
609 actualHeight = m_maxHeight;
610
611 bool doMove = false ;
612 bool doResize = false ;
613
614 if ( actualX != former_x || actualY != former_y )
615 {
616 doMove = true ;
617 }
618 if ( actualWidth != former_w || actualHeight != former_h )
619 {
620 doResize = true ;
621 }
622
623 if ( doMove || doResize )
624 {
625 // erase former position
626
627 bool partialRepaint = false ;
628
629 if ( HasFlag(wxNO_FULL_REPAINT_ON_RESIZE) )
630 {
631 wxPoint oldPos( m_x , m_y ) ;
632 wxPoint newPos( actualX , actualY ) ;
633 MacWindowToRootWindow( &oldPos.x , &oldPos.y ) ;
634 MacWindowToRootWindow( &newPos.x , &newPos.y ) ;
635 if ( oldPos == newPos )
636 {
637 partialRepaint = true ;
638 RgnHandle oldRgn,newRgn,diffRgn ;
639 oldRgn = NewRgn() ;
640 newRgn = NewRgn() ;
641 diffRgn = NewRgn() ;
642 SetRectRgn(oldRgn , oldPos.x , oldPos.y , oldPos.x + m_width , oldPos.y + m_height ) ;
643 SetRectRgn(newRgn , newPos.x , newPos.y , newPos.x + actualWidth , newPos.y + actualHeight ) ;
644 DiffRgn( newRgn , oldRgn , diffRgn ) ;
645 InvalWindowRgn( (WindowRef) MacGetRootWindow() , diffRgn ) ;
646 DiffRgn( oldRgn , newRgn , diffRgn ) ;
647 InvalWindowRgn( (WindowRef) MacGetRootWindow() , diffRgn ) ;
648 DisposeRgn(oldRgn) ;
649 DisposeRgn(newRgn) ;
650 DisposeRgn(diffRgn) ;
651 }
652 }
653
654 if ( !partialRepaint )
655 Refresh() ;
656
657 m_x = actualX ;
658 m_y = actualY ;
659 m_width = actualWidth ;
660 m_height = actualHeight ;
661
662 // update any low-level frame-relative positions
663
664 MacUpdateDimensions() ;
665 // erase new position
666
667 if ( !partialRepaint )
668 Refresh() ;
669 if ( doMove )
670 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
671
672 MacRepositionScrollBars() ;
673 if ( doMove )
674 {
675 wxPoint point(m_x, m_y);
676 wxMoveEvent event(point, m_windowId);
677 event.SetEventObject(this);
678 GetEventHandler()->ProcessEvent(event) ;
679 }
680 if ( doResize )
681 {
682 MacRepositionScrollBars() ;
683 wxSize size(m_width, m_height);
684 wxSizeEvent event(size, m_windowId);
685 event.SetEventObject(this);
686 GetEventHandler()->ProcessEvent(event);
687 }
688 }
689
690}
691
692// set the size of the window: if the dimensions are positive, just use them,
693// but if any of them is equal to -1, it means that we must find the value for
694// it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
695// which case -1 is a valid value for x and y)
696//
697// If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
698// the width/height to best suit our contents, otherwise we reuse the current
699// width/height
700void wxWindowMac::DoSetSize(int x, int y, int width, int height, int sizeFlags)
701{
702 // get the current size and position...
703 int currentX, currentY;
704 GetPosition(&currentX, &currentY);
705
706 int currentW,currentH;
707 GetSize(&currentW, &currentH);
708
709 // ... and don't do anything (avoiding flicker) if it's already ok
710 if ( x == currentX && y == currentY &&
711 width == currentW && height == currentH )
712 {
713 MacRepositionScrollBars() ; // we might have a real position shift
714 return;
715 }
716
717 if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
718 x = currentX;
719 if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
720 y = currentY;
721
722 AdjustForParentClientOrigin(x, y, sizeFlags);
723
724 wxSize size(-1, -1);
725 if ( width == -1 )
726 {
727 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
728 {
729 size = DoGetBestSize();
730 width = size.x;
731 }
732 else
733 {
734 // just take the current one
735 width = currentW;
736 }
737 }
738
739 if ( height == -1 )
740 {
741 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
742 {
743 if ( size.x == -1 )
744 {
745 size = DoGetBestSize();
746 }
747 //else: already called DoGetBestSize() above
748
749 height = size.y;
750 }
751 else
752 {
753 // just take the current one
754 height = currentH;
755 }
756 }
757
758 DoMoveWindow(x, y, width, height);
759
760}
761// For implementation purposes - sometimes decorations make the client area
762// smaller
763
764wxPoint wxWindowMac::GetClientAreaOrigin() const
765{
766 return wxPoint(MacGetLeftBorderSize( ) , MacGetTopBorderSize( ) );
767}
768
769void wxWindowMac::SetTitle(const wxString& title)
770{
771 m_label = title ;
772}
773
774wxString wxWindowMac::GetTitle() const
775{
776 return m_label ;
777}
778
779bool wxWindowMac::Show(bool show)
780{
781 if ( !wxWindowBase::Show(show) )
782 return FALSE;
783
784/*
785 WindowRef window = (WindowRef) MacGetRootWindow() ;
786 wxWindowMac* win = wxFindWinFromMacWindow( window ) ;
787 if ( win == NULL && win->m_isBeingDeleted )
788 return FALSE ;
789*/
790 MacSuperShown( show ) ;
791 Refresh() ;
792/*
793 if ( !show )
794 {
795 if ( win && !win->m_isBeingDeleted )
796 Refresh() ;
797 }
798 else
799 {
800 Refresh() ;
801 }
802*/
803 return TRUE;
804}
805
806void wxWindowMac::MacSuperShown( bool show )
807{
808 wxWindowListNode *node = GetChildren().GetFirst();
809 while ( node )
810 {
811 wxWindowMac *child = (wxWindowMac *)node->GetData();
812 if ( child->m_isShown )
813 child->MacSuperShown( show ) ;
814 node = node->GetNext();
815 }
816}
817
818void wxWindowMac::MacSuperEnabled( bool enabled )
819{
820 if ( !IsTopLevel() )
821 {
822 // to be absolutely correct we'd have to invalidate (with eraseBkground
823 // because unter MacOSX the frames are drawn with an addXXX mode)
824 // the borders area
825 }
826 wxWindowListNode *node = GetChildren().GetFirst();
827 while ( node )
828 {
829 wxWindowMac *child = (wxWindowMac *)node->GetData();
830 if ( child->m_isShown )
831 child->MacSuperEnabled( enabled ) ;
832 node = node->GetNext();
833 }
834}
835
836bool wxWindowMac::MacIsReallyShown() const
837{
838 if ( m_isShown && (m_parent != NULL) ) {
839 return m_parent->MacIsReallyShown();
840 }
841 return m_isShown;
842/*
843 bool status = m_isShown ;
844 wxWindowMac * win = this ;
845 while ( status && win->m_parent != NULL )
846 {
847 win = win->m_parent ;
848 status = win->m_isShown ;
849 }
850 return status ;
851*/
852}
853
854int wxWindowMac::GetCharHeight() const
855{
856 wxClientDC dc ( (wxWindowMac*)this ) ;
857 return dc.GetCharHeight() ;
858}
859
860int wxWindowMac::GetCharWidth() const
861{
862 wxClientDC dc ( (wxWindowMac*)this ) ;
863 return dc.GetCharWidth() ;
864}
865
866void wxWindowMac::GetTextExtent(const wxString& string, int *x, int *y,
867 int *descent, int *externalLeading, const wxFont *theFont ) const
868{
869 const wxFont *fontToUse = theFont;
870 if ( !fontToUse )
871 fontToUse = &m_font;
872
873 wxClientDC dc( (wxWindowMac*) this ) ;
874 long lx,ly,ld,le ;
875 dc.GetTextExtent( string , &lx , &ly , &ld, &le, (wxFont *)fontToUse ) ;
876 if ( externalLeading )
877 *externalLeading = le ;
878 if ( descent )
879 *descent = ld ;
880 if ( x )
881 *x = lx ;
882 if ( y )
883 *y = ly ;
884}
885
886/*
887 * Rect is given in client coordinates, for further reading, read wxTopLevelWindowMac::InvalidateRect
888 * we always intersect with the entire window, not only with the client area
889 */
890
891void wxWindowMac::Refresh(bool eraseBack, const wxRect *rect)
892{
893 if ( MacGetTopLevelWindow() == NULL )
894 return ;
895
896 wxPoint client = GetClientAreaOrigin();
897 int x1 = -client.x;
898 int y1 = -client.y;
899 int x2 = m_width - client.x;
900 int y2 = m_height - client.y;
901
902 if (IsKindOf( CLASSINFO(wxButton)))
903 {
904 // buttons have an "aura"
905 y1 -= 5;
906 x1 -= 5;
907 y2 += 5;
908 x2 += 5;
909 }
910
911 Rect clientrect = { y1, x1, y2, x2 };
912
913 if ( rect )
914 {
915 Rect r = { rect->y , rect->x , rect->y + rect->height , rect->x + rect->width } ;
916 SectRect( &clientrect , &r , &clientrect ) ;
917 }
918
919 if ( !EmptyRect( &clientrect ) )
920 {
921 int top = 0 , left = 0 ;
922
923 MacClientToRootWindow( &left , &top ) ;
924 OffsetRect( &clientrect , left , top ) ;
925
926 MacGetTopLevelWindow()->MacInvalidate( &clientrect , eraseBack ) ;
927 }
928}
929
930#if wxUSE_CARET && WXWIN_COMPATIBILITY
931// ---------------------------------------------------------------------------
932// Caret manipulation
933// ---------------------------------------------------------------------------
934
935void wxWindowMac::CreateCaret(int w, int h)
936{
937 SetCaret(new wxCaret(this, w, h));
938}
939
940void wxWindowMac::CreateCaret(const wxBitmap *WXUNUSED(bitmap))
941{
942 wxFAIL_MSG("not implemented");
943}
944
945void wxWindowMac::ShowCaret(bool show)
946{
947 wxCHECK_RET( m_caret, "no caret to show" );
948
949 m_caret->Show(show);
950}
951
952void wxWindowMac::DestroyCaret()
953{
954 SetCaret(NULL);
955}
956
957void wxWindowMac::SetCaretPos(int x, int y)
958{
959 wxCHECK_RET( m_caret, "no caret to move" );
960
961 m_caret->Move(x, y);
962}
963
964void wxWindowMac::GetCaretPos(int *x, int *y) const
965{
966 wxCHECK_RET( m_caret, "no caret to get position of" );
967
968 m_caret->GetPosition(x, y);
969}
970#endif // wxUSE_CARET
971
972wxWindowMac *wxGetActiveWindow()
973{
974 // actually this is a windows-only concept
975 return NULL;
976}
977
978// Coordinates relative to the window
979void wxWindowMac::WarpPointer (int x_pos, int y_pos)
980{
981 // We really dont move the mouse programmatically under mac
982}
983
984const wxBrush& wxWindowMac::MacGetBackgroundBrush()
985{
986 if ( m_backgroundColour == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE) )
987 {
988 m_macBackgroundBrush.SetMacTheme( kThemeBrushDocumentWindowBackground ) ;
989 }
990 else if ( m_backgroundColour == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE ) )
991 {
992 // on mac we have the difficult situation, that 3dface gray can be different colours, depending whether
993 // it is on a notebook panel or not, in order to take care of that we walk up the hierarchy until we have
994 // either a non gray background color or a non control window
995
996 WindowRef window = (WindowRef) MacGetRootWindow() ;
997
998 wxWindowMac* parent = GetParent() ;
999 while( parent )
1000 {
1001 if ( parent->MacGetRootWindow() != window )
1002 {
1003 // we are in a different window on the mac system
1004 parent = NULL ;
1005 break ;
1006 }
1007
1008 {
1009 if ( parent->m_backgroundColour != wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE )
1010 && parent->m_backgroundColour != wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE) )
1011 {
1012 // if we have any other colours in the hierarchy
1013 m_macBackgroundBrush.SetColour( parent->m_backgroundColour ) ;
1014 break ;
1015 }
1016 // if we have the normal colours in the hierarchy but another control etc. -> use it's background
1017 if ( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) ))
1018 {
1019 Rect extent = { 0 , 0 , 0 , 0 } ;
1020 int x , y ;
1021 x = y = 0 ;
1022 wxSize size = parent->GetSize() ;
1023 parent->MacClientToRootWindow( &x , &y ) ;
1024 extent.left = x ;
1025 extent.top = y ;
1026 extent.top-- ;
1027 extent.right = x + size.x ;
1028 extent.bottom = y + size.y ;
1029 m_macBackgroundBrush.SetMacThemeBackground( kThemeBackgroundTabPane , (WXRECTPTR) &extent ) ; // todo eventually change for inactive
1030 break ;
1031 }
1032 }
1033 parent = parent->GetParent() ;
1034 }
1035 if ( !parent )
1036 {
1037 m_macBackgroundBrush.SetMacTheme( kThemeBrushDialogBackgroundActive ) ; // todo eventually change for inactive
1038 }
1039 }
1040 else
1041 {
1042 m_macBackgroundBrush.SetColour( m_backgroundColour ) ;
1043 }
1044
1045 return m_macBackgroundBrush ;
1046
1047}
1048
1049void wxWindowMac::OnEraseBackground(wxEraseEvent& event)
1050{
1051 event.GetDC()->Clear() ;
1052}
1053
1054void wxWindowMac::OnNcPaint( wxNcPaintEvent& event )
1055{
1056 wxWindowDC dc(this) ;
1057 wxMacPortSetter helper(&dc) ;
1058
1059 MacPaintBorders( dc.m_macLocalOrigin.x , dc.m_macLocalOrigin.y) ;
1060}
1061
1062int wxWindowMac::GetScrollPos(int orient) const
1063{
1064 if ( orient == wxHORIZONTAL )
1065 {
1066 if ( m_hScrollBar )
1067 return m_hScrollBar->GetThumbPosition() ;
1068 }
1069 else
1070 {
1071 if ( m_vScrollBar )
1072 return m_vScrollBar->GetThumbPosition() ;
1073 }
1074 return 0;
1075}
1076
1077// This now returns the whole range, not just the number
1078// of positions that we can scroll.
1079int wxWindowMac::GetScrollRange(int orient) const
1080{
1081 if ( orient == wxHORIZONTAL )
1082 {
1083 if ( m_hScrollBar )
1084 return m_hScrollBar->GetRange() ;
1085 }
1086 else
1087 {
1088 if ( m_vScrollBar )
1089 return m_vScrollBar->GetRange() ;
1090 }
1091 return 0;
1092}
1093
1094int wxWindowMac::GetScrollThumb(int orient) const
1095{
1096 if ( orient == wxHORIZONTAL )
1097 {
1098 if ( m_hScrollBar )
1099 return m_hScrollBar->GetThumbSize() ;
1100 }
1101 else
1102 {
1103 if ( m_vScrollBar )
1104 return m_vScrollBar->GetThumbSize() ;
1105 }
1106 return 0;
1107}
1108
1109void wxWindowMac::SetScrollPos(int orient, int pos, bool refresh)
1110{
1111 if ( orient == wxHORIZONTAL )
1112 {
1113 if ( m_hScrollBar )
1114 m_hScrollBar->SetThumbPosition( pos ) ;
1115 }
1116 else
1117 {
1118 if ( m_vScrollBar )
1119 m_vScrollBar->SetThumbPosition( pos ) ;
1120 }
1121}
1122
1123void wxWindowMac::MacPaintBorders( int left , int top )
1124{
1125 if( IsTopLevel() )
1126 return ;
1127
1128 RGBColor white = { 0xFFFF, 0xFFFF , 0xFFFF } ;
1129 RGBColor black = { 0x0000, 0x0000 , 0x0000 } ;
1130 RGBColor face = { 0xDDDD, 0xDDDD , 0xDDDD } ;
1131 RGBColor shadow = { 0x4444, 0x4444 , 0x4444 } ;
1132 PenNormal() ;
1133
1134 if (HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
1135 {
1136#if wxMAC_USE_THEME_BORDER
1137 Rect rect = { top , left , m_height + top , m_width + left } ;
1138 SInt32 border = 0 ;
1139 /*
1140 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
1141 InsetRect( &rect , border , border );
1142 DrawThemeListBoxFrame(&rect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
1143 */
1144
1145 DrawThemePrimaryGroup(&rect ,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
1146#else
1147 bool sunken = HasFlag( wxSUNKEN_BORDER ) ;
1148 RGBForeColor( &face );
1149 MoveTo( left + 0 , top + m_height - 2 );
1150 LineTo( left + 0 , top + 0 );
1151 LineTo( left + m_width - 2 , top + 0 );
1152
1153 MoveTo( left + 2 , top + m_height - 3 );
1154 LineTo( left + m_width - 3 , top + m_height - 3 );
1155 LineTo( left + m_width - 3 , top + 2 );
1156
1157 RGBForeColor( sunken ? &face : &black );
1158 MoveTo( left + 0 , top + m_height - 1 );
1159 LineTo( left + m_width - 1 , top + m_height - 1 );
1160 LineTo( left + m_width - 1 , top + 0 );
1161
1162 RGBForeColor( sunken ? &shadow : &white );
1163 MoveTo( left + 1 , top + m_height - 3 );
1164 LineTo( left + 1, top + 1 );
1165 LineTo( left + m_width - 3 , top + 1 );
1166
1167 RGBForeColor( sunken ? &white : &shadow );
1168 MoveTo( left + 1 , top + m_height - 2 );
1169 LineTo( left + m_width - 2 , top + m_height - 2 );
1170 LineTo( left + m_width - 2 , top + 1 );
1171
1172 RGBForeColor( sunken ? &black : &face );
1173 MoveTo( left + 2 , top + m_height - 4 );
1174 LineTo( left + 2 , top + 2 );
1175 LineTo( left + m_width - 4 , top + 2 );
1176#endif
1177 }
1178 else if (HasFlag(wxSIMPLE_BORDER))
1179 {
1180 Rect rect = { top , left , m_height + top , m_width + left } ;
1181 RGBForeColor( &black ) ;
1182 FrameRect( &rect ) ;
1183 }
1184}
1185
1186void wxWindowMac::RemoveChild( wxWindowBase *child )
1187{
1188 if ( child == m_hScrollBar )
1189 m_hScrollBar = NULL ;
1190 if ( child == m_vScrollBar )
1191 m_vScrollBar = NULL ;
1192
1193 wxWindowBase::RemoveChild( child ) ;
1194}
1195
1196// New function that will replace some of the above.
1197void wxWindowMac::SetScrollbar(int orient, int pos, int thumbVisible,
1198 int range, bool refresh)
1199{
1200 if ( orient == wxHORIZONTAL )
1201 {
1202 if ( m_hScrollBar )
1203 {
1204 if ( range == 0 || thumbVisible >= range )
1205 {
1206 if ( m_hScrollBar->IsShown() )
1207 m_hScrollBar->Show(false) ;
1208 }
1209 else
1210 {
1211 if ( !m_hScrollBar->IsShown() )
1212 m_hScrollBar->Show(true) ;
1213 m_hScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
1214 }
1215 }
1216 }
1217 else
1218 {
1219 if ( m_vScrollBar )
1220 {
1221 if ( range == 0 || thumbVisible >= range )
1222 {
1223 if ( m_vScrollBar->IsShown() )
1224 m_vScrollBar->Show(false) ;
1225 }
1226 else
1227 {
1228 if ( !m_vScrollBar->IsShown() )
1229 m_vScrollBar->Show(true) ;
1230 m_vScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
1231 }
1232 }
1233 }
1234 MacRepositionScrollBars() ;
1235}
1236
1237// Does a physical scroll
1238void wxWindowMac::ScrollWindow(int dx, int dy, const wxRect *rect)
1239{
1240 wxClientDC dc(this) ;
1241 wxMacPortSetter helper(&dc) ;
1242
1243 {
1244 int width , height ;
1245 GetClientSize( &width , &height ) ;
1246
1247 Rect scrollrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) , dc.YLOG2DEVMAC(height) , dc.XLOG2DEVMAC(width) } ;
1248 RgnHandle updateRgn = NewRgn() ;
1249 ClipRect( &scrollrect ) ;
1250 if ( rect )
1251 {
1252 Rect r = { dc.YLOG2DEVMAC(rect->y) , dc.XLOG2DEVMAC(rect->x) , dc.YLOG2DEVMAC(rect->y + rect->height) ,
1253 dc.XLOG2DEVMAC(rect->x + rect->width) } ;
1254 SectRect( &scrollrect , &r , &scrollrect ) ;
1255 }
1256 ScrollRect( &scrollrect , dx , dy , updateRgn ) ;
1257 InvalWindowRgn( (WindowRef) MacGetRootWindow() , updateRgn ) ;
1258 DisposeRgn( updateRgn ) ;
1259 }
1260
1261 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
1262 {
1263 wxWindowMac *child = (wxWindowMac*)node->GetData();
1264 if (child == m_vScrollBar) continue;
1265 if (child == m_hScrollBar) continue;
1266 if (child->IsTopLevel()) continue;
1267
1268 int x,y;
1269 child->GetPosition( &x, &y );
1270 int w,h;
1271 child->GetSize( &w, &h );
1272 child->SetSize( x+dx, y+dy, w, h );
1273 }
1274
1275}
1276
1277void wxWindowMac::MacOnScroll(wxScrollEvent &event )
1278{
1279 if ( event.m_eventObject == m_vScrollBar || event.m_eventObject == m_hScrollBar )
1280 {
1281 wxScrollWinEvent wevent;
1282 wevent.SetPosition(event.GetPosition());
1283 wevent.SetOrientation(event.GetOrientation());
1284 wevent.m_eventObject = this;
1285
1286 if (event.m_eventType == wxEVT_SCROLL_TOP) {
1287 wevent.m_eventType = wxEVT_SCROLLWIN_TOP;
1288 } else
1289 if (event.m_eventType == wxEVT_SCROLL_BOTTOM) {
1290 wevent.m_eventType = wxEVT_SCROLLWIN_BOTTOM;
1291 } else
1292 if (event.m_eventType == wxEVT_SCROLL_LINEUP) {
1293 wevent.m_eventType = wxEVT_SCROLLWIN_LINEUP;
1294 } else
1295 if (event.m_eventType == wxEVT_SCROLL_LINEDOWN) {
1296 wevent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN;
1297 } else
1298 if (event.m_eventType == wxEVT_SCROLL_PAGEUP) {
1299 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEUP;
1300 } else
1301 if (event.m_eventType == wxEVT_SCROLL_PAGEDOWN) {
1302 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEDOWN;
1303 } else
1304 if (event.m_eventType == wxEVT_SCROLL_THUMBTRACK) {
1305 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBTRACK;
1306 }
1307
1308 GetEventHandler()->ProcessEvent(wevent);
1309 }
1310}
1311
1312// Get the window with the focus
1313wxWindowMac *wxWindowBase::FindFocus()
1314{
1315 return gFocusWindow ;
1316}
1317
1318#if WXWIN_COMPATIBILITY
1319// If nothing defined for this, try the parent.
1320// E.g. we may be a button loaded from a resource, with no callback function
1321// defined.
1322void wxWindowMac::OnCommand(wxWindowMac& win, wxCommandEvent& event)
1323{
1324 if ( GetEventHandler()->ProcessEvent(event) )
1325 return;
1326 if ( m_parent )
1327 m_parent->GetEventHandler()->OnCommand(win, event);
1328}
1329#endif // WXWIN_COMPATIBILITY_2
1330
1331#if WXWIN_COMPATIBILITY
1332wxObject* wxWindowMac::GetChild(int number) const
1333{
1334 // Return a pointer to the Nth object in the Panel
1335 wxNode *node = GetChildren().GetFirst();
1336 int n = number;
1337 while (node && n--)
1338 node = node->GetNext();
1339 if ( node )
1340 {
1341 wxObject *obj = (wxObject *)node->GetData();
1342 return(obj);
1343 }
1344 else
1345 return NULL;
1346}
1347#endif // WXWIN_COMPATIBILITY
1348
1349void wxWindowMac::OnSetFocus(wxFocusEvent& event)
1350{
1351 // panel wants to track the window which was the last to have focus in it,
1352 // so we want to set ourselves as the window which last had focus
1353 //
1354 // notice that it's also important to do it upwards the tree becaus
1355 // otherwise when the top level panel gets focus, it won't set it back to
1356 // us, but to some other sibling
1357
1358 // CS:don't know if this is still needed:
1359 //wxChildFocusEvent eventFocus(this);
1360 //(void)GetEventHandler()->ProcessEvent(eventFocus);
1361
1362 event.Skip();
1363}
1364
1365void wxWindowMac::Clear()
1366{
1367 wxClientDC dc(this);
1368 wxBrush brush(GetBackgroundColour(), wxSOLID);
1369 dc.SetBackground(brush);
1370 dc.Clear();
1371}
1372
1373// Setup background and foreground colours correctly
1374void wxWindowMac::SetupColours()
1375{
1376 if ( GetParent() )
1377 SetBackgroundColour(GetParent()->GetBackgroundColour());
1378}
1379
1380void wxWindowMac::OnIdle(wxIdleEvent& event)
1381{
1382/*
1383 // Check if we need to send a LEAVE event
1384 if (m_mouseInWindow)
1385 {
1386 POINT pt;
1387 ::GetCursorPos(&pt);
1388 if (::WindowFromPoint(pt) != (HWND) GetHWND())
1389 {
1390 // Generate a LEAVE event
1391 m_mouseInWindow = FALSE;
1392 MSWOnMouseLeave(pt.x, pt.y, 0);
1393 }
1394 }
1395*/
1396
1397 // This calls the UI-update mechanism (querying windows for
1398 // menu/toolbar/control state information)
1399 UpdateWindowUI();
1400}
1401
1402// Raise the window to the top of the Z order
1403void wxWindowMac::Raise()
1404{
1405}
1406
1407// Lower the window to the bottom of the Z order
1408void wxWindowMac::Lower()
1409{
1410}
1411
1412void wxWindowMac::DoSetClientSize(int width, int height)
1413{
1414 if ( width != -1 || height != -1 )
1415 {
1416
1417 if ( width != -1 && m_vScrollBar )
1418 width += MAC_SCROLLBAR_SIZE ;
1419 if ( height != -1 && m_vScrollBar )
1420 height += MAC_SCROLLBAR_SIZE ;
1421
1422 width += MacGetLeftBorderSize( ) + MacGetRightBorderSize( ) ;
1423 height += MacGetTopBorderSize( ) + MacGetBottomBorderSize( ) ;
1424
1425 DoSetSize( -1 , -1 , width , height ) ;
1426 }
1427}
1428
1429
1430wxWindowMac* wxWindowMac::s_lastMouseWindow = NULL ;
1431
1432bool wxWindowMac::MacGetWindowFromPointSub( const wxPoint &point , wxWindowMac** outWin )
1433{
1434 if ( IsTopLevel() )
1435 {
1436 if ((point.x < 0) || (point.y < 0) ||
1437 (point.x > (m_width)) || (point.y > (m_height)))
1438 return FALSE;
1439 }
1440 else
1441 {
1442 if ((point.x < m_x) || (point.y < m_y) ||
1443 (point.x > (m_x + m_width)) || (point.y > (m_y + m_height)))
1444 return FALSE;
1445 }
1446
1447 WindowRef window = (WindowRef) MacGetRootWindow() ;
1448
1449 wxPoint newPoint( point ) ;
1450
1451 if ( !IsTopLevel() )
1452 {
1453 newPoint.x -= m_x;
1454 newPoint.y -= m_y;
1455 }
1456
1457 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
1458 {
1459 wxWindowMac *child = (wxWindowMac*)node->GetData();
1460 // added the m_isShown test --dmazzoni
1461 if ( child->MacGetRootWindow() == window && child->m_isShown )
1462 {
1463 if (child->MacGetWindowFromPointSub(newPoint , outWin ))
1464 return TRUE;
1465 }
1466 }
1467
1468 *outWin = this ;
1469 return TRUE;
1470}
1471
1472bool wxWindowMac::MacGetWindowFromPoint( const wxPoint &screenpoint , wxWindowMac** outWin )
1473{
1474 WindowRef window ;
1475
1476 Point pt = { screenpoint.y , screenpoint.x } ;
1477 if ( ::FindWindow( pt , &window ) == 3 )
1478 {
1479
1480 wxWindowMac* win = wxFindWinFromMacWindow( window ) ;
1481 if ( win )
1482 {
1483 // No, this yields the CLIENT are, we need the whole frame. RR.
1484 // point = win->ScreenToClient( point ) ;
1485
1486 GrafPtr port;
1487 ::GetPort( &port ) ;
1488 ::SetPort( UMAGetWindowPort( window ) ) ;
1489 ::GlobalToLocal( &pt ) ;
1490 ::SetPort( port ) ;
1491
1492 wxPoint point( pt.h, pt.v ) ;
1493
1494 return win->MacGetWindowFromPointSub( point , outWin ) ;
1495 }
1496 }
1497 return FALSE ;
1498}
1499
1500static wxWindow *gs_lastWhich = NULL;
1501
1502bool wxWindowMac::MacSetupCursor( const wxPoint& pt)
1503{
1504 // first trigger a set cursor event
1505
1506 wxPoint clientorigin = GetClientAreaOrigin() ;
1507 wxSize clientsize = GetClientSize() ;
1508 wxCursor cursor ;
1509 if ( wxRect2DInt( clientorigin.x , clientorigin.y , clientsize.x , clientsize.y ).Contains( wxPoint2DInt( pt ) ) )
1510 {
1511 wxSetCursorEvent event( pt.x , pt.y );
1512
1513 bool processedEvtSetCursor = GetEventHandler()->ProcessEvent(event);
1514 if ( processedEvtSetCursor && event.HasCursor() )
1515 {
1516 cursor = event.GetCursor() ;
1517 }
1518 else
1519 {
1520
1521 // the test for processedEvtSetCursor is here to prevent using m_cursor
1522 // if the user code caught EVT_SET_CURSOR() and returned nothing from
1523 // it - this is a way to say that our cursor shouldn't be used for this
1524 // point
1525 if ( !processedEvtSetCursor && m_cursor.Ok() )
1526 {
1527 cursor = m_cursor ;
1528 }
1529 if ( wxIsBusy() )
1530 {
1531 }
1532 else
1533 {
1534 if ( !GetParent() )
1535 cursor = *wxSTANDARD_CURSOR ;
1536 }
1537 }
1538 if ( cursor.Ok() )
1539 cursor.MacInstall() ;
1540 }
1541 return cursor.Ok() ;
1542}
1543
1544bool wxWindowMac::MacDispatchMouseEvent(wxMouseEvent& event)
1545{
1546 if ((event.m_x < m_x) || (event.m_y < m_y) ||
1547 (event.m_x > (m_x + m_width)) || (event.m_y > (m_y + m_height)))
1548 return FALSE;
1549
1550
1551 if ( IsKindOf( CLASSINFO ( wxStaticBox ) ) /* || IsKindOf( CLASSINFO( wxSpinCtrl ) ) */)
1552 return FALSE ;
1553
1554 WindowRef window = (WindowRef) MacGetRootWindow() ;
1555
1556 event.m_x -= m_x;
1557 event.m_y -= m_y;
1558
1559 int x = event.m_x ;
1560 int y = event.m_y ;
1561
1562 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
1563 {
1564 wxWindowMac *child = (wxWindowMac*)node->GetData();
1565 if ( child->MacGetRootWindow() == window && child->IsShown() && child->IsEnabled() )
1566 {
1567 if (child->MacDispatchMouseEvent(event))
1568 return TRUE;
1569 }
1570 }
1571
1572 wxWindow* cursorTarget = this ;
1573 wxPoint cursorPoint( x , y ) ;
1574
1575 while( cursorTarget && !cursorTarget->MacSetupCursor( cursorPoint ) )
1576 {
1577 cursorTarget = cursorTarget->GetParent() ;
1578 if ( cursorTarget )
1579 cursorPoint += cursorTarget->GetPosition() ;
1580 }
1581 event.m_x = x ;
1582 event.m_y = y ;
1583 event.SetEventObject( this ) ;
1584
1585 if ( event.GetEventType() == wxEVT_LEFT_DOWN )
1586 {
1587 // set focus to this window
1588 if (AcceptsFocus() && FindFocus()!=this)
1589 SetFocus();
1590 }
1591
1592#if wxUSE_TOOLTIPS
1593 if ( event.GetEventType() == wxEVT_MOTION
1594 || event.GetEventType() == wxEVT_ENTER_WINDOW
1595 || event.GetEventType() == wxEVT_LEAVE_WINDOW )
1596 wxToolTip::RelayEvent( this , event);
1597#endif // wxUSE_TOOLTIPS
1598
1599 if (gs_lastWhich != this)
1600 {
1601 gs_lastWhich = this;
1602
1603 // Double clicks must always occur on the same window
1604 if (event.GetEventType() == wxEVT_LEFT_DCLICK)
1605 event.SetEventType( wxEVT_LEFT_DOWN );
1606 if (event.GetEventType() == wxEVT_RIGHT_DCLICK)
1607 event.SetEventType( wxEVT_RIGHT_DOWN );
1608
1609 // Same for mouse up events
1610 if (event.GetEventType() == wxEVT_LEFT_UP)
1611 return TRUE;
1612 if (event.GetEventType() == wxEVT_RIGHT_UP)
1613 return TRUE;
1614 }
1615
1616 GetEventHandler()->ProcessEvent( event ) ;
1617
1618 return TRUE;
1619}
1620
1621wxString wxWindowMac::MacGetToolTipString( wxPoint &pt )
1622{
1623 if ( m_tooltip )
1624 {
1625 return m_tooltip->GetTip() ;
1626 }
1627 return "" ;
1628}
1629
1630void wxWindowMac::Update()
1631{
1632 wxTopLevelWindowMac* win = MacGetTopLevelWindow( ) ;
1633 if ( win )
1634 {
1635 win->MacUpdate( 0 ) ;
1636#if TARGET_API_MAC_CARBON
1637 if ( QDIsPortBuffered( GetWindowPort( (WindowRef) win->MacGetWindowRef() ) ) )
1638 {
1639 QDFlushPortBuffer( GetWindowPort( (WindowRef) win->MacGetWindowRef() ) , NULL ) ;
1640 }
1641#endif
1642 }
1643}
1644
1645wxTopLevelWindowMac* wxWindowMac::MacGetTopLevelWindow() const
1646{
1647 wxTopLevelWindowMac* win = NULL ;
1648 WindowRef window = (WindowRef) MacGetRootWindow() ;
1649 if ( window )
1650 {
1651 win = wxFindWinFromMacWindow( window ) ;
1652 }
1653 return win ;
1654}
1655
1656const wxRegion& wxWindowMac::MacGetVisibleRegion( bool respectChildrenAndSiblings )
1657{
1658 RgnHandle visRgn = NewRgn() ;
1659 RgnHandle tempRgn = NewRgn() ;
1660 RgnHandle tempStaticBoxRgn = NewRgn() ;
1661
1662 SetRectRgn( visRgn , 0 , 0 , m_width , m_height ) ;
1663
1664 //TODO : as soon as the new scheme has proven to work correctly, move this to wxStaticBox
1665 if ( IsKindOf( CLASSINFO( wxStaticBox ) ) )
1666 {
1667 int borderTop = 14 ;
1668 int borderOther = 4 ;
1669
1670 SetRectRgn( tempStaticBoxRgn , borderOther , borderTop , m_width - borderOther , m_height - borderOther ) ;
1671 DiffRgn( visRgn , tempStaticBoxRgn , visRgn ) ;
1672 }
1673
1674 if ( !IsTopLevel() )
1675 {
1676 wxWindow* parent = GetParent() ;
1677 while( parent )
1678 {
1679 wxSize size = parent->GetSize() ;
1680 int x , y ;
1681 x = y = 0 ;
1682 parent->MacWindowToRootWindow( &x, &y ) ;
1683 MacRootWindowToWindow( &x , &y ) ;
1684
1685 SetRectRgn( tempRgn ,
1686 x + parent->MacGetLeftBorderSize() , y + parent->MacGetTopBorderSize() ,
1687 x + size.x - parent->MacGetRightBorderSize(),
1688 y + size.y - parent->MacGetBottomBorderSize()) ;
1689
1690 SectRgn( visRgn , tempRgn , visRgn ) ;
1691 if ( parent->IsTopLevel() )
1692 break ;
1693 parent = parent->GetParent() ;
1694 }
1695 }
1696 if ( respectChildrenAndSiblings )
1697 {
1698 if ( GetWindowStyle() & wxCLIP_CHILDREN )
1699 {
1700 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
1701 {
1702 wxWindowMac *child = (wxWindowMac*)node->GetData();
1703
1704 if ( !child->IsTopLevel() && child->IsShown() )
1705 {
1706 SetRectRgn( tempRgn , child->m_x , child->m_y , child->m_x + child->m_width , child->m_y + child->m_height ) ;
1707 if ( child->IsKindOf( CLASSINFO( wxStaticBox ) ) )
1708 {
1709 int borderTop = 14 ;
1710 int borderOther = 4 ;
1711
1712 SetRectRgn( tempStaticBoxRgn , child->m_x + borderOther , child->m_y + borderTop , child->m_x + child->m_width - borderOther , child->m_y + child->m_height - borderOther ) ;
1713 DiffRgn( tempRgn , tempStaticBoxRgn , tempRgn ) ;
1714 }
1715 DiffRgn( visRgn , tempRgn , visRgn ) ;
1716 }
1717 }
1718 }
1719
1720 if ( (GetWindowStyle() & wxCLIP_SIBLINGS) && GetParent() )
1721 {
1722 bool thisWindowThrough = false ;
1723 for (wxWindowListNode *node = GetParent()->GetChildren().GetFirst(); node; node = node->GetNext())
1724 {
1725 wxWindowMac *sibling = (wxWindowMac*)node->GetData();
1726 if ( sibling == this )
1727 {
1728 thisWindowThrough = true ;
1729 continue ;
1730 }
1731 if( !thisWindowThrough )
1732 {
1733 continue ;
1734 }
1735
1736 if ( !sibling->IsTopLevel() && sibling->IsShown() )
1737 {
1738 SetRectRgn( tempRgn , sibling->m_x - m_x , sibling->m_y - m_y , sibling->m_x + sibling->m_width - m_x , sibling->m_y + sibling->m_height - m_y ) ;
1739 if ( sibling->IsKindOf( CLASSINFO( wxStaticBox ) ) )
1740 {
1741 int borderTop = 14 ;
1742 int borderOther = 4 ;
1743
1744 SetRectRgn( tempStaticBoxRgn , sibling->m_x - m_x + borderOther , sibling->m_y - m_y + borderTop , sibling->m_x + sibling->m_width - m_x - borderOther , sibling->m_y + sibling->m_height - m_y - borderOther ) ;
1745 DiffRgn( tempRgn , tempStaticBoxRgn , tempRgn ) ;
1746 }
1747 DiffRgn( visRgn , tempRgn , visRgn ) ;
1748 }
1749 }
1750 }
1751 }
1752 m_macVisibleRegion = visRgn ;
1753 DisposeRgn( visRgn ) ;
1754 DisposeRgn( tempRgn ) ;
1755 DisposeRgn( tempStaticBoxRgn ) ;
1756 return m_macVisibleRegion ;
1757}
1758
1759void wxWindowMac::MacRedraw( WXHRGN updatergnr , long time, bool erase)
1760{
1761 RgnHandle updatergn = (RgnHandle) updatergnr ;
1762 // updatergn is always already clipped to our boundaries
1763 // it is in window coordinates, not in client coordinates
1764
1765 WindowRef window = (WindowRef) MacGetRootWindow() ;
1766
1767 {
1768 // ownUpdateRgn is the area that this window has to repaint, it is in window coordinates
1769 RgnHandle ownUpdateRgn = NewRgn() ;
1770 CopyRgn( updatergn , ownUpdateRgn ) ;
1771
1772 SectRgn( ownUpdateRgn , (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , ownUpdateRgn ) ;
1773
1774 // newupdate is the update region in client coordinates
1775 RgnHandle newupdate = NewRgn() ;
1776 wxSize point = GetClientSize() ;
1777 wxPoint origin = GetClientAreaOrigin() ;
1778 SetRectRgn( newupdate , origin.x , origin.y , origin.x + point.x , origin.y+point.y ) ;
1779 SectRgn( newupdate , ownUpdateRgn , newupdate ) ;
1780 OffsetRgn( newupdate , -origin.x , -origin.y ) ;
1781 m_updateRegion = newupdate ;
1782 DisposeRgn( newupdate ) ; // it's been cloned to m_updateRegion
1783
1784 if ( erase && !EmptyRgn(ownUpdateRgn) )
1785 {
1786 wxWindowDC dc(this);
1787 if (!EmptyRgn(ownUpdateRgn))
1788 dc.SetClippingRegion(wxRegion(ownUpdateRgn));
1789 wxEraseEvent eevent( GetId(), &dc );
1790 eevent.SetEventObject( this );
1791 GetEventHandler()->ProcessEvent( eevent );
1792
1793 wxNcPaintEvent eventNc( GetId() );
1794 eventNc.SetEventObject( this );
1795 GetEventHandler()->ProcessEvent( eventNc );
1796 }
1797 DisposeRgn( ownUpdateRgn ) ;
1798 if ( !m_updateRegion.Empty() )
1799 {
1800 wxPaintEvent event;
1801 event.m_timeStamp = time ;
1802 event.SetEventObject(this);
1803 GetEventHandler()->ProcessEvent(event);
1804 }
1805 }
1806
1807 // now intersect for each of the children their rect with the updateRgn and call MacRedraw recursively
1808
1809 RgnHandle childupdate = NewRgn() ;
1810 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
1811 {
1812 // calculate the update region for the child windows by intersecting the window rectangle with our own
1813 // passed in update region and then offset it to be client-wise window coordinates again
1814 wxWindowMac *child = (wxWindowMac*)node->GetData();
1815 SetRectRgn( childupdate , child->m_x , child->m_y , child->m_x + child->m_width , child->m_y + child->m_height ) ;
1816 SectRgn( childupdate , updatergn , childupdate ) ;
1817 OffsetRgn( childupdate , -child->m_x , -child->m_y ) ;
1818 if ( child->MacGetRootWindow() == window && child->IsShown() && !EmptyRgn( childupdate ) )
1819 {
1820 // because dialogs may also be children
1821 child->MacRedraw( childupdate , time , erase ) ;
1822 }
1823 }
1824 DisposeRgn( childupdate ) ;
1825 // eventually a draw grow box here
1826
1827}
1828
1829WXHWND wxWindowMac::MacGetRootWindow() const
1830{
1831 wxWindowMac *iter = (wxWindowMac*)this ;
1832
1833 while( iter )
1834 {
1835 if ( iter->IsTopLevel() )
1836 return ((wxTopLevelWindow*)iter)->MacGetWindowRef() ;
1837
1838 iter = iter->GetParent() ;
1839 }
1840 wxASSERT_MSG( 1 , "No valid mac root window" ) ;
1841 return NULL ;
1842}
1843
1844void wxWindowMac::MacCreateScrollBars( long style )
1845{
1846 wxASSERT_MSG( m_vScrollBar == NULL && m_hScrollBar == NULL , "attempt to create window twice" ) ;
1847
1848 bool hasBoth = ( style & wxVSCROLL ) && ( style & wxHSCROLL ) ;
1849 int adjust = hasBoth ? MAC_SCROLLBAR_SIZE - 1: 0 ;
1850 int width, height ;
1851 GetClientSize( &width , &height ) ;
1852
1853 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
1854 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
1855 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
1856 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
1857
1858 m_vScrollBar = new wxScrollBar(this, wxWINDOW_VSCROLL, vPoint,
1859 vSize , wxVERTICAL);
1860
1861 if ( style & wxVSCROLL )
1862 {
1863
1864 }
1865 else
1866 {
1867 m_vScrollBar->Show(false) ;
1868 }
1869 m_hScrollBar = new wxScrollBar(this, wxWINDOW_HSCROLL, hPoint,
1870 hSize , wxHORIZONTAL);
1871 if ( style & wxHSCROLL )
1872 {
1873 }
1874 else
1875 {
1876 m_hScrollBar->Show(false) ;
1877 }
1878
1879 // because the create does not take into account the client area origin
1880 MacRepositionScrollBars() ; // we might have a real position shift
1881}
1882
1883void wxWindowMac::MacRepositionScrollBars()
1884{
1885 bool hasBoth = ( m_hScrollBar && m_hScrollBar->IsShown()) && ( m_vScrollBar && m_vScrollBar->IsShown()) ;
1886 int adjust = hasBoth ? MAC_SCROLLBAR_SIZE - 1 : 0 ;
1887
1888 // get real client area
1889
1890 int width = m_width ;
1891 int height = m_height ;
1892
1893 width -= MacGetLeftBorderSize() + MacGetRightBorderSize();
1894 height -= MacGetTopBorderSize() + MacGetBottomBorderSize();
1895
1896 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
1897 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
1898 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
1899 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
1900
1901 int x = 0 ;
1902 int y = 0 ;
1903 int w = m_width ;
1904 int h = m_height ;
1905
1906 MacClientToRootWindow( &x , &y ) ;
1907 MacClientToRootWindow( &w , &h ) ;
1908
1909 wxWindowMac *iter = (wxWindowMac*)this ;
1910
1911 int totW = 10000 , totH = 10000;
1912 while( iter )
1913 {
1914 if ( iter->IsTopLevel() )
1915 {
1916 totW = iter->m_width ;
1917 totH = iter->m_height ;
1918 break ;
1919 }
1920
1921 iter = iter->GetParent() ;
1922 }
1923
1924 if ( x == 0 )
1925 {
1926 hPoint.x = -1 ;
1927 hSize.x += 1 ;
1928 }
1929 if ( y == 0 )
1930 {
1931 vPoint.y = -1 ;
1932 vSize.y += 1 ;
1933 }
1934
1935 if ( w-x >= totW )
1936 {
1937 hSize.x += 1 ;
1938 vPoint.x += 1 ;
1939 }
1940
1941 if ( h-y >= totH )
1942 {
1943 vSize.y += 1 ;
1944 hPoint.y += 1 ;
1945 }
1946
1947 if ( m_vScrollBar )
1948 {
1949 m_vScrollBar->SetSize( vPoint.x , vPoint.y, vSize.x, vSize.y , wxSIZE_ALLOW_MINUS_ONE);
1950 }
1951 if ( m_hScrollBar )
1952 {
1953 m_hScrollBar->SetSize( hPoint.x , hPoint.y, hSize.x, hSize.y, wxSIZE_ALLOW_MINUS_ONE);
1954 }
1955}
1956
1957bool wxWindowMac::AcceptsFocus() const
1958{
1959 return MacCanFocus() && wxWindowBase::AcceptsFocus();
1960}
1961
1962WXWidget wxWindowMac::MacGetContainerForEmbedding()
1963{
1964 return GetParent()->MacGetContainerForEmbedding() ;
1965}
1966
1967void wxWindowMac::MacSuperChangedPosition()
1968{
1969 // only window-absolute structures have to be moved i.e. controls
1970
1971 wxWindowListNode *node = GetChildren().GetFirst();
1972 while ( node )
1973 {
1974 wxWindowMac *child = (wxWindowMac *)node->GetData();
1975 child->MacSuperChangedPosition() ;
1976 node = node->GetNext();
1977 }
1978}
1979
1980void wxWindowMac::MacTopLevelWindowChangedPosition()
1981{
1982 // only screen-absolute structures have to be moved i.e. glcanvas
1983
1984 wxWindowListNode *node = GetChildren().GetFirst();
1985 while ( node )
1986 {
1987 wxWindowMac *child = (wxWindowMac *)node->GetData();
1988 child->MacTopLevelWindowChangedPosition() ;
1989 node = node->GetNext();
1990 }
1991}
1992long wxWindowMac::MacGetLeftBorderSize( ) const
1993{
1994 if( IsTopLevel() )
1995 return 0 ;
1996
1997 if (m_windowStyle & wxRAISED_BORDER || m_windowStyle & wxSUNKEN_BORDER )
1998 {
1999 SInt32 border = 3 ;
2000#if wxMAC_USE_THEME_BORDER
2001#if TARGET_CARBON
2002 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2003#endif
2004#endif
2005 return border ;
2006 }
2007 else if ( m_windowStyle &wxDOUBLE_BORDER)
2008 {
2009 SInt32 border = 3 ;
2010#if wxMAC_USE_THEME_BORDER
2011#if TARGET_CARBON
2012 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2013#endif
2014#endif
2015 return border ;
2016 }
2017 else if (m_windowStyle &wxSIMPLE_BORDER)
2018 {
2019 return 1 ;
2020 }
2021 return 0 ;
2022}
2023
2024long wxWindowMac::MacGetRightBorderSize( ) const
2025{
2026 // they are all symmetric in mac themes
2027 return MacGetLeftBorderSize() ;
2028}
2029
2030long wxWindowMac::MacGetTopBorderSize( ) const
2031{
2032 // they are all symmetric in mac themes
2033 return MacGetLeftBorderSize() ;
2034}
2035
2036long wxWindowMac::MacGetBottomBorderSize( ) const
2037{
2038 // they are all symmetric in mac themes
2039 return MacGetLeftBorderSize() ;
2040}
2041
2042long wxWindowMac::MacRemoveBordersFromStyle( long style )
2043{
2044 return style & ~( wxDOUBLE_BORDER | wxSUNKEN_BORDER | wxRAISED_BORDER | wxBORDER | wxSTATIC_BORDER ) ;
2045}
2046
2047// Find the wxWindowMac at the current mouse position, returning the mouse
2048// position.
2049wxWindowMac* wxFindWindowAtPointer(wxPoint& pt)
2050{
2051 pt = wxGetMousePosition();
2052 wxWindowMac* found = wxFindWindowAtPoint(pt);
2053 return found;
2054}
2055
2056// Get the current mouse position.
2057wxPoint wxGetMousePosition()
2058{
2059 int x, y;
2060 wxGetMousePosition(& x, & y);
2061 return wxPoint(x, y);
2062}
2063