summaryrefslogtreecommitdiffstats
path: root/unix/tx/TXViewport.cxx
blob: 60648933f1db24d5170439b43570342ed8cc65b6 (plain)
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
/* 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.
 */
//
// TXViewport.cxx
//

#include "TXViewport.h"
#include <stdio.h>

TXViewport::TXViewport(Display* dpy_, int w, int h, TXWindow* parent_)
  : TXWindow(dpy_, w, h, parent_), child(0), hScrollbar(0),
    vScrollbar(0), scrollbarSize(15), xOff(0), yOff(0), bumpScrollTimer(this),
    bumpScroll(false), needXScrollbar(false), needYScrollbar(false),
    bumpScrollX(0), bumpScrollY(0)
{
  clipper = new TXWindow(dpy, width()-scrollbarSize, height()-scrollbarSize,
                         this);
  clipper->setBg(black);
  hScrollbar = new TXScrollbar(dpy, width()-scrollbarSize, scrollbarSize,
                               false, this, this);
  vScrollbar = new TXScrollbar(dpy, scrollbarSize, height()-scrollbarSize,
                               true, this, this);
}

TXViewport::~TXViewport()
{
  delete clipper;
  delete hScrollbar;
  delete vScrollbar;
}

void TXViewport::setChild(TXWindow* child_)
{
  child = child_;
  XReparentWindow(dpy, child->win(), clipper->win(), 0, 0);
  xOff = yOff = 0;
  child->map();
  resizeNotify();
}

bool TXViewport::setOffset(int x, int y)
{
  if (clipper->width() >= child->width()) {
    x = (clipper->width() - child->width()) / 2;
  } else {
    if (x > 0) x = 0;
    if (x + child->width() < clipper->width())
      x = clipper->width() - child->width();
  }

  if (clipper->height() >= child->height()) {
    y = (clipper->height() - child->height()) / 2;
  } else {
    if (y > 0) y = 0;
    if (y + child->height() < clipper->height())
      y = clipper->height() - child->height();
  }

  if (x != xOff || y != yOff) {
    xOff = x;
    yOff = y;
    child->move(xOff, yOff);
    return true;
  }

  return false;
}

void TXViewport::setBumpScroll(bool b)
{
  bumpScroll = b;
  resizeNotify();
}

// Note: bumpScrollEvent() only works if the viewport is positioned at 0,0 and
// is the same width and height as the screen.
bool TXViewport::bumpScrollEvent(XMotionEvent* ev)
{
  if (!bumpScroll) return false;
  int bumpScrollPixels = 20;
  bumpScrollX = bumpScrollY = 0;

  if (ev->x_root == width()-1)  bumpScrollX = -bumpScrollPixels;
  else if (ev->x_root == 0)     bumpScrollX = bumpScrollPixels;
  if (ev->y_root == height()-1) bumpScrollY = -bumpScrollPixels;
  else if (ev->y_root == 0)     bumpScrollY = bumpScrollPixels;

  if (bumpScrollX || bumpScrollY) {
    if (bumpScrollTimer.isStarted()) return true;
    if (setOffset(xOff + bumpScrollX, yOff + bumpScrollY)) {
      bumpScrollTimer.start(25);
      return true;
    }
  }

  bumpScrollTimer.stop();
  return false;
}

bool TXViewport::handleTimeout(rfb::Timer* timer) {
  return setOffset(xOff + bumpScrollX, yOff + bumpScrollY);
}

void TXViewport::resizeNotify()
{
  int winMaxWidth, winMaxHeight;

  winMaxWidth = child->width();
  winMaxHeight = child->height();

  needXScrollbar = false;
  needYScrollbar = false;
  if (!bumpScroll && height() > scrollbarSize && width() > scrollbarSize) {
    needXScrollbar = (width() < child->width());
    needYScrollbar = (height() < child->height());
    // Adding an horizontal scrollbar occupies space, which might cause the
    // need to add a vertical scrollbar, and vice-versa. These additional
    // checks should solve this problem
    if (needXScrollbar && (height() - scrollbarSize < child->height()))
	needYScrollbar = true;
    if (needYScrollbar && (width() - scrollbarSize < child->width()))
	needXScrollbar = true;
  }

  if (needXScrollbar)
    winMaxHeight += scrollbarSize;
  if (needYScrollbar)
    winMaxWidth += scrollbarSize;
  setMaxSize(winMaxWidth, winMaxHeight);

  if (needXScrollbar && needYScrollbar) {
    clipper->resize(width()-scrollbarSize, height()-scrollbarSize);
    hScrollbar->map();
    vScrollbar->map();
  } else if (needXScrollbar) {
    clipper->resize(width(), height()-scrollbarSize);
    hScrollbar->map();
    vScrollbar->unmap();
  } else if (needYScrollbar) {
    clipper->resize(width()-scrollbarSize, height());
    hScrollbar->unmap();
    vScrollbar->map();
  } else {
    clipper->resize(width(), height());
    hScrollbar->unmap();
    vScrollbar->unmap();
  }

  setOffset(xOff, yOff);

  if (needXScrollbar) {
    hScrollbar->move(0, height()-scrollbarSize);
    hScrollbar->resize(width()-scrollbarSize, scrollbarSize);
    hScrollbar->set(child->width(), -xOff, width()-scrollbarSize);
  }

  if (needYScrollbar) {
    vScrollbar->move(width()-scrollbarSize, 0);
    vScrollbar->resize(scrollbarSize, height()-scrollbarSize);
    vScrollbar->set(child->height(), -yOff, height()-scrollbarSize);
  }
}

void TXViewport::scrollbarPos(int x, int y, TXScrollbar* sb)
{
  if (sb == hScrollbar) {
    x = -x;
    y = yOff;
  } else {
    x = xOff;
    y = -y;
  }
  setOffset(x, y);
}