Nicer Message Dialogs - Alex
[terminatorX.git] / terminatorX / src / tX_loaddlg.cc
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999-2002  Alexander K├Ânig
4  
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9  
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14  
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  
19     File: tX_loaddlg.cc
20  
21     Description: Displays the progress indicator dialog for file
22                  loading.
23                  
24 */ 
25 #include <gtk/gtk.h>
26 #include "tX_loaddlg.h"
27 #include <math.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include "tX_mastergui.h"
31
32 GtkWidget *ld_loaddlg=(GtkWidget *) NULL;
33 GtkWidget *ld_single_l=(GtkWidget *)NULL;
34 GtkWidget *ld_single_p=(GtkWidget *)NULL;
35 GtkWidget *ld_multi_l=(GtkWidget *)NULL;
36 GtkWidget *ld_multi_p=(GtkWidget *)NULL;
37 GtkWindow *ld_window=(GtkWindow *)NULL;
38
39 int ld_mode;
40 int ld_count;
41 int ld_current;
42
43 gfloat ld_old_prog;
44
45 #define WID_DYN TRUE, TRUE, 0
46 #define WID_FIX FALSE, FALSE, 0
47 #define add_widget_dyn(wid); gtk_box_pack_start(GTK_BOX(vbox), wid, WID_DYN);\
48         gtk_widget_show(wid);
49         
50 #define add_widget_fix(wid); gtk_box_pack_start(GTK_BOX(vbox), wid, WID_FIX);\
51         gtk_widget_show(wid);
52
53 #define gtk_flush(); while (gtk_events_pending()) gtk_main_iteration(); gdk_flush();
54
55
56 int ld_create_loaddlg(int mode, int count)
57 {
58         GtkWidget *vbox;
59         GtkWidget *actionarea;
60         GtkWidget *dummy;
61         
62         if (ld_loaddlg) return(1);
63         
64         ld_mode=mode;
65         ld_count=count;
66
67 #ifdef USE_GTK2
68         ld_loaddlg=gtk_dialog_new_with_buttons("terminatorX - loading",
69                 GTK_WINDOW(main_window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_NONE, NULL);
70 #else   
71         ld_loaddlg=gtk_dialog_new();    
72 #endif  
73         ld_window=&(GTK_DIALOG(ld_loaddlg)->window);
74 #ifndef         USE_GTK2
75         gtk_window_set_title(ld_window, "terminatorX - loading");       
76 #endif  
77         gtk_container_set_border_width(GTK_CONTAINER(ld_window), 5);
78         
79         vbox=GTK_WIDGET(GTK_DIALOG(ld_loaddlg)->vbox);
80         gtk_box_set_spacing(GTK_BOX(vbox), 5);
81
82         actionarea=GTK_WIDGET(GTK_DIALOG(ld_loaddlg)->action_area);
83         gtk_box_set_spacing(GTK_BOX(actionarea), 5);
84         
85         if (mode==TX_LOADDLG_MODE_MULTI)
86         {
87                 ld_multi_l=gtk_label_new("Loading Set");
88                 gtk_misc_set_alignment(GTK_MISC(ld_multi_l), 0.5, 0.5);
89                 add_widget_fix(ld_multi_l);
90                 
91                 ld_multi_p=gtk_progress_bar_new();
92                 add_widget_fix(ld_multi_p);
93                 
94                 dummy=gtk_hseparator_new();
95                 add_widget_fix(dummy);
96                 
97                 ld_current=0;
98         }
99         
100         ld_single_l=gtk_label_new("Loading File");
101         add_widget_fix(ld_single_l);
102                 
103         ld_single_p=gtk_progress_bar_new();
104         add_widget_fix(ld_single_p);
105
106 #ifndef USE_GTK2        
107         dummy=gtk_button_new_with_label("Cancel");
108         gtk_box_pack_start(GTK_BOX(actionarea), dummy, WID_DYN);
109         gtk_widget_show(dummy);
110 #endif  
111         gtk_window_set_modal(ld_window, TRUE);
112         gtk_window_set_default_size(ld_window, 400, 100);
113 #ifndef USE_GTK2
114         gtk_window_set_position(ld_window, GTK_WIN_POS_CENTER_ALWAYS);
115 #endif  
116         gtk_widget_realize(ld_loaddlg);
117         gdk_window_set_decorations(gtk_widget_get_parent_window(vbox),(GdkWMDecoration) 0);
118         gtk_widget_show(ld_loaddlg);
119
120         gtk_flush();
121                 
122         return 0;
123 }
124
125 char *strip_path(char *name)
126 {
127         char *tmp;
128         
129         tmp=strrchr(name, (int) '/');
130         
131         if (tmp)
132         {
133                 if (strlen(tmp)>1)
134                 {
135                         tmp++;
136                 }
137         }
138         else tmp=name;
139                 
140         return(tmp);
141 }
142
143 void ld_set_setname(char *name)
144 {
145         char *setname;
146         char buffer[1024];
147         
148         setname=strip_path(name);
149         sprintf(buffer, "Loading tX-set [%s]", setname);
150         gtk_label_set_text(GTK_LABEL(ld_multi_l), buffer);
151         gtk_flush();
152 }
153
154 void ld_set_filename(char *name)
155 {
156         char *filename;
157         char buffer[1024];
158         gfloat setprog;
159         
160         ld_current++;
161         ld_old_prog=-1;
162         filename=strip_path(name);
163         if (ld_mode==TX_LOADDLG_MODE_MULTI)
164         {
165                 sprintf(buffer, "Loading file No. %i of %i [%s]", ld_current, ld_count, filename);
166         }
167         else
168         {
169                 sprintf(buffer, "Loading file [%s]", filename); 
170         }
171         gtk_label_set_text(GTK_LABEL(ld_single_l), buffer);
172         
173         if (ld_mode==TX_LOADDLG_MODE_MULTI)
174         {
175                 setprog=(((float) ld_current)/((float) ld_count));
176                 gtk_progress_bar_update(GTK_PROGRESS_BAR(ld_multi_p), setprog);
177                 gtk_flush();            
178         }
179         gtk_flush();
180 }
181
182 void ld_set_progress(gfloat progress)
183 {
184         progress=floor(progress*200.0)/200.0;
185         if (progress>1.0) progress=1.0;
186         //printf("%f\n", progress);
187         
188         if (progress!=ld_old_prog)
189         {
190                 gtk_progress_bar_update(GTK_PROGRESS_BAR(ld_single_p), progress);
191                 gtk_flush();
192         }
193         
194         ld_old_prog=progress;
195 }
196
197 void ld_destroy()
198 {
199         if (ld_loaddlg)
200         {
201                 gtk_widget_hide(ld_loaddlg);
202                 gtk_widget_destroy(ld_loaddlg);
203         }
204         
205         ld_loaddlg=NULL;
206         mg_update_status();
207 }