00001 00006 #include <gtk/gtk.h> 00007 #include "dict.h" 00008 #include "gui_top_window.h" 00009 00021 int 00022 dict_clistcmp (int n, const char *key, GtkCList * c) 00023 { 00024 gchar *s; 00025 gtk_clist_get_text (c, n, 0, &s); 00026 return (strcmp (key, s)); 00027 } 00028 00046 int 00047 mybsearch (int n, int (*cmp) (int, gpointer, gpointer), const gpointer key, 00048 gpointer data) 00049 { 00050 int min = 0, max = n - 1, try, r; 00051 while (min != max) 00052 { 00053 try = (min + max) / 2; 00054 r = cmp (try, key, data); 00055 if (r <= 0) 00056 max = try; 00057 else 00058 min = try + 1; 00059 } 00060 return (min); 00061 } 00062 00077 void 00078 dict_inpch (GtkEntry * e, GtkObject * w) 00079 { 00080 const char *s = gtk_entry_get_text (e); 00081 gchar *pszsel; 00082 int n = *(int *) gtk_object_get_data (w, "dict_wnum"), r; 00083 GtkAdjustment *a; 00084 GtkCList *c = GTK_CLIST (gtk_object_get_data (w, "dict_clist")); 00085 r = 00086 mybsearch (n, (int (*)(int, gpointer, gpointer)) dict_clistcmp, 00087 (const gpointer) s, c); 00088 gtk_clist_select_row (c, r, 0); 00089 gtk_clist_get_text (c, r, 0, &pszsel); 00090 a = gtk_clist_get_vadjustment (c); 00091 gtk_adjustment_set_value (a, a->upper / n * r); 00092 gtk_clist_set_vadjustment (c, a); 00093 } 00094 00099 GtkWidget * 00100 dict_new () 00101 { 00102 int *wnum = (int *) malloc (sizeof (int)); 00103 char ****words = (char ****) malloc (sizeof (char ***)); 00104 char *def_titles[] = { "English", "Hungarian" }; 00105 GtkBox *w, *inpdic; 00106 GtkEntry *inp; 00107 GtkCList *dic; 00108 GtkScrolledWindow *dicscroll; 00109 *wnum = 0, *words = NULL; 00110 w = GTK_BOX (gtk_hbox_new (FALSE, 2)); 00111 gtk_object_set_data (GTK_OBJECT (w), "dict_wnum", (gpointer) (wnum)); 00112 gtk_object_set_data (GTK_OBJECT (w), "dict_words", (gpointer) (words)); 00113 inpdic = GTK_BOX (gtk_vbox_new (FALSE, 2)); 00114 inp = GTK_ENTRY (gtk_entry_new ()); 00115 gtk_entry_set_editable (inp, TRUE); 00116 gtk_widget_show (GTK_WIDGET (inp)); 00117 gtk_box_pack_start (inpdic, GTK_WIDGET (inp), FALSE, TRUE, 2); 00118 dicscroll = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL)); 00119 gtk_scrolled_window_set_policy (dicscroll, GTK_POLICY_AUTOMATIC, 00120 GTK_POLICY_ALWAYS); 00121 dic = GTK_CLIST (gtk_clist_new_with_titles (2, def_titles)); 00122 00123 gtk_object_set_data (GTK_OBJECT (w), "dict_clist", (gpointer) (dic)); 00124 gtk_signal_connect (GTK_OBJECT (inp), "changed", 00125 GTK_SIGNAL_FUNC (dict_inpch), (gpointer) w); 00126 gtk_object_set_data (GTK_OBJECT (w), "dict_clist", (gpointer) (dic)); 00127 gtk_container_add (GTK_CONTAINER (dicscroll), GTK_WIDGET (dic)); 00128 gtk_widget_show (GTK_WIDGET (dic)); 00129 gtk_box_pack_start (inpdic, GTK_WIDGET (dicscroll), TRUE, TRUE, 2); 00130 gtk_widget_show (GTK_WIDGET (dicscroll)); 00131 gtk_box_pack_start (w, GTK_WIDGET (inpdic), TRUE, TRUE, 2); 00132 gtk_widget_show (GTK_WIDGET (inpdic)); 00133 return (GTK_WIDGET (w)); 00134 } 00135 00148 void 00149 dict_add_words (GtkWidget * dict, int wnum, char ***words) 00150 { 00151 int *pwnum, i; 00152 char ****pwords, **ws; 00153 GtkCList *pclist; 00154 pwnum = (int *) gtk_object_get_data (GTK_OBJECT (dict), "dict_wnum"); 00155 pwords = (char ****) gtk_object_get_data (GTK_OBJECT (dict), "dict_words"); 00156 pclist = (GtkCList *) gtk_object_get_data (GTK_OBJECT (dict), "dict_clist"); 00157 *pwords = (char ***) realloc (*pwords, (*pwnum + wnum) * sizeof (char **)); 00158 for (i = 0; i < wnum; i++) 00159 (*pwords)[*pwnum + i] = words[i]; 00160 *pwnum += wnum; 00161 for (i = 0; i < wnum; i++) 00162 gtk_clist_append (pclist, words[i]); 00163 } 00164 00172 void 00173 dict_add_word (GtkWidget * dict, char **word) 00174 { 00175 dict_add_words (dict, 1, &word); 00176 } 00177 00187 GtkWidget * 00188 dict_new_from_file (const char *fn, const char *sep) 00189 { 00190 GtkWidget *dict; 00191 int wnum; 00192 char ***words; 00193 dict = dict_new (); 00194 words = dict_get_words (fn, sep, &wnum); 00195 dict_add_words (dict, wnum, words); 00196 return (dict); 00197 } 00198 00209 void 00210 gui_dict_open2_cb (GtkWidget * opencb) 00211 { 00212 GtkWidget *dict; 00213 const gchar *fn = 00214 gtk_file_selection_get_filename (GTK_FILE_SELECTION (opencb)); 00215 dict = dict_new_from_file (fn, " -> "); 00216 gtk_notebook_append_page (notebook, dict, gtk_label_new (fn)); 00217 gtk_widget_show (dict); 00218 } 00219 00227 void 00228 gui_dict_open_cb () 00229 { 00230 GtkWidget *opencb; 00231 opencb = gtk_file_selection_new ("Select dictionary to open"); 00232 gtk_window_set_modal (GTK_WINDOW (opencb), TRUE); 00233 g_signal_connect_swapped (GTK_OBJECT 00234 (GTK_FILE_SELECTION (opencb)->ok_button), 00235 "clicked", G_CALLBACK (gui_dict_open2_cb), 00236 opencb); 00237 g_signal_connect_swapped (GTK_OBJECT 00238 (GTK_FILE_SELECTION (opencb)->ok_button), 00239 "clicked", G_CALLBACK (gtk_widget_destroy), 00240 opencb); 00241 g_signal_connect_swapped (GTK_OBJECT 00242 (GTK_FILE_SELECTION (opencb)->cancel_button), 00243 "clicked", G_CALLBACK (gtk_widget_destroy), 00244 opencb); 00245 gtk_file_selection_set_filename (GTK_FILE_SELECTION (opencb), 00246 "/usr/share/dict/"); 00247 gtk_widget_show (opencb); 00248 } 00249 00254 void 00255 gui_dict_close_cb () 00256 { 00257 gtk_notebook_remove_page (notebook, 00258 gtk_notebook_get_current_page (notebook)); 00259 } 00260 00261