Back to index

easystroke  0.5.5.1
shape.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008-2009, Thomas Jaeger <ThJaeger@gmail.com>
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00011  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
00013  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
00014  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 #include <gdk/gdk.h>
00017 #include "prefdb.h"
00018 #include "shape.h"
00019 #include "main.h"
00020 
00021 #include <X11/extensions/shape.h>
00022 
00023 Shape::Shape() {
00024        int w = gdk_screen_width();
00025        int h = gdk_screen_height();
00026        Gdk::Color col = prefs.color.get().color;
00027        unsigned long bg = ((col.get_red()/257)<<16) + ((col.get_green()/257)<<8) + col.get_blue()/257;
00028        win = XCreateSimpleWindow(dpy, ROOT, 0, 0, w, h, 0, CopyFromParent, bg);
00029        XSetWindowAttributes attr;
00030        attr.override_redirect = True;
00031        XChangeWindowAttributes(dpy, win, CWOverrideRedirect, &attr);
00032 
00033        clear();
00034 }
00035 
00036 void Shape::draw(Point p, Point q) {
00037        int px = (int)p.x, py = (int)p.y, qx = (int)q.x, qy = (int)q.y;
00038        int width = prefs.trace_width.get();
00039        int x = (MIN(px, qx) - width);
00040        int y = (MIN(py, qy) - width);
00041        int w = (ABS(px - qx) + 2*width);
00042        int h = (ABS(py - qy) + 2*width);
00043        Pixmap pm = XCreatePixmap(dpy, DefaultRootWindow(dpy), w, h, 1);
00044 
00045        XGCValues gcv;
00046        gcv.foreground = 0;
00047        gcv.line_width = width;
00048        gcv.cap_style = CapRound;
00049        GC gc = XCreateGC(dpy, pm, GCCapStyle | GCForeground | GCLineWidth, &gcv);
00050        XFillRectangle(dpy, pm, gc, 0, 0, w, h);
00051        XSetForeground(dpy, gc, 1);
00052        XDrawLine(dpy, pm, gc, px-x, py-y, qx-x, qy-y);
00053        XFreeGC(dpy, gc);
00054 
00055        XShapeCombineMask(dpy, win, ShapeBounding, x, y, pm, ShapeUnion);
00056        XFreePixmap(dpy, pm);
00057 }
00058 
00059 void Shape::start_() {
00060        if (remove_timeout())
00061               clear();
00062        XMapRaised(dpy, win);
00063 }
00064 
00065 void Shape::end_() {
00066        XUnmapWindow(dpy, win);
00067        set_timeout(10);
00068 }
00069 
00070 void Shape::timeout() {
00071        clear();
00072        XFlush(dpy);
00073 }
00074 
00075 void Shape::clear() {
00076        XShapeCombineRectangles(dpy, win, ShapeBounding, 0, 0, NULL, 0, ShapeSet, YXBanded);
00077 }
00078 
00079 Shape::~Shape() {
00080        XDestroyWindow(dpy, win);
00081 }