home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / LIB / x11r6pl13_lib_v0.6.lzh / X11R6PL13_LIB0.6 / EXAMPLE / basicwin.c < prev    next >
C/C++ Source or Header  |  1995-10-20  |  11KB  |  327 lines

  1. /*
  2.  * Copyright 1989 O'Reilly and Associates, Inc.
  3.  * See ../Copyright for complete rights and liability information.
  4.  */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <X11/Xlib.h>
  9. #include <X11/Xutil.h>
  10. #include <X11/Xos.h>
  11.  
  12. #ifdef _OSK
  13. #define icon_bitmap_width 40
  14. #define icon_bitmap_height 40
  15. static char icon_bitmap_bits[] = {
  16.    0xc3, 0xc3, 0x7f, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00,
  17.    0x00, 0x00, 0x80, 0x38, 0x00, 0x40, 0x00, 0x80, 0x24, 0x00, 0x00, 0x00,
  18.    0x80, 0x44, 0x00, 0x00, 0x00, 0x80, 0x44, 0x00, 0x00, 0x00, 0x80, 0x74,
  19.    0x00, 0x0f, 0x0c, 0x00, 0x7c, 0x3e, 0x41, 0x0e, 0x00, 0x44, 0x22, 0x41,
  20.    0x02, 0x00, 0x84, 0x22, 0x46, 0x02, 0x00, 0x9c, 0x26, 0xcc, 0x02, 0x00,
  21.    0x78, 0x3c, 0xcd, 0x36, 0x80, 0x00, 0x20, 0x06, 0x0c, 0x80, 0x01, 0x00,
  22.    0x00, 0x00, 0x80, 0x01, 0x02, 0x40, 0x00, 0x80, 0x01, 0x06, 0x40, 0x00,
  23.    0x80, 0x01, 0x04, 0x20, 0x00, 0x80, 0x01, 0x04, 0x20, 0x01, 0x80, 0x01,
  24.    0x04, 0x20, 0x00, 0x80, 0x01, 0x04, 0x22, 0x00, 0x80, 0x01, 0x04, 0x33,
  25.    0xf1, 0x81, 0x01, 0x88, 0x12, 0x31, 0x03, 0x01, 0x88, 0x12, 0x11, 0x02,
  26.    0x00, 0x88, 0x12, 0x11, 0x02, 0x00, 0x48, 0x1a, 0x11, 0x02, 0x00, 0x70,
  27.    0x04, 0x19, 0x82, 0x01, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x38,
  28.    0x80, 0x01, 0x00, 0x00, 0xce, 0x80, 0x01, 0x00, 0x00, 0x83, 0x81, 0x81,
  29.    0x07, 0x80, 0x01, 0x81, 0xe1, 0x04, 0xc0, 0x00, 0x83, 0x31, 0x08, 0x40,
  30.    0x00, 0x82, 0x10, 0x08, 0x20, 0x00, 0x82, 0x19, 0x10, 0x30, 0x00, 0x86,
  31.    0x0c, 0x30, 0x18, 0x00, 0x84, 0x04, 0x60, 0x0e, 0x00, 0xdc, 0x02, 0x80,
  32.    0x03, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00};
  33. #else
  34. #include "../BITMAPS/icon_bitmap"
  35. #endif
  36. #define BITMAPDEPTH 1
  37. #define SMALL 1
  38. #define OK 0
  39.  
  40. /* These are used as arguments to nearly every Xlib routine, so it saves 
  41.  * routine arguments to declare them global.  If there were 
  42.  * additional source files, they would be declared extern there. */
  43. Display *display;
  44. int screen;
  45.  
  46. void main(argc, argv)
  47. int argc;
  48. char **argv;
  49. {
  50.  Window win;
  51.  unsigned int width, height, x = 0, y = 0;  /* window size and position */
  52.  unsigned int border_width = 4; /* four pixels */
  53.  unsigned int display_width, display_height;
  54.  unsigned int icon_width, icon_height;
  55.  char *window_name = "basicwin";
  56.  char *icon_name = "";
  57.  Pixmap icon_pixmap;
  58.  XSizeHints size_hints;
  59.  XIconSize *size_list;
  60.  int count;
  61.  XEvent report;
  62.  GC gc;
  63.  XFontStruct *font_info;
  64.  char *display_name = NULL;
  65.  int window_size = 0; /* OK, or too SMALL to display contents */
  66.  Region region;  /* for exposure event processing */
  67.  XRectangle rectangle; /* for exposure event processing */
  68.  
  69.  /* connect to X server */
  70.  if( argc > 1 )
  71.    display_name = argv[ 1 ];
  72.  
  73.  if ( (display=XOpenDisplay(display_name)) == NULL )
  74.  {
  75.   (void) fprintf( stderr, "basicwin: cannot connect to X server %s\n", XDisplayName(display_name));
  76.   exit( errno );
  77.  }
  78.  
  79.  /* get screen size from display structure macro */
  80.  screen = DefaultScreen(display);
  81.  display_width = DisplayWidth(display, screen);
  82.  display_height = DisplayHeight(display, screen);
  83.  
  84.  /* note that x and y are 0, since the default position of the window
  85.   * is the top left corner of the root. This is fine since the window
  86.   * manager often allows the user to position the window before mapping it. */
  87.  
  88.  /* size window with enough room for text */
  89.  width = display_width/3, height = display_height/4;  
  90.  
  91.  /* create opaque window */
  92.  win = XCreateSimpleWindow(display, RootWindow(display,screen), x, y,
  93.                      width, height, border_width, BlackPixel(display,
  94.                      screen), WhitePixel(display,screen));
  95.  
  96.  /* Get available icon sizes from Window manager
  97.  if (XGetIconSizes(display, RootWindow(display,screen), &size_list, &count) == 0)
  98.  {
  99.   (void) fprintf( stderr, "Window manager didn't set icon sizes - using default.\n");
  100.   icon_width = 40;
  101.   icon_height = 40;
  102.  }
  103.  else
  104.  {
  105.   while (icon_width < size_list->min_width
  106.   icon_width = size_list->max_width;
  107.   icon_height = size_list->max_height;
  108.  }
  109.  all the icon size stuff commented out */
  110.  
  111.  /* Create pixmap of depth 1 (bitmap) for icon */
  112.  icon_pixmap = XCreateBitmapFromData(display, win, icon_bitmap_bits, icon_bitmap_width, icon_bitmap_height);
  113.  
  114.  /* Set resize hints */
  115.  size_hints.flags = PPosition | PSize | PMinSize;
  116.  size_hints.x = x;
  117.  size_hints.y = y;
  118.  size_hints.width = width;
  119.  size_hints.height = height;
  120.  size_hints.min_width = 300;
  121.  size_hints.min_height = 200;
  122.  
  123.  /* set Properties for window manager (always before mapping) */
  124.  XSetStandardProperties(display, win, window_name, icon_name, 
  125.      icon_pixmap, argv, argc, &size_hints);
  126.  
  127.  /* Select event types wanted */
  128.  XSelectInput(display, win, ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask);
  129.  
  130.  /* create region for exposure event processing */
  131.  region = XCreateRegion();
  132.  
  133.  load_font(&font_info);
  134.  
  135.  /* create GC for text and drawing */
  136.  getGC(win, &gc, font_info);
  137.  
  138.  /* Display window */
  139.  XMapWindow(display, win);
  140.  
  141.  /* get events, use first to display text and graphics */
  142.  while (1)  {
  143.   XNextEvent(display, &report);
  144.   switch  (report.type) {
  145.   case Expose:
  146.    /* if this is the last contiguous expose
  147.     * in a group, draw the window */
  148.    if (report.xexpose.count == 0) {
  149.     /* if window too small to use */
  150.     while (XCheckTypedEvent(display, Expose, &report));
  151.     if (window_size == SMALL)
  152.      TooSmall(win, gc, font_info);
  153.     else {
  154.      /* place text in window */
  155.      place_text(win, gc, font_info, width, height);
  156.  
  157.      /* place graphics in window, */
  158.      place_graphics(win, gc, width, height);
  159.     }
  160.    }
  161.    break;
  162.   case ConfigureNotify:
  163.    /* window has been resized, change width and
  164.     * height to send to place_text and place_graphics
  165.     * in next Expose */
  166.    width = report.xconfigure.width;
  167.    height = report.xconfigure.height;
  168.    if ((width < size_hints.min_width) || (height < size_hints.min_height))
  169.     window_size = SMALL;
  170.    else
  171.     window_size = OK;
  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.  
  189. getGC(win, gc, font_info)
  190. Window win;
  191. GC *gc;
  192. XFontStruct *font_info;
  193. {
  194.  unsigned long valuemask = 0; /* ignore XGCvalues and 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.  
  204.  /* Create default Graphics Context */
  205.  *gc = XCreateGC(display, win, valuemask, &values);
  206.  
  207.  /* specify font */
  208.  XSetFont(display, *gc, font_info->fid);
  209.  
  210.  /* specify black foreground since default may be white on white */
  211.  XSetForeground(display, *gc, BlackPixel(display,screen));
  212.  
  213.  /* set line attributes */
  214.  XSetLineAttributes(display, *gc, line_width, line_style, cap_style, join_style);
  215.  
  216.  /* set dashes to be line_width in length */
  217.  XSetDashes(display, *gc, dash_offset, dash_list, list_length);
  218. }
  219.  
  220. load_font(font_info)
  221. XFontStruct **font_info;
  222. {
  223.  char *fontname = "fixed";
  224.  
  225.  /* Access font */
  226.  if ((*font_info = XLoadQueryFont(display,fontname)) == NULL)
  227.  {
  228.   (void) fprintf( stderr, "Basic: Cannot open 9x15 font\n");
  229.   exit( -1 );
  230.  }
  231. }
  232.  
  233. place_text(win, gc, font_info, win_width, win_height)
  234. Window win;
  235. GC gc;
  236. XFontStruct *font_info;
  237. unsigned int win_width, win_height;
  238. {
  239.  int y = 20;  /* offset from corner of window*/
  240.  char *string1 = "Hi! I'm a window, who are you?";
  241.  char *string2 = "To terminate program; Press any key";
  242.  char *string3 = "or button while in this window.";
  243.  char *string4 = "Screen Dimensions:";
  244.  int len1, len2, len3, len4;
  245.  int width1, width2, width3;
  246.  char cd_height[50], cd_width[50], cd_depth[50];
  247.  int font_height;
  248.  int initial_y_offset, x_offset;
  249.  
  250.  
  251.  /* need length for both XTextWidth and XDrawString */
  252.  len1 = strlen(string1);
  253.  len2 = strlen(string2);
  254.  len3 = strlen(string3);
  255.  
  256.  /* get string widths for centering */
  257.  width1 = XTextWidth(font_info, string1, len1);
  258.  width2 = XTextWidth(font_info, string2, len2);
  259.  width3 = XTextWidth(font_info, string3, len3);
  260.  
  261.  /* output text, centered on each line */
  262.  XDrawString(display,win,gc,(win_width - width1)/2,y,string1,len1);
  263.  XDrawString(display,win,gc,(win_width - width2)/2, (int)(win_height - 35),string2,len2);
  264.  XDrawString(display,win,gc,(win_width - width3)/2, (int)(win_height - 15),string3,len3);
  265.  
  266.  /* copy numbers into string variables */
  267.  (void) sprintf(cd_height, " Height - %d pixels", DisplayHeight(display,screen));
  268.  (void) sprintf(cd_width, " Width  - %d pixels", DisplayWidth(display,screen));
  269.  (void) sprintf(cd_depth, " Depth  - %d plane(s)", DefaultDepth(display, screen));
  270.  
  271.  /* reuse these for same purpose */
  272.  len4 = strlen(string4);
  273.  len1 = strlen(cd_height);
  274.  len2 = strlen(cd_width);
  275.  len3 = strlen(cd_depth);
  276.  
  277.  font_height = font_info->max_bounds.ascent + font_info->max_bounds.descent;
  278.  
  279.  /* To center strings vertically, we place the first string
  280.   * so that the top of it is two font_heights above the center
  281.   * of the window.  Since the baseline of the string is what we
  282.   * need to locate for XDrawString, and the baseline is one
  283.   * font_info->max_bounds.ascent below the top of the chacter,
  284.   * the final offset of the origin up from the center of the 
  285.   * window is one font_height + one descent. */
  286.  
  287.  initial_y_offset = win_height/2 - font_height - font_info->max_bounds.descent;
  288.  x_offset = (int) win_width/4;
  289.  XDrawString(display, win, gc, x_offset, (int) initial_y_offset, string4,len4);
  290.  
  291.  XDrawString(display, win, gc, x_offset, (int) initial_y_offset + font_height,cd_height,len1);
  292.  XDrawString(display, win, gc, x_offset, (int) initial_y_offset + 2 * font_height,cd_width,len2);
  293.  XDrawString(display, win, gc, x_offset, (int) initial_y_offset + 3 * font_height,cd_depth,len3);
  294. }
  295.  
  296.  
  297. place_graphics(win, gc, window_width, window_height)
  298. Window win;
  299. GC gc;
  300. unsigned int window_width, window_height;
  301. {
  302.  int x, y;
  303.  int width, height;
  304.  
  305.  height = window_height/2;
  306.  width = 3 * window_width/4;
  307.  x = window_width/2 - width/2;  /* center */
  308.  y = window_height/2 - height/2;
  309.  XDrawRectangle(display, win, gc, x, y, width, height);
  310. }
  311.  
  312. TooSmall(win, gc, font_info)
  313. Window win;
  314. GC gc;
  315. XFontStruct *font_info;
  316. {
  317.  char *string1 = "Too Small";
  318.  int y_offset, x_offset;
  319.  
  320.  y_offset = font_info->max_bounds.ascent + 2;
  321.  x_offset = 2;
  322.  
  323.  /* output text, centered on each line */
  324.  XDrawString(display, win, gc, x_offset, y_offset, string1, strlen(string1));
  325. }
  326.  
  327.