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
|
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
//
// TXScrollbar.cxx
//
#include "TXScrollbar.h"
#include <stdio.h>
#include <assert.h>
TXScrollbar::TXScrollbar(Display* dpy_, int width, int height, bool vert,
TXScrollbarCallback* cb_, TXWindow* parent_)
: TXWindow(dpy_, width, height, parent_), cb(cb_), vertical(vert),
clickedInThumb(false)
{
setEventHandler(this);
gc = XCreateGC(dpy, win(), 0, 0);
addEventMask(ExposureMask | ButtonPressMask | ButtonReleaseMask |
ButtonMotionMask);
setBg(scrollbarBg);
limit[0] = len[0] = limit[1] = len[1] = 1;
start[0] = start[1] = 0;
}
TXScrollbar::~TXScrollbar()
{
XFreeGC(dpy, gc);
}
void TXScrollbar::set(int limit_, int start_, int len_, bool vert)
{
assert(limit_ > 0 && len_ >= 0 && len_ <= limit_);
if (start_ < 0) start_ = 0;
if (start_ > limit_ - len_) start_ = limit_ - len_;
if (limit[vert] != limit_ || start[vert] != start_ || len[vert] != len_) {
limit[vert] = limit_;
start[vert] = start_;
len[vert] = len_;
paint();
}
}
void TXScrollbar::paint()
{
int x = scaleToBarX(start[0]);
int y = scaleToBarY(start[1]);
int w = scaleToBarX(len[0]);
int h = scaleToBarY(len[1]);
if (y > 0) XClearArea(dpy, win(), 0, 0, 0, y, false);
if (x > 0) XClearArea(dpy, win(), 0, y, x, y+h, false);
XClearArea(dpy, win(), x+w, y, 0, y+h, false);
XClearArea(dpy, win(), 0, y+h, 0, 0, false);
drawBevel(gc, x, y, w, h, bevel, defaultBg, lightBg, darkBg);
}
void TXScrollbar::handleEvent(TXWindow* w, XEvent* ev)
{
switch (ev->type) {
case Expose:
paint();
break;
case ButtonPress:
{
xDown = ev->xbutton.x;
yDown = ev->xbutton.y;
xStart = start[0];
yStart = start[1];
bool clickedInThumbX = false;
if (xDown < scaleToBarX(start[0])) {
set(limit[0], start[0] - len[0], len[0], false);
} else if (xDown >= scaleToBarX(start[0]+len[0])) {
set(limit[0], start[0] + len[0], len[0], false);
} else {
clickedInThumbX = true;
}
bool clickedInThumbY = false;
if (yDown < scaleToBarY(start[1])) {
set(limit[1], start[1] - len[1], len[1], true);
} else if (yDown >= scaleToBarY(start[1]+len[1])) {
set(limit[1], start[1] + len[1], len[1], true);
} else {
clickedInThumbY = true;
}
clickedInThumb = clickedInThumbX && clickedInThumbY;
if (cb) cb->scrollbarPos(start[0], start[1], this);
}
break;
case ButtonRelease:
case MotionNotify:
while (XCheckTypedWindowEvent(dpy, win(), MotionNotify, ev));
if (clickedInThumb) {
int dx = ev->xmotion.x - xDown;
int dy = ev->xmotion.y - yDown;
set(limit[0], xStart + barToScaleX(dx), len[0], false);
set(limit[1], yStart + barToScaleY(dy), len[1], true);
if (cb) cb->scrollbarPos(start[0], start[1], this);
}
break;
}
}
|