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 "empathy-chat.h"
36 #include "empathy-spell-dialog.h"
37 #include "empathy-ui-utils.h"
41 GtkWidget *button_replace;
42 GtkWidget *label_word;
43 GtkWidget *treeview_words;
57 static void spell_dialog_model_populate_columns (EmpathySpellDialog *dialog);
58 static void spell_dialog_model_populate_suggestions (EmpathySpellDialog *dialog);
59 static void spell_dialog_model_row_activated_cb (GtkTreeView *tree_view,
61 GtkTreeViewColumn *column,
62 EmpathySpellDialog *dialog);
63 static void spell_dialog_model_selection_changed_cb (GtkTreeSelection *treeselection,
64 EmpathySpellDialog *dialog);
65 static void spell_dialog_model_setup (EmpathySpellDialog *dialog);
66 static void spell_dialog_response_cb (GtkWidget *widget,
68 EmpathySpellDialog *dialog);
69 static void spell_dialog_destroy_cb (GtkWidget *widget,
70 EmpathySpellDialog *dialog);
73 spell_dialog_model_populate_columns (EmpathySpellDialog *dialog)
76 GtkTreeViewColumn *column;
77 GtkCellRenderer *renderer;
80 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview_words));
82 renderer = gtk_cell_renderer_text_new ();
83 col_offset = gtk_tree_view_insert_column_with_attributes (
84 GTK_TREE_VIEW (dialog->treeview_words),
87 "text", COL_SPELL_WORD,
90 g_object_set_data (G_OBJECT (renderer),
91 "column", GINT_TO_POINTER (COL_SPELL_WORD));
93 column = gtk_tree_view_get_column (GTK_TREE_VIEW (dialog->treeview_words), col_offset - 1);
94 gtk_tree_view_column_set_sort_column_id (column, COL_SPELL_WORD);
95 gtk_tree_view_column_set_resizable (column, FALSE);
96 gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
100 spell_dialog_model_populate_suggestions (EmpathySpellDialog *dialog)
104 GList *suggestions, *l;
106 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview_words));
107 store = GTK_LIST_STORE (model);
109 suggestions = empathy_spell_get_suggestions (dialog->word);
110 for (l = suggestions; l; l=l->next) {
116 gtk_list_store_append (store, &iter);
117 gtk_list_store_set (store, &iter,
118 COL_SPELL_WORD, word,
122 empathy_spell_free_suggestions (suggestions);
126 spell_dialog_model_row_activated_cb (GtkTreeView *tree_view,
128 GtkTreeViewColumn *column,
129 EmpathySpellDialog *dialog)
131 spell_dialog_response_cb (dialog->window, GTK_RESPONSE_OK, dialog);
135 spell_dialog_model_selection_changed_cb (GtkTreeSelection *treeselection,
136 EmpathySpellDialog *dialog)
140 count = gtk_tree_selection_count_selected_rows (treeselection);
141 gtk_widget_set_sensitive (dialog->button_replace, (count == 1));
145 spell_dialog_model_setup (EmpathySpellDialog *dialog)
149 GtkTreeSelection *selection;
151 view = GTK_TREE_VIEW (dialog->treeview_words);
153 g_signal_connect (view, "row-activated",
154 G_CALLBACK (spell_dialog_model_row_activated_cb),
157 store = gtk_list_store_new (COL_SPELL_COUNT,
158 G_TYPE_STRING); /* word */
160 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
162 selection = gtk_tree_view_get_selection (view);
163 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
165 g_signal_connect (selection, "changed",
166 G_CALLBACK (spell_dialog_model_selection_changed_cb),
169 spell_dialog_model_populate_columns (dialog);
170 spell_dialog_model_populate_suggestions (dialog);
172 g_object_unref (store);
176 spell_dialog_destroy_cb (GtkWidget *widget,
177 EmpathySpellDialog *dialog)
179 g_object_unref (dialog->chat);
180 g_free (dialog->word);
186 spell_dialog_response_cb (GtkWidget *widget,
188 EmpathySpellDialog *dialog)
190 if (response == GTK_RESPONSE_OK) {
193 GtkTreeSelection *selection;
198 view = GTK_TREE_VIEW (dialog->treeview_words);
199 selection = gtk_tree_view_get_selection (view);
201 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
205 gtk_tree_model_get (model, &iter, COL_SPELL_WORD, &new_word, -1);
207 empathy_chat_correct_word (dialog->chat,
215 gtk_widget_destroy (dialog->window);
219 empathy_spell_dialog_show (EmpathyChat *chat,
224 EmpathySpellDialog *dialog;
228 g_return_if_fail (chat != NULL);
229 g_return_if_fail (word != NULL);
231 dialog = g_new0 (EmpathySpellDialog, 1);
233 dialog->chat = g_object_ref (chat);
235 dialog->word = g_strdup (word);
237 dialog->start = start;
240 gui = empathy_glade_get_file ("empathy-spell-dialog.glade",
243 "spell_dialog", &dialog->window,
244 "button_replace", &dialog->button_replace,
245 "label_word", &dialog->label_word,
246 "treeview_words", &dialog->treeview_words,
249 empathy_glade_connect (gui,
251 "spell_dialog", "response", spell_dialog_response_cb,
252 "spell_dialog", "destroy", spell_dialog_destroy_cb,
255 g_object_unref (gui);
257 str = g_strdup_printf ("%s:\n<b>%s</b>",
258 _("Suggestions for the word"),
261 gtk_label_set_markup (GTK_LABEL (dialog->label_word), str);
264 spell_dialog_model_setup (dialog);
266 gtk_widget_show (dialog->window);