home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / educatio / xcoral16.zip / PARSE.C < prev    next >
C/C++ Source or Header  |  1993-01-15  |  24KB  |  969 lines

  1. /*
  2. ** Copyright 1989, 1992 by Lionel Fournigault
  3. **
  4. ** Permission to use, copy, and distribute for non-commercial purposes,
  5. ** is hereby granted without fee, providing that the above copyright
  6. ** notice appear in all copies and that both the copyright notice and this
  7. ** permission notice appear in supporting documentation.
  8. ** The software may be modified for your own purposes, but modified versions
  9. ** may not be distributed.
  10. ** This software is provided "as is" without any expressed or implied warranty.
  11. **
  12. **
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <X11/Xlib.h>
  17. #include <X11/Xresource.h>
  18. #include <X11/cursorfont.h>
  19. #include <X11/Xutil.h>
  20. #include <X11/keysym.h>
  21. #include <string.h>
  22. #include <pwd.h>
  23. #include <malloc.h>
  24.  
  25. #include "config.h"
  26. #include "options.h"
  27. #include "text.h"
  28.  
  29. extern     Display *dpy;
  30. extern     void exit ();
  31. extern     char *getenv ();
  32. extern     void GetShadow ();
  33.  
  34. static XrmOptionDescRec opTable [] = {
  35.  
  36.     {"=",        "*geometry",    XrmoptionIsArg,        (caddr_t) NULL },
  37.     {"-d",        ".display",    XrmoptionSepArg,    (caddr_t) NULL },
  38.     {"-display",    ".display",    XrmoptionSepArg,    (caddr_t) NULL },
  39.     {"-fn",        "*font",    XrmoptionSepArg,    (caddr_t) NULL },
  40.     {"-font",    "*font",    XrmoptionSepArg,    (caddr_t) NULL },
  41.     {"-fg",        "*foreground",    XrmoptionSepArg,    (caddr_t) NULL },
  42.     {"-foreground",    "*foreground",    XrmoptionSepArg,    (caddr_t) NULL },
  43.     {"-bg",        "*background",    XrmoptionSepArg,    (caddr_t) NULL },
  44.     {"-background",    "*background",    XrmoptionSepArg,    (caddr_t) NULL },
  45.     {"-mfn",    "*mfont",    XrmoptionSepArg,    (caddr_t) NULL },
  46.     {"-mfont",    "*mfont",    XrmoptionSepArg,    (caddr_t) NULL },
  47.     {"-mfg",    "*mfg",        XrmoptionSepArg,    (caddr_t) NULL },
  48.     {"-mforeground","*mforeground",    XrmoptionSepArg,    (caddr_t) NULL },
  49.     {"-mbg",    "*mbg",        XrmoptionSepArg,    (caddr_t) NULL },
  50.     {"-mbackground","*mbackground",    XrmoptionSepArg,    (caddr_t) NULL },
  51.     {"-dbg",    "*dbg",        XrmoptionSepArg,    (caddr_t) NULL },
  52.     {"-dfg",    "*dfg",        XrmoptionSepArg,    (caddr_t) NULL },
  53. };
  54.  
  55. static     int opTableEntries = 17;
  56. static     XrmDatabase    commandlineDB, savecommandlineDB;
  57. static    XrmDatabase    rDB;
  58. static    Options        options;
  59. static    int        IsColor;
  60.  
  61. static void SetGeometry (), SetFont (), SetTextFG (), SetTextBG ();
  62. static void SetMenuFont (), SetMenuFG (), SetMenuBG (), SetDialogBG (), SetDialogFG ();
  63. static char *GetHomeDir ();
  64.  
  65. #define Message(str) fprintf(stderr, "%s\n", str )
  66.  
  67. /*
  68. **    Function name : ParseOpenDisp
  69. **
  70. **    Description : Parse la ligne de commande et ouvre le
  71. **        display. 
  72. **    Input : Le nombre d'arguments, la table des arguments.
  73. **    Ouput :
  74. */
  75. void ParseOpenDisp ( argc, argv )
  76. int *argc;
  77. register char *argv [];
  78. {
  79.     XrmValue value;
  80.     char *str_type [10];
  81.     char *displayname;
  82.     char buf [32];
  83.        GC gc;
  84.        XGCValues gcv;
  85.  
  86.     /*
  87.      * On peche les options de la ligne de commande    
  88.      */
  89.     XrmParseCommand ( &commandlineDB, opTable,
  90.         opTableEntries, argv [0], argc, argv );
  91.  
  92.     /*
  93.      * J'ai besoin des options de la ligne de commande pour plus tard.
  94.      * En fait, c'est a cause de la fonction XMerge... qui est destructrice
  95.      * Malheureusement il faut passer par un fichier intermediaire.
  96.      */
  97.     bzero ( buf, 32 );
  98.     (void) sprintf ( buf, "/tmp/xcoral_db%d", getuid() );
  99.     XrmPutFileDatabase ( commandlineDB, buf );
  100.     if ( (savecommandlineDB = XrmGetFileDatabase (  buf )) != 0 ) 
  101.         (void) unlink ( buf );
  102.  
  103.     /*
  104.      * Un nom de fichier peut etre...
  105.      */
  106.     if ( *argc == 2 )
  107.         if ( argv[1][0] == '-' )
  108.             Usage ();
  109.         else
  110.             (void) strcpy ( options.filename, argv[1] );
  111.     else 
  112.         (void) strncpy ( options.filename, "NoName", 6 );
  113.         
  114.     /*
  115.      * Avant d'ouvrir la connexion avec le serveur, on verifie
  116.      * si la commande positionne le display. Sinon si displayname == 0
  117.      * c'est la variable d'environnemt DISPLAY qui est utilisee.
  118.      */
  119.     displayname = 0;
  120.     if ( XrmGetResource ( commandlineDB, "xcoral.display",
  121.         "xcoral.display",    str_type, &value ) == True ) {
  122.             displayname = (char *) malloc ( (unsigned) (value.size + 1) );
  123.             bzero ( displayname, (int) (value.size + 1));
  124.             (void) strncpy ( displayname, value.addr, (int) value.size );
  125.     }
  126.     
  127.     if ( ! (dpy = XOpenDisplay ( displayname ))) {
  128.         (void) fprintf ( stderr, "%s : Cant'open display '%s'\n",
  129.             argv [0], XDisplayName ( displayname ));
  130.         (void) exit (1);
  131.     }
  132.        if ( displayname != 0 )
  133.         (void) free (displayname); 
  134.  
  135.     if ( DefaultDepth ( dpy, DefaultScreen ( dpy )) == 1 ) 
  136.         IsColor = False;
  137.     else
  138.         IsColor = True;
  139.  
  140. }
  141.  
  142.  
  143. /*
  144. **    Function name : GetUserDataBase
  145. **
  146. **    Description : Recherche des ressources dans la
  147. **        hierarchie habituelle.
  148. **    Input : 
  149. **    Ouput :
  150. */
  151. void GetUserDatabase ()
  152. {
  153.     XrmDatabase homeDB, serverDB, applicationDB;
  154.  
  155.     char     filename [128];    
  156.     char     *environment;
  157.     char     *classname = "COMMAND-NAME";
  158.     char     name [128];
  159.     
  160.     (void) strcpy ( name, APP_DEFAULT );
  161.     (void) strcat ( name, classname );
  162.     
  163.     applicationDB = XrmGetFileDatabase ( name );
  164.     (void) XrmMergeDatabases ( applicationDB, &rDB );
  165.     
  166.     if ( dpy -> xdefaults != NULL ) {
  167.         serverDB = XrmGetStringDatabase ( dpy -> xdefaults );
  168.     }
  169.     else {
  170.         (void) GetHomeDir ( filename );
  171.         (void) strcat ( filename, ".Xdefaults" );
  172.         serverDB = XrmGetFileDatabase ( filename );
  173.     }
  174.  
  175.     (void) XrmMergeDatabases ( serverDB, &rDB );
  176.     
  177.     if ( (environment = getenv ( "XENVIRONEMENT" )) == NULL ) {
  178.         environment = GetHomeDir ( filename );
  179.         (void) strcat ( environment, ".Xdefaults" );
  180.     }
  181.     
  182.     homeDB = XrmGetFileDatabase ( environment );
  183.     (void) XrmMergeDatabases ( homeDB, &rDB );
  184. }
  185.  
  186.  
  187. /*
  188. **    Function name : MergeOptions
  189. **
  190. **    Description :  On ajoute les arguments de la ligne
  191. *        de commande dans la base commune.
  192. **    Input : 
  193. **    Ouput :
  194. */
  195. void MergeOptions ()
  196. {
  197.     XColor xcolor;
  198.  
  199.     XrmMergeDatabases ( commandlineDB, &rDB );
  200.  
  201.     /*
  202.      * Enfin, on remplie la table des options.
  203.      */ 
  204.     (void) SetGeometry ();
  205.     (void) SetFont (); 
  206.     (void) SetTextFG ();
  207.     (void) SetTextBG ();
  208.     (void) SetMenuFont ();
  209.     (void) SetMenuFG ();
  210.     (void) SetMenuBG ();
  211.     (void) SetDialogFG ();
  212.     (void) SetDialogBG ();
  213.  
  214.     /*
  215.      * Calcul des top et bottom shadow
  216.      */
  217.     xcolor.pixel = (unsigned long) options.mbg;
  218.     XQueryColor ( dpy, DefaultColormap ( dpy, DefaultScreen ( dpy ) ),&xcolor ); 
  219.     GetShadow ( dpy, &xcolor, &options.menu_top_shadow, &options.menu_bottom_shadow );
  220.     
  221.     xcolor.pixel = (unsigned long) options.dbg;
  222.     XQueryColor ( dpy, DefaultColormap ( dpy, DefaultScreen ( dpy ) ), &xcolor ); 
  223.     GetShadow ( dpy, &xcolor, &options.dialog_top_shadow, &options.dialog_bottom_shadow );
  224.  
  225.     xcolor.pixel = (unsigned long) options.bg;
  226.     XQueryColor ( dpy, DefaultColormap ( dpy, DefaultScreen ( dpy ) ), &xcolor );
  227.     GetShadow ( dpy, &xcolor, &options.text_top_shadow, &options.text_bottom_shadow );
  228.  
  229.     /* A partir de la, les ressources sont valides et utilisables par le programme */
  230. }
  231.  
  232. /*
  233. **    Function name : SetDefaultBG
  234. **
  235. **    Description :  Lorqu' une couleur de fond n'est pas precisee,
  236. **        on utilise celle par defaut. Il faut verifier que cette
  237. **        couleur n'est pas identique a celle de devant ( si fg
  238. **        est une option de la ligne commande, ca peut etre le cas ).
  239. **    Input :  Foreground, background, les valeurs par default.
  240. **    Ouput :
  241. */
  242. SetDefaultBG ( bg, fg, bwbg, bwfg, colorbg, colorfg)
  243. unsigned long *bg; /* RETURN */
  244. unsigned long *fg;
  245. register char *bwbg, *bwfg, *colorbg, *colorfg; /*Default bw an color bg */
  246. {    
  247.     int result;
  248.  
  249.     if ( IsColor == False )  {
  250.         *bg = PixelValue ( dpy, bwbg, &result );
  251.         if ( *bg == *fg ) {
  252.             *bg = PixelValue ( dpy, bwfg, &result );
  253.         }
  254.     }
  255.     else { /* color */
  256.         *bg = PixelValue ( dpy, colorbg, &result );
  257.         if ( *bg == *fg ) {
  258.             *bg = PixelValue ( dpy, colorfg, &result );
  259.         }
  260.     }
  261. }
  262.  
  263.  
  264. /*
  265. **    Function name : SetDialogFG
  266. **
  267. **    Description : Calcul du foreground pour la fenetre 
  268. **        de controle.
  269. **    Input : 
  270. **    Ouput :
  271. */
  272. static void SetDialogFG ()
  273. {
  274.     XrmValue value;
  275.     char *str_type[20];
  276.     register char *buf = 0;
  277.     int result;
  278.  
  279.     if ( XrmGetResource ( rDB, "xcoral.dfg",
  280.         "xcoral.DialogForeground", str_type,&value ) == True ) {
  281.  
  282.         buf = (char *) malloc ( (unsigned) (value.size + 1) );
  283.         bzero ( buf, (int) (value.size + 1));
  284.         (void) strncpy ( buf, value.addr, (int) value.size );
  285.  
  286.         if ( IsColor == False ) {
  287.             if ( (strcmp ( buf, "black" ) != 0 ) && (strcmp ( buf, "white" ) != 0 )) {
  288.                 options.dfg = PixelValue ( dpy, BW_DIALOG_FG, &result );
  289.             }
  290.             else {
  291.                 options.dfg = PixelValue ( dpy, buf, &result );
  292.             }
  293.         }
  294.         else { /* Color Display */
  295.             options.dfg = PixelValue ( dpy, buf, &result );
  296.             if ( result == False ) {
  297.                 options.dfg = PixelValue ( dpy, COLOR_DIALOG_FG, &result );
  298.             }
  299.          }
  300.               if ( buf != 0 )
  301.             (void) free ( buf );
  302.     }
  303.     else  {
  304.         options.dfg = IsColor ? PixelValue ( dpy, COLOR_DIALOG_FG, &result ) :
  305.             PixelValue ( dpy, BW_DIALOG_FG, &result );
  306.     }
  307. }
  308.  
  309. /*
  310. **    Function name : SetDialogBG
  311. **
  312. **    Description : Calcul du background pour la fenetre de
  313. **        controle.
  314. **    Input : 
  315. **    Ouput :
  316. */
  317. static void SetDialogBG ()
  318. {
  319.     XrmValue value;
  320.     char *str_type[20];
  321.     register char *buf = 0;
  322.     int result;
  323.  
  324.     if ( XrmGetResource ( rDB, "xcoral.dbg",
  325.         "xcoral.DialogBackground", str_type,&value ) == True ) { 
  326.  
  327.         buf = (char *) malloc ( (unsigned) (value.size + 1) );
  328.         bzero ( buf, (int) (value.size + 1));
  329.         (void) strncpy ( buf, value.addr, (int) value.size );
  330.  
  331.         if ( IsColor == False ) {
  332.             if ( (strcmp ( buf, "black" ) != 0 ) && (strcmp ( buf, "white" ) != 0 )) 
  333.                 options.dbg = PixelValue ( dpy, BW_DIALOG_BG, &result );
  334.             else  
  335.                 options.dbg = PixelValue ( dpy, buf, &result );
  336.             if ( options.dbg == options.dfg ) {
  337.                 if ( XrmGetResource ( savecommandlineDB, "xcoral*dbg",
  338.                     "xcoral*DialogBackground", str_type,&value ) == True ) {
  339.                     if ( options.dbg == PixelValue ( dpy, BW_DIALOG_BG, &result ))
  340.                         options.dfg = PixelValue ( dpy, BW_DIALOG_FG, &result );
  341.                     else
  342.                         options.dfg = PixelValue ( dpy, BW_DIALOG_BG, &result );
  343.                 }
  344.                 else {
  345.                     if ( strcmp ( buf, BW_DIALOG_BG ) == 0 ) 
  346.                         options.dbg = PixelValue ( dpy, BW_DIALOG_FG, &result );
  347.                     else 
  348.                         options.dbg = PixelValue ( dpy, BW_DIALOG_BG, &result );
  349.                 }
  350.             }
  351.         }
  352.         else { /* Color */
  353.             options.dbg = PixelValue ( dpy, buf, &result );
  354.             if ( result == False )
  355.                 options.dbg = PixelValue ( dpy, COLOR_DIALOG_BG, &result );
  356.             if ( options.dbg == options.dfg ) {
  357.                 options.dbg = PixelValue ( dpy, COLOR_DIALOG_BG, &result );
  358.                 options.dfg = PixelValue ( dpy, COLOR_DIALOG_FG, &result );
  359.             }
  360.         }
  361.               if ( buf != 0 )
  362.             (void) free ( buf );
  363.     }
  364.     else 
  365.         SetDefaultBG ( &options.dbg, &options.dfg, BW_DIALOG_BG, BW_DIALOG_FG,
  366.             COLOR_DIALOG_BG, COLOR_DIALOG_FG );
  367. }
  368.  
  369. /*
  370. **    Function name : SetMenuFG
  371. **
  372. **    Description : Calcul du foreground pour les menus.
  373. **    Input : 
  374. **    Ouput :
  375. */
  376. static void SetMenuFG ()
  377. {
  378.     XrmValue value;
  379.     char *str_type[20];
  380.     register char *buf = 0;
  381.     int result;
  382.  
  383.     if ( XrmGetResource ( rDB, "xcoral.mfg",
  384.         "xcoral.mforeground", str_type,&value ) == True ) {
  385.  
  386.         buf = (char *) malloc ( (unsigned) (value.size + 1) );
  387.         bzero ( buf, (int) (value.size + 1));
  388.         (void) strncpy ( buf, value.addr, (int) value.size );
  389.  
  390.         if ( IsColor == False ) {
  391.             if ( (strcmp ( buf, "black" ) != 0 ) && (strcmp ( buf, "white" ) != 0 ))
  392.                 options.mfg = PixelValue ( dpy, BW_MENU_FG, &result );
  393.             else 
  394.                 options.mfg = PixelValue ( dpy, buf, &result );
  395.         }
  396.         else { /* Color Display */
  397.             options.mfg = PixelValue ( dpy, buf, &result );
  398.             if ( result == False ) 
  399.                 options.mfg = PixelValue ( dpy, COLOR_MENU_FG, &result );
  400.         }
  401.               if ( buf != 0 )
  402.             (void) free ( buf );
  403.     }
  404.     else 
  405.         options.mfg = IsColor ? PixelValue ( dpy, COLOR_MENU_FG, &result ) :
  406.             PixelValue ( dpy, BW_MENU_FG, &result );
  407. }
  408.  
  409. /*
  410. **    Function name : SetMenuBG
  411. **
  412. **    Description : Calcul du background pour les menus.
  413. **    Input : 
  414. **    Ouput :
  415. */
  416. static void SetMenuBG ()
  417. {
  418.     XrmValue value;
  419.     char *str_type[20];
  420.     register char *buf = 0;
  421.     int result;
  422.  
  423.     if ( XrmGetResource ( rDB, "xcoral.mbg",
  424.         "xcoral.mbackground", str_type,&value ) == True ) {
  425.         buf = (char *) malloc ( (unsigned) (value.size + 1) );
  426.         bzero ( buf, (int) (value.size + 1));
  427.         (void) strncpy ( buf, value.addr, (int) value.size );
  428.  
  429.         if ( IsColor == False ) {
  430.             if ( (strcmp ( buf, "black" ) != 0 ) && (strcmp ( buf, "white" ) != 0 )) 
  431.                 options.mbg = PixelValue ( dpy, BW_MENU_BG, &result );
  432.             else  
  433.                 options.mbg = PixelValue ( dpy, buf, &result );
  434.             if ( options.mbg == options.mfg ) {
  435.                 if ( XrmGetResource ( savecommandlineDB, "xcoral*mbg",
  436.                     "xcoral*MenuBackground", str_type,&value ) == True ) {
  437.                     if ( options.mbg == PixelValue ( dpy, BW_MENU_BG, &result ))
  438.                         options.mfg = PixelValue ( dpy, BW_MENU_FG, &result );
  439.                     else
  440.                         options.mfg = PixelValue ( dpy, BW_MENU_BG, &result );
  441.                 }
  442.                 else {
  443.                     if ( strcmp ( buf, BW_MENU_BG ) == 0 ) 
  444.                         options.bg = PixelValue ( dpy, BW_MENU_FG, &result );
  445.                     else 
  446.                         options.bg = PixelValue ( dpy, BW_MENU_BG, &result );
  447.                 }
  448.             }
  449.         }
  450.         else { /* Color */
  451.             options.mbg = PixelValue ( dpy, buf, &result );
  452.             if ( result == False )
  453.                 options.mbg = PixelValue ( dpy, COLOR_MENU_BG, &result );
  454.             if ( options.mbg == options.mfg ) {
  455.                 options.mbg = PixelValue ( dpy, COLOR_MENU_BG, &result );
  456.                 options.mfg = PixelValue ( dpy, COLOR_MENU_FG, &result );
  457.             }
  458.         }
  459.               if ( buf != 0 )
  460.             (void) free ( buf );
  461.     }
  462.     else 
  463.         SetDefaultBG ( &options.mbg, &options.mfg, BW_MENU_BG, BW_MENU_FG,
  464.             COLOR_MENU_BG, COLOR_MENU_FG );
  465. }
  466.  
  467. /*
  468. **    Function name : SetTextFG
  469. **
  470. **    Description : Calcul du foreground pour les fenetres de texte.
  471. **    Input : 
  472. **    Ouput :
  473. */
  474. static void SetTextFG ()
  475. {
  476.     XrmValue value;
  477.     char *str_type[20];
  478.     register char *buf = 0;
  479.     int result;
  480.  
  481.     if ( XrmGetResource ( rDB, "xcoral.foreground",
  482.         "xcoral.Foreground", str_type,&value ) == True ) {
  483.  
  484.         buf = (char *) malloc ( (unsigned) (value.size + 1) );
  485.         bzero ( buf, (int) (value.size + 1));
  486.         (void) strncpy ( buf, value.addr, (int) value.size );
  487.  
  488.         if ( IsColor == False ) {
  489.             if ( (strcmp ( buf, "black" ) != 0 ) && (strcmp ( buf, "white" ) != 0 ))
  490.                 options.fg = PixelValue ( dpy, BW_TEXT_FG, &result );
  491.             else 
  492.                 options.fg = PixelValue ( dpy, buf, &result );
  493.         }
  494.         else { /* Color Display */
  495.             options.fg = PixelValue ( dpy, buf, &result );
  496.             if ( result == False ) {
  497.                 options.fg = PixelValue ( dpy, COLOR_TEXT_FG, &result );
  498.             }
  499.         }
  500.               if ( buf != 0 )
  501.             (void) free ( buf );
  502.     }
  503.     else {
  504.         options.fg = IsColor ? PixelValue ( dpy, COLOR_TEXT_FG, &result ) :
  505.             PixelValue ( dpy, BW_TEXT_FG, &result );
  506.     }
  507. }
  508.  
  509. /*
  510. **    Function name : SetTextBG
  511. **
  512. **    Description : Calcul du background pour les fenetres de texte.
  513. **    Input : 
  514. **    Ouput :
  515. */
  516. static void SetTextBG ()
  517. {
  518.     XrmValue value;
  519.     char *str_type[20];
  520.     register char *buf = 0;
  521.     XFontStruct *LoadFont ();
  522.     int result;
  523.  
  524.     /* Le Foreground est deja connu */
  525.  
  526.     if ( XrmGetResource ( rDB, "xcoral.background",
  527.         "xcoral.Background", str_type,&value ) == True ) {
  528.         /* 
  529.          * Le foreground est defini dans les resources ou dans
  530.          * la ligne de commande
  531.          */
  532.         buf = (char *) malloc ( (unsigned) (value.size + 1) );
  533.         bzero ( buf, (int) (value.size + 1));
  534.         (void) strncpy ( buf, value.addr, (int) value.size );
  535.  
  536.         if ( IsColor == False ) {
  537.             if ( (strcmp ( buf, "black" ) != 0 ) && (strcmp ( buf, "white" ) != 0 )) 
  538.                 options.bg = PixelValue ( dpy, BW_TEXT_BG, &result );
  539.             else  
  540.                 options.bg = PixelValue ( dpy, buf, &result );
  541.             if ( options.bg == options.fg ) {
  542.                 if ( XrmGetResource ( savecommandlineDB, "xcoral*background",
  543.                     "xcoral*Background", str_type,&value ) == True ) {
  544.                     if ( options.bg == PixelValue ( dpy, BW_TEXT_BG, &result ))
  545.                         options.fg = PixelValue ( dpy, BW_TEXT_FG, &result );
  546.                     else
  547.                         options.fg = PixelValue ( dpy, BW_TEXT_BG, &result );
  548.                 }
  549.                 else {
  550.                     if ( strcmp ( buf, BW_TEXT_BG ) == 0 ) 
  551.                         options.bg = PixelValue ( dpy, BW_TEXT_FG, &result );
  552.                     else 
  553.                         options.bg = PixelValue ( dpy, BW_TEXT_BG, &result );
  554.                 }
  555.             }
  556.         }
  557.         else { /* Color */
  558.             options.bg = PixelValue ( dpy, buf, &result );
  559.             if ( result == False )
  560.                 options.bg = PixelValue ( dpy, COLOR_TEXT_BG, &result );
  561.             if ( options.bg == options.fg ) {
  562.                 options.bg = PixelValue ( dpy, COLOR_TEXT_BG, &result );
  563.                 options.fg = PixelValue ( dpy, COLOR_TEXT_FG, &result );
  564.             }
  565.         }
  566.               if ( buf != 0 )
  567.             (void) free ( buf );
  568.     }
  569.     else {
  570.         SetDefaultBG ( &options.bg, &options.fg, BW_TEXT_BG, BW_TEXT_FG,
  571.             COLOR_TEXT_BG, COLOR_TEXT_FG );
  572.     }
  573. }
  574.  
  575. /*
  576. **    Function name : SetMenuFont
  577. **
  578. **    Description : La fonte pour les menus.
  579. **    Input : 
  580. **    Ouput :
  581. */
  582. static void SetMenuFont ()
  583. {
  584.     XrmValue value;
  585.     char *str_type[20];
  586.     register char *buf = 0;
  587.     XFontStruct *LoadFont ();
  588.  
  589.     if ( XrmGetResource ( rDB, "xcoral.mfont",
  590.         "xcoral.Mfont", str_type,&value ) == True ) {
  591.  
  592.         buf = (char *) malloc ( (unsigned) (value.size + 1) );
  593.         bzero ( buf, (int) (value.size + 1));
  594.         (void) strncpy ( buf, value.addr, (int) value.size );
  595.  
  596.         options.menu_font = LoadFont ( dpy, buf );
  597.               if ( buf != 0 )
  598.             (void) free ( buf );
  599.     }
  600.     else {
  601.         options.menu_font = LoadFont ( dpy, MENU_FONT );
  602.     }
  603. }
  604.  
  605.  
  606. /*
  607. **    Function name : SetFont
  608. **
  609. **    Description : La fonte pour les fenetres texte.
  610. **    Input : 
  611. **    Ouput :
  612. */
  613. static void SetFont ()
  614. {
  615.     XrmValue value;
  616.     char *str_type[20];
  617.     register char *buf = 0;
  618.     XFontStruct *LoadFont ();
  619.  
  620.     if ( XrmGetResource ( rDB, "xcoral.font",
  621.         "xcoral.Font", str_type,&value ) == True ) {
  622.         /*
  623.          * Ya des resssources
  624.          */
  625.         buf = (char *) malloc ( (unsigned) (value.size + 1) );
  626.         bzero ( buf, (int) (value.size + 1));
  627.         (void) strncpy ( buf, value.addr, (int) value.size );
  628.         options.text_font = LoadFont ( dpy, buf );
  629.               if ( buf != 0 )
  630.             (void) free (buf);
  631.     }
  632.     else {
  633.         options.text_font = LoadFont ( dpy, TEXT_FONT );
  634.     }
  635. }
  636.  
  637.  
  638. /*
  639. **    Function name : SetGeometry
  640. **
  641. **    Description : Initialisation de la geometrie de 
  642. **        la feneter printcipale.
  643. **    Input : 
  644. **    Ouput :
  645. */
  646. static void SetGeometry ()
  647. {
  648.     XrmValue value;
  649.     char *str_type[20];
  650.     register char *buf = 0;
  651.     int x, y, width, height;
  652.     long flags;
  653.  
  654.     if ( XrmGetResource ( rDB, "xcoral.geometry",
  655.         "xcoral.Geometry", str_type,&value ) == True ) {
  656.         /*
  657.          * Ya des resssources
  658.          */
  659.         buf = (char *) malloc ( (unsigned) (value.size + 1) );
  660.         (void) strncpy ( buf, value.addr, (int) value.size ); 
  661.  
  662.         flags = XParseGeometry ( buf,  &x, &y, &width, &height );
  663.               if ( buf != 0 )
  664.             (void) free ( buf );
  665.  
  666.         if (!((WidthValue | HeightValue) & flags) )
  667.             Usage ();
  668.  
  669.         if ( XValue & flags ) {
  670.             if ( XNegative & flags )
  671.                 x = DisplayWidth ( dpy, DefaultScreen ( dpy ) ) + x;
  672.             options.x = x;
  673.         }
  674.         else
  675.             options.x = 0;
  676.  
  677.         if ( YValue & flags ) {
  678.             if ( YNegative & flags )
  679.                 y = DisplayHeight ( dpy, DefaultScreen (dpy ) ) + y;
  680.             options.y = y;
  681.         }
  682.         else
  683.                 options.y = 0;
  684.  
  685.         if ( x == 0 || x > DisplayWidth ( dpy, DefaultScreen (dpy )) )
  686.                 options.x = 100;
  687.  
  688.         if ( y == 0 || y > DisplayHeight ( dpy, DefaultScreen (dpy )) )
  689.                 options.y = 100;
  690.  
  691.         if ( (width < DisplayWidth ( dpy, DefaultScreen (dpy ) ) / 2)
  692.             || width  > DisplayWidth ( dpy, DefaultScreen (dpy ) ) )
  693.                options.width = DisplayWidth ( dpy, DefaultScreen (dpy ) ) / 2;
  694.         else
  695.                options.width = width;
  696.  
  697.         if ( (height < DisplayHeight ( dpy, DefaultScreen (dpy ) ) / 2) 
  698.             || height > DisplayHeight ( dpy, DefaultScreen (dpy ) ) )
  699.                options.height = 
  700.                 (DisplayHeight ( dpy, DefaultScreen (dpy ) ) * 2) / 3;
  701.         else
  702.                options.height = height;
  703.     }
  704.     else {
  705.         /*
  706.          *    Largeur par defaut =  2/3 de l'ecran.
  707.          *    Hauteur par defaut =  2/3 de l'ecran.
  708.          */
  709.         options.width = (DisplayWidth ( dpy, DefaultScreen (dpy ) ) * 2) / 3;
  710.         options.height = (DisplayHeight ( dpy, DefaultScreen (dpy ) ) * 2) / 3;
  711.     }
  712. }
  713.  
  714.  
  715.  
  716. /*
  717. **    Function name : GetHomeDir
  718. **
  719. **    Description : Comme son nom l'indique.
  720. **    Input : Le container.
  721. **    Ouput :
  722. */
  723. static char *GetHomeDir ( dest )
  724. char * dest;
  725. {
  726.     int uid;
  727.     struct passwd *getpwuid ();
  728.     struct passwd *pw;
  729.     register char *ptr;
  730.     
  731.     if ((ptr = getenv ("HOME")) != NULL ) {
  732.         (void) strcpy ( dest, ptr );
  733.     }
  734.     else {
  735.         if ((ptr = getenv ("USER")) != NULL ) {
  736.             pw = getpwnam (ptr);
  737.         }
  738.         else {
  739.             uid = getuid ();
  740.             pw = getpwuid ( uid );
  741.         }
  742.         if (pw) {
  743.             (void) strcpy ( (char *) dest, (char *) pw -> pw_dir );
  744.         }
  745.         else {
  746.             *dest = ' ';
  747.         }
  748.     }
  749.     return dest;
  750. }
  751.  
  752.  
  753. /*
  754. **    Function name : Usage
  755. **
  756. **    Description : 
  757. **    Input : 
  758. **    Ouput :
  759. */
  760. Usage ()
  761. {
  762.     (void) fprintf ( stderr,"Usage : xcoral [-options...]\n");
  763.     (void) fprintf ( stderr,"Option :\n");
  764.     (void) fprintf ( stderr,
  765.         "\t[=WidthxHeight+X+Y]\t\t\t -Geometry\n" );
  766.     (void) fprintf ( stderr,
  767.         "\t[-d/-display]\t\t -Display\n" );
  768.     (void) fprintf ( stderr,
  769.         "\t[-fn/-font]\t\t -Font in text window\n" );
  770.     (void) fprintf ( stderr,
  771.         "\t[-bg/-background]\t -Background color in text window\n" );
  772.     (void) fprintf ( stderr,
  773.         "\t[-fg/-foreground]\t -Foreground color in text window\n" );
  774.     (void) fprintf ( stderr,
  775.         "\t[-mfn/-mfont]\t\t -Font in menu\n" );
  776.     (void) fprintf ( stderr,
  777.         "\t[-mbg/-mbackground]\t -Background color in menu\n" );
  778.     (void) fprintf ( stderr,
  779.         "\t[-mfg/-mforeground]\t -Foreground color in menu\n" );
  780.     (void) fprintf ( stderr,
  781.         "\t[-dbg/-mbackground]\t -Background color in dialog box\n" );
  782.     (void) fprintf ( stderr,
  783.         "\t[-dfg/-mforeground]\t -Foreground color in dialog box\n" );
  784.     
  785.     (void) exit (1);
  786. }
  787.  
  788.  
  789. /*
  790. **    Function name : PixelValue
  791. **
  792. **    Description : 
  793. **    Input : 
  794. **    Ouput :
  795. */
  796. unsigned long PixelValue ( display, str, result )
  797. Display *display;
  798. char *str;
  799. int *result;
  800. {
  801.     XColor  color, tcolor;
  802.     int status;
  803.  
  804.     status = XAllocNamedColor ( display,
  805.         DefaultColormap ( display, DefaultScreen ( display ) ), 
  806.         str, &color, &tcolor );
  807.  
  808.     if ( status == True )
  809.         *result = True;
  810.     else {
  811.         if ( IsColor == True ) {
  812.             (void) fprintf ( stderr, "Can't alloc named color %s\n", str );
  813.             (void) exit (1);
  814.         }
  815.         else
  816.             *result = False;
  817.     }
  818.     return (color.pixel);
  819. }
  820.  
  821.  
  822. /*
  823. **    Function name : GetOpFont
  824. **
  825. **    Description : Donne la fonte pour le type
  826. **        d'objet passe en argument.
  827. **    Input : Le type
  828. **    Ouput : Un pointeur sur la fonte.
  829. */
  830. XFontStruct *GetOpFont ( type )
  831. register int type;
  832. {
  833.     XFontStruct *font;
  834.  
  835.     switch ( type ) {
  836.     case OP_TEXT_FONT:
  837.         font = options.text_font;
  838.         break;
  839.     case OP_MENU_FONT:
  840.         font = options.menu_font;
  841.         break;
  842.     }
  843.     return (font);
  844. }
  845.  
  846.  
  847. /*
  848. **    Function name : GetOpColor
  849. **
  850. **    Description : Donne la couleur du type d'objet
  851. **        passe en argument.
  852. **    Input : Le type
  853. **    Ouput : La valeur en pixel.
  854. */
  855. unsigned long GetOpColor ( type )
  856. register int type;
  857. {
  858.     unsigned long color;
  859.  
  860.     switch ( type ) {
  861.     case OP_TEXT_FG:
  862.         color = options.fg;
  863.         break;
  864.     case OP_TEXT_BG:
  865.         color =  options.bg;
  866.         break;
  867.     case OP_TEXT_TS:
  868.         color =  options.text_top_shadow;
  869.         break;
  870.     case OP_TEXT_BS:
  871.         color = options.text_bottom_shadow;
  872.         break;
  873.     case OP_MENU_FG:
  874.         color =  options.mfg;
  875.         break;
  876.     case OP_MENU_BG:
  877.         color = options.mbg;
  878.         break;
  879.     case OP_MENU_TS:
  880.         color =  options.menu_top_shadow;
  881.         break;
  882.     case OP_MENU_BS:
  883.         color = options.menu_bottom_shadow;
  884.         break;
  885.     case OP_DIAL_FG:
  886.         return options.dfg;
  887.         break;
  888.     case OP_DIAL_BG:
  889.         return options.dbg;
  890.         break;
  891.     case OP_DIAL_TS:
  892.         color = options.dialog_top_shadow;
  893.         break;
  894.     case OP_DIAL_BS:
  895.         color = options.dialog_bottom_shadow;
  896.         break;
  897.     }
  898.     return color;
  899. }
  900.  
  901.  
  902. /*
  903. **    Function name : GetOpFilename
  904. **
  905. **    Description : Donne le filename de la ligne de commande.
  906. **    Input : 
  907. **    Ouput : Le filename.
  908. */
  909. char *GetOpFilename ()
  910. {
  911.         return ( options.filename ); 
  912. }
  913.  
  914.  
  915. /*
  916. **    Function name : GetOpBW
  917. **
  918. **    Description : La Largeur du bord.
  919. **    Input : 
  920. **    Ouput :
  921. */
  922. unsigned int GetOpBW ()
  923. {
  924.     return options.bw;
  925. }
  926.  
  927.  
  928. /*
  929. **    Function name : GetOpBD
  930. **
  931. **    Description : Le border 
  932. **    Input : 
  933. **    Ouput :
  934. */
  935. unsigned  long GetOpBD ()
  936. {
  937.     return options.bd;
  938. }
  939.  
  940.  
  941. /*
  942. **    Function name : GetOpGeo
  943. **
  944. **    Description : Donne un element de la geometrie.
  945. **    Input : Le type.
  946. **    Ouput : Valeur en pixels.
  947. */
  948. int GetOpGeo ( type )
  949. register int type;
  950. {
  951.     register int result;
  952.  
  953.     switch ( type ) {
  954.         case OP_X:
  955.             result = options.x;
  956.             break;
  957.         case OP_Y:
  958.             result = options.y;
  959.             break;
  960.         case OP_WIDTH:
  961.             result = options.width;
  962.             break;
  963.         case OP_HEIGHT:
  964.             result = options.height;
  965.             break;
  966.     }
  967.     return result;
  968. }
  969.