home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume19 / xpt / part01 / table.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-28  |  9.8 KB  |  353 lines

  1. /**
  2.  *
  3.  * xpt -- An X Periodic Table
  4.  *
  5.  * table.c - Create the periodic table, and handle data requests
  6.  *
  7.  * Written bu Joel P. Lord 03/05/93
  8.  *
  9.  *    This software is available for free distribution,
  10.  * under the condition that this not be removed from the
  11.  * source code.
  12.  *
  13. **/
  14.  
  15. #include "xpt.h"
  16. #include <X11/Xaw/AsciiText.h>
  17.  
  18. void init_table()
  19. {
  20.   Widget elements[104];
  21.   Widget quit_button;
  22.   int n = 0, i = 0, j = 0, k = 0, horiz_offset = 0;
  23.   Arg args[20];
  24.   char title[3];
  25.   void element_choice(), quit();
  26.   XEvent tevent;
  27.   static char *table[] = {
  28. "H                                 He",
  29. "LiBe                    B C N O F Ne",
  30. "NaMg                    AlSiP S ClAr",
  31. "K CaScTiVaCrMnFeCoNiCuZnGaGeAsSeBrKr",
  32. "RbSrY ZrNbMoTcRuRhPdAgCdInSnSbTeI Xe",
  33. "CsBaLaHfTaW ReOsIrPtAuHgTlPbBiPoAtRn",
  34. "FrRaAc                              ",
  35. "                                    ",
  36. "        CePrNdPmSmEuGdTbDyHoErTmTbLu",
  37. "        ThPaU NpPuAmCmBkCfEsFmMdNoLr"
  38. };
  39.  
  40.   n = 0;
  41.   XtSetArg(args[n], XtNfromHoriz, NULL);
  42.   n++;
  43.   XtSetArg(args[n], XtNfromVert, NULL);
  44.   n++;
  45.   XtSetArg(args[n], XtNhorizDistance, 5);
  46.   n++;
  47.   XtSetArg(args[n], XtNvertDistance, 5);
  48.   n++;
  49.   XtSetArg(args[n], XtNwidth, 24);
  50.   n++;
  51.   XtSetArg(args[n], XtNheight, 32);
  52.   n++;
  53.   mbgpix = GetColors("NavyBlue", default_cmap, BP);
  54.   XtSetArg(args[n], XtNbackground, slate_grey);
  55.   n++;
  56.   XtSetArg(args[n], XtNforeground, mfgpix);
  57.   n++;
  58.   XtSetArg(args[n], XtNfont, mfontstruct);
  59.   n++;
  60.  
  61.   title[2] = 0;
  62.   for (j = 0; j < 10; j++)
  63.     {
  64.       for(i = 0; i < 36; i += 2)
  65.     {
  66.       title[0] = table[j][i];
  67.       title[1] = table[j][i+1];
  68.       if (title[0] != ' ')
  69.         {
  70.           elements[k] = XtCreateWidget(title, commandWidgetClass,
  71.                        MainW, args, n);
  72.           XtAddCallback(elements[k], XtNcallback, element_choice, k);
  73.           XtRealizeWidget(elements[k]);
  74.           XtManageChild(elements[k]);
  75.           k++;
  76.           horiz_offset = 0;
  77.           XtSetArg(args[0], XtNfromHoriz, elements[k - 1]);
  78.           XtSetArg(args[2], XtNhorizDistance, 0);
  79.         }
  80.       else
  81.         {
  82.           horiz_offset += 26;
  83.           XtSetArg(args[2], XtNhorizDistance, horiz_offset);
  84.         }
  85.     }
  86.       horiz_offset = 0;
  87.       XtSetArg(args[0], XtNfromHoriz, NULL);
  88.       XtSetArg(args[1], XtNfromVert, elements[k-1]);
  89.       XtSetArg(args[2], XtNhorizDistance, 5);
  90.       XtSetArg(args[3], XtNvertDistance, 0);
  91.     }
  92.  
  93.   XtSetArg(args[0], XtNfromHoriz, NULL);
  94.   XtSetArg(args[1], XtNfromVert, NULL);
  95.   XtSetArg(args[2], XtNhorizDistance, 160);
  96.   XtSetArg(args[3], XtNvertDistance, 5);
  97.   XtSetArg(args[4], XtNwidth, 49);
  98.   XtSetArg(args[5], XtNheight, 32);
  99.   XtSetArg(args[6], XtNbackground, red);
  100.   XtSetArg(args[7], XtNforeground, white);
  101.   XtSetArg(args[9], XtNborderColor, red);
  102.   
  103.   quit_button = XtCreateManagedWidget("Quit", commandWidgetClass, MainW,
  104.                       args, n + 1);
  105.   XtRealizeWidget(quit_button);
  106.   XtAddCallback(quit_button, XtNcallback, quit, 0);
  107. }
  108.  
  109. static destroy_info;
  110.  
  111. void element_choice(w, element, event)
  112. Widget w;
  113. int element;
  114. XEvent *event;
  115. {
  116.   Widget element_info, ok_button, help_button;
  117.   Arg args[20];
  118.   char *data_buf;
  119.   void ok(), help();
  120.  
  121.   destroy_info = 0;
  122.  
  123.   data_buf = (char *)malloc(1000);
  124.  
  125.   element_info = XtVaCreateManagedWidget("Element Info",formWidgetClass, MainW,
  126.                        XtNfromHoriz, NULL,
  127.                        XtNfromVert, NULL,
  128.                        XtNwidth, 200,
  129.                        XtNheight, 250,
  130.                        XtNvertDistance, 60,
  131.                        XtNhorizDistance, 240,
  132.                        XtNbackground, mbgpix,
  133.                        XtNforeground, mfgpix,
  134.                        XtNfont, mfontstruct,
  135.                        XtNborderColor, slate_grey, NULL);
  136.   display_info(element_info, element);
  137.  
  138.   ok_button = XtVaCreateManagedWidget("Done", commandWidgetClass, element_info,
  139.                     XtNfromHoriz, NULL,
  140.                     XtNfromVert, NULL,
  141.                     XtNwidth, 40,
  142.                     XtNheight, 32,
  143.                     XtNvertDistance, 210,
  144.                     XtNhorizDistance, 100,
  145.                     XtNbackground, slate_grey,
  146.                     XtNforeground, mfgpix,
  147.                     XtNfont, mfontstruct,
  148.                     XtNborderColor, mbgpix, NULL);
  149.  
  150.   XtAddCallback(ok_button, XtNcallback, ok, 0);
  151.  
  152.   help_button = XtVaCreateManagedWidget("Help", commandWidgetClass,
  153.                     element_info,
  154.                     XtNfromHoriz, NULL,
  155.                     XtNfromVert, NULL,
  156.                     XtNwidth, 40,
  157.                     XtNheight, 32,
  158.                     XtNvertDistance, 210,
  159.                     XtNhorizDistance, 60,
  160.                     XtNbackground, slate_grey,
  161.                     XtNforeground, mfgpix,
  162.                     XtNfont, mfontstruct,
  163.                     XtNborderColor, mbgpix, NULL);
  164.   XtAddCallback(help_button, XtNcallback, help, 0);
  165.  
  166.   while (!destroy_info && !done)
  167.     {
  168.       XtNextEvent(&theEvent);
  169.       XtDispatchEvent(&theEvent);
  170.       if (theEvent.type == Expose)
  171.     display_info(element_info, element);
  172.     }
  173.   XtDestroyWidget(element_info);
  174.   free(data_buf);
  175. }
  176.  
  177. void ok(w, client_data, call_data)
  178. Widget w;
  179. XtPointer client_data, call_data;
  180. {
  181.   destroy_info = 1;
  182. }
  183.  
  184. void quit(w, client_data, call_data)
  185. Widget w;
  186. XtPointer client_data;
  187. XtPointer call_data;
  188. {
  189.   done = 1;
  190. }
  191.  
  192. static unsigned destroy_help = 1;
  193. static Widget help_window;
  194.  
  195. void help(w, client_data, call_data)
  196. Widget w;
  197. XtPointer client_data, call_data;
  198. {
  199.   if (!destroy_help)
  200.     {
  201.       XtDestroyWidget(help_window);
  202.       destroy_help = 1;
  203.     }
  204.   else
  205.     {
  206.       destroy_help = 0;
  207.  
  208.       help_window = XtVaCreateManagedWidget("Element Info",formWidgetClass,
  209.                         MainW,
  210.                         XtNfromHoriz, NULL,
  211.                         XtNfromVert, NULL,
  212.                         XtNwidth, 200,
  213.                         XtNheight, 250,
  214.                         XtNvertDistance, 60,
  215.                         XtNhorizDistance, 40,
  216.                         XtNbackground, mbgpix,
  217.                         XtNforeground, mfgpix,
  218.                         XtNfont, mfontstruct,
  219.                         XtNborderColor, slate_grey, NULL);
  220.       display_help(help_window);
  221.       
  222.       while (!destroy_info && !done && !destroy_help)
  223.     {
  224.       XtNextEvent(&theEvent);
  225.       XtDispatchEvent(&theEvent);
  226.       if(theEvent.type == Expose)
  227.         display_help();
  228.     }
  229.       XtDestroyWidget(help_window);
  230.       destroy_help = 1;
  231.     }
  232. }
  233.  
  234. display_help(w)
  235. Widget w;
  236. {
  237.   XDrawString(p_disp, XtWindow(w), theGC, 10, 10 + mfontheight, "Number", 6);
  238.   XDrawString(p_disp, XtWindow(w), BigGC,
  239.           100 - XTextWidth(BigFont, "Sy", 2) / 2, 110 + mfontheight,
  240.           "Sy", 2);
  241.   XDrawString(p_disp, XtWindow(w), theGC, 
  242.           190 - XTextWidth(mfontstruct, "Mass", 4),
  243.           10 + mfontheight, "Mass", 4);
  244.   XDrawString(p_disp, XtWindow(w), theGC, 10, 60 + mfontheight, "BP", 2);
  245.   XDrawString(p_disp, XtWindow(w), theGC, 10, 100 + mfontheight, "MP", 2);
  246.   XDrawString(p_disp, XtWindow(w), theGC, 10, 140 + mfontheight, "Density", 7);
  247.  
  248.   XDrawString(p_disp, XtWindow(w), theGC,
  249.           185 - XTextWidth(mfontstruct, "Atomic", 6),
  250.           55 + mfontheight, "Atomic", 6);
  251.   XDrawString(p_disp, XtWindow(w), theGC,
  252.           190 - XTextWidth(mfontstruct, "Radius", 6),
  253.           55 + 2 * mfontheight, "Radius", 6);
  254.  
  255.   XDrawString(p_disp, XtWindow(w), theGC,
  256.           185 - XTextWidth(mfontstruct, "Covalent", 8),
  257.           95 + mfontheight, "Covalent", 8);
  258.   XDrawString(p_disp, XtWindow(w), theGC,
  259.           190 - XTextWidth(mfontstruct, "Radius", 6),
  260.           95 + 2 * mfontheight, "Radius", 6);
  261.  
  262.   XDrawString(p_disp, XtWindow(w), theGC,
  263.           185 - XTextWidth(mfontstruct, "Atomic", 6),
  264.           135 + mfontheight, "Atomic", 6);
  265.   XDrawString(p_disp, XtWindow(w), theGC,
  266.           190 - XTextWidth(mfontstruct, "Volume", 6),
  267.           135 + 2 * mfontheight, "Volume", 6);
  268.  
  269.   XDrawString(p_disp, XtWindow(w), theGC,
  270.           100 - XTextWidth(mfontstruct, "Name", 4) / 2,
  271.           190 + mfontheight, "Name", 4);
  272. }
  273.  
  274. display_info(w, element)
  275. Widget w;
  276. unsigned element;
  277. {
  278.   char *temp_buf, *temp2;
  279.  
  280.   temp_buf = (char *)malloc(100);
  281.  
  282.   sprintf(temp_buf, "%d", p_table[element].atomic_number);
  283.   XDrawString(p_disp, XtWindow(w), theGC, 10, 10 + mfontheight, temp_buf,
  284.         strlen(temp_buf));
  285.  
  286.   sprintf(temp_buf, "%s", p_table[element].symbol);
  287.   temp2 = strchr(temp_buf, ' ');
  288.   *temp2 = 0;
  289.   XDrawString(p_disp, XtWindow(w), BigGC,
  290.           100 - XTextWidth(BigFont, temp_buf, strlen(temp_buf)) / 2,
  291.           110 + mfontheight, temp_buf, strlen(temp_buf));
  292.  
  293.   sprintf(temp_buf, "%1.4lf", p_table[element].atomic_mass);
  294.   XDrawString(p_disp, XtWindow(w), theGC,
  295.           190 - XTextWidth(mfontstruct, temp_buf, strlen(temp_buf)),
  296.           10 + mfontheight, temp_buf, strlen(temp_buf));
  297.  
  298.   if (p_table[element].boiling_pt > 0.0)
  299.     sprintf(temp_buf, "%.2lf", p_table[element].boiling_pt);
  300.   else
  301.     strcpy(temp_buf, "--");
  302.   XDrawString(p_disp, XtWindow(w), theGC, 10, 60 + mfontheight, temp_buf,
  303.           strlen(temp_buf));
  304.  
  305.   if (p_table[element].melting_pt > 0.0)
  306.     sprintf(temp_buf, "%.2lf", p_table[element].melting_pt);
  307.   else
  308.     strcpy(temp_buf, "--");
  309.   XDrawString(p_disp, XtWindow(w), theGC, 10, 100 + mfontheight, temp_buf,
  310.           strlen(temp_buf));
  311.  
  312.  
  313.   if (p_table[element].density > 0.0)
  314.     sprintf(temp_buf, "%.2lf", p_table[element].density);
  315.   else
  316.     strcpy(temp_buf, "--");
  317.   XDrawString(p_disp, XtWindow(w), theGC, 10, 140 + mfontheight, temp_buf,
  318.           strlen(temp_buf));
  319.  
  320.   if (p_table[element].atomic_radius > 0.0)
  321.     sprintf(temp_buf, "%.2lf", p_table[element].atomic_radius);
  322.   else
  323.     strcpy(temp_buf, "--");
  324.   XDrawString(p_disp, XtWindow(w), theGC,
  325.           190 - XTextWidth(mfontstruct, temp_buf, strlen(temp_buf)),
  326.           60 + mfontheight, temp_buf, strlen(temp_buf));
  327.  
  328.   if (p_table[element].covalent_radius > 0.0)
  329.     sprintf(temp_buf, "%.2lf", p_table[element].covalent_radius);
  330.   else
  331.     strcpy(temp_buf, "--");
  332.   XDrawString(p_disp, XtWindow(w), theGC,
  333.           190 - XTextWidth(mfontstruct, temp_buf, strlen(temp_buf)),
  334.           100 + mfontheight, temp_buf, strlen(temp_buf));
  335.  
  336.   if (p_table[element].atomic_volume > 0.0)
  337.     sprintf(temp_buf, "%.2lf", p_table[element].atomic_volume);
  338.   else
  339.     strcpy(temp_buf, "--");
  340.   XDrawString(p_disp, XtWindow(w), theGC,
  341.           190 - XTextWidth(mfontstruct, temp_buf, strlen(temp_buf)),
  342.           140 + mfontheight, temp_buf, strlen(temp_buf));
  343.  
  344.   strcpy(temp_buf, p_table[element].name);
  345.   temp2 = strchr(temp_buf, ' ');
  346.   *temp2 = 0;
  347.   XDrawString(p_disp, XtWindow(w), theGC,
  348.           100 - XTextWidth(mfontstruct, temp_buf, strlen(temp_buf)) / 2,
  349.           190 + mfontheight, temp_buf, strlen(temp_buf));
  350.  
  351.   free(temp_buf);
  352. }
  353.