00001 /*!@file GUI/XWindow.H Class definition for a simple window */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/GUI/XWindow.H $ 00035 // $Id: XWindow.H 9412 2008-03-10 23:10:15Z farhan $ 00036 // 00037 00038 #ifndef XWINDOW_H_DEFINED 00039 #define XWINDOW_H_DEFINED 00040 00041 #include "Util/Types.H" 00042 #include "Image/Dims.H" 00043 #include "Image/Point2D.H" 00044 #include "rutz/shared_ptr.h" 00045 00046 #include <X11/Xlib.h> 00047 #include <X11/extensions/XShm.h> 00048 00049 class Dims; 00050 template <class T> class Image; 00051 template <class T> class Layout; 00052 template <class T> class PixRGB; 00053 00054 //! A simple X-window to display images. 00055 /*! Use this class to create an window on your screen and display an 00056 image. It will use shared memory between us and the X server if 00057 supported by the X server and the display is local. Resizing is 00058 not allowed; so any image passed to drawImage() must have 00059 dimensions that match those specified at onstruction of the 00060 XWindow. */ 00061 class XWindow { 00062 public: 00063 //! Constructor: create, display and map the window. 00064 /*!@param dims Dimensions of usable image area. 00065 @param x Horizontal position hint for window placement; use -1 00066 for automatic placement. 00067 @param y Vertical position hint for window placement; use -1 00068 for automatic placement. 00069 @param title String that will appear in the window title. */ 00070 XWindow(const Dims& dims = Dims(320, 240), 00071 const int x = -1, const int y = -1, 00072 const char* title = "USC Neuromorphic Vision"); 00073 00074 //! Destructor 00075 virtual ~XWindow(); 00076 00077 //! Change the window's visibility (map or unmap it as needed) 00078 void setVisible(const bool v); 00079 00080 //! Check whether the window is currently mapped (i.e. visible on screen) 00081 bool isVisible(const bool v) const { return itsMapped; } 00082 00083 //! Set the position of an XWindow 00084 /*! @param x Horizontal position hint for window placement 00085 @param y Vertical position hint for window placement 00086 */ 00087 void setPosition(const int x, const int y); 00088 00089 //! Get the position of an XWindow 00090 /* Note: This may give you a bogus value if you used automatic window 00091 placement */ 00092 Point2D<int> getPosition(); 00093 00094 //! Draw an image into the XWindow 00095 /*! @param img the image to be drawn in the window 00096 00097 @param x if the x coordinate where the image should start in the 00098 window. 00099 00100 @param y if the y coordinate where the image should start in the 00101 window. 00102 00103 @param resizeWindow if true, then the window will be resized (if 00104 necessary) to precisely fit the dimensions of the image; if 00105 false (the default), then the dimension of the image must be 00106 equal to or less than the current dimensions of the window 00107 */ 00108 void drawImage(const Image< PixRGB<byte> >& img, 00109 const int x = 0, const int y = 0, 00110 const bool resizeWindow = false); 00111 00112 //! Draw an image into the XWindow 00113 /*! @param img the image to be drawn in the window 00114 00115 @param x if the x coordinate where the image should start in the 00116 window. 00117 00118 @param y if the y coordinate where the image should start in the 00119 window. 00120 00121 @param resizeWindow if true, then the window will be resized (if 00122 necessary) to precisely fit the dimensions of the image; if 00123 false (the default), then the dimension of the image must be 00124 equal to or less than the current dimensions of the window 00125 */ 00126 void drawImage(const Image<byte>& img, 00127 const int x = 0, const int y = 0, 00128 const bool resizeWindow = false); 00129 00130 //! Draw an image into the XWindow 00131 /*! @param img the image to be drawn in the window 00132 00133 @param x if the x coordinate where the image should start in the 00134 window. 00135 00136 @param y if the y coordinate where the image should start in the 00137 window. 00138 00139 @param normalize if true, inplaceNormalize() the image to 00140 [0,255]; if false, inplaceClamp() the image to [0,255] 00141 00142 @param resizeWindow if true, then the window will be resized (if 00143 necessary) to precisely fit the dimensions of the image; if 00144 false (the default), then the dimension of the image must be 00145 equal to or less than the current dimensions of the window 00146 */ 00147 void drawImage(const Image<float>& img, 00148 const int x = 0, const int y = 0, 00149 bool normalize = true, 00150 const bool resizeWindow = false); 00151 00152 //! Draw an image layout into the XWindow 00153 /*! This is equivalent to drawImage(layout.render()), but is 00154 implemented more efficiently by avoiding an intermediate 00155 copy rendered of the image. 00156 00157 @param resizeWindow if true, then the window will be resized (if 00158 necessary) to precisely fit the dimensions of the image; if 00159 false (the default), then the dimension of the image must be 00160 equal to or less than the current dimensions of the window 00161 */ 00162 void drawRgbLayout(const Layout<PixRGB<byte> >& layout, 00163 const int x = 0, const int y = 0, 00164 const bool resizeWindow = false); 00165 00166 //! Draw an image layout into the XWindow 00167 /*! This is equivalent to drawImage(layout.render()), but is 00168 implemented more efficiently by avoiding an intermediate 00169 copy rendered of the image. 00170 00171 @param resizeWindow if true, then the window will be resized (if 00172 necessary) to precisely fit the dimensions of the image; if 00173 false (the default), then the dimension of the image must be 00174 equal to or less than the current dimensions of the window 00175 */ 00176 void drawGrayLayout(const Layout<byte>& layout, 00177 const int x = 0, const int y = 0, 00178 const bool resizeWindow = false); 00179 00180 //! Get the window title 00181 const char* getTitle() const; 00182 00183 //! set the title bar to a new string 00184 void setTitle(const char* title); 00185 00186 //! return the dimensions of this window 00187 Dims getDims() const; 00188 00189 //! change the dimensions of this window 00190 void setDims(const Dims& dims); 00191 00192 /*! @name Wrappers around X11 Xlib calls 00193 00194 We wrap these calls, rather than exposing a Display* and Window 00195 directly, so that we can get the threading issues right; namely, 00196 we need to lock a global mutex each time any window wants to use 00197 the Display*, since multiple threads may be running and 00198 accessing the Display*. 00199 */ 00200 //@{ 00201 00202 //! Calls XSelectInput() 00203 void selectInput(long event_mask) const; 00204 00205 //! Calls XInternAtom() followed by XSetWMProtocols() 00206 Atom setWMProtocol(const char* atomname) const; 00207 00208 //! Calls XCheckWindowEvent() 00209 /*! Note that XCheckWindowEvent() is just like XCheckMaskEvent(), 00210 but restricted to a specific Window) */ 00211 Bool checkMaskEvent(long event_mask, XEvent* event) const; 00212 00213 //! Calls XCheckTypedWindowEvent() 00214 Bool checkTypedEvent(int event_type, XEvent* event) const; 00215 00216 //@} 00217 00218 protected: 00219 void redrawImage(); 00220 00221 private: 00222 XWindow(const XWindow&); // not implemented 00223 XWindow& operator=(const XWindow&); // not implemented 00224 00225 //! MUST BE CALLED with the display mutex already LOCKED! 00226 void setDimsImpl(const Dims& dims); 00227 00228 template <class T> 00229 void drawLayout(const Layout<T>& layout, 00230 const int x, const int y, 00231 const bool resizeWindow); 00232 00233 Dims itsWinDims; //<! window size 00234 Window itsWindow; //<! our window 00235 Visual* itsVisual; //<! pointer to current X visual 00236 int itsDepth; //<! bit depth of the window 00237 GC itsGc; //<! current graphic context 00238 std::string itsTitle; //!< the window title 00239 int itsAttemptShm;//!< whether to attempt to use shared memory buffers 00240 bool itsMapped; //!< whether the window is currently mapped 00241 00242 struct XWinImage; 00243 00244 rutz::shared_ptr<XWinImage> itsXimage; 00245 Point2D<int> itsImgPos; //!< position of the image in the window 00246 }; 00247 00248 // ###################################################################### 00249 /* So things look consistent in everyone's emacs... */ 00250 /* Local Variables: */ 00251 /* indent-tabs-mode: nil */ 00252 /* End: */ 00253 00254 #endif