/* Evolution internal utilities - Glade dialog widget utilities * * Copyright (C) 2000 Ximian, Inc. * Copyright (C) 2000 Ximian, Inc. * * Author: Federico Mena-Quintero * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. * * 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., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "e-dialog-widgets.h" /* A widget, a pointer to the variable it will modify, and extra information */ typedef struct { GtkWidget *widget; gpointer value_var; gpointer info; } WidgetHook; /* Hook information for a complete dialog */ typedef struct { GSList *whooks; } DialogHooks; /* Destroy handler for the dialog; frees the dialog hooks */ static void dialog_destroy_cb (GtkObject *dialog, gpointer data) { DialogHooks *hooks; hooks = data; g_slist_free (hooks->whooks); hooks->whooks = NULL; g_free (hooks); gtk_object_set_data (dialog, "dialog-hooks", NULL); } /* Ensures that the dialog has the necessary attached data to store the widget * hook information. */ static DialogHooks * get_dialog_hooks (GtkWidget *dialog) { DialogHooks *hooks; hooks = gtk_object_get_data (GTK_OBJECT (dialog), "dialog-hooks"); if (!hooks) { hooks = g_new0 (DialogHooks, 1); gtk_object_set_data (GTK_OBJECT (dialog), "dialog-hooks", hooks); gtk_signal_connect (GTK_OBJECT (dialog), "destroy", GTK_SIGNAL_FUNC (dialog_destroy_cb), hooks); } return hooks; } /* Converts an mapped value to the appropriate index in an item group. The * values for the items are provided as a -1-terminated array. */ static int value_to_index (const int *value_map, int value) { int i; for (i = 0; value_map[i] != -1; i++) if (value_map[i] == value) return i; return -1; } /* Converts an index in an item group to the appropriate mapped value. See the * function above. */ static int index_to_value (const int *value_map, int index) { int i; /* We do this the hard way, i.e. not as a simple array reference, to * check for correctness. */ for (i = 0; value_map[i] != -1; i++) if (i == index) return value_map[i]; return -1; } /* Callback for the "toggled" signal of toggle buttons */ static void toggled_cb (GtkToggleButton *toggle, gpointer data) { GnomePropertyBox *pbox; pbox = GNOME_PROPERTY_BOX (data); /* For radio buttons, we only notify the property box if the button is * active, because we'll get one call for each of the changed buttons in * the radio group. */ if (!GTK_IS_RADIO_BUTTON (toggle) || toggle->active) gnome_property_box_changed (pbox); } /* Hooks a radio button group */ static void hook_radio (GtkWidget *dialog, GtkRadioButton *radio, gpointer value_var, gpointer info) { GSList *group; GSList *l; int *value; const int *value_map; group = gtk_radio_button_group (radio); /* Set the value */ value = (int *) value_var; value_map = (const int *) info; e_dialog_radio_set (GTK_WIDGET (radio), *value, value_map); /* Hook to changed */ if (GNOME_IS_PROPERTY_BOX (dialog)) for (l = group; l; l = l->next) gtk_signal_connect (GTK_OBJECT (l->data), "toggled", GTK_SIGNAL_FUNC (toggled_cb), dialog); } /* Gets the value of a radio button group */ static void get_radio_value (GtkRadioButton *radio, gpointer value_var, gpointer info) { int *value; const int *value_map; value = (int *) value_var; value_map = (const int *) info; *value = e_dialog_radio_get (GTK_WIDGET (radio), value_map); } /* Callback for the "activate" signal of menu items */ static void activate_cb (GtkMenuItem *item, gpointer data) { GnomePropertyBox *pbox; pbox = GNOME_PROPERTY_BOX (data); gnome_property_box_changed (pbox); } /* Hooks an option menu */ static void hook_option_menu (GtkWidget *dialog, GtkOptionMenu *omenu, gpointer value_var, gpointer info) { int *value; const int *value_map; /* Set the value */ value = (int *) value_var; value_map = (const int *) info; e_dialog_option_menu_set (GTK_WIDGET (omenu), *value, value_map); /* Hook to changed */ if (GNOME_IS_PROPERTY_BOX (dialog)) { GtkMenu *menu; GList *l; menu = GTK_MENU (gtk_option_menu_get_menu (omenu)); for (l = GTK_MENU_SHELL (menu)->children; l; l = l->next) gtk_signal_connect (GTK_OBJECT (l->data), "activate", GTK_SIGNAL_FUNC (activate_cb), dialog); } } /* Gets the value of an option menu */ static void get_option_menu_value (GtkOptionMenu *omenu, gpointer value_var, gpointer info) { int *value; const int *value_map; value = (int *) value_var; value_map = (const int *) info; *value = e_dialog_option_menu_get (GTK_WIDGET (omenu), value_map); } /* Hooks a toggle button */ static void hook_toggle (GtkWidget *dialog, GtkToggleButton *toggle, gpointer value_var, gpointer info) { gboolean *value; /* Set the value */ value = (gboolean *) value_var; e_dialog_toggle_set (GTK_WIDGET (toggle), *value); /* Hook to changed */ if (GNOME_IS_PROPERTY_BOX (dialog)) gtk_signal_connect (GTK_OBJECT (toggle), "toggled", GTK_SIGNAL_FUNC (toggled_cb), dialog); } /* Gets the value of a toggle button */ static void get_toggle_value (GtkToggleButton *toggle, gpointer value_var, gpointer info) { gboolean *value; value = (gboolean *) value_var; *value = e_dialog_toggle_get (GTK_WIDGET (toggle)); } /* Callback for the "value_changed" signal of the adjustment of a spin button */ static void value_changed_cb (GtkAdjustment *adj, gpointer data) { GnomePropertyBox *pbox; pbox = GNOME_PROPERTY_BOX (data); gnome_property_box_changed (pbox); } /* Hooks a spin button */ static void hook_spin_button (GtkWidget *dialog, GtkSpinButton *spin, gpointer value_var, gpointer info) { double *value; GtkAdjustment *adj; /* Set the value */ value = (double *) value_var; e_dialog_spin_set (GTK_WIDGET (spin), *value); /* Hook to changed */ adj = gtk_spin_button_get_adjustment (spin); if (GNOME_IS_PROPERTY_BOX (dialog)) gtk_signal_connect (GTK_OBJECT (adj), "value_changed", GTK_SIGNAL_FUNC (value_changed_cb), dialog); } /* Gets the value of a spin button */ static void get_spin_button_value (GtkSpinButton *spin, gpointer value_var, gpointer info) { double *value; value = (double *) value_var; *value = e_dialog_spin_get_double (GTK_WIDGET (spin)); } /* Callback for the "changed" signal of a GtkEditable widget */ static void changed_cb (GtkEditable *editable, gpointer data) { GnomePropertyBox *pbox; pbox = GNOME_PROPERTY_BOX (data); gnome_property_box_changed (pbox); } /* Hooks a GtkEditable widget */ static void hook_editable (GtkWidget *dialog, GtkEditable *editable, gpointer value_var, gpointer info) { char **value; /* Set the value */ value = (char **) value_var; e_dialog_editable_set (GTK_WIDGET (editable), *value); /* Hook to changed */ if (GNOME_IS_PROPERTY_BOX (dialog)) gtk_signal_connect (GTK_OBJECT (editable), "changed", GTK_SIGNAL_FUNC (changed_cb), dialog); } /* Gets the value of a GtkEditable widget */ static void get_editable_value (GtkEditable *editable, gpointer value_var, gpointer data) { char **value; value = (char **) value_var; if (*value) g_free (*value); *value = e_dialog_editable_get (GTK_WIDGET (editable)); } /** * e_dialog_editable_set: * @widget: A #GtkEditable widget. * @value: String value. * * Sets the string value inside a #GtkEditable-derived widget. **/ void e_dialog_editable_set (GtkWidget *widget, const char *value) { g_return_if_fail (widget != NULL); g_return_if_fail (GTK_IS_EDITABLE (widget)); gtk_editable_delete_text (GTK_EDITABLE (widget), 0, -1); if (value) { gint pos; pos = 0; e_utf8_gtk_editable_insert_text (GTK_EDITABLE (widget), value, strlen (value), &pos); } } /** * e_dialog_editable_get: * @widget: A #GtkEditable widget. * * Queries the string value inside a #GtkEditable-derived widget. * * Return value: String value. You should free it when you are done with it. * This function can return NULL if the string could not be converted from * GTK+'s encoding into UTF8. **/ char * e_dialog_editable_get (GtkWidget *widget) { g_return_val_if_fail (widget != NULL, NULL); g_return_val_if_fail (GTK_IS_EDITABLE (widget), NULL); return e_utf8_gtk_editable_get_chars (GTK_EDITABLE (widget), 0, -1); } /** * e_dialog_radio_set: * @widget: A #GtkRadioButton in a radio button group. * @value: Enumerated value. * @value_map: Map from enumeration values to array indices. * * Sets the selected item in a radio group. The specified @widget can be any of * the #GtkRadioButtons in the group. Each radio button should correspond to an * enumeration value; the specified @value will be mapped to an integer from * zero to the number of items in the group minus 1 by using a mapping table * specified in @value_map. The last element in this table should be -1. Thus * a table to map three possible interpolation values to integers could be * specified as { NEAREST_NEIGHBOR, BILINEAR, HYPERBOLIC, -1 }. **/ void e_dialog_radio_set (GtkWidget *widget, int value, const int *value_map) { GSList *group; int i; GSList *l; g_return_if_fail (widget != NULL); g_return_if_fail (GTK_IS_RADIO_BUTTON (widget)); g_return_if_fail (value_map != NULL); group = gtk_radio_button_group (GTK_RADIO_BUTTON (widget)); i = value_to_index (value_map, value); if (i != -1) { /* Groups are built by prepending items, so the list ends up in reverse * order; we need to flip the index around. */ i = g_slist_length (group) - i - 1; l = g_slist_nth (group, i); if (!l) g_message ("e_dialog_radio_set(): could not find index %d in radio group!", i); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (l->data), TRUE); } else g_message ("e_dialog_radio_set(): could not find value %d in value map!", value); } /** * e_dialog_radio_get: * @widget: A #GtkRadioButton in a radio button group. * @value_map: Map from enumeration values to array indices. * * Queries the selected item in a #GtkRadioButton group. Please read the * description of e_dialog_radio_set() to see how @value_map maps enumeration * values to button indices. * * Return value: Enumeration value which corresponds to the selected item in the * radio group. **/ int e_dialog_radio_get (GtkWidget *widget, const int *value/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ /* * folder-browser-factory.c: A Bonobo Control factory for Folder Browsers * * Author: * Miguel de Icaza (miguel@ximian.com) * * (C) 2000 Ximian, Inc. */ #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <bonobo/bonobo-main.h> #include <bonobo/bonobo-object.h> #include <bonobo/bonobo-generic-factory.h> #include <bonobo/bonobo-control.h> #include <bonobo/bonobo-ui-component.h> #include <bonobo/bonobo-ui-util.h> #include <gal/util/e-util.h> #include <gal/widgets/e-gui-utils.h> #include "widgets/menus/gal-view-menus.h" #include <gal/menus/gal-view-factory-etable.h> #include <gal/menus/gal-view-etable.h> #include "folder-browser-factory.h" #include "folder-browser.h" #include "folder-browser-ui.h" #include "mail.h" #include "mail-callbacks.h" #include "shell/Evolution.h" #include "mail-config.h" #include "mail-ops.h" #include "mail-session.h" #include "mail-folder-cache.h" #include "evolution-shell-component-utils.h" #include "camel/camel-vtrash-folder.h" /* The FolderBrowser BonoboControls we have. */ static EList *control_list = NULL; /* copied from mail-display.c for now.... */ static GNOME_Evolution_ShellView fb_get_svi (BonoboControl *control) { Bonobo_ControlFrame control_frame; GNOME_Evolution_ShellView shell_view_interface; CORBA_Environment ev; control_frame = bonobo_control_get_control_frame (control); if (control_frame == NULL) return CORBA_OBJECT_NIL; CORBA_exception_init (&ev); shell_view_interface = Bonobo_Unknown_queryInterface (control_frame, "IDL:GNOME/Evolution/ShellView:1.0", &ev); CORBA_exception_free (&ev); if (shell_view_interface == CORBA_OBJECT_NIL) g_warning ("Control frame doesn't have Evolution/ShellView."); return shell_view_interface; } static void control_activate (BonoboControl *control, BonoboUIComponent *uic, FolderBrowser *fb) { GtkWidget *folder_browser; Bonobo_UIContainer container; container = bonobo_control_get_remote_ui_container (control); bonobo_ui_component_set_container (uic, container); bonobo_object_release_unref (container, NULL); g_assert (container == bonobo_ui_component_get_container (uic)); g_return_if_fail (container != CORBA_OBJECT_NIL); folder_browser = bonobo_control_get_widget (control); folder_browser_set_ui_component (FOLDER_BROWSER (folder_browser), uic); folder_browser_ui_add_global (fb); folder_browser_ui_add_list (fb); folder_browser_ui_add_message (fb); mail_folder_cache_set_shell_view (fb_get_svi (control)); mail_folder_cache_set_folder_browser (fb); if (fb->folder) mail_refresh_folder (fb->folder, NULL, NULL); } static void control_deactivate (BonoboControl *control, BonoboUIComponent *uic, FolderBrowser *fb) { mail_folder_cache_set_folder_browser (NULL); folder_browser_ui_rm_list (fb); folder_browser_ui_rm_all (fb); if (fb->folder) mail_sync_folder (fb->folder, NULL, NULL); folder_browser_set_ui_component (fb, NULL); } static void control_activate_cb (BonoboControl *control, gboolean activate, gpointer user_data) { BonoboUIComponent *uic; uic = bonobo_control_get_ui_component (control); g_assert (uic != NULL); if (activate) control_activate (control, uic, user_data); else control_deactivate (control, uic, user_data); } static void control_destroy_cb (BonoboControl *control, GtkObject *folder_browser) { gtk_object_destroy (folder_browser); } static void browser_destroy_cb (FolderBrowser *fb, BonoboControl *control) { EIterator *it; /* We do this from browser_destroy_cb rather than * control_destroy_cb because currently, the controls * don't seem to all get destroyed properly at quit * time (but the widgets get destroyed by X). FIXME. */ for (it = e_list_get_iterator (control_list); e_iterator_is_valid (it); e_iterator_next (it)) { if (e_iterator_get (it) == control) { e_iterator_delete (it); break; } } gtk_object_unref (GTK_OBJECT (it)); } BonoboControl * folder_browser_factory_new_control (const char *uri, const GNOME_Evolution_Shell shell) { BonoboControl *control; GtkWidget *folder_browser; folder_browser = folder_browser_new (shell); if (folder_browser == NULL) return NULL; FOLDER_BROWSER (folder_browser)->pref_master = TRUE; /* save UI settings changed in this FB */ if (!folder_browser_set_uri (FOLDER_BROWSER (folder_browser), uri)) { gtk_object_sink (GTK_OBJECT (folder_browser)); return NULL; } gtk_widget_show (folder_browser); control = bonobo_control_new (folder_browser); if (control == NULL) { gtk_object_destroy (GTK_OBJECT (folder_browser)); return NULL; } gtk_signal_connect (GTK_OBJECT (control), "activate", control_activate_cb, folder_browser); gtk_signal_connect (GTK_OBJECT (control), "destroy", control_destroy_cb, folder_browser); gtk_signal_connect (GTK_OBJECT (folder_browser), "destroy", browser_destroy_cb, control); if (!control_list) control_list = e_list_new (NULL, NULL, NULL); e_list_append (control_list, control); return control; } EList * folder_browser_factory_get_control_list (void) { if (!control_list) control_list = e_list_new (NULL, NULL, NULL); return control_list; }