xed/plugins/spell/xed-spell-language-dialog.c

285 lines
8.4 KiB
C
Raw Permalink Normal View History

2011-11-07 13:46:58 -06:00
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* xed-spell-language-dialog.c
* This file is part of xed
2011-11-07 13:46:58 -06:00
*
* Copyright (C) 2002 Paolo Maggi
2011-11-07 13:46:58 -06:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
2011-11-07 13:46:58 -06:00
*/
2011-11-07 13:46:58 -06:00
/*
* Modified by the xed Team, 2002. See the AUTHORS file for a
* list of people on the xed Team.
* See the ChangeLog files for a list of changes.
2011-11-07 13:46:58 -06:00
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <xed/xed-utils.h>
#include <xed/xed-app.h>
#include "xed-spell-language-dialog.h"
#include "xed-spell-checker-language.h"
2011-11-07 13:46:58 -06:00
enum
{
COLUMN_LANGUAGE_NAME = 0,
COLUMN_LANGUAGE_POINTER,
ENCODING_NUM_COLS
2011-11-07 13:46:58 -06:00
};
struct _XedSpellLanguageDialog
2011-11-07 13:46:58 -06:00
{
GtkDialog dialog;
2011-11-07 13:46:58 -06:00
GtkWidget *languages_treeview;
GtkTreeModel *model;
2011-11-07 13:46:58 -06:00
};
G_DEFINE_TYPE (XedSpellLanguageDialog, xed_spell_language_dialog, GTK_TYPE_DIALOG)
2011-11-07 13:46:58 -06:00
static void
xed_spell_language_dialog_class_init (XedSpellLanguageDialogClass *klass)
2011-11-07 13:46:58 -06:00
{
/* GObjectClass *object_class = G_OBJECT_CLASS (klass); */
2011-11-07 13:46:58 -06:00
}
static void
dialog_response_handler (GtkDialog *dlg,
gint res_id)
2011-11-07 13:46:58 -06:00
{
if (res_id == GTK_RESPONSE_HELP)
{
xed_app_show_help (XED_APP (g_application_get_default ()), GTK_WINDOW (dlg), NULL, "xed-spell-checker-plugin");
g_signal_stop_emission_by_name (dlg, "response");
}
2011-11-07 13:46:58 -06:00
}
static void
2011-11-07 13:46:58 -06:00
scroll_to_selected (GtkTreeView *tree_view)
{
GtkTreeModel *model;
GtkTreeSelection *selection;
GtkTreeIter iter;
2011-11-07 13:46:58 -06:00
model = gtk_tree_view_get_model (tree_view);
g_return_if_fail (model != NULL);
2011-11-07 13:46:58 -06:00
/* Scroll to selected */
selection = gtk_tree_view_get_selection (tree_view);
g_return_if_fail (selection != NULL);
2011-11-07 13:46:58 -06:00
if (gtk_tree_selection_get_selected (selection, NULL, &iter))
{
GtkTreePath* path;
2011-11-07 13:46:58 -06:00
path = gtk_tree_model_get_path (model, &iter);
g_return_if_fail (path != NULL);
2011-11-07 13:46:58 -06:00
gtk_tree_view_scroll_to_cell (tree_view, path, NULL, TRUE, 1.0, 0.0);
gtk_tree_path_free (path);
}
2011-11-07 13:46:58 -06:00
}
static void
language_row_activated (GtkTreeView *tree_view,
GtkTreePath *path,
GtkTreeViewColumn *column,
XedSpellLanguageDialog *dialog)
2011-11-07 13:46:58 -06:00
{
gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
2011-11-07 13:46:58 -06:00
}
static void
create_dialog (XedSpellLanguageDialog *dlg,
const gchar *data_dir)
2011-11-07 13:46:58 -06:00
{
GtkWidget *error_widget;
GtkWidget *content;
gboolean ret;
GtkCellRenderer *cell;
GtkTreeViewColumn *column;
gchar *ui_file;
gchar *root_objects[] = {
"content",
NULL
};
gtk_dialog_add_buttons (GTK_DIALOG (dlg),
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("_OK"), GTK_RESPONSE_OK,
_("_Help"), GTK_RESPONSE_HELP,
NULL);
gtk_window_set_title (GTK_WINDOW (dlg), _("Set language"));
gtk_window_set_modal (GTK_WINDOW (dlg), TRUE);
gtk_window_set_destroy_with_parent (GTK_WINDOW (dlg), TRUE);
/* HIG defaults */
gtk_container_set_border_width (GTK_CONTAINER (dlg), 5);
gtk_box_set_spacing (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dlg))), 2);
gtk_container_set_border_width (GTK_CONTAINER (gtk_dialog_get_action_area (GTK_DIALOG (dlg))), 5);
gtk_box_set_spacing (GTK_BOX (gtk_dialog_get_action_area (GTK_DIALOG (dlg))), 6);
g_signal_connect (dlg, "response",
G_CALLBACK (dialog_response_handler), NULL);
ui_file = g_build_filename (data_dir, "languages-dialog.ui", NULL);
ret = xed_utils_get_ui_objects (ui_file,
root_objects,
&error_widget,
"content", &content,
"languages_treeview", &dlg->languages_treeview,
NULL);
g_free (ui_file);
if (!ret)
{
gtk_widget_show (error_widget);
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dlg))), error_widget, TRUE, TRUE, 0);
return;
}
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dlg))), content, TRUE, TRUE, 0);
g_object_unref (content);
gtk_container_set_border_width (GTK_CONTAINER (content), 5);
dlg->model = GTK_TREE_MODEL (gtk_list_store_new (ENCODING_NUM_COLS, G_TYPE_STRING, G_TYPE_POINTER));
gtk_tree_view_set_model (GTK_TREE_VIEW (dlg->languages_treeview), dlg->model);
g_object_unref (dlg->model);
/* Add the encoding column */
cell = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes (_("Languages"),
cell,
"text",
COLUMN_LANGUAGE_NAME,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (dlg->languages_treeview), column);
gtk_tree_view_set_search_column (GTK_TREE_VIEW (dlg->languages_treeview), COLUMN_LANGUAGE_NAME);
g_signal_connect (dlg->languages_treeview, "realize",
G_CALLBACK (scroll_to_selected), dlg);
g_signal_connect (dlg->languages_treeview, "row-activated",
G_CALLBACK (language_row_activated), dlg);
2011-11-07 13:46:58 -06:00
}
static void
xed_spell_language_dialog_init (XedSpellLanguageDialog *dlg)
2011-11-07 13:46:58 -06:00
{
2011-11-07 13:46:58 -06:00
}
static void
populate_language_list (XedSpellLanguageDialog *dlg,
const XedSpellCheckerLanguage *cur_lang)
2011-11-07 13:46:58 -06:00
{
GtkListStore *store;
GtkTreeIter iter;
2011-11-07 13:46:58 -06:00
const GSList* langs;
2011-11-07 13:46:58 -06:00
/* create list store */
store = GTK_LIST_STORE (dlg->model);
2011-11-07 13:46:58 -06:00
langs = xed_spell_checker_get_available_languages ();
2011-11-07 13:46:58 -06:00
while (langs)
{
const gchar *name;
2011-11-07 13:46:58 -06:00
name = xed_spell_checker_language_to_string ((const XedSpellCheckerLanguage*)langs->data);
2011-11-07 13:46:58 -06:00
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
COLUMN_LANGUAGE_NAME, name,
COLUMN_LANGUAGE_POINTER, langs->data,
-1);
2011-11-07 13:46:58 -06:00
if (langs->data == cur_lang)
{
GtkTreeSelection *selection;
2011-11-07 13:46:58 -06:00
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dlg->languages_treeview));
g_return_if_fail (selection != NULL);
2011-11-07 13:46:58 -06:00
gtk_tree_selection_select_iter (selection, &iter);
}
2011-11-07 13:46:58 -06:00
langs = g_slist_next (langs);
}
2011-11-07 13:46:58 -06:00
}
GtkWidget *
xed_spell_language_dialog_new (GtkWindow *parent,
const XedSpellCheckerLanguage *cur_lang,
const gchar *data_dir)
2011-11-07 13:46:58 -06:00
{
XedSpellLanguageDialog *dlg;
2011-11-07 13:46:58 -06:00
g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
2011-11-07 13:46:58 -06:00
dlg = g_object_new (XED_TYPE_SPELL_LANGUAGE_DIALOG, NULL);
2011-11-07 13:46:58 -06:00
create_dialog (dlg, data_dir);
2011-11-07 13:46:58 -06:00
populate_language_list (dlg, cur_lang);
2011-11-07 13:46:58 -06:00
gtk_window_set_transient_for (GTK_WINDOW (dlg), parent);
gtk_widget_grab_focus (dlg->languages_treeview);
2011-11-07 13:46:58 -06:00
return GTK_WIDGET (dlg);
2011-11-07 13:46:58 -06:00
}
const XedSpellCheckerLanguage *
xed_spell_language_get_selected_language (XedSpellLanguageDialog *dlg)
2011-11-07 13:46:58 -06:00
{
GValue value = {0, };
const XedSpellCheckerLanguage* lang;
2011-11-07 13:46:58 -06:00
GtkTreeIter iter;
GtkTreeSelection *selection;
2011-11-07 13:46:58 -06:00
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dlg->languages_treeview));
g_return_val_if_fail (selection != NULL, NULL);
2011-11-07 13:46:58 -06:00
if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
{
return NULL;
}
2011-11-07 13:46:58 -06:00
gtk_tree_model_get_value (dlg->model, &iter, COLUMN_LANGUAGE_POINTER, &value);
2011-11-07 13:46:58 -06:00
lang = (const XedSpellCheckerLanguage* ) g_value_get_pointer (&value);
2011-11-07 13:46:58 -06:00
return lang;
2011-11-07 13:46:58 -06:00
}