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