The Gnome Chemistry Utils  0.14.0
gcucomboperiodic.c
1 /*
2  * Gnome Chemisty Utils
3  * gcucomboperiodic.c
4  *
5  * Copyright (C) 2006-2011 Jean Bréfort <jean.brefort@normalesup.org>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20  * USA
21  */
22 
23 #include "config.h"
24 #include "gcuperiodic.h"
25 #include "gcucomboperiodic.h"
26 #include <gcu/chemistry.h>
27 #include <goffice/goffice.h>
28 #include <gsf/gsf-impl-utils.h>
29 #include <glib/gi18n-lib.h>
30 
31 struct _GcuComboPeriodic {
32  GOComboBox base;
33 
34  GtkWidget *periodic, *preview_button, *label;
35  gulong handler_id;
36 };
37 
38 typedef struct {
39  GOComboBoxClass base;
40  void (* changed) (GcuComboPeriodic *combo, int id);
41 } GcuComboPeriodicClass;
42 
43 enum {
44  CHANGED,
45  LAST_SIGNAL
46 };
47 
48 static guint go_combo_pixmaps_signals [LAST_SIGNAL] = { 0, };
49 
50 static void
51 cb_screen_changed (GcuComboPeriodic *combo, G_GNUC_UNUSED GdkScreen *previous_screen)
52 {
53  GtkWidget *w = GTK_WIDGET (combo);
54  GdkScreen *screen = gtk_widget_has_screen (w)
55  ? gtk_widget_get_screen (w)
56  : NULL;
57 
58  if (screen) {
59  GtkWidget *toplevel = gtk_widget_get_toplevel (combo->periodic);
60  gtk_window_set_screen (GTK_WINDOW (toplevel), screen);
61  }
62 }
63 
64 static void
65 element_changed_cb (GcuComboPeriodic *combo)
66 {
67  int newZ = gcu_periodic_get_element (GCU_PERIODIC (combo->periodic));
68  gtk_label_set_text (GTK_LABEL (combo->label), gcu_element_get_symbol (newZ));
69  if (_go_combo_is_updating (GO_COMBO_BOX (combo)))
70  return;
71  g_signal_emit (combo, go_combo_pixmaps_signals [CHANGED], 0, newZ);
72  go_combo_box_popup_hide (GO_COMBO_BOX (combo));
73 }
74 
75 static void
76 gcu_combo_periodic_init (GcuComboPeriodic *combo)
77 {
78  combo->preview_button = gtk_toggle_button_new ();
79  combo->label = gtk_label_new ("");
80  gtk_widget_show (combo->label);
81  gtk_container_add (GTK_CONTAINER (combo->preview_button),
82  GTK_WIDGET (combo->label));
83 
84  g_signal_connect (G_OBJECT (combo),
85  "screen-changed",
86  G_CALLBACK (cb_screen_changed), NULL);
87 
88  gtk_widget_show_all (combo->preview_button);
89  combo->periodic = gcu_periodic_new ();
90  combo->handler_id = g_signal_connect_swapped (combo->periodic,
91  "element_changed", G_CALLBACK (element_changed_cb), combo);
92  gtk_widget_show_all (combo->periodic);
93  go_combo_box_construct (GO_COMBO_BOX (combo),
94  combo->preview_button, combo->periodic, combo->periodic);
95  go_combo_box_set_title(GO_COMBO_BOX (combo), _("Periodic table of the elements"));
96  gtk_widget_show_all (GTK_WIDGET (combo));
97 }
98 
99 static void
100 gcu_combo_periodic_class_init (GObjectClass *gobject_class)
101 {
102  go_combo_pixmaps_signals [CHANGED] =
103  g_signal_new ("changed",
104  G_OBJECT_CLASS_TYPE (gobject_class),
105  G_SIGNAL_RUN_LAST,
106  G_STRUCT_OFFSET (GcuComboPeriodicClass, changed),
107  NULL, NULL,
108  g_cclosure_marshal_VOID__INT,
109  G_TYPE_NONE, 1, G_TYPE_INT);
110 }
111 
112 GSF_CLASS (GcuComboPeriodic, gcu_combo_periodic,
113  gcu_combo_periodic_class_init, gcu_combo_periodic_init,
114  GO_TYPE_COMBO_BOX)
115 
116 GtkWidget *gcu_combo_periodic_new (void)
117 {
118  return GTK_WIDGET (g_object_new (GCU_TYPE_COMBO_PERIODIC, NULL));
119 }
120 
121 guint gcu_combo_periodic_get_element (GcuComboPeriodic* combo)
122 {
123  return gcu_periodic_get_element (GCU_PERIODIC (combo->periodic));
124 }
125 
126 void gcu_combo_periodic_set_element (GcuComboPeriodic* combo, guint element)
127 {
128  g_signal_handler_block (combo->periodic, combo->handler_id);
129  gcu_periodic_set_element (GCU_PERIODIC (combo->periodic), element);
130  g_signal_handler_unblock (combo->periodic, combo->handler_id);
131  gtk_label_set_text (GTK_LABEL (combo->label), gcu_element_get_symbol (element));
132 }