]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/osx/cocoa/scrolbar.mm | |
3 | // Purpose: wxScrollBar | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id: scrolbar.cpp 54129 2008-06-11 19:30:52Z SC $ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #include "wx/scrolbar.h" | |
15 | ||
16 | #ifndef WX_PRECOMP | |
17 | #include "wx/intl.h" | |
18 | #include "wx/log.h" | |
19 | #include "wx/settings.h" | |
20 | #endif | |
21 | ||
22 | #include "wx/math.h" | |
23 | #include "wx/osx/private.h" | |
24 | ||
25 | @interface wxNSScroller : NSScroller | |
26 | { | |
27 | } | |
28 | @end | |
29 | ||
30 | @implementation wxNSScroller | |
31 | ||
32 | + (void)initialize | |
33 | { | |
34 | static BOOL initialized = NO; | |
35 | if (!initialized) | |
36 | { | |
37 | initialized = YES; | |
38 | wxOSXCocoaClassAddWXMethods(self); | |
39 | } | |
40 | } | |
41 | ||
42 | @end | |
43 | ||
44 | class wxOSXScrollBarCocoaImpl : public wxWidgetCocoaImpl | |
45 | { | |
46 | public : | |
47 | wxOSXScrollBarCocoaImpl( wxWindowMac* peer, WXWidget w) : wxWidgetCocoaImpl( peer, w ) | |
48 | { | |
49 | m_maximum = 1; | |
50 | } | |
51 | ||
52 | void SetMaximum(wxInt32 v) | |
53 | { | |
54 | m_maximum = (v == 0) ? 1 : v; | |
55 | } | |
56 | ||
57 | void SetScrollThumb( wxInt32 value, wxInt32 thumbSize ) | |
58 | { | |
59 | double v = ((double) value)/m_maximum; | |
60 | double t = ((double) thumbSize)/(m_maximum+thumbSize); | |
61 | #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 | |
62 | [(wxNSScroller*) m_osxView setFloatValue:v knobProportion:t]; | |
63 | #else | |
64 | [(wxNSScroller*) m_osxView setDoubleValue:v]; | |
65 | [(wxNSScroller*) m_osxView setKnobProportion:t]; | |
66 | #endif | |
67 | } | |
68 | ||
69 | virtual wxInt32 GetValue() const | |
70 | { | |
71 | return wxRound([(wxNSScroller*) m_osxView floatValue] * m_maximum); | |
72 | } | |
73 | ||
74 | virtual wxInt32 GetMaximum() const | |
75 | { | |
76 | return m_maximum; | |
77 | } | |
78 | ||
79 | virtual void controlAction(WXWidget slf, void* _cmd, void *sender); | |
80 | virtual void mouseEvent(WX_NSEvent event, WXWidget slf, void* _cmd); | |
81 | protected: | |
82 | wxInt32 m_maximum; | |
83 | }; | |
84 | ||
85 | // we will have a mouseDown, then in the native | |
86 | // implementation of mouseDown the tracking code | |
87 | // is calling clickedAction, therefore we wire this | |
88 | // to thumbtrack and only after super mouseDown | |
89 | // returns we will call the thumbrelease | |
90 | ||
91 | void wxOSXScrollBarCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender)) | |
92 | { | |
93 | wxEventType scrollEvent = wxEVT_NULL; | |
94 | switch ([(NSScroller*)m_osxView hitPart]) | |
95 | { | |
96 | case NSScrollerIncrementLine: | |
97 | scrollEvent = wxEVT_SCROLL_LINEDOWN; | |
98 | break; | |
99 | case NSScrollerIncrementPage: | |
100 | scrollEvent = wxEVT_SCROLL_PAGEDOWN; | |
101 | break; | |
102 | case NSScrollerDecrementLine: | |
103 | scrollEvent = wxEVT_SCROLL_LINEUP; | |
104 | break; | |
105 | case NSScrollerDecrementPage: | |
106 | scrollEvent = wxEVT_SCROLL_PAGEUP; | |
107 | break; | |
108 | case NSScrollerKnob: | |
109 | case NSScrollerKnobSlot: | |
110 | scrollEvent = wxEVT_SCROLL_THUMBTRACK; | |
111 | break; | |
112 | case NSScrollerNoPart: | |
113 | default: | |
114 | return; | |
115 | } | |
116 | ||
117 | wxWindow* wxpeer = (wxWindow*) GetWXPeer(); | |
118 | if ( wxpeer ) | |
119 | wxpeer->TriggerScrollEvent(scrollEvent); | |
120 | } | |
121 | ||
122 | void wxOSXScrollBarCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd) | |
123 | { | |
124 | wxWidgetCocoaImpl::mouseEvent(event, slf, _cmd); | |
125 | ||
126 | // send a release event in case we've been tracking the thumb | |
127 | if ( strcmp( sel_getName((SEL) _cmd) , "mouseDown:") == 0 ) | |
128 | { | |
129 | NSScrollerPart hit = [(NSScroller*)m_osxView hitPart]; | |
130 | if ( (hit == NSScrollerKnob || hit == NSScrollerKnobSlot) ) | |
131 | { | |
132 | wxWindow* wxpeer = (wxWindow*) GetWXPeer(); | |
133 | if ( wxpeer ) | |
134 | wxpeer->OSXHandleClicked(0); | |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | wxWidgetImplType* wxWidgetImpl::CreateScrollBar( wxWindowMac* wxpeer, | |
140 | wxWindowMac* WXUNUSED(parent), | |
141 | wxWindowID WXUNUSED(id), | |
142 | const wxPoint& pos, | |
143 | const wxSize& size, | |
144 | long style, | |
145 | long WXUNUSED(extraStyle)) | |
146 | { | |
147 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; | |
148 | // the creation rect defines the orientation | |
149 | NSRect createRect = ( style & wxSB_HORIZONTAL ) ? NSMakeRect(r.origin.x, r.origin.y , 17, 16) : | |
150 | NSMakeRect(r.origin.x, r.origin.y , 16, 17); | |
151 | wxNSScroller* v = [[wxNSScroller alloc] initWithFrame:createRect]; | |
152 | [v setFrame:r]; | |
153 | ||
154 | wxWidgetCocoaImpl* c = new wxOSXScrollBarCocoaImpl( wxpeer, v ); | |
155 | [v setEnabled:YES]; | |
156 | return c; | |
157 | } |