OSDN Git Service

Initial commit
[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     gunichar2 *kanji = recognize_kanji(strokes);
79     kp_add_characters(ka, kanji);
80 }
81
82 static gboolean accel_activate_cb(GtkAccelGroup *accelgroup, GObject *arg1, guint arg2, GdkModifierType arg3, gpointer user_data)
83 {
84     gulong keyval = arg2;
85     gulong mask = arg3;
86     if((mask == 0x4) && (keyval == 0x71))
87         gtk_main_quit();
88     return FALSE;
89 }
90
91 int main (int argc, char *argv[])
92 {
93     GtkWidget *wnd, *vbox, *hbox, *hbox1, *padarea, *karea, *annotate, *tmp;
94
95     gtk_init (&argc, &argv);
96
97     wnd = gtk_window_new (GTK_WINDOW_TOPLEVEL);
98     gtk_window_set_resizable(GTK_WINDOW(wnd), TRUE);
99     gtk_window_set_default_size(GTK_WINDOW(wnd), 400, 300);
100     gtk_window_set_title(GTK_WINDOW(wnd), "kp123");
101     g_signal_connect(G_OBJECT(wnd), "destroy", G_CALLBACK(gtk_main_quit), NULL);
102
103     vbox = gtk_vbox_new(FALSE, 0);
104     gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
105     gtk_container_add(GTK_CONTAINER(wnd), vbox);
106
107     tmp = gtk_frame_new(NULL);
108     gtk_frame_set_shadow_type(GTK_FRAME(tmp), GTK_SHADOW_IN);
109     gtk_box_pack_start(GTK_BOX(vbox), tmp, TRUE, TRUE, 0);
110
111     hbox1 = gtk_hbox_new(FALSE, 0);
112     gtk_container_add(GTK_CONTAINER(tmp), hbox1);
113
114     padarea = g_object_new(KP_TYPE_PADAREA, NULL);
115     gtk_box_pack_start(GTK_BOX(hbox1), padarea, TRUE, TRUE, 0);
116
117     tmp = gtk_vseparator_new();
118     gtk_box_pack_start(GTK_BOX(hbox1), tmp, FALSE, FALSE, 0);
119
120     GtkListStore *store = gtk_list_store_new(1, G_TYPE_STRING);
121     karea = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
122     gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(karea), FALSE);
123     gtk_tree_view_set_enable_search(GTK_TREE_VIEW(karea), FALSE);
124     gtk_widget_set_size_request(karea, 50, -1);
125     GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
126     renderer->xalign = 0.5;
127     renderer->yalign = 0.5;
128     renderer->xpad = 5;
129     renderer->ypad = 5;
130     GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes("kanji", GTK_CELL_RENDERER(renderer), "text", 0, NULL);
131     gtk_tree_view_insert_column(GTK_TREE_VIEW(karea), column, -1);
132     gtk_box_pack_start(GTK_BOX(hbox1), karea, FALSE, FALSE, 0);
133     GtkTreeSelection *select = gtk_tree_view_get_selection(GTK_TREE_VIEW(karea));
134     gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE);
135     g_signal_connect(G_OBJECT(select), "changed", G_CALLBACK(kp_selection_changed), (gpointer)hbox1);
136     g_signal_connect(G_OBJECT(padarea), "stroke_add_remove", G_CALLBACK(kp_stroke_add_remove_cb), (gpointer)karea);
137
138     hbox = gtk_hbox_new(FALSE, 0);
139     gtk_container_set_border_width(GTK_CONTAINER(hbox), 3);
140     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
141
142     annotate = gtk_check_button_new_with_label(_("Annotate"));
143     g_signal_connect(G_OBJECT(annotate), "toggled", G_CALLBACK(kp_annotate_cb), (gpointer)padarea);
144     gtk_box_pack_start(GTK_BOX(hbox), annotate, FALSE, FALSE, 0);
145
146     tmp = kp_toolbar_btn(GTK_STOCK_QUIT, G_CALLBACK(gtk_main_quit), NULL, _("Quit"));
147     gtk_box_pack_start(GTK_BOX(hbox), tmp, FALSE, FALSE, 20);
148
149     tmp = kp_toolbar_btn(GTK_STOCK_CLEAR, G_CALLBACK(kp_clear_cb), (gpointer)padarea, _("Clear"));
150     gtk_box_pack_start(GTK_BOX(hbox), tmp, FALSE, FALSE, 20);
151
152     tmp = kp_toolbar_btn(GTK_STOCK_UNDO, G_CALLBACK(kp_undo_cb), (gpointer)padarea, _("Undo"));
153     gtk_box_pack_start(GTK_BOX(hbox), tmp, FALSE, FALSE, 20);
154
155     GtkAccelGroup *accel_group = gtk_accel_group_new();
156     gtk_window_add_accel_group(GTK_WINDOW(wnd), accel_group);
157     GClosure *closure = g_cclosure_new(G_CALLBACK(accel_activate_cb), NULL, NULL);
158     gtk_accel_group_connect(accel_group, 0x71, 0x4, GTK_ACCEL_VISIBLE, closure);
159
160     gtk_widget_show_all(wnd);
161
162     gtk_main ();
163
164     return 0;
165 }
166