#include <gtk/gtk.h>
#include <m17n-im-config.h>
-/* Argument to callback functions. */
+/* Common argument to all callback functions. */
-struct ConfigSaveRevert
+struct CallbackArgument
{
- /* IM configuration widget created by mim_config_widget (). */
+ /* IM configuration widget created by mim_config_new (). */
GtkWidget *config;
/* Button widget "revert" */
GtkWidget *revert;
};
-/* Called when the status of configuration (modified or not) is
- changed. */
+/* Called when the status of configuration is changed. */
void
-status_changed_cb (gboolean modified, gpointer data)
+status_changed_cb (GtkWidget *config, gpointer data)
{
- struct ConfigSaveRevert *config_save_revert = data;
+ struct CallbackArgument *arg = data;
+ gboolean modified = mim_config_modified (arg->config);
- if (GTK_WIDGET_SENSITIVE (config_save_revert->save))
+ if (GTK_WIDGET_SENSITIVE (arg->save))
{
if (! modified)
{
- gtk_widget_set_sensitive (config_save_revert->revert, FALSE);
- gtk_widget_set_sensitive (config_save_revert->save, FALSE);
+ gtk_widget_set_sensitive (arg->revert, FALSE);
+ gtk_widget_set_sensitive (arg->save, FALSE);
}
}
else
{
if (modified)
{
- gtk_widget_set_sensitive (config_save_revert->revert, TRUE);
- gtk_widget_set_sensitive (config_save_revert->save, TRUE);
+ gtk_widget_set_sensitive (arg->revert, TRUE);
+ gtk_widget_set_sensitive (arg->save, TRUE);
}
}
}
+/* Called when "revert" button is clicked. */
static void
revert_clicked_cb (GtkButton *button, gpointer data)
{
- struct ConfigSaveRevert *config_save_revert = data;
+ struct CallbackArgument *arg = data;
- mim_config_revert (config_save_revert->config);
+ mim_config_revert (arg->config);
status_changed_cb (FALSE, data);
}
+/* Called when "save" button is clicked. */
static void
save_clicked_cb (GtkButton *button, gpointer data)
{
- struct ConfigSaveRevert *config_save_revert = data;
+ struct CallbackArgument *arg = data;
- mim_config_save (config_save_revert->config);
+ mim_config_save (arg->config);
status_changed_cb (FALSE, data);
}
+/* Called when "quit" button is clicked. */
static void
quit_clicked_cb (GtkButton *button, gpointer data)
{
- struct ConfigSaveRevert *config_save_revert = data;
+ struct CallbackArgument *arg = data;
- if (mim_config_modified (config_save_revert->config))
+ if (mim_config_modified (arg->config))
{
GtkWidget *dialog, *label;
gint response;
if (response == GTK_RESPONSE_CANCEL)
return;
if (response == GTK_RESPONSE_YES)
- mim_config_save (config_save_revert->config);
+ mim_config_save (arg->config);
}
+ gtk_widget_destroy (arg->config);
gtk_main_quit ();
}
-
+/* Called when "ok" button is clicked. */
static void
ok_clicked_cb (GtkButton *button, gpointer data)
{
- struct ConfigSaveRevert *config_save_revert = data;
+ struct CallbackArgument *arg = data;
- if (mim_config_modified (config_save_revert->config))
- mim_config_save (config_save_revert->config);
+ if (mim_config_modified (arg->config))
+ mim_config_save (arg->config);
+ gtk_widget_destroy (arg->config);
gtk_main_quit ();
}
int
main (int argc, char **argv)
{
- GtkWidget *window, *vbox, *config, *hbox;
+ GtkWidget *window, *vbox, *hbox;
GtkWidget *revert, *save, *ok, *quit;
- struct ConfigSaveRevert config_save_revert;
- MimConfigCallback callback;
+ struct CallbackArgument arg;
#if ENABLE_NLS
bindtextdomain ("m17n-im-config", GETTEXTDIR);
bind_textdomain_codeset ("m17n-im-config", "UTF-8");
#endif
gtk_init (&argc, &argv);
- if (mim_config_init () < 0)
- {
- g_print ("m17n-im-config initialization error\n");
- exit (1);
- }
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request (window, 500, 300);
quit = gtk_button_new_from_stock (GTK_STOCK_QUIT);
ok = gtk_button_new_from_stock (GTK_STOCK_OK);
- config_save_revert.revert = revert;
- config_save_revert.save = save;
- callback.func = status_changed_cb;
- callback.arg = &config_save_revert;
- config = mim_config_widget (&callback);
- config_save_revert.config = config;
- gtk_box_pack_start (GTK_BOX (vbox), config, TRUE, TRUE, 0);
+ arg.revert = revert;
+ arg.save = save;
+ arg.config = mim_config_new (G_CALLBACK (status_changed_cb), &arg);
+ gtk_box_pack_start (GTK_BOX (vbox), arg.config, TRUE, TRUE, 0);
g_signal_connect (G_OBJECT (revert), "clicked",
- G_CALLBACK (revert_clicked_cb), &config_save_revert);
+ G_CALLBACK (revert_clicked_cb), &arg);
g_signal_connect (G_OBJECT (save), "clicked",
- G_CALLBACK (save_clicked_cb), &config_save_revert);
+ G_CALLBACK (save_clicked_cb), &arg);
g_signal_connect (G_OBJECT (quit), "clicked",
- G_CALLBACK (quit_clicked_cb), &config_save_revert);
+ G_CALLBACK (quit_clicked_cb), &arg);
g_signal_connect (G_OBJECT (ok), "clicked",
- G_CALLBACK (ok_clicked_cb), &config_save_revert);
+ G_CALLBACK (ok_clicked_cb), &arg);
hbox = gtk_hbutton_box_new ();
gtk_button_box_set_layout (GTK_BUTTON_BOX (hbox), GTK_BUTTONBOX_END);
gtk_widget_show_all (window);
gtk_main ();
- mim_config_fini ();
return 0;
}
#include <string.h>
#include <libintl.h>
#include <m17n.h>
+#include <m17n-misc.h>
#include <gtk/gtk.h>
#include <config.h>
#include "m17n-im-config.h"
#define CONFIG_STATUS_DATA " config-status-data"
#define CONFIG_TREE_VIEW " config-tree-view"
+typedef void (*MimConfigCallbackFunc) (GtkWidget *widget, gpointer data);
+
+typedef struct _MimConfigCallback
+{
+ GtkWidget *widget;
+ MimConfigCallbackFunc func;
+ gpointer data;
+} MimConfigCallback;
+
typedef struct _MimConfigStatus
{
/* Number of available input methods. */
callback = g_object_get_data (G_OBJECT (tree), CONFIG_CALLBACK_DATA);
if (callback)
- callback->func (config_status->num_modified == 0 ? FALSE : TRUE,
- callback->arg);
+ callback->func (callback->widget, callback->data);
}
}
return FALSE;
}
+static int initialized = 0;
+
+static void
+destroy_cb (GtkWidget *widget, gpointer data)
+{
+ M17N_FINI ();
+}
+
\f
/* Public API */
-int
-mim_config_init ()
+GtkWidget *
+mim_config_new (GCallback func, gpointer data)
{
+ GtkWidget *tree, *config;
+ GtkTreeStore *store;
+ GtkCellRenderer *renderer;
+ GtkTreeViewColumn *column;
+
+ if (initialized)
+ return NULL;
M17N_INIT ();
+ if (merror_code < 0)
+ return NULL;
#if ENABLE_NLS
bindtextdomain ("m17n-im-config", GETTEXTDIR);
mim_status_str[MIM_STATUS_MODIFIED] = _("modified");
mim_status_str[MIM_STATUS_NO] = _("uncustomizable");
- return 0;
-}
-
-int
-mim_config_fini ()
-{
- M17N_FINI ();
-
- return 0;
-}
-
-GtkWidget *
-mim_config_widget (MimConfigCallback *callback)
-{
- GtkWidget *tree, *config;
- GtkTreeStore *store;
- GtkCellRenderer *renderer;
- GtkTreeViewColumn *column;
-
store = make_store_for_input_methods ();
tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
g_object_unref (G_OBJECT (store));
- if (callback)
- g_object_set_data (G_OBJECT (tree), CONFIG_CALLBACK_DATA, callback);
renderer = gtk_cell_renderer_text_new ();
column = (gtk_tree_view_column_new_with_attributes
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (config),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (config), tree);
+ g_signal_connect (G_OBJECT (config), "destroy",
+ G_CALLBACK (destroy_cb), NULL);
g_object_set_data (G_OBJECT (config), CONFIG_TREE_VIEW, tree);
+ if (func)
+ {
+ MimConfigCallback *callback;
+
+ callback = g_new (MimConfigCallback, 1);
+ callback->widget = config;
+ callback->func = (MimConfigCallbackFunc) func;
+ callback->data = data;
+ g_object_set_data_full (G_OBJECT (tree), CONFIG_CALLBACK_DATA,
+ callback, g_free);
+ }
return config;
}