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