home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xcolorma.zip / xcolormap / xcolormap.c < prev   
C/C++ Source or Header  |  1992-01-11  |  5KB  |  175 lines

  1. /*
  2.  * Copyright 1991 John L. Cwikla
  3.  * 
  4.  * Permission to use, copy, modify, distribute, and sell this software
  5.  * and its documentation for any purpose is hereby granted without fee,
  6.  * provided that the above copyright notice appears in all copies and that
  7.  * both that copyright notice and this permission notice appear in
  8.  * supporting documentation, and that the name of John L. Cwikla or
  9.  * University of Illinois not be used in advertising or publicity
  10.  * pertaining to distribution of the software without specific, written
  11.  * prior permission.  John L. Cwikla and University of Illinois make no
  12.  * representations about the suitability of this software for any
  13.  * purpose.  It is provided "as is" without express or implied warranty.
  14.  *
  15.  * John L. Cwikla and University of Illinois disclaim all warranties with
  16.  * regard to this software, including all implied warranties of
  17.  * merchantability and fitness, in no event shall John L. Cwikla or
  18.  * University of Illinois be liable for any special, indirect or
  19.  * consequential damages or any damages whatsoever resulting from loss of
  20.  * use, data or profits, whether in an action of contract, negligence or
  21.  * other tortious action, arising out of or in connection with the use or
  22.  * performance of this software.
  23.  *
  24.  * Author:
  25.  *     John L. Cwikla
  26.  *     Materials Research Laboratory Center for Computation
  27.  *     University Of Illinois at Urbana-Champaign
  28.  *    104 S. Goodwin
  29.  *     Urbana, IL 61801
  30.  * 
  31.  *     cwikla@uimrl7.mrl.uiuc.edu
  32. */ 
  33.  
  34. #include <X11/IntrinsicP.h>
  35. #include <X11/Intrinsic.h>
  36. #include <X11/StringDefs.h>
  37. #include <X11/Shell.h>
  38. #include <X11/Core.h>
  39.  
  40. #include <stdio.h>
  41. #include <math.h>
  42.  
  43. #define APPNAME "XColormap"
  44.  
  45. #ifdef ULONG_NEEDED
  46. typedef unsigned long ulong;
  47. #endif
  48.  
  49. static Display *TheDisplay;
  50. static GC TheGC;
  51.  
  52. static ulong NumColors;
  53. static ulong Width = 10; /* Of the squares */
  54. static ulong Height = 10; /* Of the squares */
  55. static ulong Margin = 2; /* space between squares */
  56. static ulong Xcolor, Ycolor; /* Number of colors per direction in the matrix */
  57. static int TheScreenNo;
  58.  
  59. static void DrawSquare(Drawable d, int i)
  60. {
  61.   int row, col, x, y;
  62.  
  63.   row = i / Xcolor;
  64.   col = i % Xcolor;
  65.  
  66.   x = (Width + Margin) * col + Margin;
  67.   y = (Height + Margin) * row + Margin;
  68.  
  69.   XSetForeground(TheDisplay, TheGC, i);
  70.   XFillRectangle(TheDisplay, d, TheGC, x, y,
  71.     Width, Height);
  72. }
  73.  
  74. static void RedrawWindow(Drawable d)
  75. {
  76.   int i;
  77.   for(i=0;i<NumColors;i++)
  78.     DrawSquare(d, i);
  79. }
  80.  
  81. static void Redraw(Widget w)
  82. {
  83.   RedrawWindow(XtWindow(w));
  84. }
  85.  
  86. static void Resize(Widget wid)
  87. {
  88.   Width = (wid->core.width - Margin)/Xcolor - Margin; 
  89.   Height = (wid->core.height - Margin)/Xcolor - Margin;
  90.   XClearWindow(TheDisplay, XtWindow(wid));
  91.   Redraw(wid);
  92. }
  93.  
  94. static void QuitIt()
  95. {
  96.   printf("Have a nice day. --JLC\n");
  97.   exit(1);
  98. }
  99.  
  100. main(int argc, char *argv[])
  101. {
  102.   Widget colorwidget, toplevel;
  103.   XtAppContext app;
  104.   Arg warg[2];
  105.   int n;
  106.   ulong xWindowWidth, yWindowHeight;
  107.  
  108.   XtToolkitInitialize();
  109.   app = XtCreateApplicationContext();
  110.     
  111.   TheDisplay = XtOpenDisplay (app, NULL, APPNAME, APPNAME, 
  112.     NULL, 0, &argc, argv);
  113.  
  114.   if (!TheDisplay)
  115.   {
  116.     XtWarning ("%s: can't open display, exiting...", APPNAME);
  117.     exit (0);
  118.   }
  119.  
  120.   TheScreenNo = DefaultScreen(TheDisplay);
  121.   NumColors = XDisplayCells(TheDisplay, TheScreenNo);
  122.   Xcolor = (int)floor(sqrt(NumColors)); /* Best try at a square */
  123.   Ycolor = NumColors/Xcolor;
  124.  
  125.  
  126.   if ((argc == 2) && (!strncmp(argv[1], "-root", 5)))
  127.   {
  128.       Drawable TheWindow;
  129.       Pixmap ThePixmap;
  130.       xWindowWidth = DisplayWidth(TheDisplay, TheScreenNo);
  131.       yWindowHeight = DisplayHeight(TheDisplay, TheScreenNo);
  132.       Width = (xWindowWidth - Margin)/Xcolor - Margin;
  133.       Height = (yWindowHeight - Margin)/Xcolor - Margin;
  134.       printf("Display is %d x %d.\n", xWindowWidth, yWindowHeight);
  135.       TheWindow = RootWindow(TheDisplay, TheScreenNo);
  136.       ThePixmap = XCreatePixmap(TheDisplay, TheWindow, 
  137.     xWindowWidth, yWindowHeight, DefaultDepth(TheDisplay, TheScreenNo));
  138.       TheGC = XCreateGC(TheDisplay, TheWindow, NULL, 0);
  139.       RedrawWindow(ThePixmap);
  140.       XSetWindowBackgroundPixmap(TheDisplay, TheWindow, 
  141.     ThePixmap);
  142.       XClearWindow(TheDisplay, TheWindow);
  143.       while(XtAppPending(app))
  144.       {
  145.         XEvent event;
  146.         XtAppNextEvent(app, &event);
  147.         XtDispatchEvent(&event);
  148.       }
  149.       XFreeGC(TheDisplay, TheGC);
  150.       XFreePixmap(TheDisplay, ThePixmap);
  151.       exit(0);
  152.   }
  153.  
  154.   colorwidget = XtAppCreateShell (APPNAME, APPNAME,
  155.         applicationShellWidgetClass, TheDisplay, NULL, 0);
  156.   xWindowWidth = Xcolor * (Width + Margin) + Margin; /* Actual window width */
  157.   yWindowHeight = Ycolor * (Height + Margin) + Margin; /* Actual window height */
  158.  
  159.   n = 0;
  160.   XtSetArg(warg[n], XtNwidth, xWindowWidth); n++;
  161.   XtSetArg(warg[n], XtNheight, yWindowHeight); n++;
  162.   toplevel = XtCreateManagedWidget("TopLevel", widgetClass,
  163.     colorwidget, warg, n);
  164.  
  165.   XtRealizeWidget(colorwidget);
  166.   XtAddEventHandler(toplevel, ButtonPressMask, FALSE, QuitIt, NULL);
  167.   XtAddEventHandler(toplevel, ExposureMask, FALSE, Redraw, NULL);
  168.   XtAddEventHandler(toplevel, StructureNotifyMask, FALSE, Resize, NULL);
  169.  
  170.  
  171.   TheGC = XCreateGC(TheDisplay, XtWindow(toplevel), NULL, 0);
  172.  
  173.   XtAppMainLoop(app);
  174. }
  175.