home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <X11/Xlib.h>
- #include <X11/Xatom.h>
- #include <X11/Xutil.h>
- #include <Intrinsic.h>
- #include <StringDefs.h>
- #include <Xaw/Command.h>
-
- #include "defs.h"
- #include "externs.h"
-
- int values[4];
- int truevalue;
- int Kanji2English=GUESSKANJI;
- int showEnglish = True;
- int doBell = True;
-
- extern Display *display;
- extern Window mainwindow;
-
- int englishwidth=7;
- int englishheight=11;
-
- /* setstatus:
- * sets label for main status bar widget.
- * ( the long one at the bottom)
- */
- void setstatus(s)
- char *s;
- {
- XtVaSetValues(statusline,
- XtNlabel,s,
- NULL);
- }
-
- /*
- *UseThisKanji()
- * Returns Boolean value on whether this is a "valid" kanji for us,
- * based on usefile markings,
- * current grade set,
- * and whether it has english/kana translation
- */
- Boolean UseThisKanji(kanji)
- struct translationstruct *kanji;
- {
- /* nonexistant? */
- if(kanji == NULL) return False;
-
- /* "do not read", set by inituse ?*/
- if(kanji == (void *) 1) return False;
-
- if(showEnglish){
- if(kanji->english == NULL)
- return False;
- if(kanji->english[0] == '\0')
- return False;
- } else {
- /* we're supposed to be showing kana.. are there any? */
- if(kanji->pronunciation == NULL)
- return False;
- }
-
- /* only thing left is to check grade level */
-
- if(gradelevel == 0)
- return True;
- if(kanji->grade_level == 0)
- return False;
-
- if(kanji->grade_level > gradelevel)
- return False;
-
- return True;
- }
-
-
- /* CountKanji
- *This routine gets called a hell of a lot:
- * When we change grade level, and
- * when we change kana/english display.
- * (the secnd being because kanjidic does not always have
- * english and/or kana listings
- */
- void CountKanji(){
- int counter;
- numberofkanji=0;
- for(counter=lowest;counter <=highest;counter++){
- if(UseThisKanji(translations[counter])){
- numberofkanji++;
- }
- }
- }
-
- /* pickkanji:
- * picks an acceptably random kanji char
- */
-
- int pickkanji(){
- int rand_kanji,count;
-
-
- rand_kanji = random()%numberofkanji;
-
- for(count=lowest;count<=highest;count++){
- if(UseThisKanji(translations[count])){
- rand_kanji--;
- if(rand_kanji <0)
- return count;
- }
- }
- fprintf(stderr,"Internal error: picked kanji out of range");
- return 0;
-
- }
-
- /* printkanji:
- * updates all the kanji buttons.
- */
-
- void printkanji(){
- int i,knum;
- XChar2b onecharstring[2];
-
- onecharstring[1].byte1 = onecharstring[1].byte2 = 0;
-
- if(Kanji2English == GUESSKANJI){
- knum = values[truevalue];
- onecharstring[0].byte1 = (knum & 0xff00)>>8;
- onecharstring[0].byte2 = (knum & 0x00ff);
-
- XtVaSetValues(kanjiWidget[0],XtNlabel,onecharstring,NULL);
- XtVaSetValues(kanjiWidget[1],XtNlabel," ",NULL);
- XtVaSetValues(kanjiWidget[2],XtNlabel," ",NULL);
- XtVaSetValues(kanjiWidget[3],XtNlabel," ",NULL);
- return;
- }
- /* else */
- for(i=0;i<4;i++) {
- knum = values[i];
-
- onecharstring[0].byte1 = (knum & 0xff00)>>8;
- onecharstring[0].byte2 = (knum & 0x00ff);
-
-
- XtVaSetValues(kanjiWidget[i],
- XtNlabel,onecharstring,
- XtNsensitive,True,
- NULL);
-
- }
- }
-
- /* SetWidgetLabel:
- * Convenience function to
- * set label of four lines of english/kana.
- * Will change fonts, dependant on "showEnglish"
- */
- void SetWidgetLabel(widget,Tnum)
- Widget widget;
- int Tnum;
- {
-
- if(showEnglish == True){
- XtVaSetValues(widget,
- XtNencoding,XawTextEncoding8bit,
- XtNfont,fixedfont,
- XtNlabel,translations[Tnum]->english,
- NULL);
- } else {
- XtVaSetValues(widget,
- XtNencoding,XawTextEncodingChar2b,
- XtNfont,smallkfont,
- NULL);
- XtVaSetValues(widget,
- XtNlabel,translations[Tnum]->pronunciation,
- NULL);
- }
- }
- /* printenglish()
- * similar to printkanji()
- * updates all "english" labels...
- * EXCEPT: sometimes we want then to print kana! :-) so we use
- * SetWidgetLabel() to do the correct type
- */
- void printenglish(){
- int i;
-
- /* we must be guessing which character for one meaning */
- if(Kanji2English == GUESSMEANING){
- SetWidgetLabel(englishWidget[0],values[truevalue]);
- XtVaSetValues(englishWidget[1],XtNlabel," ",XtNsensitive,False,NULL);
- XtVaSetValues(englishWidget[2],XtNlabel," ",XtNsensitive,False,NULL);
- XtVaSetValues(englishWidget[3],XtNlabel," ",XtNsensitive,False,NULL);
- return;
- }
- /* ELSE */
- /* we have all FOUR active for picking*/
- for(i=0;i<4;i++){
- XtVaSetValues(englishWidget[i],XtNsensitive,True,NULL);
- SetWidgetLabel(englishWidget[i],values[i]);
- }
- }
-
-
- /* kanjitoenglish
- * sets up question...
- * { "what does this symbol? choose one of the four choices"}
- *
- * then returns, presumably falling back to XtAppMainLoop()
- *
- * guesskanji == GUESSKANJI : give kanji, and user guesses the meaning
- * int English
- * guesskanji == GUESSENGLISH : give english, and user guesses which
- * kanji fits best.
- */
- void kanjitoenglish(guesskanji)
- int guesskanji;
- {
- int doloop;
- Kanji2English=guesskanji;
-
- /* determine which value will be correct one.. */
- truevalue = random() %4;
-
- /* hack for random numbers...
- * only picks values that are acceptable by our current
- * "usefile"
- */
- values[0] = pickkanji();
- values[1] = pickkanji();
- values[2] = pickkanji();
- values[3] = pickkanji();
-
- /* now weed out duplicates.. */
- doloop = 0;
- do{
- if((values[0] == values[1]) || (values[0] == values[2])
- || values[0] == values[3] ){
- values[0] = pickkanji();
- doloop=1;
- } else
- if(values[1] == values[2] || values[1] == values[3]){
- values[1] = pickkanji();
- doloop=1;
- } else
- if(values[2] == values[3]){
- values[2] = pickkanji();
- doloop=1;
- } else
- doloop = 0;
- }while(doloop);
-
- DescribeCurrent(values[truevalue]);
-
- printkanji(); /* update labels */
- printenglish(); /* likewise */
- switch(guesskanji){
- case GUESSKANJI:
- XtVaSetValues(kanjimode,XtNsensitive,False,NULL);
- XtVaSetValues(englishmode,XtNsensitive,True,NULL);
- break;
- case GUESSMEANING:
- XtVaSetValues(kanjimode,XtNsensitive,True,NULL);
- XtVaSetValues(englishmode,XtNsensitive,False,NULL);
- break;
- }
-
- }
-
-
- /* englishcallback:
- * Handles clicking on "english" widgets.
- * Only really neccessary if we are guessing the meaning of a
- * kanji character
- */
- void englishcallback(w,data,calldata)
- Widget w;
- XtPointer data;XtPointer calldata;
- {
- if(Kanji2English!= GUESSKANJI)
- return;
- if(data == (XtPointer) truevalue){
- setstatus("Correct!");
- kanjitoenglish(GUESSKANJI);
- } else {
- if(doBell)
- XBell(display,100);
- setstatus("Incorrect.");
- }
-
- }
-
- /*
- *handles top four kanji buttons at top
- */
- void kanjicallback(w,data,calldata)
- Widget w;XtPointer data;XtPointer calldata;
- {
- if(Kanji2English != GUESSMEANING)
- return;
- if(data == (XtPointer) truevalue){
- setstatus("Correct!");
- kanjitoenglish(GUESSMEANING);
- } else {
- if(doBell)
- XBell(display,100);
- setstatus("Incorrect.");
- }
- }
-
-
- /* changemode:
- * change from guessing kanji to guessing english, or vica verse,
- * by clicking on the appropriate button
- *
- * Also handles changing from english <->kana display,
- * calling CountKanji(), because we have to .
- */
- void ChangeMode(w,data,calldata)
- Widget w;XtPointer data;XtPointer calldata;
- {
- int whichbutton = (int) data;
- #ifdef DEBUG
- printf("button mode change: %d\n",whichbutton);
- #endif
- switch(whichbutton){
- case GUESSMEANING:
- case GUESSKANJI:
- kanjitoenglish(whichbutton);
- break;
- case TOGGLEKANA:
- #ifdef DEBUG
- puts("We toggle kana/english now");
- #endif
- showEnglish = !showEnglish;
- CountKanji();
- kanjitoenglish(Kanji2English);
- break;
- }
- }
-
-