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-lib.h>
28 #include <libempathy/empathy-utils.h>
30 #include "empathy-chat.h"
31 #include "empathy-spell.h"
32 #include "empathy-spell-dialog.h"
33 #include "empathy-ui-utils.h"
37 GtkWidget *button_replace;
38 GtkWidget *label_word;
39 GtkWidget *treeview_words;
53 static void spell_dialog_model_populate_columns (EmpathySpellDialog *dialog);
54 static void spell_dialog_model_populate_suggestions (EmpathySpellDialog *dialog);
55 static void spell_dialog_model_row_activated_cb (GtkTreeView *tree_view,
57 GtkTreeViewColumn *column,
58 EmpathySpellDialog *dialog);
59 static void spell_dialog_model_selection_changed_cb (GtkTreeSelection *treeselection,
60 EmpathySpellDialog *dialog);
61 static void spell_dialog_model_setup (EmpathySpellDialog *dialog);
62 static void spell_dialog_response_cb (GtkWidget *widget,
64 EmpathySpellDialog *dialog);
65 static void spell_dialog_destroy_cb (GtkWidget *widget,
66 EmpathySpellDialog *dialog);
69 spell_dialog_model_populate_columns (EmpathySpellDialog *dialog)
72 GtkTreeViewColumn *column;
73 GtkCellRenderer *renderer;
76 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview_words));
78 renderer = gtk_cell_renderer_text_new ();
79 col_offset = gtk_tree_view_insert_column_with_attributes (
80 GTK_TREE_VIEW (dialog->treeview_words),
83 "text", COL_SPELL_WORD,
86 g_object_set_data (G_OBJECT (renderer),
87 "column", GINT_TO_POINTER (COL_SPELL_WORD));
89 column = gtk_tree_view_get_column (GTK_TREE_VIEW (dialog->treeview_words), col_offset - 1);
90 gtk_tree_view_column_set_sort_column_id (column, COL_SPELL_WORD);
91 gtk_tree_view_column_set_resizable (column, FALSE);
92 gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
96 spell_dialog_model_populate_suggestions (EmpathySpellDialog *dialog)
100 GList *suggestions, *l;
102 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview_words));
103 store = GTK_LIST_STORE (model);
105 suggestions = empathy_spell_get_suggestions (dialog->word);
106 for (l = suggestions; l; l=l->next) {
112 gtk_list_store_append (store, &iter);
113 gtk_list_store_set (store, &iter,
114 COL_SPELL_WORD, word,
118 empathy_spell_free_suggestions (suggestions);
122 spell_dialog_model_row_activated_cb (GtkTreeView *tree_view,
124 GtkTreeViewColumn *column,
125 EmpathySpellDialog *dialog)
127 spell_dialog_response_cb (dialog->window, GTK_RESPONSE_OK, dialog);
131 spell_dialog_model_selection_changed_cb (GtkTreeSelection *treeselection,
132 EmpathySpellDialog *dialog)
136 count = gtk_tree_selection_count_selected_rows (treeselection);
137 gtk_widget_set_sensitive (dialog->button_replace, (count == 1));
141 spell_dialog_model_setup (EmpathySpellDialog *dialog)
145 GtkTreeSelection *selection;
147 view = GTK_TREE_VIEW (dialog->treeview_words);
149 g_signal_connect (view, "row-activated",
150 G_CALLBACK (spell_dialog_model_row_activated_cb),
153 store = gtk_list_store_new (COL_SPELL_COUNT,
154 G_TYPE_STRING); /* word */
156 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
158 selection = gtk_tree_view_get_selection (view);
159 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
161 g_signal_connect (selection, "changed",
162 G_CALLBACK (spell_dialog_model_selection_changed_cb),
165 spell_dialog_model_populate_columns (dialog);
166 spell_dialog_model_populate_suggestions (dialog);
168 g_object_unref (store);
172 spell_dialog_destroy_cb (GtkWidget *widget,
173 EmpathySpellDialog *dialog)
175 g_object_unref (dialog->chat);
176 g_free (dialog->word);
182 spell_dialog_response_cb (GtkWidget *widget,
184 EmpathySpellDialog *dialog)
186 if (response == GTK_RESPONSE_OK) {
189 GtkTreeSelection *selection;
194 view = GTK_TREE_VIEW (dialog->treeview_words);
195 selection = gtk_tree_view_get_selection (view);
197 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
201 gtk_tree_model_get (model, &iter, COL_SPELL_WORD, &new_word, -1);
203 empathy_chat_correct_word (dialog->chat,
211 gtk_widget_destroy (dialog->window);
215 empathy_spell_dialog_show (EmpathyChat *chat,
220 EmpathySpellDialog *dialog;
225 g_return_if_fail (chat != NULL);
226 g_return_if_fail (word != NULL);
228 dialog = g_new0 (EmpathySpellDialog, 1);
230 dialog->chat = g_object_ref (chat);
232 dialog->word = g_strdup (word);
234 dialog->start = *start;
237 filename = empathy_file_lookup ("empathy-spell-dialog.ui",
239 gui = empathy_builder_get_file (filename,
240 "spell_dialog", &dialog->window,
241 "button_replace", &dialog->button_replace,
242 "label_word", &dialog->label_word,
243 "treeview_words", &dialog->treeview_words,
247 empathy_builder_connect (gui, dialog,
248 "spell_dialog", "response", spell_dialog_response_cb,
249 "spell_dialog", "destroy", spell_dialog_destroy_cb,
252 g_object_unref (gui);
254 str = g_markup_printf_escaped ("%s:\n<b>%s</b>",
255 _("Suggestions for the word"),
258 gtk_label_set_markup (GTK_LABEL (dialog->label_word), str);
261 spell_dialog_model_setup (dialog);
263 gtk_widget_show (dialog->window);