]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/gauge.mm
Ensure there is valid context for DrawRectangle
[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 // RCS-ID: $Id$
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_GAUGE
15
16 #include "wx/gauge.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/app.h"
20 #include "wx/log.h"
21 #endif //WX_PRECOMP
22
23 #include "wx/cocoa/autorelease.h"
24
25 #import <AppKit/NSProgressIndicator.h>
26 #import <Foundation/NSException.h>
27
28 #include <math.h>
29
30 BEGIN_EVENT_TABLE(wxGauge, wxGaugeBase)
31 END_EVENT_TABLE()
32 // WX_IMPLEMENT_COCOA_OWNER(wxGauge,NSProgressIndicator,NSView,NSView)
33
34 bool wxGauge::Create(wxWindow *parent, wxWindowID winid, int range,
35 const wxPoint& pos, const wxSize& size, long style,
36 const wxValidator& validator, const wxString& name)
37 {
38 // NOTE: wxGA_SMOOTH flag is simply ignored (gauges are ALWAYS smooth)
39 if(!CreateControl(parent,winid,pos,size,style,validator,name))
40 return false;
41 SetNSView([[NSProgressIndicator alloc] initWithFrame: MakeDefaultNSRect(size)]);
42 [m_cocoaNSView release];
43
44 // TODO: DoGetBestSize is likely totally wrong for vertical gauges but
45 // this actually makes the widgets sample work so it's better than nothing.
46 if(style & wxGA_VERTICAL)
47 {
48 wxLogDebug(wxT("wxGA_VERTICAL may not work correctly. See src/cocoa/gauge.mm"));
49 [m_cocoaNSView setBoundsRotation:-90.0];
50 }
51
52 [(NSProgressIndicator*)m_cocoaNSView setMaxValue:range];
53 [(NSProgressIndicator*)m_cocoaNSView setIndeterminate:NO];
54
55 if(m_parent)
56 m_parent->CocoaAddChild(this);
57 SetInitialFrameRect(pos,size);
58
59 return true;
60 }
61
62 wxGauge::~wxGauge()
63 {
64 }
65
66 int wxGauge::GetValue() const
67 {
68 return (int)[(NSProgressIndicator*)m_cocoaNSView doubleValue];
69 }
70
71 void wxGauge::SetValue(int value)
72 {
73 [(NSProgressIndicator*)m_cocoaNSView setDoubleValue:value];
74 }
75
76 int wxGauge::GetRange() const
77 {
78 return (int)[(NSProgressIndicator*)m_cocoaNSView maxValue];
79 }
80
81 void wxGauge::SetRange(int maxValue)
82 {
83 [(NSProgressIndicator*)m_cocoaNSView setMinValue:0.0];
84 [(NSProgressIndicator*)m_cocoaNSView setMaxValue:maxValue];
85 }
86
87 // NSProgressIndicator is not an NSControl but does respond to
88 // sizeToFit on OS X >= 10.2
89 wxSize wxGauge::DoGetBestSize() const
90 {
91 wxAutoNSAutoreleasePool pool;
92 wxASSERT(GetNSProgressIndicator());
93 NSRect storedRect = [m_cocoaNSView frame];
94 bool didFit = false;
95 NS_DURING
96 [GetNSProgressIndicator() sizeToFit];
97 didFit = true;
98 NS_HANDLER
99 // TODO: if anything other than method not implemented, re-raise
100 NS_ENDHANDLER
101 if(didFit)
102 {
103 NSRect cocoaRect = [m_cocoaNSView frame];
104 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
105 [m_cocoaNSView setFrame: storedRect];
106 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
107 return /*wxConstCast(this, wxControl)->m_bestSize =*/ size;
108 }
109 // Cocoa can't tell us the size
110 float height = NSProgressIndicatorPreferredAquaThickness;
111 return wxSize((int)(height*2),(int)height);
112 }
113
114 #endif // wxUSE_GAUGE