blob: 47e124c37519784848e255e8e1c81eb332fc8a6a [file] [log] [blame]
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18//
19// TXScrollbar.cxx
20//
21
22#include "TXScrollbar.h"
23#include <stdio.h>
24#include <assert.h>
25
26TXScrollbar::TXScrollbar(Display* dpy_, int width, int height, bool vert,
27 TXScrollbarCallback* cb_, TXWindow* parent_)
28 : TXWindow(dpy_, width, height, parent_), cb(cb_), vertical(vert),
29 clickedInThumb(false)
30{
31 setEventHandler(this);
32 gc = XCreateGC(dpy, win(), 0, 0);
33 addEventMask(ExposureMask | ButtonPressMask | ButtonReleaseMask |
34 ButtonMotionMask);
35 setBg(scrollbarBg);
36 limit[0] = len[0] = limit[1] = len[1] = 1;
37 start[0] = start[1] = 0;
38}
39
40TXScrollbar::~TXScrollbar()
41{
42 XFreeGC(dpy, gc);
43}
44
45void TXScrollbar::set(int limit_, int start_, int len_, bool vert)
46{
47 assert(limit_ > 0 && len_ >= 0 && len_ <= limit_);
48
49 if (start_ < 0) start_ = 0;
50 if (start_ > limit_ - len_) start_ = limit_ - len_;
51
52 if (limit[vert] != limit_ || start[vert] != start_ || len[vert] != len_) {
53 limit[vert] = limit_;
54 start[vert] = start_;
55 len[vert] = len_;
56 paint();
57 }
58}
59
60void TXScrollbar::paint()
61{
62 int x = scaleToBarX(start[0]);
63 int y = scaleToBarY(start[1]);
64 int w = scaleToBarX(len[0]);
65 int h = scaleToBarY(len[1]);
66 if (y > 0) XClearArea(dpy, win(), 0, 0, 0, y, false);
67 if (x > 0) XClearArea(dpy, win(), 0, y, x, y+h, false);
68 XClearArea(dpy, win(), x+w, y, 0, y+h, false);
69 XClearArea(dpy, win(), 0, y+h, 0, 0, false);
70 drawBevel(gc, x, y, w, h, bevel, defaultBg, lightBg, darkBg);
71}
72
73void TXScrollbar::handleEvent(TXWindow* w, XEvent* ev)
74{
75 switch (ev->type) {
76 case Expose:
77 paint();
78 break;
79
80 case ButtonPress:
81 {
82 xDown = ev->xbutton.x;
83 yDown = ev->xbutton.y;
84 xStart = start[0];
85 yStart = start[1];
86 bool clickedInThumbX = false;
87 if (xDown < scaleToBarX(start[0])) {
88 set(limit[0], start[0] - len[0], len[0], false);
89 } else if (xDown >= scaleToBarX(start[0]+len[0])) {
90 set(limit[0], start[0] + len[0], len[0], false);
91 } else {
92 clickedInThumbX = true;
93 }
94 bool clickedInThumbY = false;
95 if (yDown < scaleToBarY(start[1])) {
96 set(limit[1], start[1] - len[1], len[1], true);
97 } else if (yDown >= scaleToBarY(start[1]+len[1])) {
98 set(limit[1], start[1] + len[1], len[1], true);
99 } else {
100 clickedInThumbY = true;
101 }
102 clickedInThumb = clickedInThumbX && clickedInThumbY;
103 if (cb) cb->scrollbarPos(start[0], start[1], this);
104 }
105 break;
106
107 case ButtonRelease:
108 case MotionNotify:
109 while (XCheckTypedWindowEvent(dpy, win(), MotionNotify, ev));
110 if (clickedInThumb) {
111 int dx = ev->xmotion.x - xDown;
112 int dy = ev->xmotion.y - yDown;
113 set(limit[0], xStart + barToScaleX(dx), len[0], false);
114 set(limit[1], yStart + barToScaleY(dy), len[1], true);
115 if (cb) cb->scrollbarPos(start[0], start[1], this);
116 }
117 break;
118 }
119}