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 7.9KB

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