Misc. Motif; removed duplicate wxICON; variant compile fix; added wxString form
[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 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 SetValidator(validator);
44
45 m_windowStyle = style;
46
47 if ( id == -1 )
48 m_windowId = (int)NewControlId();
49 else
50 m_windowId = id;
51
52 int x = pos.x;
53 int y = pos.y;
54 int width = size.x;
55 int height = size.y;
56
57 if (width == -1)
58 {
59 if (style & wxHORIZONTAL)
60 width = 140;
61 else
62 width = 12;
63 }
64 if (height == -1)
65 {
66 if (style & wxVERTICAL)
67 height = 140;
68 else
69 height = 12;
70 }
71
72 Widget parentWidget = (Widget) parent->GetClientWidget();
73 int direction = (style & wxHORIZONTAL) ? XmHORIZONTAL: XmVERTICAL;
74
75 Widget scrollBarWidget = XtVaCreateManagedWidget("scrollBarWidget",
76 xmScrollBarWidgetClass, parentWidget,
77 XmNorientation, direction,
78 NULL);
79
80 m_mainWidget = (Widget) scrollBarWidget;
81
82 // This will duplicate other events
83 // XtAddCallback(scrollBarWidget, XmNvalueChangedCallback, (XtCallbackProc)wxScrollBarCallback, (XtPointer)this);
84 XtAddCallback(scrollBarWidget, XmNdragCallback, (XtCallbackProc)wxScrollBarCallback, (XtPointer)this);
85 XtAddCallback(scrollBarWidget, XmNdecrementCallback, (XtCallbackProc)wxScrollBarCallback, (XtPointer)this);
86 XtAddCallback(scrollBarWidget, XmNincrementCallback, (XtCallbackProc)wxScrollBarCallback, (XtPointer)this);
87 XtAddCallback(scrollBarWidget, XmNpageDecrementCallback, (XtCallbackProc)wxScrollBarCallback, (XtPointer)this);
88 XtAddCallback(scrollBarWidget, XmNpageIncrementCallback, (XtCallbackProc)wxScrollBarCallback, (XtPointer)this);
89 XtAddCallback(scrollBarWidget, XmNtoTopCallback, (XtCallbackProc)wxScrollBarCallback, (XtPointer)this);
90 XtAddCallback(scrollBarWidget, XmNtoBottomCallback, (XtCallbackProc)wxScrollBarCallback, (XtPointer)this);
91
92 SetCanAddEventHandler(TRUE);
93 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, x, y, width, height);
94 ChangeColour(m_mainWidget);
95
96 return TRUE;
97 }
98
99 wxScrollBar::~wxScrollBar()
100 {
101 }
102
103 void wxScrollBar::SetPosition(int pos)
104 {
105 if (m_mainWidget)
106 {
107 XtVaSetValues ((Widget) m_mainWidget,
108 XmNvalue, pos,
109 NULL);
110 }
111 }
112
113 int wxScrollBar::GetPosition() const
114 {
115 if (m_mainWidget)
116 {
117 int pos;
118 XtVaGetValues((Widget) m_mainWidget,
119 XmNvalue, &pos, NULL);
120 return pos;
121 }
122 else
123 return 0;
124 }
125
126 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
127 bool refresh)
128 {
129 m_viewSize = pageSize;
130 m_pageSize = thumbSize;
131 m_objectSize = range;
132
133 if (range == 0)
134 range = 1;
135 if (thumbSize == 0)
136 thumbSize = 1;
137
138 XtVaSetValues((Widget) m_mainWidget,
139 XmNvalue, position,
140 XmNminimum, 0,
141 XmNmaximum, range,
142 XmNsliderSize, thumbSize,
143 XmNpageIncrement, pageSize,
144 NULL);
145 }
146
147 void wxScrollBar::Command(wxCommandEvent& event)
148 {
149 SetPosition(event.m_commandInt);
150 ProcessCommand(event);
151 }
152
153 void wxScrollBarCallback(Widget widget, XtPointer clientData,
154 XmScaleCallbackStruct *cbs)
155 {
156 wxScrollBar *scrollBar = (wxScrollBar *)clientData;
157
158 wxEventType eventType = wxEVT_NULL;
159 switch (cbs->reason)
160 {
161 case XmCR_INCREMENT:
162 {
163 eventType = wxEVT_SCROLL_LINEDOWN;
164 break;
165 }
166 case XmCR_DECREMENT:
167 {
168 eventType = wxEVT_SCROLL_LINEUP;
169 break;
170 }
171 case XmCR_DRAG:
172 {
173 eventType = wxEVT_SCROLL_THUMBTRACK;
174 break;
175 }
176 case XmCR_VALUE_CHANGED:
177 {
178 // TODO: Should this be intercepted too, or will it cause
179 // duplicate events?
180 eventType = wxEVT_SCROLL_THUMBTRACK;
181 break;
182 }
183 case XmCR_PAGE_INCREMENT:
184 {
185 eventType = wxEVT_SCROLL_PAGEDOWN;
186 break;
187 }
188 case XmCR_PAGE_DECREMENT:
189 {
190 eventType = wxEVT_SCROLL_PAGEUP;
191 break;
192 }
193 case XmCR_TO_TOP:
194 {
195 eventType = wxEVT_SCROLL_TOP;
196 break;
197 }
198 case XmCR_TO_BOTTOM:
199 {
200 eventType = wxEVT_SCROLL_BOTTOM;
201 break;
202 }
203 default:
204 {
205 // Should never get here
206 wxFAIL_MSG("Unknown scroll event.");
207 break;
208 }
209 }
210
211 wxScrollEvent event(eventType, scrollBar->GetId());
212 event.SetEventObject(scrollBar);
213 event.SetPosition(cbs->value);
214 scrollBar->GetEventHandler()->ProcessEvent(event);
215 /*
216 if (!scrollBar->inSetValue)
217 scrollBar->ProcessCommand(event);
218 */
219 }
220