| 1 |
/* GuiScrollBar.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1993-2011,2012,2013,2014,2015,2016,2017,2020,2022 Paul Boersma, 2013 Tom Naughton |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "GuiP.h" |
| 20 |
|
| 21 |
Thing_implement (GuiScrollBar, GuiControl, 0); |
| 22 |
|
| 23 |
#if motif |
| 24 |
#define iam_scrollbar \ |
| 25 |
Melder_assert (widget -> widgetClass == xmScrollBarWidgetClass); \ |
| 26 |
GuiScrollBar me = (GuiScrollBar) widget -> userData |
| 27 |
#else |
| 28 |
#define iam_scrollbar \ |
| 29 |
GuiScrollBar me = (GuiScrollBar) _GuiObject_getUserData (widget) |
| 30 |
#endif |
| 31 |
|
| 32 |
#if gtk |
| 33 |
static void _GuiGtkScrollBar_destroyCallback (GuiObject /* widget */, gpointer void_me) { |
| 34 |
iam (GuiScrollBar); |
| 35 |
forget (me); |
| 36 |
} |
| 37 |
static void _GuiGtkScrollBar_valueChangedCallback (GuiObject widget, gpointer void_me) { |
| 38 |
iam (GuiScrollBar); |
| 39 |
trace (U"enter: blocked ", my d_blockValueChangedCallbacks); |
| 40 |
trace (U"_GuiGtkScrollBar_valueChangedCallback: ", GuiScrollBar_getValue (me)); |
| 41 |
if (my d_blockValueChangedCallbacks) { |
| 42 |
my d_blockValueChangedCallbacks = false; |
| 43 |
} else if (my d_valueChangedCallback) { |
| 44 |
struct structGuiScrollBarEvent event { me }; |
| 45 |
try { |
| 46 |
my d_valueChangedCallback (my d_valueChangedBoss, & event); |
| 47 |
} catch (MelderError) { |
| 48 |
Melder_flushError (U"Your action in a scroll bar was not completely handled."); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
#elif motif |
| 53 |
void _GuiWinScrollBar_destroy (GuiObject widget) { |
| 54 |
_GuiNativeControl_destroy (widget); |
| 55 |
iam_scrollbar; |
| 56 |
forget (me); // NOTE: my widget is not destroyed here |
| 57 |
} |
| 58 |
#elif cocoa |
| 59 |
// http://www.lucernesys.com/blog/2010/02/11/nsscroller/ |
| 60 |
|
| 61 |
@implementation GuiCocoaScrollBar { |
| 62 |
GuiScrollBar d_userData; |
| 63 |
@public |
| 64 |
double m_minimum; |
| 65 |
double m_maximum; |
| 66 |
double m_value; |
| 67 |
double m_sliderSize; |
| 68 |
double m_increment; |
| 69 |
double m_pageIncrement; |
| 70 |
} |
| 71 |
- (void) dealloc { // override |
| 72 |
GuiScrollBar me = self -> d_userData; |
| 73 |
forget (me); |
| 74 |
trace (U"deleting a scroll bar"); |
| 75 |
[super dealloc]; |
| 76 |
} |
| 77 |
- (GuiThing) getUserData { |
| 78 |
return self -> d_userData; |
| 79 |
} |
| 80 |
- (void) setUserData: (GuiThing) userData { |
| 81 |
Melder_assert (userData == nullptr || Thing_isa (userData, classGuiScrollBar)); |
| 82 |
self -> d_userData = static_cast <GuiScrollBar> (userData); |
| 83 |
} |
| 84 |
- (void) setMinimum: (double)minimum maximum:(double)maximum value:(double)value sliderSize:(double)sliderSize increment:(double)increment pageIncrement:(double)pageIncrement { |
| 85 |
Melder_assert (isdefined (minimum)); |
| 86 |
self -> m_minimum = minimum; |
| 87 |
self -> m_maximum = maximum; |
| 88 |
self -> m_value = value; |
| 89 |
self -> m_sliderSize = sliderSize; |
| 90 |
self -> m_increment = increment; |
| 91 |
self -> m_pageIncrement = pageIncrement; |
| 92 |
const double spaceLeft = (maximum - minimum) - sliderSize; |
| 93 |
if (spaceLeft <= 0.0) { |
| 94 |
[self setKnobProportion: 1.0]; |
| 95 |
[self setDoubleValue: 0.5]; |
| 96 |
} else { |
| 97 |
[self setKnobProportion: sliderSize / (maximum - minimum)]; |
| 98 |
[self setDoubleValue: (value - minimum) / spaceLeft]; |
| 99 |
} |
| 100 |
} |
| 101 |
- (void) _update { |
| 102 |
GuiScrollBar me = (GuiScrollBar) self -> d_userData; |
| 103 |
[self setMinimum: self -> m_minimum maximum: self -> m_maximum value: self -> m_value |
| 104 |
sliderSize: self -> m_sliderSize increment: self -> m_increment pageIncrement: self -> m_pageIncrement]; |
| 105 |
if (my d_valueChangedCallback) { |
| 106 |
struct structGuiScrollBarEvent event { me }; |
| 107 |
try { |
| 108 |
my d_valueChangedCallback (my d_valueChangedBoss, & event); |
| 109 |
} catch (MelderError) { |
| 110 |
Melder_flushError (U"Scroll not completely handled."); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
- (void) scrollBy: (double) step { |
| 115 |
trace (U"step ", step); |
| 116 |
if (step == 0) |
| 117 |
return; |
| 118 |
self -> m_value -= 0.3 * step * self -> m_increment; |
| 119 |
if (self -> m_value < self -> m_minimum) |
| 120 |
self -> m_value = self -> m_minimum; |
| 121 |
if (self -> m_value > self -> m_maximum - self -> m_sliderSize) |
| 122 |
self -> m_value = self -> m_maximum - self -> m_sliderSize; |
| 123 |
[self _update]; |
| 124 |
} |
| 125 |
- (void) magnifyBy: (double) step { |
| 126 |
trace (U"step ", step); |
| 127 |
const double increase = self -> m_sliderSize * (exp (- step) - 1.0); |
| 128 |
self -> m_sliderSize += increase; |
| 129 |
Melder_clipRight (& self -> m_sliderSize, self -> m_maximum - self -> m_minimum); |
| 130 |
self -> m_value -= 0.5 * increase; |
| 131 |
Melder_clip (self -> m_minimum, & self -> m_value, self -> m_maximum - self -> m_sliderSize); |
| 132 |
[self _update]; |
| 133 |
} |
| 134 |
- (void) valueChanged { |
| 135 |
GuiScrollBar me = (GuiScrollBar) d_userData; |
| 136 |
switch ([self hitPart]) { |
| 137 |
case NSScrollerIncrementLine: { |
| 138 |
// Include code here for the case where the down arrow is pressed |
| 139 |
self -> m_value += self -> m_increment; |
| 140 |
Melder_clipRight (& self -> m_value, self -> m_maximum - self -> m_sliderSize); |
| 141 |
[self setMinimum: self -> m_minimum maximum: self -> m_maximum value: self -> m_value |
| 142 |
sliderSize: self -> m_sliderSize increment: self -> m_increment pageIncrement: self -> m_pageIncrement]; |
| 143 |
} break; |
| 144 |
case NSScrollerIncrementPage: { |
| 145 |
// Include code here for the case where CTRL + down arrow is pressed, or the space the scroll knob moves in is pressed |
| 146 |
self -> m_value += self -> m_pageIncrement; |
| 147 |
Melder_clipRight (& self -> m_value, self -> m_maximum - self -> m_sliderSize); |
| 148 |
[self setMinimum: self -> m_minimum maximum: self -> m_maximum value: self -> m_value |
| 149 |
sliderSize: self -> m_sliderSize increment: self -> m_increment pageIncrement: self -> m_pageIncrement]; |
| 150 |
} break; |
| 151 |
case NSScrollerDecrementLine: { |
| 152 |
// Include code here for the case where the up arrow is pressed |
| 153 |
self -> m_value -= self -> m_increment; |
| 154 |
Melder_clipLeft (self -> m_minimum, & self -> m_value); |
| 155 |
[self setMinimum: self -> m_minimum maximum: self -> m_maximum value: self -> m_value |
| 156 |
sliderSize: self -> m_sliderSize increment: self -> m_increment pageIncrement: self -> m_pageIncrement]; |
| 157 |
} break; |
| 158 |
case NSScrollerDecrementPage: { |
| 159 |
// Include code here for the case where CTRL + up arrow is pressed, or the space the scroll knob moves in is pressed |
| 160 |
self -> m_value -= self -> m_pageIncrement; |
| 161 |
Melder_clipLeft (self -> m_minimum, & self -> m_value); |
| 162 |
[self setMinimum: self -> m_minimum maximum: self -> m_maximum value: self -> m_value |
| 163 |
sliderSize: self -> m_sliderSize increment: self -> m_increment pageIncrement: self -> m_pageIncrement]; |
| 164 |
} break; |
| 165 |
case NSScrollerKnob: { |
| 166 |
// This case is when the knob itself is pressed |
| 167 |
const double spaceLeft = (self -> m_maximum - self -> m_minimum) - self -> m_sliderSize; |
| 168 |
self -> m_value = self -> m_minimum + [self doubleValue] * (spaceLeft <= 0.0 ? 0.0 : spaceLeft); |
| 169 |
} break; |
| 170 |
default: { |
| 171 |
} break; |
| 172 |
} |
| 173 |
if (my d_valueChangedCallback) { |
| 174 |
struct structGuiScrollBarEvent event { me }; |
| 175 |
try { |
| 176 |
my d_valueChangedCallback (my d_valueChangedBoss, & event); |
| 177 |
} catch (MelderError) { |
| 178 |
Melder_flushError (U"Scroll not completely handled."); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
@end |
| 183 |
#endif |
| 184 |
#if motif |
| 185 |
static void _GuiMotifScrollBar_valueChangedCallback (GuiObject widget, XtPointer void_me, XtPointer call) { |
| 186 |
iam (GuiScrollBar); |
| 187 |
if (my d_valueChangedCallback) { |
| 188 |
struct structGuiScrollBarEvent event { me }; |
| 189 |
try { |
| 190 |
my d_valueChangedCallback (my d_valueChangedBoss, & event); |
| 191 |
} catch (MelderError) { |
| 192 |
Melder_flushError (U"Scroll not completely handled."); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
#endif |
| 197 |
|
| 198 |
GuiScrollBar GuiScrollBar_create (GuiForm parent, int left, int right, int top, int bottom, |
| 199 |
double minimum, double maximum, double value, double sliderSize, double increment, double pageIncrement, |
| 200 |
GuiScrollBarCallback valueChangedCallback, Thing valueChangedBoss, uint32 flags) |
| 201 |
{ |
| 202 |
autoGuiScrollBar me = Thing_new (GuiScrollBar); |
| 203 |
my d_shell = parent -> d_shell; |
| 204 |
my d_parent = parent; |
| 205 |
my d_valueChangedCallback = valueChangedCallback; |
| 206 |
my d_valueChangedBoss = valueChangedBoss; |
| 207 |
#if gtk |
| 208 |
GtkAdjustment *adjustment = gtk_adjustment_new (value, minimum, maximum, increment, pageIncrement, sliderSize); |
| 209 |
GtkOrientation orientation = ( flags & GuiScrollBar_HORIZONTAL ? GTK_ORIENTATION_HORIZONTAL : GTK_ORIENTATION_VERTICAL ); |
| 210 |
my d_widget = gtk_scrollbar_new (orientation, adjustment); |
| 211 |
// TODO: figure out whether we have to delete the adjustment |
| 212 |
_GuiObject_setUserData (my d_widget, me.get()); |
| 213 |
my v_positionInForm (my d_widget, left, right, top, bottom, parent); |
| 214 |
g_signal_connect (G_OBJECT (my d_widget), "value-changed", G_CALLBACK (_GuiGtkScrollBar_valueChangedCallback), me.get()); |
| 215 |
#elif motif |
| 216 |
my d_widget = XtVaCreateWidget (flags & GuiScrollBar_HORIZONTAL ? "horizontalScrollBar" : "verticalScrollBar", // the name is checked for deciding the orientation... |
| 217 |
xmScrollBarWidgetClass, parent -> d_widget, |
| 218 |
XmNorientation, flags & GuiScrollBar_HORIZONTAL ? XmHORIZONTAL : XmVERTICAL, |
| 219 |
XmNminimum, (int) minimum, |
| 220 |
XmNmaximum, (int) maximum, |
| 221 |
XmNvalue, (int) value, |
| 222 |
XmNsliderSize, (int) sliderSize, |
| 223 |
XmNincrement, (int) increment, |
| 224 |
XmNpageIncrement, (int) pageIncrement, |
| 225 |
nullptr); |
| 226 |
_GuiObject_setUserData (my d_widget, me.get()); |
| 227 |
my v_positionInForm (my d_widget, left, right, top, bottom, parent); |
| 228 |
XtAddCallback (my d_widget, XmNvalueChangedCallback, _GuiMotifScrollBar_valueChangedCallback, (XtPointer) me.get()); |
| 229 |
XtAddCallback (my d_widget, XmNdragCallback, _GuiMotifScrollBar_valueChangedCallback, (XtPointer) me.get()); |
| 230 |
#elif cocoa |
| 231 |
NSRect dummyFrame = flags & GuiScrollBar_HORIZONTAL ? NSMakeRect (20, 20, 100, [NSScroller scrollerWidth]) : NSMakeRect (20, 20, [NSScroller scrollerWidth], 100); |
| 232 |
GuiCocoaScrollBar *scroller = [[GuiCocoaScrollBar alloc] initWithFrame: dummyFrame]; |
| 233 |
my d_widget = scroller; |
| 234 |
my v_positionInForm (my d_widget, left, right, top, bottom, parent); |
| 235 |
[scroller setUserData: me.get()]; |
| 236 |
[scroller setEnabled: YES]; |
| 237 |
[scroller setMinimum: minimum maximum: maximum value: value sliderSize: sliderSize increment: increment pageIncrement: pageIncrement]; |
| 238 |
//[scroller setScrollerStyle: NSScrollerStyleOverlay]; |
| 239 |
[scroller setTarget: scroller]; |
| 240 |
[scroller setAction: @selector (valueChanged)]; |
| 241 |
#endif |
| 242 |
return me.releaseToAmbiguousOwner(); |
| 243 |
} |
| 244 |
|
| 245 |
GuiScrollBar GuiScrollBar_createShown (GuiForm parent, int left, int right, int top, int bottom, |
| 246 |
double minimum, double maximum, double value, double sliderSize, double increment, double pageIncrement, |
| 247 |
GuiScrollBarCallback valueChangedCallback, Thing valueChangedBoss, uint32 flags) |
| 248 |
{ |
| 249 |
GuiScrollBar me = GuiScrollBar_create (parent, left, right, top, bottom, |
| 250 |
minimum, maximum, value, sliderSize, increment, pageIncrement, |
| 251 |
valueChangedCallback, valueChangedBoss, flags); |
| 252 |
GuiThing_show (me); |
| 253 |
return me; |
| 254 |
} |
| 255 |
|
| 256 |
void GuiScrollBar_set (GuiScrollBar me, double minimum, double maximum, double value, double sliderSize, double increment, double pageIncrement) { |
| 257 |
/* |
| 258 |
* This function calls the native scroll bar modification function. |
| 259 |
* |
| 260 |
* Note: |
| 261 |
* On almost all platforms, using the native scroll bar modification function sends a value-changed notification to the scroll bar. |
| 262 |
* This will call our own d_valueChangedCallback if we don't prevent it. |
| 263 |
* We have to prevent that, because our d_valueChangedCallback is only for user-initiated modifications. |
| 264 |
*/ |
| 265 |
trace (U"enter ", minimum, U" ", maximum, U" ", value, U" ", sliderSize, U" ", increment, U" ", pageIncrement); |
| 266 |
#if gtk |
| 267 |
/* |
| 268 |
* We're going to modify the scroll bar with gtk_adjustment_configure (). |
| 269 |
* This function sends a *slow* value-changed notification to the scroll bar. |
| 270 |
* We have to make sure that our own d_valueChangedCallback is not called. |
| 271 |
*/ |
| 272 |
my d_blockValueChangedCallbacks = true; |
| 273 |
GtkAdjustment *adj = gtk_range_get_adjustment (GTK_RANGE (my d_widget)); |
| 274 |
gtk_adjustment_configure (GTK_ADJUSTMENT (adj), |
| 275 |
isdefined (value) ? value : gtk_adjustment_get_value (GTK_ADJUSTMENT (adj)), |
| 276 |
isdefined (minimum) ? minimum : gtk_adjustment_get_lower (GTK_ADJUSTMENT (adj)), |
| 277 |
isdefined (maximum) ? maximum : gtk_adjustment_get_upper (GTK_ADJUSTMENT (adj)), |
| 278 |
isdefined (increment) ? increment : gtk_adjustment_get_step_increment (GTK_ADJUSTMENT (adj)), |
| 279 |
isdefined (pageIncrement) ? pageIncrement : gtk_adjustment_get_page_increment (GTK_ADJUSTMENT (adj)), |
| 280 |
isdefined (sliderSize) ? sliderSize : gtk_adjustment_get_page_size (GTK_ADJUSTMENT (adj))); |
| 281 |
/* |
| 282 |
* We don't set d_blockValueChangedCallbacks back to false yet, because GTK calls the valueChangedCallback with a delay. |
| 283 |
*/ |
| 284 |
#elif motif |
| 285 |
if (isdefined (minimum)) |
| 286 |
XtVaSetValues (my d_widget, XmNminimum, (int) minimum, nullptr); |
| 287 |
if (isdefined (maximum)) |
| 288 |
XtVaSetValues (my d_widget, XmNmaximum, (int) maximum, nullptr); |
| 289 |
int oldValue, oldSliderSize, oldIncrement, oldPageIncrement; |
| 290 |
XmScrollBarGetValues (my d_widget, & oldValue, & oldSliderSize, & oldIncrement, & oldPageIncrement); |
| 291 |
XmScrollBarSetValues (my d_widget, |
| 292 |
isdefined (value) ? value : oldValue, |
| 293 |
isdefined (sliderSize) ? sliderSize : oldSliderSize, |
| 294 |
isdefined (increment) ? increment : oldIncrement, |
| 295 |
isdefined (pageIncrement) ? pageIncrement : oldPageIncrement, |
| 296 |
False); |
| 297 |
#elif cocoa |
| 298 |
/* |
| 299 |
* We're going to modify the scroll bar with setMinimum:maximum:... |
| 300 |
* This function sends a *synchronous* value-changed notification to the scroll bar. |
| 301 |
* We have to make sure that our own d_valueChangedCallback is not called. |
| 302 |
*/ |
| 303 |
GuiControlBlockValueChangedCallbacks block (me); |
| 304 |
GuiCocoaScrollBar *scroller = (GuiCocoaScrollBar *) my d_widget; |
| 305 |
[scroller |
| 306 |
setMinimum: isdefined (minimum) ? minimum : scroller -> m_minimum |
| 307 |
maximum: isdefined (maximum) ? maximum : scroller -> m_maximum |
| 308 |
value: isdefined (value) ? value : scroller -> m_value |
| 309 |
sliderSize: isdefined (sliderSize) ? sliderSize : scroller -> m_sliderSize |
| 310 |
increment: isdefined (increment) ? increment : scroller -> m_increment |
| 311 |
pageIncrement: isdefined (pageIncrement) ? pageIncrement : scroller -> m_pageIncrement]; |
| 312 |
#endif |
| 313 |
trace (U"exit"); |
| 314 |
} |
| 315 |
|
| 316 |
double GuiScrollBar_getValue (GuiScrollBar me) { |
| 317 |
#if gtk |
| 318 |
return gtk_range_get_value (GTK_RANGE (my d_widget)); |
| 319 |
#elif motif |
| 320 |
int value, slider, incr, pincr; |
| 321 |
XmScrollBarGetValues (my d_widget, & value, & slider, & incr, & pincr); |
| 322 |
return value; |
| 323 |
#elif cocoa |
| 324 |
GuiCocoaScrollBar *scroller = (GuiCocoaScrollBar *) my d_widget; |
| 325 |
return scroller -> m_value; |
| 326 |
#else |
| 327 |
return 0; |
| 328 |
#endif |
| 329 |
} |
| 330 |
|
| 331 |
double GuiScrollBar_getSliderSize (GuiScrollBar me) { |
| 332 |
#if gtk |
| 333 |
return 1; // NYI |
| 334 |
#elif motif |
| 335 |
int value, slider, incr, pincr; |
| 336 |
XmScrollBarGetValues (my d_widget, & value, & slider, & incr, & pincr); |
| 337 |
return slider; |
| 338 |
#elif cocoa |
| 339 |
GuiCocoaScrollBar *scroller = (GuiCocoaScrollBar *) my d_widget; |
| 340 |
return scroller -> m_sliderSize; |
| 341 |
#else |
| 342 |
return 0; |
| 343 |
#endif |
| 344 |
} |
| 345 |
|
| 346 |
/* End of file GuiScrollBar.cpp */ |