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.h"
39 #include "empathy-spell-dialog.h"
40 #include "empathy-ui-utils.h"
44 GtkWidget *button_replace;
45 GtkWidget *label_word;
46 GtkWidget *treeview_words;
60 static void spell_dialog_model_populate_columns (EmpathySpellDialog *dialog);
61 static void spell_dialog_model_populate_suggestions (EmpathySpellDialog *dialog);
62 static void spell_dialog_model_row_activated_cb (GtkTreeView *tree_view,
64 GtkTreeViewColumn *column,
65 EmpathySpellDialog *dialog);
66 static void spell_dialog_model_selection_changed_cb (GtkTreeSelection *treeselection,
67 EmpathySpellDialog *dialog);
68 static void spell_dialog_model_setup (EmpathySpellDialog *dialog);
69 static void spell_dialog_response_cb (GtkWidget *widget,
71 EmpathySpellDialog *dialog);
72 static void spell_dialog_destroy_cb (GtkWidget *widget,
73 EmpathySpellDialog *dialog);
76 spell_dialog_model_populate_columns (EmpathySpellDialog *dialog)
79 GtkTreeViewColumn *column;
80 GtkCellRenderer *renderer;
83 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview_words));
85 renderer = gtk_cell_renderer_text_new ();
86 col_offset = gtk_tree_view_insert_column_with_attributes (
87 GTK_TREE_VIEW (dialog->treeview_words),
90 "text", COL_SPELL_WORD,
93 g_object_set_data (G_OBJECT (renderer),
94 "column", GINT_TO_POINTER (COL_SPELL_WORD));
96 column = gtk_tree_view_get_column (GTK_TREE_VIEW (dialog->treeview_words), col_offset - 1);
97 gtk_tree_view_column_set_sort_column_id (column, COL_SPELL_WORD);
98 gtk_tree_view_column_set_resizable (column, FALSE);
99 gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
103 spell_dialog_model_populate_suggestions (EmpathySpellDialog *dialog)
107 GList *suggestions, *l;
109 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview_words));
110 store = GTK_LIST_STORE (model);
112 suggestions = empathy_spell_get_suggestions (dialog->word);
113 for (l = suggestions; l; l=l->next) {
119 gtk_list_store_append (store, &iter);
120 gtk_list_store_set (store, &iter,
121 COL_SPELL_WORD, word,
125 empathy_spell_free_suggestions (suggestions);
129 spell_dialog_model_row_activated_cb (GtkTreeView *tree_view,
131 GtkTreeViewColumn *column,
132 EmpathySpellDialog *dialog)
134 spell_dialog_response_cb (dialog->window, GTK_RESPONSE_OK, dialog);
138 spell_dialog_model_selection_changed_cb (GtkTreeSelection *treeselection,
139 EmpathySpellDialog *dialog)
143 count = gtk_tree_selection_count_selected_rows (treeselection);
144 gtk_widget_set_sensitive (dialog->button_replace, (count == 1));
148 spell_dialog_model_setup (EmpathySpellDialog *dialog)
152 GtkTreeSelection *selection;
154 view = GTK_TREE_VIEW (dialog->treeview_words);
156 g_signal_connect (view, "row-activated",
157 G_CALLBACK (spell_dialog_model_row_activated_cb),
160 store = gtk_list_store_new (COL_SPELL_COUNT,
161 G_TYPE_STRING); /* word */
163 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
165 selection = gtk_tree_view_get_selection (view);
166 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
168 g_signal_connect (selection, "changed",
169 G_CALLBACK (spell_dialog_model_selection_changed_cb),
172 spell_dialog_model_populate_columns (dialog);
173 spell_dialog_model_populate_suggestions (dialog);
175 g_object_unref (store);
179 spell_dialog_destroy_cb (GtkWidget *widget,
180 EmpathySpellDialog *dialog)
182 g_object_unref (dialog->chat);
183 g_free (dialog->word);
189 spell_dialog_response_cb (GtkWidget *widget,
191 EmpathySpellDialog *dialog)
193 if (response == GTK_RESPONSE_OK) {
196 GtkTreeSelection *selection;
201 view = GTK_TREE_VIEW (dialog->treeview_words);
202 selection = gtk_tree_view_get_selection (view);
204 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
208 gtk_tree_model_get (model, &iter, COL_SPELL_WORD, &new_word, -1);
210 empathy_chat_correct_word (dialog->chat,
218 gtk_widget_destroy (dialog->window);
222 empathy_spell_dialog_show (EmpathyChat *chat,
227 EmpathySpellDialog *dialog;
232 g_return_if_fail (chat != NULL);
233 g_return_if_fail (word != NULL);
235 dialog = g_new0 (EmpathySpellDialog, 1);
237 dialog->chat = g_object_ref (chat);
239 dialog->word = g_strdup (word);
241 dialog->start = start;
244 filename = empathy_file_lookup ("empathy-spell-dialog.glade",
246 gui = empathy_glade_get_file (filename,
249 "spell_dialog", &dialog->window,
250 "button_replace", &dialog->button_replace,
251 "label_word", &dialog->label_word,
252 "treeview_words", &dialog->treeview_words,
256 empathy_glade_connect (gui,
258 "spell_dialog", "response", spell_dialog_response_cb,
259 "spell_dialog", "destroy", spell_dialog_destroy_cb,
262 g_object_unref (gui);
264 str = g_markup_printf_escaped ("%s:\n<b>%s</b>",
265 _("Suggestions for the word"),
268 gtk_label_set_markup (GTK_LABEL (dialog->label_word), str);
271 spell_dialog_model_setup (dialog);
273 gtk_widget_show (dialog->window);