]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/window.cpp
redraw fixes
[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/scrolbar.h"
27 #include "wx/statbox.h"
28 #include "wx/button.h"
29 #include "wx/settings.h"
30 #include "wx/msgdlg.h"
31 #include "wx/frame.h"
32 #include "wx/tooltip.h"
33 #include "wx/statusbr.h"
34 #include "wx/menuitem.h"
35 #include "wx/spinctrl.h"
36 #include "wx/log.h"
37 #include "wx/geometry.h"
38 #include "wx/textctrl.h"
39
40 #include "wx/toolbar.h"
41 #include "wx/dc.h"
42
43 #if wxUSE_CARET
44 #include "wx/caret.h"
45 #endif // wxUSE_CARET
46
47 #define wxWINDOW_HSCROLL 5998
48 #define wxWINDOW_VSCROLL 5997
49
50 #define MAC_SCROLLBAR_SIZE 15
51 #define MAC_SMALL_SCROLLBAR_SIZE 11
52
53 #include "wx/mac/uma.h"
54 #ifndef __DARWIN__
55 #include <Windows.h>
56 #include <ToolUtils.h>
57 #include <Scrap.h>
58 #include <MacTextEditor.h>
59 #endif
60
61 #if TARGET_API_MAC_OSX
62 #ifndef __HIVIEW__
63 #include <HIToolbox/HIView.h>
64 #endif
65 #endif
66
67 #if wxUSE_DRAG_AND_DROP
68 #include "wx/dnd.h"
69 #endif
70
71 #include <string.h>
72
73 extern wxList wxPendingDelete;
74
75 #ifdef __WXUNIVERSAL__
76 IMPLEMENT_ABSTRACT_CLASS(wxWindowMac, wxWindowBase)
77 #else // __WXMAC__
78 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
79 #endif // __WXUNIVERSAL__/__WXMAC__
80
81 #if !USE_SHARED_LIBRARY
82
83 BEGIN_EVENT_TABLE(wxWindowMac, wxWindowBase)
84 EVT_NC_PAINT(wxWindowMac::OnNcPaint)
85 EVT_ERASE_BACKGROUND(wxWindowMac::OnEraseBackground)
86 // TODO EVT_PAINT(wxWindowMac::OnPaint)
87 EVT_SET_FOCUS(wxWindowMac::OnSetFocus)
88 EVT_KILL_FOCUS(wxWindowMac::OnSetFocus)
89 EVT_MOUSE_EVENTS(wxWindowMac::OnMouseEvent)
90 END_EVENT_TABLE()
91
92 #endif
93
94 #define wxMAC_DEBUG_REDRAW 0
95 #ifndef wxMAC_DEBUG_REDRAW
96 #define wxMAC_DEBUG_REDRAW 0
97 #endif
98
99 #define wxMAC_USE_THEME_BORDER 1
100
101 // ---------------------------------------------------------------------------
102 // Utility Routines to move between different coordinate systems
103 // ---------------------------------------------------------------------------
104
105 /*
106 * Right now we have the following setup :
107 * a border that is not part of the native control is always outside the
108 * control's border (otherwise we loose all native intelligence, future ways
109 * may be to have a second embedding control responsible for drawing borders
110 * and backgrounds eventually)
111 * so all this border calculations have to be taken into account when calling
112 * native methods or getting native oriented data
113 * so we have three coordinate systems here
114 * wx client coordinates
115 * wx window coordinates (including window frames)
116 * native coordinates
117 */
118
119 //
120 // originating from native control
121 //
122
123
124 void wxMacNativeToWindow( const wxWindow* window , RgnHandle handle )
125 {
126 OffsetRgn( handle , window->MacGetLeftBorderSize() , window->MacGetTopBorderSize() ) ;
127 }
128
129 void wxMacNativeToWindow( const wxWindow* window , Rect *rect )
130 {
131 OffsetRect( rect , window->MacGetLeftBorderSize() , window->MacGetTopBorderSize() ) ;
132 }
133
134 //
135 // directed towards native control
136 //
137
138 void wxMacWindowToNative( const wxWindow* window , RgnHandle handle )
139 {
140 OffsetRgn( handle , -window->MacGetLeftBorderSize() , -window->MacGetTopBorderSize() );
141 }
142
143 void wxMacWindowToNative( const wxWindow* window , Rect *rect )
144 {
145 OffsetRect( rect , -window->MacGetLeftBorderSize() , -window->MacGetTopBorderSize() ) ;
146 }
147
148
149 // ---------------------------------------------------------------------------
150 // Carbon Events
151 // ---------------------------------------------------------------------------
152
153 extern long wxMacTranslateKey(unsigned char key, unsigned char code) ;
154 pascal OSStatus wxMacSetupControlBackground( ControlRef iControl , SInt16 iMessage , SInt16 iDepth , Boolean iIsColor ) ;
155
156 #if TARGET_API_MAC_OSX
157
158 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_3
159 enum {
160 kEventControlVisibilityChanged = 157
161 };
162 #endif
163
164 #endif
165
166 static const EventTypeSpec eventList[] =
167 {
168 { kEventClassControl , kEventControlHit } ,
169 #if TARGET_API_MAC_OSX
170 { kEventClassControl , kEventControlDraw } ,
171 { kEventClassControl , kEventControlVisibilityChanged } ,
172 { kEventClassControl , kEventControlEnabledStateChanged } ,
173 { kEventClassControl , kEventControlHiliteChanged } ,
174 { kEventClassControl , kEventControlSetFocusPart } ,
175
176 { kEventClassService , kEventServiceGetTypes },
177 { kEventClassService , kEventServiceCopy },
178 { kEventClassService , kEventServicePaste },
179
180 // { kEventClassControl , kEventControlInvalidateForSizeChange } , // 10.3 only
181 // { kEventClassControl , kEventControlBoundsChanged } ,
182 #endif
183 } ;
184
185 static pascal OSStatus wxMacWindowControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
186 {
187 OSStatus result = eventNotHandledErr ;
188
189 wxMacCarbonEvent cEvent( event ) ;
190
191 ControlRef controlRef ;
192 wxWindowMac* thisWindow = (wxWindowMac*) data ;
193
194 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
195
196 switch( GetEventKind( event ) )
197 {
198 #if TARGET_API_MAC_OSX
199 case kEventControlDraw :
200 {
201 RgnHandle updateRgn = NULL ;
202 RgnHandle allocatedRgn = NULL ;
203 wxRegion visRegion = thisWindow->MacGetVisibleRegion() ;
204 if ( cEvent.GetParameter<RgnHandle>(kEventParamRgnHandle, &updateRgn) != noErr )
205 {
206 updateRgn = (RgnHandle) visRegion.GetWXHRGN() ;
207 }
208 else
209 {
210 if ( thisWindow->MacGetLeftBorderSize() != 0 || thisWindow->MacGetTopBorderSize() != 0 )
211 {
212 allocatedRgn = NewRgn() ;
213 CopyRgn( updateRgn , allocatedRgn ) ;
214 // hide the given region by the new region that must be shifted
215 wxMacNativeToWindow( thisWindow , allocatedRgn ) ;
216 updateRgn = allocatedRgn ;
217 }
218 }
219
220 #if 0
221 // in case we would need a coregraphics compliant background erase first
222 // now usable to track redraws
223 CGContextRef cgContext = cEvent.GetParameter<CGContextRef>(kEventParamCGContextRef) ;
224 if ( thisWindow->MacIsUserPane() )
225 {
226 static float color = 0.5 ;
227 static channel = 0 ;
228 HIRect bounds;
229 HIViewGetBounds( controlRef, &bounds );
230 CGContextSetRGBFillColor( cgContext, channel == 0 ? color : 0.5 ,
231 channel == 1 ? color : 0.5 , channel == 2 ? color : 0.5 , 1 );
232 CGContextFillRect( cgContext, bounds );
233 color += 0.1 ;
234 if ( color > 0.9 )
235 {
236 color = 0.5 ;
237 channel++ ;
238 if ( channel == 3 )
239 channel = 0 ;
240 }
241 }
242 #endif
243 if ( thisWindow->MacDoRedraw( updateRgn , cEvent.GetTicks() ) )
244 result = noErr ;
245 if ( allocatedRgn )
246 DisposeRgn( allocatedRgn ) ;
247 }
248 break ;
249 case kEventControlVisibilityChanged :
250 thisWindow->MacVisibilityChanged() ;
251 break ;
252 case kEventControlEnabledStateChanged :
253 thisWindow->MacEnabledStateChanged() ;
254 break ;
255 case kEventControlHiliteChanged :
256 thisWindow->MacHiliteChanged() ;
257 break ;
258 case kEventControlSetFocusPart :
259 {
260 Boolean focusEverything = false ;
261 ControlPartCode controlPart = cEvent.GetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode );
262 if ( cEvent.GetParameter<Boolean>(kEventParamControlFocusEverything , &focusEverything ) == noErr )
263 {
264 }
265 if ( controlPart == kControlFocusNoPart )
266 {
267 #if wxUSE_CARET
268 if ( thisWindow->GetCaret() )
269 {
270 thisWindow->GetCaret()->OnKillFocus();
271 }
272 #endif // wxUSE_CARET
273 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
274 event.SetEventObject(thisWindow);
275 thisWindow->GetEventHandler()->ProcessEvent(event) ;
276 }
277 else
278 {
279 // panel wants to track the window which was the last to have focus in it
280 wxChildFocusEvent eventFocus(thisWindow);
281 thisWindow->GetEventHandler()->ProcessEvent(eventFocus);
282
283 #if wxUSE_CARET
284 if ( thisWindow->GetCaret() )
285 {
286 thisWindow->GetCaret()->OnSetFocus();
287 }
288 #endif // wxUSE_CARET
289
290 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
291 event.SetEventObject(thisWindow);
292 thisWindow->GetEventHandler()->ProcessEvent(event) ;
293 }
294 if ( thisWindow->MacIsUserPane() )
295 result = noErr ;
296 }
297 break ;
298 #endif
299 case kEventControlHit :
300 {
301 result = thisWindow->MacControlHit( handler , event ) ;
302 }
303 break ;
304 default :
305 break ;
306 }
307 return result ;
308 }
309
310 static pascal OSStatus wxMacWindowServiceEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
311 {
312 OSStatus result = eventNotHandledErr ;
313
314 wxMacCarbonEvent cEvent( event ) ;
315
316 ControlRef controlRef ;
317 wxWindowMac* thisWindow = (wxWindowMac*) data ;
318 wxTextCtrl* textCtrl = wxDynamicCast( thisWindow , wxTextCtrl ) ;
319 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
320
321 switch( GetEventKind( event ) )
322 {
323 case kEventServiceGetTypes :
324 if( textCtrl )
325 {
326 long from, to ;
327 textCtrl->GetSelection( &from , &to ) ;
328
329 CFMutableArrayRef copyTypes = 0 , pasteTypes = 0;
330 if( from != to )
331 copyTypes = cEvent.GetParameter< CFMutableArrayRef >( kEventParamServiceCopyTypes , typeCFMutableArrayRef ) ;
332 if ( textCtrl->IsEditable() )
333 pasteTypes = cEvent.GetParameter< CFMutableArrayRef >( kEventParamServicePasteTypes , typeCFMutableArrayRef ) ;
334
335 static const OSType textDataTypes[] = { kTXNTextData /* , 'utxt' , 'PICT', 'MooV', 'AIFF' */ };
336 for ( size_t i = 0 ; i < WXSIZEOF(textDataTypes) ; ++i )
337 {
338 CFStringRef typestring = CreateTypeStringWithOSType(textDataTypes[i]);
339 if ( typestring )
340 {
341 if ( copyTypes )
342 CFArrayAppendValue (copyTypes, typestring) ;
343 if ( pasteTypes )
344 CFArrayAppendValue (pasteTypes, typestring) ;
345 CFRelease( typestring ) ;
346 }
347 }
348 result = noErr ;
349 }
350 break ;
351 case kEventServiceCopy :
352 if ( textCtrl )
353 {
354 long from, to ;
355 textCtrl->GetSelection( &from , &to ) ;
356 wxString val = textCtrl->GetValue() ;
357 val = val.Mid( from , to - from ) ;
358 ScrapRef scrapRef = cEvent.GetParameter< ScrapRef > ( kEventParamScrapRef , typeScrapRef ) ;
359 verify_noerr( ClearScrap( &scrapRef ) ) ;
360 verify_noerr( PutScrapFlavor( scrapRef , kTXNTextData , 0 , val.Length() , val.c_str() ) ) ;
361 result = noErr ;
362 }
363 break ;
364 case kEventServicePaste :
365 if ( textCtrl )
366 {
367 ScrapRef scrapRef = cEvent.GetParameter< ScrapRef > ( kEventParamScrapRef , typeScrapRef ) ;
368 Size textSize, pastedSize ;
369 verify_noerr( GetScrapFlavorSize (scrapRef, kTXNTextData, &textSize) ) ;
370 textSize++ ;
371 char *content = new char[textSize] ;
372 GetScrapFlavorData (scrapRef, kTXNTextData, &pastedSize, content );
373 content[textSize-1] = 0 ;
374 #if wxUSE_UNICODE
375 textCtrl->WriteText( wxString( content , wxConvLocal ) );
376 #else
377 textCtrl->WriteText( wxString( content ) ) ;
378 #endif
379 delete[] content ;
380 result = noErr ;
381 }
382 break ;
383 }
384
385 return result ;
386 }
387
388 pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
389 {
390 OSStatus result = eventNotHandledErr ;
391
392 switch ( GetEventClass( event ) )
393 {
394 case kEventClassControl :
395 result = wxMacWindowControlEventHandler( handler, event, data ) ;
396 break ;
397 case kEventClassService :
398 result = wxMacWindowServiceEventHandler( handler, event , data ) ;
399 default :
400 break ;
401 }
402 return result ;
403 }
404
405 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler )
406
407 // ---------------------------------------------------------------------------
408 // UserPane events for non OSX builds
409 // ---------------------------------------------------------------------------
410
411 static pascal void wxMacControlUserPaneDrawProc(ControlRef control, SInt16 part)
412 {
413 wxWindow * win = wxFindControlFromMacControl(control) ;
414 wxCHECK_RET( win , wxT("Callback from unkown control") ) ;
415 win->MacControlUserPaneDrawProc(part) ;
416 }
417
418 static pascal ControlPartCode wxMacControlUserPaneHitTestProc(ControlRef control, Point where)
419 {
420 wxWindow * win = wxFindControlFromMacControl(control) ;
421 wxCHECK_MSG( win , kControlNoPart , wxT("Callback from unkown control") ) ;
422 return win->MacControlUserPaneHitTestProc(where.h , where.v) ;
423 }
424
425 static pascal ControlPartCode wxMacControlUserPaneTrackingProc(ControlRef control, Point startPt, ControlActionUPP actionProc)
426 {
427 wxWindow * win = wxFindControlFromMacControl(control) ;
428 wxCHECK_MSG( win , kControlNoPart , wxT("Callback from unkown control") ) ;
429 return win->MacControlUserPaneTrackingProc( startPt.h , startPt.v , (void*) actionProc) ;
430 }
431
432 static pascal void wxMacControlUserPaneIdleProc(ControlRef control)
433 {
434 wxWindow * win = wxFindControlFromMacControl(control) ;
435 wxCHECK_RET( win , wxT("Callback from unkown control") ) ;
436 win->MacControlUserPaneIdleProc() ;
437 }
438
439 static pascal ControlPartCode wxMacControlUserPaneKeyDownProc(ControlRef control, SInt16 keyCode, SInt16 charCode, SInt16 modifiers)
440 {
441 wxWindow * win = wxFindControlFromMacControl(control) ;
442 wxCHECK_MSG( win , kControlNoPart , wxT("Callback from unkown control") ) ;
443 return win->MacControlUserPaneKeyDownProc(keyCode,charCode,modifiers) ;
444 }
445
446 static pascal void wxMacControlUserPaneActivateProc(ControlRef control, Boolean activating)
447 {
448 wxWindow * win = wxFindControlFromMacControl(control) ;
449 wxCHECK_RET( win , wxT("Callback from unkown control") ) ;
450 win->MacControlUserPaneActivateProc(activating) ;
451 }
452
453 static pascal ControlPartCode wxMacControlUserPaneFocusProc(ControlRef control, ControlFocusPart action)
454 {
455 wxWindow * win = wxFindControlFromMacControl(control) ;
456 wxCHECK_MSG( win , kControlNoPart , wxT("Callback from unkown control") ) ;
457 return win->MacControlUserPaneFocusProc(action) ;
458 }
459
460 static pascal void wxMacControlUserPaneBackgroundProc(ControlRef control, ControlBackgroundPtr info)
461 {
462 wxWindow * win = wxFindControlFromMacControl(control) ;
463 wxCHECK_RET( win , wxT("Callback from unkown control") ) ;
464 win->MacControlUserPaneBackgroundProc(info) ;
465 }
466
467 void wxWindowMac::MacControlUserPaneDrawProc(wxInt16 part)
468 {
469 RgnHandle rgn = NewRgn() ;
470 GetClip( rgn ) ;
471 wxMacWindowStateSaver sv( this ) ;
472 SectRgn( rgn , (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , rgn ) ;
473 MacDoRedraw( rgn , 0 ) ;
474 DisposeRgn( rgn ) ;
475 }
476
477 wxInt16 wxWindowMac::MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y)
478 {
479 return kControlNoPart ;
480 }
481
482 wxInt16 wxWindowMac::MacControlUserPaneTrackingProc(wxInt16 x, wxInt16 y, void* actionProc)
483 {
484 return kControlNoPart ;
485 }
486
487 void wxWindowMac::MacControlUserPaneIdleProc()
488 {
489 }
490
491 wxInt16 wxWindowMac::MacControlUserPaneKeyDownProc(wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers)
492 {
493 return kControlNoPart ;
494 }
495
496 void wxWindowMac::MacControlUserPaneActivateProc(bool activating)
497 {
498 }
499
500 wxInt16 wxWindowMac::MacControlUserPaneFocusProc(wxInt16 action)
501 {
502 return kControlNoPart ;
503 }
504
505 void wxWindowMac::MacControlUserPaneBackgroundProc(void* info)
506 {
507 }
508
509 ControlUserPaneDrawUPP gControlUserPaneDrawUPP = NULL ;
510 ControlUserPaneHitTestUPP gControlUserPaneHitTestUPP = NULL ;
511 ControlUserPaneTrackingUPP gControlUserPaneTrackingUPP = NULL ;
512 ControlUserPaneIdleUPP gControlUserPaneIdleUPP = NULL ;
513 ControlUserPaneKeyDownUPP gControlUserPaneKeyDownUPP = NULL ;
514 ControlUserPaneActivateUPP gControlUserPaneActivateUPP = NULL ;
515 ControlUserPaneFocusUPP gControlUserPaneFocusUPP = NULL ;
516 ControlUserPaneBackgroundUPP gControlUserPaneBackgroundUPP = NULL ;
517
518 // ===========================================================================
519 // implementation
520 // ===========================================================================
521
522 wxList wxWinMacControlList(wxKEY_INTEGER);
523
524 wxWindow *wxFindControlFromMacControl(ControlRef inControl )
525 {
526 wxNode *node = wxWinMacControlList.Find((long)inControl);
527 if (!node)
528 return NULL;
529 return (wxControl *)node->GetData();
530 }
531
532 void wxAssociateControlWithMacControl(ControlRef inControl, wxWindow *control)
533 {
534 // adding NULL ControlRef is (first) surely a result of an error and
535 // (secondly) breaks native event processing
536 wxCHECK_RET( inControl != (ControlRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
537
538 if ( !wxWinMacControlList.Find((long)inControl) )
539 wxWinMacControlList.Append((long)inControl, control);
540 }
541
542 void wxRemoveMacControlAssociation(wxWindow *control)
543 {
544 wxWinMacControlList.DeleteObject(control);
545 }
546
547 // UPP functions
548 ControlActionUPP wxMacLiveScrollbarActionUPP = NULL ;
549
550 ControlColorUPP wxMacSetupControlBackgroundUPP = NULL ;
551
552 // we have to setup the brush in the current port and return noErr
553 // or return an error code so that the control manager walks further up the
554 // hierarchy to find a correct background
555
556 pascal OSStatus wxMacSetupControlBackground( ControlRef iControl , SInt16 iMessage , SInt16 iDepth , Boolean iIsColor )
557 {
558 OSStatus status = paramErr ;
559 switch( iMessage )
560 {
561 case kControlMsgApplyTextColor :
562 break ;
563 case kControlMsgSetUpBackground :
564 {
565 wxWindow* wx = (wxWindow*) wxFindControlFromMacControl( iControl ) ;
566 if ( wx != NULL )
567 {
568 /*
569 const wxBrush &brush = wx->MacGetBackgroundBrush() ;
570 if ( brush.Ok() )
571 {
572 wxDC::MacSetupBackgroundForCurrentPort( brush ) ;
573 */
574 // this clipping is only needed for non HIView
575
576 RgnHandle clip = NewRgn() ;
577 int x = 0 , y = 0;
578
579 wx->MacWindowToRootWindow( &x,&y ) ;
580 CopyRgn( (RgnHandle) wx->MacGetVisibleRegion().GetWXHRGN() , clip ) ;
581 OffsetRgn( clip , x , y ) ;
582 SetClip( clip ) ;
583 DisposeRgn( clip ) ;
584
585 status = noErr ;
586 /*
587 }
588 else if ( wx->MacIsUserPane() )
589 {
590 // if we don't have a valid brush for such a control, we have to call the
591 // setup of our parent ourselves
592 status = SetUpControlBackground( (ControlRef) wx->GetParent()->GetHandle() , iDepth , iIsColor ) ;
593 }
594 */
595 }
596 }
597 break ;
598 default :
599 break ;
600 }
601 return status ;
602 }
603
604
605 pascal void wxMacLiveScrollbarActionProc( ControlRef control , ControlPartCode partCode ) ;
606 pascal void wxMacLiveScrollbarActionProc( ControlRef control , ControlPartCode partCode )
607 {
608 if ( partCode != 0)
609 {
610 wxWindow* wx = wxFindControlFromMacControl( control ) ;
611 if ( wx )
612 {
613 wx->MacHandleControlClick( (WXWidget) control , partCode , true /* stillDown */ ) ;
614 }
615 }
616 }
617
618 // ----------------------------------------------------------------------------
619 // constructors and such
620 // ----------------------------------------------------------------------------
621
622 wxWindowMac::wxWindowMac()
623 {
624 Init();
625 }
626
627 wxWindowMac::wxWindowMac(wxWindowMac *parent,
628 wxWindowID id,
629 const wxPoint& pos ,
630 const wxSize& size ,
631 long style ,
632 const wxString& name )
633 {
634 Init();
635 Create(parent, id, pos, size, style, name);
636 }
637
638 void wxWindowMac::Init()
639 {
640 m_peer = NULL ;
641 m_frozenness = 0 ;
642 #if WXWIN_COMPATIBILITY_2_4
643 m_backgroundTransparent = FALSE;
644 #endif
645
646 // as all windows are created with WS_VISIBLE style...
647 m_isShown = TRUE;
648
649 m_hScrollBar = NULL ;
650 m_vScrollBar = NULL ;
651 m_macBackgroundBrush = wxNullBrush ;
652
653 m_macIsUserPane = TRUE;
654
655 // make sure all proc ptrs are available
656
657 if ( gControlUserPaneDrawUPP == NULL )
658 {
659 gControlUserPaneDrawUPP = NewControlUserPaneDrawUPP( wxMacControlUserPaneDrawProc ) ;
660 gControlUserPaneHitTestUPP = NewControlUserPaneHitTestUPP( wxMacControlUserPaneHitTestProc ) ;
661 gControlUserPaneTrackingUPP = NewControlUserPaneTrackingUPP( wxMacControlUserPaneTrackingProc ) ;
662 gControlUserPaneIdleUPP = NewControlUserPaneIdleUPP( wxMacControlUserPaneIdleProc ) ;
663 gControlUserPaneKeyDownUPP = NewControlUserPaneKeyDownUPP( wxMacControlUserPaneKeyDownProc ) ;
664 gControlUserPaneActivateUPP = NewControlUserPaneActivateUPP( wxMacControlUserPaneActivateProc ) ;
665 gControlUserPaneFocusUPP = NewControlUserPaneFocusUPP( wxMacControlUserPaneFocusProc ) ;
666 gControlUserPaneBackgroundUPP = NewControlUserPaneBackgroundUPP( wxMacControlUserPaneBackgroundProc ) ;
667 }
668 if ( wxMacLiveScrollbarActionUPP == NULL )
669 {
670 wxMacLiveScrollbarActionUPP = NewControlActionUPP( wxMacLiveScrollbarActionProc );
671 }
672
673 if ( wxMacSetupControlBackgroundUPP == NULL )
674 {
675 wxMacSetupControlBackgroundUPP = NewControlColorUPP( wxMacSetupControlBackground ) ;
676 }
677
678 // we need a valid font for the encodings
679 wxWindowBase::SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
680 }
681
682 // Destructor
683 wxWindowMac::~wxWindowMac()
684 {
685 SendDestroyEvent();
686
687 m_isBeingDeleted = TRUE;
688
689 #ifndef __WXUNIVERSAL__
690 // VS: make sure there's no wxFrame with last focus set to us:
691 for ( wxWindow *win = GetParent(); win; win = win->GetParent() )
692 {
693 wxFrame *frame = wxDynamicCast(win, wxFrame);
694 if ( frame )
695 {
696 if ( frame->GetLastFocus() == this )
697 {
698 frame->SetLastFocus((wxWindow*)NULL);
699 }
700 break;
701 }
702 }
703 #endif // __WXUNIVERSAL__
704
705 // wxRemoveMacControlAssociation( this ) ;
706 // If we delete an item, we should initialize the parent panel,
707 // because it could now be invalid.
708 wxWindow *parent = GetParent() ;
709 if ( parent )
710 {
711 if (parent->GetDefaultItem() == (wxButton*) this)
712 parent->SetDefaultItem(NULL);
713 }
714 if ( m_peer && m_peer->Ok() )
715 {
716 // in case the callback might be called during destruction
717 wxRemoveMacControlAssociation( this) ;
718 // we currently are not using this hook
719 // ::SetControlColorProc( *m_peer , NULL ) ;
720 m_peer->Dispose() ;
721 }
722
723 if ( g_MacLastWindow == this )
724 {
725 g_MacLastWindow = NULL ;
726 }
727
728 wxFrame* frame = wxDynamicCast( wxGetTopLevelParent( this ) , wxFrame ) ;
729 if ( frame )
730 {
731 if ( frame->GetLastFocus() == this )
732 frame->SetLastFocus( NULL ) ;
733 }
734
735 DestroyChildren();
736
737 // delete our drop target if we've got one
738 #if wxUSE_DRAG_AND_DROP
739 if ( m_dropTarget != NULL )
740 {
741 delete m_dropTarget;
742 m_dropTarget = NULL;
743 }
744 #endif // wxUSE_DRAG_AND_DROP
745 delete m_peer ;
746 }
747
748 WXWidget wxWindowMac::GetHandle() const
749 {
750 return (WXWidget) m_peer->GetControlRef() ;
751 }
752
753
754 void wxWindowMac::MacInstallEventHandler( WXWidget control )
755 {
756 wxAssociateControlWithMacControl( (ControlRef) control , this ) ;
757 InstallControlEventHandler( (ControlRef) control , GetwxMacWindowEventHandlerUPP(),
758 GetEventTypeCount(eventList), eventList, this,
759 (EventHandlerRef *)&m_macControlEventHandler);
760
761 }
762
763 // Constructor
764 bool wxWindowMac::Create(wxWindowMac *parent, wxWindowID id,
765 const wxPoint& pos,
766 const wxSize& size,
767 long style,
768 const wxString& name)
769 {
770 wxCHECK_MSG( parent, FALSE, wxT("can't create wxWindowMac without parent") );
771
772 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
773 return FALSE;
774
775 parent->AddChild(this);
776
777 m_windowVariant = parent->GetWindowVariant() ;
778
779 if ( m_macIsUserPane )
780 {
781 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
782
783 UInt32 features = 0
784 | kControlSupportsEmbedding
785 // | kControlSupportsLiveFeedback
786 // | kControlHasSpecialBackground
787 // | kControlSupportsCalcBestRect
788 // | kControlHandlesTracking
789 | kControlSupportsFocus
790 // | kControlWantsActivate
791 // | kControlWantsIdle
792 ;
793
794 m_peer = new wxMacControl() ;
795 ::CreateUserPaneControl( MAC_WXHWND(GetParent()->MacGetTopLevelWindowRef()) , &bounds, features , m_peer->GetControlRefAddr() );
796
797
798 MacPostControlCreate(pos,size) ;
799 #if !TARGET_API_MAC_OSX
800 m_peer->SetData<ControlUserPaneDrawUPP>(kControlEntireControl,kControlUserPaneDrawProcTag,&gControlUserPaneDrawUPP) ;
801 m_peer->SetData<ControlUserPaneHitTestUPP>(kControlEntireControl,kControlUserPaneHitTestProcTag,&gControlUserPaneHitTestUPP) ;
802 m_peer->SetData<ControlUserPaneTrackingUPP>(kControlEntireControl,kControlUserPaneTrackingProcTag,&gControlUserPaneTrackingUPP) ;
803 m_peer->SetData<ControlUserPaneIdleUPP>(kControlEntireControl,kControlUserPaneIdleProcTag,&gControlUserPaneIdleUPP) ;
804 m_peer->SetData<ControlUserPaneKeyDownUPP>(kControlEntireControl,kControlUserPaneKeyDownProcTag,&gControlUserPaneKeyDownUPP) ;
805 m_peer->SetData<ControlUserPaneActivateUPP>(kControlEntireControl,kControlUserPaneActivateProcTag,&gControlUserPaneActivateUPP) ;
806 m_peer->SetData<ControlUserPaneFocusUPP>(kControlEntireControl,kControlUserPaneFocusProcTag,&gControlUserPaneFocusUPP) ;
807 m_peer->SetData<ControlUserPaneBackgroundUPP>(kControlEntireControl,kControlUserPaneBackgroundProcTag,&gControlUserPaneBackgroundUPP) ;
808 #endif
809 }
810 #ifndef __WXUNIVERSAL__
811 // Don't give scrollbars to wxControls unless they ask for them
812 if ( (! IsKindOf(CLASSINFO(wxControl)) && ! IsKindOf(CLASSINFO(wxStatusBar))) ||
813 (IsKindOf(CLASSINFO(wxControl)) && ( style & wxHSCROLL || style & wxVSCROLL)))
814 {
815 MacCreateScrollBars( style ) ;
816 }
817 #endif
818
819 wxWindowCreateEvent event(this);
820 GetEventHandler()->AddPendingEvent(event);
821
822 return TRUE;
823 }
824
825 void wxWindowMac::MacPostControlCreate(const wxPoint& pos, const wxSize& size)
826 {
827 wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid mac control") ) ;
828
829 m_peer->SetReference( (long) this ) ;
830
831 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() );
832
833 ControlRef container = (ControlRef) GetParent()->GetHandle() ;
834 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
835 ::EmbedControl( m_peer->GetControlRef() , container ) ;
836
837 // adjust font, controlsize etc
838 DoSetWindowVariant( m_windowVariant ) ;
839
840 #if !TARGET_API_MAC_OSX
841 // eventually we can fix some clipping issues be reactivating this hook
842 //if ( m_macIsUserPane )
843 // SetControlColorProc( m_peer->GetControlRef() , wxMacSetupControlBackgroundUPP ) ;
844 #endif
845 m_peer->SetTitle( wxStripMenuCodes(m_label) ) ;
846
847 if (!m_macIsUserPane)
848 {
849 SetInitialBestSize(size);
850 }
851
852 SetCursor( *wxSTANDARD_CURSOR ) ;
853 }
854
855 void wxWindowMac::DoSetWindowVariant( wxWindowVariant variant )
856 {
857 // Don't assert, in case we set the window variant before
858 // the window is created
859 // wxASSERT( m_peer->Ok() ) ;
860
861 m_windowVariant = variant ;
862
863 if (m_peer == NULL || !m_peer->Ok())
864 return;
865
866 ControlSize size ;
867 ThemeFontID themeFont = kThemeSystemFont ;
868
869 // we will get that from the settings later
870 // and make this NORMAL later, but first
871 // we have a few calculations that we must fix
872
873 switch ( variant )
874 {
875 case wxWINDOW_VARIANT_NORMAL :
876 size = kControlSizeNormal;
877 themeFont = kThemeSystemFont ;
878 break ;
879 case wxWINDOW_VARIANT_SMALL :
880 size = kControlSizeSmall;
881 themeFont = kThemeSmallSystemFont ;
882 break ;
883 case wxWINDOW_VARIANT_MINI :
884 if (UMAGetSystemVersion() >= 0x1030 )
885 {
886 // not always defined in the headers
887 size = 3 ;
888 themeFont = 109 ;
889 }
890 else
891 {
892 size = kControlSizeSmall;
893 themeFont = kThemeSmallSystemFont ;
894 }
895 break ;
896 case wxWINDOW_VARIANT_LARGE :
897 size = kControlSizeLarge;
898 themeFont = kThemeSystemFont ;
899 break ;
900 default:
901 wxFAIL_MSG(_T("unexpected window variant"));
902 break ;
903 }
904 m_peer->SetData<ControlSize>(kControlEntireControl, kControlSizeTag,&size ) ;
905
906 wxFont font ;
907 font.MacCreateThemeFont( themeFont ) ;
908 SetFont( font ) ;
909 }
910
911 void wxWindowMac::MacUpdateControlFont()
912 {
913 m_peer->SetFont( GetFont() , GetForegroundColour() , GetWindowStyle() ) ;
914 Refresh() ;
915 }
916
917 bool wxWindowMac::SetFont(const wxFont& font)
918 {
919 bool retval = !wxWindowBase::SetFont( font ) ;
920
921 MacUpdateControlFont() ;
922
923 return retval;
924 }
925
926 bool wxWindowMac::SetForegroundColour(const wxColour& col )
927 {
928 if ( !wxWindowBase::SetForegroundColour(col) )
929 return false ;
930
931 MacUpdateControlFont() ;
932
933 return true ;
934 }
935
936 bool wxWindowMac::SetBackgroundColour(const wxColour& col )
937 {
938 if ( !wxWindowBase::SetBackgroundColour(col) && m_hasBgCol )
939 return false ;
940
941 wxBrush brush ;
942 wxColour newCol(GetBackgroundColour());
943 if ( newCol == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE) )
944 {
945 brush.MacSetTheme( kThemeBrushDocumentWindowBackground ) ;
946 }
947 else if ( newCol == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE ) )
948 {
949 brush.MacSetTheme( kThemeBrushDialogBackgroundActive ) ;
950 }
951 else
952 {
953 brush.SetColour( newCol ) ;
954 }
955 MacSetBackgroundBrush( brush ) ;
956
957 MacUpdateControlFont() ;
958
959 return true ;
960 }
961
962 void wxWindowMac::MacSetBackgroundBrush( const wxBrush &brush )
963 {
964 m_macBackgroundBrush = brush ;
965 m_peer->SetBackground( brush ) ;
966 }
967
968 bool wxWindowMac::MacCanFocus() const
969 {
970 // there is currently no way to determine whether the window is running in full keyboard
971 // access mode, therefore we cannot rely on these features, yet the only other way would be
972 // to issue a SetKeyboardFocus event and verify after whether it succeeded, this would risk problems
973 // in event handlers...
974 UInt32 features = 0 ;
975 m_peer->GetFeatures( & features ) ;
976 return features & ( kControlSupportsFocus | kControlGetsFocusOnClick ) ;
977 }
978
979
980 void wxWindowMac::SetFocus()
981 {
982 if ( AcceptsFocus() )
983 {
984 #if !TARGET_API_MAC_OSX
985 wxWindow* former = FindFocus() ;
986 #endif
987 OSStatus err = m_peer->SetFocus( kControlFocusNextPart ) ;
988 // as we cannot rely on the control features to find out whether we are in full keyboard mode, we can only
989 // leave in case of an error
990 if ( err == errCouldntSetFocus )
991 return ;
992
993 #if !TARGET_API_MAC_OSX
994 // emulate carbon events when running under carbonlib where they are not natively available
995 if ( former )
996 {
997 EventRef evRef = NULL ;
998 verify_noerr( MacCreateEvent( NULL , kEventClassControl , kEventControlSetFocusPart , TicksToEventTime( TickCount() ) , kEventAttributeUserEvent ,
999 &evRef ) );
1000
1001 wxMacCarbonEvent cEvent( evRef ) ;
1002 cEvent.SetParameter<ControlRef>( kEventParamDirectObject , (ControlRef) former->GetHandle() ) ;
1003 cEvent.SetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode , kControlFocusNoPart ) ;
1004
1005 wxMacWindowEventHandler( NULL , evRef , former ) ;
1006 ReleaseEvent(evRef) ;
1007 }
1008 // send new focus event
1009 {
1010 EventRef evRef = NULL ;
1011 verify_noerr( MacCreateEvent( NULL , kEventClassControl , kEventControlSetFocusPart , TicksToEventTime( TickCount() ) , kEventAttributeUserEvent ,
1012 &evRef ) );
1013
1014 wxMacCarbonEvent cEvent( evRef ) ;
1015 cEvent.SetParameter<ControlRef>( kEventParamDirectObject , (ControlRef) GetHandle() ) ;
1016 cEvent.SetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode , kControlFocusNextPart ) ;
1017
1018 wxMacWindowEventHandler( NULL , evRef , this ) ;
1019 ReleaseEvent(evRef) ;
1020 }
1021 #endif
1022 }
1023 }
1024
1025
1026 void wxWindowMac::DoCaptureMouse()
1027 {
1028 wxTheApp->s_captureWindow = this ;
1029 }
1030
1031 wxWindow* wxWindowBase::GetCapture()
1032 {
1033 return wxTheApp->s_captureWindow ;
1034 }
1035
1036 void wxWindowMac::DoReleaseMouse()
1037 {
1038 wxTheApp->s_captureWindow = NULL ;
1039 }
1040
1041 #if wxUSE_DRAG_AND_DROP
1042
1043 void wxWindowMac::SetDropTarget(wxDropTarget *pDropTarget)
1044 {
1045 if ( m_dropTarget != 0 ) {
1046 delete m_dropTarget;
1047 }
1048
1049 m_dropTarget = pDropTarget;
1050 if ( m_dropTarget != 0 )
1051 {
1052 // TODO
1053 }
1054 }
1055
1056 #endif
1057
1058 // Old style file-manager drag&drop
1059 void wxWindowMac::DragAcceptFiles(bool accept)
1060 {
1061 // TODO
1062 }
1063
1064 // Returns the size of the native control. In the case of the toplevel window
1065 // this is the content area root control
1066
1067 void wxWindowMac::MacGetPositionAndSizeFromControl(int& x, int& y,
1068 int& w, int& h) const
1069 {
1070 Rect bounds ;
1071 m_peer->GetRect( &bounds ) ;
1072
1073
1074 x = bounds.left ;
1075 y = bounds.top ;
1076 w = bounds.right - bounds.left ;
1077 h = bounds.bottom - bounds.top ;
1078 }
1079
1080 // From a wx position / size calculate the appropriate size of the native control
1081
1082 bool wxWindowMac::MacGetBoundsForControl(const wxPoint& pos,
1083 const wxSize& size,
1084 int& x, int& y,
1085 int& w, int& h , bool adjustOrigin ) const
1086 {
1087 // the desired size, minus the border pixels gives the correct size of the control
1088
1089 x = (int)pos.x;
1090 y = (int)pos.y;
1091 // todo the default calls may be used as soon as PostCreateControl Is moved here
1092 w = size.x ; // WidthDefault( size.x );
1093 h = size.y ; // HeightDefault( size.y ) ;
1094 #if !TARGET_API_MAC_OSX
1095 GetParent()->MacWindowToRootWindow( &x , &y ) ;
1096 #endif
1097
1098 x += MacGetLeftBorderSize() ;
1099 y += MacGetTopBorderSize() ;
1100 w -= MacGetLeftBorderSize() + MacGetRightBorderSize() ;
1101 h -= MacGetTopBorderSize() + MacGetBottomBorderSize() ;
1102
1103 if ( adjustOrigin )
1104 AdjustForParentClientOrigin( x , y ) ;
1105 return true ;
1106 }
1107
1108 // Get window size (not client size)
1109 void wxWindowMac::DoGetSize(int *x, int *y) const
1110 {
1111 // take the size of the control and add the borders that have to be drawn outside
1112 int x1 , y1 , w1 , h1 ;
1113
1114 MacGetPositionAndSizeFromControl( x1 , y1, w1 ,h1 ) ;
1115
1116 w1 += MacGetLeftBorderSize() + MacGetRightBorderSize() ;
1117 h1 += MacGetTopBorderSize() + MacGetBottomBorderSize() ;
1118
1119 if(x) *x = w1 ;
1120 if(y) *y = h1 ;
1121 }
1122
1123 // get the position of the bounds of this window in client coordinates of its parent
1124 void wxWindowMac::DoGetPosition(int *x, int *y) const
1125 {
1126 int x1 , y1 , w1 ,h1 ;
1127 MacGetPositionAndSizeFromControl( x1 , y1, w1 ,h1 ) ;
1128 x1 -= MacGetLeftBorderSize() ;
1129 y1 -= MacGetTopBorderSize() ;
1130 // to non-client
1131 #if !TARGET_API_MAC_OSX
1132 if ( !GetParent()->IsTopLevel() )
1133 {
1134 Rect bounds ;
1135 GetControlBounds( (ControlRef) GetParent()->GetHandle() , &bounds ) ;
1136 x1 -= bounds.left ;
1137 y1 -= bounds.top ;
1138 }
1139 #endif
1140 if ( !IsTopLevel() )
1141 {
1142 wxWindow *parent = GetParent();
1143 if ( parent )
1144 {
1145 // we must first adjust it twice, as otherwise it gets lost by the clientareaorigin fix
1146 x1 += 2 * parent->MacGetLeftBorderSize() ;
1147 y1 += 2 * parent->MacGetTopBorderSize() ;
1148 wxPoint pt(parent->GetClientAreaOrigin());
1149 x1 -= pt.x ;
1150 y1 -= pt.y ;
1151 }
1152 }
1153 if(x) *x = x1 ;
1154 if(y) *y = y1 ;
1155 }
1156
1157 void wxWindowMac::DoScreenToClient(int *x, int *y) const
1158 {
1159 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
1160
1161 wxCHECK_RET( window , wxT("TopLevel Window Missing") ) ;
1162
1163 {
1164 Point localwhere = {0,0} ;
1165
1166 if(x) localwhere.h = * x ;
1167 if(y) localwhere.v = * y ;
1168
1169 QDGlobalToLocalPoint( GetWindowPort( window ) , &localwhere ) ;
1170 if(x) *x = localwhere.h ;
1171 if(y) *y = localwhere.v ;
1172
1173 }
1174 MacRootWindowToWindow( x , y ) ;
1175
1176 wxPoint origin = GetClientAreaOrigin() ;
1177 if(x) *x -= origin.x ;
1178 if(y) *y -= origin.y ;
1179 }
1180
1181 void wxWindowMac::DoClientToScreen(int *x, int *y) const
1182 {
1183 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
1184 wxCHECK_RET( window , wxT("TopLevel Window Missing") ) ;
1185
1186 wxPoint origin = GetClientAreaOrigin() ;
1187 if(x) *x += origin.x ;
1188 if(y) *y += origin.y ;
1189
1190 MacWindowToRootWindow( x , y ) ;
1191
1192 {
1193 Point localwhere = { 0,0 };
1194 if(x) localwhere.h = * x ;
1195 if(y) localwhere.v = * y ;
1196 QDLocalToGlobalPoint( GetWindowPort( window ) , &localwhere ) ;
1197 if(x) *x = localwhere.h ;
1198 if(y) *y = localwhere.v ;
1199 }
1200 }
1201
1202 void wxWindowMac::MacClientToRootWindow( int *x , int *y ) const
1203 {
1204 wxPoint origin = GetClientAreaOrigin() ;
1205 if(x) *x += origin.x ;
1206 if(y) *y += origin.y ;
1207
1208 MacWindowToRootWindow( x , y ) ;
1209 }
1210
1211 void wxWindowMac::MacRootWindowToClient( int *x , int *y ) const
1212 {
1213 MacRootWindowToWindow( x , y ) ;
1214
1215 wxPoint origin = GetClientAreaOrigin() ;
1216 if(x) *x -= origin.x ;
1217 if(y) *y -= origin.y ;
1218 }
1219
1220 void wxWindowMac::MacWindowToRootWindow( int *x , int *y ) const
1221 {
1222 #if TARGET_API_MAC_OSX
1223 wxPoint pt ;
1224 if ( x ) pt.x = *x ;
1225 if ( y ) pt.y = *y ;
1226
1227 if ( !IsTopLevel() )
1228 {
1229 wxTopLevelWindowMac* top = MacGetTopLevelWindow();
1230 if (top)
1231 {
1232 pt.x -= MacGetLeftBorderSize() ;
1233 pt.y -= MacGetTopBorderSize() ;
1234 wxMacControl::Convert( &pt , m_peer , top->m_peer ) ;
1235 }
1236 }
1237
1238 if ( x ) *x = (int) pt.x ;
1239 if ( y ) *y = (int) pt.y ;
1240 #else
1241 if ( !IsTopLevel() )
1242 {
1243 Rect bounds ;
1244 m_peer->GetRect( &bounds ) ;
1245 if(x) *x += bounds.left - MacGetLeftBorderSize() ;
1246 if(y) *y += bounds.top - MacGetTopBorderSize() ;
1247 }
1248 #endif
1249 }
1250
1251 void wxWindowMac::MacWindowToRootWindow( short *x , short *y ) const
1252 {
1253 int x1 , y1 ;
1254 if ( x ) x1 = *x ;
1255 if ( y ) y1 = *y ;
1256 MacWindowToRootWindow( &x1 , &y1 ) ;
1257 if ( x ) *x = x1 ;
1258 if ( y ) *y = y1 ;
1259 }
1260
1261 void wxWindowMac::MacRootWindowToWindow( int *x , int *y ) const
1262 {
1263 #if TARGET_API_MAC_OSX
1264 wxPoint pt ;
1265 if ( x ) pt.x = *x ;
1266 if ( y ) pt.y = *y ;
1267
1268 if ( !IsTopLevel() )
1269 {
1270 wxMacControl::Convert( &pt , MacGetTopLevelWindow()->m_peer , m_peer ) ;
1271 pt.x += MacGetLeftBorderSize() ;
1272 pt.y += MacGetTopBorderSize() ;
1273 }
1274
1275 if ( x ) *x = (int) pt.x ;
1276 if ( y ) *y = (int) pt.y ;
1277 #else
1278 if ( !IsTopLevel() )
1279 {
1280 Rect bounds ;
1281 m_peer->GetRect( &bounds ) ;
1282 if(x) *x -= bounds.left + MacGetLeftBorderSize() ;
1283 if(y) *y -= bounds.top + MacGetTopBorderSize() ;
1284 }
1285 #endif
1286 }
1287
1288 void wxWindowMac::MacRootWindowToWindow( short *x , short *y ) const
1289 {
1290 int x1 , y1 ;
1291 if ( x ) x1 = *x ;
1292 if ( y ) y1 = *y ;
1293 MacRootWindowToWindow( &x1 , &y1 ) ;
1294 if ( x ) *x = x1 ;
1295 if ( y ) *y = y1 ;
1296 }
1297
1298 void wxWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1299 {
1300 RgnHandle rgn = NewRgn() ;
1301 Rect content ;
1302 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) == noErr )
1303 {
1304 GetRegionBounds( rgn , &content ) ;
1305 DisposeRgn( rgn ) ;
1306 }
1307 else
1308 {
1309 m_peer->GetRect( &content ) ;
1310 }
1311 Rect structure ;
1312 m_peer->GetRect( &structure ) ;
1313 #if !TARGET_API_MAC_OSX
1314 OffsetRect( &content , -structure.left , -structure.top ) ;
1315 #endif
1316 left = content.left - structure.left ;
1317 top = content.top - structure.top ;
1318 right = structure.right - content.right ;
1319 bottom = structure.bottom - content.bottom ;
1320 }
1321
1322 wxSize wxWindowMac::DoGetSizeFromClientSize( const wxSize & size ) const
1323 {
1324 wxSize sizeTotal = size;
1325
1326 RgnHandle rgn = NewRgn() ;
1327
1328 Rect content ;
1329
1330 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) == noErr )
1331 {
1332 GetRegionBounds( rgn , &content ) ;
1333 DisposeRgn( rgn ) ;
1334 }
1335 else
1336 {
1337 m_peer->GetRect( &content ) ;
1338 }
1339 Rect structure ;
1340 m_peer->GetRect( &structure ) ;
1341 #if !TARGET_API_MAC_OSX
1342 OffsetRect( &content , -structure.left , -structure.top ) ;
1343 #endif
1344
1345 sizeTotal.x += (structure.right - structure.left) - (content.right - content.left) ;
1346 sizeTotal.y += (structure.bottom - structure.top) - (content.bottom - content.top ) ;
1347
1348 sizeTotal.x += MacGetLeftBorderSize( ) + MacGetRightBorderSize( ) ;
1349 sizeTotal.y += MacGetTopBorderSize( ) + MacGetBottomBorderSize( ) ;
1350
1351 return sizeTotal;
1352 }
1353
1354
1355 // Get size *available for subwindows* i.e. excluding menu bar etc.
1356 void wxWindowMac::DoGetClientSize(int *x, int *y) const
1357 {
1358 int ww, hh;
1359
1360 RgnHandle rgn = NewRgn() ;
1361 Rect content ;
1362 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) == noErr )
1363 {
1364 GetRegionBounds( rgn , &content ) ;
1365 DisposeRgn( rgn ) ;
1366 }
1367 else
1368 {
1369 m_peer->GetRect( &content ) ;
1370 }
1371 #if !TARGET_API_MAC_OSX
1372 Rect structure ;
1373 m_peer->GetRect( &structure ) ;
1374 OffsetRect( &content , -structure.left , -structure.top ) ;
1375 #endif
1376 ww = content.right - content.left ;
1377 hh = content.bottom - content.top ;
1378 /*
1379 ww -= MacGetLeftBorderSize( ) + MacGetRightBorderSize( ) ;
1380 hh -= MacGetTopBorderSize( ) + MacGetBottomBorderSize( );
1381 */
1382 /*
1383 if ( (m_vScrollBar && m_vScrollBar->IsShown()) || (m_hScrollBar && m_hScrollBar->IsShown()) )
1384 {
1385 int x1 = 0 ;
1386 int y1 = 0 ;
1387 int w ;
1388 int h ;
1389 GetSize( &w , &h ) ;
1390
1391 MacClientToRootWindow( &x1 , &y1 ) ;
1392 MacClientToRootWindow( &w , &h ) ;
1393
1394 wxWindowMac *iter = (wxWindowMac*)this ;
1395
1396 int totW = 10000 , totH = 10000;
1397 while( iter )
1398 {
1399 if ( iter->IsTopLevel() )
1400 {
1401 iter->GetSize( &totW , &totH ) ;
1402 break ;
1403 }
1404
1405 iter = iter->GetParent() ;
1406 }
1407
1408 if (m_hScrollBar && m_hScrollBar->IsShown() )
1409 {
1410 hh -= m_hScrollBar->GetSize().y ; // MAC_SCROLLBAR_SIZE ;
1411 if ( h-y1 >= totH )
1412 {
1413 hh += 1 ;
1414 }
1415 }
1416 if (m_vScrollBar && m_vScrollBar->IsShown() )
1417 {
1418 ww -= m_vScrollBar->GetSize().x ; // MAC_SCROLLBAR_SIZE;
1419 if ( w-x1 >= totW )
1420 {
1421 ww += 1 ;
1422 }
1423 }
1424 }
1425 */
1426 if (m_hScrollBar && m_hScrollBar->IsShown() )
1427 {
1428 hh -= m_hScrollBar->GetSize().y ; // MAC_SCROLLBAR_SIZE ;
1429 }
1430 if (m_vScrollBar && m_vScrollBar->IsShown() )
1431 {
1432 ww -= m_vScrollBar->GetSize().x ; // MAC_SCROLLBAR_SIZE;
1433 }
1434 if(x) *x = ww;
1435 if(y) *y = hh;
1436
1437 }
1438
1439 bool wxWindowMac::SetCursor(const wxCursor& cursor)
1440 {
1441 if (m_cursor == cursor)
1442 return FALSE;
1443
1444 if (wxNullCursor == cursor)
1445 {
1446 if ( ! wxWindowBase::SetCursor( *wxSTANDARD_CURSOR ) )
1447 return FALSE ;
1448 }
1449 else
1450 {
1451 if ( ! wxWindowBase::SetCursor( cursor ) )
1452 return FALSE ;
1453 }
1454
1455 wxASSERT_MSG( m_cursor.Ok(),
1456 wxT("cursor must be valid after call to the base version"));
1457
1458
1459 wxWindowMac *mouseWin = 0 ;
1460 {
1461 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
1462 CGrafPtr savePort ;
1463 Boolean swapped = QDSwapPort( GetWindowPort( window ) , &savePort ) ;
1464
1465 // TODO If we ever get a GetCurrentEvent.. replacement for the mouse
1466 // position, use it...
1467
1468 Point pt ;
1469 GetMouse( &pt ) ;
1470 ControlPartCode part ;
1471 ControlRef control ;
1472 control = wxMacFindControlUnderMouse( pt , window , &part ) ;
1473 if ( control )
1474 mouseWin = wxFindControlFromMacControl( control ) ;
1475
1476 if ( swapped )
1477 QDSwapPort( savePort , NULL ) ;
1478 }
1479
1480 if ( mouseWin == this && !wxIsBusy() )
1481 {
1482 m_cursor.MacInstall() ;
1483 }
1484
1485 return TRUE ;
1486 }
1487
1488 #if wxUSE_MENUS
1489 bool wxWindowMac::DoPopupMenu(wxMenu *menu, int x, int y)
1490 {
1491 menu->SetInvokingWindow(this);
1492 menu->UpdateUI();
1493
1494 if ( x == -1 && y == -1 )
1495 {
1496 wxPoint mouse = wxGetMousePosition();
1497 x = mouse.x; y = mouse.y;
1498 }
1499 else
1500 {
1501 ClientToScreen( &x , &y ) ;
1502 }
1503
1504 menu->MacBeforeDisplay( true ) ;
1505 long menuResult = ::PopUpMenuSelect((MenuHandle) menu->GetHMenu() ,y,x, 0) ;
1506 if ( HiWord(menuResult) != 0 )
1507 {
1508 MenuCommand id ;
1509 GetMenuItemCommandID( GetMenuHandle(HiWord(menuResult)) , LoWord(menuResult) , &id ) ;
1510 wxMenuItem* item = NULL ;
1511 wxMenu* realmenu ;
1512 item = menu->FindItem(id, &realmenu) ;
1513 if (item->IsCheckable())
1514 {
1515 item->Check( !item->IsChecked() ) ;
1516 }
1517 menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
1518 }
1519 menu->MacAfterDisplay( true ) ;
1520
1521 menu->SetInvokingWindow(NULL);
1522
1523 return TRUE;
1524 }
1525 #endif
1526
1527 // ----------------------------------------------------------------------------
1528 // tooltips
1529 // ----------------------------------------------------------------------------
1530
1531 #if wxUSE_TOOLTIPS
1532
1533 void wxWindowMac::DoSetToolTip(wxToolTip *tooltip)
1534 {
1535 wxWindowBase::DoSetToolTip(tooltip);
1536
1537 if ( m_tooltip )
1538 m_tooltip->SetWindow(this);
1539 }
1540
1541 #endif // wxUSE_TOOLTIPS
1542
1543 void wxWindowMac::DoMoveWindow(int x, int y, int width, int height)
1544 {
1545 // this is never called for a toplevel window, so we know we have a parent
1546 int former_x , former_y , former_w, former_h ;
1547
1548 // Get true coordinates of former position
1549 DoGetPosition( &former_x , &former_y ) ;
1550 DoGetSize( &former_w , &former_h ) ;
1551
1552 wxWindow *parent = GetParent();
1553 if ( parent )
1554 {
1555 wxPoint pt(parent->GetClientAreaOrigin());
1556 former_x += pt.x ;
1557 former_y += pt.y ;
1558 }
1559
1560 int actualWidth = width;
1561 int actualHeight = height;
1562 int actualX = x;
1563 int actualY = y;
1564
1565 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
1566 actualWidth = m_minWidth;
1567 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
1568 actualHeight = m_minHeight;
1569 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
1570 actualWidth = m_maxWidth;
1571 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
1572 actualHeight = m_maxHeight;
1573
1574 bool doMove = false ;
1575 bool doResize = false ;
1576
1577 if ( actualX != former_x || actualY != former_y )
1578 {
1579 doMove = true ;
1580 }
1581 if ( actualWidth != former_w || actualHeight != former_h )
1582 {
1583 doResize = true ;
1584 }
1585
1586 if ( doMove || doResize )
1587 {
1588 // we don't adjust twice for the origin
1589 Rect r = wxMacGetBoundsForControl(this , wxPoint( actualX,actualY), wxSize( actualWidth, actualHeight ) , false ) ;
1590 #if TARGET_API_MAC_OSX
1591 // this is in window relative coordinate, as this parent may have a border, its physical position is offset by this border
1592 if ( ! GetParent()->IsTopLevel() )
1593 {
1594 r.left -= GetParent()->MacGetLeftBorderSize() ;
1595 r.top -= GetParent()->MacGetTopBorderSize() ;
1596 r.right -= GetParent()->MacGetLeftBorderSize() ;
1597 r.bottom -= GetParent()->MacGetTopBorderSize() ;
1598 }
1599 #endif
1600 bool vis = m_peer->IsVisible();
1601
1602 // the HIViewSetFrame call itself should invalidate the areas, but when testing with the UnicodeTextCtrl it does not !
1603 if ( vis )
1604 m_peer->SetVisibility( false , true ) ;
1605 m_peer->SetRect( &r ) ;
1606 if ( vis )
1607 m_peer->SetVisibility( true , true ) ;
1608
1609 MacRepositionScrollBars() ;
1610 if ( doMove )
1611 {
1612 wxPoint point(actualX,actualY);
1613 wxMoveEvent event(point, m_windowId);
1614 event.SetEventObject(this);
1615 GetEventHandler()->ProcessEvent(event) ;
1616 }
1617 if ( doResize )
1618 {
1619 MacRepositionScrollBars() ;
1620 wxSize size(actualWidth, actualHeight);
1621 wxSizeEvent event(size, m_windowId);
1622 event.SetEventObject(this);
1623 GetEventHandler()->ProcessEvent(event);
1624 }
1625 }
1626
1627 }
1628
1629 wxSize wxWindowMac::DoGetBestSize() const
1630 {
1631 if ( m_macIsUserPane || IsTopLevel() )
1632 return wxWindowBase::DoGetBestSize() ;
1633
1634 Rect bestsize = { 0 , 0 , 0 , 0 } ;
1635 int bestWidth, bestHeight ;
1636 m_peer->GetBestRect( &bestsize ) ;
1637
1638 if ( EmptyRect( &bestsize ) )
1639 {
1640 bestsize.left = bestsize.top = 0 ;
1641 bestsize.right = 16 ;
1642 bestsize.bottom = 16 ;
1643 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
1644 {
1645 bestsize.bottom = 16 ;
1646 }
1647 else if ( IsKindOf( CLASSINFO( wxSpinButton ) ) )
1648 {
1649 bestsize.bottom = 24 ;
1650 }
1651 else
1652 {
1653 // return wxWindowBase::DoGetBestSize() ;
1654 }
1655 }
1656
1657 bestWidth = bestsize.right - bestsize.left ;
1658 bestHeight = bestsize.bottom - bestsize.top ;
1659 if ( bestHeight < 10 )
1660 bestHeight = 13 ;
1661
1662 return wxSize(bestWidth, bestHeight);
1663 }
1664
1665
1666 // set the size of the window: if the dimensions are positive, just use them,
1667 // but if any of them is equal to -1, it means that we must find the value for
1668 // it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
1669 // which case -1 is a valid value for x and y)
1670 //
1671 // If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
1672 // the width/height to best suit our contents, otherwise we reuse the current
1673 // width/height
1674 void wxWindowMac::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1675 {
1676 // get the current size and position...
1677 int currentX, currentY;
1678 GetPosition(&currentX, &currentY);
1679
1680 int currentW,currentH;
1681 GetSize(&currentW, &currentH);
1682
1683 // ... and don't do anything (avoiding flicker) if it's already ok
1684 if ( x == currentX && y == currentY &&
1685 width == currentW && height == currentH && ( height != -1 && width != -1 ) )
1686 {
1687 // TODO REMOVE
1688 MacRepositionScrollBars() ; // we might have a real position shift
1689 return;
1690 }
1691
1692 if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1693 x = currentX;
1694 if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1695 y = currentY;
1696
1697 AdjustForParentClientOrigin(x, y, sizeFlags);
1698
1699 wxSize size(-1, -1);
1700 if ( width == -1 )
1701 {
1702 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
1703 {
1704 size = DoGetBestSize();
1705 width = size.x;
1706 }
1707 else
1708 {
1709 // just take the current one
1710 width = currentW;
1711 }
1712 }
1713
1714 if ( height == -1 )
1715 {
1716 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
1717 {
1718 if ( size.x == -1 )
1719 {
1720 size = DoGetBestSize();
1721 }
1722 //else: already called DoGetBestSize() above
1723
1724 height = size.y;
1725 }
1726 else
1727 {
1728 // just take the current one
1729 height = currentH;
1730 }
1731 }
1732
1733 DoMoveWindow(x, y, width, height);
1734
1735 }
1736
1737 wxPoint wxWindowMac::GetClientAreaOrigin() const
1738 {
1739 RgnHandle rgn = NewRgn() ;
1740 Rect content ;
1741 m_peer->GetRegion( kControlContentMetaPart , rgn ) ;
1742 GetRegionBounds( rgn , &content ) ;
1743 DisposeRgn( rgn ) ;
1744 #if !TARGET_API_MAC_OSX
1745 // if the content rgn is empty / not supported
1746 // don't attempt to correct the coordinates to wxWindow relative ones
1747 if (!::EmptyRect( &content ) )
1748 {
1749 Rect structure ;
1750 m_peer->GetRect( &structure ) ;
1751 OffsetRect( &content , -structure.left , -structure.top ) ;
1752 }
1753 #endif
1754
1755 return wxPoint( content.left + MacGetLeftBorderSize( ) , content.top + MacGetTopBorderSize( ) );
1756 }
1757
1758 void wxWindowMac::DoSetClientSize(int clientwidth, int clientheight)
1759 {
1760 if ( clientheight != -1 || clientheight != -1 )
1761 {
1762 int currentclientwidth , currentclientheight ;
1763 int currentwidth , currentheight ;
1764
1765 GetClientSize( &currentclientwidth , &currentclientheight ) ;
1766 GetSize( &currentwidth , &currentheight ) ;
1767
1768 DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth ,
1769 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
1770 }
1771 }
1772
1773 void wxWindowMac::SetTitle(const wxString& title)
1774 {
1775 m_label = wxStripMenuCodes(title) ;
1776
1777 if ( m_peer && m_peer->Ok() )
1778 {
1779 m_peer->SetTitle( m_label ) ;
1780 }
1781 Refresh() ;
1782 }
1783
1784 wxString wxWindowMac::GetTitle() const
1785 {
1786 return m_label ;
1787 }
1788
1789 bool wxWindowMac::Show(bool show)
1790 {
1791 if ( !wxWindowBase::Show(show) )
1792 return FALSE;
1793
1794 // TODO use visibilityChanged Carbon Event for OSX
1795 bool former = MacIsReallyShown() ;
1796
1797 m_peer->SetVisibility( show , true ) ;
1798 if ( former != MacIsReallyShown() )
1799 MacPropagateVisibilityChanged() ;
1800 return TRUE;
1801 }
1802
1803 bool wxWindowMac::Enable(bool enable)
1804 {
1805 wxASSERT( m_peer->Ok() ) ;
1806 if ( !wxWindowBase::Enable(enable) )
1807 return FALSE;
1808
1809 bool former = MacIsReallyEnabled() ;
1810 m_peer->Enable( enable ) ;
1811
1812 if ( former != MacIsReallyEnabled() )
1813 MacPropagateEnabledStateChanged() ;
1814 return TRUE;
1815 }
1816
1817 //
1818 // status change propagations (will be not necessary for OSX later )
1819 //
1820
1821 void wxWindowMac::MacPropagateVisibilityChanged()
1822 {
1823 #if !TARGET_API_MAC_OSX
1824 MacVisibilityChanged() ;
1825
1826 wxWindowListNode *node = GetChildren().GetFirst();
1827 while ( node )
1828 {
1829 wxWindowMac *child = node->GetData();
1830 if ( child->IsShown() )
1831 child->MacPropagateVisibilityChanged( ) ;
1832 node = node->GetNext();
1833 }
1834 #endif
1835 }
1836
1837 void wxWindowMac::MacPropagateEnabledStateChanged( )
1838 {
1839 #if !TARGET_API_MAC_OSX
1840 MacEnabledStateChanged() ;
1841
1842 wxWindowListNode *node = GetChildren().GetFirst();
1843 while ( node )
1844 {
1845 wxWindowMac *child = node->GetData();
1846 if ( child->IsEnabled() )
1847 child->MacPropagateEnabledStateChanged() ;
1848 node = node->GetNext();
1849 }
1850 #endif
1851 }
1852
1853 void wxWindowMac::MacPropagateHiliteChanged( )
1854 {
1855 #if !TARGET_API_MAC_OSX
1856 MacHiliteChanged() ;
1857
1858 wxWindowListNode *node = GetChildren().GetFirst();
1859 while ( node )
1860 {
1861 wxWindowMac *child = node->GetData();
1862 // if ( child->IsEnabled() )
1863 child->MacPropagateHiliteChanged() ;
1864 node = node->GetNext();
1865 }
1866 #endif
1867 }
1868
1869 //
1870 // status change notifications
1871 //
1872
1873 void wxWindowMac::MacVisibilityChanged()
1874 {
1875 }
1876
1877 void wxWindowMac::MacHiliteChanged()
1878 {
1879 }
1880
1881 void wxWindowMac::MacEnabledStateChanged()
1882 {
1883 }
1884
1885 //
1886 // status queries on the inherited window's state
1887 //
1888
1889 bool wxWindowMac::MacIsReallyShown()
1890 {
1891 // only under OSX the visibility of the TLW is taken into account
1892 #if TARGET_API_MAC_OSX
1893 if ( m_peer && m_peer->Ok() )
1894 return m_peer->IsVisible();
1895 #endif
1896 wxWindow* win = this ;
1897 while( win->IsShown() )
1898 {
1899 if ( win->IsTopLevel() )
1900 return true ;
1901
1902 win = win->GetParent() ;
1903 if ( win == NULL )
1904 return true ;
1905
1906 } ;
1907 return false ;
1908 }
1909
1910 bool wxWindowMac::MacIsReallyEnabled()
1911 {
1912 return m_peer->IsEnabled() ;
1913 }
1914
1915 bool wxWindowMac::MacIsReallyHilited()
1916 {
1917 return m_peer->IsActive();
1918 }
1919
1920 void wxWindowMac::MacFlashInvalidAreas()
1921 {
1922 #if TARGET_API_MAC_OSX
1923 HIViewFlashDirtyArea( (WindowRef) MacGetTopLevelWindowRef() ) ;
1924 #endif
1925 }
1926
1927 //
1928 //
1929 //
1930
1931 int wxWindowMac::GetCharHeight() const
1932 {
1933 wxClientDC dc ( (wxWindowMac*)this ) ;
1934 return dc.GetCharHeight() ;
1935 }
1936
1937 int wxWindowMac::GetCharWidth() const
1938 {
1939 wxClientDC dc ( (wxWindowMac*)this ) ;
1940 return dc.GetCharWidth() ;
1941 }
1942
1943 void wxWindowMac::GetTextExtent(const wxString& string, int *x, int *y,
1944 int *descent, int *externalLeading, const wxFont *theFont ) const
1945 {
1946 const wxFont *fontToUse = theFont;
1947 if ( !fontToUse )
1948 fontToUse = &m_font;
1949
1950 wxClientDC dc( (wxWindowMac*) this ) ;
1951 long lx,ly,ld,le ;
1952 dc.GetTextExtent( string , &lx , &ly , &ld, &le, (wxFont *)fontToUse ) ;
1953 if ( externalLeading )
1954 *externalLeading = le ;
1955 if ( descent )
1956 *descent = ld ;
1957 if ( x )
1958 *x = lx ;
1959 if ( y )
1960 *y = ly ;
1961 }
1962
1963 /*
1964 * Rect is given in client coordinates, for further reading, read wxTopLevelWindowMac::InvalidateRect
1965 * we always intersect with the entire window, not only with the client area
1966 */
1967
1968 void wxWindowMac::Refresh(bool eraseBack, const wxRect *rect)
1969 {
1970 if ( m_peer == NULL )
1971 return ;
1972
1973 #if TARGET_API_MAC_OSX
1974 if ( rect == NULL )
1975 m_peer->SetNeedsDisplay( true ) ;
1976 else
1977 {
1978 RgnHandle update = NewRgn() ;
1979 SetRectRgn( update , rect->x , rect->y , rect->x + rect->width , rect->y + rect->height ) ;
1980 SectRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , update , update ) ;
1981 wxPoint origin = GetClientAreaOrigin() ;
1982 OffsetRgn( update, origin.x , origin.y ) ;
1983 // right now this is wx' window coordinates, as our native peer does not have borders, this is
1984 // inset
1985 OffsetRgn( update , -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
1986 m_peer->SetNeedsDisplay( true , update) ;
1987 }
1988 #else
1989 /*
1990 RgnHandle updateRgn = NewRgn() ;
1991 if ( rect == NULL )
1992 {
1993 CopyRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , updateRgn ) ;
1994 }
1995 else
1996 {
1997 SetRectRgn( updateRgn , rect->x , rect->y , rect->x + rect->width , rect->y + rect->height ) ;
1998 SectRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , updateRgn , updateRgn ) ;
1999 }
2000 InvalWindowRgn( (WindowRef) MacGetTopLevelWindowRef() , updateRgn ) ;
2001 DisposeRgn(updateRgn) ;
2002 */
2003 if ( m_peer->IsVisible())
2004 {
2005 m_peer->SetVisibility( false , false ) ;
2006 m_peer->SetVisibility( true , true ) ;
2007 }
2008 /*
2009 if ( MacGetTopLevelWindow() == NULL )
2010 return ;
2011
2012 if ( !m_peer->IsVisible())
2013 return ;
2014
2015 wxPoint client = GetClientAreaOrigin();
2016 int x1 = -client.x;
2017 int y1 = -client.y;
2018 int x2 = m_width - client.x;
2019 int y2 = m_height - client.y;
2020
2021 if (IsKindOf( CLASSINFO(wxButton)))
2022 {
2023 // buttons have an "aura"
2024 y1 -= 5;
2025 x1 -= 5;
2026 y2 += 5;
2027 x2 += 5;
2028 }
2029
2030 Rect clientrect = { y1, x1, y2, x2 };
2031
2032 if ( rect )
2033 {
2034 Rect r = { rect->y , rect->x , rect->y + rect->height , rect->x + rect->width } ;
2035 SectRect( &clientrect , &r , &clientrect ) ;
2036 }
2037
2038 if ( !EmptyRect( &clientrect ) )
2039 {
2040 int top = 0 , left = 0 ;
2041
2042 MacClientToRootWindow( &left , &top ) ;
2043 OffsetRect( &clientrect , left , top ) ;
2044
2045 MacGetTopLevelWindow()->MacInvalidate( &clientrect , eraseBack ) ;
2046 }
2047 */
2048 #endif
2049 }
2050
2051 void wxWindowMac::Freeze()
2052 {
2053 #if TARGET_API_MAC_OSX
2054 if ( !m_frozenness++ )
2055 {
2056 m_peer->SetDrawingEnabled( false ) ;
2057 }
2058 #endif
2059 }
2060
2061
2062 void wxWindowMac::Thaw()
2063 {
2064 #if TARGET_API_MAC_OSX
2065 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
2066
2067 if ( !--m_frozenness )
2068 {
2069 m_peer->SetDrawingEnabled( true ) ;
2070 m_peer->InvalidateWithChildren() ;
2071 }
2072 #endif
2073 }
2074
2075 void wxWindowMac::MacRedrawControl()
2076 {
2077 /*
2078 if ( *m_peer && MacGetTopLevelWindowRef() && m_peer->IsVisible())
2079 {
2080 #if TARGET_API_MAC_CARBON
2081 Update() ;
2082 #else
2083 wxClientDC dc(this) ;
2084 wxMacPortSetter helper(&dc) ;
2085 wxMacWindowClipper clipper(this) ;
2086 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
2087 UMADrawControl( *m_peer ) ;
2088 #endif
2089 }
2090 */
2091 }
2092
2093 /* TODO
2094 void wxWindowMac::OnPaint(wxPaintEvent& event)
2095 {
2096 // why don't we skip that here ?
2097 }
2098 */
2099
2100 wxWindowMac *wxGetActiveWindow()
2101 {
2102 // actually this is a windows-only concept
2103 return NULL;
2104 }
2105
2106 // Coordinates relative to the window
2107 void wxWindowMac::WarpPointer (int x_pos, int y_pos)
2108 {
2109 // We really don't move the mouse programmatically under Mac.
2110 }
2111
2112 void wxWindowMac::OnEraseBackground(wxEraseEvent& event)
2113 {
2114 #if TARGET_API_MAC_OSX
2115 if ( m_macBackgroundBrush.Ok() == false || m_macBackgroundBrush.GetStyle() == wxTRANSPARENT )
2116 {
2117 event.Skip() ;
2118 }
2119 else
2120 #endif
2121 event.GetDC()->Clear() ;
2122 }
2123
2124 void wxWindowMac::OnNcPaint( wxNcPaintEvent& event )
2125 {
2126 wxWindowDC dc(this) ;
2127 wxMacPortSetter helper(&dc) ;
2128
2129 MacPaintBorders( dc.m_macLocalOrigin.x , dc.m_macLocalOrigin.y) ;
2130 }
2131
2132 int wxWindowMac::GetScrollPos(int orient) const
2133 {
2134 if ( orient == wxHORIZONTAL )
2135 {
2136 if ( m_hScrollBar )
2137 return m_hScrollBar->GetThumbPosition() ;
2138 }
2139 else
2140 {
2141 if ( m_vScrollBar )
2142 return m_vScrollBar->GetThumbPosition() ;
2143 }
2144 return 0;
2145 }
2146
2147 // This now returns the whole range, not just the number
2148 // of positions that we can scroll.
2149 int wxWindowMac::GetScrollRange(int orient) const
2150 {
2151 if ( orient == wxHORIZONTAL )
2152 {
2153 if ( m_hScrollBar )
2154 return m_hScrollBar->GetRange() ;
2155 }
2156 else
2157 {
2158 if ( m_vScrollBar )
2159 return m_vScrollBar->GetRange() ;
2160 }
2161 return 0;
2162 }
2163
2164 int wxWindowMac::GetScrollThumb(int orient) const
2165 {
2166 if ( orient == wxHORIZONTAL )
2167 {
2168 if ( m_hScrollBar )
2169 return m_hScrollBar->GetThumbSize() ;
2170 }
2171 else
2172 {
2173 if ( m_vScrollBar )
2174 return m_vScrollBar->GetThumbSize() ;
2175 }
2176 return 0;
2177 }
2178
2179 void wxWindowMac::SetScrollPos(int orient, int pos, bool refresh)
2180 {
2181 if ( orient == wxHORIZONTAL )
2182 {
2183 if ( m_hScrollBar )
2184 m_hScrollBar->SetThumbPosition( pos ) ;
2185 }
2186 else
2187 {
2188 if ( m_vScrollBar )
2189 m_vScrollBar->SetThumbPosition( pos ) ;
2190 }
2191 }
2192
2193 void wxWindowMac::MacPaintBorders( int left , int top )
2194 {
2195 if( IsTopLevel() )
2196 return ;
2197
2198 int major,minor;
2199 wxGetOsVersion( &major, &minor );
2200
2201 RGBColor darkShadow = { 0x0000, 0x0000 , 0x0000 } ;
2202 RGBColor lightShadow = { 0x4444, 0x4444 , 0x4444 } ;
2203 // OS X has lighter border edges than classic:
2204 if (major >= 10)
2205 {
2206 darkShadow.red = 0x8E8E;
2207 darkShadow.green = 0x8E8E;
2208 darkShadow.blue = 0x8E8E;
2209 lightShadow.red = 0xBDBD;
2210 lightShadow.green = 0xBDBD;
2211 lightShadow.blue = 0xBDBD;
2212 }
2213
2214 PenNormal() ;
2215
2216 int w , h ;
2217 GetSize( &w , &h ) ;
2218 Rect rect = { top , left , h + top , w + left } ;
2219 if (HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
2220 {
2221 #if wxMAC_USE_THEME_BORDER
2222 SInt32 border = 0 ;
2223 GetThemeMetric( kThemeMetricEditTextFrameOutset , &border ) ;
2224 InsetRect( &rect , border , border );
2225 DrawThemeEditTextFrame(&rect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
2226 #else
2227 RGBColor white = { 0xFFFF, 0xFFFF , 0xFFFF } ;
2228 RGBColor face = { 0xDDDD, 0xDDDD , 0xDDDD } ;
2229
2230 bool sunken = HasFlag( wxSUNKEN_BORDER ) ;
2231 RGBForeColor( &face );
2232 MoveTo( left + 0 , top + h - 2 );
2233 LineTo( left + 0 , top + 0 );
2234 LineTo( left + w - 2 , top + 0 );
2235
2236 MoveTo( left + 2 , top + h - 3 );
2237 LineTo( left + w - 3 , top + h - 3 );
2238 LineTo( left + w - 3 , top + 2 );
2239
2240 RGBForeColor( sunken ? &face : &darkShadow );
2241 MoveTo( left + 0 , top + h - 1 );
2242 LineTo( left + w - 1 , top + h - 1 );
2243 LineTo( left + w - 1 , top + 0 );
2244
2245 RGBForeColor( sunken ? &lightShadow : &white );
2246 MoveTo( left + 1 , top + h - 3 );
2247 LineTo( left + 1, top + 1 );
2248 LineTo( left + w - 3 , top + 1 );
2249
2250 RGBForeColor( sunken ? &white : &lightShadow );
2251 MoveTo( left + 1 , top + h - 2 );
2252 LineTo( left + w - 2 , top + h - 2 );
2253 LineTo( left + w - 2 , top + 1 );
2254
2255 RGBForeColor( sunken ? &darkShadow : &face );
2256 MoveTo( left + 2 , top + h - 4 );
2257 LineTo( left + 2 , top + 2 );
2258 LineTo( left + w - 4 , top + 2 );
2259 #endif
2260 }
2261 else if (HasFlag(wxSIMPLE_BORDER))
2262 {
2263 Rect rect = { top , left , h + top , w + left } ;
2264 RGBForeColor( &darkShadow ) ;
2265 FrameRect( &rect ) ;
2266 }
2267 }
2268
2269 void wxWindowMac::RemoveChild( wxWindowBase *child )
2270 {
2271 if ( child == m_hScrollBar )
2272 m_hScrollBar = NULL ;
2273 if ( child == m_vScrollBar )
2274 m_vScrollBar = NULL ;
2275
2276 wxWindowBase::RemoveChild( child ) ;
2277 }
2278
2279 // New function that will replace some of the above.
2280 void wxWindowMac::SetScrollbar(int orient, int pos, int thumbVisible,
2281 int range, bool refresh)
2282 {
2283 if ( orient == wxHORIZONTAL )
2284 {
2285 if ( m_hScrollBar )
2286 {
2287 if ( range == 0 || thumbVisible >= range )
2288 {
2289 if ( m_hScrollBar->IsShown() )
2290 m_hScrollBar->Show(false) ;
2291 }
2292 else
2293 {
2294 if ( !m_hScrollBar->IsShown() )
2295 m_hScrollBar->Show(true) ;
2296 m_hScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
2297 }
2298 }
2299 }
2300 else
2301 {
2302 if ( m_vScrollBar )
2303 {
2304 if ( range == 0 || thumbVisible >= range )
2305 {
2306 if ( m_vScrollBar->IsShown() )
2307 m_vScrollBar->Show(false) ;
2308 }
2309 else
2310 {
2311 if ( !m_vScrollBar->IsShown() )
2312 m_vScrollBar->Show(true) ;
2313 m_vScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
2314 }
2315 }
2316 }
2317 MacRepositionScrollBars() ;
2318 }
2319
2320 // Does a physical scroll
2321 void wxWindowMac::ScrollWindow(int dx, int dy, const wxRect *rect)
2322 {
2323 if( dx == 0 && dy ==0 )
2324 return ;
2325
2326
2327 {
2328
2329 int width , height ;
2330 GetClientSize( &width , &height ) ;
2331 #if TARGET_API_MAC_OSX
2332 // note there currently is a bug in OSX which makes inefficient refreshes in case an entire control
2333 // area is scrolled, this does not occur if width and height are 2 pixels less,
2334 // TODO write optimal workaround
2335 wxRect scrollrect( MacGetLeftBorderSize() , MacGetTopBorderSize() , width , height ) ;
2336 if ( rect )
2337 {
2338 scrollrect.Intersect( *rect ) ;
2339 }
2340 if ( m_peer->GetNeedsDisplay() )
2341 {
2342 // becuase HIViewScrollRect does not scroll the already invalidated area we have two options
2343 // either immediate redraw or full invalidate
2344 #if 1
2345 // is the better overall solution, as it does not slow down scrolling
2346 m_peer->SetNeedsDisplay( true ) ;
2347 #else
2348 // this would be the preferred version for fast drawing controls
2349 if( UMAGetSystemVersion() < 0x1030 )
2350 Update() ;
2351 else
2352 HIViewRender(m_peer->GetControlRef()) ;
2353 #endif
2354 }
2355 // as the native control might be not a 0/0 wx window coordinates, we have to offset
2356 scrollrect.Offset( -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
2357 m_peer->ScrollRect( scrollrect , dx , dy ) ;
2358 #else
2359
2360 wxPoint pos;
2361 pos.x = pos.y = 0;
2362
2363 Rect scrollrect;
2364 RgnHandle updateRgn = NewRgn() ;
2365
2366 {
2367 wxClientDC dc(this) ;
2368 wxMacPortSetter helper(&dc) ;
2369
2370 m_peer->GetRect( &scrollrect ) ;
2371 scrollrect.top += MacGetTopBorderSize() ;
2372 scrollrect.left += MacGetLeftBorderSize() ;
2373 scrollrect.bottom = scrollrect.top + height ;
2374 scrollrect.right = scrollrect.left + width ;
2375
2376 if ( rect )
2377 {
2378 Rect r = { dc.YLOG2DEVMAC(rect->y) , dc.XLOG2DEVMAC(rect->x) , dc.YLOG2DEVMAC(rect->y + rect->height) ,
2379 dc.XLOG2DEVMAC(rect->x + rect->width) } ;
2380 SectRect( &scrollrect , &r , &scrollrect ) ;
2381 }
2382 ScrollRect( &scrollrect , dx , dy , updateRgn ) ;
2383 }
2384 // ScrollWindowRect( (WindowRef) MacGetTopLevelWindowRef() , &scrollrect , dx , dy , kScrollWindowInvalidate, updateRgn ) ;
2385 #endif
2386 }
2387
2388 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
2389 {
2390 wxWindowMac *child = node->GetData();
2391 if (child == m_vScrollBar) continue;
2392 if (child == m_hScrollBar) continue;
2393 if (child->IsTopLevel()) continue;
2394
2395 int x,y;
2396 child->GetPosition( &x, &y );
2397 int w,h;
2398 child->GetSize( &w, &h );
2399 if (rect)
2400 {
2401 wxRect rc(x,y,w,h);
2402 if (rect->Intersects(rc))
2403 child->SetSize( x+dx, y+dy, w, h );
2404 }
2405 else
2406 {
2407 child->SetSize( x+dx, y+dy, w, h );
2408 }
2409 }
2410 }
2411
2412 void wxWindowMac::MacOnScroll(wxScrollEvent &event )
2413 {
2414 if ( event.m_eventObject == m_vScrollBar || event.m_eventObject == m_hScrollBar )
2415 {
2416 wxScrollWinEvent wevent;
2417 wevent.SetPosition(event.GetPosition());
2418 wevent.SetOrientation(event.GetOrientation());
2419 wevent.m_eventObject = this;
2420
2421 if (event.m_eventType == wxEVT_SCROLL_TOP)
2422 wevent.m_eventType = wxEVT_SCROLLWIN_TOP;
2423 else if (event.m_eventType == wxEVT_SCROLL_BOTTOM)
2424 wevent.m_eventType = wxEVT_SCROLLWIN_BOTTOM;
2425 else if (event.m_eventType == wxEVT_SCROLL_LINEUP)
2426 wevent.m_eventType = wxEVT_SCROLLWIN_LINEUP;
2427 else if (event.m_eventType == wxEVT_SCROLL_LINEDOWN)
2428 wevent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN;
2429 else if (event.m_eventType == wxEVT_SCROLL_PAGEUP)
2430 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEUP;
2431 else if (event.m_eventType == wxEVT_SCROLL_PAGEDOWN)
2432 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEDOWN;
2433 else if (event.m_eventType == wxEVT_SCROLL_THUMBTRACK)
2434 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBTRACK;
2435 else if (event.m_eventType == wxEVT_SCROLL_THUMBRELEASE)
2436 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBRELEASE;
2437
2438 GetEventHandler()->ProcessEvent(wevent);
2439 }
2440 }
2441
2442 // Get the window with the focus
2443 wxWindowMac *wxWindowBase::FindFocus()
2444 {
2445 ControlRef control ;
2446 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
2447 return wxFindControlFromMacControl( control ) ;
2448 }
2449
2450 void wxWindowMac::OnSetFocus(wxFocusEvent& event)
2451 {
2452 // panel wants to track the window which was the last to have focus in it,
2453 // so we want to set ourselves as the window which last had focus
2454 //
2455 // notice that it's also important to do it upwards the tree becaus
2456 // otherwise when the top level panel gets focus, it won't set it back to
2457 // us, but to some other sibling
2458
2459 // CS:don't know if this is still needed:
2460 //wxChildFocusEvent eventFocus(this);
2461 //(void)GetEventHandler()->ProcessEvent(eventFocus);
2462
2463 if ( MacGetTopLevelWindow() && m_peer->NeedsFocusRect() )
2464 {
2465 wxMacWindowStateSaver sv( this ) ;
2466
2467 int w , h ;
2468 int x , y ;
2469 x = y = 0 ;
2470 MacWindowToRootWindow( &x , &y ) ;
2471 GetSize( &w , &h ) ;
2472 Rect rect = {y , x , h + y , w + x } ;
2473
2474 if ( event.GetEventType() == wxEVT_SET_FOCUS )
2475 DrawThemeFocusRect( &rect , true ) ;
2476 else
2477 DrawThemeFocusRect( &rect , false ) ;
2478 }
2479
2480 event.Skip();
2481 }
2482
2483 void wxWindowMac::OnInternalIdle()
2484 {
2485 // This calls the UI-update mechanism (querying windows for
2486 // menu/toolbar/control state information)
2487 if (wxUpdateUIEvent::CanUpdate(this))
2488 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2489 }
2490
2491 // Raise the window to the top of the Z order
2492 void wxWindowMac::Raise()
2493 {
2494 m_peer->SetZOrder( true , NULL ) ;
2495 }
2496
2497 // Lower the window to the bottom of the Z order
2498 void wxWindowMac::Lower()
2499 {
2500 m_peer->SetZOrder( false , NULL ) ;
2501 }
2502
2503
2504 // static wxWindow *gs_lastWhich = NULL;
2505
2506 bool wxWindowMac::MacSetupCursor( const wxPoint& pt)
2507 {
2508 // first trigger a set cursor event
2509
2510 wxPoint clientorigin = GetClientAreaOrigin() ;
2511 wxSize clientsize = GetClientSize() ;
2512 wxCursor cursor ;
2513 if ( wxRect2DInt( clientorigin.x , clientorigin.y , clientsize.x , clientsize.y ).Contains( wxPoint2DInt( pt ) ) )
2514 {
2515 wxSetCursorEvent event( pt.x , pt.y );
2516
2517 bool processedEvtSetCursor = GetEventHandler()->ProcessEvent(event);
2518 if ( processedEvtSetCursor && event.HasCursor() )
2519 {
2520 cursor = event.GetCursor() ;
2521 }
2522 else
2523 {
2524
2525 // the test for processedEvtSetCursor is here to prevent using m_cursor
2526 // if the user code caught EVT_SET_CURSOR() and returned nothing from
2527 // it - this is a way to say that our cursor shouldn't be used for this
2528 // point
2529 if ( !processedEvtSetCursor && m_cursor.Ok() )
2530 {
2531 cursor = m_cursor ;
2532 }
2533 if ( wxIsBusy() )
2534 {
2535 }
2536 else
2537 {
2538 if ( !GetParent() )
2539 cursor = *wxSTANDARD_CURSOR ;
2540 }
2541 }
2542 if ( cursor.Ok() )
2543 cursor.MacInstall() ;
2544 }
2545 return cursor.Ok() ;
2546 }
2547
2548 wxString wxWindowMac::MacGetToolTipString( wxPoint &pt )
2549 {
2550 if ( m_tooltip )
2551 {
2552 return m_tooltip->GetTip() ;
2553 }
2554 return wxEmptyString ;
2555 }
2556
2557 void wxWindowMac::Update()
2558 {
2559 #if TARGET_API_MAC_OSX
2560 WindowRef window = (WindowRef)MacGetTopLevelWindowRef() ;
2561 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
2562 // for composited windows this also triggers a redraw of all
2563 // invalid views in the window
2564 if( UMAGetSystemVersion() >= 0x1030 )
2565 HIWindowFlush(window) ;
2566 else
2567 #endif
2568 {
2569 // the only way to trigger the redrawing on earlier systems is to call
2570 // ReceiveNextEvent
2571
2572 EventRef currentEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
2573 UInt32 currentEventClass = 0 ;
2574 UInt32 currentEventKind = 0 ;
2575 if ( currentEvent != NULL )
2576 {
2577 currentEventClass = ::GetEventClass( currentEvent ) ;
2578 currentEventKind = ::GetEventKind( currentEvent ) ;
2579 }
2580 if ( currentEventClass != kEventClassMenu )
2581 {
2582 // when tracking a menu, strange redraw errors occur if we flush now, so leave..
2583
2584 EventRef theEvent;
2585 OSStatus status = noErr ;
2586 status = ReceiveNextEvent( 0 , NULL , kEventDurationNoWait , false , &theEvent ) ;
2587 }
2588 else
2589 m_peer->SetNeedsDisplay( true ) ;
2590 }
2591 #else
2592 ::Draw1Control( m_peer->GetControlRef() ) ;
2593 #endif
2594 }
2595
2596 wxTopLevelWindowMac* wxWindowMac::MacGetTopLevelWindow() const
2597 {
2598 wxTopLevelWindowMac* win = NULL ;
2599 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
2600 if ( window )
2601 {
2602 win = wxFindWinFromMacWindow( window ) ;
2603 }
2604 return win ;
2605 }
2606 wxRegion wxWindowMac::MacGetVisibleRegion( bool includeOuterStructures )
2607 {
2608
2609 Rect r ;
2610 RgnHandle visRgn = NewRgn() ;
2611 RgnHandle tempRgn = NewRgn() ;
2612 if ( m_peer->IsVisible())
2613 {
2614 m_peer->GetRect( &r ) ;
2615 r.left -= MacGetLeftBorderSize() ;
2616 r.top -= MacGetTopBorderSize() ;
2617 r.bottom += MacGetBottomBorderSize() ;
2618 r.right += MacGetRightBorderSize() ;
2619
2620 if (! MacGetTopLevelWindow()->MacUsesCompositing() )
2621 {
2622 MacRootWindowToWindow( &r.left , & r.top ) ;
2623 MacRootWindowToWindow( &r.right , & r.bottom ) ;
2624 }
2625 else
2626 {
2627 r.right -= r.left ;
2628 r.bottom -= r.top ;
2629 r.left = 0 ;
2630 r.top = 0 ;
2631 }
2632 if ( includeOuterStructures )
2633 InsetRect( &r , -3 , -3 ) ;
2634 RectRgn( visRgn , &r ) ;
2635
2636 if ( !IsTopLevel() )
2637 {
2638 wxWindow* child = this ;
2639 wxWindow* parent = child->GetParent() ;
2640 while( parent )
2641 {
2642 int x , y ;
2643 wxSize size ;
2644 // we have to find a better clipping algorithm here, in order not to clip things
2645 // positioned like status and toolbar
2646 if ( 1 /* parent->IsTopLevel() && child->IsKindOf( CLASSINFO( wxToolBar ) ) */ )
2647 {
2648 size = parent->GetSize() ;
2649 x = y = 0 ;
2650 }
2651 else
2652 {
2653 size = parent->GetClientSize() ;
2654 wxPoint origin = parent->GetClientAreaOrigin() ;
2655 x = origin.x ;
2656 y = origin.y ;
2657 }
2658 parent->MacWindowToRootWindow( &x, &y ) ;
2659 MacRootWindowToWindow( &x , &y ) ;
2660
2661 SetRectRgn( tempRgn ,
2662 x + parent->MacGetLeftBorderSize() , y + parent->MacGetTopBorderSize() ,
2663 x + size.x - parent->MacGetRightBorderSize(),
2664 y + size.y - parent->MacGetBottomBorderSize()) ;
2665
2666 SectRgn( visRgn , tempRgn , visRgn ) ;
2667 if ( parent->IsTopLevel() )
2668 break ;
2669 child = parent ;
2670 parent = child->GetParent() ;
2671 }
2672 }
2673 }
2674
2675 wxRegion vis = visRgn ;
2676 DisposeRgn( visRgn ) ;
2677 DisposeRgn( tempRgn ) ;
2678 return vis ;
2679 }
2680
2681 /*
2682 This function must not change the updatergn !
2683 */
2684 bool wxWindowMac::MacDoRedraw( WXHRGN updatergnr , long time )
2685 {
2686 RgnHandle updatergn = (RgnHandle) updatergnr ;
2687 bool handled = false ;
2688 Rect updatebounds ;
2689 GetRegionBounds( updatergn , &updatebounds ) ;
2690
2691 if ( !EmptyRgn(updatergn) )
2692 {
2693 RgnHandle newupdate = NewRgn() ;
2694 wxSize point = GetClientSize() ;
2695 wxPoint origin = GetClientAreaOrigin() ;
2696 SetRectRgn( newupdate , origin.x , origin.y , origin.x + point.x , origin.y+point.y ) ;
2697 SectRgn( newupdate , updatergn , newupdate ) ;
2698
2699 {
2700 wxWindowDC dc(this);
2701 if (!EmptyRgn(newupdate))
2702 dc.SetClippingRegion(wxRegion(newupdate));
2703
2704 wxEraseEvent eevent( GetId(), &dc );
2705 eevent.SetEventObject( this );
2706 GetEventHandler()->ProcessEvent( eevent );
2707 }
2708
2709 // calculate a client-origin version of the update rgn and set m_updateRegion to that
2710 OffsetRgn( newupdate , -origin.x , -origin.y ) ;
2711 m_updateRegion = newupdate ;
2712 DisposeRgn( newupdate ) ;
2713
2714 if ( !m_updateRegion.Empty() )
2715 {
2716 // paint the window itself
2717 wxPaintEvent event;
2718 event.m_timeStamp = time ;
2719 event.SetEventObject(this);
2720 handled = GetEventHandler()->ProcessEvent(event);
2721
2722 // paint custom borders
2723 wxNcPaintEvent eventNc( GetId() );
2724 eventNc.SetEventObject( this );
2725 GetEventHandler()->ProcessEvent( eventNc );
2726 }
2727 }
2728 return handled ;
2729 }
2730
2731 void wxWindowMac::MacRedraw( WXHRGN updatergnr , long time, bool erase)
2732 {
2733 RgnHandle updatergn = (RgnHandle) updatergnr ;
2734 // updatergn is always already clipped to our boundaries
2735 // if we are in compositing mode then it is in relative to the upper left of the control
2736 // if we are in non-compositing, then it is relatvie to the uppder left of the content area
2737 // of the toplevel window
2738 // it is in window coordinates, not in client coordinates
2739
2740 // ownUpdateRgn is the area that this window has to repaint, it is in window coordinates
2741 RgnHandle ownUpdateRgn = NewRgn() ;
2742 CopyRgn( updatergn , ownUpdateRgn ) ;
2743
2744 if ( MacGetTopLevelWindow()->MacUsesCompositing() == false )
2745 {
2746 Rect bounds;
2747 m_peer->GetRectInWindowCoords( &bounds );
2748 RgnHandle controlRgn = NewRgn();
2749 RectRgn( controlRgn, &bounds );
2750 //KO: This sets the ownUpdateRgn to the area of this control that is inside
2751 // the window update region
2752 SectRgn( ownUpdateRgn, controlRgn, ownUpdateRgn );
2753 DisposeRgn( controlRgn );
2754
2755 //KO: convert ownUpdateRgn to local coordinates
2756 OffsetRgn( ownUpdateRgn, -bounds.left, -bounds.top );
2757 }
2758
2759 MacDoRedraw( ownUpdateRgn , time ) ;
2760 DisposeRgn( ownUpdateRgn ) ;
2761
2762 }
2763
2764 WXWindow wxWindowMac::MacGetTopLevelWindowRef() const
2765 {
2766 wxWindowMac *iter = (wxWindowMac*)this ;
2767
2768 while( iter )
2769 {
2770 if ( iter->IsTopLevel() )
2771 return ((wxTopLevelWindow*)iter)->MacGetWindowRef() ;
2772
2773 iter = iter->GetParent() ;
2774 }
2775 wxASSERT_MSG( 1 , wxT("No valid mac root window") ) ;
2776 return NULL ;
2777 }
2778
2779 void wxWindowMac::MacCreateScrollBars( long style )
2780 {
2781 wxASSERT_MSG( m_vScrollBar == NULL && m_hScrollBar == NULL , wxT("attempt to create window twice") ) ;
2782
2783 if ( style & ( wxVSCROLL | wxHSCROLL ) )
2784 {
2785 bool hasBoth = ( style & wxVSCROLL ) && ( style & wxHSCROLL ) ;
2786 int scrlsize = MAC_SCROLLBAR_SIZE ;
2787 wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL ;
2788 if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL || GetWindowVariant() == wxWINDOW_VARIANT_MINI )
2789 {
2790 scrlsize = MAC_SMALL_SCROLLBAR_SIZE ;
2791 variant = wxWINDOW_VARIANT_SMALL ;
2792 }
2793
2794 int adjust = hasBoth ? scrlsize - 1: 0 ;
2795 int width, height ;
2796 GetClientSize( &width , &height ) ;
2797
2798 wxPoint vPoint(width-scrlsize, 0) ;
2799 wxSize vSize(scrlsize, height - adjust) ;
2800 wxPoint hPoint(0 , height-scrlsize ) ;
2801 wxSize hSize( width - adjust, scrlsize) ;
2802
2803
2804 if ( style & wxVSCROLL )
2805 {
2806 m_vScrollBar = new wxScrollBar(this, wxWINDOW_VSCROLL, vPoint,
2807 vSize , wxVERTICAL);
2808 }
2809
2810 if ( style & wxHSCROLL )
2811 {
2812 m_hScrollBar = new wxScrollBar(this, wxWINDOW_HSCROLL, hPoint,
2813 hSize , wxHORIZONTAL);
2814 }
2815 }
2816
2817
2818 // because the create does not take into account the client area origin
2819 MacRepositionScrollBars() ; // we might have a real position shift
2820 }
2821
2822 void wxWindowMac::MacRepositionScrollBars()
2823 {
2824 if ( !m_hScrollBar && !m_vScrollBar )
2825 return ;
2826
2827 bool hasBoth = ( m_hScrollBar && m_hScrollBar->IsShown()) && ( m_vScrollBar && m_vScrollBar->IsShown()) ;
2828 int scrlsize = m_hScrollBar ? m_hScrollBar->GetSize().y : ( m_vScrollBar ? m_vScrollBar->GetSize().x : MAC_SCROLLBAR_SIZE ) ;
2829 int adjust = hasBoth ? scrlsize - 1 : 0 ;
2830
2831 // get real client area
2832
2833 int width ;
2834 int height ;
2835 GetSize( &width , &height ) ;
2836
2837 width -= MacGetLeftBorderSize() + MacGetRightBorderSize();
2838 height -= MacGetTopBorderSize() + MacGetBottomBorderSize();
2839
2840 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
2841 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
2842 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
2843 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
2844 /*
2845 int x = 0 ;
2846 int y = 0 ;
2847 int w ;
2848 int h ;
2849 GetSize( &w , &h ) ;
2850
2851 MacClientToRootWindow( &x , &y ) ;
2852 MacClientToRootWindow( &w , &h ) ;
2853
2854 wxWindowMac *iter = (wxWindowMac*)this ;
2855
2856 int totW = 10000 , totH = 10000;
2857 while( iter )
2858 {
2859 if ( iter->IsTopLevel() )
2860 {
2861 iter->GetSize( &totW , &totH ) ;
2862 break ;
2863 }
2864
2865 iter = iter->GetParent() ;
2866 }
2867
2868 if ( x == 0 )
2869 {
2870 hPoint.x = -1 ;
2871 hSize.x += 1 ;
2872 }
2873 if ( y == 0 )
2874 {
2875 vPoint.y = -1 ;
2876 vSize.y += 1 ;
2877 }
2878
2879 if ( w-x >= totW )
2880 {
2881 hSize.x += 1 ;
2882 vPoint.x += 1 ;
2883 }
2884
2885 if ( h-y >= totH )
2886 {
2887 vSize.y += 1 ;
2888 hPoint.y += 1 ;
2889 }
2890 */
2891 if ( m_vScrollBar )
2892 {
2893 m_vScrollBar->SetSize( vPoint.x , vPoint.y, vSize.x, vSize.y , wxSIZE_ALLOW_MINUS_ONE);
2894 }
2895 if ( m_hScrollBar )
2896 {
2897 m_hScrollBar->SetSize( hPoint.x , hPoint.y, hSize.x, hSize.y, wxSIZE_ALLOW_MINUS_ONE);
2898 }
2899 }
2900
2901 bool wxWindowMac::AcceptsFocus() const
2902 {
2903 return MacCanFocus() && wxWindowBase::AcceptsFocus();
2904 }
2905
2906 void wxWindowMac::MacSuperChangedPosition()
2907 {
2908 // only window-absolute structures have to be moved i.e. controls
2909
2910 wxWindowListNode *node = GetChildren().GetFirst();
2911 while ( node )
2912 {
2913 wxWindowMac *child = node->GetData();
2914 child->MacSuperChangedPosition() ;
2915 node = node->GetNext();
2916 }
2917 }
2918
2919 void wxWindowMac::MacTopLevelWindowChangedPosition()
2920 {
2921 // only screen-absolute structures have to be moved i.e. glcanvas
2922
2923 wxWindowListNode *node = GetChildren().GetFirst();
2924 while ( node )
2925 {
2926 wxWindowMac *child = node->GetData();
2927 child->MacTopLevelWindowChangedPosition() ;
2928 node = node->GetNext();
2929 }
2930 }
2931
2932 long wxWindowMac::MacGetLeftBorderSize( ) const
2933 {
2934 if( IsTopLevel() )
2935 return 0 ;
2936
2937 if (m_windowStyle & wxRAISED_BORDER || m_windowStyle & wxSUNKEN_BORDER )
2938 {
2939 SInt32 border = 3 ;
2940 return border ;
2941 }
2942 else if ( m_windowStyle &wxDOUBLE_BORDER)
2943 {
2944 SInt32 border = 3 ;
2945 return border ;
2946 }
2947 else if (m_windowStyle &wxSIMPLE_BORDER)
2948 {
2949 return 1 ;
2950 }
2951 return 0 ;
2952 }
2953
2954 long wxWindowMac::MacGetRightBorderSize( ) const
2955 {
2956 // they are all symmetric in mac themes
2957 return MacGetLeftBorderSize() ;
2958 }
2959
2960 long wxWindowMac::MacGetTopBorderSize( ) const
2961 {
2962 // they are all symmetric in mac themes
2963 return MacGetLeftBorderSize() ;
2964 }
2965
2966 long wxWindowMac::MacGetBottomBorderSize( ) const
2967 {
2968 // they are all symmetric in mac themes
2969 return MacGetLeftBorderSize() ;
2970 }
2971
2972 long wxWindowMac::MacRemoveBordersFromStyle( long style )
2973 {
2974 return style & ~wxBORDER_MASK ;
2975 }
2976
2977 // Find the wxWindowMac at the current mouse position, returning the mouse
2978 // position.
2979 wxWindowMac* wxFindWindowAtPointer(wxPoint& pt)
2980 {
2981 pt = wxGetMousePosition();
2982 wxWindowMac* found = wxFindWindowAtPoint(pt);
2983 return found;
2984 }
2985
2986 // Get the current mouse position.
2987 wxPoint wxGetMousePosition()
2988 {
2989 int x, y;
2990 wxGetMousePosition(& x, & y);
2991 return wxPoint(x, y);
2992 }
2993
2994 void wxWindowMac::OnMouseEvent( wxMouseEvent &event )
2995 {
2996 if ( event.GetEventType() == wxEVT_RIGHT_DOWN )
2997 {
2998 // copied from wxGTK : CS
2999 // generate a "context menu" event: this is similar to wxEVT_RIGHT_DOWN
3000 // except that:
3001 //
3002 // (a) it's a command event and so is propagated to the parent
3003 // (b) under MSW it can be generated from kbd too
3004 // (c) it uses screen coords (because of (a))
3005 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
3006 this->GetId(),
3007 this->ClientToScreen(event.GetPosition()));
3008 if ( ! GetEventHandler()->ProcessEvent(evtCtx) )
3009 event.Skip() ;
3010 }
3011 else
3012 {
3013 event.Skip() ;
3014 }
3015 }
3016
3017 void wxWindowMac::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED( mouseStillDown ) )
3018 {
3019 }
3020
3021 Rect wxMacGetBoundsForControl( wxWindow* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
3022 {
3023 int x ,y , w ,h ;
3024
3025 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin) ;
3026 Rect bounds = { y , x , y+h , x+w };
3027 return bounds ;
3028 }
3029
3030 wxInt32 wxWindowMac::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
3031 {
3032 return eventNotHandledErr ;
3033 }
3034
3035