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