1 #include <vector>
2 #include <string>
3 #include "wmwidget.h"
4 #include "xwrapper.h"
5 #include "colors.h"
6 
7 #ifndef _WMAPP_H
8 #define _WMAPP_H
9 
10 class WMWindow;
11 
12 namespace WindowManager {
13   enum WindowManager { WindowMaker, Afterstep, Other };
14 }
15 
16 struct WMMouseClick {
17   int button;
18   int x;
19   int y;
20 
21   WMMouseClick(int Button = Button1, int X = 0, int Y = 0);
22   WMMouseClick relative_to(const WMWidget &w) const;
23   WMMouseClick relative_to(const WMWidget *w) const;
24   WMMouseClick b_relative_to(const WMWidget &w) const;
25   WMMouseClick b_relative_to(const WMWidget *w) const;
26 };
27 
WMMouseClick(int Button,int X,int Y)28 inline WMMouseClick::WMMouseClick(int Button, int X, int Y)
29 : button(Button), x(X), y(Y) { }
30 
31 inline WMMouseClick
relative_to(const WMWidget & w)32 WMMouseClick::relative_to(const WMWidget &w) const
33 { return WMMouseClick(button, x - w.left(), y - w.top()); }
34 
35 inline WMMouseClick
relative_to(const WMWidget * w)36 WMMouseClick::relative_to(const WMWidget *w) const
37 { return relative_to(*w); }
38 
39 inline WMMouseClick
b_relative_to(const WMWidget & w)40 WMMouseClick::b_relative_to(const WMWidget &w) const
41 { return WMMouseClick(button, x - w.b_left(), y - w.b_top()); }
42 
43 inline WMMouseClick
b_relative_to(const WMWidget * w)44 WMMouseClick::b_relative_to(const WMWidget *w) const
45 { return b_relative_to(*w); }
46 
47 class WMApp {
48  private:
49   static int		wWindowSize;
50   static int		wArgc;
51   static char **	wArgv;
52   static std::string	wName;
53 
54   std::vector<WMWindow *>	wWindows;
55   mutable int		wActiveWindow;
56   mutable int		wNextWindow;
57   mutable bool		wFinished;
58   mutable WMMouseClick	wMouseEvent;
59   void *		wUserData;
60 
61   void create_window(X::Window *dest, int left, int top) const;
62   void Xsetup();
63   void Xwait() const;
64 
65  public:
66   static WindowManager::WindowManager wManager;
67   static Xwrapper Xw;
68   static WMPixmap char_pixmaps[5];
69   static WMPixmap checkbox_pixmap, xbutton_pixmap, leds_pixmap[4];
70   static WMPixmap emptybar_pixmap, fullbar_pixmap;
71   static WMPixmap tile_pixmap;
72   static Color colormap[WMColor::numcolors];
73 
74   // Always call this function first in main():
75   static void initialize(int argc = 0, char *argv[] = 0);
76   static unsigned int size();
77 
78   WMApp();
79   ~WMApp();
80 
81   void setuserdata(void *data);
82   void *getuserdata() const;
83 
84   void addwindow(WMWindow *);
85   void addwindow(WMWindow &);
86 
87   // Use this call to start the GUI.
88   void run(unsigned int window = 0);
89   void run(int window);
90   void run(WMWindow &);
91   void run(WMWindow *);
92 
93   // Use these within callbacks of windows or buttons in order to switch
94   // to a different window, or to exit the GUI.
95   void switch_to(unsigned int window) const;
96   void switch_to(int window) const;
97   void switch_to(WMWindow &) const;
98   void switch_to(WMWindow *) const;
99   void stop() const;
100 
101   // Use this within callbacks of windows or buttons in order to play
102   // with other widgets.
103   WMWindow * window(unsigned int) const;
104   WMWindow * current() const;
105   unsigned int currentnum() const;
106 
107   // Use this within callbacks to see which mouse button was pressed
108   // and where.
109   const WMMouseClick & mouseclick() const;
110 
111   // Windows use this call to see if it's time for them to exit yet.
112   bool done() const;
113 
114   // utility functions for windows
115   void mask() const;
116   void repaint() const;
117   void Xshow() const;
118 };
119 
120 inline
WMApp()121 WMApp::WMApp()
122 : wActiveWindow(0), wNextWindow(0), wFinished(false) { }
123 
124 inline
~WMApp()125 WMApp::~WMApp()
126 {
127   if (wProgramWin == wActiveWin)
128     X::XDestroyWindow(Xw.xdisplay(), wProgramWin);
129   else {
130     X::XDestroyWindow(Xw.xdisplay(), wProgramWin);
131     X::XDestroyWindow(Xw.xdisplay(), wActiveWin);
132   }
133 }
134 
135 inline void
setuserdata(void * data)136 WMApp::setuserdata(void *data) { wUserData = data; }
137 
138 inline void *
getuserdata()139 WMApp::getuserdata() const { return wUserData; }
140 
141 inline void
addwindow(WMWindow * w)142 WMApp::addwindow(WMWindow *w) { wWindows.push_back(w); }
143 
144 inline void
addwindow(WMWindow & w)145 WMApp::addwindow(WMWindow &w) { wWindows.push_back(&w); }
146 
147 inline unsigned int
size()148 WMApp::size() { return wWindowSize; }
149 
150 inline WMWindow *
window(unsigned int i)151 WMApp::window(unsigned int i) const
152 { return i < wWindows.size() ? wWindows[i] : 0; }
153 
154 inline WMWindow *
current()155 WMApp::current() const { return wWindows[wActiveWindow]; }
156 
157 inline unsigned int
currentnum()158 WMApp::currentnum() const { return wActiveWindow; }
159 
160 inline const WMMouseClick &
mouseclick()161 WMApp::mouseclick() const { return wMouseEvent; }
162 
163 inline void
switch_to(unsigned int window)164 WMApp::switch_to(unsigned int window) const
165 { if (window < wWindows.size()) wNextWindow = window; }
166 
167 inline void
switch_to(int window)168 WMApp::switch_to(int window) const
169 {
170   if (window >= 0 && window < static_cast<int>(wWindows.size()))
171     wNextWindow = window;
172 }
173 
174 inline void
stop()175 WMApp::stop() const { wFinished = true; }
176 
177 inline bool
done()178 WMApp::done() const { return wFinished || wActiveWindow != wNextWindow; }
179 
180 #endif
181