changeset 5:008e28de2870

Saving work
author Eris Caffee <discordia@eldalin.com>
date Mon, 16 Apr 2012 10:32:59 -0500
parents c6dce2064bfb
children 1b73e60d53b4
files include/App.h src/App.cpp src/main.cpp
diffstat 3 files changed, 100 insertions(+), 19 deletions(-) [+]
line diff
     1.1 --- a/include/App.h	Sun Apr 15 01:52:09 2012 -0500
     1.2 +++ b/include/App.h	Mon Apr 16 10:32:59 2012 -0500
     1.3 @@ -31,13 +31,22 @@
     1.4        };
     1.5  
     1.6     /////////////////////////////////////
     1.7 -   typedef struct
     1.8 +   // TODO: Need to be able to support child windows.
     1.9 +   class WindowData
    1.10        {
    1.11        public:
    1.12 +      WindowData(const std::string & n, Window w, App::BitMap * i) :
    1.13 +	 name(n), win(w), icon(i) {};
    1.14 +      ~WindowData()
    1.15 +	 {
    1.16 +	 if (icon) { delete icon; icon = NULL; }
    1.17 +	 }
    1.18 +
    1.19 +
    1.20        std::string name;
    1.21        Window win;
    1.22        App::BitMap * icon;
    1.23 -      } WindowData;
    1.24 +      };
    1.25  
    1.26     /////////////////////////////////////
    1.27  
    1.28 @@ -60,10 +69,14 @@
    1.29     inline int display_width(void) const
    1.30        { return DisplayWidth(dpy, DefaultScreen(dpy)); };
    1.31  
    1.32 +   void run(void);
    1.33 +
    1.34     private:
    1.35     App();
    1.36     App(const App & a);
    1.37  
    1.38     Display * dpy;
    1.39     WindowMap windows;
    1.40 +   Atom wm_delete_window;  // Need this in contructor and run().
    1.41 +
    1.42     };
     2.1 --- a/src/App.cpp	Sun Apr 15 01:52:09 2012 -0500
     2.2 +++ b/src/App.cpp	Mon Apr 16 10:32:59 2012 -0500
     2.3 @@ -32,11 +32,18 @@
     2.4     // Create the main window
     2.5     create_window("main", 
     2.6  		 0, 0 , 
     2.7 -		 this->display_width()/3, this->display_height()/4, 
     2.8 +		 this->display_width()/2, this->display_height()/2, 
     2.9  		 icon);
    2.10  
    2.11 -   // Even though we are not passing any actual size_hints, the Xlib programming
    2.12 -   // manual says to pass the flags below anyway.
    2.13 +   // Get a reference to our WindowData structure.  We need the Window handle from it for a few things.
    2.14 +   App::WindowData * wd = windows["main"];
    2.15 +
    2.16 +
    2.17 +
    2.18 +
    2.19 +   // We need to set the window manager hints, size hints, window name, and icon name
    2.20 +
    2.21 +   // Even though we are not passing any actual size_hints, the Xlib programming manual says to pass the flags below anyway.
    2.22     XSizeHints *size_hints;
    2.23     if (!(size_hints = XAllocSizeHints())) 
    2.24        {
    2.25 @@ -45,6 +52,7 @@
    2.26        }
    2.27     size_hints->flags = PPosition | PSize;
    2.28  
    2.29 +   // Get X Properties for the window and icon names.
    2.30     XTextProperty windowName, iconName;
    2.31     const char * ptr = appname.c_str();
    2.32     if (XStringListToTextProperty(const_cast<char **>(&ptr), 1, &windowName) == 0) 
    2.33 @@ -70,18 +78,51 @@
    2.34     wm_hints->icon_pixmap = icon->pixmap;
    2.35     wm_hints->flags = StateHint | IconPixmapHint | InputHint;
    2.36  
    2.37 -   App::WindowData * wd = windows["main"];
    2.38 -    XSetWMProperties(dpy, wd->win, &windowName, &iconName,
    2.39 -   		    NULL, 0, size_hints, wm_hints,
    2.40 -   		    NULL);
    2.41 +   XSetWMProperties(dpy, wd->win, &windowName, &iconName,
    2.42 +    		    NULL, 0, size_hints, wm_hints,
    2.43 +		    NULL);
    2.44 +
    2.45 +
    2.46 +
    2.47 +   // Select the event types we want to receive.
    2.48 +   //Other interesting events include KeyReleaseMask and ButtonReleaseMask
    2.49 +   XSelectInput(dpy, wd->win, ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask);
    2.50 +
    2.51 +
    2.52 +
    2.53 +   // Make sure we get delete events from the window manager.
    2.54 +   // "wm_delete_window" is the Atom which corresponds to the delete
    2.55 +   //  window message sent by the window manager. 
    2.56 +
    2.57 +   wm_delete_window = XInternAtom (dpy, "WM_DELETE_WINDOW", False);
    2.58 +   XSetWMProtocols(dpy, wd->win, &wm_delete_window, 1);
    2.59 +
    2.60 +
    2.61 +   // This give BadMatch and I don't know why.
    2.62 +   // XSetWindowAttributes setwinattr;
    2.63 +   // setwinattr.background_pixmap = bg_pixmap->pixmap;
    2.64 +   // XChangeWindowAttributes(dpy, wd->win, CWBackPixmap, &setwinattr);
    2.65 +
    2.66 +   // Also BadMatch
    2.67 +   // XSetWindowBackgroundPixmap(dpy, wd->win, icon->pixmap);
    2.68 +
    2.69 +
    2.70 +   //   XSetWindowBackgroundPixmap(dpy, wd->win, None);
    2.71 +
    2.72 +
    2.73 +   // Map the window.  Remember: this does not display the window immediately.
    2.74 +   // The request is queued until events are read, or we call XFlush or XSync.
    2.75 +   XMapWindow(dpy, wd->win);
    2.76     }
    2.77  
    2.78  ////////////////////////////////////////////////////////////////////////////////
    2.79  App::~App()
    2.80     {
    2.81 +   // Ugh.  I really wish I could figure out why I can't make a map of string 
    2.82 +   // to WindowData instead of having ton use WindowData *
    2.83 +   // I'd like to eliminate this next loop.
    2.84     for( WindowMap::iterator i = windows.begin(); i != windows.end(); ++i)
    2.85        {
    2.86 -      delete (*i).second->icon;
    2.87        App::WindowData * wp = (*i).second;
    2.88        windows.erase(i);
    2.89        delete wp;
    2.90 @@ -126,20 +167,48 @@
    2.91  			     x, y, 
    2.92  			     width, height, 
    2.93  			     border_width, 
    2.94 -			     BlackPixel(dpy, screen_num), 
    2.95 -			     WhitePixel(dpy, screen_num));
    2.96 +			     None, None);
    2.97 +   //			     BlackPixel(dpy, screen_num),
    2.98 +   //			     WhitePixel(dpy, screen_num));
    2.99  
   2.100     icon->make_pixmap(dpy, win);
   2.101  
   2.102     // Add it to the window list
   2.103 -   App::WindowData * win_data = new App::WindowData;
   2.104 -   win_data->name = win_name;
   2.105 -   win_data->win = win;
   2.106 -   win_data->icon = icon;
   2.107 +   App::WindowData * win_data = new App::WindowData(win_name, win, icon);
   2.108     windows.insert(win_pair(win_name, win_data));
   2.109     }
   2.110  
   2.111  ////////////////////////////////////////////////////////////////////////////////
   2.112 +void App::run(void)
   2.113 +   {
   2.114 +   XEvent e;
   2.115 +   int done = 0;
   2.116 +   while (!done)
   2.117 +      {
   2.118 +      XNextEvent(dpy, &e);
   2.119 +      switch (e.type)
   2.120 +	 {
   2.121 +	 case Expose:
   2.122 +	    // We'll only redraw the entire window at a time, so unless this is 
   2.123 +	    // the last contiguous expose, don't draw the window.
   2.124 +	    if (e.xexpose.count != 0)
   2.125 +	       break;
   2.126 +	    // Draw the window contents here
   2.127 +	    break;
   2.128 +	 case ClientMessage:
   2.129 +	    if ((Atom)e.xclient.data.l[0] == wm_delete_window)
   2.130 +	       done = 1;
   2.131 +	    break;
   2.132 +	 case ButtonPress:
   2.133 +	 case KeyPress:
   2.134 +	    done = 1;
   2.135 +	 default:
   2.136 +	    break;
   2.137 +	 }
   2.138 +      }
   2.139 +   }
   2.140 +
   2.141 +////////////////////////////////////////////////////////////////////////////////
   2.142  // App::BitMap
   2.143  ////////////////////////////////////////////////////////////////////////////////
   2.144  App::BitMap::BitMap(const std::string & file)
   2.145 @@ -150,7 +219,7 @@
   2.146  ////////////////////////////////////////////////////////////////////////////////
   2.147  App::BitMap::~BitMap() 
   2.148     {
   2.149 -   if (data) XFree(data);
   2.150 +   if (data) { XFree(data); data = NULL; }
   2.151     }
   2.152  
   2.153  ////////////////////////////////////////////////////////////////////////////////
     3.1 --- a/src/main.cpp	Sun Apr 15 01:52:09 2012 -0500
     3.2 +++ b/src/main.cpp	Mon Apr 16 10:32:59 2012 -0500
     3.3 @@ -7,7 +7,6 @@
     3.4  int main(void)
     3.5     {
     3.6     App * app = new App("xlib_App");
     3.7 -
     3.8 -   sleep(5);
     3.9 +   app->run();
    3.10     exit(0);
    3.11     }