]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/gauge.mm
fix memory leak in wxScreenDC, fixes #13249
[wxWidgets.git] / src / cocoa / gauge.mm
CommitLineData
24e97652 1/////////////////////////////////////////////////////////////////////////////
9d2c19f1 2// Name: src/cocoa/gauge.mm
24e97652
DE
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
526954c5 9// Licence: wxWindows licence
24e97652
DE
10/////////////////////////////////////////////////////////////////////////////
11
449c5673 12#include "wx/wxprec.h"
9d2c19f1 13
24e97652
DE
14#if wxUSE_GAUGE
15
9d2c19f1
WS
16#include "wx/gauge.h"
17
449c5673
DE
18#ifndef WX_PRECOMP
19 #include "wx/app.h"
15bc1a64 20 #include "wx/log.h"
449c5673 21#endif //WX_PRECOMP
24e97652 22
15bc1a64
DE
23#include "wx/cocoa/autorelease.h"
24
24e97652 25#import <AppKit/NSProgressIndicator.h>
15bc1a64
DE
26#import <Foundation/NSException.h>
27
28#include <math.h>
24e97652 29
24e97652
DE
30BEGIN_EVENT_TABLE(wxGauge, wxGaugeBase)
31END_EVENT_TABLE()
32// WX_IMPLEMENT_COCOA_OWNER(wxGauge,NSProgressIndicator,NSView,NSView)
33
34bool 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{
f22c3fa8 38 // NOTE: wxGA_SMOOTH flag is simply ignored (gauges are ALWAYS smooth)
24e97652
DE
39 if(!CreateControl(parent,winid,pos,size,style,validator,name))
40 return false;
8d656ea9 41 SetNSView([[NSProgressIndicator alloc] initWithFrame: MakeDefaultNSRect(size)]);
24e97652 42 [m_cocoaNSView release];
363f7de0 43
f22c3fa8
DE
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 {
2e11bb42 48 wxLogDebug(wxT("wxGA_VERTICAL may not work correctly. See src/cocoa/gauge.mm"));
f22c3fa8
DE
49 [m_cocoaNSView setBoundsRotation:-90.0];
50 }
51
363f7de0
DE
52 [(NSProgressIndicator*)m_cocoaNSView setMaxValue:range];
53 [(NSProgressIndicator*)m_cocoaNSView setIndeterminate:NO];
54
24e97652
DE
55 if(m_parent)
56 m_parent->CocoaAddChild(this);
8d656ea9
DE
57 SetInitialFrameRect(pos,size);
58
24e97652
DE
59 return true;
60}
61
62wxGauge::~wxGauge()
63{
64}
65
363f7de0
DE
66int wxGauge::GetValue() const
67{
2108cc97 68 return (int)[(NSProgressIndicator*)m_cocoaNSView doubleValue];
363f7de0
DE
69}
70
71void wxGauge::SetValue(int value)
72{
73 [(NSProgressIndicator*)m_cocoaNSView setDoubleValue:value];
74}
75
76int wxGauge::GetRange() const
77{
2108cc97 78 return (int)[(NSProgressIndicator*)m_cocoaNSView maxValue];
363f7de0
DE
79}
80
81void wxGauge::SetRange(int maxValue)
82{
83 [(NSProgressIndicator*)m_cocoaNSView setMinValue:0.0];
84 [(NSProgressIndicator*)m_cocoaNSView setMaxValue:maxValue];
85}
86
15bc1a64
DE
87// NSProgressIndicator is not an NSControl but does respond to
88// sizeToFit on OS X >= 10.2
89wxSize wxGauge::DoGetBestSize() const
90{
91 wxAutoNSAutoreleasePool pool;
92 wxASSERT(GetNSProgressIndicator());
93 NSRect storedRect = [m_cocoaNSView frame];
94 bool didFit = false;
95NS_DURING
96 [GetNSProgressIndicator() sizeToFit];
97 didFit = true;
98NS_HANDLER
99 // TODO: if anything other than method not implemented, re-raise
100NS_ENDHANDLER
101 if(didFit)
102 {
103 NSRect cocoaRect = [m_cocoaNSView frame];
c05c7cb5 104 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
15bc1a64
DE
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
24e97652 114#endif // wxUSE_GAUGE