File: win-save-phrase.cpp

package info (click to toggle)
gcin 2.9.4%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,812 kB
  • sloc: cpp: 34,326; ansic: 9,319; makefile: 648; sh: 556
file content (240 lines) | stat: -rw-r--r-- 5,567 bytes parent folder | download | duplicates (3)
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
#include "gcin.h"
#include "pho.h"
#include "win-save-phrase.h"
#include  "tsin.h"
#include "gtab.h"

extern int c_len;
extern gboolean test_mode;


typedef struct {
  WSP_S *mywsp;
  int mywspN;
  GtkWidget *label_countdown, *win;
  int countdown, countdown_handle;
} SAVE_SESS;

gboolean is_legal_en_char(char *ch);
static gboolean wsp_str(WSP_S *wsp, int wspN, char *out)
{
  int i;
  int ofs=0;
  gboolean is_en = TRUE;
  for(i=0;i<wspN;i++) {
	if (!is_legal_en_char(wsp[i].ch))
	  is_en = FALSE;	  
//    utf8_putchar(wsp[i].ch);
	int n = utf8cpy(out+ofs, wsp[i].ch);
	ofs+=n;
  }

  return is_en;
//  out[ofs]=0;
//  dbg(" c_len:%d wsp %s\n", c_len, out);
}


static void free_mywsp(SAVE_SESS *sess)
{
  free(sess->mywsp); sess->mywsp=NULL;
  free(sess);
}


static gboolean close_win_save_phrase(GtkWidget *widget, gpointer data)
{
  SAVE_SESS *sess = (SAVE_SESS *)data;

  g_source_remove(sess->countdown_handle);
  gtk_widget_destroy(sess->win);
  free_mywsp(sess);
  return TRUE;
}

#if 0
static gint delete_event( GtkWidget *widget,
                   GdkEvent  *event,
                   gpointer   data)
{
  free_mywsp(data);
  return FALSE;
}
#endif

extern int ph_key_sz;

static gboolean cb_ok(GtkWidget *widget, gpointer data)
{
  SAVE_SESS *sess = (SAVE_SESS *)data;
  g_source_remove(sess->countdown_handle);

  int i;
  phokey_t pho[MAX_PHRASE_LEN];
  u_int pho32[MAX_PHRASE_LEN];
  u_int64_t pho64[MAX_PHRASE_LEN];
  char tt[1024];
  void *dat;
  gboolean is_en = wsp_str(sess->mywsp, sess->mywspN, tt);

//  dbg("wsp_str %s is_en:%d\n", tt, is_en);

  if (is_en) {
	save_phrase_to_db(&en_hand, tt, NULL, sess->mywspN, 1);
  } else {
    if (tsin_hand.ph_key_sz==2) {
	  for(i=0;i<sess->mywspN;i++)
		pho[i] = sess->mywsp[i].key;
	  dat = pho;
	} else
	if (tsin_hand.ph_key_sz==4) {
      for(i=0;i< sess->mywspN;i++) {
		  pho32[i] = sess->mywsp[i].key;
	  }
	  dat = pho32;
    } else
    if (tsin_hand.ph_key_sz==8) {
	  for(i=0;i< sess->mywspN;i++)
		pho64[i] = sess->mywsp[i].key;
	  dat = pho64;
	}
	
	save_phrase_to_db(&tsin_hand, dat, tt, sess->mywspN, 1);
  }

  gtk_widget_destroy(sess->win);

  free_mywsp(sess);
  return TRUE;
}

static void disp_countdown(SAVE_SESS *sess)
{
  char tt[64];

  sprintf(tt, _(_L("%d 秒後自動加入")), sess->countdown);
  gtk_label_set_text(GTK_LABEL(sess->label_countdown), tt);
}


gboolean timeout_countdown(gpointer data)
{
  SAVE_SESS *sess = (SAVE_SESS *)data;

  if (!sess->countdown) {
    cb_ok(NULL, data);
    return FALSE;
  }

  sess->countdown--;
  disp_countdown(sess);
  return TRUE;
}


void create_win_save_phrase(WSP_S *wsp, int wspN)
{
#if WIN32
  if (test_mode)
    return;
#endif

  if (!wspN)
    return;

  SAVE_SESS *sess = tzmalloc(SAVE_SESS, 1);

  GtkWidget *main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
//  gtk_window_set_has_resize_grip(GTK_WINDOW(main_window), FALSE);
  gtk_window_set_keep_above(GTK_WINDOW(main_window), TRUE);
  sess->win = main_window;

  gtk_window_set_default_size(GTK_WINDOW (main_window), 20, 10);

  gtk_window_set_title(GTK_WINDOW(main_window), _(_L("加片語到詞庫")));

#if 0
  g_signal_connect (G_OBJECT (main_window), "delete_event",
                     G_CALLBACK (delete_event), sess);
#endif

  GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (main_window), vbox);

  char tt[512];  
  gboolean is_en = wsp_str(wsp, wspN, tt);
  
  tt[0] = 0;
  wsp_str(wsp, wspN, tt);
  gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new(tt), FALSE, FALSE, 0);

  int i;
  if (tsin_hand.ph_key_sz==2  && !is_en)
  for(i=0; i<wspN; i++) {
    strcat(tt, phokey_to_str(wsp[i].key));
    strcat(tt, " ");
  }

  if (tt[0])
    gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new(tt), FALSE, FALSE, 0);

  sess->mywsp = tmemdup(wsp, WSP_S, wspN);
  sess->mywspN = wspN;

  GtkWidget *hbox_cancel_ok = gtk_hbox_new (FALSE, 10);
  gtk_box_pack_start (GTK_BOX (vbox), hbox_cancel_ok , FALSE, FALSE, 5);

#if GTK_CHECK_VERSION(3,10,0)
  GtkWidget *button_ok = gtk_button_new_with_label ("OK");
#else
  GtkWidget *button_ok = gtk_button_new_from_stock (GTK_STOCK_OK);
#endif  
  gtk_box_pack_start (GTK_BOX (hbox_cancel_ok), button_ok, TRUE, TRUE, 5);
#if GTK_CHECK_VERSION(3,10,0)
  GtkWidget *button_cancel = gtk_button_new_with_label ("取消");
#else  
  GtkWidget *button_cancel = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
#endif  
  gtk_box_pack_start (GTK_BOX (hbox_cancel_ok), button_cancel, TRUE, TRUE, 0);

  sess->label_countdown = gtk_label_new(NULL);
  gtk_box_pack_start (GTK_BOX (vbox), sess->label_countdown, FALSE, FALSE, 5);

#if 1
#if WIN32
  set_no_focus(main_window);
#endif
  gtk_widget_realize(main_window);
#if UNIX
  set_no_focus(main_window);
#else
  win32_init_win(main_window);
#endif
#endif

//  dbg("mmmmmmmmmmmmm\n");

  GTK_WIDGET_SET_FLAGS (button_ok, GTK_CAN_DEFAULT);
  gtk_widget_grab_default (button_ok);


#if 1
//  dbg("main_window %x\n", main_window);
  g_signal_connect (G_OBJECT (button_cancel), "clicked",
                            G_CALLBACK (close_win_save_phrase),
                            sess);

  g_signal_connect (G_OBJECT (button_ok), "clicked",
                            G_CALLBACK (cb_ok),
                            sess);
#endif

  gtk_window_present(GTK_WINDOW(main_window));
  gtk_window_set_keep_above(GTK_WINDOW(main_window), TRUE);
//  gtk_window_set_modal(GTK_WINDOW(main_window), TRUE);

  sess->countdown = 3;
  disp_countdown(sess);
  sess->countdown_handle = g_timeout_add(1000, timeout_countdown, sess);
  gtk_widget_show_all(main_window);
}