Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.20 KB | None | 0 0
  1. #include <X11/Xlib.h>
  2.  
  3. #include <X11/Xutil.h>
  4.  
  5. #include <X11/Xos.h>
  6.  
  7. #include <X11/Xatom.h>
  8.  
  9. #include <stdio.h>
  10.  
  11. #include "bitmaps/icon_bitmap"
  12. #define BITMAPDEPTH 1
  13. #define TOO_SMALL 0
  14. #define BIG_ENOUGH 1
  15. /* These are used as arguments to nearly every Xlib routine, so it
  16. * saves routine arguments to declare them global; if there were
  17. * additional source files, they would be declared extern there */
  18. Display *display;
  19. int screen_num;
  20. /* progname is the string by which this program was invoked; this
  21. * is global because it is needed in most application functions */
  22. static char *progname;
  23. void main(argc, argv)
  24. int argc;
  25. char **argv;
  26. {
  27. Window win;
  28. unsigned int width, height; /* Window size */
  29. int x, y; /* Window position */
  30. unsigned int border_width = 4; /* Four pixels */
  31. unsigned int display_width, display_height;
  32. unsigned int icon_width, icon_height;
  33. char *window_name = "Basic Window Program";
  34. char *icon_name = "basicwin";
  35. Pixmap icon_pixmap;
  36. XSizeHints *size_hints;
  37. XIconSize *size_list;
  38. XWMHints *wm_hints;
  39. XClassHint *class_hints;
  40. XTextProperty windowName, iconName;
  41. int count;
  42. XEvent report;
  43. GC gc;
  44. XFontStruct *font_info;
  45. char *display_name = NULL;
  46. int window_size = 0; /* BIG_ENOUGH or TOO_SMALL to
  47. * display contents */
  48. progname = argv[0];
  49. if (!(size_hints = XAllocSizeHints())) {
  50. fprintf(stderr, "%s: failure allocating memory, progname);
  51. exit(0);
  52. }
  53. if (!(wm_hints = XAllocWMHints())) {
  54. fprintf(stderr, "%s: failure allocating memory, progname);
  55. exit(0);
  56. }
  57. if (!(class_hints = XAllocClassHint())) {
  58. fprintf(stderr, "%s: failure allocating memory, progname);
  59. exit(0);
  60. }
  61. /* Connect to X server */
  62. if ( (display=XOpenDisplay(display_name)) == NULL )
  63. {
  64. (void) fprintf( stderr, "%s: cannot connect to X server %s\n",
  65. progname, XDisplayName(display_name));
  66. exit( -1 );
  67. }
  68. /* Get screen size from display structure macro */
  69. screen_num = DefaultScreen(display);
  70. display_width = DisplayWidth(display, screen_num);
  71. display_height = DisplayHeight(display, screen_num);
  72. /* Note that in a real application, x and y would default
  73. * to 0 but would be settable from the command line or
  74. * resource database */
  75. x = y = 0;
  76. /* Size window with enough room for text */
  77. width = display_width/3, height = display_height/4;
  78. /* Create opaque window */
  79. win = XCreateSimpleWindow(display, RootWindow(display,screen_num),
  80. x, y, width, height, border_width, BlackPixel(display,
  81. screen_num), WhitePixel(display,screen_num));
  82. /* Get available icon sizes from window manager */
  83. if (XGetIconSizes(display, RootWindow(display,screen_num),
  84. &size_list, &count) == 0)
  85. (void) fprintf( stderr, "%s: Window manager didn't set \
  86. icon sizes - using default.\n", progname);
  87. else {
  88. ;
  89. /* A real application would search through size_list
  90. * here to find an acceptable icon size and then
  91. * create a pixmap of that size; this requires that
  92. * the application have data for several sizes of icons */
  93. }
  94. /* Create pixmap of depth 1 (bitmap) for icon */
  95. icon_pixmap = XCreateBitmapFromData(display, win,
  96. icon_bitmap_bits, icon_bitmap_width,
  97. icon_bitmap_height);
  98. /* Set size hints for window manager; the window manager
  99. * may override these settings */
  100. /* Note that in a real application, if size or position
  101. * were set by the user, the flags would be USPosition
  102. * and USSize and these would override the window manager's
  103. * preferences for this window */
  104. /* x, y, width, and height hints are now taken from
  105. * the actual settings of the window when mapped; note
  106. * that PPosition and PSize must be specified anyway */
  107. size_hints->flags = PPosition | PSize | PMinSize;
  108. size_hints->min_width = 300;
  109. size_hints->min_height = 200;
  110. /* These calls store window_name and icon_name into
  111. * XTextProperty structures and set their other fields
  112. * properly */
  113. if (XStringListToTextProperty(&window_name, 1, &windowName) == 0) {
  114. (void) fprintf( stderr, "%s: structure allocation for \
  115. windowName failed.\n", progname);
  116. exit(-1);
  117. }
  118.  
  119. if (XStringListToTextProperty(&icon_name, 1, &iconName) == 0) {
  120. (void) fprintf( stderr, "%s: structure allocation for \
  121. iconName failed.\n", progname);
  122. exit(-1);
  123. }
  124. wm_hints->initial_state = NormalState;
  125. wm_hints->input = True;
  126. wm_hints->icon_pixmap = icon_pixmap;
  127. wm_hints->flags = StateHint | IconPixmapHint | InputHint;
  128. class_hints->res_name = progname;
  129. class_hints->res_class = "Basicwin";
  130. XSetWMProperties(display, win, &windowName, &iconName,
  131. argv, argc, size_hints, wm_hints,
  132. class_hints);
  133. }
  134. /* Select event types wanted */
  135. XSelectInput(display, win, ExposureMask | KeyPressMask |
  136. ButtonPressMask | StructureNotifyMask);
  137. load_font(&font_info);
  138. /* Create GC for text and drawing */
  139. getGC(win, &gc, font_info);
  140. /* Display window */
  141. XMapWindow(display, win);
  142. /* Get events, use first to display text and graphics */
  143. while (1) {
  144. XNextEvent(display, &report);
  145. switch (report.type) {
  146. case Expose:
  147. /* Unless this is the last contiguous expose,
  148. * don't draw the window */
  149. if (report.xexpose.count != 0)
  150. break;
  151. /* If window too small to use */
  152. if (window_size == TOO_SMALL)
  153. TooSmall(win, gc, font_info);
  154. else {
  155. /* Place text in window */
  156. place_text(win, gc, font_info, width, height);
  157. /* Place graphics in window */
  158. place_graphics(win, gc, width, height);
  159. }
  160. break;
  161. case ConfigureNotify:
  162. /* Window has been resized; change width
  163. * and height to send to place_text and
  164. * place_graphics in next Expose */
  165. width = report.xconfigure.width;
  166. height = report.xconfigure.height;
  167. if ((width < size_hints->min_width) ||
  168. (height < size_hints->min_height))
  169. window_size = TOO_SMALL;
  170. else
  171. window_size = BIG_ENOUGH;
  172. break;
  173. case ButtonPress:
  174. /* Trickle down into KeyPress (no break) */
  175. case KeyPress:
  176. XUnloadFont(display, font_info->fid);
  177. XFreeGC(display, gc);
  178. XCloseDisplay(display);
  179. exit(1);
  180. default:
  181. /* All events selected by StructureNotifyMask
  182. * except ConfigureNotify are thrown away here,
  183. * since nothing is done with them */
  184. break;
  185. } /* End switch */
  186. } /* End while */
  187. }
  188. getGC(win, gc, font_info)
  189. Window win;
  190. GC *gc;
  191. XFontStruct *font_info;
  192. {
  193. unsigned long valuemask = 0; /* Ignore XGCvalues and
  194. * use defaults */
  195. XGCValues values;
  196. unsigned int line_width = 6;
  197. int line_style = LineOnOffDash;
  198. int cap_style = CapRound;
  199. int join_style = JoinRound;
  200. int dash_offset = 0;
  201. static char dash_list[] = {12, 24};
  202. int list_length = 2;
  203. /* Create default Graphics Context */
  204. *gc = XCreateGC(display, win, valuemask, &values);
  205. /* Specify font */
  206. XSetFont(display, *gc, font_info->fid);
  207. /* Specify black foreground since default window background
  208. * is white and default foreground is undefined */
  209. XSetForeground(display, *gc, BlackPixel(display,screen_num));
  210. /* Set line attributes */
  211. XSetLineAttributes(display, *gc, line_width, line_style,
  212. cap_style, join_style);
  213. /* Set dashes */
  214. XSetDashes(display, *gc, dash_offset, dash_list, list_length);
  215. }
  216. load_font(font_info)
  217. XFontStruct **font_info;
  218. {
  219. char *fontname = "9x15";
  220. /* Load font and get font information structure */
  221. if ((*font_info = XLoadQueryFont(display,fontname)) == NULL)
  222. {
  223. (void) fprintf( stderr, "%s: Cannot open 9x15 font\n",
  224. progname);
  225. exit( -1 );
  226. }
  227. }
  228. place_text(win, gc, font_info, win_width, win_height)
  229. Window win;
  230. GC gc;
  231. XFontStruct *font_info;
  232. unsigned int win_width, win_height;
  233. {
  234. char *string1 = "Hi! I'm a window, who are you?";
  235. char *string2 = "To terminate program; Press any key";
  236. char *string3 = "or button while in this window.";
  237. char *string4 = "Screen Dimensions:";
  238. int len1, len2, len3, len4;
  239. int width1, width2, width3;
  240. char cd_height[50], cd_width[50], cd_depth[50];
  241. int font_height;
  242. int initial_y_offset, x_offset;
  243. /* Need length for both XTextWidth and XDrawString */
  244. len1 = strlen(string1);
  245. len2 = strlen(string2);
  246. len3 = strlen(string3);
  247. /* Get string widths for centering */
  248. width1 = XTextWidth(font_info, string1, len1);
  249. width2 = XTextWidth(font_info, string2, len2);
  250. width3 = XTextWidth(font_info, string3, len3);
  251. font_height = font_info->ascent + font_info->descent;
  252. /* Output text, centered on each line */
  253. XDrawString(display, win, gc, (win_width - width1)/2,
  254. font_height,
  255. string1, len1);
  256. XDrawString(display, win, gc, (win_width - width2)/2,
  257. (int)(win_height - (2 * font_height)),
  258. string2, len2);
  259. XDrawString(display, win, gc, (win_width - width3)/2,
  260. (int)(win_height - font_height),
  261. string3, len3);
  262. /* Copy numbers into string variables */
  263. (void) sprintf(cd_height, " Height - %d pixels",
  264. DisplayHeight(display,screen_num));
  265. (void) sprintf(cd_width, " Width - %d pixels",
  266. DisplayWidth(display,screen_num));
  267. (void) sprintf(cd_depth, " Depth - %d plane(s)",
  268. DefaultDepth(display, screen_num));
  269. /* Reuse these for same purpose */
  270. len4 = strlen(string4);
  271. len1 = strlen(cd_height);
  272. len2 = strlen(cd_width);
  273. len3 = strlen(cd_depth);
  274. /* To center strings vertically, we place the first string
  275. * so that the top of it is two font_heights above the center
  276. * of the window; since the baseline of the string is what
  277. * we need to locate for XDrawString and the baseline is
  278. * one font_info -> ascent below the top of the character,
  279. * the final offset of the origin up from the center of
  280. * the window is one font_height + one descent */
  281. initial_y_offset = win_height/2 - font_height -
  282. font_info->descent;
  283. x_offset = (int) win_width/4;
  284. XDrawString(display, win, gc, x_offset, (int) initial_y_offset,
  285. string4,len4);
  286. XDrawString(display, win, gc, x_offset, (int) initial_y_offset +
  287. font_height,cd_height,len1);
  288. XDrawString(display, win, gc, x_offset, (int) initial_y_offset +
  289. 2 * font_height,cd_width,len2);
  290. XDrawString(display, win, gc, x_offset, (int) initial_y_offset +
  291. 3 * font_height,cd_depth,len3);
  292. }
  293. place_graphics(win, gc, window_width, window_height)
  294. Window win;
  295. GC gc;
  296. unsigned int window_width, window_height;
  297. {
  298. int x, y;
  299. int width, height;
  300. height = window_height/2;
  301. width = 3 * window_width/4;
  302. x = window_width/2 - width/2; /* Center */
  303. y = window_height/2 - height/2;
  304. XDrawRectangle(display, win, gc, x, y, width, height);
  305. }
  306. TooSmall(win, gc, font_info)
  307. Window win;
  308. GC gc;
  309. XFontStruct *font_info;
  310. {
  311. char *string1 = "Too Small";
  312. int y_offset, x_offset;
  313. y_offset = font_info->ascent + 2;
  314. x_offset = 2;
  315. /* Output text, centered on each line */
  316. XDrawString(display, win, gc, x_offset, y_offset, string1,
  317. strlen(string1));
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement