home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sources / misc / 4185 < prev    next >
Encoding:
Text File  |  1992-12-14  |  57.5 KB  |  1,801 lines

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