aboutsummaryrefslogtreecommitdiff
path: root/Src/Wasabi/api/wnd/wndclass/scrollbar.cpp
blob: ce3e5b033a9b5094453ceda4aeaf681cb9a7cbfd (plain) (blame)
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
#include <precomp.h>

#include <bfc/wasabi_std.h>

#include "scrollbar.h"
#include <tataki/canvas/canvas.h>
#include <api/wnd/notifmsg.h>
#include <api/wnd/PaintCanvas.h>

#define TIMER_ID  9871
#define TIMER_ID2 9872

#define FIRST_DELAY 350
#define NEXT_DELAY  75

ScrollBar::ScrollBar() {
	leftrgn = NULL;
	rightrgn = NULL;
	buttonrgn = NULL;

	position = 0;
	moving = 0;
	lefting = 0;
	righting = 0;
	clicked = 0;
	height = DEFAULT_HEIGHT;
	buttonx = 0;

	shiftleft = 0;
	shiftright = 0;

	curmouseposition = POS_NONE;
	clickmouseposition = POS_NONE;
	pageing = 0;
	timer = timer2 = 0;
	npages = 100;
	pageway = PAGE_NONE;
	updown = 256;
	insetpos = 0;
	clickbuttonx = 0;
	vertical = 0;
	firstdelay = 0;
	lastx = lasty = 0;
}

ScrollBar::~ScrollBar() {
  deleteResources();
}

void ScrollBar::deleteResources() {
  delete leftrgn; leftrgn = NULL;
  delete buttonrgn; buttonrgn = NULL;
  delete rightrgn; rightrgn = NULL;
}

// this one is inherited
void ScrollBar::freeResources() {
  SCROLLBAR_PARENT::freeResources();
  deleteResources();
}

void ScrollBar::reloadResources() {
  SCROLLBAR_PARENT::reloadResources();
  loadBmps();
}


int ScrollBar::onMouseMove (int x, int y) {

  SCROLLBAR_PARENT::onMouseMove(x, y);
  lastx = x;
  lasty = y;

  if (clicked && clickmouseposition == POS_BUTTON) {

    POINT pt={x,y};
    int x;
    if (!vertical) 
      x = pt.x - clickpos.x;
    else
      x = pt.y - clickpos.y;

    RECT r;
    getClientRect(&r);
    int maxwidth;
    if (!vertical)
      maxwidth = (r.right-r.left)-(shiftright+shiftleft+bmpbutton.getWidth())+1;
    else
      maxwidth = (r.bottom-r.top)-(shiftright+shiftleft+bmpbutton.getHeight())+1;
    buttonx = MIN(MAX(clickbuttonx + x, 0), maxwidth);
    calcPosition();
    invalidate();

  } else {

    int oldposition = curmouseposition;
    curmouseposition = getMousePosition();
    if (oldposition != curmouseposition) invalidate();

    if (curmouseposition != POS_NONE && !getCapture())
      beginCapture();

    if (curmouseposition == POS_NONE && getCapture() && !clicked && !pageing)
      endCapture();
  }


  return 1;
}

int ScrollBar::getWidth() {
  if (!bmpbutton) return 0;
  if (!vertical)
    return bmpbutton.getHeight();
  else
    return bmpbutton.getWidth();
  return 0;
}

int ScrollBar::getMousePosition() {
  int v = POS_NONE;

  POINT pt={lastx, lasty};

  RECT c;
  getClientRect(&c);
  pt.x -= c.left;
  pt.y -= c.top;

  api_region *l, *b, *r;
  l = leftrgn->clone();
  b = buttonrgn->clone();
  if (!vertical)
    b->offset(buttonx+shiftleft, 0);
  else
    b->offset(0, buttonx+shiftleft);
  r = rightrgn->clone();
  if (!vertical)
    r->offset(c.right-c.left-bmpleft.getWidth(), 0);
  else
    r->offset(0, c.bottom-c.top-bmpleft.getHeight());

  if (b->ptInRegion(&pt))
    v = POS_BUTTON;
  if (l->ptInRegion(&pt))
    v = POS_LEFT;
  if (r->ptInRegion(&pt))
    v = POS_RIGHT;

  leftrgn->disposeClone(l);
  buttonrgn->disposeClone(b);
  rightrgn->disposeClone(r);

  return v;
}

int ScrollBar::onLeftButtonDown(int x, int y) {
  clickmouseposition = getMousePosition();
  if (!pageing && clickmouseposition != POS_NONE) {
    clicked = 1;
    if (clickmouseposition == POS_LEFT || clickmouseposition == POS_RIGHT)
      handleUpDown();
    if (clickmouseposition) {
      clickpos.x = lastx;
      clickpos.y = lasty;
      clickbuttonx = buttonx;
    }
  } else {
    clicked = 0;
    pageing = 1;
    handlePageUpDown();
  }
  invalidate();
  return 1;
}

void ScrollBar::handleUpDown() {
  setTimer(TIMER_ID2, FIRST_DELAY);
  timer2 = 1;
  firstdelay = 1;

  checkUpDown();
}

int ScrollBar::checkUpDown() {
  if (!clicked) {
    if (timer2) {
      killTimer(TIMER_ID2);
      timer2 = 0;
      return 1;
    }
  }

  if (getMousePosition() == clickmouseposition)
    upDown(clickmouseposition);

  return 1;

}

void ScrollBar::handlePageUpDown() {

  setTimer(TIMER_ID, FIRST_DELAY);
  timer = 1;
  firstdelay = 1;

  checkPageUpDown();
}

int ScrollBar::checkPageUpDown() {

  if (!pageing) {
    if (timer) {
      killTimer(TIMER_ID);
      timer = 0;
      pageway = PAGE_NONE;
      return 1;
    }
  }

  POINT pt={lastx,lasty};
  RECT c;
  getClientRect(&c);
  pt.x -= c.left;
  pt.y -= c.top;
  
  if (!vertical) {
    int middlebutton = shiftleft + buttonx + bmpbutton.getWidth()/2;
    api_region *r = buttonrgn->clone();
    r->offset(buttonx+shiftleft, 0);
    if (pt.x > middlebutton && !r->ptInRegion(&pt) && pageway != PAGE_DOWN)
      pageUp();
    if (pt.x < middlebutton && !r->ptInRegion(&pt) && pageway != PAGE_UP)
      pageDown();
    buttonrgn->disposeClone(r);
  } else {
    int middlebutton = shiftleft + buttonx + bmpbutton.getHeight()/2;
    api_region *r = buttonrgn->clone();
    r->offset(0, buttonx+shiftleft);
    if (pt.y > middlebutton && !r->ptInRegion(&pt) && pageway != PAGE_DOWN)
      pageUp();
    if (pt.y < middlebutton && !r->ptInRegion(&pt) && pageway != PAGE_UP)
      pageDown();
    buttonrgn->disposeClone(r);
  }
  return 1;

}

int ScrollBar::onLeftButtonUp(int x, int y) {
  clicked = 0;
  clickmouseposition = POS_NONE;
  curmouseposition = POS_NONE;
  onMouseMove(x,y);
  if (pageing) {
    pageing = 0;
    checkPageUpDown();
  }
  onSetFinalPosition();
  invalidate();
  return 1;
}

int ScrollBar::onRightButtonDown(int x, int y) {
  return 1;
}

int ScrollBar::onRightButtonUp(int x, int y) {
  return 1;
}

int ScrollBar::onMouseWheelUp(int clicked, int lines) {
  return 1;
}

int ScrollBar::onMouseWheelDown(int clicked, int lines) {
  return 1;
}

int ScrollBar::onPaint(Canvas *canvas) {
  AutoSkinBitmap &thisleft = curmouseposition == POS_LEFT ? (clicked ? bmplpressed : bmplhilite) : bmpleft;
  AutoSkinBitmap &thisbutton = curmouseposition == POS_BUTTON ? (clicked ? bmpbpressed : bmpbhilite) : bmpbutton;
  AutoSkinBitmap &thisright = curmouseposition == POS_RIGHT ? (clicked ? bmprpressed : bmprhilite) : bmpright;

  if (curmouseposition != clickmouseposition && clicked) {
    thisleft = bmpleft;
    thisbutton = bmpbutton;
    thisright = bmpright;
  }

  RECT r;
  PaintBltCanvas paintcanvas;

  if (canvas == NULL) {
    if (!paintcanvas.beginPaint(this)) return 0;
    canvas = &paintcanvas;
  }
  SCROLLBAR_PARENT::onPaint(canvas);

  getClientRect(&r);

  renderBaseTexture(canvas, r);

  if (!vertical) {
    RECT c;

    c.left = r.left;
    c.right = r.left;
    c.top = r.top;
    c.bottom = r.bottom;
    if (bmpbackgroundleft.getBitmap()) {
      c.right = c.left + bmpbackgroundleft.getWidth();
      bmpbackgroundleft.getBitmap()->stretchToRectAlpha(canvas, &c);
    }
    int l = c.right;
    c.left = r.right;
    c.right = r.right;
    if (bmpbackgroundright.getBitmap()) {
      c.left = r.right - bmpbackgroundright.getWidth();
      bmpbackgroundright.getBitmap()->stretchToRectAlpha(canvas, &c);
    }
    c.right = c.left;
    c.left = l;
    if (bmpbackgroundmiddle.getBitmap()) {
      bmpbackgroundmiddle.getBitmap()->stretchToRectAlpha(canvas, &c);
    }

    c.left = r.left + buttonx+shiftleft;
    c.top = r.top + 0;
    c.right = r.left + buttonx+thisbutton.getWidth()+shiftleft;
    c.bottom = r.top + getWidth();

    thisbutton.stretchToRectAlpha(canvas, &c);

    c.left = r.left;
    c.top = r.top;
    c.right = r.left + thisleft.getWidth();
    c.bottom = r.top + getWidth();

    thisleft.stretchToRectAlpha(canvas, &c);

    c.left = r.right-thisright.getWidth();
    c.top = r.top;
    c.right = r.right;
    c.bottom = r.top+getWidth();

    thisright.stretchToRectAlpha(canvas, &c);
  } else {
    RECT c;

    c.top = r.top;
    c.bottom = r.top;
    c.left = r.left;
    c.right = r.right;
    if (bmpbackgroundleft.getBitmap()) {
      c.bottom = c.top + bmpbackgroundleft.getHeight();
      bmpbackgroundleft.getBitmap()->stretchToRectAlpha(canvas, &c);
    }
    int l = c.bottom;
    c.top = r.bottom;
    c.bottom = r.bottom;
    if (bmpbackgroundright.getBitmap()) {
      c.top = r.bottom - bmpbackgroundright.getHeight();
      bmpbackgroundright.getBitmap()->stretchToRectAlpha(canvas, &c);
    }
    c.bottom = c.top;
    c.top = l;
    if (bmpbackgroundmiddle.getBitmap()) {
      bmpbackgroundmiddle.getBitmap()->stretchToRectAlpha(canvas, &c);
    }

    c.left = r.right - thisleft.getWidth();
    c.top = r.top+buttonx + shiftleft;
    c.right = r.right;
    c.bottom = r.top+buttonx+thisbutton.getHeight() + shiftleft;

    thisbutton.stretchToRectAlpha(canvas, &c);

    c.left = r.right - thisleft.getWidth();
    c.top = r.top;
    c.right = r.right;
    c.bottom = r.top+thisleft.getHeight();

    thisleft.stretchToRectAlpha(canvas, &c);

    c.left = r.right-thisright.getWidth();
    c.top = r.bottom-thisright.getHeight();
    c.right = r.right;
    c.bottom = r.bottom;

    thisright.stretchToRectAlpha(canvas, &c);
  }

  return 1;
}

int ScrollBar::getHeight() {
  return height;
}

void ScrollBar::setHeight(int newheight) {
  height = newheight;
}

int ScrollBar::onResize() {
  calcXPosition();
  invalidate();
  return 1;
}

int ScrollBar::onInit() {
  SCROLLBAR_PARENT::onInit();
  return 1;
}

void ScrollBar::setBitmaps(wchar_t *left, wchar_t *lpressed, wchar_t *lhilite,
                  wchar_t *right, wchar_t *rpressed, wchar_t *rhilite, 
                  wchar_t *button, wchar_t *bpressed, wchar_t *bhilite) {

  deleteResources();

  bmpleft = left;
  bmplpressed = lpressed;
  bmplhilite = lhilite;
  bmpright = right;
  bmprpressed = rpressed;
  bmprhilite = rhilite;
  bmpbutton = button;
  bmpbpressed = bpressed;
  bmpbhilite = bhilite;

  loadBmps();
}

void ScrollBar::setBackgroundBitmaps(const wchar_t *left, const wchar_t *middle, const wchar_t *right) {
  bmpbackgroundleft = left;
  bmpbackgroundmiddle = middle;
  bmpbackgroundright = right;
}

void ScrollBar::loadBmps() {

  if (bmpleft.getBitmap()) leftrgn = new RegionI(bmpleft);
  if (bmpbutton.getBitmap()) buttonrgn = new RegionI(bmpbutton);
  if (bmpright.getBitmap()) rightrgn = new RegionI(bmpright);

  calcOverlapping();
  calcXPosition();
}

void ScrollBar::setPosition(int pos) {
  setPrivatePosition(pos, FALSE);
}

void ScrollBar::setPrivatePosition(int pos, bool signal, bool smooth) {
  if (insetpos) return; // helps stupid people (like me)
  insetpos = 1;
  position = MIN(SCROLLBAR_FULL, pos);
  position = MAX(0, position);
  calcXPosition();
  if (signal) onSetPosition(smooth);
  if (isInited() && isVisible())
  invalidate();
  insetpos = 0;
}

int ScrollBar::getPosition() {
  return position;
}

int ScrollBar::onSetPosition(bool smooth) {
  notifyParent(ChildNotify::SCROLLBAR_SETPOSITION, smooth);
  return 1;
}

int ScrollBar::onSetFinalPosition() {
  notifyParent(ChildNotify::SCROLLBAR_SETFINALPOSITION);
  return 1;
}

void ScrollBar::calcOverlapping() {

  if (!vertical) {

    shiftleft = bmpleft.getWidth();
    if (leftrgn && buttonrgn) {
      int i;
      for (i=shiftleft;i>=0;i--) {
        api_region *reg = buttonrgn->clone();
        reg->offset(i, 0);
        if (leftrgn->doesIntersectRgn(reg)) {
          i++;
          buttonrgn->disposeClone(reg);
          break;
        }
        buttonrgn->disposeClone(reg);
      }
      if (i >= 0)
        shiftleft = i;
    }

    shiftright = bmpright.getWidth();
    if (rightrgn && buttonrgn) {
      int i;
      for (i=0;i>=-shiftright;i--) {
        api_region *reg = rightrgn->clone();
        reg->offset(i+bmpbutton.getWidth(), 0);
        if (reg->doesIntersectRgn(buttonrgn)) {
          i++;
          rightrgn->disposeClone(reg);
          break;
        }
        rightrgn->disposeClone(reg);
      }
      if (i >= -shiftright)
        shiftright += i;
    }

  } else {

    shiftleft = bmpleft.getHeight();
    if (leftrgn && buttonrgn) {
      int i;
      for (i=shiftleft;i>=0;i--) {
        api_region *reg = buttonrgn->clone();
        reg->offset(0, i);
        if (leftrgn->doesIntersectRgn(reg)) {
          i++;
          buttonrgn->disposeClone(reg);
          break;
        }
        buttonrgn->disposeClone(reg);
      }
      if (i >= 0)
        shiftleft = i;
    }

    shiftright = bmpright.getHeight();
    if (rightrgn && buttonrgn) {
      int i;
      for (i=0;i>=-shiftright;i--) {
        api_region *reg = rightrgn->clone();
        reg->offset(0, i+bmpbutton.getHeight());
        if (reg->doesIntersectRgn(buttonrgn)) {
          i++;
          rightrgn->disposeClone(reg);
          break;
        }
        rightrgn->disposeClone(reg);
      }
      if (i >= -shiftright)
        shiftright += i;
    }

  }

}

void ScrollBar::calcXPosition() {

  if (!isInited()) return;

  RECT r;
  getClientRect(&r);

  int maxwidth;

  if (!vertical)
    maxwidth = (r.right-r.left)-(bmpbutton.getWidth()+shiftleft+shiftright)+1;
  else
    maxwidth = (r.bottom-r.top)-(bmpbutton.getHeight()+shiftleft+shiftright)+1;
  int oldx = buttonx;
  buttonx = (int)(((float)getPosition() / SCROLLBAR_FULL) * maxwidth);
  if (buttonx != oldx)
    invalidate();
}

void ScrollBar::calcPosition() {

  if (!isInited()) return;

  RECT r;
  getClientRect(&r);

  int maxwidth;

  if (!vertical)
    maxwidth = r.right-r.left-(bmpbutton.getWidth()+shiftleft+shiftright)+1;
  else
    maxwidth = r.bottom-r.top-(bmpbutton.getHeight()+shiftleft+shiftright)+1;
  setPrivatePosition((int)((float)buttonx / maxwidth * SCROLLBAR_FULL));
  //invalidate();
}

void ScrollBar::timerCallback(int id) {
  switch (id) {
    case TIMER_ID:
      if (firstdelay) {
        killTimer(TIMER_ID);
        setTimer(TIMER_ID, NEXT_DELAY);
        timer = 1;
        firstdelay = 0;
      }
      checkPageUpDown();
      break;
    case TIMER_ID2:
      if (firstdelay) {
        killTimer(TIMER_ID2);
        setTimer(TIMER_ID2, NEXT_DELAY);
        timer2 = 1;
        firstdelay = 0;
      }
      checkUpDown();
      break;
    default:
      SCROLLBAR_PARENT::timerCallback(id);
  }
}

// FG> smooth scrolling forced on, sorry, microsoft does it too so the user perceives IE scrolling as faster than it actually is
// eventho they tell you "The smooth-scrolling effect for list boxes should be disabled when this setting is FALSE. Your application must do this if it creates customized list boxes", they
// break their own rule so people don't bitch too much. ergo there is no reason we should not do that too.

int ScrollBar::pageUp() { 

  pageway = PAGE_UP;

  setPrivatePosition((int)MAX(0.f, (float)getPosition() + (float)SCROLLBAR_FULL / (npages-1)), TRUE, 1/*Std::osparam_getSmoothScroll()*/);

  return 1; 
};

int ScrollBar::pageDown() { 

  pageway = PAGE_DOWN;

  setPrivatePosition((int)MIN((float)SCROLLBAR_FULL, (float)getPosition() - (float)SCROLLBAR_FULL / (npages-1)), TRUE, 1/*Std::osparam_getSmoothScroll()*/);

  return 1; 
};

void ScrollBar::setNPages(int n) {
  //ASSERT(n >= 2);
  if (n < 2) n = 2;
  npages = n;
}

void ScrollBar::gotoPage(int page) {

  page = MIN(page, npages-1);
  page = MAX(page, 0);

  setPrivatePosition((int)((float)SCROLLBAR_FULL / (npages-1) * page), TRUE, FALSE);

}

void ScrollBar::setUpDownValue(int newupdown) {
  updown = newupdown;
}

int ScrollBar::upDown(int which) {
  switch (which) {
    case POS_LEFT:
      setPrivatePosition(getPosition()-updown);
      break;
    case POS_RIGHT:
      setPrivatePosition(getPosition()+updown);
      break;
  }
  return 1;
}

void ScrollBar::setVertical(bool isvertical) {
  vertical = isvertical;
  calcOverlapping();
  if (isInited())
    invalidate();
}