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