Added KWinGrid
[kwingrid.git] / old / kwingrid.cc
1 #include <kapplication.h>
2 #include <dcopclient.h>
3 #include <kwin.h>
4 #include <Xlib.h>
5
6 #include "kwingrid.h"
7 #include "kwingrid.moc"
8
9 KWinGrid::KWinGrid(int hgap__, int vgap__, int hsplit__, int vsplit__)
10     : DCOPObject("grid"), hgap_(hgap__), vgap_(vgap__), hsplit_(hsplit__), vsplit_(vsplit__)
11 {
12     module_ = new KWinModule(KApplication::kApplication());
13 }
14
15 void KWinGrid::move(int __xslot, int __yslot)
16 {
17     moveResize(__xslot, __yslot, -1, -1);
18 }
19
20 void KWinGrid::resize(int __xsize, int __ysize)
21 {
22     moveResize(-1,-1,__xsize,__ysize);
23 }
24
25 void KWinGrid::toDesk(int __desk)
26 {
27     int w = module_->activeWindow();
28     if (w) 
29         KWin::setOnDesktop(w,__desk);
30 }
31
32 void KWinGrid::moveResize(int __xslot, int __yslot,
33                           int __xsize, int __ysize)
34 {
35     initGeometry();
36     if (activeWindow_) {
37         QRect newGeometry = doMoveResize(__xslot,__yslot,__xsize,__ysize);
38         updateGeometry(newGeometry);
39         applyGeometry();
40     }
41 }
42
43 void KWinGrid::moveRelative(int __xdiff, int __ydiff)
44 {
45     initGeometry();
46     if (activeWindow_) {
47         int xSlot = (outer_.left()-region_.left()+hsize_/2)/hsize_;
48         int ySlot = (outer_.top()-region_.top()+vsize_/2)/vsize_;
49         xSlot += __xdiff;
50         ySlot += __ydiff;
51         if (xSlot<0)
52             xSlot = 0;
53         else if (xSlot >= hsplit_)
54             xSlot = hsplit_-1;
55         if (ySlot<0)
56             ySlot = 0;
57         else if (ySlot >= vsplit_)
58             ySlot = vsplit_-1;
59         QRect newGeometry = doMoveResize(xSlot,ySlot,-1,-1);
60         updateGeometry(newGeometry);
61         applyGeometry();
62     }
63 }
64
65 void KWinGrid::resizeRelative(int __xdiff, int __ydiff)
66 {
67     initGeometry();
68     if (activeWindow_) {
69         int xSize = (outer_.width()+hsize_/2)/hsize_;
70         int ySize = (outer_.height()+vsize_/2)/vsize_;
71         xSize += __xdiff;
72         ySize += __ydiff;
73         if (xSize<1)
74             xSize = 1;
75         if (xSize>hsplit_)
76             xSize = hsplit_;
77         if (ySize<1)
78             ySize = 1;
79         if (ySize>vsplit_)
80             ySize = vsplit_;
81         QRect newGeometry = doMoveResize(-1,-1,xSize,ySize);
82         updateGeometry(newGeometry);
83         applyGeometry();
84     }
85 }
86
87 QRect KWinGrid::doMoveResize(int __xslot, int __yslot,
88                              int __xsize, int __ysize)
89 {
90     QRect newGeometry(outer_);
91         
92     if (__xsize != -1) 
93         newGeometry.setWidth(__xsize*hsize_-hgap_);
94     if (__ysize != -1)
95         newGeometry.setHeight(__ysize*vsize_-vgap_);
96     QSize size = newGeometry.size();
97     if (__xslot != -1) {
98         QPoint p(newGeometry.topLeft());
99         p.setX(region_.left() 
100                + __xslot*hsize_
101                + hgap_/2);
102         newGeometry.moveTopLeft(p);
103     }
104     if (__yslot != -1) {
105         QPoint p(newGeometry.topLeft());
106         p.setY(region_.top()
107                + __yslot*vsize_
108                + vgap_/2);
109         newGeometry.moveTopLeft(p);
110     }
111         
112     return newGeometry;
113 }
114
115 void KWinGrid::initGeometry()
116 {
117     activeWindow_ = module_->activeWindow();
118     if (activeWindow_) {
119         KWin::Info info(KWin::info(activeWindow_));
120         inner_ = info.geometry;
121         outer_ = info.frameGeometry;
122         region_ = module_->workArea();
123
124         hsize_ = (region_.width()-hgap_)/hsplit_;
125         vsize_ = (region_.height()-vgap_)/vsplit_;
126
127         int hdelta = region_.width()-hsize_*hsplit_;
128         int vdelta = region_.height()-vsize_*vsplit_;
129         QPoint topLeft(region_.topLeft());
130         topLeft+=QPoint(hdelta/2,vdelta/2);
131         region_.moveTopLeft(topLeft);
132         region_.setSize(QSize(hsize_*hsplit_,vsize_*vsplit_));
133     }
134 }
135
136 void KWinGrid::updateGeometry(QRect& __new)
137 {
138     QRect newInner(inner_);
139     newInner.moveTopLeft(QPoint(__new.top()+(inner_.top()-outer_.top()),
140                                 __new.left()+(inner_.left()-outer_.left())));
141     newInner.setSize(QSize(__new.width()-(outer_.width()-inner_.width()),
142                            __new.height()-(outer_.height()-inner_.height())));
143     inner_ = newInner;
144     outer_ = __new;
145 }
146
147 void KWinGrid::applyGeometry()
148 {
149     XMoveResizeWindow(KApplication::kApplication()->getDisplay(),activeWindow_,
150                       outer_.x(),outer_.y(),
151                       inner_.width(),inner_.height());
152     KWin::setActiveWindow(activeWindow_);
153 }
154
155 // slots
156
157 void KWinGrid::move_TL()
158 {
159     move(0,0);
160 }
161
162 void KWinGrid::move_TR()
163 {
164     move(hsplit_/2,0);
165 }
166
167 void KWinGrid::move_BL()
168 {
169     move(0,vsplit_/2);
170 }
171
172 void KWinGrid::move_BR()
173 {
174     move(hsplit_/2,vsplit_/2);
175 }
176
177     
178 void KWinGrid::resize_Q()
179 {
180     resize(vsplit_/2,hsplit_/2);
181 }
182
183 void KWinGrid::resize_H()
184 {
185     resize(vsplit_,hsplit_/2);
186 }
187
188 void KWinGrid::resize_V()
189 {
190     resize(vsplit_/2,hsplit_);
191 }
192
193 void KWinGrid::resize_F()
194 {
195     resize(vsplit_,hsplit_);
196 }
197
198
199 void KWinGrid::move_L()
200 {
201     moveRelative(-1,0);
202 }
203
204 void KWinGrid::move_R()
205 {
206     moveRelative(1,0);
207 }
208
209 void KWinGrid::move_U()
210 {
211     moveRelative(0,-1);
212 }
213
214 void KWinGrid::move_D()
215 {
216     moveRelative(0,1);
217 }
218
219     
220 void KWinGrid::resize_IH()
221 {
222     resizeRelative(1,0);
223 }
224
225 void KWinGrid::resize_DH()
226 {
227     resizeRelative(-1,0);
228 }
229
230 void KWinGrid::resize_IV()
231 {
232     resizeRelative(0,1);
233 }
234
235 void KWinGrid::resize_DV()
236 {
237     resizeRelative(0,-1);
238 }
239