]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/scrolbar.mm
fix compilation both for Robert's cross-compiler and for all normal compilers by...
[wxWidgets.git] / src / cocoa / scrolbar.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/scrolbar.mm
3 // Purpose: wxScrollBar
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2004/04/25
7 // RCS-ID: $Id$
8 // Copyright: (c) 2004 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_SCROLLBAR
15
16 #include "wx/scrolbar.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/app.h"
20 #endif //WX_PRECOMP
21
22 #import <AppKit/NSScroller.h>
23
24 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
25 BEGIN_EVENT_TABLE(wxScrollBar, wxScrollBarBase)
26 END_EVENT_TABLE()
27 WX_IMPLEMENT_COCOA_OWNER(wxScrollBar,NSScroller,NSControl,NSView)
28
29 bool wxScrollBar::Create(wxWindow *parent, wxWindowID winid,
30 const wxPoint& pos, const wxSize& size, long style,
31 const wxValidator& validator, const wxString& name)
32 {
33 if(!CreateControl(parent,winid,pos,size,style,validator,name))
34 return false;
35 SetNSScroller([[NSScroller alloc] initWithFrame: MakeDefaultNSRect(size)]);
36 [m_cocoaNSView release];
37 if(m_parent)
38 m_parent->CocoaAddChild(this);
39 SetInitialFrameRect(pos,size);
40
41 return true;
42 }
43
44 wxScrollBar::~wxScrollBar()
45 {
46 DisassociateNSScroller(GetNSScroller());
47 }
48
49 /* A word about NSScroller vs. wxScrollbar:
50
51 NSScroller uses two float values to represent the state of the scroller.
52 The floatValue indicates where the knob is positioned on a scale from
53 0.0 to 1.0. A value of 0.0 indicates the scroller is at the top or left,
54 a value of 1.0 indicates the scroller is at the bottom or right. A value
55 of 0.5 indicates the scroller is dead center.
56
57 wxScrollBar uses three values. The position indicates the number of
58 scroll units where 0 is at the top or left. The range indicates how
59 many scroll units there are in the entire bar and the thumb size indicates
60 how many scroll units the thumb takes. The scrollbar is at the bottom
61 or right when position == range - thumbSize.
62
63 It may be easier to consider wx position to be the top or left of the thumb.
64 In Cocoa, floatValue can be considered as if it were the center of the
65 thumb and the range is ALWAYS (no matter what the knobProportion is) the
66 distance between the center point of the knob from one extreme to the other.
67 */
68
69 int wxScrollBar::GetThumbPosition() const
70 {
71 return (int)((m_range-m_thumbSize)*[GetNSScroller() floatValue]);
72 }
73
74 void wxScrollBar::SetThumbPosition(int position)
75 {
76 [GetNSScroller() setFloatValue:((float)position)/(m_range-m_thumbSize)];
77 }
78
79 void wxScrollBar::SetScrollbar(int position, int thumbSize,
80 int range, int pageSize, bool refresh)
81 {
82 m_range = range;
83 m_thumbSize = thumbSize;
84 m_pageSize = pageSize;
85 [GetNSScroller() setFloatValue:((float)position)/(m_range-m_thumbSize)
86 knobProportion:((float)m_thumbSize)/m_range];
87 }
88
89 void wxScrollBar::Cocoa_wxNSScrollerAction()
90 {
91 NSScrollerPart hitPart = [GetNSScroller() hitPart];
92 wxEventType command;
93 // Note: the comments about the part that is hit are for OS X, the
94 // constants are sort of a leftover from NeXT. It makes more sense if
95 // you remember that in NeXT clicking the knob slot used to do what
96 // option-clicking does now.
97 switch(hitPart)
98 {
99 // User dragged knob
100 case NSScrollerKnob:
101 command = wxEVT_SCROLL_THUMBTRACK;
102 break;
103 // User option-clicked slot
104 case NSScrollerKnobSlot:
105 command = wxEVT_SCROLL_THUMBTRACK;
106 break;
107 // User clicked Up/Left button
108 case NSScrollerDecrementLine:
109 command = wxEVT_SCROLL_LINEUP;
110 break;
111 // User option-clicked Up/left or clicked in slot
112 case NSScrollerDecrementPage:
113 command = wxEVT_SCROLL_PAGEUP;
114 break;
115 // User clicked Down/Right button
116 case NSScrollerIncrementLine:
117 command = wxEVT_SCROLL_LINEDOWN;
118 break;
119 // User option-clicked Down/Right or clicked in slot
120 case NSScrollerIncrementPage:
121 command = wxEVT_SCROLL_PAGEDOWN;
122 break;
123 // No-op
124 case NSScrollerNoPart:
125 default:
126 return;
127 }
128 // TODO: When scrolling by pages, readjust the floatValue using the
129 // pageSize (which may be different from thumbSize).
130 wxScrollEvent event(command, GetId(), GetThumbPosition(),
131 HasFlag(wxSB_VERTICAL)?wxVERTICAL:wxHORIZONTAL);
132 event.SetEventObject(this);
133 GetEventHandler()->ProcessEvent(event);
134 }
135
136 #endif // wxUSE_SCROLLBAR