1 /*
2  * TilEm II
3  *
4  * Copyright (c) 2010-2011 Thibault Duponchelle
5  * Copyright (c) 2011 Benjamin Moody
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, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * 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, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24 
25 #include <stdio.h>
26 #include <gtk/gtk.h>
27 #include <ticalcs.h>
28 #include <tilem.h>
29 
30 #include "gui.h"
31 
32 /* Update the progress_bar */
progress_bar_update_activity(TilemLinkProgress * linkpb)33 static void progress_bar_update_activity(TilemLinkProgress *linkpb)
34 {
35 	char *s;
36 	gdouble f;
37 
38 	if (!linkpb->window || !linkpb->emu->pbar_title)
39 		return;
40 
41 	gtk_window_set_title(GTK_WINDOW(linkpb->window), linkpb->emu->pbar_title);
42 
43 	s = g_strdup_printf("<big><b>%s</b></big>", linkpb->emu->pbar_title);
44 	gtk_label_set_markup(linkpb->title_lbl, s);
45 	g_free(s);
46 
47 	if (linkpb->emu->paused && linkpb->emu->pbar_status) {
48 		s = g_strconcat(linkpb->emu->pbar_status, " (paused)", NULL);
49 		gtk_label_set_text(linkpb->status_lbl, s);
50 		g_free(s);
51 	}
52 	else if (linkpb->emu->paused)
53 		gtk_label_set_text(linkpb->status_lbl, "(paused)");
54 	else
55 		gtk_label_set_text(linkpb->status_lbl, linkpb->emu->pbar_status);
56 
57 	if (linkpb->emu->pbar_progress < 0.0) {
58 		gtk_progress_bar_pulse(linkpb->progress_bar);
59 	}
60 	else {
61 		f = CLAMP(linkpb->emu->pbar_progress, 0.0, 1.0);
62 		gtk_progress_bar_set_fraction(linkpb->progress_bar, f);
63 	}
64 }
65 
66 /* Callback to destroy the progress bar */
destroy_progress(G_GNUC_UNUSED GtkDialog * dlg,G_GNUC_UNUSED gint response,gpointer data)67 static void destroy_progress(G_GNUC_UNUSED GtkDialog *dlg,
68                              G_GNUC_UNUSED gint response,
69                              gpointer data)
70 {
71 	TilemLinkProgress* linkpb = data;
72 	tilem_calc_emulator_cancel_tasks(linkpb->emu);
73 	gtk_widget_destroy(linkpb->window);
74 	linkpb->window = NULL;
75 	linkpb->progress_bar = NULL;
76 	linkpb->title_lbl = NULL;
77 	linkpb->status_lbl = NULL;
78 }
79 
80 /* Create the progress bar window */
progress_bar_init(TilemLinkProgress * linkpb)81 static void progress_bar_init(TilemLinkProgress* linkpb)
82 {
83 	GtkWidget *pw, *parent, *vbox, *vbox2, *lbl, *pb;
84 
85 	if (linkpb->emu->ewin)
86 		parent = linkpb->emu->ewin->window;
87 	else
88 		parent = NULL;
89 
90 	pw = gtk_dialog_new_with_buttons("", GTK_WINDOW(parent), 0,
91 	                                 GTK_STOCK_CANCEL,
92 	                                 GTK_RESPONSE_CANCEL,
93 	                                 NULL);
94 	linkpb->window = pw;
95 
96 	gtk_window_set_resizable(GTK_WINDOW(pw), FALSE);
97 
98 	vbox = gtk_dialog_get_content_area(GTK_DIALOG(pw));
99 
100 	vbox2 = gtk_vbox_new(FALSE, 6);
101 	gtk_container_set_border_width(GTK_CONTAINER(vbox2), 6);
102 
103 	lbl = gtk_label_new(NULL);
104 	gtk_label_set_width_chars(GTK_LABEL(lbl), 35);
105 	gtk_misc_set_alignment(GTK_MISC(lbl), 0.0, 0.5);
106 	gtk_box_pack_start(GTK_BOX(vbox2), lbl, FALSE, FALSE, 0);
107 	linkpb->title_lbl = GTK_LABEL(lbl);
108 
109 	pb = gtk_progress_bar_new();
110 	gtk_box_pack_start(GTK_BOX(vbox2), pb, FALSE, FALSE, 0);
111 	linkpb->progress_bar = GTK_PROGRESS_BAR(pb);
112 
113 	lbl = gtk_label_new(NULL);
114 	gtk_misc_set_alignment(GTK_MISC(lbl), 0.0, 0.5);
115 	gtk_box_pack_start(GTK_BOX(vbox2), lbl, FALSE, FALSE, 0);
116 	linkpb->status_lbl = GTK_LABEL(lbl);
117 
118 	gtk_box_pack_start(GTK_BOX(vbox), vbox2, FALSE, FALSE, 6);
119 
120 	g_signal_connect(pw, "response", G_CALLBACK(destroy_progress), linkpb);
121 
122 	progress_bar_update_activity(linkpb);
123 
124 	gtk_widget_show_all(pw);
125 }
126 
127 /* Create or update the progress bar */
progress_bar_update(TilemCalcEmulator * emu)128 void progress_bar_update(TilemCalcEmulator* emu)
129 {
130 	g_return_if_fail(emu != NULL);
131 
132 	g_mutex_lock(emu->pbar_mutex);
133 
134 	if (!emu->linkpb) {
135 		emu->linkpb = g_slice_new0(TilemLinkProgress);
136 		emu->linkpb->emu = emu;
137 	}
138 
139 	if (!emu->linkpb->window && emu->pbar_title) {
140 		progress_bar_init(emu->linkpb);
141 	}
142 	else if (emu->linkpb->window && !emu->pbar_title) {
143 		gtk_widget_destroy(emu->linkpb->window);
144 		emu->linkpb->window = NULL;
145 		emu->linkpb->title_lbl = NULL;
146 		emu->linkpb->status_lbl = NULL;
147 		emu->linkpb->progress_bar = NULL;
148 	}
149 	else {
150 		progress_bar_update_activity(emu->linkpb);
151 	}
152 
153 	emu->pbar_update_pending = FALSE;
154 	g_mutex_unlock(emu->pbar_mutex);
155 }
156 
157