medit/moo/plugins/usertools/moocommanddisplay.cpp

322 lines
8.6 KiB
C++
Raw Normal View History

2006-08-16 22:08:49 -07:00
/*
* moocommanddisplay.c
*
2010-12-21 20:15:45 -08:00
* Copyright (C) 2004-2010 by Yevgen Muntyan <emuntyan@users.sourceforge.net>
2006-08-16 22:08:49 -07:00
*
* This file is part of medit. medit is free software; you can
* redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License,
* or (at your option) any later version.
2006-08-16 22:08:49 -07:00
*
* You should have received a copy of the GNU Lesser General Public
* License along with medit. If not, see <http://www.gnu.org/licenses/>.
2006-08-16 22:08:49 -07:00
*/
2010-08-30 22:19:58 -07:00
#include "moocommanddisplay.h"
#include "moocommand-private.h"
2007-12-19 12:02:42 -08:00
#include "mooutils/mooutils-misc.h"
2006-08-16 22:08:49 -07:00
#include <string.h>
2006-08-20 01:49:33 -07:00
typedef struct {
MooCommandData *data;
MooCommandFactory *factory;
2006-08-20 01:49:33 -07:00
gboolean changed;
} Data;
struct _MooCommandDisplay {
MooTreeHelper base;
GtkComboBox *factory_combo;
2006-08-20 01:49:33 -07:00
GtkNotebook *notebook;
Data *data;
int n_factories;
2006-08-20 01:49:33 -07:00
int active;
int original;
};
2006-08-16 22:08:49 -07:00
2006-08-20 01:49:33 -07:00
struct _MooCommandDisplayClass {
MooTreeHelperClass base_class;
2006-08-16 22:08:49 -07:00
};
2006-08-20 01:49:33 -07:00
G_DEFINE_TYPE (MooCommandDisplay, _moo_command_display, MOO_TYPE_TREE_HELPER)
2006-08-16 22:08:49 -07:00
static void combo_changed (MooCommandDisplay *display);
static void
block_combo (MooCommandDisplay *display)
{
g_signal_handlers_block_by_func (display->factory_combo,
2006-08-16 22:08:49 -07:00
(gpointer) combo_changed,
display);
}
static void
unblock_combo (MooCommandDisplay *display)
{
g_signal_handlers_unblock_by_func (display->factory_combo,
2006-08-16 22:08:49 -07:00
(gpointer) combo_changed,
display);
}
static void
combo_changed (MooCommandDisplay *display)
{
GtkWidget *widget;
int index;
MooCommandFactory *factory;
2006-08-16 22:08:49 -07:00
index = gtk_combo_box_get_active (display->factory_combo);
2006-08-16 22:08:49 -07:00
g_return_if_fail (index >= 0);
2006-08-20 01:49:33 -07:00
if (index == display->active)
return;
if (display->active >= 0)
{
widget = gtk_notebook_get_nth_page (display->notebook, display->active);
if (_moo_command_factory_save_data (display->data[display->active].factory, widget,
display->data[display->active].data))
display->data[display->active].changed = TRUE;
}
2006-08-20 01:49:33 -07:00
display->active = index;
factory = display->data[index].factory;
g_return_if_fail (factory != NULL);
2006-08-16 22:08:49 -07:00
2006-08-20 01:49:33 -07:00
if (!display->data[index].data)
display->data[index].data = moo_command_data_new (factory->n_keys);
2006-08-16 22:08:49 -07:00
gtk_notebook_set_current_page (display->notebook, index);
widget = gtk_notebook_get_nth_page (display->notebook, index);
_moo_command_factory_load_data (factory, widget, display->data[index].data);
2006-08-16 22:08:49 -07:00
}
static void
combo_set_active (MooCommandDisplay *display,
int index)
{
block_combo (display);
gtk_combo_box_set_active (display->factory_combo, index);
2006-08-16 22:08:49 -07:00
unblock_combo (display);
if (index < 0)
{
gtk_widget_hide (GTK_WIDGET (display->notebook));
}
else
{
gtk_notebook_set_current_page (display->notebook, index);
gtk_widget_show (GTK_WIDGET (display->notebook));
}
}
static int
combo_find_factory (MooCommandDisplay *display,
MooCommandFactory *factory)
2006-08-16 22:08:49 -07:00
{
2006-08-20 01:49:33 -07:00
int i;
2006-08-16 22:08:49 -07:00
g_return_val_if_fail (!factory || MOO_IS_COMMAND_FACTORY (factory), -1);
2006-08-16 22:08:49 -07:00
if (!factory)
2006-08-20 01:49:33 -07:00
return -1;
2006-08-16 22:08:49 -07:00
for (i = 0; i < display->n_factories; ++i)
if (display->data[i].factory == factory)
2006-08-20 01:49:33 -07:00
return i;
2006-08-16 22:08:49 -07:00
2006-08-20 01:49:33 -07:00
g_return_val_if_reached (-1);
2006-08-16 22:08:49 -07:00
}
void
_moo_command_display_set (MooCommandDisplay *display,
MooCommandFactory *factory,
2006-08-16 22:08:49 -07:00
MooCommandData *data)
{
int index;
GtkWidget *widget;
g_return_if_fail (MOO_IS_COMMAND_DISPLAY (display));
g_return_if_fail (!factory || MOO_IS_COMMAND_FACTORY (factory));
g_return_if_fail (!factory == !data);
2006-08-16 22:08:49 -07:00
for (index = 0; index < display->n_factories; ++index)
2006-08-16 22:08:49 -07:00
{
2006-08-20 01:49:33 -07:00
display->data[index].changed = FALSE;
if (display->data[index].data)
moo_command_data_unref (display->data[index].data);
display->data[index].data = NULL;
2006-08-16 22:08:49 -07:00
}
index = combo_find_factory (display, factory);
2006-08-16 22:08:49 -07:00
2006-08-20 01:49:33 -07:00
display->active = index;
display->original = index;
2006-08-16 22:08:49 -07:00
combo_set_active (display, index);
2006-08-20 01:49:33 -07:00
if (index >= 0)
{
display->data[index].data = moo_command_data_ref (data);
widget = gtk_notebook_get_nth_page (display->notebook, index);
_moo_command_factory_load_data (factory, widget, data);
2006-08-20 01:49:33 -07:00
}
2006-08-16 22:08:49 -07:00
}
gboolean
_moo_command_display_get (MooCommandDisplay *display,
MooCommandFactory **factory_p,
2006-08-20 01:49:33 -07:00
MooCommandData **data_p)
2006-08-16 22:08:49 -07:00
{
2006-08-20 01:49:33 -07:00
GtkWidget *widget;
Data *data;
2006-08-16 22:08:49 -07:00
2006-08-20 01:49:33 -07:00
g_return_val_if_fail (MOO_IS_COMMAND_DISPLAY (display), FALSE);
2006-08-16 22:08:49 -07:00
2006-08-20 01:49:33 -07:00
if (display->active < 0)
return FALSE;
2006-08-16 22:08:49 -07:00
2006-08-20 01:49:33 -07:00
data = &display->data[display->active];
widget = gtk_notebook_get_nth_page (display->notebook, display->active);
2006-08-16 22:08:49 -07:00
if (_moo_command_factory_save_data (data->factory, widget, data->data))
2006-08-20 01:49:33 -07:00
data->changed = TRUE;
2006-08-16 22:08:49 -07:00
2006-08-20 01:49:33 -07:00
if (display->active == display->original && !data->changed)
2006-08-16 22:08:49 -07:00
return FALSE;
*factory_p = data->factory;
2006-08-20 01:49:33 -07:00
*data_p = data->data;
data->changed = FALSE;
display->original = display->active;
2006-08-16 22:08:49 -07:00
return TRUE;
}
static void
init_factory_combo (MooCommandDisplay *display,
GtkComboBox *combo,
GtkNotebook *notebook)
2006-08-16 22:08:49 -07:00
{
GtkListStore *store;
GSList *factories;
2006-08-16 22:08:49 -07:00
GtkCellRenderer *cell;
g_return_if_fail (MOO_IS_COMMAND_DISPLAY (display));
g_return_if_fail (GTK_IS_COMBO_BOX (combo));
g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
display->factory_combo = combo;
2006-08-16 22:08:49 -07:00
display->notebook = notebook;
cell = gtk_cell_renderer_text_new ();
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (display->factory_combo), cell, TRUE);
2017-10-23 16:13:26 -07:00
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (display->factory_combo), cell, "text", 0, nullptr);
2006-08-16 22:08:49 -07:00
2006-08-20 01:49:33 -07:00
store = gtk_list_store_new (1, G_TYPE_STRING);
2006-08-16 22:08:49 -07:00
factories = moo_command_list_factories ();
2006-08-20 01:49:33 -07:00
display->active = -1;
display->original = -1;
display->n_factories = 0;
display->data = g_new0 (Data, g_slist_length (factories));
2006-08-16 22:08:49 -07:00
while (factories)
2006-08-16 22:08:49 -07:00
{
GtkTreeIter iter;
MooCommandFactory *cmd_factory;
2006-08-16 22:08:49 -07:00
GtkWidget *widget;
2016-10-16 01:31:24 -07:00
cmd_factory = (MooCommandFactory*) factories->data;
widget = _moo_command_factory_create_widget (cmd_factory);
2006-08-16 22:08:49 -07:00
2006-08-20 01:49:33 -07:00
if (widget)
{
gtk_widget_show (widget);
gtk_notebook_append_page (display->notebook, widget, NULL);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, 0, cmd_factory->display_name, -1);
2006-08-16 22:08:49 -07:00
display->data[display->n_factories].factory = cmd_factory;
display->n_factories++;
2006-08-20 01:49:33 -07:00
}
2006-08-16 22:08:49 -07:00
factories = g_slist_delete_link (factories, factories);
2006-08-16 22:08:49 -07:00
}
2007-06-14 00:18:35 -07:00
gtk_combo_box_set_model (display->factory_combo, GTK_TREE_MODEL (store));
g_signal_connect_swapped (display->factory_combo, "changed",
2006-08-16 22:08:49 -07:00
G_CALLBACK (combo_changed), display);
g_object_unref (store);
}
MooCommandDisplay *
_moo_command_display_new (GtkComboBox *factory_combo,
2006-08-16 22:08:49 -07:00
GtkNotebook *notebook,
GtkWidget *treeview,
GtkWidget *new_btn,
GtkWidget *delete_btn,
GtkWidget *up_btn,
GtkWidget *down_btn)
{
MooCommandDisplay *display;
2016-10-16 01:31:24 -07:00
display = (MooCommandDisplay*) g_object_new (MOO_TYPE_COMMAND_DISPLAY, (const char*) NULL);
2006-08-16 22:08:49 -07:00
_moo_tree_helper_connect (MOO_TREE_HELPER (display), treeview,
new_btn, delete_btn, up_btn, down_btn);
init_factory_combo (display, factory_combo, notebook);
2006-08-16 22:08:49 -07:00
2010-11-07 03:32:15 -08:00
g_object_ref_sink (display);
2006-08-16 22:08:49 -07:00
return display;
}
2006-08-20 01:49:33 -07:00
static void
moo_command_display_dispose (GObject *object)
{
MooCommandDisplay *display = MOO_COMMAND_DISPLAY (object);
if (display->data)
{
int i;
for (i = 0; i < display->n_factories; ++i)
2006-08-20 01:49:33 -07:00
if (display->data[i].data)
moo_command_data_unref (display->data[i].data);
g_free (display->data);
display->data = NULL;
}
G_OBJECT_CLASS (_moo_command_display_parent_class)->dispose (object);
}
2006-08-16 22:08:49 -07:00
static void
_moo_command_display_init (G_GNUC_UNUSED MooCommandDisplay *display)
{
}
static void
2006-08-20 01:49:33 -07:00
_moo_command_display_class_init (MooCommandDisplayClass *klass)
2006-08-16 22:08:49 -07:00
{
2006-08-20 01:49:33 -07:00
G_OBJECT_CLASS (klass)->dispose = moo_command_display_dispose;
2006-08-16 22:08:49 -07:00
}