]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */ | |
3 | /* | |
4 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
5 | * | |
6 | * Copyright (C) 2013, Guilhem Bonnefille <guilhem.bonnefille@gmail.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | */ | |
23 | #include <glib/gi18n.h> | |
24 | ||
25 | #include "babel.h" | |
26 | #include "babel_ui.h" | |
27 | ||
28 | ||
29 | static void babel_ui_selector_add_entry_cb ( gpointer data, gpointer user_data ) | |
30 | { | |
31 | BabelFile *file = (BabelFile*)data; | |
32 | GtkWidget *combo = GTK_WIDGET(user_data); | |
33 | ||
34 | GList *formats = g_object_get_data ( G_OBJECT(combo), "formats" ); | |
35 | formats = g_list_append ( formats, file ); | |
36 | g_object_set_data ( G_OBJECT(combo), "formats", formats ); | |
37 | ||
38 | const gchar *label = file->label; | |
39 | vik_combo_box_text_append ( combo, label ); | |
40 | } | |
41 | ||
42 | void a_babel_ui_type_selector_dialog_sensitivity_cb ( GtkComboBox *widget, gpointer user_data ) | |
43 | { | |
44 | /* user_data is the GtkDialog */ | |
45 | GtkDialog *dialog = GTK_DIALOG(user_data); | |
46 | ||
47 | /* Retrieve the associated file format descriptor */ | |
48 | BabelFile *file = a_babel_ui_file_type_selector_get ( GTK_WIDGET(widget) ); | |
49 | ||
50 | if ( file ) | |
51 | /* Not NULL => valid selection */ | |
52 | gtk_dialog_set_response_sensitive ( dialog, GTK_RESPONSE_ACCEPT, TRUE ); | |
53 | else | |
54 | /* NULL => invalid selection */ | |
55 | gtk_dialog_set_response_sensitive ( dialog, GTK_RESPONSE_ACCEPT, FALSE ); | |
56 | } | |
57 | ||
58 | /** | |
59 | * a_babel_ui_file_type_selector_new: | |
60 | * @mode: the mode to filter the file types | |
61 | * | |
62 | * Create a file type selector. | |
63 | * | |
64 | * This widget relies on a combo box listing labels of file formats. | |
65 | * We store in the "data" of the GtkWidget a list with the BabelFile | |
66 | * entries, in order to retrieve the selected file format. | |
67 | * | |
68 | * Returns: a GtkWidget | |
69 | */ | |
70 | GtkWidget *a_babel_ui_file_type_selector_new ( BabelMode mode ) | |
71 | { | |
72 | GList *formats = NULL; | |
73 | /* Create the combo */ | |
74 | GtkWidget * combo = vik_combo_box_text_new (); | |
75 | ||
76 | /* Add a first label to invite user to select a file format */ | |
77 | /* We store a NULL pointer to distinguish this entry */ | |
78 | formats = g_list_append ( formats, NULL ); | |
79 | vik_combo_box_text_append ( combo, _("Select a file format") ); | |
80 | ||
81 | /* Prepare space for file format list */ | |
82 | g_object_set_data ( G_OBJECT(combo), "formats", formats ); | |
83 | ||
84 | /* Add all known and compatible file formats */ | |
85 | if ( mode.tracksRead && mode.routesRead && mode.waypointsRead && | |
86 | !mode.tracksWrite && !mode.routesWrite && !mode.waypointsWrite ) | |
87 | a_babel_foreach_file_read_any ( babel_ui_selector_add_entry_cb, combo ); | |
88 | else | |
89 | a_babel_foreach_file_with_mode ( mode, babel_ui_selector_add_entry_cb, combo ); | |
90 | ||
91 | /* Initialize the selection with the really first entry */ | |
92 | gtk_combo_box_set_active ( GTK_COMBO_BOX(combo), 0 ); | |
93 | ||
94 | return combo; | |
95 | } | |
96 | ||
97 | /** | |
98 | * a_babel_ui_file_type_selector_destroy: | |
99 | * @selector: the selector to destroy | |
100 | * | |
101 | * Destroy the selector and any related data. | |
102 | */ | |
103 | void a_babel_ui_file_type_selector_destroy ( GtkWidget *selector ) | |
104 | { | |
105 | GList *formats = g_object_get_data ( G_OBJECT(selector), "formats" ); | |
106 | g_free ( formats ); | |
107 | } | |
108 | ||
109 | /** | |
110 | * a_babel_ui_file_type_selector_get: | |
111 | * @selector: the selector | |
112 | * | |
113 | * Retrieve the selected file type. | |
114 | * | |
115 | * Returns: the selected BabelFile or NULL | |
116 | */ | |
117 | BabelFile *a_babel_ui_file_type_selector_get ( GtkWidget *selector ) | |
118 | { | |
119 | gint active = gtk_combo_box_get_active ( GTK_COMBO_BOX(selector) ); | |
120 | if (active >= 0) { | |
121 | GList *formats = g_object_get_data ( G_OBJECT(selector), "formats" ); | |
122 | return (BabelFile*)g_list_nth_data ( formats, active ); | |
123 | } else { | |
124 | return NULL; | |
125 | } | |
126 | } | |
127 | ||
128 | /** | |
129 | * a_babel_ui_modes_new: | |
130 | * @tracks: | |
131 | * @routes: | |
132 | * @waypoints: | |
133 | * | |
134 | * Creates a selector for babel modes. | |
135 | * This selector is based on 3 checkboxes. | |
136 | * | |
137 | * Returns: a GtkWidget packing all checkboxes. | |
138 | */ | |
139 | GtkWidget *a_babel_ui_modes_new ( gboolean tracks, gboolean routes, gboolean waypoints ) | |
140 | { | |
141 | GtkWidget *hbox = gtk_hbox_new( FALSE, 0 ); | |
142 | GtkWidget *button = NULL; | |
143 | ||
144 | button = gtk_check_button_new_with_label ( _("Tracks") ); | |
145 | gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(button), tracks ); | |
146 | gtk_box_pack_start ( GTK_BOX(hbox), button, TRUE, TRUE, 0 ); | |
147 | gtk_widget_show ( button ); | |
148 | ||
149 | button = gtk_check_button_new_with_label ( _("Routes") ); | |
150 | gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(button), routes ); | |
151 | gtk_box_pack_start ( GTK_BOX(hbox), button, TRUE, TRUE, 0 ); | |
152 | gtk_widget_show ( button ); | |
153 | ||
154 | button = gtk_check_button_new_with_label ( _("Waypoints") ); | |
155 | gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(button), waypoints ); | |
156 | gtk_box_pack_start ( GTK_BOX(hbox), button, TRUE, TRUE, 0 ); | |
157 | gtk_widget_show ( button ); | |
158 | ||
159 | return hbox; | |
160 | } | |
161 | ||
162 | /** | |
163 | * a_babel_ui_modes_get: | |
164 | * @container: | |
165 | * @tracks: return value | |
166 | * @routes: return value | |
167 | * @waypoints: return value | |
168 | * | |
169 | * Retrieve state of checkboxes. | |
170 | */ | |
171 | void a_babel_ui_modes_get ( GtkWidget *container, gboolean *tracks, gboolean *routes, gboolean *waypoints ) | |
172 | { | |
173 | GList* children = gtk_container_get_children ( GTK_CONTAINER(container) ); | |
174 | GtkWidget *child = NULL; | |
175 | ||
176 | child = g_list_nth_data ( children, 0 ); | |
177 | *tracks = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(child) ); | |
178 | ||
179 | child = g_list_nth_data ( children, 1 ); | |
180 | *routes = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(child) ); | |
181 | ||
182 | child = g_list_nth_data ( children, 2 ); | |
183 | *waypoints = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(child) ); | |
184 | } | |
185 |