--- /dev/null
+TARGET=kwingrid
+
+MOCS=\
+kwingrid.moc
+
+OBJECTS=\
+main.o \
+kwingrid.o \
+kwingrid_iface_skel.o \
+kwingrid_iface_stub.o
+
+LIBS=-lkdecore -lkdeui -lDCOP -lqt-mt -lX11 -lXext
+LOPTS=-L/usr/X11R6/lib -g
+COPTS=-I. -I /usr/include/kde -I /usr/include/qt3 -I /usr/X11R6/include/X11 -O2 -Wall -g
+
+all: mocs bin
+
+clean:
+ rm -f $(OBJECTS) $(MOCS) *_skel.cpp *_stub.h *_stub.cpp $(TARGET)
+
+bin: $(TARGET)
+
+mocs: $(MOCS)
+
+%.moc: %.h
+ moc $< -o $@
+
+%.o: %.cc
+ g++ $(COPTS) -c $< -o $@
+
+%.o: %.cpp
+ g++ $(COPTS) -c $< -o $@
+
+%.idl: %.h
+ dcopidl $< > $@
+
+%_skel.cpp %_stub.h %_stub.cpp: %.idl
+ dcopidl2cpp $<
+
+$(TARGET): $(OBJECTS)
+ g++ $(LOPTS) -o $@ $(OBJECTS) $(LIBS)
+
+
--- /dev/null
+#include <iostream>
+
+#include <kapplication.h>
+#include <dcopclient.h>
+#include <kwin.h>
+#include <Xlib.h>
+#include <Xutil.h>
+#include <netwm_def.h>
+
+#include "kwingrid.h"
+#include "kwingrid.moc"
+
+KWinGrid::KWinGrid(int hgap__, int vgap__, int hsplit__, int vsplit__, int split__)
+ : DCOPObject("grid"), split_(split__),
+ hgap_(hgap__), vgap_(vgap__), hsplit_(hsplit__), vsplit_(vsplit__)
+{
+ module_ = new KWinModule(KApplication::kApplication());
+ connect(module_,SIGNAL(activeWindowChanged(WId)),this,SLOT(activeWindowChanged(WId)));
+}
+
+void KWinGrid::move(int __xslot, int __yslot)
+{
+ moveResize(__xslot, __yslot, -1, -1);
+}
+
+void KWinGrid::resize(int __xsize, int __ysize)
+{
+ moveResize(-1,-1,__xsize,__ysize);
+}
+
+void KWinGrid::toDesk(int __desk)
+{
+ int w = activeWindow();
+ if (w)
+ KWin::setOnDesktop(w,__desk);
+}
+
+void KWinGrid::quit()
+{
+ KApplication::kApplication()->quit();
+}
+
+int KWinGrid::activeWindow()
+{
+ int av = module_->activeWindow();
+ KWin::WindowInfo info = KWin::windowInfo(av,NET::WMWindowType);
+ if (info.windowType(NET::AllTypesMask) == NET::Desktop) return 0;
+ return av;
+}
+
+void KWinGrid::updateTimestamp(void)
+{
+ timestamp_ = QDateTime::currentDateTime();
+}
+
+void KWinGrid::activeWindowChanged(WId id)
+{
+ if (!activeWindow_ || timestamp_.isNull())
+ return;
+
+ QDateTime tm = QDateTime::currentDateTime();
+
+ int deltaDays = timestamp_.date().daysTo(tm.date());
+ int deltaMSecs = timestamp_.time().msecsTo(tm.time());
+
+ if (deltaDays>2 || deltaDays<0) {
+ activeWindow_ = 0;
+ return;
+ }
+
+ deltaMSecs += deltaDays * 1000*(60*60*24);
+
+ if (deltaMSecs <= 300 && deltaMSecs > 0)
+ KWin::forceActiveWindow(activeWindow_);
+ else
+ activeWindow_ = 0;
+}
+
+void KWinGrid::moveResize(int __xslot, int __yslot,
+ int __xsize, int __ysize)
+{
+ initGeometry();
+ if (activeWindow_) {
+ QRect newGeometry = doMoveResize(__xslot,__yslot,__xsize,__ysize);
+ updateGeometry(newGeometry);
+ applyGeometry();
+ }
+}
+
+void KWinGrid::moveRelative(int __xdiff, int __ydiff)
+{
+ initGeometry();
+ if (activeWindow_) {
+ int xSlot = (outer_.left()-region_.left()+hsize_/2)/hsize_;
+ int ySlot = (outer_.top()-region_.top()+vsize_/2)/vsize_;
+ xSlot += __xdiff;
+ ySlot += __ydiff;
+ if (xSlot<0) {
+ if (numScreens_ > 1 and screen_ > 0) {
+ initGeometry(screen_-1);
+ xSlot = hsplit_-1;
+ ySlot = (outer_.top()-region_.top()+vsize_/2)/vsize_ + __ydiff;
+ } else
+ xSlot = 0;
+ } else if (xSlot >= hsplit_) {
+ if (numScreens_ > 1 and screen_ < numScreens_-1) {
+ initGeometry(screen_+1);
+ xSlot = 0;
+ ySlot = (outer_.top()-region_.top()+vsize_/2)/vsize_ + __ydiff;
+ } else
+ xSlot = hsplit_-1;
+ }
+ if (ySlot<0)
+ ySlot = 0;
+ else if (ySlot >= vsplit_)
+ ySlot = vsplit_-1;
+ QRect newGeometry = doMoveResize(xSlot,ySlot,-1,-1);
+ updateGeometry(newGeometry);
+ applyGeometry();
+ }
+}
+
+void KWinGrid::resizeRelative(int __xdiff, int __ydiff)
+{
+ initGeometry();
+ if (activeWindow_) {
+ int xSize = (outer_.width()+hsize_/2)/hsize_;
+ int ySize = (outer_.height()+vsize_/2)/vsize_;
+ xSize += __xdiff;
+ ySize += __ydiff;
+ if (xSize<1)
+ xSize = 1;
+ if (xSize>hsplit_)
+ xSize = hsplit_;
+ if (ySize<1)
+ ySize = 1;
+ if (ySize>vsplit_)
+ ySize = vsplit_;
+ QRect newGeometry = doMoveResize(-1,-1,xSize,ySize);
+ updateGeometry(newGeometry);
+ applyGeometry();
+ }
+}
+
+QRect KWinGrid::doMoveResize(int __xslot, int __yslot,
+ int __xsize, int __ysize)
+{
+ QRect newGeometry(outer_);
+
+ if (__xsize == -1) {
+ __xsize = (outer_.width()+hsize_/2)/hsize_;
+ if (__xsize<1) __xsize = 1;
+ if (__xsize>hsplit_) __xsize = hsplit_;
+ }
+ if (__ysize == -1) {
+ __ysize = (outer_.height()+vsize_/2)/vsize_;
+ if (__ysize<1) __ysize = 1;
+ if (__ysize>vsplit_) __ysize = vsplit_;
+ }
+
+ newGeometry.setWidth(__xsize*hsize_-hgap_);
+ newGeometry.setHeight(__ysize*vsize_-vgap_);
+
+ if (__xslot == -1) {
+ __xslot = (outer_.left()-region_.left()+hsize_/2)/hsize_;
+ if (__xslot<0) __xslot = 0;
+ if (__xslot>=hsplit_) __xslot = hsplit_-1;
+ }
+ if (__yslot == -1) {
+ __yslot = (outer_.top()-region_.top()+vsize_/2)/vsize_;
+ if (__yslot<0) __yslot = 0;
+ if (__yslot>=vsplit_) __yslot = vsplit_-1;
+ }
+ newGeometry.moveTopLeft(QPoint(region_.left() + __xslot*hsize_ + hgap_/2,
+ region_.top() + __yslot*vsize_ + vgap_/2));
+ return newGeometry;
+}
+
+std::ostream& operator<<(std::ostream& os, QRect rect)
+{
+ os << '(' << rect.width() << 'x' << rect.height() << '+' << rect.x() << '+' << rect.y() << ')';
+ return os;
+}
+
+std::ostream& operator<<(std::ostream& os, QPoint p)
+{
+ os << '(' << p.x() << ',' << p.y() << ')';
+ return os;
+}
+
+std::ostream& operator<<(std::ostream& os, QSize s)
+{
+ os << '(' << s.width() << 'x' << s.height() << ')';
+ return os;
+}
+
+void KWinGrid::initGeometry(int __forceScreen)
+{
+ activeWindow_ = activeWindow();
+ if (activeWindow_) {
+ KWin::WindowInfo info(KWin::windowInfo(activeWindow_));
+ inner_ = info.geometry();
+ outer_ = info.frameGeometry();
+ orig_ = outer_;
+ if (split_) {
+ if (__forceScreen == -1)
+ screen_ = outer_.left()>=split_ ? 1 : 0;
+ else
+ screen_ = __forceScreen;
+ region_ = QApplication::desktop()->screenGeometry();
+ if (screen_ == 1)
+ region_.setLeft(split_);
+ else
+ region_.setRight(split_-1);
+ numScreens_ = 2;
+ } else {
+ if (__forceScreen == -1)
+ screen_ = QApplication::desktop()->screenNumber(outer_.topLeft());
+ else
+ screen_ = __forceScreen;
+ region_ = QApplication::desktop()->screenGeometry(screen_);
+ numScreens_ = QApplication::desktop()->numScreens();
+ }
+ QRect wa = module_->workArea();
+ region_ = region_ & wa;
+
+ hsize_ = (region_.width()-hgap_)/hsplit_;
+ vsize_ = (region_.height()-vgap_)/vsplit_;
+
+ int hdelta = region_.width()-hsize_*hsplit_;
+ int vdelta = region_.height()-vsize_*vsplit_;
+ QPoint topLeft(region_.topLeft());
+ topLeft+=QPoint(hdelta/2,vdelta/2);
+ region_.moveTopLeft(topLeft);
+ region_.setSize(QSize(hsize_*hsplit_,vsize_*vsplit_));
+ }
+}
+
+void KWinGrid::updateGeometry(QRect& __new)
+{
+ QRect newInner(inner_);
+ newInner.moveTopLeft(QPoint(__new.left()+(inner_.left()-outer_.left()),
+ __new.top()+(inner_.top()-outer_.top())));
+ newInner.setSize(QSize(__new.width()-(outer_.width()-inner_.width()),
+ __new.height()-(outer_.height()-inner_.height())));
+ inner_ = newInner;
+ outer_ = __new;
+
+ XSizeHints hints;
+ long supplied;
+ if (XGetWMNormalHints(KApplication::kApplication()->getDisplay(),
+ activeWindow_, &hints, &supplied)) {
+ hints.flags &= supplied;
+ if (hints.flags & PResizeInc && hints.width_inc != 0 && hints.height_inc != 0) {
+ QSize base(0,0);
+ if (hints.flags & PBaseSize) {
+ base.setWidth(hints.base_width);
+ base.setHeight(hints.base_height);
+ } else if (hints.flags & PMinSize) {
+ base.setWidth(hints.min_width);
+ base.setHeight(hints.min_height);
+ }
+ QSize newSize(((inner_.width()-base.width())/hints.width_inc)*hints.width_inc + base.width(),
+ ((inner_.height()-base.height())/hints.height_inc)*hints.height_inc + base.height());
+ QSize delta(inner_.size() - newSize);
+ QPoint offset(delta.width()/2,delta.height()/2);
+ inner_.setSize(newSize);
+ outer_.setSize(outer_.size() - delta);
+ inner_.moveTopLeft(inner_.topLeft() + offset);
+ outer_.moveTopLeft(outer_.topLeft() + offset);
+ }
+ }
+}
+
+void KWinGrid::applyGeometry()
+{
+ updateTimestamp();
+ if (orig_.topLeft() == outer_.topLeft())
+ // If the position of the window did not change,
+ // XMoveResizeWindow sometimes still moves the window a little
+ // bit. Seems to have something todo with window gravity
+ // ... we just leave the position allone in that case.
+ XResizeWindow(KApplication::kApplication()->getDisplay(),activeWindow_,
+ inner_.width(),inner_.height());
+ else {
+ // I don't really know, whats all this stuff concerning window
+ // gravity. I only know, this works for my openoffice windows,
+ // which have StaticGravity. I have not found any window with
+ // a window_gravity of neither StaticGravity nor
+ // NorthWestGravity on my desktop so did not check other
+ // window gravities.
+ QPoint pos = outer_.topLeft();
+ XWindowAttributes winAttributes;
+ if (XGetWindowAttributes(KApplication::kApplication()->getDisplay(),activeWindow_,
+ &winAttributes) && winAttributes.win_gravity == StaticGravity)
+ pos = inner_.topLeft();
+ XMoveResizeWindow(KApplication::kApplication()->getDisplay(),activeWindow_,
+ pos.x(),pos.y(), inner_.width(),inner_.height());
+ }
+ //XSync(KApplication::kApplication()->getDisplay(),False);
+ //KWin::forceActiveWindow(activeWindow_);
+}
+
+// slots
+
+void KWinGrid::move_TL()
+{
+ move(0,0);
+}
+
+void KWinGrid::move_TR()
+{
+ move(hsplit_/2,0);
+}
+
+void KWinGrid::move_BL()
+{
+ move(0,vsplit_/2);
+}
+
+void KWinGrid::move_BR()
+{
+ move(hsplit_/2,vsplit_/2);
+}
+
+
+void KWinGrid::resize_Q()
+{
+ resize(vsplit_/2,hsplit_/2);
+}
+
+void KWinGrid::resize_H()
+{
+ resize(vsplit_,hsplit_/2);
+}
+
+void KWinGrid::resize_V()
+{
+ resize(vsplit_/2,hsplit_);
+}
+
+void KWinGrid::resize_F()
+{
+ resize(vsplit_,hsplit_);
+}
+
+
+void KWinGrid::move_L()
+{
+ moveRelative(-1,0);
+}
+
+void KWinGrid::move_R()
+{
+ moveRelative(1,0);
+}
+
+void KWinGrid::move_U()
+{
+ moveRelative(0,-1);
+}
+
+void KWinGrid::move_D()
+{
+ moveRelative(0,1);
+}
+
+
+void KWinGrid::resize_IH()
+{
+ resizeRelative(1,0);
+}
+
+void KWinGrid::resize_DH()
+{
+ resizeRelative(-1,0);
+}
+
+void KWinGrid::resize_IV()
+{
+ resizeRelative(0,1);
+}
+
+void KWinGrid::resize_DV()
+{
+ resizeRelative(0,-1);
+}
+
--- /dev/null
+// -*- c++ -*-
+
+#ifndef __kwingrid_h
+#define __kwingrid_h 1
+
+#include <qobject.h>
+#include <qdatetime.h>
+#include <kwinmodule.h>
+#include <kwingrid_iface.h>
+
+class KWinGrid
+ : public QObject, public virtual KWinGrid_Iface
+{
+ Q_OBJECT
+
+public:
+ KWinGrid(int hgap__, int vgap__, int hsplit__, int vsplit__, int split__=0);
+
+ virtual void move(int __xslot, int __yslot);
+ virtual void resize(int __xsize, int __ysize);
+ virtual void moveResize(int __xslot, int __yslot,
+ int __xsize, int __ysize);
+ virtual void moveRelative(int __xdiff, int __ydiff);
+ virtual void resizeRelative(int __xdiff, int __ydiff);
+ virtual void toDesk(int __desk);
+
+ virtual void quit();
+
+ virtual int activeWindow();
+
+public slots:
+ void move_TL();
+ void move_TR();
+ void move_BL();
+ void move_BR();
+
+ void resize_Q();
+ void resize_H();
+ void resize_V();
+ void resize_F();
+
+ void move_L();
+ void move_R();
+ void move_U();
+ void move_D();
+
+ void resize_IH();
+ void resize_DH();
+ void resize_IV();
+ void resize_DV();
+
+public slots:
+ void activeWindowChanged(WId id);
+
+private:
+ void initGeometry(int __forceScreens=-1);
+ void updateGeometry(QRect& __new);
+ void applyGeometry();
+ QRect doMoveResize(int __xslot, int __yslot,
+ int __xsize, int __ysize);
+
+ void updateTimestamp(void);
+
+ KWinModule* module_;
+ DCOPClient* client_;
+
+ int split_;
+
+ int activeWindow_;
+ QRect inner_;
+ QRect outer_;
+ QRect orig_;
+ QRect region_;
+ int screen_;
+ int numScreens_;
+
+ int hsize_;
+ int vsize_;
+
+ int hgap_;
+ int vgap_;
+ int hsplit_;
+ int vsplit_;
+
+ QDateTime timestamp_;
+};
+
+#endif
--- /dev/null
+#ifndef __kwingrid_iface_h
+#define __kwingrid_iface_h
+
+#include <qstring.h>
+#include <dcopobject.h>
+
+class KWinGrid_Iface : virtual public DCOPObject
+{
+ K_DCOP
+public:
+ virtual ~KWinGrid_Iface() {}
+
+k_dcop:
+ virtual void move(int __xslot, int __yslot) = 0;
+ virtual void resize(int __xsize, int __ysize) = 0;
+ virtual void moveResize(int __xslot, int __yslot,
+ int __xsize, int __ysize) = 0;
+ virtual void moveRelative(int __xdiff, int __ydiff) = 0;
+ virtual void resizeRelative(int __xdiff, int __ydiff) = 0;
+ virtual void toDesk(int __desk) = 0;
+
+ virtual void quit() = 0;
+
+ virtual int activeWindow() = 0;
+};
+
+#endif
--- /dev/null
+#include <stdlib.h>
+#include <kuniqueapplication.h>
+#include <klocale.h>
+#include <kcmdlineargs.h>
+#include <kaboutdata.h>
+#include <kdebug.h>
+#include <kglobalaccel.h>
+#include <qnamespace.h>
+
+#include "kwingrid.h"
+
+static KCmdLineOptions winGridOpts[] =
+{
+ { "split <width>", I18N_NOOP("split"), 0 },
+ { "+hgap", I18N_NOOP("hgap"), 0 },
+ { "+vgap", I18N_NOOP("vgap"), 0 },
+ { "+hsplit", I18N_NOOP("hsplit"), 0 },
+ { "+vsplit", I18N_NOOP("vsplit"), 0 },
+ { 0,0,0 }
+};
+
+/* Die optimalen werte für hgap und vgap berechnen sich zu:
+ *
+ * gap = size%split + n*split
+ *
+ * für n = 0,1,2,... und size die Bildschirmgröße in der jeweiligen Richtung
+ */
+
+int main(int argc, char **argv)
+{
+ KAboutData * aboutdata = new KAboutData("kwingrid","KWinGrid","0.5",
+ I18N_NOOP("Window Grid"),
+ KAboutData::License_GPL,
+ "(C) 1999,2000,2002,2004 Stefan Bund",
+ "", "http://www.j32.de/");
+ aboutdata->addAuthor("Stefan Bund",I18N_NOOP("Developer"),"stefab@j32.de",
+ "http://www.j32.de");
+
+ KCmdLineArgs::init(argc, argv, aboutdata);
+ KCmdLineArgs::addCmdLineOptions(winGridOpts);
+ KUniqueApplication::addCmdLineOptions();
+
+ if (! KUniqueApplication::start()) {
+ kdError() << "KWinGrid is already running!" << endl;
+ return 0;
+ }
+
+ KApplication * app = new KUniqueApplication;
+ KCmdLineArgs * args = KCmdLineArgs::parsedArgs();
+
+ if (args->count()!=4) {
+ kdError() << "Invalid arguments. Try --help\n";
+ return 0;
+ }
+
+ int split = 0;
+ if (args->isSet("split"))
+ split=args->getOption("split").toInt();
+
+ int hgap = atoi(args->arg(0));
+ int vgap = atoi(args->arg(1));
+ int hsplit = atoi(args->arg(2));
+ int vsplit = atoi(args->arg(3));
+
+ args->clear();
+
+ KWinGrid * winGrid = new KWinGrid(hgap,vgap,hsplit,vsplit,split);
+
+ KGlobalAccel * accel = new KGlobalAccel(app);
+
+ accel->insert("move_TL",
+ I18N_NOOP("Move top-left"),
+ I18N_NOOP("Move active window top-left"),
+ Qt::ALT+Qt::SHIFT+Qt::Key_I, KKey::QtWIN+Qt::SHIFT+Qt::Key_I,
+ winGrid, SLOT ( move_TL() ));
+ accel->insert("move_TR",
+ I18N_NOOP("Move top-right"),
+ I18N_NOOP("Move active window top-right"),
+ Qt::ALT+Qt::SHIFT+Qt::Key_O, KKey::QtWIN+Qt::SHIFT+Qt::Key_O,
+ winGrid, SLOT ( move_TR() ));
+ accel->insert("move_BL",
+ I18N_NOOP("Move bottom-left"),
+ I18N_NOOP("Move active window bottom-left"),
+ Qt::ALT+Qt::SHIFT+Qt::Key_K, KKey::QtWIN+Qt::SHIFT+Qt::Key_K,
+ winGrid, SLOT ( move_BL() ));
+ accel->insert("move_BR",
+ I18N_NOOP("Move bottom-right"),
+ I18N_NOOP("Move active window bottom-right"),
+ Qt::ALT+Qt::SHIFT+Qt::Key_L, KKey::QtWIN+Qt::SHIFT+Qt::Key_L,
+ winGrid, SLOT ( move_BR() ));
+
+ accel->insert("resize_Q",
+ I18N_NOOP("Resize quarter"), I18N_NOOP("Resize quarter"),
+ Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_I, KKey::QtWIN+Qt::CTRL+Qt::SHIFT+Qt::Key_I,
+ winGrid, SLOT ( resize_Q() ));
+ accel->insert("resize_H",
+ I18N_NOOP("Resize horizontal"), I18N_NOOP("Resize horizontal"),
+ Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_O, KKey::QtWIN+Qt::CTRL+Qt::SHIFT+Qt::Key_O,
+ winGrid, SLOT ( resize_H() ));
+ accel->insert("resize_V",
+ I18N_NOOP("Resize vertical"), I18N_NOOP("Resize vertical"),
+ Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_K, KKey::QtWIN+Qt::CTRL+Qt::SHIFT+Qt::Key_K,
+ winGrid, SLOT ( resize_V() ));
+ accel->insert("resize_F",
+ I18N_NOOP("Resize full"), I18N_NOOP("Resize full"),
+ Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_L, KKey::QtWIN+Qt::CTRL+Qt::SHIFT+Qt::Key_L,
+ winGrid, SLOT ( resize_F() ));
+
+ accel->insert("move_L",
+ I18N_NOOP("Move left"), I18N_NOOP("Move left"),
+ Qt::ALT+Qt::SHIFT+Qt::Key_Left, KKey::QtWIN+Qt::SHIFT+Qt::Key_Left,
+ winGrid, SLOT ( move_L() ));
+ accel->insert("move_R",
+ I18N_NOOP("Move right"), I18N_NOOP("Move right"),
+ Qt::ALT+Qt::SHIFT+Qt::Key_Right, KKey::QtWIN+Qt::SHIFT+Qt::Key_Right,
+ winGrid, SLOT ( move_R() ));
+ accel->insert("move_U",
+ I18N_NOOP("Move up"), I18N_NOOP("Move up"),
+ Qt::ALT+Qt::SHIFT+Qt::Key_Up, KKey::QtWIN+Qt::SHIFT+Qt::Key_Up,
+ winGrid, SLOT ( move_U() ));
+ accel->insert("move_D",
+ I18N_NOOP("Move down"), I18N_NOOP("Move down"),
+ Qt::ALT+Qt::SHIFT+Qt::Key_Down, KKey::QtWIN+Qt::SHIFT+Qt::Key_Down,
+ winGrid, SLOT ( move_D() ));
+
+ accel->insert("resize_IH",
+ I18N_NOOP("Increase horizontal size"), I18N_NOOP("Increase horizontal size"),
+ Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_Right,KKey::QtWIN+Qt::CTRL+Qt::SHIFT+Qt::Key_Right,
+ winGrid, SLOT ( resize_IH() ));
+ accel->insert("resize_IV",
+ I18N_NOOP("Increase vertical size"), I18N_NOOP("Increase vertical size"),
+ Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_Down,KKey::QtWIN+Qt::CTRL+Qt::SHIFT+Qt::Key_Down,
+ winGrid, SLOT ( resize_IV() ));
+ accel->insert("resize_DH",
+ I18N_NOOP("Decrease horizontal size"), I18N_NOOP("Decrease horizontal size"),
+ Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_Left,KKey::QtWIN+Qt::CTRL+Qt::SHIFT+Qt::Key_Left,
+ winGrid, SLOT ( resize_DH() ));
+ accel->insert("resize_DV",
+ I18N_NOOP("Decrease vertical size"), I18N_NOOP("Decrease vertical size"),
+ Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_Up,KKey::QtWIN+Qt::CTRL+Qt::SHIFT+Qt::Key_Up,
+ winGrid, SLOT ( resize_DV() ));
+
+
+ accel->updateConnections();
+
+
+ int ret = app->exec();
+
+ delete app;
+ return ret;
+}
+
--- /dev/null
+TARGET=kwingrid
+
+MOCS=\
+kwingrid.moc
+
+OBJECTS=\
+main.o \
+kwingrid.o \
+kwingrid_iface_skel.o \
+kwingrid_iface_stub.o
+
+LIBS=-lkdecore -lkdeui -lDCOP -lqt-mt -lX11 -lXext
+LOPTS=-L/usr/X11R6/lib
+COPTS=-I. -I /usr/include/kde -I /usr/include/qt3 -I /usr/X11R6/include/X11 -O2
+
+all: mocs bin
+
+clean:
+ rm -f $(OBJECTS) $(MOCS) *_skel.cpp *_stub.h *_stub.cpp $(TARGET)
+
+bin: $(TARGET)
+
+mocs: $(MOCS)
+
+%.moc: %.h
+ moc $< -o $@
+
+%.o: %.cc
+ g++ $(COPTS) -c $< -o $@
+
+%.o: %.cpp
+ g++ $(COPTS) -c $< -o $@
+
+%.idl: %.h
+ dcopidl $< > $@
+
+%_skel.cpp %_stub.h %_stub.cpp: %.idl
+ dcopidl2cpp $<
+
+$(TARGET): $(OBJECTS)
+ g++ $(LOPTS) -o $@ $(OBJECTS) $(LIBS)
+
+
--- /dev/null
+#include <kapplication.h>
+#include <dcopclient.h>
+#include <kwin.h>
+#include <Xlib.h>
+
+#include "kwingrid.h"
+#include "kwingrid.moc"
+
+KWinGrid::KWinGrid(int hgap__, int vgap__, int hsplit__, int vsplit__)
+ : DCOPObject("grid"), hgap_(hgap__), vgap_(vgap__), hsplit_(hsplit__), vsplit_(vsplit__)
+{
+ module_ = new KWinModule(KApplication::kApplication());
+}
+
+void KWinGrid::move(int __xslot, int __yslot)
+{
+ moveResize(__xslot, __yslot, -1, -1);
+}
+
+void KWinGrid::resize(int __xsize, int __ysize)
+{
+ moveResize(-1,-1,__xsize,__ysize);
+}
+
+void KWinGrid::toDesk(int __desk)
+{
+ int w = module_->activeWindow();
+ if (w)
+ KWin::setOnDesktop(w,__desk);
+}
+
+void KWinGrid::moveResize(int __xslot, int __yslot,
+ int __xsize, int __ysize)
+{
+ initGeometry();
+ if (activeWindow_) {
+ QRect newGeometry = doMoveResize(__xslot,__yslot,__xsize,__ysize);
+ updateGeometry(newGeometry);
+ applyGeometry();
+ }
+}
+
+void KWinGrid::moveRelative(int __xdiff, int __ydiff)
+{
+ initGeometry();
+ if (activeWindow_) {
+ int xSlot = (outer_.left()-region_.left()+hsize_/2)/hsize_;
+ int ySlot = (outer_.top()-region_.top()+vsize_/2)/vsize_;
+ xSlot += __xdiff;
+ ySlot += __ydiff;
+ if (xSlot<0)
+ xSlot = 0;
+ else if (xSlot >= hsplit_)
+ xSlot = hsplit_-1;
+ if (ySlot<0)
+ ySlot = 0;
+ else if (ySlot >= vsplit_)
+ ySlot = vsplit_-1;
+ QRect newGeometry = doMoveResize(xSlot,ySlot,-1,-1);
+ updateGeometry(newGeometry);
+ applyGeometry();
+ }
+}
+
+void KWinGrid::resizeRelative(int __xdiff, int __ydiff)
+{
+ initGeometry();
+ if (activeWindow_) {
+ int xSize = (outer_.width()+hsize_/2)/hsize_;
+ int ySize = (outer_.height()+vsize_/2)/vsize_;
+ xSize += __xdiff;
+ ySize += __ydiff;
+ if (xSize<1)
+ xSize = 1;
+ if (xSize>hsplit_)
+ xSize = hsplit_;
+ if (ySize<1)
+ ySize = 1;
+ if (ySize>vsplit_)
+ ySize = vsplit_;
+ QRect newGeometry = doMoveResize(-1,-1,xSize,ySize);
+ updateGeometry(newGeometry);
+ applyGeometry();
+ }
+}
+
+QRect KWinGrid::doMoveResize(int __xslot, int __yslot,
+ int __xsize, int __ysize)
+{
+ QRect newGeometry(outer_);
+
+ if (__xsize != -1)
+ newGeometry.setWidth(__xsize*hsize_-hgap_);
+ if (__ysize != -1)
+ newGeometry.setHeight(__ysize*vsize_-vgap_);
+ QSize size = newGeometry.size();
+ if (__xslot != -1) {
+ QPoint p(newGeometry.topLeft());
+ p.setX(region_.left()
+ + __xslot*hsize_
+ + hgap_/2);
+ newGeometry.moveTopLeft(p);
+ }
+ if (__yslot != -1) {
+ QPoint p(newGeometry.topLeft());
+ p.setY(region_.top()
+ + __yslot*vsize_
+ + vgap_/2);
+ newGeometry.moveTopLeft(p);
+ }
+
+ return newGeometry;
+}
+
+void KWinGrid::initGeometry()
+{
+ activeWindow_ = module_->activeWindow();
+ if (activeWindow_) {
+ KWin::Info info(KWin::info(activeWindow_));
+ inner_ = info.geometry;
+ outer_ = info.frameGeometry;
+ region_ = module_->workArea();
+
+ hsize_ = (region_.width()-hgap_)/hsplit_;
+ vsize_ = (region_.height()-vgap_)/vsplit_;
+
+ int hdelta = region_.width()-hsize_*hsplit_;
+ int vdelta = region_.height()-vsize_*vsplit_;
+ QPoint topLeft(region_.topLeft());
+ topLeft+=QPoint(hdelta/2,vdelta/2);
+ region_.moveTopLeft(topLeft);
+ region_.setSize(QSize(hsize_*hsplit_,vsize_*vsplit_));
+ }
+}
+
+void KWinGrid::updateGeometry(QRect& __new)
+{
+ QRect newInner(inner_);
+ newInner.moveTopLeft(QPoint(__new.top()+(inner_.top()-outer_.top()),
+ __new.left()+(inner_.left()-outer_.left())));
+ newInner.setSize(QSize(__new.width()-(outer_.width()-inner_.width()),
+ __new.height()-(outer_.height()-inner_.height())));
+ inner_ = newInner;
+ outer_ = __new;
+}
+
+void KWinGrid::applyGeometry()
+{
+ XMoveResizeWindow(KApplication::kApplication()->getDisplay(),activeWindow_,
+ outer_.x(),outer_.y(),
+ inner_.width(),inner_.height());
+ KWin::setActiveWindow(activeWindow_);
+}
+
+// slots
+
+void KWinGrid::move_TL()
+{
+ move(0,0);
+}
+
+void KWinGrid::move_TR()
+{
+ move(hsplit_/2,0);
+}
+
+void KWinGrid::move_BL()
+{
+ move(0,vsplit_/2);
+}
+
+void KWinGrid::move_BR()
+{
+ move(hsplit_/2,vsplit_/2);
+}
+
+
+void KWinGrid::resize_Q()
+{
+ resize(vsplit_/2,hsplit_/2);
+}
+
+void KWinGrid::resize_H()
+{
+ resize(vsplit_,hsplit_/2);
+}
+
+void KWinGrid::resize_V()
+{
+ resize(vsplit_/2,hsplit_);
+}
+
+void KWinGrid::resize_F()
+{
+ resize(vsplit_,hsplit_);
+}
+
+
+void KWinGrid::move_L()
+{
+ moveRelative(-1,0);
+}
+
+void KWinGrid::move_R()
+{
+ moveRelative(1,0);
+}
+
+void KWinGrid::move_U()
+{
+ moveRelative(0,-1);
+}
+
+void KWinGrid::move_D()
+{
+ moveRelative(0,1);
+}
+
+
+void KWinGrid::resize_IH()
+{
+ resizeRelative(1,0);
+}
+
+void KWinGrid::resize_DH()
+{
+ resizeRelative(-1,0);
+}
+
+void KWinGrid::resize_IV()
+{
+ resizeRelative(0,1);
+}
+
+void KWinGrid::resize_DV()
+{
+ resizeRelative(0,-1);
+}
+
--- /dev/null
+// -*- c++ -*-
+
+#ifndef __kwingrid_h
+#define __kwingrid_h 1
+
+#include <qobject.h>
+#include <kwinmodule.h>
+#include <kwingrid_iface.h>
+
+class KWinGrid
+ : public QObject, public virtual KWinGrid_Iface
+{
+ Q_OBJECT
+
+public:
+ KWinGrid(int hgap__, int vgap__, int hsplit__, int vsplit__);
+
+ virtual void move(int __xslot, int __yslot);
+ virtual void resize(int __xsize, int __ysize);
+ virtual void moveResize(int __xslot, int __yslot,
+ int __xsize, int __ysize);
+ virtual void moveRelative(int __xdiff, int __ydiff);
+ virtual void resizeRelative(int __xdiff, int __ydiff);
+ virtual void toDesk(int __desk);
+
+public slots:
+ void move_TL();
+ void move_TR();
+ void move_BL();
+ void move_BR();
+
+ void resize_Q();
+ void resize_H();
+ void resize_V();
+ void resize_F();
+
+ void move_L();
+ void move_R();
+ void move_U();
+ void move_D();
+
+ void resize_IH();
+ void resize_DH();
+ void resize_IV();
+ void resize_DV();
+
+private:
+ void initGeometry();
+ void updateGeometry(QRect& __new);
+ void applyGeometry();
+ QRect doMoveResize(int __xslot, int __yslot,
+ int __xsize, int __ysize);
+
+ KWinModule* module_;
+ DCOPClient* client_;
+
+ int activeWindow_;
+ QRect inner_;
+ QRect outer_;
+ QRect region_;
+
+ int hsize_;
+ int vsize_;
+
+ int hgap_;
+ int vgap_;
+ int hsplit_;
+ int vsplit_;
+};
+
+#endif
--- /dev/null
+#ifndef __kwingrid_iface_h
+#define __kwingrid_iface_h
+
+#include <qstring.h>
+#include <dcopobject.h>
+
+class KWinGrid_Iface : virtual public DCOPObject
+{
+ K_DCOP
+public:
+ virtual ~KWinGrid_Iface() {}
+
+k_dcop:
+ virtual void move(int __xslot, int __yslot) = 0;
+ virtual void resize(int __xsize, int __ysize) = 0;
+ virtual void moveResize(int __xslot, int __yslot,
+ int __xsize, int __ysize) = 0;
+ virtual void moveRelative(int __xdiff, int __ydiff) = 0;
+ virtual void resizeRelative(int __xdiff, int __ydiff) = 0;
+ virtual void toDesk(int __desk) = 0;
+};
+
+#endif
--- /dev/null
+#include <stdlib.h>
+#include <kuniqueapplication.h>
+#include <klocale.h>
+#include <kcmdlineargs.h>
+#include <kaboutdata.h>
+#include <kdebug.h>
+#include <kglobalaccel.h>
+#include <qnamespace.h>
+
+#include "kwingrid.h"
+
+static KCmdLineOptions winGridOpts[] =
+{
+ { "+hgap", I18N_NOOP("hgap"), 0 },
+ { "+vgap", I18N_NOOP("vgap"), 0 },
+ { "+hsplit", I18N_NOOP("hsplit"), 0 },
+ { "+vsplit", I18N_NOOP("vsplit"), 0 },
+ { 0,0,0 }
+};
+
+/* Die optimalen werte für hgap und vgap berechnen sich zu:
+ *
+ * gap = size % split + n * split
+ *
+ * für n = 0,1,2,... und size die Bildschirmgröße in der jeweiligen Richtung
+ */
+
+int main(int argc, char **argv)
+{
+ KAboutData * aboutdata = new KAboutData("kwingrid","KWinGrid","0.5",
+ I18N_NOOP("Window Grid"),
+ KAboutData::License_GPL,
+ "(C) 1999,2000,2002, Stefan Bund",
+ "", "http://home.t-online.de/home/Stefan.Bund/");
+ aboutdata->addAuthor("Stefan Bund",I18N_NOOP("Developer"),"asbund@gmx.de",
+ "http://home.t-online.de/home/Stefan.Bund");
+
+ KCmdLineArgs::init(argc, argv, aboutdata);
+ KCmdLineArgs::addCmdLineOptions(winGridOpts);
+ KUniqueApplication::addCmdLineOptions();
+
+ if (! KUniqueApplication::start()) {
+ kdError() << "KWinGrid is already running!" << endl;
+ return 0;
+ }
+
+ KApplication * app = new KUniqueApplication;
+ KCmdLineArgs * args = KCmdLineArgs::parsedArgs();
+
+ if (args->count()!=4) {
+ kdError() << "Invalid arguments. Try --help\n";
+ return 0;
+ }
+
+ int hgap = atoi(args->arg(0));
+ int vgap = atoi(args->arg(1));
+ int hsplit = atoi(args->arg(2));
+ int vsplit = atoi(args->arg(3));
+
+ args->clear();
+
+ KWinGrid * winGrid = new KWinGrid(hgap,vgap,hsplit,vsplit);
+
+ KGlobalAccel * accel = new KGlobalAccel(app);
+
+ accel->insert("move_TL",
+ I18N_NOOP("Move top-left"),
+ I18N_NOOP("Move active window top-left"),
+ Qt::ALT+Qt::SHIFT+Qt::Key_I, KKey::QtWIN+Qt::SHIFT+Qt::Key_I,
+ winGrid, SLOT ( move_TL() ));
+ accel->insert("move_TR",
+ I18N_NOOP("Move top-right"),
+ I18N_NOOP("Move active window top-right"),
+ Qt::ALT+Qt::SHIFT+Qt::Key_O, KKey::QtWIN+Qt::SHIFT+Qt::Key_O,
+ winGrid, SLOT ( move_TR() ));
+ accel->insert("move_BL",
+ I18N_NOOP("Move bottom-left"),
+ I18N_NOOP("Move active window bottom-left"),
+ Qt::ALT+Qt::SHIFT+Qt::Key_K, KKey::QtWIN+Qt::SHIFT+Qt::Key_K,
+ winGrid, SLOT ( move_BL() ));
+ accel->insert("move_BR",
+ I18N_NOOP("Move bottom-right"),
+ I18N_NOOP("Move active window bottom-right"),
+ Qt::ALT+Qt::SHIFT+Qt::Key_L, KKey::QtWIN+Qt::SHIFT+Qt::Key_L,
+ winGrid, SLOT ( move_BR() ));
+
+ accel->insert("resize_Q",
+ I18N_NOOP("Resize quarter"), I18N_NOOP("Resize quarter"),
+ Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_I, KKey::QtWIN+Qt::CTRL+Qt::SHIFT+Qt::Key_I,
+ winGrid, SLOT ( resize_Q() ));
+ accel->insert("resize_H",
+ I18N_NOOP("Resize horizontal"), I18N_NOOP("Resize horizontal"),
+ Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_O, KKey::QtWIN+Qt::CTRL+Qt::SHIFT+Qt::Key_O,
+ winGrid, SLOT ( resize_H() ));
+ accel->insert("resize_V",
+ I18N_NOOP("Resize vertical"), I18N_NOOP("Resize vertical"),
+ Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_K, KKey::QtWIN+Qt::CTRL+Qt::SHIFT+Qt::Key_K,
+ winGrid, SLOT ( resize_V() ));
+ accel->insert("resize_F",
+ I18N_NOOP("Resize full"), I18N_NOOP("Resize full"),
+ Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_L, KKey::QtWIN+Qt::CTRL+Qt::SHIFT+Qt::Key_L,
+ winGrid, SLOT ( resize_F() ));
+
+ accel->insert("move_L",
+ I18N_NOOP("Move left"), I18N_NOOP("Move left"),
+ Qt::ALT+Qt::SHIFT+Qt::Key_Left, KKey::QtWIN+Qt::SHIFT+Qt::Key_Left,
+ winGrid, SLOT ( move_L() ));
+ accel->insert("move_R",
+ I18N_NOOP("Move right"), I18N_NOOP("Move right"),
+ Qt::ALT+Qt::SHIFT+Qt::Key_Right, KKey::QtWIN+Qt::SHIFT+Qt::Key_Right,
+ winGrid, SLOT ( move_R() ));
+ accel->insert("move_U",
+ I18N_NOOP("Move up"), I18N_NOOP("Move up"),
+ Qt::ALT+Qt::SHIFT+Qt::Key_Up, KKey::QtWIN+Qt::SHIFT+Qt::Key_Up,
+ winGrid, SLOT ( move_U() ));
+ accel->insert("move_D",
+ I18N_NOOP("Move down"), I18N_NOOP("Move down"),
+ Qt::ALT+Qt::SHIFT+Qt::Key_Down, KKey::QtWIN+Qt::SHIFT+Qt::Key_Down,
+ winGrid, SLOT ( move_D() ));
+
+ accel->insert("resize_IH",
+ I18N_NOOP("Increase horizontal size"), I18N_NOOP("Increase horizontal size"),
+ Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_Right,KKey::QtWIN+Qt::CTRL+Qt::SHIFT+Qt::Key_Right,
+ winGrid, SLOT ( resize_IH() ));
+ accel->insert("resize_IV",
+ I18N_NOOP("Increase vertical size"), I18N_NOOP("Increase vertical size"),
+ Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_Down,KKey::QtWIN+Qt::CTRL+Qt::SHIFT+Qt::Key_Down,
+ winGrid, SLOT ( resize_IV() ));
+ accel->insert("resize_DH",
+ I18N_NOOP("Decrease horizontal size"), I18N_NOOP("Decrease horizontal size"),
+ Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_Left,KKey::QtWIN+Qt::CTRL+Qt::SHIFT+Qt::Key_Left,
+ winGrid, SLOT ( resize_DH() ));
+ accel->insert("resize_DV",
+ I18N_NOOP("Decrease vertical size"), I18N_NOOP("Decrease vertical size"),
+ Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_Up,KKey::QtWIN+Qt::CTRL+Qt::SHIFT+Qt::Key_Up,
+ winGrid, SLOT ( resize_DV() ));
+
+
+ accel->updateConnections();
+
+
+ int ret = app->exec();
+
+ delete app;
+ return ret;
+}
+