Commit a25db684 authored by Steve Frécinaux's avatar Steve Frécinaux Committed by Paolo Borelli

Factor out a widget for the highlight selection

We want a separate widget so that we can use both in a dialog or inside
a popover.
parent ce38452c
......@@ -118,6 +118,7 @@ gedit_NOINST_H_FILES = \
gedit/gedit-encodings-dialog.h \
gedit/gedit-file-chooser-dialog.h \
gedit/gedit-highlight-mode-dialog.h \
gedit/gedit-highlight-mode-selector.h \
gedit/gedit-history-entry.h \
gedit/gedit-io-error-info-bar.h \
gedit/gedit-menu-stack-switcher.h \
......@@ -194,6 +195,7 @@ gedit_libgedit_c_files = \
gedit/gedit-encodings-dialog.c \
gedit/gedit-file-chooser-dialog.c \
gedit/gedit-highlight-mode-dialog.c \
gedit/gedit-highlight-mode-selector.c \
gedit/gedit-history-entry.c \
gedit/gedit-io-error-info-bar.c \
gedit/gedit-menu-stack-switcher.c \
......
......@@ -31,6 +31,7 @@
#include "gedit-window.h"
#include "gedit-window-private.h"
#include "gedit-highlight-mode-dialog.h"
#include "gedit-highlight-mode-selector.h"
void
_gedit_cmd_view_toggle_side_panel (GSimpleAction *action,
......@@ -119,18 +120,17 @@ _gedit_cmd_view_leave_fullscreen_mode (GSimpleAction *action,
}
static void
on_language_selected (GeditHighlightModeDialog *dlg,
GtkSourceLanguage *language,
GeditWindow *window)
on_language_selected (GeditHighlightModeSelector *sel,
GtkSourceLanguage *language,
GeditWindow *window)
{
GeditDocument *doc;
doc = gedit_window_get_active_document (window);
if (!doc)
return;
gedit_document_set_language (doc, language);
if (doc)
{
gedit_document_set_language (doc, language);
}
}
void
......@@ -140,18 +140,20 @@ _gedit_cmd_view_highlight_mode (GSimpleAction *action,
{
GtkWindow *window = GTK_WINDOW (user_data);
GtkWidget *dlg;
GeditHighlightModeSelector *sel;
GeditDocument *doc;
dlg = gedit_highlight_mode_dialog_new (window);
sel = gedit_highlight_mode_dialog_get_selector (GEDIT_HIGHLIGHT_MODE_DIALOG (dlg));
doc = gedit_window_get_active_document (GEDIT_WINDOW (window));
if (doc)
{
gedit_highlight_mode_dialog_select_language (GEDIT_HIGHLIGHT_MODE_DIALOG (dlg),
gedit_document_get_language (doc));
gedit_highlight_mode_selector_select_language (sel,
gedit_document_get_language (doc));
}
g_signal_connect (dlg, "language-selected",
g_signal_connect (sel, "language-selected",
G_CALLBACK (on_language_selected), window);
gtk_widget_show (GTK_WIDGET (dlg));
......
......@@ -18,38 +18,14 @@
* along with gedit. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtk/gtk.h>
#include "gedit-highlight-mode-dialog.h"
#include <gdk/gdkkeysyms.h>
#include <glib/gi18n.h>
#include <string.h>
enum
{
COLUMN_NAME,
COLUMN_LANG,
N_COLUMNS
};
struct _GeditHighlightModeDialogPrivate
{
GtkWidget *treeview;
GtkWidget *entry;
GtkListStore *liststore;
GtkTreeModelFilter *treemodelfilter;
GtkTreeSelection *treeview_selection;
GeditHighlightModeSelector *selector;
};
/* Signals */
enum
{
LANGUAGE_SELECTED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE_WITH_PRIVATE (GeditHighlightModeDialog, gedit_highlight_mode_dialog, GTK_TYPE_DIALOG)
static void
......@@ -60,22 +36,7 @@ gedit_highlight_mode_dialog_response (GtkDialog *dialog,
if (response_id == GTK_RESPONSE_OK)
{
GtkSourceLanguage *lang;
GtkTreeIter iter;
if (gtk_tree_selection_get_selected (priv->treeview_selection, NULL, &iter))
{
gtk_tree_model_get (GTK_TREE_MODEL (priv->treemodelfilter), &iter,
COLUMN_LANG, &lang,
-1);
}
g_signal_emit (G_OBJECT (dialog), signals[LANGUAGE_SELECTED], 0, lang);
if (lang)
{
g_object_unref (lang);
}
gedit_highlight_mode_selector_activate_selected_language (priv->selector);
}
gtk_widget_destroy (GTK_WIDGET (dialog));
......@@ -84,242 +45,24 @@ gedit_highlight_mode_dialog_response (GtkDialog *dialog,
static void
gedit_highlight_mode_dialog_class_init (GeditHighlightModeDialogClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
dialog_class->response = gedit_highlight_mode_dialog_response;
signals[LANGUAGE_SELECTED] =
g_signal_new ("language-selected",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GeditHighlightModeDialogClass, language_selected),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE,
1,
GTK_SOURCE_TYPE_LANGUAGE);
/* Bind class to template */
gtk_widget_class_set_template_from_resource (widget_class,
"/org/gnome/gedit/ui/gedit-highlight-mode-dialog.ui");
gtk_widget_class_bind_template_child_private (widget_class, GeditHighlightModeDialog, treeview);
gtk_widget_class_bind_template_child_private (widget_class, GeditHighlightModeDialog, entry);
gtk_widget_class_bind_template_child_private (widget_class, GeditHighlightModeDialog, liststore);
gtk_widget_class_bind_template_child_private (widget_class, GeditHighlightModeDialog, treemodelfilter);
gtk_widget_class_bind_template_child_private (widget_class, GeditHighlightModeDialog, treeview_selection);
}
static gboolean
visible_func (GtkTreeModel *model,
GtkTreeIter *iter,
GeditHighlightModeDialog *dlg)
{
const gchar *entry_text;
gchar *name;
gchar *name_lower;
gchar *text_lower;
gboolean visible = FALSE;
entry_text = gtk_entry_get_text (GTK_ENTRY (dlg->priv->entry));
if (*entry_text == '\0')
{
return TRUE;
}
gtk_tree_model_get (model, iter, COLUMN_NAME, &name, -1);
name_lower = g_utf8_strdown (name, -1);
g_free (name);
text_lower = g_utf8_strdown (entry_text, -1);
if (strstr (name_lower, text_lower) != NULL)
{
visible = TRUE;
}
g_free (name_lower);
g_free (text_lower);
return visible;
}
static void
on_entry_changed (GtkEntry *entry,
GeditHighlightModeDialog *dlg)
{
GtkTreeIter iter;
gtk_tree_model_filter_refilter (dlg->priv->treemodelfilter);
if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (dlg->priv->treemodelfilter), &iter))
{
gtk_tree_selection_select_iter (dlg->priv->treeview_selection, &iter);
}
}
static gboolean
move_selection (GeditHighlightModeDialog *dlg,
gint howmany)
{
GtkTreeIter iter;
GtkTreePath *path;
gint *indices;
gint ret = FALSE;
if (!gtk_tree_selection_get_selected (dlg->priv->treeview_selection, NULL, &iter) &&
!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (dlg->priv->treemodelfilter), &iter))
{
return FALSE;
}
path = gtk_tree_model_get_path (GTK_TREE_MODEL (dlg->priv->treemodelfilter), &iter);
indices = gtk_tree_path_get_indices (path);
if (indices)
{
gint num;
gint idx;
GtkTreePath *new_path;
idx = indices[0];
num = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (dlg->priv->treemodelfilter), NULL);
if ((idx + howmany) < 0)
{
idx = 0;
}
else if ((idx + howmany) >= num)
{
idx = num - 1;
}
else
{
idx = idx + howmany;
}
new_path = gtk_tree_path_new_from_indices (idx, -1);
gtk_tree_selection_select_path (dlg->priv->treeview_selection, new_path);
gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (dlg->priv->treeview),
new_path, NULL, TRUE, 0.5, 0);
gtk_tree_path_free (new_path);
ret = TRUE;
}
gtk_tree_path_free (path);
return ret;
}
static gboolean
on_entry_key_press_event (GtkWidget *entry,
GdkEventKey *event,
GeditHighlightModeDialog *dlg)
{
if (event->keyval == GDK_KEY_Down)
{
return move_selection (dlg, 1);
}
else if (event->keyval == GDK_KEY_Up)
{
return move_selection (dlg, -1);
}
else if (event->keyval == GDK_KEY_Page_Down)
{
return move_selection (dlg, 5);
}
else if (event->keyval == GDK_KEY_Page_Up)
{
return move_selection (dlg, -5);
}
return FALSE;
}
static void
on_selection_changed (GtkTreeSelection *selection,
GeditHighlightModeDialog *dlg)
{
gtk_dialog_set_response_sensitive (GTK_DIALOG (dlg),
GTK_RESPONSE_OK,
gtk_tree_selection_get_selected (selection, NULL, NULL));
}
static void
on_row_activated (GtkTreeView *tree_view,
GtkTreePath *path,
GtkTreeViewColumn *column,
GeditHighlightModeDialog *dlg)
{
gtk_window_activate_default (GTK_WINDOW (dlg));
gtk_widget_class_bind_template_child_private (widget_class, GeditHighlightModeDialog, selector);
}
static void
gedit_highlight_mode_dialog_init (GeditHighlightModeDialog *dlg)
{
GeditHighlightModeDialogPrivate *priv;
GtkSourceLanguageManager *lm;
const gchar * const *ids;
gint i;
GtkTreeIter iter;
dlg->priv = gedit_highlight_mode_dialog_get_instance_private (dlg);
priv = dlg->priv;
gtk_widget_init_template (GTK_WIDGET (dlg));
gtk_dialog_set_default_response(GTK_DIALOG (dlg), GTK_RESPONSE_OK);
gtk_tree_model_filter_set_visible_func (priv->treemodelfilter,
(GtkTreeModelFilterVisibleFunc)visible_func,
dlg,
NULL);
g_signal_connect (priv->entry, "changed",
G_CALLBACK (on_entry_changed), dlg);
g_signal_connect (priv->entry, "key-press-event",
G_CALLBACK (on_entry_key_press_event), dlg);
g_signal_connect (priv->treeview_selection, "changed",
G_CALLBACK (on_selection_changed), dlg);
g_signal_connect (priv->treeview, "row-activated",
G_CALLBACK (on_row_activated), dlg);
/* Populate tree model */
gtk_list_store_append (priv->liststore, &iter);
gtk_list_store_set (priv->liststore, &iter,
COLUMN_NAME, _("Plain Text"),
COLUMN_LANG, NULL,
-1);
lm = gtk_source_language_manager_get_default ();
ids = gtk_source_language_manager_get_language_ids (lm);
for (i = 0; ids[i] != NULL; i++)
{
GtkSourceLanguage *lang;
lang = gtk_source_language_manager_get_language (lm, ids[i]);
if (!gtk_source_language_get_hidden (lang))
{
gtk_list_store_append (priv->liststore, &iter);
gtk_list_store_set (priv->liststore, &iter,
COLUMN_NAME, gtk_source_language_get_name (lang),
COLUMN_LANG, lang,
-1);
}
}
/* select first item */
if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (dlg->priv->treemodelfilter), &iter))
{
gtk_tree_selection_select_iter (dlg->priv->treeview_selection, &iter);
}
gtk_dialog_set_default_response (GTK_DIALOG (dlg), GTK_RESPONSE_OK);
}
GtkWidget *
......@@ -330,52 +73,12 @@ gedit_highlight_mode_dialog_new (GtkWindow *parent)
NULL));
}
void
gedit_highlight_mode_dialog_select_language (GeditHighlightModeDialog *dlg,
GtkSourceLanguage *language)
GeditHighlightModeSelector *
gedit_highlight_mode_dialog_get_selector (GeditHighlightModeDialog *dlg)
{
GtkTreeIter iter;
g_return_val_if_fail (GEDIT_IS_HIGHLIGHT_MODE_DIALOG (dlg), NULL);
g_return_if_fail (GEDIT_IS_HIGHLIGHT_MODE_DIALOG (dlg));
if (language == NULL)
{
return;
}
if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (dlg->priv->treemodelfilter), &iter))
{
do
{
GtkSourceLanguage *lang;
gtk_tree_model_get (GTK_TREE_MODEL (dlg->priv->treemodelfilter),
&iter,
COLUMN_LANG, &lang,
-1);
if (lang != NULL)
{
gboolean equal = (lang == language);
g_object_unref (lang);
if (equal)
{
GtkTreePath *path;
path = gtk_tree_model_get_path (GTK_TREE_MODEL (dlg->priv->treemodelfilter), &iter);
gtk_tree_selection_select_iter (dlg->priv->treeview_selection, &iter);
gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (dlg->priv->treeview),
path, NULL, TRUE, 0.5, 0);
gtk_tree_path_free (path);
break;
}
}
}
while (gtk_tree_model_iter_next (GTK_TREE_MODEL (dlg->priv->treemodelfilter), &iter));
}
return dlg->priv->selector;
}
/* ex:set ts=8 noet: */
......@@ -22,9 +22,8 @@
#ifndef __GEDIT_HIGHLIGHT_MODE_DIALOG_H__
#define __GEDIT_HIGHLIGHT_MODE_DIALOG_H__
#include <glib-object.h>
#include <gtksourceview/gtksource.h>
#include "gedit-window.h"
#include <glib.h>
#include "gedit-highlight-mode-selector.h"
G_BEGIN_DECLS
......@@ -50,17 +49,13 @@ struct _GeditHighlightModeDialog
struct _GeditHighlightModeDialogClass
{
GtkDialogClass parent_class;
void (* language_selected) (GeditHighlightModeDialog *dialog,
GtkSourceLanguage *language);
};
GType gedit_highlight_mode_dialog_get_type (void) G_GNUC_CONST;
GType gedit_highlight_mode_dialog_get_type (void) G_GNUC_CONST;
GtkWidget *gedit_highlight_mode_dialog_new (GtkWindow *parent);
GtkWidget *gedit_highlight_mode_dialog_new (GtkWindow *parent);
void gedit_highlight_mode_dialog_select_language (GeditHighlightModeDialog *dlg,
GtkSourceLanguage *language);
GeditHighlightModeSelector *gedit_highlight_mode_dialog_get_selector (GeditHighlightModeDialog *dlg);
G_END_DECLS
......
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.8 -->
<object class="GtkListStore" id="liststore">
<columns>
<!-- column-name name -->
<column type="gchararray"/>
<!-- column-name lang -->
<column type="GtkSourceLanguage"/>
</columns>
</object>
<object class="GtkTreeModelFilter" id="treemodelfilter">
<property name="child_model">liststore</property>
</object>
<template class="GeditHighlightModeDialog" parent="GtkDialog">
<property name="width_request">300</property>
<property name="height_request">400</property>
......@@ -79,71 +68,11 @@
</packing>
</child>
<child>
<object class="GtkGrid" id="grid">
<object class="GeditHighlightModeSelector" id="selector">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="has_focus">False</property>
<property name="is_focus">False</property>
<property name="row_spacing">3</property>
<property name="border_width">6</property>
<child>
<object class="GtkSearchEntry" id="entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="has_focus">False</property>
<property name="is_focus">False</property>
<property name="activates_default">True</property>
<property name="placeholder_text" translatable="yes">Search highlight mode...</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="has_focus">False</property>
<property name="is_focus">False</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkTreeView" id="treeview">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="has_focus">False</property>
<property name="is_focus">False</property>
<property name="model">treemodelfilter</property>
<property name="headers_visible">False</property>
<property name="headers_clickable">False</property>
<property name="enable_search">False</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="treeview_selection"/>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn">
<child>
<object class="GtkCellRendererText" id="cellrenderertext"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<property name="can_focus">True</property>
<property name="has_focus">True</property>
<property name="is_focus">True</property>
</object>
<packing>
<property name="expand">False</property>
......
/*
* gedit-highlight-mode-selector.c
* This file is part of gedit
*
* Copyright (C) 2013 - Ignacio Casal Quinteiro
*
* gedit 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.
*
* gedit 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 gedit. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gdk/gdkkeysyms.h>
#include <glib/gi18n.h>
#include <string.h>
#include "gedit-highlight-mode-selector.h"
enum
{
COLUMN_NAME,
COLUMN_LANG,
N_COLUMNS
};
struct _GeditHighlightModeSelectorPrivate
{
GtkWidget *treeview;
GtkWidget *entry;
GtkListStore *liststore;
GtkTreeModelFilter *treemodelfilter;
GtkTreeSelection *treeview_selection;
};
/* Signals */
enum
{
LANGUAGE_SELECTED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE_WITH_PRIVATE (GeditHighlightModeSelector, gedit_highlight_mode_selector, GTK_TYPE_GRID)
static void
gedit_highlight_mode_selector_class_init (GeditHighlightModeSelectorClass *klass)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
signals[LANGUAGE_SELECTED] =
g_signal_new ("language-selected",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GeditHighlightModeSelectorClass, language_selected),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE,
1,
GTK_SOURCE_TYPE_LANGUAGE);
/* Bind class to template */
gtk_widget_class_set_template_from_resource (widget_class,
"/org/gnome/gedit/ui/gedit-highlight-mode-selector.ui");
gtk_widget_class_bind_template_child_private (widget_class, GeditHighlightModeSelector, treeview);
gtk_widget_class_bind_template_child_private (widget_class, GeditHighlightModeSelector, entry);
gtk_widget_class_bind_template_child_private (widget_class, GeditHighlightModeSelector, liststore);
gtk_widget_class_bind_template_child_private (widget_class, GeditHighlightModeSelector, treemodelfilter);
gtk_widget_class_bind_template_child_private (widget_class, GeditHighlightModeSelector, treeview_selection);
}
static gboolean
visible_func (GtkTreeModel *model,
GtkTreeIter *iter,
GeditHighlightModeSelector *selector)
{
const gchar *entry_text;
gchar *name;
gchar *name_lower;
gchar *text_lower;
gboolean visible = FALSE;
entry_text = gtk_entry_get_text (GTK_ENTRY (selector->priv->entry));
if (*entry_text == '\0')
{
return TRUE;
}
gtk_tree_model_get (model, iter, COLUMN_NAME, &name, -1);
name_lower = g_utf8_strdown (name, -1);
g_free (name);
text_lower = g_utf8_strdown (entry_text, -1);
if (strstr (name_lower, text_lower) != NULL)
{
visible = TRUE;
}
g_free (name_lower);
g_free (text_lower);
return visible;
}
static void
on_entry_changed (GtkEntry *entry,
GeditHighlightModeSelector *selector)
{
GtkTreeIter iter;
gtk_tree_model_filter_refilter (selector->priv->treemodelfilter);
if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (selector->priv->treemodelfilter), &iter))
{
gtk_tree_selection_select_iter (selector->priv->treeview_selection, &iter);
}
}
static gboolean
move_selection (GeditHighlightModeSelector *selector,
gint howmany)
{
GtkTreeIter iter;
GtkTreePath *path;
gint *indices;
gint ret = FALSE;
if (!gtk_tree_selection_get_selected (selector->priv->treeview_selection, NULL, &iter) &&
!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (selector->priv->treemodelfilter), &iter))
{
return FALSE;
}
path = gtk_tree_model_get_path (GTK_TREE_MODEL (selector->priv->treemodelfilter), &iter);
indices = gtk_tree_path_get_indices (path);
if (indices)
{
gint num;
gint idx;
GtkTreePath *new_path;
idx = indices[0];
num = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (selector->priv->treemodelfilter), NULL);
if ((idx + howmany) < 0)
{
idx = 0;
}
else if ((idx + howmany) >= num)
{
idx = num - 1;
}
else
{
idx = idx + howmany;
}
new_path = gtk_tree_path_new_from_indices (idx, -1);