Implement a reasonable DoGetBestSize (NSProgressIndicator is not an NSControl)
[wxWidgets.git] / src / cocoa / gauge.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        cocoa/gauge.mm
3 // Purpose:     wxGauge
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2003/07/15
7 // RCS-ID:      $Id$
8 // Copyright:   (c) 2003 David Elliott
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13 #if wxUSE_GAUGE
14
15 #ifndef WX_PRECOMP
16     #include "wx/app.h"
17     #include "wx/gauge.h"
18     #include "wx/log.h"
19 #endif //WX_PRECOMP
20
21 #include "wx/cocoa/autorelease.h"
22
23 #import <AppKit/NSProgressIndicator.h>
24 #import <Foundation/NSException.h>
25
26 #include <math.h>
27
28 IMPLEMENT_DYNAMIC_CLASS(wxGauge, wxControl)
29 BEGIN_EVENT_TABLE(wxGauge, wxGaugeBase)
30 END_EVENT_TABLE()
31 // WX_IMPLEMENT_COCOA_OWNER(wxGauge,NSProgressIndicator,NSView,NSView)
32
33 bool wxGauge::Create(wxWindow *parent, wxWindowID winid, int range,
34             const wxPoint& pos, const wxSize& size, long style,
35             const wxValidator& validator, const wxString& name)
36 {
37     if(!CreateControl(parent,winid,pos,size,style,validator,name))
38         return false;
39     SetNSView([[NSProgressIndicator alloc] initWithFrame: MakeDefaultNSRect(size)]);
40     [m_cocoaNSView release];
41
42     [(NSProgressIndicator*)m_cocoaNSView setMaxValue:range];
43     [(NSProgressIndicator*)m_cocoaNSView setIndeterminate:NO];
44
45     if(m_parent)
46         m_parent->CocoaAddChild(this);
47     SetInitialFrameRect(pos,size);
48
49     return true;
50 }
51
52 wxGauge::~wxGauge()
53 {
54 }
55
56 int wxGauge::GetValue() const
57 {
58     return (int)[(NSProgressIndicator*)m_cocoaNSView doubleValue];
59 }
60
61 void wxGauge::SetValue(int value)
62 {
63     [(NSProgressIndicator*)m_cocoaNSView setDoubleValue:value];
64 }
65
66 int wxGauge::GetRange() const
67 {
68     return (int)[(NSProgressIndicator*)m_cocoaNSView maxValue];
69 }
70
71 void wxGauge::SetRange(int maxValue)
72 {
73     [(NSProgressIndicator*)m_cocoaNSView setMinValue:0.0];
74     [(NSProgressIndicator*)m_cocoaNSView setMaxValue:maxValue];
75 }
76
77 // NSProgressIndicator is not an NSControl but does respond to
78 // sizeToFit on OS X >= 10.2
79 wxSize wxGauge::DoGetBestSize() const
80 {
81     wxAutoNSAutoreleasePool pool;
82     wxASSERT(GetNSProgressIndicator());
83     NSRect storedRect = [m_cocoaNSView frame];
84     bool didFit = false;
85 NS_DURING
86     [GetNSProgressIndicator() sizeToFit];
87     didFit = true;
88 NS_HANDLER
89     // TODO: if anything other than method not implemented, re-raise
90 NS_ENDHANDLER
91     if(didFit)
92     {
93         NSRect cocoaRect = [m_cocoaNSView frame];
94         wxSize size((int)ceilf(cocoaRect.size.width),(int)ceilf(cocoaRect.size.height));
95         [m_cocoaNSView setFrame: storedRect];
96         wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
97         return /*wxConstCast(this, wxControl)->m_bestSize =*/ size;
98     }
99     // Cocoa can't tell us the size
100     float height = NSProgressIndicatorPreferredAquaThickness;
101     return wxSize((int)(height*2),(int)height);
102 }
103
104 #endif // wxUSE_GAUGE