]> git.saurik.com Git - wxWidgets.git/blob - src/motif/scrolbar.cpp
Needed some variation; got bored of seeing the compilation errors for this
[wxWidgets.git] / src / motif / scrolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: scrolbar.cpp
3 // Purpose: wxScrollBar
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "scrolbar.h"
14 #endif
15
16 #include "wx/scrolbar.h"
17
18 #include <X11/IntrinsicP.h>
19 #include <Xm/Xm.h>
20 #include <Xm/RowColumn.h>
21 #include <Xm/ScrollBar.h>
22
23 #include <wx/motif/private.h>
24
25 static void wxScrollBarCallback(Widget widget, XtPointer clientData,
26 XmScaleCallbackStruct *cbs);
27
28 #if !USE_SHARED_LIBRARY
29 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
30 #endif
31
32 // Scrollbar
33 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
34 const wxPoint& pos,
35 const wxSize& size, long style,
36 const wxValidator& validator,
37 const wxString& name)
38 {
39 if (!parent)
40 return FALSE;
41 parent->AddChild(this);
42 SetName(name);
43 m_backgroundColour = parent->GetBackgroundColour();
44 m_foregroundColour = parent->GetForegroundColour();
45 SetValidator(validator);
46
47 m_windowStyle = style;
48
49 if ( id == -1 )
50 m_windowId = (int)NewControlId();
51 else
52 m_windowId = id;
53
54 int x = pos.x;
55 int y = pos.y;
56 int width = size.x;
57 int height = size.y;
58
59 if (width == -1)
60 {
61 if (style & wxHORIZONTAL)
62 width = 140;
63 else
64 width = 12;
65 }
66 if (height == -1)
67 {
68 if (style & wxVERTICAL)
69 height = 140;
70 else
71 height = 12;
72 }
73
74 Widget parentWidget = (Widget) parent->GetClientWidget();
75 int direction = (style & wxHORIZONTAL) ? XmHORIZONTAL: XmVERTICAL;
76
77 Widget scrollBarWidget = XtVaCreateManagedWidget("scrollBarWidget",
78 xmScrollBarWidgetClass, parentWidget,
79 XmNorientation, direction,
80 NULL);
81
82 m_mainWidget = (Widget) scrollBarWidget;
83
84 // This will duplicate other events
85 // XtAddCallback(scrollBarWidget, XmNvalueChangedCallback, (XtCallbackProc)wxScrollBarCallback, (XtPointer)this);
86 XtAddCallback(scrollBarWidget, XmNdragCallback, (XtCallbackProc)wxScrollBarCallback, (XtPointer)this);
87 XtAddCallback(scrollBarWidget, XmNdecrementCallback, (XtCallbackProc)wxScrollBarCallback, (XtPointer)this);
88 XtAddCallback(scrollBarWidget, XmNincrementCallback, (XtCallbackProc)wxScrollBarCallback, (XtPointer)this);
89 XtAddCallback(scrollBarWidget, XmNpageDecrementCallback, (XtCallbackProc)wxScrollBarCallback, (XtPointer)this);
90 XtAddCallback(scrollBarWidget, XmNpageIncrementCallback, (XtCallbackProc)wxScrollBarCallback, (XtPointer)this);
91 XtAddCallback(scrollBarWidget, XmNtoTopCallback, (XtCallbackProc)wxScrollBarCallback, (XtPointer)this);
92 XtAddCallback(scrollBarWidget, XmNtoBottomCallback, (XtCallbackProc)wxScrollBarCallback, (XtPointer)this);
93
94 SetCanAddEventHandler(TRUE);
95 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, x, y, width, height);
96 ChangeBackgroundColour();
97
98 return TRUE;
99 }
100
101 wxScrollBar::~wxScrollBar()
102 {
103 }
104
105 void wxScrollBar::SetThumbPosition(int pos)
106 {
107 if (m_mainWidget)
108 {
109 XtVaSetValues ((Widget) m_mainWidget,
110 XmNvalue, pos,
111 NULL);
112 }
113 }
114
115 int wxScrollBar::GetThumbPosition() const
116 {
117 if (m_mainWidget)
118 {
119 int pos;
120 XtVaGetValues((Widget) m_mainWidget,
121 XmNvalue, &pos, NULL);
122 return pos;
123 }
124 else
125 return 0;
126 }
127
128 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
129 bool refresh)
130 {
131 m_viewSize = pageSize;
132 m_pageSize = thumbSize;
133 m_objectSize = range;
134
135 if (range == 0)
136 range = 1;
137 if (thumbSize == 0)
138 thumbSize = 1;
139
140 XtVaSetValues((Widget) m_mainWidget,
141 XmNvalue, position,
142 XmNminimum, 0,
143 XmNmaximum, range,
144 XmNsliderSize, thumbSize,
145 XmNpageIncrement, pageSize,
146 NULL);
147 }
148
149 void wxScrollBar::Command(wxCommandEvent& event)
150 {
151 SetThumbPosition(event.m_commandInt);
152 ProcessCommand(event);
153 }
154
155 void wxScrollBar::ChangeFont(bool keepOriginalSize)
156 {
157 // TODO
158 // Do anything for a scrollbar? A font will never be seen.
159 }
160
161 void wxScrollBar::ChangeBackgroundColour()
162 {
163 wxWindow::ChangeBackgroundColour();
164
165 XtVaSetValues ((Widget) GetMainWidget(),
166 XmNtroughColor, m_backgroundColour.AllocColour(XtDisplay((Widget) GetMainWidget())),
167 NULL);
168 }
169
170 void wxScrollBar::ChangeForegroundColour()
171 {
172 wxWindow::ChangeForegroundColour();
173 }
174
175 static void wxScrollBarCallback(Widget widget, XtPointer clientData,
176 XmScaleCallbackStruct *cbs)
177 {
178 wxScrollBar *scrollBar = (wxScrollBar *)clientData;
179
180 wxEventType eventType = wxEVT_NULL;
181 switch (cbs->reason)
182 {
183 case XmCR_INCREMENT:
184 {
185 eventType = wxEVT_SCROLL_LINEDOWN;
186 break;
187 }
188 case XmCR_DECREMENT:
189 {
190 eventType = wxEVT_SCROLL_LINEUP;
191 break;
192 }
193 case XmCR_DRAG:
194 {
195 eventType = wxEVT_SCROLL_THUMBTRACK;
196 break;
197 }
198 case XmCR_VALUE_CHANGED:
199 {
200 // TODO: Should this be intercepted too, or will it cause
201 // duplicate events?
202 eventType = wxEVT_SCROLL_THUMBTRACK;
203 break;
204 }
205 case XmCR_PAGE_INCREMENT:
206 {
207 eventType = wxEVT_SCROLL_PAGEDOWN;
208 break;
209 }
210 case XmCR_PAGE_DECREMENT:
211 {
212 eventType = wxEVT_SCROLL_PAGEUP;
213 break;
214 }
215 case XmCR_TO_TOP:
216 {
217 eventType = wxEVT_SCROLL_TOP;
218 break;
219 }
220 case XmCR_TO_BOTTOM:
221 {
222 eventType = wxEVT_SCROLL_BOTTOM;
223 break;
224 }
225 default:
226 {
227 // Should never get here
228 wxFAIL_MSG("Unknown scroll event.");
229 break;
230 }
231 }
232
233 wxScrollEvent event(eventType, scrollBar->GetId());
234 event.SetEventObject(scrollBar);
235 event.SetPosition(cbs->value);
236 scrollBar->GetEventHandler()->ProcessEvent(event);
237 /*
238 if (!scrollBar->inSetValue)
239 scrollBar->ProcessCommand(event);
240 */
241 }
242