Applied patch 1206278 from Mike Wetherell.
[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:     wxWidgets 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     //flag checking
38     wxASSERT_MSG( !(style & wxGA_HORIZONTAL), wxT("Horizontal gauge not supported on cocoa"));//*
39     wxASSERT_MSG( !(style & wxGA_SMOOTH), wxT("Smooth gauge not supported on cocoa"));
40     //* - GNUStep made isVertical and setVertical part of thier framework, but its specific to them
41     //the way they do it is just handle that flag in drawRect.
42
43     if(!CreateControl(parent,winid,pos,size,style,validator,name))
44         return false;
45     SetNSView([[NSProgressIndicator alloc] initWithFrame: MakeDefaultNSRect(size)]);
46     [m_cocoaNSView release];
47
48     [(NSProgressIndicator*)m_cocoaNSView setMaxValue:range];
49     [(NSProgressIndicator*)m_cocoaNSView setIndeterminate:NO];
50
51     if(m_parent)
52         m_parent->CocoaAddChild(this);
53     SetInitialFrameRect(pos,size);
54
55     return true;
56 }
57
58 wxGauge::~wxGauge()
59 {
60 }
61
62 int wxGauge::GetValue() const
63 {
64     return (int)[(NSProgressIndicator*)m_cocoaNSView doubleValue];
65 }
66
67 void wxGauge::SetValue(int value)
68 {
69     [(NSProgressIndicator*)m_cocoaNSView setDoubleValue:value];
70 }
71
72 int wxGauge::GetRange() const
73 {
74     return (int)[(NSProgressIndicator*)m_cocoaNSView maxValue];
75 }
76
77 void wxGauge::SetRange(int maxValue)
78 {
79     [(NSProgressIndicator*)m_cocoaNSView setMinValue:0.0];
80     [(NSProgressIndicator*)m_cocoaNSView setMaxValue:maxValue];
81 }
82
83 // NSProgressIndicator is not an NSControl but does respond to
84 // sizeToFit on OS X >= 10.2
85 wxSize wxGauge::DoGetBestSize() const
86 {
87     wxAutoNSAutoreleasePool pool;
88     wxASSERT(GetNSProgressIndicator());
89     NSRect storedRect = [m_cocoaNSView frame];
90     bool didFit = false;
91 NS_DURING
92     [GetNSProgressIndicator() sizeToFit];
93     didFit = true;
94 NS_HANDLER
95     // TODO: if anything other than method not implemented, re-raise
96 NS_ENDHANDLER
97     if(didFit)
98     {
99         NSRect cocoaRect = [m_cocoaNSView frame];
100         wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
101         [m_cocoaNSView setFrame: storedRect];
102         wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
103         return /*wxConstCast(this, wxControl)->m_bestSize =*/ size;
104     }
105     // Cocoa can't tell us the size
106     float height = NSProgressIndicatorPreferredAquaThickness;
107     return wxSize((int)(height*2),(int)height);
108 }
109
110 #endif // wxUSE_GAUGE