Guest User

Untitled

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