Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 1 | /* 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 | // TXViewport.cxx |
| 20 | // |
| 21 | |
| 22 | #include "TXViewport.h" |
| 23 | #include <stdio.h> |
| 24 | |
| 25 | TXViewport::TXViewport(Display* dpy_, int w, int h, TXWindow* parent_) |
| 26 | : TXWindow(dpy_, w, h, parent_), child(0), hScrollbar(0), |
| 27 | vScrollbar(0), scrollbarSize(15), xOff(0), yOff(0), bumpScrollTimer(this), |
Adam Tkac | b33e79e | 2008-03-14 13:37:09 +0000 | [diff] [blame] | 28 | bumpScroll(false), needXScrollbar(false), needYScrollbar(false), |
| 29 | bumpScrollX(0), bumpScrollY(0) |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 30 | { |
| 31 | clipper = new TXWindow(dpy, width()-scrollbarSize, height()-scrollbarSize, |
| 32 | this); |
| 33 | clipper->setBg(black); |
| 34 | hScrollbar = new TXScrollbar(dpy, width()-scrollbarSize, scrollbarSize, |
| 35 | false, this, this); |
| 36 | vScrollbar = new TXScrollbar(dpy, scrollbarSize, height()-scrollbarSize, |
| 37 | true, this, this); |
| 38 | } |
| 39 | |
| 40 | TXViewport::~TXViewport() |
| 41 | { |
| 42 | delete clipper; |
| 43 | delete hScrollbar; |
| 44 | delete vScrollbar; |
| 45 | } |
| 46 | |
| 47 | void TXViewport::setChild(TXWindow* child_) |
| 48 | { |
| 49 | child = child_; |
| 50 | XReparentWindow(dpy, child->win(), clipper->win(), 0, 0); |
| 51 | xOff = yOff = 0; |
| 52 | child->map(); |
| 53 | resizeNotify(); |
| 54 | } |
| 55 | |
| 56 | bool TXViewport::setOffset(int x, int y) |
| 57 | { |
| 58 | if (clipper->width() >= child->width()) { |
| 59 | x = (clipper->width() - child->width()) / 2; |
| 60 | } else { |
| 61 | if (x > 0) x = 0; |
| 62 | if (x + child->width() < clipper->width()) |
| 63 | x = clipper->width() - child->width(); |
| 64 | } |
| 65 | |
| 66 | if (clipper->height() >= child->height()) { |
| 67 | y = (clipper->height() - child->height()) / 2; |
| 68 | } else { |
| 69 | if (y > 0) y = 0; |
| 70 | if (y + child->height() < clipper->height()) |
| 71 | y = clipper->height() - child->height(); |
| 72 | } |
| 73 | |
| 74 | if (x != xOff || y != yOff) { |
| 75 | xOff = x; |
| 76 | yOff = y; |
| 77 | child->move(xOff, yOff); |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | void TXViewport::setBumpScroll(bool b) |
| 85 | { |
| 86 | bumpScroll = b; |
| 87 | resizeNotify(); |
| 88 | } |
| 89 | |
| 90 | // Note: bumpScrollEvent() only works if the viewport is positioned at 0,0 and |
| 91 | // is the same width and height as the screen. |
| 92 | bool TXViewport::bumpScrollEvent(XMotionEvent* ev) |
| 93 | { |
| 94 | if (!bumpScroll) return false; |
| 95 | int bumpScrollPixels = 20; |
| 96 | bumpScrollX = bumpScrollY = 0; |
| 97 | |
| 98 | if (ev->x_root == width()-1) bumpScrollX = -bumpScrollPixels; |
| 99 | else if (ev->x_root == 0) bumpScrollX = bumpScrollPixels; |
| 100 | if (ev->y_root == height()-1) bumpScrollY = -bumpScrollPixels; |
| 101 | else if (ev->y_root == 0) bumpScrollY = bumpScrollPixels; |
| 102 | |
| 103 | if (bumpScrollX || bumpScrollY) { |
| 104 | if (bumpScrollTimer.isStarted()) return true; |
| 105 | if (setOffset(xOff + bumpScrollX, yOff + bumpScrollY)) { |
| 106 | bumpScrollTimer.start(25); |
| 107 | return true; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | bumpScrollTimer.stop(); |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | bool TXViewport::handleTimeout(rfb::Timer* timer) { |
| 116 | return setOffset(xOff + bumpScrollX, yOff + bumpScrollY); |
| 117 | } |
| 118 | |
| 119 | void TXViewport::resizeNotify() |
| 120 | { |
Adam Tkac | f818725 | 2011-01-17 10:34:55 +0000 | [diff] [blame] | 121 | int winMaxWidth, winMaxHeight; |
| 122 | |
| 123 | winMaxWidth = child->width(); |
| 124 | winMaxHeight = child->height(); |
| 125 | |
Adam Tkac | bb44366 | 2011-01-17 10:35:46 +0000 | [diff] [blame] | 126 | needXScrollbar = false; |
| 127 | needYScrollbar = false; |
| 128 | if (!bumpScroll && height() > scrollbarSize && width() > scrollbarSize) { |
| 129 | needXScrollbar = (width() < child->width()); |
| 130 | needYScrollbar = (height() < child->height()); |
| 131 | // Adding an horizontal scrollbar occupies space, which might cause the |
| 132 | // need to add a vertical scrollbar, and vice-versa. These additional |
| 133 | // checks should solve this problem |
| 134 | if (needXScrollbar && (height() - scrollbarSize < child->height())) |
| 135 | needYScrollbar = true; |
| 136 | if (needYScrollbar && (width() - scrollbarSize < child->width())) |
| 137 | needXScrollbar = true; |
| 138 | } |
Adam Tkac | b33e79e | 2008-03-14 13:37:09 +0000 | [diff] [blame] | 139 | |
Adam Tkac | f818725 | 2011-01-17 10:34:55 +0000 | [diff] [blame] | 140 | if (needXScrollbar) |
| 141 | winMaxHeight += scrollbarSize; |
| 142 | if (needYScrollbar) |
| 143 | winMaxWidth += scrollbarSize; |
| 144 | setMaxSize(winMaxWidth, winMaxHeight); |
| 145 | |
Adam Tkac | b33e79e | 2008-03-14 13:37:09 +0000 | [diff] [blame] | 146 | if (needXScrollbar && needYScrollbar) { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 147 | clipper->resize(width()-scrollbarSize, height()-scrollbarSize); |
| 148 | hScrollbar->map(); |
| 149 | vScrollbar->map(); |
Adam Tkac | b33e79e | 2008-03-14 13:37:09 +0000 | [diff] [blame] | 150 | } else if (needXScrollbar) { |
| 151 | clipper->resize(width(), height()-scrollbarSize); |
| 152 | hScrollbar->map(); |
| 153 | vScrollbar->unmap(); |
| 154 | } else if (needYScrollbar) { |
| 155 | clipper->resize(width()-scrollbarSize, height()); |
| 156 | hScrollbar->unmap(); |
| 157 | vScrollbar->map(); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 158 | } else { |
| 159 | clipper->resize(width(), height()); |
| 160 | hScrollbar->unmap(); |
| 161 | vScrollbar->unmap(); |
| 162 | } |
| 163 | |
| 164 | setOffset(xOff, yOff); |
| 165 | |
Adam Tkac | b33e79e | 2008-03-14 13:37:09 +0000 | [diff] [blame] | 166 | if (needXScrollbar) { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 167 | hScrollbar->move(0, height()-scrollbarSize); |
| 168 | hScrollbar->resize(width()-scrollbarSize, scrollbarSize); |
| 169 | hScrollbar->set(child->width(), -xOff, width()-scrollbarSize); |
Adam Tkac | b33e79e | 2008-03-14 13:37:09 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | if (needYScrollbar) { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 173 | vScrollbar->move(width()-scrollbarSize, 0); |
| 174 | vScrollbar->resize(scrollbarSize, height()-scrollbarSize); |
| 175 | vScrollbar->set(child->height(), -yOff, height()-scrollbarSize); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void TXViewport::scrollbarPos(int x, int y, TXScrollbar* sb) |
| 180 | { |
| 181 | if (sb == hScrollbar) { |
| 182 | x = -x; |
| 183 | y = yOff; |
| 184 | } else { |
| 185 | x = xOff; |
| 186 | y = -y; |
| 187 | } |
| 188 | setOffset(x, y); |
| 189 | } |