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