Debian Codegraph @

src/win-pho-near.c

raw symbols SCIP
1 /* Copyright (C) 2011 Edward Der-Hua Liu, Hsin-Chu, Taiwan
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation version 2.1
6 * of the License.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #include <sys/stat.h>
19
20 #include "hime.h"
21
22 #include "gtab.h"
23 #include "pho.h"
24
25 static GtkWidget *win_pho_near = NULL;
26
27 static struct {
28 int type;
29 char group[5];
30 } groups[] = {
31 {0, {15, 19, -1}}, // ㄓㄗ, 以 -1 做結尾
32 {0, {16, 20, -1}}, // ㄔㄘ
33 {0, {8, 18, -1}}, // ㄌㄖ
34 {0, {4, 11, -1}}, // ㄈㄏ
35 {0, {17, 21, -1}}, // ㄕㄙ
36 {2, {2, 3, -1}}, // ㄛㄜ
37 {2, {10, 12, -1}}, // ㄣㄥ
38 };
39
40 static int groupsN = sizeof (groups) / sizeof (groups[0]);
41
42 static char *find_group (int type, int num) {
43 int i;
44
45 for (i = 0; i < groupsN; i++) {
46 if (groups[i].type != type)
47 continue;
48
49 char *p;
50 for (p = groups[i].group; *p > 0; p++)
51 if (*p == num) {
52 return groups[i].group;
53 }
54 }
55
56 static char alone[2] = {-1, -1};
57 alone[0] = num;
58
59 return alone;
60 }
61
62 void key_typ_pho (phokey_t phokey, u_char rtyp_pho[]);
63 gboolean get_start_stop_idx (phokey_t key, int *start_i, int *stop_i);
64 void close_win_pho_near ();
65
66 typedef struct {
67 GtkWidget *label;
68 phokey_t pk;
69 } NEAR_ENTRY;
70
71 static NEAR_ENTRY *near_entries;
72 static int near_entriesN;
73 gboolean add_to_tsin_buf (char *str, phokey_t *pho, int len);
74 void tsin_remove_last ();
75
76 static void cb_sel (GtkWidget *button, gpointer user_data) {
77 NEAR_ENTRY *near_entry = &near_entries[GPOINTER_TO_INT (user_data)];
78 const char *ch = gtk_label_get_text (GTK_LABEL (near_entry->label));
79 char tt[CH_SZ + 1];
80 strcpy (tt, ch);
81
82 tsin_remove_last ();
83 char_play (tt);
84 add_to_tsin_buf (tt, &near_entry->pk, 1);
85
86 close_win_pho_near ();
87 }
88
89 char *phokey2pinyin (phokey_t k);
90
91 void create_win_pho_near (phokey_t pho) {
92 if (win_pho_near)
93 close_win_pho_near ();
94
95 win_pho_near = gtk_window_new (GTK_WINDOW_TOPLEVEL);
96 gtk_window_set_has_resize_grip (GTK_WINDOW (win_pho_near), FALSE);
97 gtk_widget_realize (win_pho_near);
98 set_no_focus (win_pho_near);
99
100 GtkWidget *frame = gtk_frame_new (NULL);
101 gtk_container_add (GTK_CONTAINER (win_pho_near), frame);
102
103 GtkWidget *vbox_top = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
104 gtk_orientable_set_orientation (GTK_ORIENTABLE (vbox_top), GTK_ORIENTATION_VERTICAL);
105 gtk_container_add (GTK_CONTAINER (frame), vbox_top);
106 gtk_container_set_border_width (GTK_CONTAINER (vbox_top), 0);
107
108 char t_typ_pho[4], mtyp_pho[4];
109 key_typ_pho (pho, (u_char *) t_typ_pho);
110 memcpy (mtyp_pho, t_typ_pho, 4);
111
112 char *group0;
113 char *group2;
114 gboolean b_first = TRUE;
115
116 // dbg("zz %d %d\n",t_typ_pho[0], t_typ_pho[2]);
117
118 for (group0 = find_group (0, t_typ_pho[0]); *group0 >= 0; group0++) {
119 mtyp_pho[0] = *group0;
120 // dbg("%d\n",mtyp_pho[0]);
121
122 for (group2 = find_group (2, t_typ_pho[2]); *group2 >= 0; group2++) {
123 mtyp_pho[2] = *group2;
124
125 for (mtyp_pho[3] = 0; mtyp_pho[3] < 5; mtyp_pho[3]++) {
126 // dbg(" %d\n",mtyp_pho[2]);
127 phokey_t pk = pho2key (mtyp_pho);
128 char *pho_str = pin_juyin ? phokey2pinyin (pk) : phokey_to_str (pk);
129
130 int start_i, stop_i;
131
132 if (!get_start_stop_idx (pk, &start_i, &stop_i))
133 continue;
134
135 if (b_first) {
136 b_first = FALSE;
137 } else {
138 GtkWidget *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
139 gtk_box_pack_start (GTK_BOX (vbox_top), separator, FALSE, FALSE, 0);
140 }
141
142 GtkWidget *hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
143 gtk_box_pack_start (GTK_BOX (vbox_top), hbox, FALSE, FALSE, 0);
144
145 GtkWidget *label_pho = gtk_label_new (pho_str);
146 gtk_box_pack_start (GTK_BOX (hbox), label_pho, FALSE, FALSE, 0);
147 set_label_font_size (label_pho, hime_font_size_pho_near);
148
149 int i;
150 for (i = start_i; i < stop_i; i++) {
151 char tt[CH_SZ + 1];
152 memset (tt, 0, sizeof (tt));
153 utf8cpy (tt, pho_idx_str (i));
154
155 GtkWidget *button = gtk_button_new ();
156 gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
157
158 GtkWidget *label_pho = gtk_label_new (tt);
159 gtk_container_add (GTK_CONTAINER (button), label_pho);
160 set_label_font_size (label_pho, hime_font_size_pho_near);
161
162 near_entries = trealloc (near_entries, NEAR_ENTRY, near_entriesN);
163 near_entries[near_entriesN].label = label_pho;
164 near_entries[near_entriesN].pk = pk;
165 g_signal_connect (G_OBJECT (button), "clicked",
166 G_CALLBACK (cb_sel), GINT_TO_POINTER (near_entriesN));
167
168 near_entriesN++;
169 }
170 }
171 }
172 }
173
174 gtk_widget_show_all (win_pho_near);
175 }
176
177 void close_win_pho_near () {
178 if (!win_pho_near)
179 return;
180
181 gtk_widget_destroy (win_pho_near);
182 win_pho_near = NULL;
183
184 free (near_entries);
185 near_entriesN = 0;
186 near_entries = NULL;
187 }