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