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