libvorbis for ogg - Alex
[terminatorX.git] / 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 //      if(needinit) ld_init();
65         
66         ld_mode=mode;
67         ld_count=count;
68         
69         ld_loaddlg=gtk_dialog_new();
70         ld_window=&(GTK_DIALOG(ld_loaddlg)->window);
71         gtk_window_set_title(ld_window, "terminatorX - Loading");
72         gtk_container_set_border_width(GTK_CONTAINER(ld_window), 5);
73         
74         vbox=GTK_WIDGET(GTK_DIALOG(ld_loaddlg)->vbox);
75         gtk_box_set_spacing(GTK_BOX(vbox), 5);
76
77         actionarea=GTK_WIDGET(GTK_DIALOG(ld_loaddlg)->action_area);
78         gtk_box_set_spacing(GTK_BOX(actionarea), 5);
79         
80         if (mode==TX_LOADDLG_MODE_MULTI)
81         {
82                 ld_multi_l=gtk_label_new("Loading Set");
83                 gtk_misc_set_alignment(GTK_MISC(ld_multi_l), 0.5, 0.5);
84                 add_widget_fix(ld_multi_l);
85                 
86                 ld_multi_p=gtk_progress_bar_new();
87                 add_widget_fix(ld_multi_p);
88                 
89                 dummy=gtk_hseparator_new();
90                 add_widget_fix(dummy);
91                 
92                 ld_current=0;
93         }
94         
95         ld_single_l=gtk_label_new("Loading File");
96         add_widget_fix(ld_single_l);
97                 
98         ld_single_p=gtk_progress_bar_new();
99         add_widget_fix(ld_single_p);
100         
101         dummy=gtk_button_new_with_label("Cancel");
102         gtk_box_pack_start(GTK_BOX(actionarea), dummy, WID_DYN);
103         gtk_widget_show(dummy);
104
105         dummy=gtk_widget_get_toplevel(dummy);
106
107
108         gtk_window_set_modal(ld_window, TRUE);
109         gtk_window_set_default_size(ld_window, 400, 100);
110         gtk_window_set_position(ld_window, GTK_WIN_POS_CENTER_ALWAYS);
111         gtk_widget_realize(ld_loaddlg);
112         gdk_window_set_decorations(gtk_widget_get_parent_window(vbox),(GdkWMDecoration) 0);
113
114 //      gtk_window_reposition(ld_window, gdk_screen_width()/2-200, gdk_screen_height()/2-50);
115         //gtk_window_reposition(ld_window, gdk_screen_width()/2-200, gdk_screen_height()/2-50);
116         gtk_widget_show(ld_loaddlg);
117
118         gtk_flush();
119                 
120         return(0);
121 }
122
123 char *strip_path(char *name)
124 {
125         char *tmp;
126         
127         tmp=strrchr(name, (int) '/');
128         
129         if (tmp)
130         {
131                 if (strlen(tmp)>1)
132                 {
133                         tmp++;
134                 }
135         }
136         else tmp=name;
137                 
138         return(tmp);
139 }
140
141 void ld_set_setname(char *name)
142 {
143         char *setname;
144         char buffer[1024];
145         
146         setname=strip_path(name);
147         sprintf(buffer, "Loading tX-set [%s]", setname);
148         gtk_label_set_text(GTK_LABEL(ld_multi_l), buffer);
149         gtk_flush();
150 }
151
152 void ld_set_filename(char *name)
153 {
154         char *filename;
155         char buffer[1024];
156         gfloat setprog;
157         
158         ld_current++;
159         ld_old_prog=-1;
160         filename=strip_path(name);
161         if (ld_mode==TX_LOADDLG_MODE_MULTI)
162         {
163                 sprintf(buffer, "Loading file No. %i of %i [%s]", ld_current, ld_count, filename);
164         }
165         else
166         {
167                 sprintf(buffer, "Loading file [%s]", filename); 
168         }
169         gtk_label_set_text(GTK_LABEL(ld_single_l), buffer);
170         
171         if (ld_mode==TX_LOADDLG_MODE_MULTI)
172         {
173                 setprog=(((float) ld_current)/((float) ld_count));
174                 gtk_progress_bar_update(GTK_PROGRESS_BAR(ld_multi_p), setprog);
175                 gtk_flush();            
176         }
177         gtk_flush();
178 }
179
180 void ld_set_progress(gfloat progress)
181 {
182         progress=floor(progress*1000.0)/1000.0;
183         
184         //printf("%f\n", progress);
185         
186         if (progress!=ld_old_prog)
187         {
188                 gtk_progress_bar_update(GTK_PROGRESS_BAR(ld_single_p), progress);
189                 gtk_flush();
190         }
191         
192         ld_old_prog=progress;
193 }
194
195 void ld_destroy()
196 {
197         if (ld_loaddlg)
198         {
199                 gtk_widget_hide(ld_loaddlg);
200                 gtk_widget_destroy(ld_loaddlg);
201         }
202         
203         ld_loaddlg=NULL;
204         mg_update_status();
205 }