Logo Search packages:      
Sourcecode: hanterm-classic version File versions  Download package

hanja.c

/*
      Hanja handling routine
      written by Yang, Woosuk.
      3. 10. 1994.
*/
#include "ptyx.h"
#include "data.h"
#include "error.h"
#include "hangul.h"
#include "hanja.h"
#include <X11/keysym.h>
#include <stdio.h>

extern void ShowCursor();
extern void HideCursor();

#if !defined(NO_PATCH_ADJUSTHANGULSTRING) /* by hanmaum 1998.2.4 */
extern int (*converter)();
#endif

/*
      입력 : 한글 코드 (int han_code)
      출력 : 한자 테이블 (int hanja_code_table[] : 매칭되는 한자들.)
          리턴값 : 매칭된 수.
*/
int hanja_mode = 0;

int
get_hanja_codes(han_code, hanja_code_table)
unsigned int han_code, hanja_code_table[];
{
      unsigned char t[20];
      unsigned int code, j;
      int i, k, count;

      for (i = 0; hanja_table[i][0]; i++) {
            if (han_code == hanja_table[i][0]) {
                  count = hanja_table[i+1][1]-hanja_table[i][1];
                  if (count > 0x80) { /* gap exist */
                        count -= 0xa2;
                  }
                  for (j = hanja_table[i][1],k = 0; k<count; j++, k++) {
                        if ((j & 0x00ff) > 0xfe) 
                              j += 0xa2;
                        hanja_code_table[k] = j;
                  }
                  return count;
            } 
      }
      return 0;
}

#define MAXHANJABUF 200
#define HANJA_START_X 20

static int hanja_sx, hanja_count, hanja_max_item, hanja_max_disp;

unsigned int hanja_codes[MAXHANJABUF];

toggle_hanja_mode(han_code)
    unsigned int han_code;
{
    int count;

    if (!hanja_mode && han_code) {
        if ((count = get_hanja_codes(han_code, hanja_codes)) != 0)
            hanja_mode = 1;
    }
    else
      hanja_mode = 0;

    if (hanja_mode) {
        hanja_count = count;
        hanja_sx = 0; hanja_max_disp = 0;
      show_hanja_buf();
    } else {
        chat_clear_line(HANJA_START_X, 
            term->screen.max_col - HANJA_START_X + 1);
    }
}

show_hanja_buf()
{
    TScreen *screen = &term->screen;
    Char hanja_line[200];
    char tmp[200];
    int i;

    sprintf((char*)hanja_line, "[%2d/%2d]", hanja_sx + 1, hanja_count);
    for (i = 0; i < 10; i++) {
        if ((int)strlen((char*)hanja_line) + HANJA_START_X > term->screen.max_col - 6)
            break;
        if (i + hanja_sx >= hanja_count) break;
        sprintf(tmp, " %d.%c%c", (i<9)?i+1:i-9, (Char)(hanja_codes[i+hanja_sx]/0x100),
                                      (Char)(hanja_codes[i+hanja_sx]%0x100));
        strcat((char*)hanja_line, (char*)tmp);
    }

    hanja_max_item = i;
    if (i + hanja_sx < hanja_count) hanja_max_disp = i;
    else if (i > hanja_max_disp) hanja_max_disp = i;

    for (i=strlen((char*)hanja_line); i<=term->screen.max_col - HANJA_START_X; i++) {
      hanja_line[i] = ' ';
    }
    hanja_line[i] = '\0';
      convert_ks_to_johab(hanja_line, tmp, strlen((char*)hanja_line));

    HDrawImageString(screen->display, TextWindow(screen), screen->normalGC,
                 screen->normalHGC,
                 CursorX(screen, HANJA_START_X),
                 (screen->max_row + 1) * FontHeight(screen) + 
                 screen->ascent + screen->border * 2,
                 tmp,
                 strlen(tmp));
}

hanja_mode_input(keysym, event, string, nbytes)
    KeySym keysym;
    XKeyEvent *event;
    Char *string;
    int nbytes;
{
    int sel;

    if (keysym==XK_Escape){
      toggle_hanja_mode(0);
    }
    else if (keysym==XK_space){
            hanja_sx += hanja_max_disp;
            if (hanja_sx >= hanja_count)
                  hanja_sx = 0;
        show_hanja_buf();
    }
    else if (IsCursorKey(keysym)) {
      switch (keysym) {
          case XK_Left :
          case XK_Up :
            hanja_sx -= hanja_max_disp;
              if (hanja_sx < 0) 
                        hanja_sx = 0;
            show_hanja_buf();
            break;
          case XK_Right :
          case XK_Down :
                  hanja_sx += hanja_max_disp;
                  if (hanja_sx >= hanja_count)
                        hanja_sx = 0;
                show_hanja_buf();
                break;
            case XK_Home :
            hanja_sx = 0;
            show_hanja_buf();
            break;
        }
    } else if (nbytes > 0) {
          if (*string >= '0' && *string <= '9') {
            sel = *string - '1';
            if (sel < 0)
                sel = 9;
            if (sel < hanja_max_item) {
                put_hanja(hanja_sx + sel);
                toggle_hanja_mode(0);
              }
          }
    }
}

put_hanja(item)
int item;
{
    extern Char temp_hangul[2];
    extern int hangul_state;
    Char code[2];
#if !defined(NO_PATCH_ADJUSTHANGULSTRING) /* by hanmaum 1998.2.4 */
    char temp_buf[10];
    int len;
#endif

    code[0] = hanja_codes[item] / 0x100;
    code[1] = hanja_codes[item] % 0x100;
    HideCursor();
    convert_ks_to_johab(code, temp_hangul, 2);
    ShowCursor();
#if !defined(NO_PATCH_ADJUSTHANGULSTRING) /* by hanmaum 1998.2.4 */
    len = (*converter)
      ((unsigned int)0xff, (unsigned int)code[0], (unsigned int)code[1], 
      temp_buf, 2);
    if (len > 0) v_write(term->screen.respond, temp_buf, len);
#else
    v_write(term->screen.respond, code, 2);
#endif
    HideCursor();
    temp_hangul[0] = 0;
    hangul_state = 1;
    ShowCursor();
}


Generated by  Doxygen 1.6.0   Back to index