home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume21 / kdrill / part01 / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-07  |  5.3 KB  |  231 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #include <Xos.h>
  5. #include <Intrinsic.h>
  6. #include <StringDefs.h>
  7. #include <Shell.h>
  8. #include <Xaw/Command.h>
  9. #include <Xaw/Label.h>
  10. #include <Xaw/Form.h>
  11. #include <Composite.h>
  12. #include "defs.h"
  13. #include "externs.h"
  14.  
  15. #include "icon.xbm"
  16.  
  17. XFontStruct *largekfont;
  18. XFontStruct *smallkfont;
  19. XFontStruct *fixedfont;
  20.  
  21. /* I should just get rid of this, but I might use it at some
  22.  * later date
  23.  */
  24. static XtActionsRec actionlist[] = {
  25.     { "quit",quit },
  26. };
  27.  
  28. /*
  29.  * We  must have these resources set!!
  30.  *
  31.  * ( Note the similarity between this stuff and optionDescList[].
  32.  *   coincidence? I think not :-)
  33.  */
  34. static char *fallback[] = {
  35.     "KDrill.noBell:     0",
  36.     "KDrill*usefile:    usefile", 
  37.     "KDrill*dictfile:   kanjidic",
  38.     "KDrill*kanjifont:  kanji24",
  39.     "KDrill*smallkanji: kanji16",
  40.     "KDrill*font:       fixed",
  41.     "KDrill*gradelevel: 0",
  42.     "KDrill*showkana:   0",
  43.     NULL
  44. };
  45.  
  46. /* command line arguments should set the appropriate resources.. so we can
  47.  * pull them out when wanted
  48.  */
  49. static XrmOptionDescRec optionDescList[] = {
  50.     {"-usefile",    ".usefile",    XrmoptionSepArg,(caddr_t) NULL},
  51.     {"-dictfile",    ".dictfile",    XrmoptionSepArg,(caddr_t) NULL},
  52.     {"-font",    ".font",    XrmoptionSepArg,(caddr_t) NULL},
  53.     {"-kanjifont",    ".kanjifont",    XrmoptionSepArg,(caddr_t) NULL},
  54.     {"-smallkanji",    ".smallkanji",    XrmoptionSepArg,(caddr_t) NULL},
  55.     {"-noBell",     ".noBell",    XrmoptionNoArg, "1"},
  56.     {"-guessmeaning",".guessmeaning",XrmoptionNoArg,"1"},
  57.     {"-gradelevel", ".gradelevel",  XrmoptionSepArg,(caddr_t) NULL},    
  58.     {"-showkana",   ".showkana",    XrmoptionNoArg,"1"},
  59. };
  60.  
  61.  
  62.  
  63. /* initgc:
  64.  *    Initialises global gc values.
  65.  *      We don't really need this any more. But
  66.  *    GC's are good to have, I suppose.
  67.  */
  68. void initgc(){
  69.  
  70.     gc =  XCreateGC(display,mainwindow,0,NULL);
  71.     cleargc = XCreateGC(display,mainwindow,0,NULL);
  72.  
  73.     XSetForeground(display,gc,black);
  74.     XSetBackground(display,gc,white);
  75.     XSetForeground(display,cleargc,white);
  76.     XSetBackground(display,cleargc,black);
  77.  
  78. }
  79.  
  80. /* getusefile:
  81.  *    Gets a "usefile", to abridge the full dictionary.
  82.  *    If possible, will get it from the user's home directory,
  83.  *    otherwise, tries current directory
  84.  */
  85. FILE *getusefile(){
  86.     FILE *f;
  87.     char *homedir;
  88.     char fullname[100];
  89.  
  90.     if(usefile == NULL) return NULL;
  91.     fullname[0] = '\0';
  92.     if(usefile[0] == '~'){
  93.         homedir = (char *) getenv("HOME");
  94.         if(homedir == NULL){
  95.             perror("Cannot expand '~' char. no HOME env. varriable");
  96.             exit(0);
  97.         }
  98.         sprintf(fullname,"%s%s",homedir,usefile);
  99.     } else {
  100.         strcpy(fullname,usefile);
  101.     }
  102.  
  103.     if((f = fopen(fullname,"r")) == NULL){
  104.         puts("no usefile. using entire dictionary...");
  105.         usefile = NULL;
  106.     } else {
  107.         printf("using \"%s\" to abridge dictionary\n",fullname);
  108.     }
  109.     return f;
  110. }
  111.  
  112. /* inituse:
  113.  *    clear out global array of 'translations' (really abridgements),
  114.  *    and set what kanji chars are appropriate to check on.
  115.  *
  116.  *    we set "okay" chars to have a NULL pointer stored for them in the
  117.  *    translation table, since later, we will want to check if
  118.  *    malloc malloc'd them properly.
  119.  *
  120.  *     unacceptable chars have a pointer stored of (void *) 1
  121.  *
  122.  *    We should really redo this.. but either way, there would
  123.  *    seem to be a conflict as to whether we have actually
  124.  *    malloced memory that starts at 0x00000001, or whatever.
  125.  *    (on a sparc, that will never happen :-) 
  126.  *    
  127.  */
  128. void inituse(){
  129.     FILE *f;
  130.     int i;
  131.     char inbuf[100];
  132.  
  133.     f = getusefile();
  134.  
  135.     /* set all translations flags == NULL.
  136.      *    NULL means it's okay to use this character
  137.      *    
  138.      */
  139.     if(usefile == NULL){
  140.         i = 0x2000;
  141.         while(i<0x8000){
  142.             translations[i++] = (void *)NULL;
  143.         }
  144.         return;
  145.     } else {    /* set all to UNREADABLE, for now */
  146.         i = 0x2000;
  147.         while(i<0x8000){
  148.             translations[i++] = (void *)1;
  149.         }
  150.     }
  151.  
  152.     while(fgets(inbuf,99,f) != NULL){
  153.         int usechar;
  154.         usechar = xtoi(inbuf);
  155.         if( usechar<0x8000)
  156.             translations[usechar] = NULL;
  157.     }
  158.  
  159. }
  160.  
  161.  
  162. /* initstuffs:
  163.  *    calls the various init routines to setup
  164.  *    GCs, fonts, dictionaries, and widgets
  165.  *    (But initializing the translations is done later)
  166.  */
  167.  
  168. static Pixmap iconpixmap;
  169.  
  170. void initstuffs(argc,argv)
  171. int *argc;
  172. char *argv[];
  173. {
  174.     char tempnumber[10];
  175.     XWMHints wmhints;
  176.  
  177.     srandom (time(NULL));
  178.  
  179.     toplevel = XtVaAppInitialize(&Context,"KDrill",
  180.                     optionDescList,XtNumber(optionDescList),
  181.                     argc,argv,
  182.                    fallback,
  183.                     NULL,NULL);
  184.  
  185.     if(*argc >1){
  186.         usage();
  187.     }
  188.  
  189.     /* this don't work?!! */
  190.     doBell = !GetXtBoolean("noBell","NoBell");
  191.  
  192.  
  193.     if(GetXtBoolean("guessmeaning","Guessmeaning"))
  194.         Kanji2English = GUESSMEANING;
  195.     GetXtString("gradelevel","Gradelevel",tempnumber);
  196.     gradelevel = atoi(tempnumber);
  197.     if(gradelevel>6){
  198.         fprintf(stderr,"Grade level >6: defaulting to All (0)\n");
  199.         gradelevel = 0;
  200.     }
  201.  
  202.     /* display set in here */
  203.     MakeWidgets();
  204.  
  205.     XtAppAddActions(Context,actionlist,XtNumber(actionlist));
  206.     XtRealizeWidget(toplevel);
  207.     mainwindow = XtWindow(form);
  208.  
  209.     iconpixmap = XCreateBitmapFromData(display,mainwindow,
  210.                        icon_bits,icon_width,icon_height);
  211.  
  212.     XtVaSetValues(toplevel,XtNiconName,"kdrill",
  213.                 XtNiconPixmap,iconpixmap,
  214.                 NULL);
  215. #ifdef OLDWAY
  216. wmhints.icon_pixmap = iconpixmap;
  217.     wmhints.flags = IconPixmapHint;
  218.  
  219.     XSetWMProperties(display,mainwindow,"kdrill","kdrill",
  220.             NULL,NULL, /* argv,argc */
  221.             NULL,&wmhints,NULL); /* size, wmhints, classhints */
  222. #endif /* OLDWAY */
  223.  
  224.  
  225.     white = WhitePixel(display,0);
  226.     black = BlackPixel(display,0);
  227.     initgc();
  228.     SetBackingStore(display,XtScreen(toplevel),XtWindow(toplevel));
  229.     
  230. }
  231.