1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/gauge.mm
4 // Author: David Elliott
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
22 #include "wx/cocoa/autorelease.h"
24 #import <AppKit/NSProgressIndicator.h>
25 #import <Foundation/NSException.h>
29 BEGIN_EVENT_TABLE(wxGauge, wxGaugeBase)
31 // WX_IMPLEMENT_COCOA_OWNER(wxGauge,NSProgressIndicator,NSView,NSView)
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)
37 // NOTE: wxGA_SMOOTH flag is simply ignored (gauges are ALWAYS smooth)
38 if(!CreateControl(parent,winid,pos,size,style,validator,name))
40 SetNSView([[NSProgressIndicator alloc] initWithFrame: MakeDefaultNSRect(size)]);
41 [m_cocoaNSView release];
43 // TODO: DoGetBestSize is likely totally wrong for vertical gauges but
44 // this actually makes the widgets sample work so it's better than nothing.
45 if(style & wxGA_VERTICAL)
47 wxLogDebug(wxT("wxGA_VERTICAL may not work correctly. See src/cocoa/gauge.mm"));
48 [m_cocoaNSView setBoundsRotation:-90.0];
51 [(NSProgressIndicator*)m_cocoaNSView setMaxValue:range];
52 [(NSProgressIndicator*)m_cocoaNSView setIndeterminate:NO];
55 m_parent->CocoaAddChild(this);
56 SetInitialFrameRect(pos,size);
65 int wxGauge::GetValue() const
67 return (int)[(NSProgressIndicator*)m_cocoaNSView doubleValue];
70 void wxGauge::SetValue(int value)
72 [(NSProgressIndicator*)m_cocoaNSView setDoubleValue:value];
75 int wxGauge::GetRange() const
77 return (int)[(NSProgressIndicator*)m_cocoaNSView maxValue];
80 void wxGauge::SetRange(int maxValue)
82 [(NSProgressIndicator*)m_cocoaNSView setMinValue:0.0];
83 [(NSProgressIndicator*)m_cocoaNSView setMaxValue:maxValue];
86 // NSProgressIndicator is not an NSControl but does respond to
87 // sizeToFit on OS X >= 10.2
88 wxSize wxGauge::DoGetBestSize() const
90 wxAutoNSAutoreleasePool pool;
91 wxASSERT(GetNSProgressIndicator());
92 NSRect storedRect = [m_cocoaNSView frame];
95 [GetNSProgressIndicator() sizeToFit];
98 // TODO: if anything other than method not implemented, re-raise
102 NSRect cocoaRect = [m_cocoaNSView frame];
103 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
104 [m_cocoaNSView setFrame: storedRect];
105 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
106 return /*wxConstCast(this, wxControl)->m_bestSize =*/ size;
108 // Cocoa can't tell us the size
109 float height = NSProgressIndicatorPreferredAquaThickness;
110 return wxSize((int)(height*2),(int)height);
113 #endif // wxUSE_GAUGE