File: win-sym.c

package info (click to toggle)
gcin 1.4.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,488 kB
  • ctags: 3,272
  • sloc: ansic: 25,634; makefile: 524; sh: 411; cpp: 231
file content (430 lines) | stat: -rw-r--r-- 7,972 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include <sys/stat.h>
#include "gcin.h"
#include "pho.h"
#include "gtab.h"
#include "win-sym.h"

static GtkWidget *gwin_sym = NULL;
static int cur_in_method;
gboolean win_sym_enabled=0;

typedef struct {
  char **sym;
  int symN;
} SYM_ROW;

static SYM_ROW *syms;
static int symsN;

typedef struct {
  SYM_ROW *syms;
  int symsN;
} PAGE;

static PAGE *pages;
static int pagesN;
static int idx;

extern char *TableDir;

FILE *watch_fopen(char *filename, time_t *pfile_modify_time)
{
  FILE *fp;
  char fname[256];

  if (!getenv("GCIN_TABLE_DIR"))
    get_gcin_user_fname(filename, fname);
  else
    get_sys_table_file_name(filename, fname);

  if ((fp=fopen(fname, "r"))==NULL) {
    strcat(strcat(strcpy(fname, TableDir), "/"), filename);

    if ((fp=fopen(fname, "r"))==NULL)
     return NULL;
  }

  struct stat st;
  fstat(fileno(fp), &st);

  if (st.st_mtime == *pfile_modify_time) {
    fclose(fp);
    return NULL;
  }

  *pfile_modify_time = st.st_mtime;
  return fp;
}

static void save_page()
{
  if (!symsN)
    return;

  pages=trealloc(pages, PAGE, pagesN+1);
  pages[pagesN].syms = syms;
  pages[pagesN].symsN = symsN;
  pagesN++;
  syms = NULL;
  symsN = 0;
}


static gboolean read_syms()
{
  FILE *fp;
  static char symbol_table[] = "symbol-table";
  static time_t file_modify_time;

  if ((fp=watch_fopen(symbol_table, &file_modify_time))==NULL)
    return FALSE;

  int pg;
  for(pg=0; pg < pagesN; pg++) {
    syms = pages[pg].syms;
    symsN = pages[pg].symsN;

    int i;
    for(i=0; i < symsN; i++) {
      int j;
      for(j=0; j < syms[i].symN; j++)
        if (syms[i].sym[j])
          free(syms[i].sym[j]);
    }
    free(syms);
  }
  pagesN = 0; pages = NULL;
  syms = NULL; symsN = 0;


  while (!feof(fp)) {
    char tt[1024];

    bzero(tt, sizeof(tt));
    fgets(tt, sizeof(tt), fp);
//    dbg(tt);
    int len=strlen(tt);

    if (!len)
      continue;

    if (tt[len-1]=='\n') {
      tt[len-1]=0;
      if (tt[0]==0)
        save_page();
    } else
      break;

    if (tt[0]=='#')
      continue;

    char *p=tt;

    syms=realloc(syms, sizeof(SYM_ROW) * (symsN+1));
    SYM_ROW *psym = &syms[symsN++];
    bzero(psym, sizeof(SYM_ROW));


    while (*p) {
      char *n = p;

      while (*n && *n!='\t')
        n++;

      *n = 0;

      psym->sym=realloc(psym->sym, (psym->symN+1) * sizeof(char *));
      psym->sym[psym->symN++] = strdup(p);

      p = n + 1;
    }

    if (!psym->symN) {
      free(syms);
      syms=NULL;
      symsN=0;
    }
  }

  if (symsN)
    save_page();

  fclose(fp);

  idx = 0;
  syms = pages[idx].syms;
  symsN = pages[idx].symsN;

  return TRUE;
}


gboolean add_to_tsin_buf(char *str, phokey_t *pho, int len);
void send_text_call_back(char *text);
void tsin_reset_in_pho(), reset_gtab_all(), clr_in_area_pho();

static void cb_button_sym(GtkButton *button, char *str)
{
  phokey_t pho[256];
  bzero(pho, sizeof(pho));

  if (current_CS->in_method == 6 && current_CS->im_state != GCIN_STATE_DISABLED) {
    add_to_tsin_buf(str, pho, utf8_str_N(str));
  }
  else
    send_text_call_back(str);

  switch (current_CS->in_method) {
    case 3:
       clr_in_area_pho();
       break;
#if USE_TSIN
    case 6:
       tsin_reset_in_pho();
       break;
#endif
    default:
       reset_gtab_all();
       break;
  }

  if (gcin_win_sym_click_close) {
    win_sym_enabled=0;
    hide_win_sym();
  }
}

void update_active_in_win_geom();
extern int win_status_y;

void move_win_sym()
{
#if 0
  dbg("move_win_sym %d\n", current_CS->in_method);
#endif
  if (!gwin_sym)
    return;

  int wx, wy;
#if 0
  if (gcin_pop_up_win) {
    wx = dpy_xl;
  } else
#endif
  {
  //  dbg("win_y: %d  %d\n", win_y, win_yl);
    update_active_in_win_geom();

    wx = win_x; wy = win_y + win_yl;
  }

  int winsym_xl, winsym_yl;
  get_win_size(gwin_sym, &winsym_xl, &winsym_yl);

  if (wx + winsym_xl > dpy_xl)
    wx = dpy_xl - winsym_xl;
  if (wx < 0)
    wx = 0;

#if 0
  if (gcin_pop_up_win) {
    wy = win_status_y - winsym_yl;
  } else
#endif
  {
    if (wy + winsym_yl > dpy_yl)
      wy = win_y - winsym_yl;
    if (wy < 0)
      wy = 0;
  }

  gtk_window_move(GTK_WINDOW(gwin_sym), wx, wy);
}


void hide_win_sym()
{
  if (!gwin_sym)
    return;
  gtk_widget_hide(gwin_sym);

}

void show_win_sym()
{
  if (!current_CS)
    return;

  if (!gwin_sym || !win_sym_enabled || current_CS->im_state == GCIN_STATE_DISABLED)
    return;
#if 0
  dbg("show_win_sym\n");
#endif
  gtk_widget_show_all(gwin_sym);
  move_win_sym();
}


void lookup_gtab_out(char *ch, char *out);
void str_to_all_phokey_chars(char *b5_str, char *out);

static void sym_lookup_key(char *instr, char *outstr)
{
  if (current_CS->in_method == 3 || current_CS->in_method == 6) {
    str_to_all_phokey_chars(instr, outstr);
  } else {
    outstr[0]=0;

    while (*instr) {
      char tt[512];

      lookup_gtab_out(instr, tt);
      strcat(outstr, tt);

      instr+= utf8_sz(instr);

      if (*instr)
          strcat(outstr, " | ");
    }
  }
}

static void destory_win()
{
  if (gwin_sym)
    gtk_widget_destroy(gwin_sym);
  gwin_sym = NULL;
}


gboolean  button_scroll_event(GtkWidget *widget,GdkEventScroll *event, gpointer user_data)
{
  if (pagesN < 2)
    return;

  switch (event->direction) {
    case GDK_SCROLL_UP:
      idx--;
      if (idx < 0)
        idx = pagesN - 1;
      break;
    case GDK_SCROLL_DOWN:
      idx = (idx+1) % pagesN;
      break;
  }

  syms = pages[idx].syms;
  symsN = pages[idx].symsN;
  destory_win();
//  win_sym_enabled = 0;
  create_win_sym();
}


void create_win_sym()
{
  if (!current_CS) {
    dbg("create_win_sym, null CS\n");
    return;
  }

  if (current_CS->in_method < 0 || current_CS->in_method >= MAX_GTAB_NUM_KEY) {
    p_err("bad current_CS %d\n", current_CS->in_method);
  }

  if (current_CS->in_method != 3 && current_CS->in_method != 6 && !cur_inmd)
    return;

  if (read_syms() || cur_in_method != current_CS->in_method) {
    destory_win();
  } else {
    if (!syms)
      return;
  }


  if (gwin_sym) {
    if (win_sym_enabled)
      show_win_sym();
    else
      hide_win_sym();

    return;
  }

  gwin_sym = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  cur_in_method = current_CS->in_method;

  GtkWidget *vbox_top = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (gwin_sym), vbox_top);

  gtk_container_set_border_width (GTK_CONTAINER (vbox_top), 0);

  int i;
  for(i=0; i < symsN; i++) {
    SYM_ROW *psym = &syms[i];
    GtkWidget *hbox_row = gtk_hbox_new (FALSE, 0);
    gtk_box_pack_start (GTK_BOX (vbox_top), hbox_row, FALSE, FALSE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (hbox_row), 0);

    int j;
    for(j=0; j < psym->symN; j++) {
      char *str = psym->sym[j];

      if (!str[0])
         continue;

      GtkWidget *button = gtk_button_new();
      GtkWidget *label = gtk_label_new(str);

      gtk_container_add(GTK_CONTAINER(button), label);
      set_label_font_size(label, gcin_font_size_symbol);

      gtk_container_set_border_width (GTK_CONTAINER (button), 0);
      gtk_box_pack_start (GTK_BOX (hbox_row), button, FALSE, FALSE, 0);

      if (utf8_str_N(str) > 0) {
        char phos[512];

        sym_lookup_key(str, phos);

        int phos_len = strlen(phos);

        if (phos_len) {
          GtkTooltips *button_pho_tips = gtk_tooltips_new ();
          gtk_tooltips_set_tip (GTK_TOOLTIPS (button_pho_tips), button, phos, NULL);
        }
      }

      g_signal_connect (G_OBJECT (button), "clicked",
         G_CALLBACK (cb_button_sym), str);
    }
  }

  gtk_widget_realize (gwin_sym);
  GdkWindow *gdkwin_sym = gwin_sym->window;
  set_no_focus(gwin_sym);

  if (win_sym_enabled)
    gtk_widget_show_all(gwin_sym);

  g_signal_connect (G_OBJECT (gwin_sym), "scroll-event",
    G_CALLBACK (button_scroll_event), NULL);

  move_win_sym();
#if 0
  dbg("in_method:%d\n", current_CS->in_method);
#endif
  return;
}


void toggle_win_sym()
{
  win_sym_enabled^=1;
  create_win_sym();
}

void change_win_sym_font_size()
{
}