You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TXWindow.cxx 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. // TXWindow.cxx
  20. //
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <X11/Xatom.h>
  25. #include "TXWindow.h"
  26. #include <list>
  27. #include <limits.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <vector>
  32. std::list<TXWindow*> windows;
  33. Atom wmProtocols, wmDeleteWindow, wmTakeFocus;
  34. Atom xaTIMESTAMP, xaTARGETS, xaSELECTION_TIME, xaSELECTION_STRING;
  35. Atom xaCLIPBOARD;
  36. unsigned long TXWindow::black, TXWindow::white;
  37. unsigned long TXWindow::defaultFg, TXWindow::defaultBg;
  38. unsigned long TXWindow::lightBg, TXWindow::darkBg;
  39. unsigned long TXWindow::disabledFg, TXWindow::disabledBg;
  40. unsigned long TXWindow::enabledBg;
  41. unsigned long TXWindow::scrollbarBg;
  42. Colormap TXWindow::cmap = 0;
  43. GC TXWindow::defaultGC = 0;
  44. Font TXWindow::defaultFont = 0;
  45. XFontStruct* TXWindow::defaultFS = 0;
  46. Time TXWindow::cutBufferTime = 0;
  47. Pixmap TXWindow::dot = 0, TXWindow::tick = 0;
  48. const int TXWindow::dotSize = 4, TXWindow::tickSize = 8;
  49. char* TXWindow::defaultWindowClass;
  50. TXGlobalEventHandler* TXWindow::globalEventHandler = NULL;
  51. void TXWindow::init(Display* dpy, const char* defaultWindowClass_)
  52. {
  53. cmap = DefaultColormap(dpy,DefaultScreen(dpy));
  54. wmProtocols = XInternAtom(dpy, "WM_PROTOCOLS", False);
  55. wmDeleteWindow = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  56. wmTakeFocus = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
  57. xaTIMESTAMP = XInternAtom(dpy, "TIMESTAMP", False);
  58. xaTARGETS = XInternAtom(dpy, "TARGETS", False);
  59. xaSELECTION_TIME = XInternAtom(dpy, "SELECTION_TIME", False);
  60. xaSELECTION_STRING = XInternAtom(dpy, "SELECTION_STRING", False);
  61. xaCLIPBOARD = XInternAtom(dpy, "CLIPBOARD", False);
  62. XColor cols[6];
  63. cols[0].red = cols[0].green = cols[0].blue = 0x0000;
  64. cols[1].red = cols[1].green = cols[1].blue = 0xbbbb;
  65. cols[2].red = cols[2].green = cols[2].blue = 0xeeee;
  66. cols[3].red = cols[3].green = cols[3].blue = 0x5555;
  67. cols[4].red = cols[4].green = cols[4].blue = 0x8888;
  68. cols[5].red = cols[5].green = cols[5].blue = 0xffff;
  69. getColours(dpy, cols, 6);
  70. black = defaultFg = cols[0].pixel;
  71. defaultBg = disabledBg = cols[1].pixel;
  72. lightBg = cols[2].pixel;
  73. darkBg = disabledFg = cols[3].pixel;
  74. scrollbarBg = cols[4].pixel;
  75. white = enabledBg = cols[5].pixel;
  76. defaultGC = XCreateGC(dpy, DefaultRootWindow(dpy), 0, 0);
  77. defaultFS
  78. = XLoadQueryFont(dpy, "-*-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*");
  79. if (!defaultFS) {
  80. defaultFS = XLoadQueryFont(dpy, "fixed");
  81. if (!defaultFS) {
  82. fprintf(stderr,"Failed to load any font\n");
  83. exit(1);
  84. }
  85. }
  86. defaultFont = defaultFS->fid;
  87. XSetForeground(dpy, defaultGC, defaultFg);
  88. XSetBackground(dpy, defaultGC, defaultBg);
  89. XSetFont(dpy, defaultGC, defaultFont);
  90. XSelectInput(dpy, DefaultRootWindow(dpy), PropertyChangeMask);
  91. static unsigned char dotBits[] = { 0x06, 0x0f, 0x0f, 0x06};
  92. dot = XCreateBitmapFromData(dpy, DefaultRootWindow(dpy), (char*)dotBits,
  93. dotSize, dotSize);
  94. static unsigned char tickBits[] = { 0x80, 0xc0, 0xe2, 0x76, 0x3e, 0x1c, 0x08, 0x00};
  95. tick = XCreateBitmapFromData(dpy, DefaultRootWindow(dpy), (char*)tickBits,
  96. tickSize, tickSize);
  97. defaultWindowClass = strdup(defaultWindowClass_);
  98. }
  99. void TXWindow::handleXEvents(Display* dpy)
  100. {
  101. while (XPending(dpy)) {
  102. XEvent ev;
  103. XNextEvent(dpy, &ev);
  104. if (globalEventHandler) {
  105. if (globalEventHandler->handleGlobalEvent(&ev))
  106. continue;
  107. }
  108. if (ev.type == MappingNotify) {
  109. XRefreshKeyboardMapping(&ev.xmapping);
  110. } else if (ev.type == PropertyNotify &&
  111. ev.xproperty.window == DefaultRootWindow(dpy) &&
  112. ev.xproperty.atom == XA_CUT_BUFFER0) {
  113. cutBufferTime = ev.xproperty.time;
  114. } else {
  115. std::list<TXWindow*>::iterator i;
  116. for (i = windows.begin(); i != windows.end(); i++) {
  117. if ((*i)->win() == ev.xany.window)
  118. (*i)->handleXEvent(&ev);
  119. }
  120. }
  121. }
  122. }
  123. TXGlobalEventHandler* TXWindow::setGlobalEventHandler(TXGlobalEventHandler* h)
  124. {
  125. TXGlobalEventHandler* old = globalEventHandler;
  126. globalEventHandler = h;
  127. return old;
  128. }
  129. void TXWindow::getColours(Display* dpy, XColor* cols, int nCols)
  130. {
  131. std::vector<bool> got;
  132. bool failed = false;
  133. int i;
  134. for (i = 0; i < nCols; i++) {
  135. if (XAllocColor(dpy, cmap, &cols[i])) {
  136. got.push_back(true);
  137. } else {
  138. got.push_back(false);
  139. failed = true;
  140. }
  141. }
  142. if (!failed) {
  143. return;
  144. }
  145. // AllocColor has failed. This is because the colormap is full. So the
  146. // only thing we can do is use the "shared" pixels in the colormap. The
  147. // code below is designed to work even when the colormap isn't full so is
  148. // more complex than it needs to be in this case. However it would be
  149. // useful one day to be able to restrict the number of colours allocated by
  150. // an application so I'm leaving it in here.
  151. // For each pixel in the colormap, try to allocate exactly its RGB values.
  152. // If this returns a different pixel then it must be a private or
  153. // unallocated pixel, so we can't use it. If it returns us the same pixel
  154. // again, it's almost certainly a shared colour, so we can use it (actually
  155. // it is possible that it was an unallocated pixel which we've now
  156. // allocated - by going through the pixels in reverse order we make this
  157. // unlikely except for the lowest unallocated pixel - this works because of
  158. // the way the X server allocates new pixels).
  159. int cmapSize = DisplayCells(dpy,DefaultScreen(dpy));
  160. XColor* cm = new XColor[cmapSize];
  161. std::vector<bool> shared;
  162. std::vector<bool> usedAsNearest;
  163. for (i = 0; i < cmapSize; i++) {
  164. cm[i].pixel = i;
  165. shared.push_back(false);
  166. usedAsNearest.push_back(false);
  167. }
  168. XQueryColors(dpy, cmap, cm, cmapSize);
  169. for (i = cmapSize-1; i >= 0; i--) {
  170. if (XAllocColor(dpy, cmap, &cm[i])) {
  171. if (cm[i].pixel == (unsigned long)i) {
  172. shared[i] = true;
  173. } else {
  174. XFreeColors(dpy, cmap, &cm[i].pixel, 1, 0);
  175. }
  176. }
  177. }
  178. for (int j = 0; j < nCols; j++) {
  179. unsigned long minDistance = ULONG_MAX;
  180. unsigned long nearestPixel = 0;
  181. if (!got[j]) {
  182. for (i = 0; i < cmapSize; i++) {
  183. if (shared[i]) {
  184. unsigned long rd = (cm[i].red - cols[j].red)/2;
  185. unsigned long gd = (cm[i].green - cols[j].green)/2;
  186. unsigned long bd = (cm[i].blue - cols[j].blue)/2;
  187. unsigned long distance = (rd*rd + gd*gd + bd*bd);
  188. if (distance < minDistance) {
  189. minDistance = distance;
  190. nearestPixel = i;
  191. }
  192. }
  193. }
  194. cols[j].pixel = nearestPixel;
  195. usedAsNearest[nearestPixel] = true;
  196. }
  197. }
  198. for (i = 0; i < cmapSize; i++) {
  199. if (shared[i] && !usedAsNearest[i]) {
  200. unsigned long p = i;
  201. XFreeColors(dpy, cmap, &p, 1, 0);
  202. }
  203. }
  204. }
  205. Window TXWindow::windowWithName(Display* dpy, Window top, const char* name)
  206. {
  207. char* windowName;
  208. if (XFetchName(dpy, top, &windowName)) {
  209. if (strcmp(windowName, name) == 0) {
  210. XFree(windowName);
  211. return top;
  212. }
  213. XFree(windowName);
  214. }
  215. Window* children;
  216. Window dummy;
  217. unsigned int nchildren;
  218. if (!XQueryTree(dpy, top, &dummy, &dummy, &children,&nchildren) || !children)
  219. return 0;
  220. for (int i = 0; i < (int)nchildren; i++) {
  221. Window w = windowWithName(dpy, children[i], name);
  222. if (w) {
  223. XFree((char*)children);
  224. return w;
  225. }
  226. }
  227. XFree((char*)children);
  228. return 0;
  229. }
  230. TXWindow::TXWindow(Display* dpy_, int w, int h, TXWindow* parent_,
  231. int borderWidth)
  232. : dpy(dpy_), xPad(3), yPad(3), bevel(2), parent(parent_), width_(w),
  233. height_(h), eventHandler(0), dwc(0), eventMask(0), toplevel_(false)
  234. {
  235. sizeHints.flags = 0;
  236. XSetWindowAttributes attr;
  237. attr.background_pixel = defaultBg;
  238. attr.border_pixel = 0;
  239. Window par = parent ? parent->win() : DefaultRootWindow(dpy);
  240. win_ = XCreateWindow(dpy, par, 0, 0, width_, height_, borderWidth,
  241. CopyFromParent, CopyFromParent, CopyFromParent,
  242. CWBackPixel | CWBorderPixel, &attr);
  243. if (parent) map();
  244. windows.push_back(this);
  245. }
  246. TXWindow::~TXWindow()
  247. {
  248. windows.remove(this);
  249. XDestroyWindow(dpy, win());
  250. }
  251. void TXWindow::toplevel(const char* name, TXDeleteWindowCallback* dwc_,
  252. int argc, char** argv, const char* windowClass,
  253. bool iconic)
  254. {
  255. toplevel_ = true;
  256. XWMHints wmHints;
  257. wmHints.flags = InputHint|StateHint;
  258. wmHints.input = True;
  259. wmHints.initial_state = iconic ? IconicState : NormalState;
  260. XClassHint classHint;
  261. if (!windowClass) windowClass = defaultWindowClass;
  262. classHint.res_name = (char*)name;
  263. classHint.res_class = (char*)windowClass;
  264. XSetWMProperties(dpy, win(), 0, 0, argv, argc,
  265. &sizeHints, &wmHints, &classHint);
  266. XStoreName(dpy, win(), name);
  267. XSetIconName(dpy, win(), name);
  268. Atom protocols[10];
  269. int nProtocols = 0;
  270. protocols[nProtocols++] = wmTakeFocus;
  271. dwc = dwc_;
  272. if (dwc)
  273. protocols[nProtocols++] = wmDeleteWindow;
  274. XSetWMProtocols(dpy, win(), protocols, nProtocols);
  275. addEventMask(StructureNotifyMask);
  276. }
  277. void TXWindow::setName(const char* name)
  278. {
  279. XClassHint classHint;
  280. XGetClassHint(dpy, win(), &classHint);
  281. XFree(classHint.res_name);
  282. classHint.res_name = (char*)name;
  283. XSetClassHint(dpy, win(), &classHint);
  284. XFree(classHint.res_class);
  285. XStoreName(dpy, win(), name);
  286. XSetIconName(dpy, win(), name);
  287. }
  288. void TXWindow::setMaxSize(int w, int h)
  289. {
  290. sizeHints.flags |= PMaxSize;
  291. sizeHints.max_width = w;
  292. sizeHints.max_height = h;
  293. XSetWMNormalHints(dpy, win(), &sizeHints);
  294. }
  295. void TXWindow::setUSPosition(int x, int y)
  296. {
  297. sizeHints.flags |= USPosition;
  298. sizeHints.x = x;
  299. sizeHints.y = y;
  300. XSetWMNormalHints(dpy, win(), &sizeHints);
  301. move(x, y);
  302. }
  303. void TXWindow::setGeometry(const char* geom, int x, int y, int w, int h)
  304. {
  305. char defGeom[256];
  306. sprintf(defGeom,"%dx%d+%d+%d",w,h,x,y);
  307. XWMGeometry(dpy, DefaultScreen(dpy), strEmptyToNull((char*)geom), defGeom,
  308. 0, &sizeHints, &x, &y, &w, &h, &sizeHints.win_gravity);
  309. sizeHints.flags |= PWinGravity;
  310. setUSPosition(x, y);
  311. resize(w, h);
  312. }
  313. TXEventHandler* TXWindow::setEventHandler(TXEventHandler* h)
  314. {
  315. TXEventHandler* old = eventHandler;
  316. eventHandler = h;
  317. return old;
  318. }
  319. void TXWindow::addEventMask(long mask)
  320. {
  321. eventMask |= mask;
  322. XSelectInput(dpy, win(), eventMask);
  323. }
  324. void TXWindow::removeEventMask(long mask)
  325. {
  326. eventMask &= ~mask;
  327. XSelectInput(dpy, win(), eventMask);
  328. }
  329. void TXWindow::unmap()
  330. {
  331. XUnmapWindow(dpy, win());
  332. if (toplevel_) {
  333. XUnmapEvent ue;
  334. ue.type = UnmapNotify;
  335. ue.display = dpy;
  336. ue.event = DefaultRootWindow(dpy);
  337. ue.window = win();
  338. ue.from_configure = False;
  339. XSendEvent(dpy, DefaultRootWindow(dpy), False,
  340. (SubstructureRedirectMask|SubstructureNotifyMask),
  341. (XEvent*)&ue);
  342. }
  343. }
  344. void TXWindow::resize(int w, int h)
  345. {
  346. //if (w == width_ && h == height_) return;
  347. XResizeWindow(dpy, win(), w, h);
  348. width_ = w;
  349. height_ = h;
  350. resizeNotify();
  351. }
  352. void TXWindow::setBorderWidth(int bw)
  353. {
  354. XWindowChanges c;
  355. c.border_width = bw;
  356. XConfigureWindow(dpy, win(), CWBorderWidth, &c);
  357. }
  358. void TXWindow::ownSelection(Atom selection, Time time)
  359. {
  360. XSetSelectionOwner(dpy, selection, win(), time);
  361. if (XGetSelectionOwner(dpy, selection) == win()) {
  362. selectionOwner_[selection] = true;
  363. selectionOwnTime[selection] = time;
  364. }
  365. }
  366. void TXWindow::handleXEvent(XEvent* ev)
  367. {
  368. switch (ev->type) {
  369. case ClientMessage:
  370. if (ev->xclient.message_type == wmProtocols) {
  371. if ((Atom)ev->xclient.data.l[0] == wmDeleteWindow) {
  372. if (dwc) dwc->deleteWindow(this);
  373. } else if ((Atom)ev->xclient.data.l[0] == wmTakeFocus) {
  374. takeFocus(ev->xclient.data.l[1]);
  375. }
  376. }
  377. break;
  378. case ConfigureNotify:
  379. if (ev->xconfigure.width != width_ || ev->xconfigure.height != height_) {
  380. width_ = ev->xconfigure.width;
  381. height_ = ev->xconfigure.height;
  382. resizeNotify();
  383. }
  384. break;
  385. case SelectionNotify:
  386. if (ev->xselection.property != None) {
  387. Atom type;
  388. int format;
  389. unsigned long nitems, after;
  390. unsigned char *data;
  391. XGetWindowProperty(dpy, win(), ev->xselection.property, 0, 16384, True,
  392. AnyPropertyType, &type, &format,
  393. &nitems, &after, &data);
  394. if (type != None) {
  395. selectionNotify(&ev->xselection, type, format, nitems, data);
  396. XFree(data);
  397. break;
  398. }
  399. }
  400. selectionNotify(&ev->xselection, 0, 0, 0, 0);
  401. break;
  402. case SelectionRequest:
  403. {
  404. XSelectionEvent se;
  405. se.type = SelectionNotify;
  406. se.display = ev->xselectionrequest.display;
  407. se.requestor = ev->xselectionrequest.requestor;
  408. se.selection = ev->xselectionrequest.selection;
  409. se.time = ev->xselectionrequest.time;
  410. se.target = ev->xselectionrequest.target;
  411. if (ev->xselectionrequest.property == None)
  412. ev->xselectionrequest.property = ev->xselectionrequest.target;
  413. if (!selectionOwner_[se.selection]) {
  414. se.property = None;
  415. } else {
  416. se.property = ev->xselectionrequest.property;
  417. if (se.target == xaTARGETS) {
  418. Atom targets[2];
  419. targets[0] = xaTIMESTAMP;
  420. targets[1] = XA_STRING;
  421. XChangeProperty(dpy, se.requestor, se.property, XA_ATOM, 32,
  422. PropModeReplace, (unsigned char*)targets, 2);
  423. } else if (se.target == xaTIMESTAMP) {
  424. Time t = selectionOwnTime[se.selection];
  425. XChangeProperty(dpy, se.requestor, se.property, XA_INTEGER, 32,
  426. PropModeReplace, (unsigned char*)&t, 1);
  427. } else if (se.target == XA_STRING) {
  428. if (!selectionRequest(se.requestor, se.selection, se.property))
  429. se.property = None;
  430. } else {
  431. se.property = None;
  432. }
  433. }
  434. XSendEvent(dpy, se.requestor, False, 0, (XEvent*)&se);
  435. break;
  436. }
  437. case SelectionClear:
  438. selectionOwner_[ev->xselectionclear.selection] = false;
  439. break;
  440. }
  441. if (eventHandler) eventHandler->handleEvent(this, ev);
  442. }
  443. void TXWindow::drawBevel(GC gc, int x, int y, int w, int h, int b,
  444. unsigned long middle, unsigned long tl,
  445. unsigned long br, bool round)
  446. {
  447. if (round) {
  448. XGCValues gcv;
  449. gcv.line_width = b;
  450. XChangeGC(dpy, gc, GCLineWidth, &gcv);
  451. XSetForeground(dpy, gc, middle);
  452. XFillArc(dpy, win(), gc, x, y, w-b/2, h-b/2, 0, 360*64);
  453. XSetForeground(dpy, gc, tl);
  454. XDrawArc(dpy, win(), gc, x, y, w-b/2, h-b/2, 45*64, 180*64);
  455. XSetForeground(dpy, gc, br);
  456. XDrawArc(dpy, win(), gc, x, y, w-b/2, h-b/2, 225*64, 180*64);
  457. } else {
  458. XSetForeground(dpy, gc, middle);
  459. if (w-2*b > 0 && h-2*b > 0)
  460. XFillRectangle(dpy, win(), gc, x+b, y+b, w-2*b, h-2*b);
  461. XSetForeground(dpy, gc, tl);
  462. XFillRectangle(dpy, win(), gc, x, y, w, b);
  463. XFillRectangle(dpy, win(), gc, x, y, b, h);
  464. XSetForeground(dpy, gc, br);
  465. for (int i = 0; i < b; i++) {
  466. if (w-i > 0) XFillRectangle(dpy, win(), gc, x+i, y+h-1-i, w-i, 1);
  467. if (h-1-i > 0) XFillRectangle(dpy, win(), gc, x+w-1-i, y+i+1, 1, h-1-i);
  468. }
  469. }
  470. }