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