1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2004-2007 Imendio AB
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.
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.
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.
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>
35 #include <libempathy/empathy-utils.h>
37 #include "empathy-chat.h"
38 #include "empathy-spell-dialog.h"
39 #include "empathy-ui-utils.h"
43 GtkWidget *button_replace;
44 GtkWidget *label_word;
45 GtkWidget *treeview_words;
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,
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,
70 EmpathySpellDialog *dialog);
71 static void spell_dialog_destroy_cb (GtkWidget *widget,
72 EmpathySpellDialog *dialog);
75 spell_dialog_model_populate_columns (EmpathySpellDialog *dialog)
78 GtkTreeViewColumn *column;
79 GtkCellRenderer *renderer;
82 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview_words));
84 renderer = gtk_cell_renderer_text_new ();
85 col_offset = gtk_tree_view_insert_column_with_attributes (
86 GTK_TREE_VIEW (dialog->treeview_words),
89 "text", COL_SPELL_WORD,
92 g_object_set_data (G_OBJECT (renderer),
93 "column", GINT_TO_POINTER (COL_SPELL_WORD));
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);
102 spell_dialog_model_populate_suggestions (EmpathySpellDialog *dialog)
106 GList *suggestions, *l;
108 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview_words));
109 store = GTK_LIST_STORE (model);
111 suggestions = empathy_spell_get_suggestions (dialog->word);
112 for (l = suggestions; l; l=l->next) {
118 gtk_list_store_append (store, &iter);
119 gtk_list_store_set (store, &iter,
120 COL_SPELL_WORD, word,
124 empathy_spell_free_suggestions (suggestions);
128 spell_dialog_model_row_activated_cb (GtkTreeView *tree_view,
130 GtkTreeViewColumn *column,
131 EmpathySpellDialog *dialog)
133 spell_dialog_response_cb (dialog->window, GTK_RESPONSE_OK, dialog);
137 spell_dialog_model_selection_changed_cb (GtkTreeSelection *treeselection,
138 EmpathySpellDialog *dialog)
142 count = gtk_tree_selection_count_selected_rows (treeselection);
143 gtk_widget_set_sensitive (dialog->button_replace, (count == 1));
147 spell_dialog_model_setup (EmpathySpellDialog *dialog)
151 GtkTreeSelection *selection;
153 view = GTK_TREE_VIEW (dialog->treeview_words);
155 g_signal_connect (view, "row-activated",
156 G_CALLBACK (spell_dialog_model_row_activated_cb),
159 store = gtk_list_store_new (COL_SPELL_COUNT,
160 G_TYPE_STRING); /* word */
162 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
164 selection = gtk_tree_view_get_selection (view);
165 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
167 g_signal_connect (selection, "changed",
168 G_CALLBACK (spell_dialog_model_selection_changed_cb),
171 spell_dialog_model_populate_columns (dialog);
172 spell_dialog_model_populate_suggestions (dialog);
174 g_object_unref (store);
178 spell_dialog_destroy_cb (GtkWidget *widget,
179 EmpathySpellDialog *dialog)
181 g_object_unref (dialog->chat);
182 g_free (dialog->word);
188 spell_dialog_response_cb (GtkWidget *widget,
190 EmpathySpellDialog *dialog)
192 if (response == GTK_RESPONSE_OK) {
195 GtkTreeSelection *selection;
200 view = GTK_TREE_VIEW (dialog->treeview_words);
201 selection = gtk_tree_view_get_selection (view);
203 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
207 gtk_tree_model_get (model, &iter, COL_SPELL_WORD, &new_word, -1);
209 empathy_chat_correct_word (dialog->chat,
217 gtk_widget_destroy (dialog->window);
221 empathy_spell_dialog_show (EmpathyChat *chat,
226 EmpathySpellDialog *dialog;
231 g_return_if_fail (chat != NULL);
232 g_return_if_fail (word != NULL);
234 dialog = g_new0 (EmpathySpellDialog, 1);
236 dialog->chat = g_object_ref (chat);
238 dialog->word = g_strdup (word);
240 dialog->start = start;
243 filename = empathy_file_lookup ("empathy-spell-dialog.glade",
245 gui = empathy_glade_get_file (filename,
248 "spell_dialog", &dialog->window,
249 "button_replace", &dialog->button_replace,
250 "label_word", &dialog->label_word,
251 "treeview_words", &dialog->treeview_words,
255 empathy_glade_connect (gui,
257 "spell_dialog", "response", spell_dialog_response_cb,
258 "spell_dialog", "destroy", spell_dialog_destroy_cb,
261 g_object_unref (gui);
263 str = g_strdup_printf ("%s:\n<b>%s</b>",
264 _("Suggestions for the word"),
267 gtk_label_set_markup (GTK_LABEL (dialog->label_word), str);
270 spell_dialog_model_setup (dialog);
272 gtk_widget_show (dialog->window);