1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/gauge.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
23 #include "wx/cocoa/autorelease.h"
25 #import <AppKit/NSProgressIndicator.h>
26 #import <Foundation/NSException.h>
30 IMPLEMENT_DYNAMIC_CLASS(wxGauge, wxControl)
32 BEGIN_EVENT_TABLE(wxGauge, wxGaugeBase)
34 // WX_IMPLEMENT_COCOA_OWNER(wxGauge,NSProgressIndicator,NSView,NSView)
36 bool wxGauge::Create(wxWindow *parent, wxWindowID winid, int range,
37 const wxPoint& pos, const wxSize& size, long style,
38 const wxValidator& validator, const wxString& name)
40 // NOTE: wxGA_SMOOTH flag is simply ignored (gauges are ALWAYS smooth)
41 if(!CreateControl(parent,winid,pos,size,style,validator,name))
43 SetNSView([[NSProgressIndicator alloc] initWithFrame: MakeDefaultNSRect(size)]);
44 [m_cocoaNSView release];
46 // TODO: DoGetBestSize is likely totally wrong for vertical gauges but
47 // this actually makes the widgets sample work so it's better than nothing.
48 if(style & wxGA_VERTICAL)
50 wxLogDebug(wxT("wxGA_VERTICAL may not work correctly. See src/cocoa/gauge.mm"));
51 [m_cocoaNSView setBoundsRotation:-90.0];
54 [(NSProgressIndicator*)m_cocoaNSView setMaxValue:range];
55 [(NSProgressIndicator*)m_cocoaNSView setIndeterminate:NO];
58 m_parent->CocoaAddChild(this);
59 SetInitialFrameRect(pos,size);
68 int wxGauge::GetValue() const
70 return (int)[(NSProgressIndicator*)m_cocoaNSView doubleValue];
73 void wxGauge::SetValue(int value)
75 [(NSProgressIndicator*)m_cocoaNSView setDoubleValue:value];
78 int wxGauge::GetRange() const
80 return (int)[(NSProgressIndicator*)m_cocoaNSView maxValue];
83 void wxGauge::SetRange(int maxValue)
85 [(NSProgressIndicator*)m_cocoaNSView setMinValue:0.0];
86 [(NSProgressIndicator*)m_cocoaNSView setMaxValue:maxValue];
89 // NSProgressIndicator is not an NSControl but does respond to
90 // sizeToFit on OS X >= 10.2
91 wxSize wxGauge::DoGetBestSize() const
93 wxAutoNSAutoreleasePool pool;
94 wxASSERT(GetNSProgressIndicator());
95 NSRect storedRect = [m_cocoaNSView frame];
98 [GetNSProgressIndicator() sizeToFit];
101 // TODO: if anything other than method not implemented, re-raise
105 NSRect cocoaRect = [m_cocoaNSView frame];
106 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
107 [m_cocoaNSView setFrame: storedRect];
108 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
109 return /*wxConstCast(this, wxControl)->m_bestSize =*/ size;
111 // Cocoa can't tell us the size
112 float height = NSProgressIndicatorPreferredAquaThickness;
113 return wxSize((int)(height*2),(int)height);
116 #endif // wxUSE_GAUGE