Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C++ by Stefan ( 15 years ago )
/* first include the standard headers that we're likely to need */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* create a namespace with title @gui */
namespace gui {
typedef struct {
Window window;
unsigned int w, h;
unsigned long background, border;
} CWindow;
typedef struct {
int function; /* logical operation */
unsigned long plane_mask;/* plane mask */
unsigned long foreground;/* foreground pixel */
unsigned long background;/* background pixel */
int line_width; /* line width (in pixels) */
int line_style; /* LineSolid, LineOnOffDash, LineDoubleDash */
int cap_style; /* CapNotLast, CapButt, CapRound, CapProjecting */
int join_style; /* JoinMiter, JoinRound, JoinBevel */
int fill_style; /* FillSolid, FillTiled, FillStippled FillOpaqueStippled*/
int fill_rule; /* EvenOddRule, WindingRule */
int arc_mode; /* ArcChord, ArcPieSlice */
Pixmap tile; /* tile pixmap for tiling operations */
Pixmap stipple; /* stipple 1 plane pixmap for stippling */
int ts_x_origin; /* offset for tile or stipple operations */
int ts_y_origin;
Font font; /* default text font for text operations */
int subwindow_mode; /* ClipByChildren, IncludeInferiors */
Bool graphics_exposures; /* boolean, should exposures be generated */
int clip_x_origin; /* origin for clipping */
int clip_y_origin;
Pixmap clip_mask; /* bitmap clipping; other calls for rects */
int dash_offset; /* patterned/dashed line information */
char dashes;
} CMask;
typedef struct {
XColor x, y;
} CColor;
struct Root {
Display *display;
int id;
} disp;
/* client class */
class Client {
friend class Draw;
public :
/* create a new screen */
void init()
{
disp.display = XOpenDisplay(NULL);
if (!disp.display)
fprintf(stderr, "unable to connect to displayn");
disp.id = DefaultScreen(disp.display);
}
/* close the window and screen */
void close()
{
XCloseDisplay(disp.display);
}
/* create a new window, and listen events */
void create_window(CWindow &win;)
{
win.window = XCreateSimpleWindow(disp.display, DefaultRootWindow(disp.display), /* display, parent */
0,0, /* x, y: the window manager will place the window elsewhere */
win.w,win.h, /* width, height */
2, win.border, /* border width & colour, unless you have a window manager */
win.background); /* background colour */
XSelectInput(disp.display, win.window,
ButtonPressMask|StructureNotifyMask|ExposureMask );
}
/* set a a new background color for window */
void set_background(CWindow &win;)
{
win.background = BlackPixel(disp.display, disp.id);
}
/* set a new window broder */
void set_border(CWindow &win;)
{
win.border = BlackPixel(disp.display, disp.id);
}
/* set a new window size */
void set_size(CWindow &win;, int x, int y)
{
win.w = x;
win.h = y;
}
/* put the window on the screen */
void show_window(CWindow win)
{
XMapWindow(disp.display, win.window);
}
/* clear the window */
void clear_window(XEvent ev)
{
XClearWindow(disp.display, ev.xany.window);
}
/* print next event in x quene*/
void getevent(XEvent &ev;)
{
XNextEvent(disp.display, &ev;);
}
};
/* drawing class*/
class Draw : public Client
{
public:
/* fill a CColor value with a colorcode*/
CColor fill_with_color(char arr[])
{
Colormap cmap;
CColor x;
cmap = DefaultColormap(disp.display, disp.id);
XAllocNamedColor(disp.display, cmap, arr, &x.x, &x.y);
return x;
}
/* draw a line */
void draw_line(CWindow &win;, int begin_y, int begin_x, int end_x, int end_y)
{
GC pen;
CMask mask;
CColor x = fill_with_color("Red");
mask.foreground = x.x.pixel;
mask.line_width = 10;
mask.line_style = LineSolid;
pen = XCreateGC(disp.display, win.window,
GCForeground|GCLineWidth|GCLineStyle, (XGCValues*)&mask;);
XDrawLine(disp.display, win.window, pen, begin_x, begin_y, end_y, end_x);
}
};
}
using namespace gui;
/* example */
int main(int argc, char ** argv)
{
Client c;
Draw d;
CWindow win;
XEvent ev;
c.init();
c.set_background(win);
c.set_border(win);
c.set_size(win, 400,400);
c.create_window(win);
c.show_window(win);
while(1){
c.getevent(ev);
switch(ev.type){
case Expose:
d.draw_line(win, 90, 70, 90, 170);
break;
case ConfigureNotify:
if (win.w != ev.xconfigure.width
|| win.h != ev.xconfigure.height) {
win.w = ev.xconfigure.width;
win.h = ev.xconfigure.height;
c.clear_window(ev);
printf("Size changed to: %d by %dn", win.w, win.h);
}
break;
case ButtonPress:
c.close();
return 0;
}
}
}
Revise this Paste