]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-spell-dialog.c
Use empathy_file_lookup for glade files since some are in libempathy-gtk/ and others...
[empathy.git] / libempathy-gtk / empathy-spell-dialog.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2004-2007 Imendio AB
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22
23 #include <string.h>
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtkcellrenderertext.h>
27 #include <gtk/gtkdialog.h>
28 #include <gtk/gtklabel.h>
29 #include <gtk/gtkliststore.h>
30 #include <gtk/gtktreeview.h>
31 #include <gtk/gtktreeselection.h>
32 #include <gtk/gtksizegroup.h>
33 #include <glade/glade.h>
34
35 #include <libempathy/empathy-utils.h>
36
37 #include "empathy-chat.h"
38 #include "empathy-spell-dialog.h"
39 #include "empathy-ui-utils.h"
40
41 typedef struct {
42         GtkWidget   *window;
43         GtkWidget   *button_replace;
44         GtkWidget   *label_word;
45         GtkWidget   *treeview_words;
46
47         EmpathyChat  *chat;
48
49         gchar       *word;
50         GtkTextIter  start;
51         GtkTextIter  end;
52 } EmpathySpellDialog;
53
54 enum {
55         COL_SPELL_WORD,
56         COL_SPELL_COUNT
57 };
58
59 static void spell_dialog_model_populate_columns     (EmpathySpellDialog *dialog);
60 static void spell_dialog_model_populate_suggestions (EmpathySpellDialog *dialog);
61 static void spell_dialog_model_row_activated_cb     (GtkTreeView       *tree_view,
62                                                      GtkTreePath       *path,
63                                                      GtkTreeViewColumn *column,
64                                                      EmpathySpellDialog *dialog);
65 static void spell_dialog_model_selection_changed_cb (GtkTreeSelection  *treeselection,
66                                                      EmpathySpellDialog *dialog);
67 static void spell_dialog_model_setup                (EmpathySpellDialog *dialog);
68 static void spell_dialog_response_cb                (GtkWidget         *widget,
69                                                      gint               response,
70                                                      EmpathySpellDialog *dialog);
71 static void spell_dialog_destroy_cb                 (GtkWidget         *widget,
72                                                      EmpathySpellDialog *dialog);
73
74 static void
75 spell_dialog_model_populate_columns (EmpathySpellDialog *dialog)
76 {
77         GtkTreeModel      *model;
78         GtkTreeViewColumn *column;
79         GtkCellRenderer   *renderer;
80         guint              col_offset;
81
82         model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview_words));
83
84         renderer = gtk_cell_renderer_text_new ();
85         col_offset = gtk_tree_view_insert_column_with_attributes (
86                 GTK_TREE_VIEW (dialog->treeview_words),
87                 -1, _("Word"),
88                 renderer,
89                 "text", COL_SPELL_WORD,
90                 NULL);
91
92         g_object_set_data (G_OBJECT (renderer),
93                            "column", GINT_TO_POINTER (COL_SPELL_WORD));
94
95         column = gtk_tree_view_get_column (GTK_TREE_VIEW (dialog->treeview_words), col_offset - 1);
96         gtk_tree_view_column_set_sort_column_id (column, COL_SPELL_WORD);
97         gtk_tree_view_column_set_resizable (column, FALSE);
98         gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
99 }
100
101 static void
102 spell_dialog_model_populate_suggestions (EmpathySpellDialog *dialog)
103 {
104         GtkTreeModel *model;
105         GtkListStore *store;
106         GList        *suggestions, *l;
107
108         model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview_words));
109         store = GTK_LIST_STORE (model);
110
111         suggestions = empathy_spell_get_suggestions (dialog->word);
112         for (l = suggestions; l; l=l->next) {
113                 GtkTreeIter  iter;
114                 gchar       *word;
115
116                 word = l->data;
117
118                 gtk_list_store_append (store, &iter);
119                 gtk_list_store_set (store, &iter,
120                                     COL_SPELL_WORD, word,
121                                     -1);
122         }
123
124         empathy_spell_free_suggestions (suggestions);
125 }
126
127 static void
128 spell_dialog_model_row_activated_cb (GtkTreeView       *tree_view,
129                                GtkTreePath       *path,
130                                GtkTreeViewColumn *column,
131                                EmpathySpellDialog *dialog)
132 {
133         spell_dialog_response_cb (dialog->window, GTK_RESPONSE_OK, dialog);
134 }
135
136 static void
137 spell_dialog_model_selection_changed_cb (GtkTreeSelection  *treeselection,
138                                    EmpathySpellDialog *dialog)
139 {
140         gint count;
141
142         count = gtk_tree_selection_count_selected_rows (treeselection);
143         gtk_widget_set_sensitive (dialog->button_replace, (count == 1));
144 }
145
146 static void
147 spell_dialog_model_setup (EmpathySpellDialog *dialog)
148 {
149         GtkTreeView      *view;
150         GtkListStore     *store;
151         GtkTreeSelection *selection;
152
153         view = GTK_TREE_VIEW (dialog->treeview_words);
154
155         g_signal_connect (view, "row-activated",
156                           G_CALLBACK (spell_dialog_model_row_activated_cb),
157                           dialog);
158
159         store = gtk_list_store_new (COL_SPELL_COUNT,
160                                     G_TYPE_STRING);   /* word */
161
162         gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
163
164         selection = gtk_tree_view_get_selection (view);
165         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
166
167         g_signal_connect (selection, "changed",
168                           G_CALLBACK (spell_dialog_model_selection_changed_cb),
169                           dialog);
170
171         spell_dialog_model_populate_columns (dialog);
172         spell_dialog_model_populate_suggestions (dialog);
173
174         g_object_unref (store);
175 }
176
177 static void
178 spell_dialog_destroy_cb (GtkWidget         *widget,
179                          EmpathySpellDialog *dialog)
180 {
181         g_object_unref (dialog->chat);
182         g_free (dialog->word);
183
184         g_free (dialog);
185 }
186
187 static void
188 spell_dialog_response_cb (GtkWidget         *widget,
189                           gint               response,
190                           EmpathySpellDialog *dialog)
191 {
192         if (response == GTK_RESPONSE_OK) {
193                 GtkTreeView      *view;
194                 GtkTreeModel     *model;
195                 GtkTreeSelection *selection;
196                 GtkTreeIter       iter;
197
198                 gchar            *new_word;
199
200                 view = GTK_TREE_VIEW (dialog->treeview_words);
201                 selection = gtk_tree_view_get_selection (view);
202
203                 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
204                         return;
205                 }
206
207                 gtk_tree_model_get (model, &iter, COL_SPELL_WORD, &new_word, -1);
208
209                 empathy_chat_correct_word (dialog->chat,
210                                           dialog->start,
211                                           dialog->end,
212                                           new_word);
213
214                 g_free (new_word);
215         }
216
217         gtk_widget_destroy (dialog->window);
218 }
219
220 void
221 empathy_spell_dialog_show (EmpathyChat  *chat,
222                           GtkTextIter  start,
223                           GtkTextIter  end,
224                           const gchar *word)
225 {
226         EmpathySpellDialog *dialog;
227         GladeXML          *gui;
228         gchar             *str;
229         gchar             *filename;
230
231         g_return_if_fail (chat != NULL);
232         g_return_if_fail (word != NULL);
233
234         dialog = g_new0 (EmpathySpellDialog, 1);
235
236         dialog->chat = g_object_ref (chat);
237
238         dialog->word = g_strdup (word);
239
240         dialog->start = start;
241         dialog->end = end;
242
243         filename = empathy_file_lookup ("empathy-spell-dialog.glade",
244                                         "libempathy-gtk");
245         gui = empathy_glade_get_file (filename,
246                                      "spell_dialog",
247                                      NULL,
248                                      "spell_dialog", &dialog->window,
249                                      "button_replace", &dialog->button_replace,
250                                      "label_word", &dialog->label_word,
251                                      "treeview_words", &dialog->treeview_words,
252                                      NULL);
253         g_free (filename);
254
255         empathy_glade_connect (gui,
256                               dialog,
257                               "spell_dialog", "response", spell_dialog_response_cb,
258                               "spell_dialog", "destroy", spell_dialog_destroy_cb,
259                               NULL);
260
261         g_object_unref (gui);
262
263         str = g_strdup_printf ("%s:\n<b>%s</b>",
264                                _("Suggestions for the word"),
265                                word);
266
267         gtk_label_set_markup (GTK_LABEL (dialog->label_word), str);
268         g_free (str);
269
270         spell_dialog_model_setup (dialog);
271
272         gtk_widget_show (dialog->window);
273 }