Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / src / motif / scrolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/scrolbar.cpp
3 // Purpose: wxScrollBar
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #include "wx/scrolbar.h"
15
16 #ifdef __VMS__
17 #pragma message disable nosimpint
18 #endif
19 #include <Xm/Xm.h>
20 #include <Xm/ScrollBar.h>
21 #ifdef __VMS__
22 #pragma message enable nosimpint
23 #endif
24
25 #include "wx/motif/private.h"
26
27 static void wxScrollBarCallback(Widget widget, XtPointer clientData,
28 XmScaleCallbackStruct *cbs);
29
30 // Scrollbar
31 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
32 const wxPoint& pos,
33 const wxSize& size, long style,
34 const wxValidator& validator,
35 const wxString& name)
36 {
37 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
38 return false;
39 PreCreation();
40
41 wxSize newSize =
42 ( style & wxHORIZONTAL ) ? wxSize( 140, 16 ) : wxSize( 16, 140 );
43 if( size.x != -1 ) newSize.x = size.x;
44 if( size.y != -1 ) newSize.y = size.y;
45
46 Widget parentWidget = (Widget) parent->GetClientWidget();
47
48 m_mainWidget =
49 DoCreateScrollBar( (WXWidget)parentWidget,
50 (wxOrientation)(style & (wxHORIZONTAL|wxVERTICAL)),
51 (void (*)())wxScrollBarCallback );
52
53 PostCreation();
54 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
55 pos.x, pos.y, newSize.x, newSize.y);
56
57 return true;
58 }
59
60 wxScrollBar::~wxScrollBar()
61 {
62 }
63
64 void wxScrollBar::SetThumbPosition(int pos)
65 {
66 XtVaSetValues ((Widget) m_mainWidget,
67 XmNvalue, pos,
68 NULL);
69 }
70
71 int wxScrollBar::GetThumbPosition() const
72 {
73 int pos;
74 XtVaGetValues((Widget) m_mainWidget,
75 XmNvalue, &pos, NULL);
76 return pos;
77 }
78
79 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
80 bool WXUNUSED(refresh))
81 {
82 m_viewSize = pageSize;
83 m_pageSize = thumbSize;
84 m_objectSize = range;
85
86 if (range == 0)
87 range = 1;
88 if (thumbSize == 0)
89 thumbSize = 1;
90
91 XtVaSetValues((Widget) m_mainWidget,
92 XmNvalue, position,
93 XmNminimum, 0,
94 XmNmaximum, range,
95 XmNsliderSize, thumbSize,
96 XmNpageIncrement, pageSize,
97 NULL);
98 }
99
100 void wxScrollBar::Command(wxCommandEvent& event)
101 {
102 SetThumbPosition(event.GetInt());
103 ProcessCommand(event);
104 }
105
106 void wxScrollBar::ChangeFont(bool WXUNUSED(keepOriginalSize))
107 {
108 // TODO
109 // Do anything for a scrollbar? A font will never be seen.
110 }
111
112 void wxScrollBar::ChangeBackgroundColour()
113 {
114 wxWindow::ChangeBackgroundColour();
115
116 XtVaSetValues ((Widget) GetMainWidget(),
117 XmNtroughColor, m_backgroundColour.AllocColour(XtDisplay((Widget) GetMainWidget())),
118 NULL);
119 }
120
121 static void wxScrollBarCallback(Widget widget, XtPointer clientData,
122 XmScaleCallbackStruct *cbs)
123 {
124 wxScrollBar *scrollBar = (wxScrollBar*)wxGetWindowFromTable(widget);
125 wxCHECK_RET( scrollBar, wxT("invalid widget in scrollbar callback") );
126
127 wxOrientation orientation = (wxOrientation)wxPtrToUInt(clientData);
128 wxEventType eventType = wxEVT_NULL;
129
130 switch (cbs->reason)
131 {
132 case XmCR_INCREMENT:
133 {
134 eventType = wxEVT_SCROLL_LINEDOWN;
135 break;
136 }
137 case XmCR_DECREMENT:
138 {
139 eventType = wxEVT_SCROLL_LINEUP;
140 break;
141 }
142 case XmCR_DRAG:
143 {
144 eventType = wxEVT_SCROLL_THUMBTRACK;
145 break;
146 }
147 case XmCR_VALUE_CHANGED:
148 {
149 eventType = wxEVT_SCROLL_THUMBRELEASE;
150 break;
151 }
152 case XmCR_PAGE_INCREMENT:
153 {
154 eventType = wxEVT_SCROLL_PAGEDOWN;
155 break;
156 }
157 case XmCR_PAGE_DECREMENT:
158 {
159 eventType = wxEVT_SCROLL_PAGEUP;
160 break;
161 }
162 case XmCR_TO_TOP:
163 {
164 eventType = wxEVT_SCROLL_TOP;
165 break;
166 }
167 case XmCR_TO_BOTTOM:
168 {
169 eventType = wxEVT_SCROLL_BOTTOM;
170 break;
171 }
172 default:
173 {
174 // Should never get here
175 wxFAIL_MSG("Unknown scroll event.");
176 break;
177 }
178 }
179
180 wxScrollEvent event(eventType, scrollBar->GetId(),
181 cbs->value, orientation);
182 event.SetEventObject(scrollBar);
183 scrollBar->HandleWindowEvent(event);
184 }