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

  1. #include <stdio.h>
  2.  
  3. #include <Xos.h>
  4. #include <Intrinsic.h>
  5. #include <StringDefs.h>
  6. #include <Xaw/Command.h>
  7. #include <Xaw/Label.h>
  8. #include <Xaw/Form.h>
  9. #include <Composite.h>
  10. #include "defs.h"
  11. #include "externs.h"
  12.  
  13. Widget toplevel,form,quitbutton,statusline;
  14. Widget gradedisplay;
  15. Widget currentkanjiForm,currentkanjiGrade,currentkanjiFreq,currentkanjiNum;
  16. Widget kanjimode,englishmode,togglekana;
  17. Widget englishWidget[4],kanjiWidget[4];
  18.  
  19. int gradelevel=0;
  20.  
  21.  char *gradelist[8] = {
  22.     "Grade: All  ",
  23.     "Grade: One  ",
  24.     "Grade: Two  ",
  25.     "Grade: Three",
  26.     "Grade: Four ",
  27.     "Grade: Five ",
  28.     "Grade: Six  ",
  29.     "Grade: ERROR!"
  30. };
  31. void changegrade(widget,data,calldata)
  32. Widget widget;
  33. XtPointer data; XtPointer calldata;
  34. {
  35.     gradelevel = gradelevel +1;
  36.     if(gradelevel ==7) gradelevel = 0;
  37.     
  38.     XtVaSetValues(widget,
  39.               XtNlabel,gradelist[gradelevel],
  40.               NULL);
  41.  
  42.     /* need to recount all kanji equal or below the current level */
  43.     CountKanji();
  44.     if(numberofkanji<9){
  45.         XtVaSetValues(statusline,XtNlabel,"Too Few Kanji",NULL);
  46.         fprintf(stderr,"Too Few Kanji\n");
  47.         if(gradelevel==0){
  48.             fprintf("You must have either a too-small usefile,or a corrupted kanjidic\n");
  49.             exit(0);
  50.         }
  51.         changegrade(0,0,0);
  52.         return;
  53.     }
  54.     /* and make another puzzle... */
  55.     kanjitoenglish(Kanji2English);
  56.     return;
  57. }
  58.  
  59. /* DescribeCurrent()
  60.  *    Sets labels to display difficulty of
  61.  *    Current "true" kanji"
  62.  */
  63. void DescribeCurrent(kanjinum)
  64. int kanjinum;
  65. {
  66.     char gradelabel[10],freqlabel[10],numlabel[10];
  67.  
  68.  
  69.  
  70.     if(translations[kanjinum]->grade_level !=0){
  71.         sprintf(gradelabel,"G: %4d",translations[kanjinum]->grade_level);
  72.     } else {
  73.         sprintf(gradelabel,"G:     ");
  74.     }
  75.     if(translations[kanjinum]->frequency !=0){
  76.         sprintf(freqlabel,"F: %4d",translations[kanjinum]->frequency);
  77. #ifdef DEBUG
  78.     printf("Describing %x\n",kanjinum);
  79.     printf("Freqnum %d\n",translations[kanjinum]->frequency);
  80. #endif
  81.  
  82.     } else {
  83.         sprintf(freqlabel,"F:     ");
  84.     }
  85.     sprintf(numlabel,"#: %4x",kanjinum);
  86.  
  87.     XtVaSetValues(currentkanjiGrade,XtNlabel,gradelabel,NULL);
  88.     XtVaSetValues(currentkanjiFreq,XtNlabel,freqlabel,NULL);
  89.     XtVaSetValues(currentkanjiNum,XtNlabel,numlabel,NULL);
  90. }
  91.  
  92. /* MakeKanjiButtons()
  93.  *  set up kanji button widgets at initialization
  94.  */
  95. void MakeKanjiButtons(){
  96.     int i;
  97.     /* make KANJI buttons */
  98.     for(i=0;i<4;i++){
  99.         char labl[20];
  100.         sprintf(labl,"kanji%d",i);
  101.  
  102.         kanjiWidget[i]=XtVaCreateManagedWidget(
  103.             labl,commandWidgetClass,
  104.             form,
  105.                   XtNlabel,"",
  106.                   XtNwidth,50, XtNheight,50,
  107.                   XtNshapeStyle,XawShapeOval,
  108.                   XtNencoding,XawTextEncodingChar2b,
  109.                   XtNfont,largekfont,
  110.                   NULL);
  111.         XtAddCallback(kanjiWidget[i],XtNcallback,kanjicallback,(XtPointer)i);
  112.         if(i>0){
  113.             XtVaSetValues(kanjiWidget[i],
  114.                       XtNfromHoriz,kanjiWidget[i-1],
  115.                       NULL);
  116.         }
  117.  
  118.     }
  119.     currentkanjiForm = XtVaCreateManagedWidget(
  120.             "curkanjiForm",formWidgetClass,form,
  121.             XtNhorizDistance,40,
  122.             XtNright,XawChainRight,XtNleft,XawChainRight,
  123.             XtNfromHoriz,kanjiWidget[3],NULL);
  124.     currentkanjiGrade = XtVaCreateManagedWidget(
  125.             "curkanjiGrade",labelWidgetClass,currentkanjiForm,
  126.             XtNlabel,"G:     ",
  127.             NULL);
  128.     currentkanjiFreq = XtVaCreateManagedWidget(
  129.             "curkanjiFreq",labelWidgetClass,currentkanjiForm,
  130.             XtNlabel,"F:     ",
  131.             XtNfromVert,currentkanjiGrade,
  132.             NULL);
  133.     currentkanjiNum = XtVaCreateManagedWidget(
  134.             "curkanjiNum",labelWidgetClass,currentkanjiForm,
  135.             XtNlabel,"#:     ",
  136.             XtNfromVert,currentkanjiFreq,
  137.             NULL);
  138. }
  139. /* MakeEnglishButtons:
  140.  * Just that. initialize the english buttons that display the possible
  141.  * guesses.
  142.  */
  143. void MakeEnglishButtons(){
  144.     int i;
  145.     for(i=0;i<4;i++){
  146.         char labl[20];
  147.         sprintf(labl,"english%d",i);
  148.  
  149.         englishWidget[i]=XtVaCreateManagedWidget(
  150.             labl,commandWidgetClass,form,
  151.             XtNwidth,320,
  152.             XtNright,XawChainRight,
  153.             XtNleft,XawChainLeft,
  154.             XtNfont,fixedfont,
  155.             NULL);
  156.         
  157.         XtAddCallback(englishWidget[i],XtNcallback,englishcallback,(XtPointer) i);
  158.  
  159.         if(i==0){
  160.             XtVaSetValues(englishWidget[0],
  161.                       XtNfromVert,currentkanjiForm,
  162.                       XtNvertDistance,10,
  163.                       NULL);
  164.         } else {
  165.             XtVaSetValues(englishWidget[i],
  166.                       XtNfromVert,englishWidget[i-1],
  167.                       NULL);
  168.         }
  169.         
  170.  
  171.     }
  172. }
  173.  
  174. void initfonts(){
  175.     char LargeKname[100],SmallKname[100],Fixedname[100];
  176.  
  177.     GetXtString("kanjifont","Kanjifont",LargeKname);
  178.     GetXtString("smallkanji","Smallkanji",SmallKname);
  179.     GetXtString("font","Font",Fixedname);
  180.  
  181. #ifdef DEBUG
  182.     printf("kanjifont from resources is \"%s\"\n",LargeKname);
  183.     printf("smallkanjifont from resources is \"%s\"\n",SmallKname);
  184.     printf("regular font from resources is \"%s\"\n",Fixedname);
  185. #endif
  186.     largekfont = XLoadQueryFont(display,LargeKname);
  187.     smallkfont = XLoadQueryFont(display,SmallKname);
  188.     fixedfont  = XLoadQueryFont(display,Fixedname);
  189.     if(largekfont == NULL){
  190.         fprintf(stderr,"could not load large kanji font\n");
  191.         exit(0);
  192.     }
  193.     if(smallkfont == NULL){
  194.         fprintf(stderr,"could not load small kanji font\n");
  195.         exit(0);
  196.     }
  197.     if(fixedfont == NULL){
  198.         fprintf(stderr,"could not load english font??\n");
  199.         exit(0);
  200.     }
  201.     
  202.  
  203. }
  204.  
  205. int MakeWidgets(){
  206.     /* I suppose I shouldn't hard-code window-width and height..
  207.         but it makes things easier for now.
  208.     */
  209.     form = XtVaCreateManagedWidget("kdrill",formWidgetClass,
  210.                        toplevel,
  211.               XtNx,200, XtNy,100,
  212.               NULL);
  213.  
  214.     
  215.     display = XtDisplay(toplevel);
  216.  
  217.     initfonts();
  218.  
  219.     MakeKanjiButtons();
  220.     MakeEnglishButtons();
  221.  
  222.     kanjimode = XtVaCreateManagedWidget(
  223.             "kanjimode",commandWidgetClass, form,
  224.             XtNlabel,"Guess kanji",
  225.             XtNvertDistance,20,
  226.             XtNfromVert,englishWidget[3],
  227.             XtNshapeStyle,XawShapeOval,
  228.             NULL);
  229.     XtAddCallback(kanjimode,XtNcallback,ChangeMode,(XtPointer) GUESSKANJI);
  230.  
  231.     englishmode = XtVaCreateManagedWidget(
  232.             "englishmode",commandWidgetClass, form,
  233.             XtNlabel,"Guess Meaning",
  234.             XtNvertDistance,20,
  235.             XtNfromVert,englishWidget[3],
  236.             XtNshapeStyle,XawShapeOval,
  237.             XtNfromHoriz,kanjimode,
  238.             NULL);
  239.     XtAddCallback(englishmode,XtNcallback,ChangeMode,(XtPointer) GUESSMEANING);
  240.  
  241.     togglekana = XtVaCreateManagedWidget("togglekana",commandWidgetClass,
  242.                           form,
  243.             XtNlabel,"English/Kana",
  244.             XtNvertDistance,20,
  245.             XtNfromVert,englishWidget[3],
  246.             XtNshapeStyle,XawShapeOval,
  247.             XtNfromHoriz,englishmode,
  248.             NULL);
  249.     XtAddCallback(togglekana,XtNcallback,ChangeMode,(XtPointer) TOGGLEKANA);
  250.  
  251.  
  252.     quitbutton = XtVaCreateManagedWidget("quit",commandWidgetClass,
  253.                          form,
  254.               XtNlabel,"quit",
  255.               XtNfromVert,kanjimode,
  256.               XtNshapeStyle,XawShapeEllipse,
  257.               NULL);
  258.  
  259.     XtAddCallback(quitbutton,XtNcallback,quit,NULL);
  260.  
  261.     statusline = XtVaCreateManagedWidget("status",labelWidgetClass,
  262.                          form,
  263.               XtNlabel,"Welcome to kanjidrill",
  264.               XtNfromHoriz,quitbutton,
  265.               XtNfromVert,kanjimode,
  266.               XtNresize,True,
  267.               NULL);
  268.  
  269.     gradedisplay = XtVaCreateManagedWidget(
  270.             "gradedisplay",commandWidgetClass,form,
  271.             XtNlabel,gradelist[gradelevel],
  272.             XtNresize,False,
  273.             XtNfromVert,kanjimode,
  274.             XtNfromHoriz,statusline,
  275.             NULL);
  276.     XtAddCallback(gradedisplay,XtNcallback,changegrade,NULL);
  277.  
  278.     return 0;
  279. }
  280.