]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/scrolbar.mm
Add implementation of Objective-C class name uniquifying.
[wxWidgets.git] / src / cocoa / scrolbar.mm
CommitLineData
5369a054 1/////////////////////////////////////////////////////////////////////////////
851dee09 2// Name: src/cocoa/scrolbar.mm
5369a054
DE
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
851dee09 9// Licence: wxWindows licence
5369a054
DE
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
851dee09 13
5369a054
DE
14#if wxUSE_SCROLLBAR
15
851dee09
WS
16#include "wx/scrolbar.h"
17
5369a054
DE
18#ifndef WX_PRECOMP
19 #include "wx/app.h"
5369a054
DE
20#endif //WX_PRECOMP
21
22#import <AppKit/NSScroller.h>
23
24IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
25BEGIN_EVENT_TABLE(wxScrollBar, wxScrollBarBase)
26END_EVENT_TABLE()
27WX_IMPLEMENT_COCOA_OWNER(wxScrollBar,NSScroller,NSControl,NSView)
28
29bool 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
44wxScrollBar::~wxScrollBar()
45{
46 DisassociateNSScroller(GetNSScroller());
47}
48
49/* A word about NSScroller vs. wxScrollbar:
50
51NSScroller uses two float values to represent the state of the scroller.
52The floatValue indicates where the knob is positioned on a scale from
530.0 to 1.0. A value of 0.0 indicates the scroller is at the top or left,
54a value of 1.0 indicates the scroller is at the bottom or right. A value
55of 0.5 indicates the scroller is dead center.
56
57wxScrollBar uses three values. The position indicates the number of
58scroll units where 0 is at the top or left. The range indicates how
59many scroll units there are in the entire bar and the thumb size indicates
60how many scroll units the thumb takes. The scrollbar is at the bottom
61or right when position == range - thumbSize.
62
63It may be easier to consider wx position to be the top or left of the thumb.
64In Cocoa, floatValue can be considered as if it were the center of the
65thumb and the range is ALWAYS (no matter what the knobProportion is) the
66distance between the center point of the knob from one extreme to the other.
67*/
68
69int wxScrollBar::GetThumbPosition() const
70{
71 return (int)((m_range-m_thumbSize)*[GetNSScroller() floatValue]);
72}
73
74void wxScrollBar::SetThumbPosition(int position)
75{
76 [GetNSScroller() setFloatValue:((float)position)/(m_range-m_thumbSize)];
77}
78
79void 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
89void 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