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