gcucomboperiodic.c

00001 /* 
00002  * Gnome Chemisty Utils
00003  * gcucomboperiodic.c
00004  *
00005  * Copyright (C) 2006-2010 Jean Bréfort <jean.brefort@normalesup.org>
00006  *
00007  * This program is free software; you can redistribute it and/or 
00008  * modify it under the terms of the GNU General Public License as 
00009  * published by the Free Software Foundation; either version 2 of the
00010  * License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00020  * USA
00021  */
00022 
00023 #include "config.h"
00024 #include "gcuperiodic.h"
00025 #include "gcucomboperiodic.h"
00026 #include "chemistry.h"
00027 #include <goffice/goffice.h>
00028 #include <gsf/gsf-impl-utils.h>
00029 #include <glib/gi18n-lib.h>
00030 
00031 struct _GcuComboPeriodic {
00032         GOComboBox     base;
00033 
00034         GtkWidget    *periodic, *preview_button, *label;
00035         gulong handler_id;
00036 };
00037 
00038 typedef struct {
00039         GOComboBoxClass base;
00040         void (* changed) (GcuComboPeriodic *combo, int id);
00041 } GcuComboPeriodicClass;
00042 
00043 enum {
00044         CHANGED,
00045         LAST_SIGNAL
00046 };
00047 
00048 static guint go_combo_pixmaps_signals [LAST_SIGNAL] = { 0, };
00049 
00050 static void
00051 cb_screen_changed (GcuComboPeriodic *combo, G_GNUC_UNUSED GdkScreen *previous_screen)
00052 {
00053         GtkWidget *w = GTK_WIDGET (combo);
00054         GdkScreen *screen = gtk_widget_has_screen (w)
00055                 ? gtk_widget_get_screen (w)
00056                 : NULL;
00057 
00058         if (screen) {
00059                 GtkWidget *toplevel = gtk_widget_get_toplevel (combo->periodic);
00060                 gtk_window_set_screen (GTK_WINDOW (toplevel), screen);
00061         }
00062 }
00063 
00064 static void
00065 element_changed_cb (GcuComboPeriodic *combo)
00066 {
00067         int newZ = gcu_periodic_get_element (GCU_PERIODIC (combo->periodic));
00068         gtk_label_set_text (GTK_LABEL (combo->label), gcu_element_get_symbol (newZ));
00069         if (_go_combo_is_updating (GO_COMBO_BOX (combo)))
00070                 return;
00071         g_signal_emit (combo, go_combo_pixmaps_signals [CHANGED], 0, newZ);
00072         go_combo_box_popup_hide (GO_COMBO_BOX (combo));
00073 }
00074 
00075 static void
00076 gcu_combo_periodic_init (GcuComboPeriodic *combo)
00077 {
00078         combo->preview_button = gtk_toggle_button_new ();
00079         combo->label = gtk_label_new ("");
00080         gtk_widget_show (combo->label);
00081         gtk_container_add (GTK_CONTAINER (combo->preview_button),
00082                 GTK_WIDGET (combo->label));
00083 
00084         g_signal_connect (G_OBJECT (combo),
00085                 "screen-changed",
00086                 G_CALLBACK (cb_screen_changed), NULL);
00087 
00088         gtk_widget_show_all (combo->preview_button);
00089         combo->periodic = gcu_periodic_new ();
00090         combo->handler_id = g_signal_connect_swapped (combo->periodic,
00091                 "element_changed", G_CALLBACK (element_changed_cb), combo);
00092         gtk_widget_show_all (combo->periodic);
00093         go_combo_box_construct (GO_COMBO_BOX (combo),
00094                 combo->preview_button, combo->periodic, combo->periodic);
00095         go_combo_box_set_title(GO_COMBO_BOX (combo), _("Periodic table of the elements"));
00096         gtk_widget_show_all (GTK_WIDGET (combo));
00097 }
00098 
00099 static void
00100 gcu_combo_periodic_class_init (GObjectClass *gobject_class)
00101 {
00102         go_combo_pixmaps_signals [CHANGED] =
00103                 g_signal_new ("changed",
00104                               G_OBJECT_CLASS_TYPE (gobject_class),
00105                               G_SIGNAL_RUN_LAST,
00106                               G_STRUCT_OFFSET (GcuComboPeriodicClass, changed),
00107                               NULL, NULL,
00108                               g_cclosure_marshal_VOID__INT,
00109                               G_TYPE_NONE, 1, G_TYPE_INT);
00110 }
00111 
00112 GSF_CLASS (GcuComboPeriodic, gcu_combo_periodic,
00113            gcu_combo_periodic_class_init, gcu_combo_periodic_init,
00114            GO_TYPE_COMBO_BOX)
00115 
00116 GtkWidget *gcu_combo_periodic_new (void)
00117 {
00118         return GTK_WIDGET (g_object_new (GCU_TYPE_COMBO_PERIODIC, NULL));
00119 }
00120 
00121 guint   gcu_combo_periodic_get_element  (GcuComboPeriodic* combo)
00122 {
00123         return gcu_periodic_get_element (GCU_PERIODIC (combo->periodic));
00124 }
00125 
00126 void    gcu_combo_periodic_set_element  (GcuComboPeriodic* combo, guint element)
00127 {
00128         g_signal_handler_block (combo->periodic, combo->handler_id);
00129         gcu_periodic_set_element (GCU_PERIODIC (combo->periodic), element);
00130         g_signal_handler_unblock (combo->periodic, combo->handler_id);
00131         gtk_label_set_text (GTK_LABEL (combo->label), gcu_element_get_symbol (element));
00132 }
Generated by  doxygen 1.6.2-20100208