设置文件:
~/.config/xfce4/panel/*.rc
插件右键属性信号:
g_signal_connect (G_OBJECT (plugin), "configure-plugin", G_CALLBACK (sample_configure), sample);
sample-dialogs.c
void sample_configure (XfcePanelPlugin *plugin, SamplePlugin *sample)
{
dialog = xfce_titled_dialog_new_with_buttons(...);
textView = gtk_text_view_new ();
g_signal_connect (G_OBJECT(textView), "focus-out-event", G_CALLBACK (textView_change_cb), sample);
g_signal_connect (G_OBJECT (dialog), "response", G_CALLBACK(sample_configure_response), sample);
}
static gboolean textView_change_cb(GtkWidget *widget, GdkEventFocus *ev, SamplePlugin *sample)
{
GtkTextIter start, end;
GtkTextBuffer *buffer;
gchar *s;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
gtk_text_buffer_get_bounds (buffer, &start, &end);
s = gtk_text_iter_get_text (&start, &end);
sample->setting1 = s;
update(sample);
return FALSE;
}
static void sample_configure_response (GtkWidget *dialog, gint response, SamplePlugin *sample)
{
gboolean result;
if (response == GTK_RESPONSE_HELP) {
result = g_spawn_command_line_async ("exo-open --launch WebBrowser " PLUGIN_WEBSITE, NULL);
if (G_UNLIKELY (result == FALSE))
g_warning (_("Unable to open the following url: %s"), PLUGIN_WEBSITE);
} else {
/* remove the dialog data from the plugin */
g_object_set_data (G_OBJECT (sample->plugin), "dialog", NULL);
/* unlock the panel menu */
xfce_panel_plugin_unblock_menu (sample->plugin);
/* save the plugin */
sample_save (sample->plugin, sample);
/* destroy the properties dialog */
gtk_widget_destroy (dialog);
}
}
sample.c
void sample_save (XfcePanelPlugin *plugin, SamplePlugin *sample)
{
XfceRc *rc;
gchar *file;
/* get the config file location */
file = xfce_panel_plugin_save_location (plugin, TRUE);
if (G_UNLIKELY (file == NULL)) {
DBG ("Failed to open config file");
return;
}
/* open the config file, read/write */
rc = xfce_rc_simple_open (file, FALSE);
g_free (file);
if (G_LIKELY (rc != NULL)) {
/* save the settings */
DBG(".");
if (sample->setting1)
xfce_rc_write_entry (rc, "setting1", sample->setting1);
xfce_rc_write_int_entry (rc, "setting2", sample->setting2);
xfce_rc_write_bool_entry (rc, "setting3", sample->setting3);
/* close the rc file */
xfce_rc_close (rc);
}
}
static void sample_read (SamplePlugin *sample)
{
XfceRc *rc;
gchar *file;
const gchar *value;
/* get the plugin config file location */
file = xfce_panel_plugin_save_location (sample->plugin, TRUE);
if (G_LIKELY (file != NULL)) {
/* open the config file, readonly */
rc = xfce_rc_simple_open (file, TRUE);
/* cleanup */
g_free (file);
if (G_LIKELY (rc != NULL)) {
/* read the settings */
value = xfce_rc_read_entry (rc, "setting1", "");
sample->setting1 = g_strdup (value);
sample->setting2 = xfce_rc_read_int_entry (rc, "setting2", DEFAULT_SETTING2);
sample->setting3 = xfce_rc_read_bool_entry (rc, "setting3", DEFAULT_SETTING3);
/* cleanup */
xfce_rc_close (rc);
/* leave the function, everything went well */
return;
}
}
/* something went wrong, apply default values */
DBG ("Applying default settings");
sample->setting1 = g_strdup (DEFAULT_SETTING1);
sample->setting2 = DEFAULT_SETTING2;
sample->setting3 = DEFAULT_SETTING3;
}
void update (SamplePlugin *sample)
{
gtk_widget_set_tooltip_text(sample->label, sample->setting1);
}