-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSpinner.cpp
More file actions
662 lines (554 loc) · 13.1 KB
/
Spinner.cpp
File metadata and controls
662 lines (554 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
Spinner.cpp: A number spinner control.
Written by DarkWyrm <bpmagic@columbus.rr.com>, Copyright 2004
Released under the MIT license.
Original BScrollBarButton class courtesy Haiku project
*/
#include "Spinner.h"
#include <String.h>
#include <ScrollBar.h>
#include <Window.h>
#include <stdio.h>
#include <Font.h>
#include <Box.h>
#include <MessageFilter.h>
#include <ControlLook.h>
#include "global.h"
#include <TextView.h>
enum
{
M_UP='mmup',
M_DOWN,
M_TEXT_CHANGED='mtch'
};
typedef enum
{
ARROW_LEFT=0,
ARROW_RIGHT,
ARROW_UP,
ARROW_DOWN,
ARROW_NONE
} arrow_direction;
class SpinnerMsgFilter : public BMessageFilter
{
public:
SpinnerMsgFilter(void);
~SpinnerMsgFilter(void);
virtual filter_result Filter(BMessage *msg, BHandler **target);
};
class SpinnerArrowButton : public BView
{
public:
SpinnerArrowButton(BPoint location, const char *name,
arrow_direction dir);
~SpinnerArrowButton(void);
void AttachedToWindow(void);
void DetachedToWindow(void);
void MouseDown(BPoint pt);
void MouseUp(BPoint pt);
void MouseMoved(BPoint pt, uint32 code, const BMessage *msg);
void Draw(BRect update);
void SetEnabled(bool value);
bool IsEnabled(void) const { return enabled; }
void SetViewColor(rgb_color color);
private:
arrow_direction fDirection;
BPoint tri1,tri2,tri3;
Spinner *parent;
bool mousedown;
bool enabled;
rgb_color myColor;
};
class SpinnerPrivateData
{
public:
SpinnerPrivateData(void)
{
thumbframe.Set(0,0,0,0);
enabled=true;
tracking=false;
mousept.Set(0,0);
thumbinc=1.0;
repeaterid=-1;
exit_repeater=false;
arrowdown=ARROW_NONE;
#ifdef TEST_MODE
sbinfo.proportional=true;
sbinfo.double_arrows=false;
sbinfo.knob=0;
sbinfo.min_knob_size=14;
#else
get_scroll_bar_info(&sbinfo);
#endif
}
~SpinnerPrivateData(void)
{
if(repeaterid!=-1)
{
exit_repeater=false;
kill_thread(repeaterid);
}
}
thread_id repeaterid;
scroll_bar_info sbinfo;
BRect thumbframe;
bool enabled;
bool tracking;
BPoint mousept;
float thumbinc;
bool exit_repeater;
arrow_direction arrowdown;
};
Spinner::Spinner(BRect frame, const char *name, const char *label, int32 min, int32 max, int32 step, BMessage *msg,
uint32 resize,uint32 flags)
: BControl(frame, name,NULL,msg,resize,flags)
{
BRect r(Bounds());
if(r.Height()<14*2)
r.bottom=r.top+14*2;
ResizeTo(r.Width(),r.Height());
r.right-=14;
font_height fh;
BFont font;
font.GetHeight(&fh);
float textheight=fh.ascent+fh.descent+fh.leading;
BString t("");
t << max;
BFont f(be_plain_font);
float width1 = f.StringWidth(t.String())+2;
float width2 = f.StringWidth(label);
ResizeTo(width1+width2+30, textheight+6); //14*2-2); //+18+14 length of textcontrol
r = Bounds();
r.right -= 14+3; //Distance between textcontrol and spinnerbutton
r.bottom=r.top+textheight+8; //+10; //Changed from 8 to 10, because the spinnerbutton look nicer
r.OffsetTo(0, ( (Bounds().Height()-r.Height())/2) );
fTextControl=new BTextControl(r,"textcontrol",label,"0",new BMessage(M_TEXT_CHANGED),
B_FOLLOW_ALL,B_WILL_DRAW|B_NAVIGABLE);
AddChild(fTextControl);
BTextView *tview=fTextControl->TextView();
tview->SetAlignment(B_ALIGN_RIGHT); //change from left to right not completed
tview->SetWordWrap(false);
if (strcmp(label, "") == 0) //check if Label was set
{
fTextControl->SetDivider(width2);
}
else if (strcmp(label,"") != 0)
{
fTextControl->SetDivider(width2+4);
}
//fTextControl->SetDivider(width2+5);
//fTextControl->SetAlignment(B_ALIGN_RIGHT,B_ALIGN_RIGHT);
BString string("QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,/qwertyuiop{}| "
"asdfghjkl:\"zxcvbnm<>?!@#$%^&*()-_=+`~äöüÄÖÜß\r");
for(int32 i=0; i<string.CountChars(); i++)
{
char c=string.ByteAt(i);
tview->DisallowChar(c);
}
r=Bounds();
r.left=r.right-15;
//r.bottom/=2+4;
//r.top
fUpButton=new SpinnerArrowButton(BPoint(r.left, r.top-1),"up",ARROW_UP); //to make one line with the textcontrol
AddChild(fUpButton);
r=Bounds();
r.left=r.right-15;
r.top=r.bottom/2; //remove +1 to make one line with the textcontrol
fDownButton=new SpinnerArrowButton(BPoint(r.left, r.top),"down",ARROW_DOWN);
AddChild(fDownButton);
fStep=step;
fMin=min;
fMax=max;
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
privatedata=new SpinnerPrivateData;
fFilter=new SpinnerMsgFilter;
SetValue(min);
}
Spinner::~Spinner(void)
{
delete privatedata;
delete fFilter;
}
void Spinner::AttachedToWindow(void)
{
Window()->AddCommonFilter(fFilter);
fTextControl->SetTarget(this);
}
void Spinner::DetachedFromWindow(void)
{
Window()->RemoveCommonFilter(fFilter);
}
void Spinner::SetValue(int32 value)
{
if(value>GetMax() || value<GetMin())
return;
BControl::SetValue(value);
char string[50];
sprintf(string,"%ld",value);
fTextControl->SetText(string);
}
void Spinner::SetViewColor(rgb_color color)
{
BControl::SetViewColor(color);
fTextControl->SetViewColor(color);
fTextControl->SetLowColor(color);
fUpButton->SetViewColor(color);
fDownButton->SetViewColor(color);
}
void Spinner::SetLabel(const char *text)
{
fTextControl->SetLabel(text);
}
void Spinner::ValueChanged(int32 value)
{
}
void Spinner::MessageReceived(BMessage *msg)
{
if(msg->what==M_TEXT_CHANGED) //
{
BString string(fTextControl->Text());
int32 newvalue=0;
sscanf(string.String(),"%d",&newvalue); //change from %ld to %d | lorglas 11.02.2020
if(newvalue>=GetMin() && newvalue<=GetMax())
{
// new value is in range, so set it and go
SetValue(newvalue);
Invoke();
Draw(Bounds());
ValueChanged(Value());
}
else
{
// new value is out of bounds. Clip to range if current value is not
// at the end of its range
if(newvalue<GetMin() && Value()!=GetMin())
{
SetValue(GetMin());
Invoke();
Draw(Bounds());
ValueChanged(Value());
}
else if(newvalue>GetMax() && Value()!=GetMax())
{
SetValue(GetMax());
Invoke();
Draw(Bounds());
ValueChanged(Value());
}
else
{
char string[100];
sprintf(string,"%ld",Value());
fTextControl->SetText(string);
}
}
}
else
BControl::MessageReceived(msg);
}
void Spinner::SetSteps(int32 stepsize)
{
fStep=stepsize;
}
void Spinner::SetRange(int32 min, int32 max)
{
SetMin(min);
SetMax(max);
}
void Spinner::GetRange(int32 *min, int32 *max)
{
*min=fMin;
*max=fMax;
}
void Spinner::SetMax(int32 max)
{
fMax=max;
if(Value()>fMax)
SetValue(fMax);
}
void Spinner::SetMin(int32 min)
{
fMin=min;
if(Value()<fMin)
SetValue(fMin);
}
void Spinner::SetEnabled(bool value)
{
if(IsEnabled()==value)
return;
BControl::SetEnabled(value);
fTextControl->SetEnabled(value);
fTextControl->TextView()->MakeSelectable(value);
fUpButton->SetEnabled(value);
fDownButton->SetEnabled(value);
}
void Spinner::MakeFocus(bool value)
{
fTextControl->MakeFocus(value);
}
SpinnerArrowButton::SpinnerArrowButton(BPoint location,
const char *name, arrow_direction dir)
:BView(BRect(0,0,16,12).OffsetToCopy(location),
name, B_FOLLOW_ALL, B_WILL_DRAW)
{
fDirection=dir;
enabled=true;
myColor = ui_color(B_PANEL_BACKGROUND_COLOR);
mousedown=false;
}
SpinnerArrowButton::~SpinnerArrowButton(void)
{
}
void SpinnerArrowButton::MouseDown(BPoint pt)
{
if(enabled==false)
return;
if (!IsEnabled())
return;
mousedown=true;
int redcost = 1000;
Draw(Bounds());
BRect bounds = Bounds();
uint32 buttons;
BPoint point;
int32 step=parent->GetSteps();
int32 newvalue=parent->Value();
int32 waitvalue=150000;
do
{
if(fDirection==ARROW_UP)
{
parent->privatedata->arrowdown=ARROW_UP;
newvalue+=step;
}
else
{
parent->privatedata->arrowdown=ARROW_DOWN;
newvalue-=step;
}
if( newvalue>=parent->GetMin() && newvalue<=parent->GetMax())
{
// new value is in range, so set it and go
parent->SetValue(newvalue);
parent->Invoke();
// parent->Draw(parent->Bounds());
parent->ValueChanged(parent->Value());
}
else
{
// new value is out of bounds. Clip to range if current value is not
// at the end of its range
if(newvalue<parent->GetMin() && parent->Value()!=parent->GetMin())
{
parent->SetValue(parent->GetMin());
parent->Invoke();
// parent->Draw(parent->Bounds());
parent->ValueChanged(parent->Value());
}
else
if(newvalue>parent->GetMax() && parent->Value()!=parent->GetMax())
{
parent->SetValue(parent->GetMax());
parent->Invoke();
// parent->Draw(parent->Bounds());
parent->ValueChanged(parent->Value());
}
else
{
// cases which go here are if new value is <minimum and value already at
// minimum or if > maximum and value already at maximum
return;
}
}
Window()->UpdateIfNeeded();
snooze(waitvalue);
GetMouse(&point, &buttons, false);
bool inside = bounds.Contains(point);
//if ((parent->Value() == B_CONTROL_ON) != inside)
// parent->SetValue(inside ? B_CONTROL_ON : B_CONTROL_OFF);
if(waitvalue<=50000)
waitvalue=50000;
else
{
waitvalue -= redcost;
redcost = redcost*10;
}
} while (buttons != 0);
}
void SpinnerArrowButton::MouseUp(BPoint pt)
{
if(enabled)
{
mousedown=false;
if(parent)
{
parent->privatedata->arrowdown=ARROW_NONE;
parent->privatedata->exit_repeater=true;
}
Draw(Bounds());
}
}
void SpinnerArrowButton::MouseMoved(BPoint pt, uint32 transit, const BMessage *msg)
{
if(enabled==false)
return;
if(transit==B_ENTERED_VIEW)
{
BPoint point;
uint32 buttons;
GetMouse(&point,&buttons);
if( (buttons & B_PRIMARY_MOUSE_BUTTON)==0 &&
(buttons & B_SECONDARY_MOUSE_BUTTON)==0 &&
(buttons & B_PRIMARY_MOUSE_BUTTON)==0 )
mousedown=false;
else
mousedown=true;
Draw(Bounds());
}
if(transit==B_EXITED_VIEW || transit==B_OUTSIDE_VIEW)
MouseUp(Bounds().LeftTop());
}
void SpinnerArrowButton::Draw(BRect update)
{
BRect rect(Bounds());
rgb_color background = B_TRANSPARENT_COLOR;
if (Parent())
background = Parent()->ViewColor();
if (background == B_TRANSPARENT_COLOR)
background = ui_color(B_PANEL_BACKGROUND_COLOR);
rgb_color base = ui_color(B_PANEL_BACKGROUND_COLOR);
uint32 flags = 0;
if(!parent->IsEnabled())
flags |= BControlLook::B_DISABLED;
if(mousedown)
flags = 1 << 2;
BRect r(Bounds());
if(fDirection == ARROW_UP)
r.bottom = r.bottom*2;
else
r.top = - r.bottom;
be_control_look->DrawButtonFrame(this, r, update, base, background, flags);
be_control_look->DrawButtonBackground(this, r, update, base, flags);
rect.InsetBy(2.0,2.0);
base = ui_color(B_PANEL_TEXT_COLOR);
float tint = B_NO_TINT;
if(!parent->IsEnabled())
tint = B_LIGHTEN_MAX_TINT;
be_control_look->DrawArrowShape(this, rect, update, base, fDirection, flags, tint);
}
void SpinnerArrowButton::AttachedToWindow(void)
{
parent=(Spinner*)Parent();
}
void SpinnerArrowButton::DetachedToWindow(void)
{
parent=NULL;
}
void SpinnerArrowButton::SetEnabled(bool value)
{
enabled=value;
Invalidate();
}
void SpinnerArrowButton::SetViewColor(rgb_color color)
{
myColor = color;
Invalidate();
}
SpinnerMsgFilter::SpinnerMsgFilter(void)
: BMessageFilter(B_PROGRAMMED_DELIVERY, B_ANY_SOURCE,B_KEY_DOWN)
{
}
SpinnerMsgFilter::~SpinnerMsgFilter(void)
{
}
filter_result SpinnerMsgFilter::Filter(BMessage *msg, BHandler **target)
{
int32 c;
msg->FindInt32("raw_char",&c);
switch(c)
{
case B_ENTER:
{
BTextView *text=dynamic_cast<BTextView*>(*target);
if(text && text->IsFocus())
{
BView *view=text->Parent();
while(view)
{
Spinner *spin=dynamic_cast<Spinner*>(view);
if(spin)
{
BString string(text->Text());
int32 newvalue=0;
sscanf(string.String(),"%d",&newvalue); //change from %ld to %d | lorglas 11.02.2020
//printf("%d",newvalue);
if(newvalue!=spin->Value())
{
spin->SetValue(newvalue);
spin->Invoke();
}
return B_SKIP_MESSAGE;
}
view=view->Parent();
}
}
return B_DISPATCH_MESSAGE;
}
case B_TAB:
{
return B_DISPATCH_MESSAGE;
}
/*case B_TAB:
{
// Cause Tab characters to perform keybaord navigation
BHandler *h=*target;
BView *v=NULL;
h=h->NextHandler();
while(h)
{
v=dynamic_cast<BView*>(h);
if(v)
{
*target=v->Window();
return B_DISPATCH_MESSAGE;
}
h=h->NextHandler();
}
return B_SKIP_MESSAGE;
// return B_DISPATCH_MESSAGE;
}*/
case B_UP_ARROW:
case B_DOWN_ARROW:
{
BTextView *text=dynamic_cast<BTextView*>(*target);
if(text && text->IsFocus())
{
// We have a text view. See if it currently has the focus and belongs
// to a Spinner control. If so, change the value of the spinner
// TextViews are complicated beasts which are actually multiple views.
// Travel up the hierarchy to see if any of the target's ancestors are
// a Spinner.
BView *view=text->Parent();
while(view)
{
Spinner *spin=dynamic_cast<Spinner*>(view);
if(spin)
{
int32 step=spin->GetSteps();
if(c==B_DOWN_ARROW)
step=0-step;
spin->SetValue(spin->Value()+step);
spin->Invoke();
return B_SKIP_MESSAGE;
}
view=view->Parent();
}
}
return B_DISPATCH_MESSAGE;
}
default:
return B_DISPATCH_MESSAGE;
}
// shut the stupid compiler up
return B_SKIP_MESSAGE;
}