Showing posts with label Xwindow programming. Show all posts
Showing posts with label Xwindow programming. Show all posts

Monday, May 21, 2012

Drawing With Mouse in Xwindow

Here i am giving a code block which takes two points and draw the line between those two points. Where point is a structure

struct Point {
                      double x;
                      double y;
                    };

/ * Function Start */ 

void Draw_Gesture(Point p, Point q)
{
                 Drawable d = w;
                 XGCValues values;
                 values.line_width = prefs.trace_width.get();
                 values.line_style = LineSolid;
                 GC gc = XCreateGC(dpy, d, GCLineWidth, &values);
                 XDrawLine(dpy, w, gc, p.x, p.y, q.x, q.y);
                return;
}

/ * Function End */ 

Now to draw line, as long as Moving the mouse, we have to write a function to Listen mouse events. From that event variable we will get x and y coordinates of current mouse locations. We assign these x and y coordinates to x and y of Point structure. Then we will send these Point Structure variables to Draw_Gesture functions. Now Draw_Gesture  function Draws the line as long as mouse moves in window w.

Drawing with Mouse
Drawing With Mouse

Keeping a Window Always Top in Xwindow Programing

 This program shows how to create a simple window in Xwindow Programing and keeping that window Always on top of all the Windows.

/ * Program Start */ 

/*Program Name: WindowTop.cc
Purpose: Creating Xwindow & Keeping that window always top of all windows*/

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <X11/extensions/shape.h>
#define NIL (0)

void Keep_Window_Always_Top(Display *dpy, Window w)
{
  Atom stateAbove;
  if (w) {
     stateAbove = XInternAtom(dpy, "_NET_WM_STATE_ABOVE", False);
     XChangeProperty(dpy, w, XInternAtom(dpy, "_NET_WM_STATE", False), XA_ATOM, 32, PropModeReplace, (unsigned char *) &stateAbove, 1);
    }
  
    return ;
}

int main()
{
  char *window_name = (char*)"gem input window";
  Display *dpy = XOpenDisplay(NIL);
  int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
  Window w = XCreateSimpleWindow(dpy, RootWindow(dpy, DefaultScreen(dpy)), 0, 0,
                     230, 150, 0, whiteColor, whiteColor);
  XStoreName(dpy, w, window_name);
  Keep_Window_Always_Top(dpy, w);
  XMapWindow(dpy, w);
  XMoveWindow(dpy, w, 0, 600);
  return 0;
}

/ * Program End */ 

To run this program type g++ -lX11  WindowTop.cc in terminal