home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume34 / imagemagick / part17 < prev    next >
Text File  |  1992-12-14  |  59KB  |  1,795 lines

  1. Newsgroups: comp.sources.misc
  2. From: cristy@eplrx7.es.duPont.com (John Cristy)
  3. Subject:  v34i045:  imagemagick - X11 image processing and display v2.2, Part17/26
  4. Message-ID: <1992Dec15.035604.22565@sparky.imd.sterling.com>
  5. X-Md4-Signature: fd311e5c661a81fa5295eaecb075f7c8
  6. Date: Tue, 15 Dec 1992 03:56:04 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: cristy@eplrx7.es.duPont.com (John Cristy)
  10. Posting-number: Volume 34, Issue 45
  11. Archive-name: imagemagick/part17
  12. Environment: UNIX, VMS, X11, SGI, DEC, Cray, Sun, Vax
  13.  
  14. #!/bin/sh
  15. # this is Part.17 (part 17 of a multipart archive)
  16. # do not concatenate these parts, unpack them in order with /bin/sh
  17. # file ImageMagick/X.c continued
  18. #
  19. if test ! -r _shar_seq_.tmp; then
  20.     echo 'Please unpack part 1 first!'
  21.     exit 1
  22. fi
  23. (read Scheck
  24.  if test "$Scheck" != 17; then
  25.     echo Please unpack part "$Scheck" next!
  26.     exit 1
  27.  else
  28.     exit 0
  29.  fi
  30. ) < _shar_seq_.tmp || exit 1
  31. if test ! -f _shar_wnt_.tmp; then
  32.     echo 'x - still skipping ImageMagick/X.c'
  33. else
  34. echo 'x - continuing file ImageMagick/X.c'
  35. sed 's/^X//' << 'SHAR_EOF' >> 'ImageMagick/X.c' &&
  36. X      if (colors == (XColor *) NULL)
  37. X        Error("unable to create colormap","memory allocation failed");
  38. X      p=colors;
  39. X      color.flags=DoRed | DoGreen | DoBlue;
  40. X      if (visual_info->class == StaticColor)
  41. X        for (i=0; i < image->colors; i++)
  42. X        {
  43. X          color.red=(unsigned short) (image->colormap[i].red << 8);
  44. X          color.green=(unsigned short) (image->colormap[i].green << 8);
  45. X          color.blue=(unsigned short) (image->colormap[i].blue << 8);
  46. X          status=XAllocColor(display,colormap,&color);
  47. X          if (status == 0)
  48. X            {
  49. X              colormap=XCopyColormapAndFree(display,colormap);
  50. X              XAllocColor(display,colormap,&color);
  51. X            }
  52. X          pixel_info->pixels[i]=color.pixel;
  53. X          *p++=color;
  54. X        }
  55. X      else
  56. X        for (i=0; i < image->colors; i++)
  57. X        {
  58. X          gray_value=Intensity(image->colormap[i]);
  59. X          color.red=(unsigned short) (gray_value << 8);
  60. X          color.green=(unsigned short) (gray_value << 8);
  61. X          color.blue=(unsigned short) (gray_value << 8);
  62. X          status=XAllocColor(display,colormap,&color);
  63. X          if (status == 0)
  64. X            {
  65. X              colormap=XCopyColormapAndFree(display,colormap);
  66. X              XAllocColor(display,colormap,&color);
  67. X            }
  68. X          pixel_info->pixels[i]=color.pixel;
  69. X          *p++=color;
  70. X        }
  71. X      break;
  72. X    }
  73. X    case GrayScale:
  74. X    case PseudoColor:
  75. X    {
  76. X      unsigned int
  77. X        colormap_type;
  78. X
  79. X      /*
  80. X        Define Standard Colormap for GrayScale or PseudoColor visual.
  81. X      */
  82. X      number_colors=image->colors;
  83. X      colors=(XColor *) malloc(visual_info->colormap_size*sizeof(XColor));
  84. X      if (colors == (XColor *) NULL)
  85. X        Error("unable to create colormap","memory allocation failed");
  86. X      /*
  87. X        Determine if image colors will "fit" into X server colormap.
  88. X      */
  89. X      colormap_type=resource_info->colormap;
  90. X      status=XAllocColorCells(display,colormap,False,
  91. X        (unsigned long *) NULL,0,pixel_info->pixels,image->colors);
  92. X      if (status != 0)
  93. X        colormap_type=PrivateColormap;
  94. X      if (colormap_type == SharedColormap)
  95. X        {
  96. X          DiversityPacket
  97. X            *diversity;
  98. X
  99. X          register RunlengthPacket
  100. X            *q;
  101. X
  102. X          unsigned short
  103. X            index;
  104. X
  105. X          /*
  106. X            Define Standard colormap for shared GrayScale or PseudoColor visual:
  107. X          */
  108. X          diversity=(DiversityPacket *)
  109. X            malloc(image->colors*sizeof(DiversityPacket));
  110. X          if (diversity == (DiversityPacket *) NULL)
  111. X            Error("unable to create colormap","memory allocation failed");
  112. X          for (i=0; i < image->colors; i++)
  113. X          {
  114. X            diversity[i].red=image->colormap[i].red;
  115. X            diversity[i].green=image->colormap[i].green;
  116. X            diversity[i].blue=image->colormap[i].blue;
  117. X            diversity[i].index=(unsigned short) i;
  118. X            diversity[i].count=0;
  119. X          }
  120. X          q=image->pixels;
  121. X          for (i=0; i < image->packets; i++)
  122. X          {
  123. X            diversity[q->index].count+=(q->length+1);
  124. X            q++;
  125. X          }
  126. X          /*
  127. X            Sort colors by decreasing intensity.
  128. X          */
  129. X          (void) qsort((void *) diversity,(int) image->colors,
  130. X            sizeof(DiversityPacket),IntensityCompare);
  131. X          for (i=0; i < image->colors; i+=Max(image->colors >> 4,2))
  132. X            diversity[i].count<<=4;  /* increase this colors popularity */
  133. X          diversity[image->colors-1].count<<=4;
  134. X          (void) qsort((void *) diversity,(int) image->colors,
  135. X            sizeof(DiversityPacket),PopularityCompare);
  136. X          /*
  137. X            Allocate colors.
  138. X          */
  139. X          p=colors;
  140. X          color.flags=DoRed | DoGreen | DoBlue;
  141. X          if (visual_info->class == PseudoColor)
  142. X            for (i=0; i < image->colors; i++)
  143. X            {
  144. X              index=diversity[i].index;
  145. X              color.red=(unsigned short) (image->colormap[index].red << 8);
  146. X              color.green=(unsigned short) (image->colormap[index].green << 8);
  147. X              color.blue=(unsigned short) (image->colormap[index].blue << 8);
  148. X              status=XAllocColor(display,colormap,&color);
  149. X              if (status == 0)
  150. X                break;
  151. X              pixel_info->pixels[index]=color.pixel;
  152. X              *p++=color;
  153. X            }
  154. X          else
  155. X            for (i=0; i < image->colors; i++)
  156. X            {
  157. X              index=diversity[i].index;
  158. X              gray_value=Intensity(image->colormap[index]);
  159. X              color.red=(unsigned short) (gray_value << 8);
  160. X              color.green=(unsigned short) (gray_value << 8);
  161. X              color.blue=(unsigned short) (gray_value << 8);
  162. X              status=XAllocColor(display,colormap,&color);
  163. X              if (status == 0)
  164. X                break;
  165. X              pixel_info->pixels[index]=color.pixel;
  166. X              *p++=color;
  167. X            }
  168. X          if (i < image->colors)
  169. X            {
  170. X              register int
  171. X                j;
  172. X
  173. X              XColor
  174. X                *server_colors;
  175. X
  176. X              /*
  177. X                Read X server colormap.
  178. X              */
  179. X              server_colors=(XColor *)
  180. X                malloc(visual_info->colormap_size*sizeof(XColor));
  181. X              if (server_colors == (XColor *) NULL)
  182. X                Error("unable to create colormap","memory allocation failed");
  183. X              for (j=0; j < visual_info->colormap_size; j++)
  184. X                server_colors[j].pixel=(unsigned long) j;
  185. X              XQueryColors(display,colormap,server_colors,
  186. X                (int) Min(visual_info->colormap_size,256));
  187. X              /*
  188. X                Select remaining colors from X server colormap.
  189. X              */
  190. X              if (visual_info->class == PseudoColor)
  191. X                for (; i < image->colors; i++)
  192. X                {
  193. X                  index=diversity[i].index;
  194. X                  color.red=(unsigned short) (image->colormap[index].red << 8);
  195. X                  color.green=(unsigned short)
  196. X                    (image->colormap[index].green << 8);
  197. X                  color.blue=(unsigned short)
  198. X                    (image->colormap[index].blue << 8);
  199. X                  XBestPixel(server_colors,(unsigned int)
  200. X                    Min(visual_info->colormap_size,256),&color);
  201. X                  XAllocColor(display,colormap,&server_colors[color.pixel]);
  202. X                  pixel_info->pixels[index]=color.pixel;
  203. X                  *p++=color;
  204. X                }
  205. X              else
  206. X                for (; i < image->colors; i++)
  207. X                {
  208. X                  index=diversity[i].index;
  209. X                  gray_value=Intensity(image->colormap[index]);
  210. X                  color.red=(unsigned short) (gray_value << 8);
  211. X                  color.green=(unsigned short) (gray_value << 8);
  212. X                  color.blue=(unsigned short) (gray_value << 8);
  213. X                  XBestPixel(server_colors,(unsigned int)
  214. X                    Min(visual_info->colormap_size,256),&color);
  215. X                  XAllocColor(display,colormap,&server_colors[color.pixel]);
  216. X                  pixel_info->pixels[index]=color.pixel;
  217. X                  *p++=color;
  218. X                }
  219. X              if (image->colors < visual_info->colormap_size)
  220. X                {
  221. X                  /*
  222. X                    Fill up colors array-- more choices for pen colors.
  223. X                  */
  224. X                  retain_colors=
  225. X                    Min(visual_info->colormap_size-image->colors,256);
  226. X                  for (i=0; i < retain_colors; i++)
  227. X                    *p++=server_colors[i];
  228. X                  number_colors+=retain_colors;
  229. X                }
  230. X              (void) free((char *) server_colors);
  231. X            }
  232. X          (void) free((char *) diversity);
  233. X          break;
  234. X        }
  235. X      /*
  236. X        Define Standard colormap for private GrayScale or PseudoColor visual.
  237. X      */
  238. X      if (status == 0)
  239. X        {
  240. X          /*
  241. X            Not enough colormap entries in the colormap-- Create a new colormap.
  242. X          */
  243. X          colormap=XCreateColormap(display,
  244. X            XRootWindow(display,visual_info->screen),visual_info->visual,
  245. X            AllocNone);
  246. X          if (colormap == (Colormap) NULL)
  247. X            Error("unable to create colormap",(char *) NULL);
  248. X          map_info->colormap=colormap;
  249. X          if (image->colors < visual_info->colormap_size)
  250. X            {
  251. X              /*
  252. X                Retain colors from the default colormap to help lessens the
  253. X                effects of colormap flashing.
  254. X              */
  255. X              retain_colors=Min(visual_info->colormap_size-image->colors,256);
  256. X              p=colors+image->colors;
  257. X              for (i=0; i < retain_colors; i++)
  258. X              {
  259. X                p->pixel=(unsigned long) i;
  260. X                p++;
  261. X              }
  262. X              XQueryColors(display,
  263. X                XDefaultColormap(display,visual_info->screen),
  264. X                colors+image->colors,(int) retain_colors);
  265. X              /*
  266. X                Transfer colors from default to private colormap.
  267. X              */
  268. X              XAllocColorCells(display,colormap,False,(unsigned long *) NULL,0,
  269. X                pixel_info->pixels,retain_colors);
  270. X              p=colors+image->colors;
  271. X              for (i=0; i < retain_colors; i++)
  272. X              {
  273. X                p->pixel=pixel_info->pixels[i];
  274. X                p++;
  275. X              }
  276. X              XStoreColors(display,colormap,colors+image->colors,retain_colors);
  277. X              number_colors+=retain_colors;
  278. X            }
  279. X          XAllocColorCells(display,colormap,False,(unsigned long *) NULL,0,
  280. X            pixel_info->pixels,image->colors);
  281. X        }
  282. X      /*
  283. X        Store the image colormap.
  284. X      */
  285. X      p=colors;
  286. X      color.flags=DoRed | DoGreen | DoBlue;
  287. X      if (visual_info->class == PseudoColor)
  288. X        for (i=0; i < image->colors; i++)
  289. X        {
  290. X          color.red=(unsigned short) (image->colormap[i].red << 8);
  291. X          color.green=(unsigned short) (image->colormap[i].green << 8);
  292. X          color.blue=(unsigned short) (image->colormap[i].blue << 8);
  293. X          color.pixel=pixel_info->pixels[i];
  294. X          *p++=color;
  295. X        }
  296. X      else
  297. X        for (i=0; i < image->colors; i++)
  298. X        {
  299. X          gray_value=Intensity(image->colormap[i]);
  300. X          color.red=(unsigned short) (gray_value << 8);
  301. X          color.green=(unsigned short) (gray_value << 8);
  302. X          color.blue=(unsigned short) (gray_value << 8);
  303. X          color.pixel=pixel_info->pixels[i];
  304. X          *p++=color;
  305. X        }
  306. X      XStoreColors(display,colormap,colors,image->colors);
  307. X      break;
  308. X    }
  309. X    case TrueColor:
  310. X    case DirectColor:
  311. X    default:
  312. X    {
  313. X      unsigned int
  314. X        linear_colormap;
  315. X
  316. X      /*
  317. X        Define Standard Colormap for TrueColor or DirectColor visual.
  318. X      */
  319. X      number_colors=(unsigned int) ((map_info->red_max*map_info->red_mult)+
  320. X        (map_info->green_max*map_info->green_mult)+
  321. X        (map_info->blue_max*map_info->blue_mult)+1);
  322. X      linear_colormap=
  323. X        ((map_info->red_max+1) == visual_info->colormap_size) &&
  324. X        ((map_info->green_max+1) == visual_info->colormap_size) &&
  325. X        ((map_info->blue_max+1) == visual_info->colormap_size);
  326. X      if (linear_colormap)
  327. X        number_colors=visual_info->colormap_size;
  328. X      /*
  329. X        Allocate color array.
  330. X      */
  331. X      colors=(XColor *) malloc(number_colors*sizeof(XColor));
  332. X      if (colors == (XColor *) NULL)
  333. X        Error("unable to create colormap","memory allocation failed");
  334. X      /*
  335. X        Initialize linear color ramp.
  336. X      */
  337. X      p=colors;
  338. X      color.flags=DoRed | DoGreen | DoBlue;
  339. X      if (linear_colormap)
  340. X        for (i=0; i < number_colors; i++)
  341. X        {
  342. X          color.blue=(unsigned short) 0;
  343. X          if (map_info->blue_max > 0)
  344. X            color.blue=(unsigned short)
  345. X              (((i % map_info->green_mult)*65535)/map_info->blue_max);
  346. X          color.green=color.blue;
  347. X          color.red=color.blue;
  348. X          color.pixel=XStandardPixel(map_info,color,16);
  349. X          *p++=color;
  350. X        }
  351. X      else
  352. X        for (i=0; i < number_colors; i++)
  353. X        {
  354. X          color.red=(unsigned short) 0;
  355. X          if (map_info->red_max > 0)
  356. X            color.red=(unsigned short)
  357. X              (((i/map_info->red_mult)*65535)/map_info->red_max);
  358. X          color.green=(unsigned short) 0;
  359. X          if (map_info->green_max > 0)
  360. X            color.green=(unsigned short) ((((i/map_info->green_mult) %
  361. X              (map_info->green_max+1))*65535)/map_info->green_max);
  362. X          color.blue=(unsigned short) 0;
  363. X          if (map_info->blue_max > 0)
  364. X            color.blue=(unsigned short)
  365. X              (((i % map_info->green_mult)*65535)/map_info->blue_max);
  366. X          color.pixel=XStandardPixel(map_info,color,16);
  367. X          *p++=color;
  368. X        }
  369. X      if ((visual_info->class == DirectColor) &&
  370. X          (colormap != XDefaultColormap(display,visual_info->screen)))
  371. X        XStoreColors(display,colormap,colors,number_colors);
  372. X      else
  373. X        for (i=0; i < number_colors; i++)
  374. X          XAllocColor(display,colormap,&colors[i]);
  375. X      break;
  376. X    }
  377. X  }
  378. X  if ((visual_info->class != DirectColor) && (visual_info->class != TrueColor))
  379. X    {
  380. X      /*
  381. X        Set background/border/foreground/pen pixels.
  382. X      */
  383. X      status=XAllocColor(display,colormap,&pixel_info->background_color);
  384. X      if (status == 0)
  385. X        XBestPixel(colors,number_colors,&pixel_info->background_color);
  386. X      status=XAllocColor(display,colormap,&pixel_info->foreground_color);
  387. X      if (status == 0)
  388. X        XBestPixel(colors,number_colors,&pixel_info->foreground_color);
  389. X      status=XAllocColor(display,colormap,&pixel_info->border_color);
  390. X      if (status == 0)
  391. X        XBestPixel(colors,number_colors,&pixel_info->border_color);
  392. X      for (i=0; i < MaxNumberPens; i++)
  393. X      {
  394. X        status=XAllocColor(display,colormap,&pixel_info->pen_color[i]);
  395. X        if (status == 0)
  396. X          XBestPixel(colors,number_colors,&pixel_info->pen_color[i]);
  397. X        pixel_info->pixels[image->colors+i]=pixel_info->pen_color[i].pixel;
  398. X      }
  399. X      pixel_info->colors=image->colors+MaxNumberPens;
  400. X    }
  401. X  (void) free((char *) colors);
  402. X  if (resource_info->debug)
  403. X    {
  404. X      (void) fprintf(stderr,"Standard Colormap:\n");
  405. X      (void) fprintf(stderr,"  colormap id: 0x%lx\n",map_info->colormap);
  406. X      (void) fprintf(stderr,"  red, green, blue max: %lu %lu %lu\n",
  407. X        map_info->red_max,map_info->green_max,map_info->blue_max);
  408. X      (void) fprintf(stderr,"  red, green, blue mult: %lu %lu %lu\n",
  409. X        map_info->red_mult,map_info->green_mult,map_info->blue_mult);
  410. X    }
  411. }
  412. X
  413. /*
  414. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  415. %                                                                             %
  416. %                                                                             %
  417. %                                                                             %
  418. %   X M a k e W i n d o w                                                     %
  419. %                                                                             %
  420. %                                                                             %
  421. %                                                                             %
  422. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  423. %
  424. %  Function XMakeWindow creates an X11 window.
  425. %
  426. %  The format of the XMakeWindow routine is:
  427. %
  428. %      XMakeWindow(display,parent,argv,argc,class_hint,manager_hints,property,
  429. %        window_info)
  430. %
  431. %  A description of each parameter follows:
  432. %
  433. %    o display: Specifies a connection to an X server; returned from
  434. %      XOpenDisplay.
  435. %
  436. %    o parent: Specifies the parent window_info.
  437. %
  438. %    o argv: Specifies the application's argument list.
  439. %
  440. %    o argc: Specifies the number of arguments.
  441. %
  442. %    o class_hint: Specifies a pointer to a X11 XClassHint structure.
  443. %
  444. %    o manager_hints: Specifies a pointer to a X11 XWMHints structure.
  445. %
  446. %    o property: A property to define on the window_info.
  447. %
  448. %    o window_info: Specifies a pointer to a X11 XWindowInfo structure.
  449. %
  450. %
  451. */
  452. void XMakeWindow(display,parent,argv,argc,class_hint,manager_hints,property,
  453. X  window_info)
  454. Display
  455. X  *display;
  456. X
  457. Window
  458. X  parent;
  459. X
  460. char
  461. X  **argv;
  462. X
  463. int
  464. X  argc;
  465. X
  466. XXClassHint
  467. X  *class_hint;
  468. X
  469. XXWMHints
  470. X  *manager_hints;
  471. X
  472. Atom
  473. X  property;
  474. X
  475. XXWindowInfo
  476. X  *window_info;
  477. {
  478. #define MinWindowSize  64
  479. X
  480. X  int
  481. X    status;
  482. X
  483. X  XSizeHints
  484. X    *size_hints;
  485. X
  486. X  XTextProperty
  487. X    icon_name,
  488. X    window_name;
  489. X
  490. X  /*
  491. X    Set window_info hints.
  492. X  */
  493. X  size_hints=XAllocSizeHints();
  494. X  if (size_hints == (XSizeHints *) NULL)
  495. X    Error("unable to make window_info","memory allocation failed");
  496. X  size_hints->flags=window_info->flags;
  497. X  size_hints->x=window_info->x;
  498. X  size_hints->y=window_info->y;
  499. X  size_hints->width=window_info->width;
  500. X  size_hints->height=window_info->height;
  501. X  if (!window_info->immutable)
  502. X    {
  503. X      /*
  504. X        Window size can be changed.
  505. X      */
  506. X      size_hints->min_width=window_info->min_width;
  507. X      size_hints->min_height=window_info->min_height;
  508. X      size_hints->flags|=PMinSize;
  509. X    }
  510. X  else
  511. X    {
  512. X      /*
  513. X        Window size cannot be changed.
  514. X      */
  515. X      size_hints->min_width=window_info->width;
  516. X      size_hints->min_height=window_info->height;
  517. X      size_hints->max_width=window_info->width;
  518. X      size_hints->max_height=window_info->height;
  519. X      size_hints->flags|=PMinSize | PMaxSize;
  520. X    }
  521. X  size_hints->flags|=PResizeInc;
  522. X  size_hints->width_inc=window_info->width_inc;
  523. X  size_hints->height_inc=window_info->height_inc;
  524. #ifndef PRE_R4_ICCCM
  525. X  size_hints->flags|=PBaseSize;
  526. X  size_hints->base_width=size_hints->min_width;
  527. X  size_hints->base_height=size_hints->min_height;
  528. #endif
  529. X  if (window_info->geometry != (char *) NULL)
  530. X    {
  531. X      char
  532. X        default_geometry[2048];
  533. X
  534. X      int
  535. X        flags,
  536. X        gravity;
  537. X
  538. X      /*
  539. X        User specified geometry.
  540. X      */
  541. X      (void) sprintf(default_geometry,"%dx%d\0",size_hints->width,
  542. X        size_hints->height);
  543. X      flags=XWMGeometry(display,window_info->screen,window_info->geometry,
  544. X        default_geometry,window_info->border_width,size_hints,&size_hints->x,
  545. X        &size_hints->y,&size_hints->width,&size_hints->height,&gravity);
  546. X      window_info->x=size_hints->x;
  547. X      window_info->y=size_hints->y;
  548. X      if ((flags & WidthValue) && (flags & HeightValue))
  549. X        size_hints->flags|=USSize;
  550. X      if ((flags & XValue) && (flags & YValue))
  551. X        size_hints->flags|=USPosition;
  552. #ifndef PRE_R4_ICCCM
  553. X      size_hints->win_gravity=gravity;
  554. X      size_hints->flags|=PWinGravity;
  555. #endif
  556. X    }
  557. X  if (window_info->id == (Window) NULL)
  558. X    window_info->id=XCreateWindow(display,parent,window_info->x,window_info->y,
  559. X      window_info->width,window_info->height,window_info->border_width,
  560. X      window_info->depth,InputOutput,window_info->visual_info->visual,
  561. X      CWBackingStore | CWBackPixel | CWBackPixmap | CWBitGravity |
  562. X      CWBorderPixel | CWColormap | CWCursor | CWDontPropagate | CWEventMask |
  563. X      CWOverrideRedirect | CWSaveUnder | CWWinGravity,&window_info->attributes);
  564. X  else
  565. X    {
  566. X      unsigned int
  567. X        mask;
  568. X
  569. X      XEvent
  570. X        discard_event;
  571. X
  572. X      XWindowChanges
  573. X        window_info_changes;
  574. X
  575. X      /*
  576. X        Window already exists;  change relevant attributes.
  577. X      */
  578. X      XChangeWindowAttributes(display,window_info->id,CWBackingStore |
  579. X        CWBackPixel | CWBackPixmap | CWBitGravity | CWBorderPixel |
  580. X        CWColormap | CWCursor | CWDontPropagate | CWEventMask |
  581. X        CWOverrideRedirect | CWSaveUnder | CWWinGravity,
  582. X        &window_info->attributes);
  583. X      XSync(display,False);
  584. X      while (XCheckTypedWindowEvent(display,window_info->id,ConfigureNotify,
  585. X        &discard_event));
  586. X      window_info_changes.x=window_info->x;
  587. X      window_info_changes.y=window_info->y;
  588. X      window_info_changes.width=window_info->width;
  589. X      window_info_changes.height=window_info->height;
  590. X      mask=CWWidth | CWHeight;
  591. X      if (window_info->flags & USPosition)
  592. X        mask|=CWX | CWY;
  593. X      XReconfigureWMWindow(display,window_info->id,window_info->screen,mask,
  594. X        &window_info_changes);
  595. X    }
  596. X  if (window_info->id == (Window) NULL)
  597. X    Error("unable to create window",window_info->name);
  598. X  status=XStringListToTextProperty(&window_info->name,1,&window_name);
  599. X  if (status == 0)
  600. X    Error("unable to create text property",window_info->name);
  601. X  if (window_info->icon_name == (char *) NULL)
  602. X    icon_name=window_name;
  603. X  else
  604. X    {
  605. X      status=XStringListToTextProperty(&window_info->icon_name,1,&icon_name);
  606. X      if (status == 0)
  607. X        Error("unable to create text property",window_info->icon_name);
  608. X    }
  609. X  if (window_info->icon_geometry != (char *) NULL)
  610. X    {
  611. X      int
  612. X        flags,
  613. X        gravity,
  614. X        height,
  615. X        width;
  616. X
  617. X      /*
  618. X        User specified icon geometry.
  619. X      */
  620. X      size_hints->flags|=USPosition;
  621. X      flags=XWMGeometry(display,window_info->screen,window_info->icon_geometry,
  622. X        (char *) NULL,0,size_hints,&manager_hints->icon_x,
  623. X        &manager_hints->icon_y,&width,&height,&gravity);
  624. X      if ((flags & XValue) && (flags & YValue))
  625. X        manager_hints->flags|=IconPositionHint;
  626. X    }
  627. X  XSetWMProperties(display,window_info->id,&window_name,&icon_name,argv,argc,
  628. X    size_hints,manager_hints,class_hint);
  629. X  XSetWMProtocols(display,window_info->id,&property,1);
  630. X  XFree((void *) size_hints);
  631. }
  632. X
  633. /*
  634. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  635. %                                                                             %
  636. %                                                                             %
  637. %                                                                             %
  638. %   X P o p U p A l e r t                                                     %
  639. %                                                                             %
  640. %                                                                             %
  641. %                                                                             %
  642. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  643. %
  644. %  Function XPopupAlert displays a popup window with an alert to the user.
  645. %  The function returns when the user presses a button or key.
  646. %
  647. %  The format of the XPopupAlert routine is:
  648. %
  649. %    XPopupAlert(display,popup_window,message,qualifier)
  650. %
  651. %  A description of each parameter follows:
  652. %
  653. %    o display: Specifies a connection to an X server;  returned from
  654. %      XOpenDisplay.
  655. %
  656. %    o popup_window: Specifies a pointer to a XWindowInfo structure.
  657. %
  658. %    o message: Specifies the message to display before terminating the
  659. %      program.
  660. %
  661. %    o qualifier: Specifies any qualifier to the message.
  662. %
  663. %
  664. */
  665. void XPopupAlert(display,popup_window,message,qualifier)
  666. Display
  667. X  *display;
  668. X
  669. XXWindowInfo
  670. X  *popup_window;
  671. X
  672. char
  673. X  *message,
  674. X  *qualifier;
  675. {
  676. X  char
  677. X    text[2048];
  678. X
  679. X  int
  680. X    i,
  681. X    state,
  682. X    x,
  683. X    y;
  684. X
  685. X  unsigned int
  686. X    mask;
  687. X
  688. X  Window
  689. X    window;
  690. X
  691. X  XEvent
  692. X    event;
  693. X
  694. X  /*
  695. X    Position and map popup window.
  696. X  */
  697. X  (void) sprintf(text,"%s\0",message);
  698. X  if (qualifier != (char *) NULL)
  699. X    {
  700. X      (void) strcat(text," (");
  701. X      (void) strcat(text,qualifier);
  702. X      (void) strcat(text,")");
  703. X    }
  704. X  popup_window->width=XTextWidth(popup_window->font_info,text,strlen(text))+
  705. X    4*popup_window->font_info->max_bounds.width;
  706. X  popup_window->height=
  707. X    popup_window->font_info->ascent+popup_window->font_info->descent+8;
  708. X  XQueryPointer(display,XRootWindow(display,popup_window->screen),&window,
  709. X    &window,&i,&i,&popup_window->x,&popup_window->y,&mask);
  710. X  x=Min(popup_window->x+2,XDisplayWidth(display,popup_window->screen)-
  711. X    popup_window->width);
  712. X  y=Min(popup_window->y+2,XDisplayHeight(display,popup_window->screen)-
  713. X    popup_window->height);
  714. X  XMoveResizeWindow(display,popup_window->id,x,y,popup_window->width,
  715. X    popup_window->height);
  716. X  XMapRaised(display,popup_window->id);
  717. X  XClearWindow(display,popup_window->id);
  718. X  /*
  719. X    Display message in popup window.
  720. X  */
  721. X  x=2*popup_window->font_info->max_bounds.width;
  722. X  y=popup_window->font_info->ascent+4;
  723. X  XDrawString(display,popup_window->id,popup_window->graphic_context,x,y,text,
  724. X    strlen(text));
  725. X  XBell(display,0);
  726. X  /*
  727. X    Wait for a key press.
  728. X  */
  729. X  state=DefaultState;
  730. X  do
  731. X  {
  732. X    /*
  733. X      Wait for next event.
  734. X    */
  735. X    XMaskEvent(display,ButtonPressMask | KeyPressMask | VisibilityChangeMask,
  736. X      &event);
  737. X    switch (event.type)
  738. X    {
  739. X      case ButtonPress:
  740. X      case KeyPress:
  741. X      {
  742. X        state|=ExitState;
  743. X        break;
  744. X      }
  745. X      case VisibilityNotify:
  746. X      {
  747. X        XMapRaised(display,popup_window->id);
  748. X        break;
  749. X      }
  750. X      default:
  751. X        break;
  752. X    }
  753. X  } while (!(state & ExitState));
  754. X  XWithdrawWindow(display,popup_window->id,popup_window->screen);
  755. }
  756. X
  757. /*
  758. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  759. %                                                                             %
  760. %                                                                             %
  761. %                                                                             %
  762. %   X P o p U p M e n u                                                       %
  763. %                                                                             %
  764. %                                                                             %
  765. %                                                                             %
  766. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  767. %
  768. %  Function XPopupMenu maps a menu and returns the command pointed to by the
  769. %  user when the button is released.
  770. %
  771. %  The format of the XPopupMenu routine is:
  772. %
  773. %    selection_number=XPopupMenu(display,popup_window,x,y,menu_title,
  774. %      menu_selections,number_selections,item)
  775. %
  776. %  A description of each parameter follows:
  777. %
  778. %    o selection_number: Specifies the number of the selection that the
  779. %      user choose.
  780. %
  781. %    o display: Specifies a connection to an X server;  returned from
  782. %      XOpenDisplay.
  783. %
  784. %    o popup_window: Specifies a pointer to a XWindowInfo structure.
  785. %
  786. %    o x: Specifies an unsigned integer representing the root offset in the
  787. %      x-direction.
  788. %
  789. %    o y: Specifies an unsigned integer representing the root offset in the
  790. %      x-direction.
  791. %
  792. %    o menu_title: Specifies a character string that describes the menu
  793. %      selections.
  794. %
  795. %    o menu_selections: Specifies a pointer to one or more strings that
  796. %      make up the choices in the menu.
  797. %
  798. %    o number_selections: Specifies the number of choices in the menu.
  799. %
  800. %    o item: Specifies a character array.  The item selected from the menu
  801. %      is returned here.
  802. %
  803. %
  804. */
  805. unsigned int XPopupMenu(display,popup_window,x,y,menu_title,menu_selections,
  806. X  number_selections,item)
  807. Display
  808. X  *display;
  809. X
  810. XXWindowInfo
  811. X  *popup_window;
  812. X
  813. int
  814. X  x,
  815. X  y;
  816. X
  817. char
  818. X  *menu_title,
  819. X  **menu_selections;
  820. X
  821. unsigned int
  822. X  number_selections;
  823. X
  824. char
  825. X  *item;
  826. {
  827. X  typedef struct _Selection
  828. X  {
  829. X    int
  830. X      id,
  831. X      x,
  832. X      y;
  833. X
  834. X    unsigned int
  835. X      height;
  836. X  } Selection;
  837. X
  838. X  int
  839. X    id,
  840. X    state;
  841. X
  842. X  Selection
  843. X    selection;
  844. X
  845. X  unsigned int
  846. X    height,
  847. X    title_height,
  848. X    width;
  849. X
  850. X  XEvent
  851. X    event;
  852. X
  853. X  /*
  854. X    Size and position menu window under current pointer location and map.
  855. X  */
  856. X  popup_window->width=
  857. X    XTextWidth(popup_window->font_info,menu_title,strlen(menu_title));
  858. X  for (selection.id=0; selection.id < number_selections; selection.id++)
  859. X  {
  860. X    width=XTextWidth(popup_window->font_info,menu_selections[selection.id],
  861. X      strlen(menu_selections[selection.id]));
  862. X    if (width > popup_window->width)
  863. X      popup_window->width=width;
  864. X  }
  865. X  popup_window->width+=4*popup_window->font_info->max_bounds.width;
  866. X  title_height=
  867. X    (popup_window->font_info->descent+popup_window->font_info->ascent)*2;
  868. X  popup_window->height=title_height+number_selections*
  869. X    (popup_window->font_info->ascent+popup_window->font_info->descent+4);
  870. X  width=popup_window->width+2*popup_window->border_width;
  871. X  popup_window->x=x-(int) width/2;
  872. X  if (popup_window->x < 0)
  873. X    popup_window->x=0;
  874. X  else
  875. X    if (popup_window->x > (XDisplayWidth(display,popup_window->screen)-width))
  876. X      popup_window->x=XDisplayWidth(display,popup_window->screen)-width;
  877. X  height=popup_window->height+2*popup_window->border_width;
  878. X  popup_window->y=y-(int) popup_window->border_width;
  879. X  if (popup_window->y < 0)
  880. X    popup_window->y=0;
  881. X  else
  882. X    if (popup_window->y > (XDisplayHeight(display,popup_window->screen)-height))
  883. X      popup_window->y=XDisplayHeight(display,popup_window->screen)-height;
  884. X  XMoveResizeWindow(display,popup_window->id,popup_window->x,popup_window->y,
  885. X    popup_window->width,popup_window->height);
  886. X  XMapRaised(display,popup_window->id);
  887. X  XClearWindow(display,popup_window->id);
  888. X  /*
  889. X    Draw title.
  890. X  */
  891. X  width=XTextWidth(popup_window->font_info,menu_title,strlen(menu_title));
  892. X  XDrawString(display,popup_window->id,popup_window->graphic_context,
  893. X    (int) (popup_window->width-width) >> 1,
  894. X    (int) (popup_window->font_info->ascent*3) >> 1,menu_title,
  895. X    strlen(menu_title));
  896. X  XDrawLine(display,popup_window->id,popup_window->graphic_context,0,
  897. X    title_height-2,popup_window->width,title_height-2);
  898. X  /*
  899. X    Draw menu selections.
  900. X  */
  901. X  selection.x=2*popup_window->font_info->max_bounds.width;
  902. X  selection.y=title_height+popup_window->font_info->ascent;
  903. X  selection.height=popup_window->font_info->ascent+
  904. X    popup_window->font_info->descent+4;
  905. X  for (selection.id=0; selection.id < number_selections; selection.id++)
  906. X  {
  907. X    XDrawString(display,popup_window->id,popup_window->graphic_context,
  908. X      selection.x,selection.y,menu_selections[selection.id],
  909. X      strlen(menu_selections[selection.id]));
  910. X    selection.y+=(int) selection.height;
  911. X  }
  912. X  /*
  913. X    Highlight menu as pointer moves;  return item on button release.
  914. X  */
  915. X  selection.id=(-1);
  916. X  state=DefaultState;
  917. X  do
  918. X  {
  919. X    /*
  920. X      Wait for next event.
  921. X    */
  922. X    XMaskEvent(display,ButtonPressMask | ButtonMotionMask | ButtonReleaseMask |
  923. X      EnterWindowMask | LeaveWindowMask | VisibilityChangeMask,&event);
  924. X    switch (event.type)
  925. X    {
  926. X      case ButtonPress:
  927. X        break;
  928. X      case ButtonRelease:
  929. X      {
  930. X        /*
  931. X          Exit menu.
  932. X        */
  933. X        *item='\0';
  934. X        state|=ExitState;
  935. X        break;
  936. X      }
  937. X      case EnterNotify:
  938. X      {
  939. X        if (event.xcrossing.window != popup_window->id)
  940. X          break;
  941. X        id=((event.xcrossing.y-title_height)/(int) selection.height);
  942. X        if ((id < 0) || (id >= number_selections))
  943. X          break;
  944. X        /*
  945. X          Highlight this selection.
  946. X        */
  947. X        selection.id=id;
  948. X        selection.y=title_height+
  949. X          popup_window->font_info->ascent+2+selection.id*selection.height;
  950. X        XFillRectangle(display,popup_window->id,popup_window->graphic_context,0,
  951. X          selection.y-popup_window->font_info->ascent-2,popup_window->width,
  952. X          selection.height);
  953. X        XDrawString(display,popup_window->id,popup_window->highlight_context,
  954. X          selection.x,selection.y,menu_selections[selection.id],
  955. X          strlen(menu_selections[selection.id]));
  956. X        break;
  957. X      }
  958. X      case LeaveNotify:
  959. X      {
  960. X        if (event.xcrossing.window != popup_window->id)
  961. X          break;
  962. X        if ((selection.id >= 0) && (selection.id < number_selections))
  963. X          {
  964. X            /*
  965. X              Unhighlight last selection.
  966. X            */
  967. X            XClearArea(display,popup_window->id,0,selection.y-
  968. X              popup_window->font_info->ascent-2,popup_window->width,
  969. X              selection.height,False);
  970. X            XDrawString(display,popup_window->id,popup_window->graphic_context,
  971. X              selection.x,selection.y,menu_selections[selection.id],
  972. X              strlen(menu_selections[selection.id]));
  973. X          }
  974. X        selection.id=(-1);
  975. X        break;
  976. X      }
  977. X      case MotionNotify:
  978. X      {
  979. X        if (event.xmotion.window != popup_window->id)
  980. X          break;
  981. X        /*
  982. X          Determine if pointer has moved to a new selection.
  983. X        */
  984. X        id=(event.xmotion.y-title_height)/(int) selection.height;
  985. X        if ((selection.id >= 0) && (selection.id < number_selections))
  986. X          {
  987. X            /*
  988. X              Unhighlight last selection.
  989. X            */
  990. X            if (id == selection.id)
  991. X              break;
  992. X            XClearArea(display,popup_window->id,0,selection.y-
  993. X              popup_window->font_info->ascent-2,popup_window->width,
  994. X              selection.height,False);
  995. X            XDrawString(display,popup_window->id,popup_window->graphic_context,
  996. X              selection.x,selection.y,menu_selections[selection.id],
  997. X              strlen(menu_selections[selection.id]));
  998. X          }
  999. X        selection.id=id;
  1000. X        if ((id < 0) || (id >= number_selections))
  1001. X          break;
  1002. X        /*
  1003. X          Highlight this selection.
  1004. X        */
  1005. X        selection.y=title_height+
  1006. X          popup_window->font_info->ascent+2+selection.id*selection.height;
  1007. X        XFillRectangle(display,popup_window->id,popup_window->graphic_context,0,
  1008. X          selection.y-popup_window->font_info->ascent-2,popup_window->width,
  1009. X          selection.height);
  1010. X        XDrawString(display,popup_window->id,popup_window->highlight_context,
  1011. X          selection.x,selection.y,menu_selections[selection.id],
  1012. X          strlen(menu_selections[selection.id]));
  1013. X        break;
  1014. X      }
  1015. X      case VisibilityNotify:
  1016. X      {
  1017. X        XMapRaised(display,popup_window->id);
  1018. X        break;
  1019. X      }
  1020. X      default:
  1021. X        break;
  1022. X    }
  1023. X  } while (!(state & ExitState));
  1024. X  XWithdrawWindow(display,popup_window->id,popup_window->screen);
  1025. X  if ((selection.id >= 0) && (selection.id < number_selections))
  1026. X    (void) strcpy(item,menu_selections[selection.id]);
  1027. X  return((unsigned int) selection.id);
  1028. }
  1029. X
  1030. /*
  1031. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1032. %                                                                             %
  1033. %                                                                             %
  1034. %                                                                             %
  1035. %   X P o p U p Q u e r y                                                     %
  1036. %                                                                             %
  1037. %                                                                             %
  1038. %                                                                             %
  1039. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1040. %
  1041. %  Function XPopupQuery displays a popup window with a query to the user.  The
  1042. %  user keys their reply and presses return to exit.  The typed text is
  1043. %  returned as the reply function parameter.
  1044. %
  1045. %  The format of the XPopupQuery routine is:
  1046. %
  1047. %    XPopupQuery(display,popup_window,query,reply)
  1048. %
  1049. %  A description of each parameter follows:
  1050. %
  1051. %    o display: Specifies a connection to an X server;  returned from
  1052. %      XOpenDisplay.
  1053. %
  1054. %    o popup_window: Specifies a pointer to a XWindowInfo structure.
  1055. %
  1056. %    o query: Specifies a pointer to the query to present to the user.
  1057. %
  1058. %    o reply: The response from the user is returned in this parameter.
  1059. %
  1060. %
  1061. */
  1062. void XPopupQuery(display,popup_window,query,reply)
  1063. Display
  1064. X  *display;
  1065. X
  1066. XXWindowInfo
  1067. X  *popup_window;
  1068. X
  1069. char
  1070. X  *query,
  1071. X  *reply;
  1072. {
  1073. X  char
  1074. X    *p,
  1075. X    text[2048];
  1076. X
  1077. X  GC
  1078. X    graphic_context;
  1079. X
  1080. X  int
  1081. X    i,
  1082. X    state,
  1083. X    x,
  1084. X    y;
  1085. X
  1086. X  unsigned int
  1087. X    height,
  1088. X    mask;
  1089. X
  1090. X  Window
  1091. X    window;
  1092. X
  1093. X  XEvent
  1094. X    event;
  1095. X
  1096. X  XFontStruct
  1097. X    *font_info;
  1098. X
  1099. X  /*
  1100. X    Position and map popup window.
  1101. X  */
  1102. X  (void) sprintf(text,"%s %s\0",query,reply);
  1103. X  popup_window->width=XTextWidth(popup_window->font_info,text,strlen(text))+
  1104. X    22*popup_window->font_info->max_bounds.width;
  1105. X  popup_window->height=
  1106. X    popup_window->font_info->ascent+popup_window->font_info->descent+8;
  1107. X  XQueryPointer(display,XRootWindow(display,popup_window->screen),&window,
  1108. X    &window,&i,&i,&popup_window->x,&popup_window->y,&mask);
  1109. X  x=Min(popup_window->x+2,XDisplayWidth(display,popup_window->screen)-
  1110. X    popup_window->width);
  1111. X  y=Min(popup_window->y+2,XDisplayHeight(display,popup_window->screen)-
  1112. X    popup_window->height);
  1113. X  XMoveResizeWindow(display,popup_window->id,x,y,popup_window->width,
  1114. X    popup_window->height);
  1115. X  XMapRaised(display,popup_window->id);
  1116. X  XClearWindow(display,popup_window->id);
  1117. X  /*
  1118. X    Display query in popup window.
  1119. X  */
  1120. X  font_info=popup_window->font_info;
  1121. X  graphic_context=popup_window->graphic_context;
  1122. X  x=2*font_info->max_bounds.width;
  1123. X  y=font_info->ascent+4;
  1124. X  height=font_info->ascent+font_info->descent;
  1125. X  XDrawString(display,popup_window->id,graphic_context,x,y,query,strlen(query));
  1126. X  x+=XTextWidth(font_info,query,strlen(query))+font_info->max_bounds.width;
  1127. X  /*
  1128. X    Display reply in popup window.
  1129. X  */
  1130. X  XDrawString(display,popup_window->id,graphic_context,x,y,reply,strlen(reply));
  1131. X  x+=XTextWidth(font_info,reply,strlen(reply));
  1132. X  /*
  1133. X    Begin editing the reply.
  1134. X  */
  1135. X  state=DefaultState;
  1136. X  p=reply+strlen(reply);
  1137. X  do
  1138. X  {
  1139. X    if ((x+font_info->max_bounds.width) >= popup_window->width)
  1140. X      {
  1141. X        /*
  1142. X          Resize popup window.
  1143. X        */
  1144. X        (void) sprintf(text,"%s %s\0",query,reply);
  1145. X        popup_window->width=
  1146. X          XTextWidth(popup_window->font_info,text,strlen(text))+
  1147. X          22*popup_window->font_info->max_bounds.width;
  1148. X        XResizeWindow(display,popup_window->id,popup_window->width,
  1149. X          popup_window->height);
  1150. X        /*
  1151. X          Display reply in popup window.
  1152. X        */
  1153. X        x=2*font_info->max_bounds.width;
  1154. X        XDrawString(display,popup_window->id,graphic_context,x,y,query,
  1155. X          strlen(query));
  1156. X        x+=XTextWidth(font_info,query,strlen(query))+
  1157. X          font_info->max_bounds.width;
  1158. X        XDrawString(display,popup_window->id,graphic_context,x,y,reply,
  1159. X          strlen(reply));
  1160. X        x+=XTextWidth(font_info,reply,strlen(reply));
  1161. X      }
  1162. X    /*
  1163. X      Display text cursor.
  1164. X    */
  1165. X    *p='\0';
  1166. X    XDrawString(display,popup_window->id,graphic_context,x,y,"_",1);
  1167. X    /*
  1168. X      Wait for next event.
  1169. X    */
  1170. X    XMaskEvent(display,ButtonPressMask | KeyPressMask | VisibilityChangeMask,
  1171. X      &event);
  1172. X    /*
  1173. X      Erase text cursor.
  1174. X    */
  1175. X    XClearArea(display,popup_window->id,x,y-font_info->ascent,
  1176. X      (unsigned int) font_info->max_bounds.width,height,False);
  1177. X    switch (event.type)
  1178. X    {
  1179. X      case ButtonPress:
  1180. X      {
  1181. X        Atom
  1182. X          type;
  1183. X
  1184. X        int
  1185. X          format,
  1186. X          status;
  1187. X
  1188. X        unsigned char
  1189. X          *data;
  1190. X
  1191. X        unsigned long
  1192. X          after,
  1193. X          length;
  1194. X
  1195. X        if ((event.xbutton.button == Button3) &&
  1196. X            (event.xbutton.state & Mod1Mask))
  1197. X          {
  1198. X            /* 
  1199. X              Convert Alt-Button3 to Button2.
  1200. X            */
  1201. X            event.xbutton.button=Button2;
  1202. X            event.xbutton.state&=(~Mod1Mask);
  1203. X          }
  1204. X        if (event.xbutton.button != Button2)
  1205. X          break;
  1206. X        /*
  1207. X          Obtain response from cut buffer.
  1208. X        */
  1209. X        status=XGetWindowProperty(display,XRootWindow(display,0),XA_CUT_BUFFER0,
  1210. X          0L,2047L,False,XA_STRING,&type,&format,&length,&after,&data);
  1211. X        if ((status != Success) || (type != XA_STRING) || (format == 32) ||
  1212. X            (length <= 0))
  1213. X          break;
  1214. X        /*
  1215. X          Append cut buffer to reply.
  1216. X        */
  1217. X        (void) strncpy(p,(char *) data,(int) length);
  1218. X        XFree((void *) data);
  1219. X        XDrawString(display,popup_window->id,graphic_context,x,y,p,
  1220. X          (int) length);
  1221. X        x+=XTextWidth(font_info,p,(unsigned int) length);
  1222. X        p+=length;
  1223. X        *p='\0';
  1224. X        break;
  1225. X      }
  1226. X      case KeyPress:
  1227. X      {
  1228. X        static char
  1229. X          command[2048];
  1230. X
  1231. X        static KeySym
  1232. X          key_symbol;
  1233. X
  1234. X        /*
  1235. X          Respond to a user key press.
  1236. X        */
  1237. X        *command='\0';
  1238. X        XLookupString((XKeyEvent *) &event.xkey,command,sizeof(command),
  1239. X          &key_symbol,(XComposeStatus *) NULL);
  1240. X        if (key_symbol == XK_Control_L)
  1241. X          {
  1242. X            state|=ControlState;
  1243. X            break;
  1244. X          }
  1245. X        if (state & ControlState)
  1246. X          switch (key_symbol)
  1247. X          {
  1248. X            case XK_u:
  1249. X            case XK_U:
  1250. X            {
  1251. X              key_symbol=XK_Delete;
  1252. X              break;
  1253. X            }
  1254. X            default:
  1255. X              break;
  1256. X          }
  1257. X        switch (key_symbol)
  1258. X        {
  1259. X          case XK_BackSpace:
  1260. X          {
  1261. X            /*
  1262. X              Erase one character.
  1263. X            */
  1264. X            if (p == reply)
  1265. X              break;
  1266. X            p--;
  1267. X            x-=XTextWidth(font_info,p,1);
  1268. X            XClearArea(display,popup_window->id,x,y-font_info->ascent,
  1269. X              (unsigned int) font_info->max_bounds.width,height,False);
  1270. X            break;
  1271. X          }
  1272. X          case XK_Escape:
  1273. X          {
  1274. X            /*
  1275. X              Return null response.
  1276. X            */
  1277. X            *reply='\0';
  1278. X            state|=ExitState;
  1279. X          }
  1280. X          case XK_Delete:
  1281. X          {
  1282. X            /*
  1283. X              Erase the entire line of text.
  1284. X            */
  1285. X            while (p != reply)
  1286. X            {
  1287. X              p--;
  1288. X              x-=XTextWidth(font_info,p,1);
  1289. X              XClearArea(display,popup_window->id,x,y-font_info->ascent,
  1290. X                (unsigned int) font_info->max_bounds.width,height,False);
  1291. X            }
  1292. X            break;
  1293. X          }
  1294. X          case XK_Return:
  1295. X          {
  1296. X            /*
  1297. X              Commit to current response.
  1298. X            */
  1299. X            state|=ExitState;
  1300. X            break;
  1301. X          }
  1302. X          default:
  1303. X          {
  1304. X            /*
  1305. X              Draw a single character on the popup window.
  1306. X            */
  1307. X            if (*command == '\0')
  1308. X              break;
  1309. X            *p=(*command);
  1310. X            XDrawString(display,popup_window->id,graphic_context,x,y,p,1);
  1311. X            x+=XTextWidth(font_info,p,1);
  1312. X            p++;
  1313. X            break;
  1314. X          }
  1315. X        }
  1316. X        break;
  1317. X      }
  1318. X      case KeyRelease:
  1319. X      {
  1320. X        static char
  1321. X          command[2048];
  1322. X
  1323. X        static KeySym
  1324. X          key_symbol;
  1325. X
  1326. X        /*
  1327. X          Respond to a user key release.
  1328. X        */
  1329. X        *command='\0';
  1330. X        XLookupString((XKeyEvent *) &event.xkey,command,sizeof(command),
  1331. X          &key_symbol,(XComposeStatus *) NULL);
  1332. X        if (key_symbol == XK_Control_L)
  1333. X          state&=(~ControlState);
  1334. X        break;
  1335. X      }
  1336. X      case VisibilityNotify:
  1337. X      {
  1338. X        XMapRaised(display,popup_window->id);
  1339. X        break;
  1340. X      }
  1341. X      default:
  1342. X        break;
  1343. X    }
  1344. X  } while (!(state & ExitState));
  1345. X  XWithdrawWindow(display,popup_window->id,popup_window->screen);
  1346. }
  1347. X
  1348. /*
  1349. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1350. %                                                                             %
  1351. %                                                                             %
  1352. %                                                                             %
  1353. %   X R e a d C o l o r m a p                                                 %
  1354. %                                                                             %
  1355. %                                                                             %
  1356. %                                                                             %
  1357. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1358. %
  1359. %  Function XReadColormap returns the red, green, and blue colormap of a window.
  1360. %  Additionally, the number of colors in the colormap is returned.
  1361. %
  1362. %  The format of the XReadColormap function is:
  1363. %
  1364. %      number_colors=XReadColormap(display,window_attributes,colors)
  1365. %
  1366. %  A description of each parameter follows:
  1367. %
  1368. %    o number_colors: XReadColormap returns the number of colors in the
  1369. %      colormap.
  1370. %
  1371. %    o display: Specifies a pointer to the Display structure;  returned from
  1372. %      XOpenDisplay.
  1373. %
  1374. %    o window_attributes: Specifies a pointer to the window attributes
  1375. %      structure;  returned from XGetWindowAttributes.
  1376. %
  1377. %    o colors: Specifies a an array of XColor structures.  The colormap
  1378. %      red, green, and blue are returned.
  1379. %
  1380. %
  1381. */
  1382. unsigned int XReadColormap(display,window_attributes,colors)
  1383. Display
  1384. X  *display;
  1385. X
  1386. XXWindowAttributes
  1387. X  *window_attributes;
  1388. X
  1389. XXColor
  1390. X  **colors;
  1391. {
  1392. X  unsigned int
  1393. X    number_colors;
  1394. X
  1395. X  register int
  1396. X    i;
  1397. X
  1398. X  if (window_attributes->colormap == (Colormap) NULL)
  1399. X    return(0);
  1400. X  number_colors=window_attributes->visual->map_entries;
  1401. X  *colors=(XColor *) malloc(number_colors*sizeof(XColor));
  1402. X  if (*colors == (XColor *) NULL)
  1403. X    {
  1404. X      Warning("unable to read colormap","memory allocation failed");
  1405. X      return(0);
  1406. X    }
  1407. X  if ((window_attributes->visual->class != DirectColor) &&
  1408. X      (window_attributes->visual->class != TrueColor))
  1409. X    for (i=0; i < number_colors; i++)
  1410. X    {
  1411. X      (*colors)[i].pixel=i;
  1412. X      (*colors)[i].pad=0;
  1413. X    }
  1414. X  else
  1415. X    {
  1416. X      unsigned long
  1417. X        blue,
  1418. X        blue_bit,
  1419. X        green,
  1420. X        green_bit,
  1421. X        red,
  1422. X        red_bit;
  1423. X
  1424. X      /*
  1425. X        DirectColor or TrueColor visual.
  1426. X      */
  1427. X      red=0;
  1428. X      green=0;
  1429. X      blue=0;
  1430. X      red_bit=window_attributes->visual->red_mask &
  1431. X        (~(window_attributes->visual->red_mask)+1);
  1432. X      green_bit=window_attributes->visual->green_mask &
  1433. X        (~(window_attributes->visual->green_mask)+1);
  1434. X      blue_bit=window_attributes->visual->blue_mask &
  1435. X        (~(window_attributes->visual->blue_mask)+1);
  1436. X      for (i=0; i < number_colors; i++)
  1437. X      {
  1438. X        (*colors)[i].pixel=red | green | blue;
  1439. X        (*colors)[i].pad=0;
  1440. X        red+=red_bit;
  1441. X        if (red > window_attributes->visual->red_mask)
  1442. X          red=0;
  1443. X        green+=green_bit;
  1444. X        if (green > window_attributes->visual->green_mask)
  1445. X          green=0;
  1446. X        blue+=blue_bit;
  1447. X        if (blue > window_attributes->visual->blue_mask)
  1448. X          blue=0;
  1449. X      }
  1450. X    }
  1451. X  XQueryColors(display,window_attributes->colormap,*colors,(int) number_colors);
  1452. X  return(number_colors);
  1453. }
  1454. X
  1455. /*
  1456. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1457. %                                                                             %
  1458. %                                                                             %
  1459. %                                                                             %
  1460. %   X R e f r e s h W i n d o w                                               %
  1461. %                                                                             %
  1462. %                                                                             %
  1463. %                                                                             %
  1464. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1465. %
  1466. %  Function XRefreshWindow refreshes an image in a X window.
  1467. %
  1468. %  The format of the XRefreshWindow routine is:
  1469. %
  1470. %      XRefreshWindow(display,window,event)
  1471. %
  1472. %  A description of each parameter follows:
  1473. %
  1474. %    o display: Specifies a connection to an X server;  returned from
  1475. %      XOpenDisplay.
  1476. %
  1477. %    o window: Specifies a pointer to a XWindowInfo structure.
  1478. %
  1479. %    o event: Specifies a pointer to a XEvent structure.  If it is NULL,
  1480. %      the entire image is refreshed.
  1481. %
  1482. %
  1483. */
  1484. void XRefreshWindow(display,window,event)
  1485. Display
  1486. X  *display;
  1487. X
  1488. XXWindowInfo
  1489. X  *window;
  1490. X
  1491. XXEvent
  1492. X  *event;
  1493. {
  1494. X  int
  1495. X    x,
  1496. X    y;
  1497. X
  1498. X  unsigned int
  1499. X    height,
  1500. X    width;
  1501. X
  1502. X  if (event != (XEvent *) NULL)
  1503. X    {
  1504. X      /*
  1505. X        Determine geometry from expose event.
  1506. X      */
  1507. X      x=event->xexpose.x;
  1508. X      y=event->xexpose.y;
  1509. X      width=event->xexpose.width;
  1510. X      height=event->xexpose.height;
  1511. X    }
  1512. X  else
  1513. X    {
  1514. X      XEvent
  1515. X        discard_event;
  1516. X
  1517. X      /*
  1518. X        Refresh entire window; discard outstanding expose events.
  1519. X      */
  1520. X      x=0;
  1521. X      y=0;
  1522. X      width=window->width;
  1523. X      height=window->height;
  1524. X      XSync(display,False);
  1525. X      while (XCheckTypedWindowEvent(display,window->id,Expose,&discard_event));
  1526. X    }
  1527. X  /*
  1528. X    Check boundary conditions.
  1529. X  */
  1530. X  if ((window->ximage->width-(x+window->x)) < width)
  1531. X    width=window->ximage->width-(x+window->x);
  1532. X  if ((window->ximage->height-(y+window->y)) < height)
  1533. X    height=window->ximage->height-(y+window->y);
  1534. X  /*
  1535. X    Refresh image.
  1536. X  */
  1537. X  if (window->pixmap != (Pixmap) NULL)
  1538. X    {
  1539. X      if (window->depth > 1)
  1540. X        XCopyArea(display,window->pixmap,window->id,window->graphic_context,
  1541. X          x+window->x,y+window->y,width,height,x,y);
  1542. X      else
  1543. X        XCopyPlane(display,window->pixmap,window->id,window->highlight_context,
  1544. X          x+window->x,y+window->y,width,height,x,y,1L);
  1545. X    }
  1546. X  else
  1547. X    XPutImage(display,window->id,window->graphic_context,window->ximage,
  1548. X      x+window->x,y+window->y,x,y,width,height);
  1549. }
  1550. X
  1551. /*
  1552. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1553. %                                                                             %
  1554. %                                                                             %
  1555. %                                                                             %
  1556. %   X S e l e c t W i n d o w                                                 %
  1557. %                                                                             %
  1558. %                                                                             %
  1559. %                                                                             %
  1560. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1561. %
  1562. %  Function XSelectWindow allows a user to select a window using the mouse.  If
  1563. %  the mouse moves, a clipping rectangle is drawn and the extents of the
  1564. %  rectangle is returned in the clip_info structure.
  1565. %
  1566. %  The format of the XSelectWindow function is:
  1567. %
  1568. %      target_window=XSelectWindow(display,clip_info)
  1569. %
  1570. %  A description of each parameter follows:
  1571. %
  1572. %    o window: XSelectWindow returns the window id.
  1573. %
  1574. %    o display: Specifies a pointer to the Display structure;  returned from
  1575. %      XOpenDisplay.
  1576. %
  1577. %    o clip_info: Specifies a pointer to a XRectangle structure.  It contains
  1578. %      the extents of any clipping rectangle.
  1579. %
  1580. %
  1581. */
  1582. Window XSelectWindow(display,clip_info)
  1583. Display
  1584. X  *display;
  1585. X
  1586. XXRectangle
  1587. X  *clip_info;
  1588. {
  1589. #define ClipRectangle(clip_info)  \
  1590. {  \
  1591. X  if ((clip_info->width*clip_info->height) >= MinimumClipArea)  \
  1592. X    {  \
  1593. X      XSetFunction(display,graphic_context,GXinvert);  \
  1594. X      XDrawRectangle(display,root_window,graphic_context,clip_info->x,  \
  1595. X        clip_info->y,clip_info->width-1,clip_info->height-1);  \
  1596. X      XSetFunction(display,graphic_context,GXcopy);  \
  1597. X    }  \
  1598. }
  1599. #define MinimumClipArea  (unsigned int) 9
  1600. X
  1601. X  Cursor
  1602. X    target_cursor;
  1603. X
  1604. X  GC
  1605. X    graphic_context;
  1606. X
  1607. X  int
  1608. X    presses,
  1609. X    status,
  1610. X    x_offset,
  1611. X    y_offset;
  1612. X
  1613. X  Window
  1614. X    root_window,
  1615. X    target_window;
  1616. X
  1617. X  XEvent
  1618. X    event;
  1619. X
  1620. X  XGCValues
  1621. X    graphic_context_value;
  1622. X
  1623. X  /*
  1624. X    Initialize graphic context.
  1625. X  */
  1626. X  root_window=XRootWindow(display,XDefaultScreen(display));
  1627. X  graphic_context_value.function=GXcopy;
  1628. X  graphic_context_value.line_width=2;
  1629. X  graphic_context_value.plane_mask=AllPlanes;
  1630. X  graphic_context_value.subwindow_mode=IncludeInferiors;
  1631. X  graphic_context=XCreateGC(display,root_window,GCFunction | GCLineWidth |
  1632. X    GCPlaneMask | GCSubwindowMode,&graphic_context_value);
  1633. X  if (graphic_context == (GC) NULL)
  1634. X    return(False);
  1635. X  /*
  1636. X    Make the target cursor.
  1637. X  */
  1638. X  target_cursor=XCreateFontCursor(display,XC_tcross);
  1639. X  /*
  1640. X    Grab the pointer using target cursor.
  1641. X  */
  1642. X  status=XGrabPointer(display,root_window,False,(unsigned int)
  1643. X    (ButtonPressMask | ButtonReleaseMask | ButtonMotionMask),GrabModeSync,
  1644. X    GrabModeAsync,root_window,target_cursor,CurrentTime);
  1645. X  if (status != GrabSuccess)
  1646. X    Error("unable to grab the mouse",(char *) NULL);
  1647. X  /*
  1648. X    Select a window.
  1649. X  */
  1650. X  target_window=(Window) NULL;
  1651. X  presses=0;
  1652. X  clip_info->width=0;
  1653. X  clip_info->height=0;
  1654. X  do
  1655. X  {
  1656. X    if ((clip_info->width*clip_info->height) >= MinimumClipArea)
  1657. X      ClipRectangle(clip_info);
  1658. X    /*
  1659. X      Allow another event.
  1660. X    */
  1661. X    XAllowEvents(display,SyncPointer,CurrentTime);
  1662. X    XWindowEvent(display,root_window,ButtonPressMask | ButtonReleaseMask |
  1663. X      ButtonMotionMask,&event);
  1664. X    ClipRectangle(clip_info);
  1665. X    switch (event.type)
  1666. X    {
  1667. X      case ButtonPress:
  1668. X      {
  1669. X        if (target_window == (Window) NULL)
  1670. X          {
  1671. X            target_window=event.xbutton.subwindow;
  1672. X            if (target_window == (Window) NULL)
  1673. X              target_window=root_window;
  1674. X          }
  1675. X        x_offset=event.xbutton.x_root;
  1676. X        y_offset=event.xbutton.y_root;
  1677. X        clip_info->x=x_offset;
  1678. X        clip_info->y=y_offset;
  1679. X        clip_info->width=0;
  1680. X        clip_info->height=0;
  1681. X        presses++;
  1682. X        break;
  1683. X      }
  1684. X      case ButtonRelease:
  1685. X      {
  1686. X        presses--;
  1687. X        break;
  1688. X      }
  1689. X      case MotionNotify:
  1690. X      {
  1691. X        /*
  1692. X          Discard pending button motion events.
  1693. X        */
  1694. X        while (XCheckMaskEvent(display,ButtonMotionMask,&event));
  1695. X        clip_info->x=event.xmotion.x;
  1696. X        clip_info->y=event.xmotion.y;
  1697. X        /*
  1698. X          Check boundary conditions.
  1699. X        */
  1700. X        if (clip_info->x < x_offset)
  1701. X          clip_info->width=(unsigned int) (x_offset-clip_info->x);
  1702. X        else
  1703. X          {
  1704. X            clip_info->width=(unsigned int) (clip_info->x-x_offset);
  1705. X            clip_info->x=x_offset;
  1706. X          }
  1707. X        if (clip_info->y < y_offset)
  1708. X          clip_info->height=(unsigned int) (y_offset-clip_info->y);
  1709. X        else
  1710. X          {
  1711. X            clip_info->height=(unsigned int) (clip_info->y-y_offset);
  1712. X            clip_info->y=y_offset;
  1713. X          }
  1714. X      }
  1715. X      default:
  1716. X        break;
  1717. X    }
  1718. X  }
  1719. X  while ((target_window == (Window) NULL) || (presses > 0));
  1720. X  XUngrabPointer(display,CurrentTime);
  1721. X  if ((clip_info->width*clip_info->height) < MinimumClipArea)
  1722. X    {
  1723. X      clip_info->width=0;
  1724. X      clip_info->height=0;
  1725. X    }
  1726. X  XFreeCursor(display,target_cursor);
  1727. X  XFreeGC(display,graphic_context);
  1728. X  return(target_window);
  1729. }
  1730. X
  1731. /*
  1732. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1733. %                                                                             %
  1734. %                                                                             %
  1735. %                                                                             %
  1736. %   X V i s u a l C l a s s N a m e                                           %
  1737. %                                                                             %
  1738. %                                                                             %
  1739. %                                                                             %
  1740. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1741. %
  1742. %  Function XVisualClassName returns the visual class name as a character
  1743. %  string.
  1744. %
  1745. %  The format of the XVisualClassName routine is:
  1746. %
  1747. %      visual_type=XVisualClassName(visual_info)
  1748. %
  1749. %  A description of each parameter follows:
  1750. %
  1751. %    o visual_type: XVisualClassName returns the visual class as a character
  1752. %      string.
  1753. %
  1754. %    o visual_info: Specifies a pointer to a X11 XVisualInfo structure;
  1755. %      returned from XGetVisualInfo.
  1756. %
  1757. %
  1758. */
  1759. char *XVisualClassName(visual_info)
  1760. XXVisualInfo
  1761. X  *visual_info;
  1762. {
  1763. X  if (visual_info == (XVisualInfo *) NULL)
  1764. X    return((char *) NULL);
  1765. X  switch (visual_info->class)
  1766. X  {
  1767. X    case StaticGray: return("StaticGray");
  1768. X    case GrayScale: return("GrayScale");
  1769. X    case StaticColor: return("StaticColor");
  1770. X    case PseudoColor: return("PseudoColor");
  1771. X    case TrueColor: return("TrueColor");
  1772. X    case DirectColor: return("DirectColor");
  1773. X  }
  1774. X  return("unknown visual class");
  1775. }
  1776. X
  1777. /*
  1778. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1779. %                                                                             %
  1780. %                                                                             %
  1781. %                                                                             %
  1782. %   X W i n d o w B y I D                                                     %
  1783. %                                                                             %
  1784. %                                                                             %
  1785. %                                                                             %
  1786. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1787. SHAR_EOF
  1788. true || echo 'restore of ImageMagick/X.c failed'
  1789. fi
  1790. echo 'End of  part 17'
  1791. echo 'File ImageMagick/X.c is continued in part 18'
  1792. echo 18 > _shar_seq_.tmp
  1793. exit 0
  1794. exit 0 # Just in case...
  1795.