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