forked from hime-ime/hime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hime-juyin-learn.cpp
198 lines (145 loc) · 4.86 KB
/
hime-juyin-learn.cpp
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
/* Copyright (C) 2011 Edward Der-Hua Liu, Hsin-Chu, Taiwan
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "hime.h"
#include "pho.h"
#include "config.h"
#if HIME_i18n_message
#include <libintl.h>
#endif
#include "gst.h"
#include "tsin.h"
GtkWidget *hbox_buttons;
char current_str[MAX_PHRASE_LEN*CH_SZ+1];
gboolean b_pinyin, is_chs;
static GtkClipboard *pclipboard;
GtkWidget *mainwin;
GtkTextBuffer *buffer;
void do_exit()
{
exit(0);
}
#define MAX_SAME_PHO 32
void all_wrap()
{
GtkTextIter mstart,mend;
gtk_text_buffer_get_bounds (buffer, &mstart, &mend);
gtk_text_buffer_apply_tag_by_name (buffer, "char_wrap", &mstart, &mend);
}
char *phokey2pinyin(phokey_t k);
static void selection_received(GtkClipboard *pclip, const gchar *text, gpointer data)
{
if (!text)
return;
char *outtext = NULL;
int outlen = 0;
while (*text) {
char tt[CH_SZ * 5 * MAX_SAME_PHO + 1];
int len = u8cpy(tt, (char *)text);
tt[len]=0;
phokey_t phokeys[MAX_SAME_PHO];
int phokeysN = utf8_pho_keys((char *)text, phokeys);
int i;
for(i=0; i <phokeysN ;i++) {
char *phostr;
if (b_pinyin)
phostr = phokey2pinyin(phokeys[i]);
else
phostr = phokey_to_str(phokeys[i]);
strcat(tt, phostr);
}
int ttlen = strlen(tt);
outtext = (char *)realloc(outtext, outlen + ttlen + 1);
memcpy(outtext + outlen, tt, ttlen);
outlen+=ttlen;
text += len;
}
outtext[outlen]=0;
gtk_text_buffer_set_text (buffer, outtext, -1);
free(outtext);
all_wrap();
}
void req_clipboard()
{
gtk_clipboard_request_text(pclipboard, selection_received, mainwin);
}
gboolean cb_button_fetch()
{
req_clipboard();
return TRUE;
}
void set_window_hime_icon(GtkWidget *window);
#if WIN32
void init_hime_program_files();
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
void init_TableDir();
#endif
gboolean is_pinyin_kbm();
int main(int argc, char **argv)
{
#if WIN32
init_TableDir();
#endif
gtk_init (&argc, &argv);
#if HIME_i18n_message
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
textdomain(GETTEXT_PACKAGE);
#endif
b_pinyin = is_pinyin_kbm();
pho_load();
mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_has_resize_grip(GTK_WINDOW(mainwin), FALSE);
gtk_window_set_default_size(GTK_WINDOW (mainwin), 640, 220);
set_window_hime_icon(mainwin);
GtkWidget *sw = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
GtkWidget *vbox_top = gtk_vbox_new (FALSE, 0);
gtk_orientable_set_orientation(GTK_ORIENTABLE(vbox_top), GTK_ORIENTATION_VERTICAL);
gtk_container_add (GTK_CONTAINER(mainwin), vbox_top);
GtkWidget *view = gtk_text_view_new ();
gtk_widget_set_hexpand (view, TRUE);
gtk_widget_set_vexpand (view, TRUE);
gtk_container_add (GTK_CONTAINER(sw), view);
gtk_box_pack_start (GTK_BOX (vbox_top), sw, TRUE, TRUE, 0);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
gtk_text_buffer_create_tag (buffer,
"blue_background", "background", "blue", "foreground", "white", NULL);
gtk_text_buffer_create_tag (buffer, "char_wrap",
"wrap_mode", GTK_WRAP_CHAR, NULL);
hbox_buttons = gtk_hbox_new (FALSE, 0);
gtk_box_pack_start (GTK_BOX (vbox_top), hbox_buttons, FALSE, FALSE, 0);
GtkWidget *button_fetch = gtk_button_new_with_label(_(_L("自剪貼區更新")));
gtk_box_pack_start (GTK_BOX (hbox_buttons), button_fetch, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (button_fetch), "clicked",
G_CALLBACK (cb_button_fetch), NULL);
GtkWidget *button_exit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
gtk_box_pack_start (GTK_BOX (hbox_buttons), button_exit, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (button_exit), "clicked",
G_CALLBACK (do_exit), NULL);
g_signal_connect (G_OBJECT (mainwin), "delete_event",
G_CALLBACK (do_exit), NULL);
gtk_widget_show_all(mainwin);
#if UNIX
pclipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
#else
pclipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
#endif
req_clipboard();
gtk_main();
return 0;
}