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.h 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.h
  20. //
  21. // A TXWindow is the base class for all tx windows (widgets). In addition it
  22. // contains a number of static methods and members which are used throughout
  23. // tx.
  24. //
  25. // Before calling any other tx methods, TXWindow::init() must be called with
  26. // the X display to use.
  27. #ifndef __TXWINDOW_H__
  28. #define __TXWINDOW_H__
  29. #include <rdr/types.h>
  30. #include <X11/X.h>
  31. #include <X11/Xlib.h>
  32. #include <X11/Xutil.h>
  33. #include <map>
  34. // TXDeleteWindowCallback's deleteWindow() method is called when a top-level
  35. // window is "deleted" (closed) by the user using the window manager.
  36. class TXWindow;
  37. class TXDeleteWindowCallback {
  38. public:
  39. virtual void deleteWindow(TXWindow* w) = 0;
  40. };
  41. // TXEventHandler is an interface implemented by classes wanting to handle X
  42. // events on a window. Most derived classes of window are their own event
  43. // handlers.
  44. class TXEventHandler {
  45. public:
  46. virtual void handleEvent(TXWindow* w, XEvent* ev) = 0;
  47. };
  48. // TXGlobalEventHandler is similar to TXEventHandler but will be called early and
  49. // for every X event. The handler should return true to indicate that the
  50. // event was swallowed and shouldn't be processed further.
  51. class TXGlobalEventHandler {
  52. public:
  53. virtual bool handleGlobalEvent(XEvent* ev) = 0;
  54. };
  55. class TXWindow {
  56. public:
  57. // Constructor - creates a window of the given size, with the default
  58. // background (currently grey). It is mapped by default if it has a parent.
  59. // If no parent is specified its parent is the root window and it will remain
  60. // unmapped.
  61. TXWindow(Display* dpy_, int width=1, int height=1, TXWindow* parent_=0,
  62. int borderWidth=0);
  63. virtual ~TXWindow();
  64. // toplevel() declares that this is a top-level window. Various
  65. // window-manager-related properties are set on the window. The given
  66. // TXDeleteWindowCallback is notified when the window is "deleted" (cloesd)
  67. // by the user.
  68. void toplevel(const char* name, TXDeleteWindowCallback* dwc=0,
  69. int argc=0, char** argv=0, const char* windowClass=0,
  70. bool iconic=false);
  71. // setMaxSize() tells the window manager the maximum size to allow a
  72. // top-level window. It has no effect on a non-top-level window.
  73. void setMaxSize(int w, int h);
  74. // setUSPosition() tells the window manager the position which the "user" has
  75. // asked for a top-level window. Most window managers ignore requests by a
  76. // program for position, so you have to tell it that the "user" asked for the
  77. // position. This has no effect on a non-top-level window.
  78. void setUSPosition(int x, int y);
  79. void setGeometry(const char* geom, int x, int y, int w, int h);
  80. void setName(const char* name);
  81. // setTransientFor() tells the window manager that this window is "owned" by
  82. // the given window. The window manager can use this information as it sees
  83. // fit.
  84. void setTransientFor(Window w) { XSetTransientForHint(dpy, win(), w); }
  85. // setEventHandler() sets the TXEventHandler to handle X events for this
  86. // window. It returns the previous event handler, so that handlers can chain
  87. // themselves.
  88. TXEventHandler* setEventHandler(TXEventHandler* h);
  89. // Accessor methods
  90. Window win() { return win_; }
  91. int width() { return width_; }
  92. int height() { return height_; }
  93. // selectionOwner() returns true if this window owns the given selection.
  94. bool selectionOwner(Atom selection) { return selectionOwner_[selection]; }
  95. // Wrappers around common Xlib calls
  96. void addEventMask(long mask);
  97. void removeEventMask(long mask);
  98. void map() { XMapWindow(dpy, win()); }
  99. void unmap();
  100. void setBg(unsigned long bg) { XSetWindowBackground(dpy, win(), bg); }
  101. void move(int x, int y) { XMoveWindow(dpy, win(), x, y); }
  102. void resize(int w, int h);
  103. void raise() { XRaiseWindow(dpy, win()); }
  104. void setBorderWidth(int bw);
  105. void invalidate(int x=0, int y=0, int w=0, int h=0) { XClearArea(dpy, win(), x, y, w, h, True); }
  106. // ownSelection requests that the window owns the given selection from the
  107. // given time (the time should be taken from an X event).
  108. void ownSelection(Atom selection, Time time);
  109. // drawBevel draws a rectangular or circular bevel filling the given
  110. // rectangle, using the given colours for the middle, the top/left and the
  111. // bottom/right.
  112. void drawBevel(GC gc, int x, int y, int w, int h, int b,
  113. unsigned long middle, unsigned long tl, unsigned long br,
  114. bool round=false);
  115. // Methods to be overridden in a derived class
  116. // resizeNotify() is called whenever the window's dimensions may have
  117. // changed.
  118. virtual void resizeNotify() {}
  119. // takeFocus() is called when the window has received keyboard focus from the
  120. // window manager.
  121. virtual void takeFocus(Time /*time*/) {}
  122. // selectionNotify() is called when the selection owner has replied to a
  123. // request for information about a selection from the selection owner.
  124. virtual void selectionNotify(XSelectionEvent* /*ev*/, Atom /*type*/,
  125. int /*format*/, int /*nitems*/,
  126. void* /*data*/) {}
  127. // selectionRequest() is called when this window is the selection owner and
  128. // another X client has requested the selection. It should set the given
  129. // property on the given window to the value of the given selection,
  130. // returning true if successful, false otherwise.
  131. virtual bool selectionRequest(Window /*requestor*/,
  132. Atom /*selection*/,
  133. Atom /*property*/) { return false;}
  134. // Static methods
  135. // init() must be called before any other tx methods.
  136. static void init(Display* dpy, const char* defaultWindowClass);
  137. // getColours() sets the pixel values in the cols array to the best available
  138. // for the given rgb values, even in the case of a full colormap.
  139. static void getColours(Display* dpy, XColor* cols, int nCols);
  140. // handleXEvents() should be called whenever there are events to handle on
  141. // the connection to the X display. It process all available events, then
  142. // returns when there are no more events to process.
  143. static void handleXEvents(Display* dpy);
  144. // setGlobalEventHandler() sets the TXGlobalEventHandler to intercept all
  145. // X events. It returns the previous events handler, so that handlers can
  146. // chain themselves.
  147. static TXGlobalEventHandler* setGlobalEventHandler(TXGlobalEventHandler* h);
  148. // windowWithName() locates a window with a given name on a display.
  149. static Window windowWithName(Display* dpy, Window top, const char* name);
  150. // strEmptyToNull() returns the string it's given but turns an empty string
  151. // into null, which can be useful for passing rfb parameters to Xlib calls.
  152. static char* strEmptyToNull(char* s) { return s && s[0] ? s : 0; }
  153. // The following are default values for various things.
  154. static unsigned long black, white;
  155. static unsigned long defaultFg, defaultBg, lightBg, darkBg;
  156. static unsigned long disabledFg, disabledBg, enabledBg;
  157. static unsigned long scrollbarBg;
  158. static GC defaultGC;
  159. static Colormap cmap;
  160. static Font defaultFont;
  161. static XFontStruct* defaultFS;
  162. static Time cutBufferTime;
  163. static Pixmap dot, tick;
  164. static const int dotSize, tickSize;
  165. static char* defaultWindowClass;
  166. Display* const dpy;
  167. int xPad, yPad, bevel;
  168. private:
  169. // handleXEvent() is called from handleXEvents() when an event for this
  170. // window arrives. It does general event processing before calling on to the
  171. // event handler.
  172. void handleXEvent(XEvent* ev);
  173. TXWindow* parent;
  174. Window win_;
  175. int width_, height_;
  176. TXEventHandler* eventHandler;
  177. TXDeleteWindowCallback* dwc;
  178. long eventMask;
  179. XSizeHints sizeHints;
  180. std::map<Atom,Time> selectionOwnTime;
  181. std::map<Atom,bool> selectionOwner_;
  182. bool toplevel_;
  183. static TXGlobalEventHandler* globalEventHandler;
  184. };
  185. extern Atom wmProtocols, wmDeleteWindow, wmTakeFocus;
  186. extern Atom xaTIMESTAMP, xaTARGETS, xaSELECTION_TIME, xaSELECTION_STRING;
  187. extern Atom xaCLIPBOARD;
  188. #endif