Upgrade bundled zlib to 1.2.8.
[wxWidgets.git] / src / cocoa / gauge.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/gauge.mm
3 // Purpose:     wxGauge
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2003/07/15
7 // Copyright:   (c) 2003 David Elliott
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #if wxUSE_GAUGE
14
15 #include "wx/gauge.h"
16
17 #ifndef WX_PRECOMP
18     #include "wx/app.h"
19     #include "wx/log.h"
20 #endif //WX_PRECOMP
21
22 #include "wx/cocoa/autorelease.h"
23
24 #import <AppKit/NSProgressIndicator.h>
25 #import <Foundation/NSException.h>
26
27 #include <math.h>
28
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     // NOTE: wxGA_SMOOTH flag is simply ignored (gauges are ALWAYS smooth)
38     if(!CreateControl(parent,winid,pos,size,style,validator,name))
39         return false;
40     SetNSView([[NSProgressIndicator alloc] initWithFrame: MakeDefaultNSRect(size)]);
41     [m_cocoaNSView release];
42
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)
46     {
47         wxLogDebug(wxT("wxGA_VERTICAL may not work correctly.  See src/cocoa/gauge.mm"));
48         [m_cocoaNSView setBoundsRotation:-90.0];
49     }
50
51     [(NSProgressIndicator*)m_cocoaNSView setMaxValue:range];
52     [(NSProgressIndicator*)m_cocoaNSView setIndeterminate:NO];
53
54     if(m_parent)
55         m_parent->CocoaAddChild(this);
56     SetInitialFrameRect(pos,size);
57
58     return true;
59 }
60
61 wxGauge::~wxGauge()
62 {
63 }
64
65 int wxGauge::GetValue() const
66 {
67     return (int)[(NSProgressIndicator*)m_cocoaNSView doubleValue];
68 }
69
70 void wxGauge::SetValue(int value)
71 {
72     [(NSProgressIndicator*)m_cocoaNSView setDoubleValue:value];
73 }
74
75 int wxGauge::GetRange() const
76 {
77     return (int)[(NSProgressIndicator*)m_cocoaNSView maxValue];
78 }
79
80 void wxGauge::SetRange(int maxValue)
81 {
82     [(NSProgressIndicator*)m_cocoaNSView setMinValue:0.0];
83     [(NSProgressIndicator*)m_cocoaNSView setMaxValue:maxValue];
84 }
85
86 // NSProgressIndicator is not an NSControl but does respond to
87 // sizeToFit on OS X >= 10.2
88 wxSize wxGauge::DoGetBestSize() const
89 {
90     wxAutoNSAutoreleasePool pool;
91     wxASSERT(GetNSProgressIndicator());
92     NSRect storedRect = [m_cocoaNSView frame];
93     bool didFit = false;
94 NS_DURING
95     [GetNSProgressIndicator() sizeToFit];
96     didFit = true;
97 NS_HANDLER
98     // TODO: if anything other than method not implemented, re-raise
99 NS_ENDHANDLER
100     if(didFit)
101     {
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;
107     }
108     // Cocoa can't tell us the size
109     float height = NSProgressIndicatorPreferredAquaThickness;
110     return wxSize((int)(height*2),(int)height);
111 }
112
113 #endif // wxUSE_GAUGE