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