Initial commit

This commit is contained in:
matt
2020-09-20 12:16:44 +00:00
parent 09a4460ddb
commit 408c005d3e
839 changed files with 190481 additions and 0 deletions

9
unix/tx/CMakeLists.txt Normal file
View File

@@ -0,0 +1,9 @@
include_directories(${X11_INCLUDE_DIR})
include_directories(${CMAKE_SOURCE_DIR}/common)
include_directories(${CMAKE_SOURCE_DIR}/common/rfb)
add_library(tx STATIC
TXWindow.cxx)
target_link_libraries(tx ${X11_LIBRARIES})

124
unix/tx/TXButton.h Normal file
View File

@@ -0,0 +1,124 @@
/* 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.
*/
//
// TXButton.h
//
// A TXButton is a clickable button with some text in it. The button must be
// big enough to contain the text - if not then it will be resized
// appropriately.
//
#ifndef __TXBUTTON_H__
#define __TXBUTTON_H__
#include "TXWindow.h"
#include <rfb/util.h>
// TXButtonCallback's buttonActivate() method is called when a button is
// activated.
class TXButton;
class TXButtonCallback {
public:
virtual void buttonActivate(TXButton* button)=0;
};
class TXButton : public TXWindow, public TXEventHandler {
public:
TXButton(Display* dpy_, const char* text_, TXButtonCallback* cb_=0,
TXWindow* parent_=0, int w=1, int h=1)
: TXWindow(dpy_, w, h, parent_), cb(cb_), down(false),
disabled_(false)
{
setEventHandler(this);
setText(text_);
gc = XCreateGC(dpy, win(), 0, 0);
XSetFont(dpy, gc, defaultFont);
addEventMask(ExposureMask | ButtonPressMask | ButtonReleaseMask);
}
virtual ~TXButton() {
XFreeGC(dpy, gc);
}
// setText() changes the text in the button.
void setText(const char* text_) {
text.buf = rfb::strDup(text_);
int textWidth = XTextWidth(defaultFS, text.buf, strlen(text.buf));
int textHeight = (defaultFS->ascent + defaultFS->descent);
int newWidth = __rfbmax(width(), textWidth + xPad*2 + bevel*2);
int newHeight = __rfbmax(height(), textHeight + yPad*2 + bevel*2);
if (width() < newWidth || height() < newHeight) {
resize(newWidth, newHeight);
}
}
// disabled() sets or queries the disabled state of the checkbox. A disabled
// checkbox cannot be changed via the user interface.
void disabled(bool b) { disabled_ = b; paint(); }
bool disabled() { return disabled_; }
private:
void paint() {
int tw = XTextWidth(defaultFS, text.buf, strlen(text.buf));
int startx = (width() - tw) / 2;
int starty = (height() + defaultFS->ascent - defaultFS->descent) / 2;
if (down || disabled_) {
drawBevel(gc, 0, 0, width(), height(), bevel, defaultBg, darkBg,lightBg);
startx++; starty++;
} else {
drawBevel(gc, 0, 0, width(), height(), bevel, defaultBg, lightBg,darkBg);
}
XSetForeground(dpy, gc, disabled_ ? disabledFg : defaultFg);
XDrawString(dpy, win(), gc, startx, starty, text.buf, strlen(text.buf));
}
virtual void handleEvent(TXWindow* w, XEvent* ev) {
switch (ev->type) {
case Expose:
paint();
break;
case ButtonPress:
if (!disabled_) {
down = true;
paint();
}
break;
case ButtonRelease:
if (!down) break;
down = false;
paint();
if (ev->xbutton.x >= 0 && ev->xbutton.x < width() &&
ev->xbutton.y >= 0 && ev->xbutton.y < height()) {
if (cb) cb->buttonActivate(this);
}
break;
}
}
GC gc;
rfb::CharArray text;
TXButtonCallback* cb;
bool down;
bool disabled_;
};
#endif

142
unix/tx/TXCheckbox.h Normal file
View File

@@ -0,0 +1,142 @@
/* 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.
*/
//
// TXCheckbox.h
//
// A TXCheckbox has a box which may be "checked" with some text next to it.
// The checkbox window must be big enough to contain the text - if not then it
// will be resized appropriately.
//
// There are two styles of checkbox: the normal style which uses a tick in a
// square box, and the radio style which uses a dot inside a circle. The
// default behaviour when clicking on the checkbox is to toggle it on or off,
// but this behaviour can be changed by the callback object. In particular to
// get radiobutton behaviour, the callback must ensure that only one of a set
// of radiobuttons is selected.
//
#ifndef __TXCHECKBOX_H__
#define __TXCHECKBOX_H__
#include "TXWindow.h"
#include <rfb/util.h>
// TXCheckboxCallback's checkboxSelect() method is called when the state of a
// checkbox changes.
class TXCheckbox;
class TXCheckboxCallback {
public:
virtual void checkboxSelect(TXCheckbox* checkbox)=0;
};
class TXCheckbox : public TXWindow, public TXEventHandler {
public:
TXCheckbox(Display* dpy_, const char* text_, TXCheckboxCallback* cb_,
bool radio_=false, TXWindow* parent_=0, int w=1, int h=1)
: TXWindow(dpy_, w, h, parent_), cb(cb_), text(0),
boxSize(radio_ ? 12 : 13), boxPad(4),
checked_(false), disabled_(false), radio(radio_)
{
setEventHandler(this);
setText(text_);
gc = XCreateGC(dpy, win(), 0, 0);
XSetFont(dpy, gc, defaultFont);
addEventMask(ExposureMask| ButtonPressMask | ButtonReleaseMask);
}
virtual ~TXCheckbox() {
XFreeGC(dpy, gc);
if (text) free(text);
}
// setText() changes the text in the checkbox.
void setText(const char* text_) {
if (text) free(text);
text = strdup((char*)text_);
int textWidth = XTextWidth(defaultFS, text, strlen(text));
int textHeight = (defaultFS->ascent + defaultFS->descent);
int newWidth = __rfbmax(width(), textWidth + xPad*2 + boxPad*2 + boxSize);
int newHeight = __rfbmax(height(), textHeight + yPad*2);
if (width() < newWidth || height() < newHeight) {
resize(newWidth, newHeight);
}
}
// checked() sets or queries the state of the checkbox
void checked(bool b) { checked_ = b; paint(); }
bool checked() { return checked_; }
// disabled() sets or queries the disabled state of the checkbox. A disabled
// checkbox cannot be changed via the user interface.
void disabled(bool b) { disabled_ = b; paint(); }
bool disabled() { return disabled_; }
private:
void paint() {
if (disabled_)
drawBevel(gc, xPad + boxPad, (height() - boxSize) / 2, boxSize, boxSize,
bevel, disabledBg, darkBg, lightBg, radio);
else
drawBevel(gc, xPad + boxPad, (height() - boxSize) / 2, boxSize, boxSize,
bevel, enabledBg, darkBg, lightBg, radio);
XSetBackground(dpy, gc, disabled_ ? disabledBg : enabledBg);
XSetForeground(dpy, gc, disabled_ ? disabledFg : defaultFg);
if (checked_) {
Pixmap icon = radio ? dot : tick;
int iconSize = radio ? dotSize : tickSize;
XCopyPlane(dpy, icon, win(), gc, 0, 0, iconSize, iconSize,
xPad + boxPad + (boxSize - iconSize) / 2,
(height() - iconSize) / 2, 1);
}
XDrawString(dpy, win(), gc, xPad + boxSize + boxPad*2,
(height() + defaultFS->ascent - defaultFS->descent) / 2,
text, strlen(text));
}
virtual void handleEvent(TXWindow* w, XEvent* ev) {
switch (ev->type) {
case Expose:
paint();
break;
case ButtonPress:
break;
case ButtonRelease:
if (ev->xbutton.x >= 0 && ev->xbutton.x < width() &&
ev->xbutton.y >= 0 && ev->xbutton.y < height()) {
if (!disabled_) {
checked_ = !checked_;
if (cb) cb->checkboxSelect(this);
paint();
}
}
break;
}
}
TXCheckboxCallback* cb;
GC gc;
char* text;
int boxSize;
int boxPad;
bool checked_;
bool disabled_;
bool radio;
};
#endif

96
unix/tx/TXDialog.h Normal file
View File

@@ -0,0 +1,96 @@
/* 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.
*/
//
// TXDialog.h
//
// A TXDialog is a pop-up dialog window. The dialog can be made visible by
// calling its show() method. Dialogs can be modal or non-modal. For a modal
// dialog box, the show() method only returns when the dialog box has been
// dismissed. For a non-modal dialog box, the show() method returns
// immediately.
//
#ifndef __TXDIALOG_H__
#define __TXDIALOG_H__
#include "TXWindow.h"
#include <errno.h>
class TXDialog : public TXWindow, public TXDeleteWindowCallback {
public:
TXDialog(Display* dpy, int width, int height, const char* name,
bool modal_=false)
: TXWindow(dpy, width, height), done(false), ok(false), modal(modal_)
{
toplevel(name, this);
resize(width, height);
}
virtual ~TXDialog() {}
// show() makes the dialog visible. For a modal dialog box, this processes X
// events until the done flag has been set, after which it returns the value
// of the ok flag. For a non-modal dialog box it always returns true
// immediately.
bool show() {
ok = false;
done = false;
initDialog();
raise();
map();
if (modal) {
while (true) {
TXWindow::handleXEvents(dpy);
if (done) {
return ok;
}
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(ConnectionNumber(dpy), &rfds);
int n = select(FD_SETSIZE, &rfds, 0, 0, 0);
if (n < 0) throw rdr::SystemException("select",errno);
}
}
return true;
}
// initDialog() can be overridden in a derived class. Typically it is used
// to make sure that checkboxes have the right state, etc.
virtual void initDialog() {}
// resize() is overridden here to re-center the dialog
void resize(int w, int h) {
TXWindow::resize(w,h);
int dpyWidth = WidthOfScreen(DefaultScreenOfDisplay(dpy));
int dpyHeight = HeightOfScreen(DefaultScreenOfDisplay(dpy));
setUSPosition((dpyWidth - width() - 10) / 2, (dpyHeight - height() - 30) / 2);
}
protected:
virtual void deleteWindow(TXWindow* w) {
ok = false;
done = true;
unmap();
}
bool done;
bool ok;
bool modal;
};
#endif

126
unix/tx/TXLabel.h Normal file
View File

@@ -0,0 +1,126 @@
/* 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.
*/
//
// TXLabel.h
//
// An TXLabel allows you to put up multiline text in a window with various
// alignments. The label must be big enough to contain the text - if not then
// it will be resized appropriately.
//
#ifndef __TXLABEL_H__
#define __TXLABEL_H__
#include <stdlib.h>
#include "TXWindow.h"
#include <rfb/util.h>
class TXLabel : public TXWindow, public TXEventHandler {
public:
enum HAlign { left, centre, right };
enum VAlign { top, middle, bottom };
TXLabel(Display* dpy_, const char* text_, TXWindow* parent_=0,
int w=1, int h=1, HAlign ha=centre, VAlign va=middle)
: TXWindow(dpy_, w, h, parent_), lineSpacing(2), lines(0),
halign(ha), valign(va)
{
setEventHandler(this);
setText(text_);
addEventMask(ExposureMask);
}
// setText() changes the text in the label.
void setText(const char* text_) {
text.buf = rfb::strDup(text_);
lines = 0;
int lineStart = 0;
int textWidth = 0;
int i = -1;
do {
i++;
if (text.buf[i] == '\n' || text.buf[i] == 0) {
int tw = XTextWidth(defaultFS, &text.buf[lineStart], i-lineStart);
if (tw > textWidth) textWidth = tw;
lineStart = i+1;
lines++;
}
} while (text.buf[i] != 0);
int textHeight = ((defaultFS->ascent + defaultFS->descent + lineSpacing)
* lines);
int newWidth = __rfbmax(width(), textWidth + xPad*2);
int newHeight = __rfbmax(height(), textHeight + yPad*2);
if (width() < newWidth || height() < newHeight) {
resize(newWidth, newHeight);
}
invalidate();
}
private:
int xOffset(int textWidth) {
switch (halign) {
case left: return xPad;
case right: return width() - xPad - textWidth;
default: return (width() - textWidth) / 2;
}
}
int yOffset(int lineNum) {
int textHeight = ((defaultFS->ascent + defaultFS->descent + lineSpacing)
* lines);
int lineOffset = ((defaultFS->ascent + defaultFS->descent + lineSpacing)
* lineNum + defaultFS->ascent);
switch (valign) {
case top: return yPad + lineOffset;
case bottom: return height() - yPad - textHeight + lineOffset;
default: return (height() - textHeight) / 2 + lineOffset;
}
}
void paint() {
int lineNum = 0;
int lineStart = 0;
int i = -1;
do {
i++;
if (text.buf[i] == '\n' || text.buf[i] == 0) {
int tw = XTextWidth(defaultFS, &text.buf[lineStart], i-lineStart);
XDrawString(dpy, win(), defaultGC, xOffset(tw), yOffset(lineNum),
&text.buf[lineStart], i-lineStart);
lineStart = i+1;
lineNum++;
}
} while (text.buf[i] != 0);
}
virtual void handleEvent(TXWindow* w, XEvent* ev) {
switch (ev->type) {
case Expose:
paint();
break;
}
}
int lineSpacing;
rfb::CharArray text;
int lines;
HAlign halign;
VAlign valign;
};
#endif

513
unix/tx/TXWindow.cxx Normal file
View File

@@ -0,0 +1,513 @@
/* 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.
*/
//
// TXWindow.cxx
//
#include <X11/Xatom.h>
#include "TXWindow.h"
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <rfb/util.h>
std::list<TXWindow*> windows;
Atom wmProtocols, wmDeleteWindow, wmTakeFocus;
Atom xaTIMESTAMP, xaTARGETS, xaSELECTION_TIME, xaSELECTION_STRING;
Atom xaCLIPBOARD;
unsigned long TXWindow::black, TXWindow::white;
unsigned long TXWindow::defaultFg, TXWindow::defaultBg;
unsigned long TXWindow::lightBg, TXWindow::darkBg;
unsigned long TXWindow::disabledFg, TXWindow::disabledBg;
unsigned long TXWindow::enabledBg;
unsigned long TXWindow::scrollbarBg;
Colormap TXWindow::cmap = 0;
GC TXWindow::defaultGC = 0;
Font TXWindow::defaultFont = 0;
XFontStruct* TXWindow::defaultFS = 0;
Time TXWindow::cutBufferTime = 0;
Pixmap TXWindow::dot = 0, TXWindow::tick = 0;
const int TXWindow::dotSize = 4, TXWindow::tickSize = 8;
char* TXWindow::defaultWindowClass;
TXGlobalEventHandler* TXWindow::globalEventHandler = NULL;
void TXWindow::init(Display* dpy, const char* defaultWindowClass_)
{
cmap = DefaultColormap(dpy,DefaultScreen(dpy));
wmProtocols = XInternAtom(dpy, "WM_PROTOCOLS", False);
wmDeleteWindow = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
wmTakeFocus = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
xaTIMESTAMP = XInternAtom(dpy, "TIMESTAMP", False);
xaTARGETS = XInternAtom(dpy, "TARGETS", False);
xaSELECTION_TIME = XInternAtom(dpy, "SELECTION_TIME", False);
xaSELECTION_STRING = XInternAtom(dpy, "SELECTION_STRING", False);
xaCLIPBOARD = XInternAtom(dpy, "CLIPBOARD", False);
XColor cols[6];
cols[0].red = cols[0].green = cols[0].blue = 0x0000;
cols[1].red = cols[1].green = cols[1].blue = 0xbbbb;
cols[2].red = cols[2].green = cols[2].blue = 0xeeee;
cols[3].red = cols[3].green = cols[3].blue = 0x5555;
cols[4].red = cols[4].green = cols[4].blue = 0x8888;
cols[5].red = cols[5].green = cols[5].blue = 0xffff;
getColours(dpy, cols, 6);
black = defaultFg = cols[0].pixel;
defaultBg = disabledBg = cols[1].pixel;
lightBg = cols[2].pixel;
darkBg = disabledFg = cols[3].pixel;
scrollbarBg = cols[4].pixel;
white = enabledBg = cols[5].pixel;
defaultGC = XCreateGC(dpy, DefaultRootWindow(dpy), 0, 0);
defaultFS
= XLoadQueryFont(dpy, "-*-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*");
if (!defaultFS) {
defaultFS = XLoadQueryFont(dpy, "fixed");
if (!defaultFS) {
fprintf(stderr,"Failed to load any font\n");
exit(1);
}
}
defaultFont = defaultFS->fid;
XSetForeground(dpy, defaultGC, defaultFg);
XSetBackground(dpy, defaultGC, defaultBg);
XSetFont(dpy, defaultGC, defaultFont);
XSelectInput(dpy, DefaultRootWindow(dpy), PropertyChangeMask);
static unsigned char dotBits[] = { 0x06, 0x0f, 0x0f, 0x06};
dot = XCreateBitmapFromData(dpy, DefaultRootWindow(dpy), (char*)dotBits,
dotSize, dotSize);
static unsigned char tickBits[] = { 0x80, 0xc0, 0xe2, 0x76, 0x3e, 0x1c, 0x08, 0x00};
tick = XCreateBitmapFromData(dpy, DefaultRootWindow(dpy), (char*)tickBits,
tickSize, tickSize);
defaultWindowClass = rfb::strDup(defaultWindowClass_);
}
void TXWindow::handleXEvents(Display* dpy)
{
while (XPending(dpy)) {
XEvent ev;
XNextEvent(dpy, &ev);
if (globalEventHandler) {
if (globalEventHandler->handleGlobalEvent(&ev))
continue;
}
if (ev.type == MappingNotify) {
XRefreshKeyboardMapping(&ev.xmapping);
} else if (ev.type == PropertyNotify &&
ev.xproperty.window == DefaultRootWindow(dpy) &&
ev.xproperty.atom == XA_CUT_BUFFER0) {
cutBufferTime = ev.xproperty.time;
} else {
std::list<TXWindow*>::iterator i;
for (i = windows.begin(); i != windows.end(); i++) {
if ((*i)->win() == ev.xany.window)
(*i)->handleXEvent(&ev);
}
}
}
}
TXGlobalEventHandler* TXWindow::setGlobalEventHandler(TXGlobalEventHandler* h)
{
TXGlobalEventHandler* old = globalEventHandler;
globalEventHandler = h;
return old;
}
void TXWindow::getColours(Display* dpy, XColor* cols, int nCols)
{
bool* got = new bool[nCols];
bool failed = false;
int i;
for (i = 0; i < nCols; i++) {
if (XAllocColor(dpy, cmap, &cols[i])) {
got[i] = true;
} else {
got[i] = false;
failed = true;
}
}
if (!failed) {
delete [] got;
return;
}
// AllocColor has failed. This is because the colormap is full. So the
// only thing we can do is use the "shared" pixels in the colormap. The
// code below is designed to work even when the colormap isn't full so is
// more complex than it needs to be in this case. However it would be
// useful one day to be able to restrict the number of colours allocated by
// an application so I'm leaving it in here.
// For each pixel in the colormap, try to allocate exactly its RGB values.
// If this returns a different pixel then it must be a private or
// unallocated pixel, so we can't use it. If it returns us the same pixel
// again, it's almost certainly a shared colour, so we can use it (actually
// it is possible that it was an unallocated pixel which we've now
// allocated - by going through the pixels in reverse order we make this
// unlikely except for the lowest unallocated pixel - this works because of
// the way the X server allocates new pixels).
int cmapSize = DisplayCells(dpy,DefaultScreen(dpy));
XColor* cm = new XColor[cmapSize];
bool* shared = new bool[cmapSize];
bool* usedAsNearest = new bool[cmapSize];
for (i = 0; i < cmapSize; i++) {
cm[i].pixel = i;
shared[i] = usedAsNearest[i] = false;
}
XQueryColors(dpy, cmap, cm, cmapSize);
for (i = cmapSize-1; i >= 0; i--) {
if (XAllocColor(dpy, cmap, &cm[i])) {
if (cm[i].pixel == (unsigned long)i) {
shared[i] = true;
} else {
XFreeColors(dpy, cmap, &cm[i].pixel, 1, 0);
}
}
}
for (int j = 0; j < nCols; j++) {
unsigned long minDistance = ULONG_MAX;
unsigned long nearestPixel = 0;
if (!got[j]) {
for (i = 0; i < cmapSize; i++) {
if (shared[i]) {
unsigned long rd = (cm[i].red - cols[j].red)/2;
unsigned long gd = (cm[i].green - cols[j].green)/2;
unsigned long bd = (cm[i].blue - cols[j].blue)/2;
unsigned long distance = (rd*rd + gd*gd + bd*bd);
if (distance < minDistance) {
minDistance = distance;
nearestPixel = i;
}
}
}
cols[j].pixel = nearestPixel;
usedAsNearest[nearestPixel] = true;
}
}
for (i = 0; i < cmapSize; i++) {
if (shared[i] && !usedAsNearest[i]) {
unsigned long p = i;
XFreeColors(dpy, cmap, &p, 1, 0);
}
}
}
Window TXWindow::windowWithName(Display* dpy, Window top, const char* name)
{
char* windowName;
if (XFetchName(dpy, top, &windowName)) {
if (strcmp(windowName, name) == 0) {
XFree(windowName);
return top;
}
XFree(windowName);
}
Window* children;
Window dummy;
unsigned int nchildren;
if (!XQueryTree(dpy, top, &dummy, &dummy, &children,&nchildren) || !children)
return 0;
for (int i = 0; i < (int)nchildren; i++) {
Window w = windowWithName(dpy, children[i], name);
if (w) {
XFree((char*)children);
return w;
}
}
XFree((char*)children);
return 0;
}
TXWindow::TXWindow(Display* dpy_, int w, int h, TXWindow* parent_,
int borderWidth)
: dpy(dpy_), xPad(3), yPad(3), bevel(2), parent(parent_), width_(w),
height_(h), eventHandler(0), dwc(0), eventMask(0), toplevel_(false)
{
sizeHints.flags = 0;
XSetWindowAttributes attr;
attr.background_pixel = defaultBg;
attr.border_pixel = 0;
Window par = parent ? parent->win() : DefaultRootWindow(dpy);
win_ = XCreateWindow(dpy, par, 0, 0, width_, height_, borderWidth,
CopyFromParent, CopyFromParent, CopyFromParent,
CWBackPixel | CWBorderPixel, &attr);
if (parent) map();
windows.push_back(this);
}
TXWindow::~TXWindow()
{
windows.remove(this);
XDestroyWindow(dpy, win());
}
void TXWindow::toplevel(const char* name, TXDeleteWindowCallback* dwc_,
int argc, char** argv, const char* windowClass,
bool iconic)
{
toplevel_ = true;
XWMHints wmHints;
wmHints.flags = InputHint|StateHint;
wmHints.input = True;
wmHints.initial_state = iconic ? IconicState : NormalState;
XClassHint classHint;
if (!windowClass) windowClass = defaultWindowClass;
classHint.res_name = (char*)name;
classHint.res_class = (char*)windowClass;
XSetWMProperties(dpy, win(), 0, 0, argv, argc,
&sizeHints, &wmHints, &classHint);
XStoreName(dpy, win(), name);
XSetIconName(dpy, win(), name);
Atom protocols[10];
int nProtocols = 0;
protocols[nProtocols++] = wmTakeFocus;
dwc = dwc_;
if (dwc)
protocols[nProtocols++] = wmDeleteWindow;
XSetWMProtocols(dpy, win(), protocols, nProtocols);
addEventMask(StructureNotifyMask);
}
void TXWindow::setName(const char* name)
{
XClassHint classHint;
XGetClassHint(dpy, win(), &classHint);
XFree(classHint.res_name);
classHint.res_name = (char*)name;
XSetClassHint(dpy, win(), &classHint);
XFree(classHint.res_class);
XStoreName(dpy, win(), name);
XSetIconName(dpy, win(), name);
}
void TXWindow::setMaxSize(int w, int h)
{
sizeHints.flags |= PMaxSize;
sizeHints.max_width = w;
sizeHints.max_height = h;
XSetWMNormalHints(dpy, win(), &sizeHints);
}
void TXWindow::setUSPosition(int x, int y)
{
sizeHints.flags |= USPosition;
sizeHints.x = x;
sizeHints.y = y;
XSetWMNormalHints(dpy, win(), &sizeHints);
move(x, y);
}
void TXWindow::setGeometry(const char* geom, int x, int y, int w, int h)
{
char defGeom[256];
sprintf(defGeom,"%dx%d+%d+%d",w,h,x,y);
XWMGeometry(dpy, DefaultScreen(dpy), strEmptyToNull((char*)geom), defGeom,
0, &sizeHints, &x, &y, &w, &h, &sizeHints.win_gravity);
sizeHints.flags |= PWinGravity;
setUSPosition(x, y);
resize(w, h);
}
TXEventHandler* TXWindow::setEventHandler(TXEventHandler* h)
{
TXEventHandler* old = eventHandler;
eventHandler = h;
return old;
}
void TXWindow::addEventMask(long mask)
{
eventMask |= mask;
XSelectInput(dpy, win(), eventMask);
}
void TXWindow::removeEventMask(long mask)
{
eventMask &= ~mask;
XSelectInput(dpy, win(), eventMask);
}
void TXWindow::unmap()
{
XUnmapWindow(dpy, win());
if (toplevel_) {
XUnmapEvent ue;
ue.type = UnmapNotify;
ue.display = dpy;
ue.event = DefaultRootWindow(dpy);
ue.window = win();
ue.from_configure = False;
XSendEvent(dpy, DefaultRootWindow(dpy), False,
(SubstructureRedirectMask|SubstructureNotifyMask),
(XEvent*)&ue);
}
}
void TXWindow::resize(int w, int h)
{
//if (w == width_ && h == height_) return;
XResizeWindow(dpy, win(), w, h);
width_ = w;
height_ = h;
resizeNotify();
}
void TXWindow::setBorderWidth(int bw)
{
XWindowChanges c;
c.border_width = bw;
XConfigureWindow(dpy, win(), CWBorderWidth, &c);
}
void TXWindow::ownSelection(Atom selection, Time time)
{
XSetSelectionOwner(dpy, selection, win(), time);
if (XGetSelectionOwner(dpy, selection) == win()) {
selectionOwner_[selection] = true;
selectionOwnTime[selection] = time;
}
}
void TXWindow::handleXEvent(XEvent* ev)
{
switch (ev->type) {
case ClientMessage:
if (ev->xclient.message_type == wmProtocols) {
if ((Atom)ev->xclient.data.l[0] == wmDeleteWindow) {
if (dwc) dwc->deleteWindow(this);
} else if ((Atom)ev->xclient.data.l[0] == wmTakeFocus) {
takeFocus(ev->xclient.data.l[1]);
}
}
break;
case ConfigureNotify:
if (ev->xconfigure.width != width_ || ev->xconfigure.height != height_) {
width_ = ev->xconfigure.width;
height_ = ev->xconfigure.height;
resizeNotify();
}
break;
case SelectionNotify:
if (ev->xselection.property != None) {
Atom type;
int format;
unsigned long nitems, after;
unsigned char *data;
XGetWindowProperty(dpy, win(), ev->xselection.property, 0, 16384, True,
AnyPropertyType, &type, &format,
&nitems, &after, &data);
if (type != None) {
selectionNotify(&ev->xselection, type, format, nitems, data);
XFree(data);
break;
}
}
selectionNotify(&ev->xselection, 0, 0, 0, 0);
break;
case SelectionRequest:
{
XSelectionEvent se;
se.type = SelectionNotify;
se.display = ev->xselectionrequest.display;
se.requestor = ev->xselectionrequest.requestor;
se.selection = ev->xselectionrequest.selection;
se.time = ev->xselectionrequest.time;
se.target = ev->xselectionrequest.target;
if (ev->xselectionrequest.property == None)
ev->xselectionrequest.property = ev->xselectionrequest.target;
if (!selectionOwner_[se.selection]) {
se.property = None;
} else {
se.property = ev->xselectionrequest.property;
if (se.target == xaTARGETS) {
Atom targets[2];
targets[0] = xaTIMESTAMP;
targets[1] = XA_STRING;
XChangeProperty(dpy, se.requestor, se.property, XA_ATOM, 32,
PropModeReplace, (unsigned char*)targets, 2);
} else if (se.target == xaTIMESTAMP) {
rdr::U32 t = selectionOwnTime[se.selection];
XChangeProperty(dpy, se.requestor, se.property, XA_INTEGER, 32,
PropModeReplace, (unsigned char*)&t, 1);
} else if (se.target == XA_STRING) {
if (!selectionRequest(se.requestor, se.selection, se.property))
se.property = None;
} else {
se.property = None;
}
}
XSendEvent(dpy, se.requestor, False, 0, (XEvent*)&se);
break;
}
case SelectionClear:
selectionOwner_[ev->xselectionclear.selection] = false;
break;
}
if (eventHandler) eventHandler->handleEvent(this, ev);
}
void TXWindow::drawBevel(GC gc, int x, int y, int w, int h, int b,
unsigned long middle, unsigned long tl,
unsigned long br, bool round)
{
if (round) {
XGCValues gcv;
gcv.line_width = b;
XChangeGC(dpy, gc, GCLineWidth, &gcv);
XSetForeground(dpy, gc, middle);
XFillArc(dpy, win(), gc, x, y, w-b/2, h-b/2, 0, 360*64);
XSetForeground(dpy, gc, tl);
XDrawArc(dpy, win(), gc, x, y, w-b/2, h-b/2, 45*64, 180*64);
XSetForeground(dpy, gc, br);
XDrawArc(dpy, win(), gc, x, y, w-b/2, h-b/2, 225*64, 180*64);
} else {
XSetForeground(dpy, gc, middle);
if (w-2*b > 0 && h-2*b > 0)
XFillRectangle(dpy, win(), gc, x+b, y+b, w-2*b, h-2*b);
XSetForeground(dpy, gc, tl);
XFillRectangle(dpy, win(), gc, x, y, w, b);
XFillRectangle(dpy, win(), gc, x, y, b, h);
XSetForeground(dpy, gc, br);
for (int i = 0; i < b; i++) {
if (w-i > 0) XFillRectangle(dpy, win(), gc, x+i, y+h-1-i, w-i, 1);
if (h-1-i > 0) XFillRectangle(dpy, win(), gc, x+w-1-i, y+i+1, 1, h-1-i);
}
}
}

228
unix/tx/TXWindow.h Normal file
View File

@@ -0,0 +1,228 @@
/* 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.
*/
//
// TXWindow.h
//
// A TXWindow is the base class for all tx windows (widgets). In addition it
// contains a number of static methods and members which are used throughout
// tx.
//
// Before calling any other tx methods, TXWindow::init() must be called with
// the X display to use.
#ifndef __TXWINDOW_H__
#define __TXWINDOW_H__
#include <rdr/types.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <map>
// TXDeleteWindowCallback's deleteWindow() method is called when a top-level
// window is "deleted" (closed) by the user using the window manager.
class TXWindow;
class TXDeleteWindowCallback {
public:
virtual void deleteWindow(TXWindow* w) = 0;
};
// TXEventHandler is an interface implemented by classes wanting to handle X
// events on a window. Most derived classes of window are their own event
// handlers.
class TXEventHandler {
public:
virtual void handleEvent(TXWindow* w, XEvent* ev) = 0;
};
// TXGlobalEventHandler is similar to TXEventHandler but will be called early and
// for every X event. The handler should return true to indicate that the
// event was swallowed and shouldn't be processed further.
class TXGlobalEventHandler {
public:
virtual bool handleGlobalEvent(XEvent* ev) = 0;
};
class TXWindow {
public:
// Constructor - creates a window of the given size, with the default
// background (currently grey). It is mapped by default if it has a parent.
// If no parent is specified its parent is the root window and it will remain
// unmapped.
TXWindow(Display* dpy_, int width=1, int height=1, TXWindow* parent_=0,
int borderWidth=0);
virtual ~TXWindow();
// toplevel() declares that this is a top-level window. Various
// window-manager-related properties are set on the window. The given
// TXDeleteWindowCallback is notified when the window is "deleted" (cloesd)
// by the user.
void toplevel(const char* name, TXDeleteWindowCallback* dwc=0,
int argc=0, char** argv=0, const char* windowClass=0,
bool iconic=false);
// setMaxSize() tells the window manager the maximum size to allow a
// top-level window. It has no effect on a non-top-level window.
void setMaxSize(int w, int h);
// setUSPosition() tells the window manager the position which the "user" has
// asked for a top-level window. Most window managers ignore requests by a
// program for position, so you have to tell it that the "user" asked for the
// position. This has no effect on a non-top-level window.
void setUSPosition(int x, int y);
void setGeometry(const char* geom, int x, int y, int w, int h);
void setName(const char* name);
// setTransientFor() tells the window manager that this window is "owned" by
// the given window. The window manager can use this information as it sees
// fit.
void setTransientFor(Window w) { XSetTransientForHint(dpy, win(), w); }
// setEventHandler() sets the TXEventHandler to handle X events for this
// window. It returns the previous event handler, so that handlers can chain
// themselves.
TXEventHandler* setEventHandler(TXEventHandler* h);
// Accessor methods
Window win() { return win_; }
int width() { return width_; }
int height() { return height_; }
// selectionOwner() returns true if this window owns the given selection.
bool selectionOwner(Atom selection) { return selectionOwner_[selection]; }
// Wrappers around common Xlib calls
void addEventMask(long mask);
void removeEventMask(long mask);
void map() { XMapWindow(dpy, win()); }
void unmap();
void setBg(unsigned long bg) { XSetWindowBackground(dpy, win(), bg); }
void move(int x, int y) { XMoveWindow(dpy, win(), x, y); }
void resize(int w, int h);
void raise() { XRaiseWindow(dpy, win()); }
void setBorderWidth(int bw);
void invalidate(int x=0, int y=0, int w=0, int h=0) { XClearArea(dpy, win(), x, y, w, h, True); }
// ownSelection requests that the window owns the given selection from the
// given time (the time should be taken from an X event).
void ownSelection(Atom selection, Time time);
// drawBevel draws a rectangular or circular bevel filling the given
// rectangle, using the given colours for the middle, the top/left and the
// bottom/right.
void drawBevel(GC gc, int x, int y, int w, int h, int b,
unsigned long middle, unsigned long tl, unsigned long br,
bool round=false);
// Methods to be overridden in a derived class
// resizeNotify() is called whenever the window's dimensions may have
// changed.
virtual void resizeNotify() {}
// takeFocus() is called when the window has received keyboard focus from the
// window manager.
virtual void takeFocus(Time time) {}
// selectionNotify() is called when the selection owner has replied to a
// request for information about a selection from the selection owner.
virtual void selectionNotify(XSelectionEvent* ev, Atom type, int format,
int nitems, void* data) {}
// selectionRequest() is called when this window is the selection owner and
// another X client has requested the selection. It should set the given
// property on the given window to the value of the given selection,
// returning true if successful, false otherwise.
virtual bool selectionRequest(Window requestor,
Atom selection, Atom property) { return false;}
// Static methods
// init() must be called before any other tx methods.
static void init(Display* dpy, const char* defaultWindowClass);
// getColours() sets the pixel values in the cols array to the best available
// for the given rgb values, even in the case of a full colormap.
static void getColours(Display* dpy, XColor* cols, int nCols);
// handleXEvents() should be called whenever there are events to handle on
// the connection to the X display. It process all available events, then
// returns when there are no more events to process.
static void handleXEvents(Display* dpy);
// setGlobalEventHandler() sets the TXGlobalEventHandler to intercept all
// X events. It returns the previous events handler, so that handlers can
// chain themselves.
static TXGlobalEventHandler* setGlobalEventHandler(TXGlobalEventHandler* h);
// windowWithName() locates a window with a given name on a display.
static Window windowWithName(Display* dpy, Window top, const char* name);
// strEmptyToNull() returns the string it's given but turns an empty string
// into null, which can be useful for passing rfb parameters to Xlib calls.
static char* strEmptyToNull(char* s) { return s && s[0] ? s : 0; }
// The following are default values for various things.
static unsigned long black, white;
static unsigned long defaultFg, defaultBg, lightBg, darkBg;
static unsigned long disabledFg, disabledBg, enabledBg;
static unsigned long scrollbarBg;
static GC defaultGC;
static Colormap cmap;
static Font defaultFont;
static XFontStruct* defaultFS;
static Time cutBufferTime;
static Pixmap dot, tick;
static const int dotSize, tickSize;
static char* defaultWindowClass;
Display* const dpy;
int xPad, yPad, bevel;
private:
// handleXEvent() is called from handleXEvents() when an event for this
// window arrives. It does general event processing before calling on to the
// event handler.
void handleXEvent(XEvent* ev);
TXWindow* parent;
Window win_;
int width_, height_;
TXEventHandler* eventHandler;
TXDeleteWindowCallback* dwc;
long eventMask;
XSizeHints sizeHints;
std::map<Atom,Time> selectionOwnTime;
std::map<Atom,bool> selectionOwner_;
bool toplevel_;
static TXGlobalEventHandler* globalEventHandler;
};
extern Atom wmProtocols, wmDeleteWindow, wmTakeFocus;
extern Atom xaTIMESTAMP, xaTARGETS, xaSELECTION_TIME, xaSELECTION_STRING;
extern Atom xaCLIPBOARD;
#endif