~ubuntu-branches/ubuntu/vivid/gcl/vivid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**************************************************************/

#include <stdio.h>
#include <X11/Xlib.h>           /* the X library */
#include <X11/Xutil.h>          /* the X library */

   /* a few arbitary constants */
#define START_X         10
#define START_Y         20
#define WINDOW_WIDTH   225   
#define WINDOW_HEIGHT  400
#define BORDER_WIDTH     1
#define KEY_STR_LENGTH  20

Display *the_display;           /* the display that will be used */
int the_screen;                 /* the screen that will be used */
Window root_window;             /* the root window on the screen */
XSizeHints size_hints;          /* size hints for the window manager */
XEvent the_event;               /* the structure for the input event */
XSetWindowAttributes attributes;/* the windows attributes */

GC the_solid_GC,
   the_clear_GC;     /* the graphics contexts */

XGCValues the_solid_GC_values,
          the_clear_GC_values;

Colormap cmap;
XFontStruct *the_fontstruct;       /* the font info to be used */

Window open_window(void)
{
  Window the_window;              /* the window that will be opened */
  int i, stop;

  /* Set the display to be the default display (ie, your
     display as given in the environment variable DISPLAY). */

  if ((the_display = XOpenDisplay("")) == NULL)
    {
      printf("can't open display\n");
      return(-1);
    }

  /* A few useful values. */

  the_screen = DefaultScreen(the_display);
  root_window = RootWindow(the_display,the_screen);

  /* Set the size hints for the window manager. */

  size_hints.x = START_X;
  size_hints.y = START_Y;
  size_hints.width = WINDOW_WIDTH;
  size_hints.height = WINDOW_HEIGHT;
  size_hints.flags = PSize|PPosition;

  /* Create a window of fixed size, origin, and borderwidth.
     The window will have a black border and white background. */

  the_window = XCreateSimpleWindow(the_display,root_window,
				   size_hints.x,size_hints.y,size_hints.width,
				   size_hints.height,BORDER_WIDTH,
				   BlackPixel(the_display,the_screen),
				   WhitePixel(the_display,the_screen));
				   
  XSetStandardProperties(the_display,the_window,"My Window","My Icon",
			 None,NULL,NULL,&size_hints);

  cmap = DefaultColormap(the_display, the_screen);

  the_solid_GC = XCreateGC(the_display, the_window, None, &the_solid_GC_values);
  the_clear_GC = XCreateGC(the_display, the_window, None, &the_clear_GC_values);

  /* for a sun */
  XSetBackground(the_display, the_solid_GC, BlackPixel(the_display,the_screen));
  XSetForeground(the_display, the_solid_GC, BlackPixel(the_display,the_screen));

  XSetBackground(the_display, the_clear_GC, WhitePixel(the_display,the_screen));
  XSetForeground(the_display, the_clear_GC, WhitePixel(the_display,the_screen));

  if ((the_fontstruct = XLoadQueryFont(the_display,"8x13")) == NULL)
    {
      printf("could not open font\n");
      return(-1);
    }
  /* Put the font into the graphics context for draw operations. */
  XSetFont(the_display, the_solid_GC, the_fontstruct->fid);
  XSetFont(the_display, the_clear_GC, the_fontstruct->fid);

  /* Tell the server to make the window visible. */

  XMapWindow(the_display,the_window);

  attributes.bit_gravity = NorthWestGravity;
  XChangeWindowAttributes(the_display, the_window, CWBitGravity, &attributes);
  XFlush(the_display);
  return(the_window);
}

int close_window(Window the_window)
{
  XDestroyWindow(the_display, the_window);
  XFlush(the_display);
  return(1);
}

int draw_line(Window the_window, int x1, int y1, int x2, int y2)
{
  XDrawLine(the_display, the_window, the_solid_GC, x1, y1, x2, y2);
  XFlush(the_display);
  return(1);
}

int draw_arc(Window the_window, int x, int y, int width, int height, int angle1, int angle2)
{
  XDrawArc(the_display, the_window, the_solid_GC,
	   x, y, width, height, angle1, angle2);
  XFlush(the_display);
  return(1);
}

int fill_arc(Window the_window, int x, int y, int width, int height, int angle1, int angle2)
{
  XFillArc(the_display, the_window, the_solid_GC,
	   x, y, width, height, angle1, angle2);
  XFlush(the_display);
  return(1);
}

int clear_arc(Window the_window, int x, int y, int width, int height, int angle1, int angle2)
{
  XFillArc(the_display, the_window, the_clear_GC,
	   x, y, width, height, angle1, angle2);
  XFlush(the_display);
  return(1);
}

int set_arc_mode (int pie_or_chord)
{
  if (pie_or_chord == 0) {
    XSetArcMode(the_display, the_solid_GC, ArcChord);
    XSetArcMode(the_display, the_clear_GC, ArcChord);
  }
  else {
    XSetArcMode(the_display, the_solid_GC, ArcPieSlice);
    XSetArcMode(the_display, the_clear_GC, ArcPieSlice);
  }
  return(1);
}

int erase_line(Window the_window, int x1, int y1, int x2, int y2)
{
  XDrawLine(the_display, the_window, the_clear_GC, x1, y1, x2, y2);
  XFlush(the_display);
  return(1);
}

int draw_text(Window the_window, char *string, int x, int y)
{
  XDrawString(the_display, the_window, the_solid_GC, x, y,
	      string, strlen(string));
  XFlush(the_display);
  return(1);
}

int erase_text(Window the_window, char *string, int x, int y)
{
  XDrawString(the_display, the_window, the_clear_GC, x, y,
	      string, strlen(string));
  XFlush(the_display);
  return(1);
}

int clear_window(Window the_window)
{
  XClearWindow(the_display, the_window);
  XFlush(the_display);
  return(1);
}

int resize_window(Window the_window, int width, int height)
{
  XResizeWindow(the_display, the_window, width, height);
  XFlush(the_display);
  return(1);
}

int raise_window(Window the_window)
{
  XRaiseWindow(the_display, the_window);
  XFlush(the_display);
  return(1);
}

int use_font (char *font_name)
{
  if ((the_fontstruct = XLoadQueryFont(the_display, font_name)) == NULL)
    return(-1);

  /* Put the font into the graphics context for draw operations. */
  XSetFont(the_display, the_solid_GC, the_fontstruct->fid);
  XSetFont(the_display, the_clear_GC, the_fontstruct->fid);
  XFlush(the_display);
  return(1);
}



int set_background (Window the_window, char *color_string)
{
  XColor color;
  int result;

  if (result = XParseColor(the_display, cmap, color_string, &color)) {
    if (result = XAllocColor(the_display, cmap, &color)) {
      XSetWindowBackground(the_display, the_window, color.pixel);
      XSetBackground(the_display, the_clear_GC, color.pixel);
      XSetForeground(the_display, the_clear_GC, color.pixel);
      XFlush(the_display);
    }
  }
  return(result);
}

int set_foreground (char *color_string)
{
  XColor color;
  int result;

  if (result = XParseColor(the_display, cmap, color_string, &color)) {
    if (result = XAllocColor(the_display, cmap, &color)) {
      XSetForeground(the_display, the_solid_GC, color.pixel);
      XSetBackground(the_display, the_solid_GC, color.pixel);
      XFlush(the_display);
      return(1);
    }
  }
}