OSDN Git Service

modified: src/kp123.c
[kp123/kp123.git] / src / kp123.c
1
2 #include "defs.h"
3 #include "intl.h"
4 #include "padarea.h"
5
6 #include "recognize_kanji.h"
7
8 static GtkWidget* kp_toolbar_btn(gchar *id, GCallback cb, gpointer data, gchar *tooltip_text)
9 {
10     GtkWidget *btn, *image;
11     btn = gtk_button_new();
12
13     image = gtk_image_new_from_stock(id, GTK_ICON_SIZE_SMALL_TOOLBAR);
14     gtk_container_add(GTK_CONTAINER(btn), image);
15
16     gtk_button_set_relief(GTK_BUTTON(btn), GTK_RELIEF_NONE);
17     g_signal_connect(G_OBJECT(btn), "clicked", G_CALLBACK(cb), data);
18     gtk_widget_set_tooltip_text(btn, tooltip_text);
19     return btn;
20 }
21
22 static void kp_annotate_cb(GtkWidget *w, gpointer data)
23 {
24     kp_padarea_annotate(KP_PADAREA(data), gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)));
25 }
26
27 static void kp_clear_cb(GtkWidget *w, gpointer data)
28 {
29     KpPadArea *pa = KP_PADAREA(data);
30     kp_padarea_clear(pa);
31 }
32
33 static void kp_undo_cb(GtkWidget *w, gpointer data)
34 {
35     KpPadArea *pa = KP_PADAREA(data);
36     kp_padarea_undo(pa);
37 }
38
39 static void kp_add_characters(GtkTreeView *list, gunichar2 *kanji)
40 {
41     GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model(list));
42     GtkTreeIter iter;
43     gint i, h;
44     GtkTreeViewColumn *column = gtk_tree_view_get_column(list, 0);
45     gtk_list_store_clear(store);
46     if(!kanji) return;
47     gtk_tree_view_column_cell_get_size(column, NULL, NULL, NULL, NULL, &h);
48     GtkAllocation allocation;
49     gtk_widget_get_allocation(GTK_WIDGET(list), &allocation);
50     for(i = 0; kanji[i]; i++)
51     {
52         if(h*(i + 2) > allocation.height)
53             break;
54         gchar *utfstr = g_convert((gchar*)&(kanji[i]), 2, "UTF-8", "UTF-16LE", NULL, NULL, NULL);
55         gtk_list_store_append(store, &iter);
56         gtk_list_store_set(store, &iter, 0, utfstr, -1);
57         g_free(utfstr);
58     }
59 }
60
61 static void kp_selection_changed(GtkTreeSelection *selection, gpointer data)
62 {
63     GtkTreeIter iter;
64     GtkTreeModel *model;
65     gchar *sel;
66
67     if(!gtk_tree_selection_get_selected(selection, &model, &iter))
68         return;
69     gtk_tree_model_get(model, &iter, 0, &sel, -1);
70     gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), sel, -1);
71     gtk_clipboard_set_text(gtk_clipboard_get(NULL), sel, -1);
72 }
73
74 static void kp_stroke_add_remove_cb(KpPadArea *pa, gpointer data)
75 {
76     GtkTreeView *ka = GTK_TREE_VIEW(data);
77     GList *strokes = pa->strokes;
78     g_printf("recognize\n");
79     gunichar2 *kanji = recognize_kanji(strokes);
80     kp_add_characters(ka, kanji);
81 }
82
83 static gboolean accel_activate_cb(GtkAccelGroup *accelgroup, GObject *arg1, guint arg2, GdkModifierType arg3, gpointer user_data)
84 {
85     gulong keyval = arg2;
86     gulong mask = arg3;
87     if((mask == 0x4) && (keyval == 0x71))
88         gtk_main_quit();
89     return FALSE;
90 }
91
92 int main (int argc, char *argv[])
93 {
94     GtkWidget *wnd, *vbox, *hbox, *hbox1, *padarea, *karea, *annotate, *tmp;
95
96     gtk_init (&argc, &argv);
97
98     wnd = gtk_window_new (GTK_WINDOW_TOPLEVEL);
99     gtk_window_set_resizable(GTK_WINDOW(wnd), TRUE);
100     gtk_window_set_default_size(GTK_WINDOW(wnd), 400, 300);
101     gtk_window_set_title(GTK_WINDOW(wnd), "kp123");
102     g_signal_connect(G_OBJECT(wnd), "destroy", G_CALLBACK(gtk_main_quit), NULL);
103
104     vbox = gtk_vbox_new(FALSE, 0);
105     gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
106     gtk_container_add(GTK_CONTAINER(wnd), vbox);
107
108     tmp = gtk_frame_new(NULL);
109     gtk_frame_set_shadow_type(GTK_FRAME(tmp), GTK_SHADOW_IN);
110     gtk_box_pack_start(GTK_BOX(vbox), tmp, TRUE, TRUE, 0);
111
112     hbox1 = gtk_hbox_new(FALSE, 0);
113     gtk_container_add(GTK_CONTAINER(tmp), hbox1);
114
115     padarea = g_object_new(KP_TYPE_PADAREA, NULL);
116     gtk_box_pack_start(GTK_BOX(hbox1), padarea, TRUE, TRUE, 0);
117
118     tmp = gtk_vseparator_new();
119     gtk_box_pack_start(GTK_BOX(hbox1), tmp, FALSE, FALSE, 0);
120
121     GtkListStore *store = gtk_list_store_new(1, G_TYPE_STRING);
122     karea = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
123     gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(karea), FALSE);
124     gtk_tree_view_set_enable_search(GTK_TREE_VIEW(karea), FALSE);
125     gtk_widget_set_size_request(karea, 50, -1);
126     GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
127     renderer->xalign = 0.5;
128     renderer->yalign = 0.5;
129     renderer->xpad = 5;
130     renderer->ypad = 5;
131     GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes("kanji", GTK_CELL_RENDERER(renderer), "text", 0, NULL);
132     gtk_tree_view_insert_column(GTK_TREE_VIEW(karea), column, -1);
133     gtk_box_pack_start(GTK_BOX(hbox1), karea, FALSE, FALSE, 0);
134     GtkTreeSelection *select = gtk_tree_view_get_selection(GTK_TREE_VIEW(karea));
135     gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE);
136     g_signal_connect(G_OBJECT(select), "changed", G_CALLBACK(kp_selection_changed), (gpointer)hbox1);
137     g_signal_connect(G_OBJECT(padarea), "stroke_add_remove", G_CALLBACK(kp_stroke_add_remove_cb), (gpointer)karea);
138
139     hbox = gtk_hbox_new(FALSE, 0);
140     gtk_container_set_border_width(GTK_CONTAINER(hbox), 3);
141     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
142
143     annotate = gtk_check_button_new_with_label(_("Annotate"));
144     g_signal_connect(G_OBJECT(annotate), "toggled", G_CALLBACK(kp_annotate_cb), (gpointer)padarea);
145     gtk_box_pack_start(GTK_BOX(hbox), annotate, FALSE, FALSE, 0);
146
147     tmp = kp_toolbar_btn(GTK_STOCK_QUIT, G_CALLBACK(gtk_main_quit), NULL, _("Quit"));
148     gtk_box_pack_start(GTK_BOX(hbox), tmp, FALSE, FALSE, 20);
149
150     tmp = kp_toolbar_btn(GTK_STOCK_CLEAR, G_CALLBACK(kp_clear_cb), (gpointer)padarea, _("Clear"));
151     gtk_box_pack_start(GTK_BOX(hbox), tmp, FALSE, FALSE, 20);
152
153     tmp = kp_toolbar_btn(GTK_STOCK_UNDO, G_CALLBACK(kp_undo_cb), (gpointer)padarea, _("Undo"));
154     gtk_box_pack_start(GTK_BOX(hbox), tmp, FALSE, FALSE, 20);
155
156     GtkAccelGroup *accel_group = gtk_accel_group_new();
157     gtk_window_add_accel_group(GTK_WINDOW(wnd), accel_group);
158     GClosure *closure = g_cclosure_new(G_CALLBACK(accel_activate_cb), NULL, NULL);
159     gtk_accel_group_connect(accel_group, 0x71, 0x4, GTK_ACCEL_VISIBLE, closure);
160
161     gtk_widget_show_all(wnd);
162
163     gtk_main ();
164
165     return 0;
166 }
167