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