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