tagging release
[dasher.git] / trunk / Src / Gtk2 / module_settings_window.cpp
blob053c222a5de8c0825ee490b70c80471a773b0caa
1 #include "module_settings_window.h"
3 #include <glib/gi18n.h>
5 typedef struct _ModuleSettingsData ModuleSettingsData;
7 struct _ModuleSettingsData {
8 ModuleSettingsData *pNext;
9 GtkWidget *pWidget;
10 int iParameter;
11 int iDivisor;
14 struct _ModuleSettingsWindowPrivate {
15 DasherAppSettings *pAppSettings;
16 ModuleSettingsData *pFirst;
19 typedef struct _ModuleSettingsWindowPrivate ModuleSettingsWindowPrivate;
21 #define MODULE_SETTINGS_WINDOW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), TYPE_MODULE_SETTINGS_WINDOW, ModuleSettingsWindowPrivate))
23 G_DEFINE_TYPE(ModuleSettingsWindow, module_settings_window, GTK_TYPE_DIALOG)
25 /* Private members */
26 static ModuleSettingsData *module_settings_window_get_settings_data(ModuleSettingsWindow *pSelf, GtkWidget *pWidget);
27 static void module_settings_window_handle_bool_changed(ModuleSettingsWindow *pSelf, GtkToggleButton *pToggleButton);
28 static void module_settings_window_handle_long_changed(ModuleSettingsWindow *pSelf, GtkRange *pRange);
29 static void module_settings_window_handle_longspin_changed(ModuleSettingsWindow *pSelf, GtkSpinButton *pSpinButton);
30 static void module_settings_window_handle_string_changed(ModuleSettingsWindow *pSelf, GtkEditable *pEditable);
31 static gboolean module_settings_window_handle_close(ModuleSettingsWindow *pSelf);
33 // Callbacks (not strictly class members)
34 extern "C" void handle_bool_changed(GtkToggleButton *pToggleButton, gpointer pUserData);
35 extern "C" void handle_long_changed(GtkRange *pRange, gpointer pUserData);
36 extern "C" void handle_longspin_changed(GtkSpinButton *pSpinButton, gpointer pUserData);
37 extern "C" void handle_string_changed(GtkEditable *pEditable, gpointer pUserData);
38 extern "C" gboolean handle_close(GtkWidget *pWidget, gpointer pUserData);
39 extern "C" gboolean handle_close_event(GtkWidget *pWidget, GdkEvent *pEvent, gpointer pUserData);
41 static void
42 module_settings_window_class_init(ModuleSettingsWindowClass *pClass) {
43 // GObjectClass *pObjectClass = (GObjectClass *) pClass;
44 // pObjectClass->finalize = module_settings_window_destroy;
45 g_type_class_add_private(pClass, sizeof(ModuleSettingsWindowPrivate));
48 static void
49 module_settings_window_init(ModuleSettingsWindow *pDasherControl) {
50 ModuleSettingsWindowPrivate *pPrivateData = MODULE_SETTINGS_WINDOW_GET_PRIVATE(pDasherControl);
52 pPrivateData->pFirst = NULL;
54 GtkWidget *pButton = gtk_dialog_add_button(&(pDasherControl->window), GTK_STOCK_CLOSE, GTK_RESPONSE_NONE);
56 g_signal_connect(G_OBJECT(pButton), "clicked", G_CALLBACK(handle_close), pDasherControl);
57 g_signal_connect(G_OBJECT(&(pDasherControl->window)), "delete-event", G_CALLBACK(handle_close_event), pDasherControl);
60 GtkWidget *
61 module_settings_window_new(DasherAppSettings *pAppSettings, const gchar *szName, SModuleSettings *pSettings, int iCount) {
62 ModuleSettingsWindow *pDasherControl;
63 pDasherControl = MODULE_SETTINGS_WINDOW(g_object_new(module_settings_window_get_type(), NULL));
65 gtk_window_set_title(GTK_WINDOW(pDasherControl), "Dasher Module Options");
67 ModuleSettingsWindowPrivate *pPrivate = MODULE_SETTINGS_WINDOW_GET_PRIVATE(pDasherControl);
69 pPrivate->pAppSettings = pAppSettings;
71 gchar *szFrameTitle = g_strdup_printf(_("%s Options:"), szName);
73 GtkWidget *pFrame = gtk_frame_new(szFrameTitle);
75 delete[] szFrameTitle;
77 gtk_container_add(GTK_CONTAINER(pDasherControl->window.vbox), pFrame);
78 g_object_set(G_OBJECT(pFrame), "border-width", 8, NULL);
80 GtkWidget *pTable = gtk_table_new(iCount, 2, FALSE);
81 g_object_set(G_OBJECT(pTable), "border-width", 8, NULL);
82 gtk_container_add(GTK_CONTAINER(pFrame), pTable);
85 for(int i(0); i < iCount; ++i) {
86 GtkWidget *pControl = NULL;
88 if(pSettings[i].iType == T_BOOL) {
89 pControl = gtk_check_button_new_with_label(pSettings[i].szDescription);
91 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pControl),
92 dasher_app_settings_get_bool(pPrivate->pAppSettings, pSettings[i].iParameter));
94 gtk_table_attach_defaults(GTK_TABLE(pTable), pControl, 0, 2, i, i+1);
95 g_signal_connect(G_OBJECT(pControl), "toggled", G_CALLBACK(handle_bool_changed), pDasherControl);
98 else {
99 GtkWidget *pLabel = gtk_label_new(pSettings[i].szDescription);
100 gtk_misc_set_alignment(GTK_MISC(pLabel), 0, 0.5);
101 gtk_table_attach_defaults(GTK_TABLE(pTable), pLabel, 0, 1, i, i+1);
103 switch(pSettings[i].iType) {
104 case T_LONG:
105 pControl = gtk_hscale_new_with_range(pSettings[i].iMin / static_cast<double>(pSettings[i].iDivisor),
106 pSettings[i].iMax / static_cast<double>(pSettings[i].iDivisor),
107 pSettings[i].iStep / static_cast<double>(pSettings[i].iDivisor));
109 gtk_range_set_increments(GTK_RANGE(pControl), pSettings[i].iStep / static_cast<double>(pSettings[i].iDivisor),
110 pSettings[i].iStep / static_cast<double>(pSettings[i].iDivisor));
112 gtk_range_set_value(GTK_RANGE(pControl),
113 dasher_app_settings_get_long(pPrivate->pAppSettings, pSettings[i].iParameter)
114 / static_cast<double>(pSettings[i].iDivisor));
116 gtk_widget_set_size_request(pControl, 256, -1);
118 g_signal_connect(G_OBJECT(pControl), "value-changed", G_CALLBACK(handle_long_changed), pDasherControl);
119 break;
120 case T_LONGSPIN:
121 pControl = gtk_spin_button_new_with_range(pSettings[i].iMin / static_cast<double>(pSettings[i].iDivisor),
122 pSettings[i].iMax / static_cast<double>(pSettings[i].iDivisor),
123 pSettings[i].iStep / static_cast<double>(pSettings[i].iDivisor));
124 gtk_spin_button_set_value(GTK_SPIN_BUTTON(pControl),
125 dasher_app_settings_get_long(pPrivate->pAppSettings, pSettings[i].iParameter)
126 / static_cast<double>(pSettings[i].iDivisor));
128 gtk_widget_set_size_request(pControl, 256, -1);
130 g_signal_connect(G_OBJECT(pControl), "value-changed", G_CALLBACK(handle_longspin_changed), pDasherControl);
131 break;
132 case T_STRING:
133 pControl = gtk_entry_new();
134 gtk_entry_set_text(GTK_ENTRY(pControl),
135 dasher_app_settings_get_string(pPrivate->pAppSettings, pSettings[i].iParameter));
136 gtk_widget_set_size_request(pControl, 256, -1);
138 g_signal_connect(G_OBJECT(pControl), "changed", G_CALLBACK(handle_string_changed), pDasherControl);
139 break;
140 default:
141 g_error("Unknown type specified for module setting");
142 break;
145 gtk_table_attach_defaults(GTK_TABLE(pTable), pControl, 1, 2, i, i+1);
148 gtk_table_set_row_spacings(GTK_TABLE(pTable), 4);
149 gtk_table_set_col_spacings(GTK_TABLE(pTable), 4);
152 ModuleSettingsData *pNewData = new ModuleSettingsData;
154 pNewData->pNext = pPrivate->pFirst;
155 pNewData->pWidget = pControl;
156 pNewData->iParameter = pSettings[i].iParameter;
157 pNewData->iDivisor = pSettings[i].iDivisor;
159 pPrivate->pFirst = pNewData;
163 gtk_widget_show_all(pFrame);
165 return GTK_WIDGET(pDasherControl);
168 /* private members */
169 static ModuleSettingsData *
170 module_settings_window_get_settings_data(ModuleSettingsWindow *pSelf, GtkWidget *pWidget) {
171 ModuleSettingsWindowPrivate *pPrivate = MODULE_SETTINGS_WINDOW_GET_PRIVATE(pSelf);
173 ModuleSettingsData *pCurrentData = pPrivate->pFirst;
175 while(pCurrentData != NULL) {
176 if(pCurrentData->pWidget == pWidget)
177 return pCurrentData;
179 pCurrentData = pCurrentData->pNext;
182 return NULL;
185 static void
186 module_settings_window_handle_bool_changed(ModuleSettingsWindow *pSelf, GtkToggleButton *pToggleButton) {
187 ModuleSettingsData *pData = module_settings_window_get_settings_data(pSelf, GTK_WIDGET(pToggleButton));
189 if(!pData)
190 return;
192 ModuleSettingsWindowPrivate *pPrivate = MODULE_SETTINGS_WINDOW_GET_PRIVATE(pSelf);
194 gboolean bNewValue = gtk_toggle_button_get_active(pToggleButton);
195 dasher_app_settings_set_bool(pPrivate->pAppSettings, pData->iParameter, bNewValue);
198 static void
199 module_settings_window_handle_long_changed(ModuleSettingsWindow *pSelf, GtkRange *pRange) {
200 ModuleSettingsData *pData = module_settings_window_get_settings_data(pSelf, GTK_WIDGET(pRange));
202 if(!pData)
203 return;
205 ModuleSettingsWindowPrivate *pPrivate = MODULE_SETTINGS_WINDOW_GET_PRIVATE(pSelf);
207 gint iNewValue = (gint)(gtk_range_get_value(pRange) * pData->iDivisor);
208 dasher_app_settings_set_long(pPrivate->pAppSettings, pData->iParameter, iNewValue);
211 static void
212 module_settings_window_handle_longspin_changed(ModuleSettingsWindow *pSelf, GtkSpinButton *pSpinButton) {
213 ModuleSettingsData *pData = module_settings_window_get_settings_data(pSelf, GTK_WIDGET(pSpinButton));
215 if(!pData)
216 return;
218 ModuleSettingsWindowPrivate *pPrivate = MODULE_SETTINGS_WINDOW_GET_PRIVATE(pSelf);
220 gint iNewValue = (gint)(gtk_spin_button_get_value(pSpinButton) * pData->iDivisor);
221 dasher_app_settings_set_long(pPrivate->pAppSettings, pData->iParameter, iNewValue);
224 static void
225 module_settings_window_handle_string_changed(ModuleSettingsWindow *pSelf, GtkEditable *pEditable) {
226 ModuleSettingsData *pData = module_settings_window_get_settings_data(pSelf, GTK_WIDGET(pEditable));
228 if(!pData)
229 return;
231 ModuleSettingsWindowPrivate *pPrivate = MODULE_SETTINGS_WINDOW_GET_PRIVATE(pSelf);
233 const gchar *szNewValue = gtk_editable_get_chars(pEditable, 0, -1);
234 dasher_app_settings_set_string(pPrivate->pAppSettings, pData->iParameter, szNewValue);
237 static gboolean
238 module_settings_window_handle_close(ModuleSettingsWindow *pSelf) {
239 gtk_widget_hide(GTK_WIDGET(&(pSelf->window)));
240 return TRUE;
243 // Callbacks
245 extern "C" void
246 handle_bool_changed(GtkToggleButton *pToggleButton, gpointer pUserData) {
247 module_settings_window_handle_bool_changed(MODULE_SETTINGS_WINDOW(pUserData), pToggleButton);
250 extern "C" void
251 handle_long_changed(GtkRange *pRange, gpointer pUserData) {
252 module_settings_window_handle_long_changed(MODULE_SETTINGS_WINDOW(pUserData), pRange);
255 extern "C" void
256 handle_longspin_changed(GtkSpinButton *pSpinButton, gpointer pUserData) {
257 module_settings_window_handle_longspin_changed(MODULE_SETTINGS_WINDOW(pUserData), pSpinButton);
260 extern "C" void
261 handle_string_changed(GtkEditable *pEditable, gpointer pUserData) {
262 module_settings_window_handle_string_changed(MODULE_SETTINGS_WINDOW(pUserData), pEditable);
265 extern "C" gboolean
266 handle_close(GtkWidget *pWidget, gpointer pUserData) {
267 return module_settings_window_handle_close(MODULE_SETTINGS_WINDOW(pUserData));
270 extern "C" gboolean
271 handle_close_event(GtkWidget *pWidget, GdkEvent *pEvent, gpointer pUserData) {
272 return module_settings_window_handle_close(MODULE_SETTINGS_WINDOW(pUserData));