]> git.street.me.uk Git - andy/viking.git/blame - src/background.c
Interpolating DEM data.
[andy/viking.git] / src / background.c
CommitLineData
50a14534
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <gtk/gtk.h>
23#include "vikstatus.h"
24#include "background.h"
25#include "gtkcellrendererprogress.h"
26
27static GtkWidget *bgwindow = NULL;
28static GtkWidget *bgtreeview = NULL;
29static GtkListStore *bgstore = NULL;
30
31static GSList *statusbars_to_update = NULL;
32
33static gint bgitemcount = 0;
34
35enum
36{
37 TITLE_COLUMN,
38 PROGRESS_COLUMN,
39 DATA_COLUMN,
40 N_COLUMNS,
41};
42
43void a_background_update_status ( VikStatusbar *vs, gchar *str )
44{
45 gdk_threads_enter ();
46 vik_statusbar_set_message ( vs, 1, str );
47 gdk_threads_leave ();
48}
49
50static void background_thread_update ()
51{
52 static gchar buf[20];
53 g_snprintf(buf, sizeof(buf), "%d items", bgitemcount);
54 g_slist_foreach ( statusbars_to_update, (GFunc) a_background_update_status, buf );
55}
56
57void a_background_thread_progress ( gpointer callbackdata, gdouble fraction )
58{
59 gpointer *args = (gpointer *) callbackdata;
60 a_background_testcancel ( callbackdata );
61 gdk_threads_enter();
62 gtk_list_store_set( GTK_LIST_STORE(bgstore), (GtkTreeIter *) args[5], PROGRESS_COLUMN, fraction, -1 );
63 gdk_threads_leave();
64
65 args[6] = GINT_TO_POINTER(GPOINTER_TO_INT(args[6])-1);
66 bgitemcount--;
67 background_thread_update();
68}
69
70static void thread_die ( gpointer args[6] )
71{
72 vik_thr_free_func userdata_free_func = args[3];
73
74 userdata_free_func ( args[2] );
75
76 if ( GPOINTER_TO_INT(args[6]) )
77 {
78 bgitemcount -= GPOINTER_TO_INT(args[6]);
79 background_thread_update ();
80 }
81
82 g_free ( args[5] ); /* free iter */
83 g_free ( args );
84
85 g_thread_exit ( NULL );
86}
87
88void a_background_testcancel ( gpointer callbackdata )
89{
90 gpointer *args = (gpointer *) callbackdata;
91 if ( args[0] )
92 {
93 vik_thr_free_func cleanup = args[4];
94 if ( cleanup )
95 cleanup ( args[2] );
96 thread_die( args );
97 }
98}
99
100void thread_helper ( gpointer args[6] )
101{
102 /* unpack args */
103 vik_thr_func func = args[1];
104 gpointer userdata = args[2];
105
106 func ( userdata, args );
107
50a14534
EB
108 gdk_threads_enter();
109 if ( ! args[0] )
110 gtk_list_store_remove ( bgstore, (GtkTreeIter *) args[5] );
111 gdk_threads_leave();
112
113 thread_die ( args );
114}
115
116void a_background_thread ( GtkWindow *parent, const gchar *message, vik_thr_func func, gpointer userdata, vik_thr_free_func userdata_free_func, vik_thr_free_func userdata_cancel_cleanup_func, gint number_items )
117{
118 GtkTreeIter *piter = g_malloc ( sizeof ( GtkTreeIter ) );
119 gpointer *args = g_malloc ( sizeof(gpointer) * 7 );
120
121 args[0] = GINT_TO_POINTER(0);
122 args[1] = func;
123 args[2] = userdata;
124 args[3] = userdata_free_func;
125 args[4] = userdata_cancel_cleanup_func;
126 args[5] = piter;
127 args[6] = GINT_TO_POINTER(number_items);
128
129 bgitemcount += number_items;
130
131 gtk_list_store_append ( bgstore, piter );
132 gtk_list_store_set ( bgstore, piter, TITLE_COLUMN, message, PROGRESS_COLUMN, 0.0, DATA_COLUMN, args, -1 );
133
134 /* run the thread in the background */
135 g_thread_create( (GThreadFunc) thread_helper, args, FALSE, NULL );
136}
137
138void a_background_show_window ()
139{
140 gtk_widget_show_all ( bgwindow );
141}
142
143static void cancel_job_with_iter ( GtkTreeIter *piter )
144{
145 gpointer *args;
146 gtk_tree_model_get( GTK_TREE_MODEL(bgstore), piter, DATA_COLUMN, &args, -1 );
147
148 /* we know args still exists because it is free _after_ the list item is destroyed */
149 /* need MUTEX ? */
150 args[0] = GINT_TO_POINTER(1); /* set killswitch */
151
50a14534
EB
152 gtk_list_store_remove ( bgstore, piter );
153}
154
155static void bgwindow_response (GtkDialog *dialog, gint arg1 )
156{
439f34ec
AF
157 /* note this function is a signal handler called back from the GTK main loop,
158 * so GDK is already locked. We need to release the lock before calling
159 * thread-safe routines
160 */
161 if ( arg1 == 1 ) /* cancel */
162 {
163 GtkTreeIter iter;
164 if ( gtk_tree_selection_get_selected ( gtk_tree_view_get_selection ( GTK_TREE_VIEW(bgtreeview) ), NULL, &iter ) )
165 cancel_job_with_iter ( &iter );
166 gdk_threads_leave();
167 background_thread_update();
168 gdk_threads_enter();
169 }
50a14534 170 else if ( arg1 == 2 ) /* clear */
439f34ec
AF
171 {
172 GtkTreeIter iter;
173 while ( gtk_tree_model_get_iter_first ( GTK_TREE_MODEL(bgstore), &iter ) )
174 cancel_job_with_iter ( &iter );
439f34ec
AF
175 gdk_threads_leave();
176 background_thread_update();
177 gdk_threads_enter();
178 }
50a14534
EB
179 else /* OK */
180 gtk_widget_hide ( bgwindow );
181}
182
183void a_background_init()
184{
185 GtkCellRenderer *renderer;
186 GtkTreeViewColumn *column;
187 GtkWidget *scrolled_window;
188
189 /* store & treeview */
190 bgstore = gtk_list_store_new ( N_COLUMNS, G_TYPE_STRING, G_TYPE_DOUBLE, G_TYPE_POINTER );
191 bgtreeview = gtk_tree_view_new_with_model ( GTK_TREE_MODEL(bgstore) );
192 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (bgtreeview), TRUE);
193 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (bgtreeview)),
194 GTK_SELECTION_SINGLE);
195
196 /* add columns */
197 renderer = gtk_cell_renderer_text_new ();
198 column = gtk_tree_view_column_new_with_attributes ( "Job", renderer, "text", TITLE_COLUMN, NULL );
199 gtk_tree_view_append_column ( GTK_TREE_VIEW(bgtreeview), column );
200
201 renderer = gtk_cell_renderer_progress_new ();
202 column = gtk_tree_view_column_new_with_attributes ( "Progress", renderer, "percentage", PROGRESS_COLUMN, NULL );
203 gtk_tree_view_append_column ( GTK_TREE_VIEW(bgtreeview), column );
204
205 /* setup window */
206 scrolled_window = gtk_scrolled_window_new ( NULL, NULL );
207 gtk_container_add ( GTK_CONTAINER(scrolled_window), bgtreeview );
208 gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
209
210 bgwindow = gtk_dialog_new_with_buttons ( "", NULL, 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_DELETE, 1, GTK_STOCK_CLEAR, 2, NULL );
211 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(bgwindow)->vbox), scrolled_window, TRUE, TRUE, 0 );
212 gtk_window_set_default_size ( GTK_WINDOW(bgwindow), 400, 400 );
213 gtk_window_set_title ( GTK_WINDOW(bgwindow), "Viking Background Jobs" );
214 /* don't destroy win */
215 g_signal_connect ( G_OBJECT(bgwindow), "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL );
216
217 g_signal_connect ( G_OBJECT(bgwindow), "response", G_CALLBACK(bgwindow_response), 0 );
218
219}
220
221void a_background_add_status(VikStatusbar *vs)
222{
223 statusbars_to_update = g_slist_prepend(statusbars_to_update,vs);
224}
225
226void a_background_remove_status(VikStatusbar *vs)
227{
228 statusbars_to_update = g_slist_remove(statusbars_to_update,vs);
229}
230