]> git.saurik.com Git - wxWidgets.git/blame - src/mac/control.cpp
Added calendar contributed by Lorne White
[wxWidgets.git] / src / mac / control.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: control.cpp
3// Purpose: wxControl class
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "control.h"
14#endif
15
16#include "wx/control.h"
519cb848
SC
17#include "wx/notebook.h"
18#include "wx/tabctrl.h"
19#include "wx/spinbutt.h"
e9576ca5
SC
20
21#if !USE_SHARED_LIBRARY
22IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
23
24BEGIN_EVENT_TABLE(wxControl, wxWindow)
519cb848
SC
25 EVT_MOUSE_EVENTS( wxControl::OnMouseEvent )
26 EVT_CHAR( wxControl::OnKeyDown )
27 EVT_PAINT( wxControl::OnPaint )
e9576ca5
SC
28END_EVENT_TABLE()
29#endif
30
519cb848
SC
31#include <wx/mac/uma.h>
32
e9576ca5 33// Item members
519cb848
SC
34
35ControlActionUPP wxMacLiveScrollbarActionUPP = NULL ;
36
37pascal void wxMacLiveScrollbarActionProc( ControlHandle control , ControlPartCode partCode )
38{
39 if ( partCode != 0)
40 {
41 wxControl* wx = (wxControl*) GetControlReference( control ) ;
42 if ( wx )
43 {
44 wx->MacHandleControlClick( control , partCode ) ;
45 }
46 }
47}
48
e9576ca5
SC
49wxControl::wxControl()
50{
519cb848
SC
51 m_macControl = NULL ;
52 m_macHorizontalBorder = 0 ; // additional pixels around the real control
53 m_macVerticalBorder = 0 ;
e9576ca5
SC
54 m_backgroundColour = *wxWHITE;
55 m_foregroundColour = *wxBLACK;
e7549107
SC
56#if WXWIN_COMPATIBILITY
57 m_callback = 0;
58#endif // WXWIN_COMPATIBILITY
519cb848
SC
59
60 if ( wxMacLiveScrollbarActionUPP == NULL )
61 {
62 wxMacLiveScrollbarActionUPP = NewControlActionProc( wxMacLiveScrollbarActionProc ) ;
63 }
e9576ca5
SC
64}
65
66wxControl::~wxControl()
67{
e7549107 68 m_isBeingDeleted = TRUE;
e9576ca5
SC
69 // If we delete an item, we should initialize the parent panel,
70 // because it could now be invalid.
e7549107
SC
71 wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
72 if ( panel )
e9576ca5 73 {
e7549107
SC
74 if (panel->GetDefaultItem() == (wxButton*) this)
75 panel->SetDefaultItem(NULL);
e9576ca5 76 }
519cb848
SC
77 if ( m_macControl )
78 {
79 UMADisposeControl( m_macControl ) ;
80 m_macControl = NULL ;
81 }
e9576ca5
SC
82}
83
e7549107 84void wxControl::SetLabel(const wxString& title)
e9576ca5 85{
e7549107 86 m_label = title ;
519cb848
SC
87
88 if ( m_macControl )
89 {
90 Str255 maclabel ;
e7549107
SC
91 wxString label ;
92
93 if( wxApp::s_macDefaultEncodingIsPC )
94 label = wxMacMakeMacStringFromPC( title ) ;
95 else
96 label = title ;
519cb848
SC
97
98 strcpy( (char*) maclabel , label ) ;
99 c2pstr( (char*) maclabel ) ;
100
101 ::SetControlTitle( m_macControl , maclabel ) ;
102 }
e9576ca5
SC
103}
104
e7549107 105wxSize wxControl::DoGetBestSize()
e9576ca5 106{
e7549107 107 return wxSize(20, 20);
e9576ca5
SC
108}
109
e7549107 110bool wxControl::ProcessCommand (wxCommandEvent & event)
e9576ca5
SC
111{
112 // Tries:
113 // 1) A callback function (to become obsolete)
114 // 2) OnCommand, starting at this window and working up parent hierarchy
115 // 3) OnCommand then calls ProcessEvent to search the event tables.
e7549107
SC
116#if WXWIN_COMPATIBILITY
117 if ( m_callback )
e9576ca5 118 {
e7549107
SC
119 (void)(*m_callback)(this, event);
120
121 return TRUE;
e9576ca5
SC
122 }
123 else
e7549107 124#endif // WXWIN_COMPATIBILITY
e9576ca5 125 {
e7549107 126 return GetEventHandler()->ProcessEvent(event);
e9576ca5
SC
127 }
128}
129
519cb848
SC
130// ------------------------
131wxList *wxWinMacControlList = NULL;
132wxControl *wxFindControlFromMacControl(ControlHandle inControl )
133{
134 wxNode *node = wxWinMacControlList->Find((long)inControl);
135 if (!node)
136 return NULL;
137 return (wxControl *)node->Data();
138}
139
140void wxAssociateControlWithMacControl(ControlHandle inControl, wxControl *control)
141{
142 // adding NULL WindowRef is (first) surely a result of an error and
143 // (secondly) breaks menu command processing
144 wxCHECK_RET( inControl != (ControlHandle) NULL, "attempt to add a NULL WindowRef to window list" );
145
146 if ( !wxWinMacControlList->Find((long)inControl) )
147 wxWinMacControlList->Append((long)inControl, control);
148}
149
150void wxRemoveMacControlAssociation(wxControl *control)
151{
152 wxWinMacControlList->DeleteObject(control);
153}
154
155void wxControl::MacPreControlCreate( wxWindow *parent, wxWindowID id, wxString label ,
156 const wxPoint& pos,
157 const wxSize& size, long style,
158 const wxValidator& validator,
159 const wxString& name , Rect *outBounds , StringPtr maclabel )
160{
161 m_label = label ;
162 SetName(name);
163 if ( &validator )
164 SetValidator(validator);
165
166 m_windowStyle = style;
167 parent->AddChild((wxButton *)this);
168
169 m_backgroundColour = parent->GetBackgroundColour() ;
170 m_foregroundColour = parent->GetForegroundColour() ;
171
172 if (id == -1)
173 m_windowId = NewControlId();
174 else
175 m_windowId = id;
176
177 m_width = size.x ;
178 m_height = size.y ;
179 int x = pos.x ;
180 int y = pos.y ;
181 AdjustForParentClientOrigin(x, y, wxSIZE_USE_EXISTING);
182 m_x = x ;
183 m_y = y ;
184
185
186 Point localOrigin ;
187 Rect clipRect ;
188
189 parent->MacClientToRootWindow( &x , &y ) ;
190 outBounds->top = y + m_macVerticalBorder ;
191 outBounds->left = x + m_macHorizontalBorder ;
192 outBounds->bottom = outBounds->top + m_height - 2 * m_macVerticalBorder;
193 outBounds->right = outBounds->left + m_width - 2 * m_macHorizontalBorder ;
194
195 strcpy( (char*) maclabel , label ) ;
196 if( wxApp::s_macDefaultEncodingIsPC )
197 {
198 wxMacConvertFromPCForControls( (char*) maclabel ) ;
199 }
200
201 c2pstr( (char*) maclabel ) ;
202}
203
204void wxControl::MacPostControlCreate()
205{
206 wxASSERT_MSG( m_macControl != NULL , "No valid mac control" ) ;
207
208 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
209 {
210 // no font
211 }
212 else if ( IsKindOf( CLASSINFO( wxStaticBox ) ) )
213 {
214 ControlFontStyleRec controlstyle ;
215 controlstyle.flags = kControlUseFontMask ;
216 controlstyle.font = kControlFontSmallBoldSystemFont ;
217
218 ::UMASetControlFontStyle( m_macControl , &controlstyle ) ;
219 }
220 else
221 {
222 ControlFontStyleRec controlstyle ;
223 controlstyle.flags = kControlUseFontMask ;
224 controlstyle.font = kControlFontSmallSystemFont ;
225
226 ::UMASetControlFontStyle( m_macControl , &controlstyle ) ;
227 }
228 ControlHandle container = GetParent()->MacGetContainerForEmbedding() ;
229 wxASSERT_MSG( container != NULL , "No valid mac container control" ) ;
230 ::UMAEmbedControl( m_macControl , container ) ;
231 MacAdjustControlRect() ;
232 wxAssociateControlWithMacControl( m_macControl , this ) ;
233}
234
235void wxControl::MacAdjustControlRect()
236{
237 wxASSERT_MSG( m_macControl != NULL , "No valid mac control" ) ;
238 if ( m_width == -1 || m_height == -1 )
239 {
240 Rect bestsize = { 0 , 0 , 0 , 0 } ;
241 short baselineoffset ;
242
243 UMAGetBestControlRect( m_macControl , &bestsize , &baselineoffset ) ;
244
245 if ( EmptyRect( &bestsize ) )
246 {
247 baselineoffset = 0;
248 bestsize.left = bestsize.top = 0 ;
249 bestsize.right = 16 ;
250 bestsize.bottom = 16 ;
251 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
252 {
253 bestsize.bottom = 16 ;
254 }
255 else if ( IsKindOf( CLASSINFO( wxSpinButton ) ) )
256 {
257 bestsize.bottom = 24 ;
258 }
259 }
260
261 if ( m_width == -1 )
262 {
263 if ( IsKindOf( CLASSINFO( wxButton ) ) )
264 {
265 m_width = m_label.Length() * 8 + 12 + 2 * m_macHorizontalBorder;
266 }
267 else if ( IsKindOf( CLASSINFO( wxStaticText ) ) )
268 {
269 m_width = m_label.Length() * 8 ;
270 }
271 else
272 m_width = bestsize.right - bestsize.left + 2 * m_macHorizontalBorder;
273 }
274 if ( m_height == -1 )
275 {
276 m_height = bestsize.bottom - bestsize.top ;
277 if ( m_height < 10 )
278 m_height = 13 ;
279
280 m_height += 2 * m_macVerticalBorder;
281 }
282
283 wxMacDrawingHelper helper ( wxFindWinFromMacWindow( GetMacRootWindow() ) ) ;
284 if ( helper.Ok() )
285 {
286 UMASizeControl( m_macControl , m_width - 2 * m_macHorizontalBorder, m_height - 2 * m_macVerticalBorder ) ;
287 }
288 }
289}
290ControlHandle wxControl::MacGetContainerForEmbedding()
291{
292 if ( m_macControl )
293 return m_macControl ;
294
295 return wxWindow::MacGetContainerForEmbedding() ;
296}
297
298void wxControl::MacSuperChangedPosition()
299{
300 if ( m_macControl )
301 {
302 int former_mac_x = (**m_macControl).contrlRect.left ;
303 int former_mac_y = (**m_macControl).contrlRect.top ;
304 int mac_x = m_x ;
305 int mac_y = m_y ;
306 GetParent()->MacClientToRootWindow( & mac_x , & mac_y ) ;
307
308 WindowRef rootwindow = GetMacRootWindow() ;
309 wxWindow* wxrootwindow = wxFindWinFromMacWindow( rootwindow ) ;
310 UMASetThemeWindowBackground( rootwindow , kThemeBrushDialogBackgroundActive , false ) ;
311 wxMacDrawingHelper focus( wxrootwindow ) ;
312
313 if ( mac_x != former_mac_x || mac_y != former_mac_y )
314 {
315 {
316 Rect inval = { former_mac_y , former_mac_x , former_mac_y + m_height , former_mac_x + m_width } ;
317 InvalRect( &inval ) ;
318 }
319 UMAMoveControl( m_macControl , mac_x + m_macHorizontalBorder , mac_y + m_macVerticalBorder ) ;
320 {
321 Rect inval = { mac_y , mac_x , mac_y + m_height , mac_x + m_width } ;
322 InvalRect( &inval ) ;
323 }
324 }
325 if ( wxrootwindow->IsKindOf( CLASSINFO( wxDialog ) ) )
326 {
327 }
328 else
329 {
330 UMASetThemeWindowBackground( rootwindow , kThemeBrushDocumentWindowBackground , false ) ;
331 }
332 }
333
334 wxWindow::MacSuperChangedPosition() ;
335}
336
337void wxControl::MacSuperEnabled( bool enabled )
338{
e7549107 339/*
519cb848
SC
340 if ( m_macControl )
341 {
342 if ( UMAHasAppearance() )
343 {
344 if ( !enabled )
345 {
346 ::DeactivateControl( m_macControl ) ;
347 }
348 else
349 {
350 if ( m_macEnabled )
351 ::ActivateControl( m_macControl ) ;
352 }
353 }
354 else
355 {
356 if ( !enabled )
357 {
358 ::HiliteControl( m_macControl , 255 ) ;
359 }
360 else
361 {
362 if ( m_macEnabled )
363 ::HiliteControl( m_macControl , 0 ) ;
364 }
365 }
366 }
367 wxWindow::MacSuperEnabled( enabled ) ;
e7549107 368 */
519cb848
SC
369}
370
371void wxControl::MacSuperShown( bool show )
372{
e7549107 373 /*
519cb848
SC
374 if ( m_macControl )
375 {
376 if ( !show )
377 {
378 ::UMAHideControl( m_macControl ) ;
379 }
380 else
381 {
382 if ( m_macShown )
383 ::UMAShowControl( m_macControl ) ;
384 }
385 }
386
387 wxWindow::MacSuperShown( show ) ;
e7549107 388 */
519cb848
SC
389}
390
391void wxControl::DoSetSize(int x, int y,
392 int width, int height,
393 int sizeFlags )
394{
395 if ( m_macControl == NULL )
396 {
397 wxWindow::DoSetSize( x , y ,width , height ,sizeFlags ) ;
398 return ;
399 }
400
401 WindowRef rootwindow = GetMacRootWindow() ;
402 wxWindow* wxrootwindow = wxFindWinFromMacWindow( rootwindow ) ;
403 UMASetThemeWindowBackground( rootwindow , kThemeBrushDialogBackgroundActive , false ) ;
404
405 int former_x = m_x ;
406 int former_y = m_y ;
407 int former_w = m_width ;
408 int former_h = m_height ;
409
410 int former_mac_x = (**m_macControl).contrlRect.left ;
411 int former_mac_y = (**m_macControl).contrlRect.top ;
412
413 int currentX, currentY;
414 GetPosition(&currentX, &currentY);
415 int currentW,currentH;
416 GetSize(&currentW, &currentH);
417
418 int actualWidth = width;
419 int actualHeight = height;
420 int actualX = x;
421 int actualY = y;
422 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
423 actualX = currentX;
424 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
425 actualY = currentY;
426 if (width == -1)
427 actualWidth = currentW ;
428 if (height == -1)
429 actualHeight = currentH ;
430
431 if ( actualX == currentX && actualY == currentY && actualWidth == currentW && actualHeight == currentH)
432 return ;
433
434 AdjustForParentClientOrigin(actualX, actualY, sizeFlags);
435 wxMacDrawingHelper focus( wxFindWinFromMacWindow( GetMacRootWindow() ) ) ;
436
437 int mac_x = actualX ;
438 int mac_y = actualY ;
439 GetParent()->MacClientToRootWindow( & mac_x , & mac_y ) ;
440
441 if ( mac_x != former_mac_x || mac_y != former_mac_y )
442 {
443 {
444 Rect inval = { former_mac_y , former_mac_x , former_mac_y + m_height , former_mac_x + m_width } ;
445 InvalRect( &inval ) ;
446 }
447 UMAMoveControl( m_macControl , mac_x + m_macHorizontalBorder , mac_y + m_macVerticalBorder ) ;
448 {
449 Rect inval = { mac_y , mac_x , mac_y + m_height , mac_x + m_width } ;
450 InvalRect( &inval ) ;
451 }
452 }
453
454 if ( actualX != former_x || actualY != former_y )
455 {
456 m_x = actualX ;
457 m_y = actualY ;
458
459 MacRepositionScrollBars() ;
460 // To consider -> should the parameters be the effective or the virtual coordinates (AdjustForParent..)
461 wxMoveEvent event(wxPoint(m_x, m_y), m_windowId);
462 event.SetEventObject(this);
463 GetEventHandler()->ProcessEvent(event);
464 }
465 if ( actualWidth != former_w || actualHeight != former_h )
466 {
467 {
468 Rect inval = { mac_y , mac_x , mac_y + former_h , mac_x + former_w } ;
469 InvalRect( &inval ) ;
470 }
471 m_width = actualWidth ;
472 m_height = actualHeight ;
473
474 UMASizeControl( m_macControl , m_width - 2 * m_macHorizontalBorder, m_height - 2 * m_macVerticalBorder ) ;
475 {
476 Rect inval = { mac_y , mac_x , mac_y + m_height , mac_x + m_width } ;
477 InvalRect( &inval ) ;
478 }
479
480 MacRepositionScrollBars() ;
481 wxSizeEvent event(wxSize(m_width, m_height), m_windowId);
482 event.SetEventObject(this);
483 GetEventHandler()->ProcessEvent(event);
484 }
485 if ( wxrootwindow->IsKindOf( CLASSINFO( wxDialog ) ) )
486 {
487 }
488 else
489 {
490 UMASetThemeWindowBackground( rootwindow , kThemeBrushDocumentWindowBackground , false ) ;
491 }
492}
493
519cb848
SC
494bool wxControl::Show(bool show)
495{
e7549107
SC
496 if ( !wxWindow::Show( show ) )
497 return FALSE ;
498
499 if ( m_macControl )
500 {
501 if ( show )
502 ::UMAShowControl( m_macControl ) ;
503 else
504 ::UMAHideControl( m_macControl ) ;
505 }
506 return TRUE ;
519cb848
SC
507}
508
e7549107 509bool wxControl::Enable(bool enable)
519cb848 510{
e7549107
SC
511 if ( !wxWindow::Enable(enable) )
512 return FALSE;
519cb848 513
e7549107 514 if ( m_macControl )
519cb848 515 {
e7549107
SC
516
517 if ( UMAHasAppearance() )
518 {
519 if ( enable )
520 ::ActivateControl( m_macControl ) ;
521 else
522 ::DeactivateControl( m_macControl ) ;
523 }
519cb848 524 else
e7549107
SC
525 {
526 if ( enable )
527 ::HiliteControl( m_macControl , 0 ) ;
528 else
529 ::HiliteControl( m_macControl , 255 ) ;
530 }
519cb848 531 }
e7549107 532 return TRUE ;
519cb848
SC
533}
534
535void wxControl::Refresh(bool eraseBack, const wxRect *rect)
536{
537 if ( m_macControl )
538 {
539 wxWindow::Refresh( eraseBack , rect ) ;
540 }
541 else
542 {
543 wxWindow::Refresh( eraseBack , rect ) ;
544 }
545}
546
547void wxControl::OnPaint(wxPaintEvent& event)
548{
549 if ( m_macControl )
550 {
551 WindowRef window = GetMacRootWindow() ;
552 if ( window )
553 {
554 wxWindow* win = wxFindWinFromMacWindow( window ) ;
555 if ( win )
556 {
557 wxMacDrawingHelper help( win ) ;
558 SetOrigin( 0 , 0 ) ;
559
560 bool hasTabBehind = false ;
561 wxWindow* parent = GetParent() ;
562 while ( parent )
563 {
e7549107 564 if( parent->MacGetWindowData() )
519cb848 565 {
e7549107 566 UMASetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , kThemeBrushDialogBackgroundActive , false ) ;
519cb848
SC
567 break ;
568 }
569
570 if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) ))
571 {
572 if ( ((wxControl*)parent)->m_macControl )
573 SetUpControlBackground( ((wxControl*)parent)->m_macControl , -1 , true ) ;
574 break ;
575 }
576
577 parent = parent->GetParent() ;
578 }
579
580 UMADrawControl( m_macControl ) ;
e7549107 581 UMASetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , win->MacGetWindowData()->m_macWindowBackgroundTheme , false ) ;
519cb848
SC
582 }
583 }
584 }
585 else
586 {
e7549107 587 // wxWindow::OnPaint( event ) ;
519cb848
SC
588 }
589}
590
591void wxControl::OnKeyDown( wxKeyEvent &event )
592{
593 if ( m_macControl == NULL )
594 return ;
595
596 EventRecord *ev = wxTheApp->MacGetCurrentEvent() ;
597 short keycode ;
598 short keychar ;
599 keychar = short(ev->message & charCodeMask);
600 keycode = short(ev->message & keyCodeMask) >> 8 ;
601
602 UMAHandleControlKey( m_macControl , keycode , keychar , ev->modifiers ) ;
603}
604
605void wxControl::OnMouseEvent( wxMouseEvent &event )
606{
607 if ( m_macControl == NULL )
608 {
609 event.Skip() ;
610 return ;
611 }
612
613 if (event.GetEventType() == wxEVT_LEFT_DOWN )
614 {
615
616 int x = event.m_x ;
617 int y = event.m_y ;
618
619 MacClientToRootWindow( &x , &y ) ;
620
621 ControlHandle control ;
622 Point localwhere ;
623 GrafPtr port ;
624 SInt16 controlpart ;
625 WindowRef window = GetMacRootWindow() ;
626
627 localwhere.h = x ;
628 localwhere.v = y ;
629
630 short modifiers = 0;
631
632 if ( !event.m_leftDown && !event.m_rightDown )
633 modifiers |= btnState ;
634
635 if ( event.m_shiftDown )
636 modifiers |= shiftKey ;
637
638 if ( event.m_controlDown )
639 modifiers |= controlKey ;
640
641 if ( event.m_altDown )
642 modifiers |= optionKey ;
643
644 if ( event.m_metaDown )
645 modifiers |= cmdKey ;
646
647 controlpart = FindControl( localwhere , window , &control ) ;
648 {
649 if ( AcceptsFocus() && FindFocus() != this )
650 {
651 SetFocus() ;
652 }
653 if ( control && UMAIsControlActive( control ) )
654 {
655 {
656 if ( controlpart == kControlIndicatorPart && !UMAHasAppearance() )
657 controlpart = UMAHandleControlClick( control , localwhere , modifiers , (ControlActionUPP) NULL ) ;
658 else
659 controlpart = UMAHandleControlClick( control , localwhere , modifiers , (ControlActionUPP) -1 ) ;
660 wxTheApp->s_lastMouseDown = 0 ;
661 if ( controlpart && ! ( ( UMAHasAppearance() || (controlpart != kControlIndicatorPart) )
662 && (IsKindOf( CLASSINFO( wxScrollBar ) ) ) ) ) // otherwise we will get the event twice
663 {
664 MacHandleControlClick( control , controlpart ) ;
665 }
666 }
667 }
668 }
669 }
670}
671
672bool wxControl::MacCanFocus() const
673{
674 { if ( m_macControl == NULL )
675 return true ;
676 else
677 return false ;
678 }
679}
680
681void wxControl::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
682{
683 wxASSERT_MSG( m_macControl != NULL , "No valid mac control" ) ;
684}
685