0ea0e26d89f7aec038d54e0ef26526fd608a9c17
[terminatorX.git] / src / tX_mastergui.cc
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999-2003  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_mastergui.cc
20  
21     Description: This implements the main (aka master) gtk+ GUI of terminatorX
22                  It serves as a container for the vtt-guis.
23 */    
24
25 #include <gtk/gtk.h>
26 #include <gdk/gdkx.h>
27 #include <X11/Xlib.h>
28 #include <math.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <gdk/gdkkeysyms.h>
32 #include "version.h"
33 #include "tX_global.h"
34 #include "tX_engine.h"
35 #include "tX_vttgui.h"
36 #include "tX_vtt.h"
37 #include "tX_flash.h"
38 #include "tX_dialog.h"
39 #include "tX_loaddlg.h"
40 #include "tX_seqpar.h"
41 #include "tX_pbutton.h"
42 #include "tX_sequencer.h"
43 #include "tX_mastergui.h"
44 #include "tX_knobloader.h"
45 #include <sys/time.h>
46 #include <sys/wait.h>
47
48 #ifdef USE_SCHEDULER
49 #include <sys/resource.h>
50 #endif
51
52 #define TX_SET_ID_10 "terminatorX turntable set file - version 1.0 - data:"
53 #define TX_SET_ID_11 "terminatorX turntable set file - version 1.1 - data:"
54 #define TX_SET_ID_12 "terminatorX turntable set file - version 1.2 - data:"
55 #define TX_SET_ID_13 "terminatorX turntable set file - version 1.3 - data:"
56 #define TX_SET_ID_14 "terminatorX turntable set file - version 1.4 - data:"
57
58 int audioon=0;
59 int sequencer_ready=1;
60
61 GtkWidget *tt_parent;
62 GtkWidget *control_parent;
63 GtkWidget *audio_parent;
64 GtkWidget *main_window;
65 GtkWidget *wav_progress;
66 GtkWidget *grab_button;
67 GtkWidget *main_flash_l;
68 GtkWidget *main_flash_r;
69
70 GtkWidget *seq_rec_btn;
71 GtkWidget *seq_play_btn;
72 GtkWidget *seq_stop_btn;
73 GtkAdjustment *seq_adj;
74 GtkWidget *seq_slider;
75 GtkWidget *seq_entry;
76 GtkWidget *panel_bar;
77
78 int buttons_on_panel_bar=0;
79
80 int seq_adj_care=1;
81 int seq_stop_override=0;
82
83 GtkAdjustment *volume_adj;
84 GtkAdjustment *pitch_adj;
85
86 /* seq-pars */
87 tX_seqpar_master_volume sp_master_volume;
88 tX_seqpar_master_pitch sp_master_pitch;
89
90 GtkWidget *engine_btn;
91
92 GtkWidget *main_menubar;
93 GtkWidget *rec_menu_item;
94 GtkWidget *fullscreen_item;
95
96 int rec_dont_care=0;
97 gint update_tag;
98
99 #define connect_entry(wid, func, ptr); gtk_signal_connect(GTK_OBJECT(wid), "activate", (GtkSignalFunc) func, (void *) ptr);
100 #define connect_adj(wid, func, ptr); gtk_signal_connect(GTK_OBJECT(wid), "value_changed", (GtkSignalFunc) func, (void *) ptr);
101 #define connect_button(wid, func, ptr); gtk_signal_connect(GTK_OBJECT(wid), "clicked", (GtkSignalFunc) func, (void *) ptr);
102
103 Window xwindow;
104 #define WID_DYN TRUE, TRUE, 0
105 #define WID_FIX FALSE, FALSE, 0
106 extern void add_vtt(GtkWidget *ctrl, GtkWidget *audio, char *fn);
107 extern void destroy_gui(vtt_class *vtt);
108 extern void gui_show_frame(vtt_class *vtt, int show);
109
110 GdkWindow *save_dialog_win=NULL;
111 GdkWindow *load_dialog_win=NULL;
112 GtkWidget *save_dialog=NULL;
113 GtkWidget *load_dialog=NULL;
114
115 GdkWindow *rec_dialog_win=NULL;
116 GtkWidget *rec_dialog=NULL;
117
118 GtkWidget *no_of_vtts=NULL;
119 GtkWidget *used_mem=NULL;
120
121 int stop_update=0;
122 int update_delay;
123
124 vtt_class *old_focus=NULL;
125
126 int grab_status=0;
127 int last_grab_status=0;
128
129 GtkTooltips *gui_tooltips=NULL;
130
131 void gui_set_tooltip(GtkWidget *wid, char *tip)
132 {
133         gtk_tooltips_set_tip(gui_tooltips, wid, tip, NULL);
134 }
135
136 void turn_audio_off(void)
137 {
138         if (audioon) 
139         {
140                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 0);
141                 while (gtk_events_pending()) gtk_main_iteration();              
142         }
143 }
144
145
146 gint pos_update(gpointer data)
147 {
148         f_prec temp;
149
150         if (stop_update) 
151         {               
152                 cleanup_all_vtts();
153                 tX_seqpar :: update_all_graphics();
154                 if (old_focus) gui_show_frame(old_focus, 0);
155                 old_focus=NULL;
156                 gtk_tx_flash_clear(main_flash_l);
157                 gtk_tx_flash_clear(main_flash_r);
158                 gdk_flush();    
159                 update_tag=0;
160                 return(FALSE);
161         }
162         else
163         {
164                 update_all_vtts();
165                 
166                 /*left vu meter */
167                 temp=vtt_class::mix_max_l;
168                 vtt_class::mix_max_l=0;
169                 gtk_tx_flash_set_level(main_flash_l, temp);
170
171                 /*right vu meter */
172                 temp=vtt_class::mix_max_r;
173                 vtt_class::mix_max_r=0;
174                 gtk_tx_flash_set_level(main_flash_r, temp);
175                 
176                 if (vtt_class::focused_vtt!=old_focus)
177                 {
178                         if (old_focus) gui_show_frame(old_focus, 0);
179                         old_focus=vtt_class::focused_vtt;
180                         if (old_focus) gui_show_frame(old_focus, 1);                    
181                 }
182                 if (grab_status!=last_grab_status)
183                 {
184                         last_grab_status=grab_status;
185                         if (!grab_status) 
186                         {
187                                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(grab_button), 0);
188                         }
189                 }
190                 gdk_flush();    
191                 update_delay--;
192                 
193                 if (update_delay < 0)
194                 {
195                         seq_update();
196                         tX_seqpar :: update_all_graphics();
197                         update_delay=globals.update_delay;
198                 }
199                 return(TRUE);
200         }
201 }
202
203 void mg_update_status()
204 {
205         FILE *procfs;
206         pid_t mypid;
207         char filename[PATH_MAX];
208         char buffer[256];
209         int found=0;    
210         int mem;
211         
212         mypid=getpid();
213         sprintf(filename, "/proc/%i/status", mypid);
214         procfs=fopen(filename, "r");
215         if (procfs)
216         {
217                 while((!feof(procfs)) && !found)
218                 {
219                         fgets(buffer, 256, procfs);
220                         if (strncmp("VmSize:", buffer, 7)==0)
221                         {
222                                 found=1;
223                                 sscanf(buffer, "VmSize: %i kB", &mem);
224                                 sprintf(buffer, "%i", mem);
225                                 gtk_label_set(GTK_LABEL(used_mem), buffer);
226                         }
227                 }
228         }
229         fclose(procfs); 
230         
231         sprintf(buffer, "%i", vtt_class::vtt_amount);
232         gtk_label_set(GTK_LABEL(no_of_vtts), buffer);
233 }
234
235 GtkSignalFunc new_table(GtkWidget *, char *fn)
236 {
237         turn_audio_off();
238                 
239                 if (fn) 
240                 {
241                         ld_create_loaddlg(TX_LOADDLG_MODE_SINGLE, 1);
242                         ld_set_filename(fn);
243                 }
244                 
245                 add_vtt(control_parent, audio_parent, fn);                              
246                 
247                 if (fn) ld_destroy();           
248         mg_update_status();
249         return NULL;
250 }
251
252 GtkSignalFunc drop_new_table(GtkWidget *widget, GdkDragContext *context,
253                 gint x, gint y, GtkSelectionData *selection_data,
254                 guint info, guint time, vtt_class *vtt)
255 {
256         char filename[PATH_MAX];
257         char *fn;
258         
259         strncpy(filename, (char *) selection_data->data, (size_t) selection_data->length);
260         filename[selection_data->length]=0;
261
262         fn = strchr (filename, '\r');
263         *fn=0;  
264         
265         fn = strchr (filename, ':');
266         if (fn) fn++; else fn=(char *) selection_data->data;
267         
268         new_table(NULL, fn);
269
270         strcpy (filename, "dont segfault workaround ;)");
271         return NULL;
272 }
273
274 bool tx_mg_have_setname=false;
275 char tx_mg_current_setname[PATH_MAX]="";
276
277 GtkSignalFunc new_tables() {
278         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window), 
279                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
280                 "Are you sure you want to loose all turntables and events?");
281         
282         int res=gtk_dialog_run(GTK_DIALOG(dialog));
283         gtk_widget_destroy(dialog);
284                 
285         if (res!=GTK_RESPONSE_YES) {
286                 return NULL;
287         }
288
289         vtt_class::delete_all();
290         new_table(NULL, NULL);
291         sequencer.delete_all_events();
292         gtk_window_set_title(GTK_WINDOW(main_window), "terminatorX");
293
294         return NULL;
295 }
296
297 /* Loading saved setups */
298
299 GtkSignalFunc cancel_load_tables(GtkWidget *wid)
300 {
301         gtk_widget_destroy(load_dialog);
302         load_dialog=NULL;
303         load_dialog_win=NULL;
304         return(0);
305 }
306
307 void load_tt_part(char * buffer)
308 {
309         FILE *in;
310         char idbuff[256];
311         char wbuf[PATH_MAX];
312         xmlDocPtr doc;
313         
314         turn_audio_off();
315         
316         strcpy(globals.tables_filename, buffer);
317         
318         doc = xmlParseFile(buffer);
319         if (doc) {
320                 vtt_class::load_all(doc, buffer);
321                 xmlFreeDoc(doc);
322         } 
323         
324 #ifdef ENABLE_TX_LEGACY
325         else {  
326                 in=fopen(buffer, "r");  
327         
328                 if (in) {
329                         fread(idbuff, strlen(TX_SET_ID_10), 1, in);
330                         if (strncmp(idbuff, TX_SET_ID_10, strlen(TX_SET_ID_10))==0) {
331                                 if (vtt_class::load_all_10(in, buffer)) tx_note("Error while reading set.", true);
332                         } else if (strncmp(idbuff, TX_SET_ID_11, strlen(TX_SET_ID_11))==0)      {
333                                 if (vtt_class::load_all_11(in, buffer)) tx_note("Error while reading set.", true);                      
334                         } else if (strncmp(idbuff, TX_SET_ID_12, strlen(TX_SET_ID_12))==0) {
335                                 if (vtt_class::load_all_12(in, buffer)) tx_note("Error while reading set.", true);                      
336                         } else if (strncmp(idbuff, TX_SET_ID_13, strlen(TX_SET_ID_13))==0) {
337                                 if (vtt_class::load_all_13(in, buffer)) tx_note("Error while reading set.", true);                      
338                         } else if (strncmp(idbuff, TX_SET_ID_14, strlen(TX_SET_ID_14))==0) {
339                                 if (vtt_class::load_all_14(in, buffer)) tx_note("Error while reading set.", true);                      
340                         }       else {
341                                 tx_note("This file is not a terminatorX set-file.", true);
342                                 fclose(in);
343                                 return;
344                         }
345                         fclose(in);     
346                 } else {
347                         strcpy(idbuff, "Failed to access file: \"");    // I'm stealing the unrelated sting for a temp :)
348                         strcat(idbuff, globals.tables_filename);
349                         strcat(idbuff, "\"");
350                         tx_note(idbuff, true);
351                         
352                         return;
353                 }
354         }
355 #else
356         else {
357                 strcpy(idbuff, "Failed to access file: \"");    // I'm stealing the unrelated sting for a temp :)
358                 strcat(idbuff, globals.tables_filename);
359                 strcat(idbuff, "\"");
360                 tx_note(idbuff, true);
361                 
362                 return;
363         }
364 #endif  
365         
366         tx_mg_have_setname=true;
367         strcpy(tx_mg_current_setname, buffer);
368         
369         tX_seqpar :: init_all_graphics();
370         vg_init_all_non_seqpars();
371                 
372         gtk_adjustment_set_value(volume_adj, 2.0-globals.volume);
373         gtk_adjustment_set_value(pitch_adj, globals.pitch);
374         sprintf(wbuf,"terminatorX - %s", strip_path(buffer));
375         gtk_window_set_title(GTK_WINDOW(main_window), wbuf);            
376 }
377
378 void do_load_tables(GtkWidget *wid)
379 {
380         char buffer[PATH_MAX];
381         
382         strcpy(buffer, gtk_file_selection_get_filename(GTK_FILE_SELECTION(load_dialog)));
383         
384         gtk_widget_destroy(load_dialog);
385         
386         load_dialog=NULL;
387         load_dialog_win=NULL;
388
389         load_tt_part(buffer);   
390 }
391
392 GtkSignalFunc load_tables()
393 {
394         if (load_dialog_win) 
395         {
396                 gdk_window_raise(load_dialog_win);
397                 return 0;
398         }
399         
400         load_dialog=gtk_file_selection_new("Load Set"); 
401         
402         gtk_file_selection_show_fileop_buttons(GTK_FILE_SELECTION(load_dialog));
403         gtk_file_selection_complete(GTK_FILE_SELECTION(load_dialog), "*.tX");
404         
405         if (strlen(globals.tables_filename))
406         {
407                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(load_dialog), globals.tables_filename);
408         }
409         
410         gtk_widget_show(load_dialog);
411         
412         load_dialog_win=load_dialog->window;
413         
414         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(load_dialog)->ok_button), "clicked", GTK_SIGNAL_FUNC(do_load_tables), NULL);
415         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(load_dialog)->cancel_button), "clicked", GTK_SIGNAL_FUNC (cancel_load_tables), NULL); 
416         gtk_signal_connect (GTK_OBJECT(load_dialog), "delete-event", GTK_SIGNAL_FUNC(cancel_load_tables), NULL);        
417         
418         return NULL;
419 }
420
421 GtkSignalFunc drop_set(GtkWidget *widget, GdkDragContext *context,
422                 gint x, gint y, GtkSelectionData *selection_data,
423                 guint info, guint time, vtt_class *vtt)
424 {
425         char filename[PATH_MAX];
426         char *fn;
427         
428         strncpy(filename, (char *) selection_data->data, (size_t) selection_data->length);
429         filename[selection_data->length]=0;
430
431         fn = strchr (filename, '\r');
432         *fn=0;  
433         
434         fn = strchr (filename, ':');
435         if (fn) fn++; else fn=(char *) selection_data->data;
436         
437         load_tt_part(fn);
438
439         strcpy (filename, "dont segfault workaround ;)");
440         return NULL;
441 }
442
443
444 /* save tables */
445
446 GtkSignalFunc cancel_save_tables(GtkWidget *wid)
447 {
448         gtk_widget_destroy(save_dialog);
449         save_dialog=NULL;
450         save_dialog_win=NULL;
451         return(0);
452 }
453
454 void do_save_tables(GtkWidget *wid)
455 {
456         FILE *out;
457         char buffer[PATH_MAX];
458         char wbuf[PATH_MAX];
459         char *ext;
460         
461         if (wid) {
462                 strcpy(buffer, gtk_file_selection_get_filename(GTK_FILE_SELECTION(save_dialog)));
463                 int len=strlen(buffer);
464                 if (!len || (buffer[len-1]=='/')) {                     
465                         tx_note("Invalid filename for set file.", true);                        
466                         return;
467                 }
468                 strcpy(globals.tables_filename, buffer);
469                 gtk_widget_destroy(save_dialog);
470                 save_dialog=NULL;
471                 save_dialog_win=NULL;
472         } else {
473                 strcpy(buffer, tx_mg_current_setname);
474         }
475         
476         ext=strrchr(buffer, '.');
477         
478         if (ext)
479         {
480                 if (strcmp(ext, ".tX")) strcat(buffer, ".tX");
481         }
482         else
483         {
484                 strcat(buffer, ".tX");
485         }
486
487         tx_mg_have_setname=true;
488         strcpy(tx_mg_current_setname, buffer);
489         
490         out=fopen(buffer, "w");
491         
492         if (out)
493         {
494                 if (vtt_class::save_all(out)) tx_note("Error while saving set.", true);
495                 fclose(out);
496                 sprintf(wbuf,"terminatorX - %s", strip_path(buffer));
497                 gtk_window_set_title(GTK_WINDOW(main_window), wbuf);                            
498         }
499         else
500         {
501                 tx_note("Failed to open file for write access.", true);
502         }
503 }
504
505 GtkSignalFunc save_tables_as()
506 {
507         if (save_dialog_win) {
508                 gtk_widget_destroy(save_dialog);
509                 save_dialog=NULL;
510                 save_dialog_win=NULL;
511         }
512         
513         save_dialog=gtk_file_selection_new("Save Set"); 
514
515         if (tx_mg_have_setname) {
516                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(save_dialog), tx_mg_current_setname);
517         }
518         
519         gtk_widget_show(save_dialog);
520         
521         save_dialog_win=save_dialog->window;
522         
523         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(save_dialog)->ok_button), "clicked", GTK_SIGNAL_FUNC(do_save_tables), NULL);
524         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(save_dialog)->cancel_button), "clicked", GTK_SIGNAL_FUNC (cancel_save_tables), NULL); 
525         gtk_signal_connect (GTK_OBJECT(save_dialog), "delete-event", GTK_SIGNAL_FUNC(cancel_save_tables), NULL);        
526
527         return NULL;
528 }
529
530 GtkSignalFunc save_tables()
531 {
532         if (!tx_mg_have_setname) {
533                 save_tables_as();
534         } else {
535                 do_save_tables(NULL);
536         }
537         
538         return NULL;
539 }
540
541 GtkSignalFunc master_volume_changed (GtkWidget *wid, void *d)
542 {
543         sp_master_volume.receive_gui_value((float) 2.0-GTK_ADJUSTMENT(wid)->value);
544         return NULL;    
545 }
546
547 GtkSignalFunc master_pitch_changed(GtkWidget *wid, void *d)
548 {
549         sp_master_pitch.receive_gui_value((float) GTK_ADJUSTMENT(wid)->value);  
550         return NULL;    
551 }
552
553 GtkSignalFunc saturate_changed(GtkWidget *w, void *d)
554 {
555         vtt_class::enable_saturate (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)));
556         return NULL;    
557 }
558
559 void mg_enable_critical_buttons(int enable)
560 {
561         gtk_widget_set_sensitive(seq_rec_btn, enable);
562         gtk_widget_set_sensitive(seq_play_btn, enable);
563         gtk_widget_set_sensitive(seq_slider, enable);
564
565         gtk_widget_set_sensitive(rec_menu_item, enable);
566         vg_enable_critical_buttons(enable);
567 }
568
569
570 GtkSignalFunc seq_stop(GtkWidget *w, void *);
571
572 GtkSignalFunc audio_on(GtkWidget *w, void *d)
573 {
574         tX_engine_error res;
575         
576         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
577         {               
578                 sequencer_ready=0;
579                 mg_enable_critical_buttons(0);
580                 res=tX_engine::get_instance()->run();
581                 sequencer_ready=1;
582
583                 if (res!=NO_ERROR)
584                 {
585                         mg_enable_critical_buttons(1);
586                         switch(res)
587                         {
588                                 case ERROR_BUSY:
589                                 tx_note("Error starting engine: engine is already running.", true);
590                                 break;
591                                 case ERROR_AUDIO:
592                                 tx_note("Error starting engine: failed to access audiodevice.", true);
593                                 break;
594                                 case ERROR_TAPE:
595                                 tx_note("Error starting engine: failed to open the recording file.", true);
596                                 break;
597                                 default:tx_note("Error starting engine: Unknown error.", true);
598                         }
599                         return 0;
600                 }
601
602                 stop_update=0;
603                 audioon=1;
604                 update_delay=globals.update_delay;
605                 update_tag=gtk_timeout_add(globals.update_idle, (GtkFunction) pos_update, NULL);
606                 gtk_widget_set_sensitive(grab_button, 1);
607         }
608         else
609         {               
610                 if (!sequencer_ready) return NULL;
611                 gtk_widget_set_sensitive(grab_button, 0);
612                 tX_engine::get_instance()->stop();
613                 stop_update=1;
614                 audioon=0;
615                 if (tX_engine::get_instance()->get_recording_request()) {
616                         tX_engine::get_instance()->set_recording_request(false);
617                         rec_dont_care=1;
618                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(rec_menu_item), 0);
619                         rec_dont_care=0;
620                 }
621                 seq_stop(NULL, NULL);
622                 mg_enable_critical_buttons(1);
623         }
624         
625         return NULL;
626 }
627
628 GtkSignalFunc cancel_rec(GtkWidget *wid)
629 {
630         gtk_widget_destroy(rec_dialog);
631         rec_dialog=NULL;
632         rec_dialog_win=NULL;
633         rec_dont_care=0;
634         return(0);
635 }
636
637 void do_rec(GtkWidget *wid)
638 {
639         char buffer[PATH_MAX];
640         
641         strcpy(buffer, gtk_file_selection_get_filename(GTK_FILE_SELECTION(rec_dialog)));
642
643         if (strlen(buffer))
644         {
645                 strcpy(globals.record_filename, buffer);                
646                 tX_engine::get_instance()->set_recording_request(true);
647                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(rec_menu_item), 1);
648         }
649         
650         rec_dont_care=0;
651         
652         gtk_widget_destroy(rec_dialog);
653         
654         rec_dialog=NULL;
655         rec_dialog_win=NULL;
656 }
657
658 GtkSignalFunc select_rec_file()
659 {
660         if (rec_dialog_win) 
661         {
662                 gdk_window_raise(rec_dialog_win);
663                 return 0;
664         }
665         
666         rec_dialog=gtk_file_selection_new("Record To Disk");    
667         
668         if (strlen(globals.record_filename))
669         {
670                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(rec_dialog), globals.record_filename);
671         }
672         
673         gtk_widget_show(rec_dialog);
674         
675         rec_dialog_win=rec_dialog->window;
676         
677         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(rec_dialog)->ok_button), "clicked", GTK_SIGNAL_FUNC(do_rec), NULL);
678         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(rec_dialog)->cancel_button), "clicked", GTK_SIGNAL_FUNC (cancel_rec), NULL);  
679         gtk_signal_connect (GTK_OBJECT(rec_dialog), "delete-event", GTK_SIGNAL_FUNC(cancel_rec), NULL); 
680         
681         return NULL;
682 }
683
684 GtkSignalFunc tape_on(GtkWidget *w, void *d)
685 {
686         if (rec_dont_care) return 0;
687
688         if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(w)))
689         {       
690                 {
691                         rec_dont_care=1;
692                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(w), 0);
693                         select_rec_file();
694                 }
695         }
696         else
697         {
698                         tX_engine::get_instance()->set_recording_request(false);
699         }
700         
701         return NULL;
702 }
703
704 void grab_on(GtkWidget *w, void *d)
705 {
706         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) {
707                 tX_engine::get_instance()->set_grab_request();
708         }
709         grab_status=1;
710 }
711
712 void grab_off()
713 {
714         grab_status=0;
715 }
716
717 void quit()
718 {
719         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window), 
720         GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
721         "Exit terminatorX and loose all unsaved data?");
722         
723         int res=gtk_dialog_run(GTK_DIALOG(dialog));
724         gtk_widget_destroy(dialog);
725                 
726         if (res!=GTK_RESPONSE_YES) {
727                 return;
728         }
729
730         turn_audio_off();
731         if (update_tag)
732         gtk_timeout_remove(update_tag);
733         globals.width=main_window->allocation.width;
734         globals.height=main_window->allocation.height;
735
736         gtk_main_quit();
737 }
738
739 void mplcfitx()
740 /* Most Probably Least Called Function In terminatorX :) */
741 {
742         show_about(0);
743 }
744
745 GtkSignalFunc seq_play(GtkWidget *w, void *)
746 {
747         if ((sequencer.is_empty()) &&   (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(seq_rec_btn)))) {
748                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
749                  {
750                         tx_note("Sequencer playback triggered - but no events recorded yet - nothing to playback!\n\nTo perform live with terminatorX just activate the audio engine with the \"Power\" button.");
751                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 0);
752                  }
753         } else {
754                 if (seq_stop_override) return NULL;
755                         
756                 seq_adj_care=0;
757                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 1);
758                 sequencer.trig_play();
759         
760                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 1);
761         }
762         
763         return NULL;
764 }
765
766 GtkSignalFunc seq_stop(GtkWidget *w, void *)
767 {
768         if (!sequencer_ready) return NULL;
769         sequencer.trig_stop();
770         seq_adj_care=1;
771         seq_stop_override=1;
772         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(seq_play_btn), 0);
773         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(seq_rec_btn), 0);
774         while (gtk_events_pending()) gtk_main_iteration();              
775         seq_stop_override=0;
776         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 0); 
777         gtk_widget_set_sensitive(seq_slider, 1);        
778         gtk_widget_set_sensitive(engine_btn, 1);
779         gtk_widget_set_sensitive(seq_rec_btn, 1);
780
781         return NULL;
782 }
783
784 GtkSignalFunc seq_rec(GtkWidget *w, void *)
785 {
786         seq_adj_care=0;
787         gtk_widget_set_sensitive(seq_slider, 0);
788
789         if (seq_stop_override) return NULL;
790         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 1);
791         gtk_widget_set_sensitive(engine_btn, 0);
792         gtk_widget_set_sensitive(seq_rec_btn, 0);
793         sequencer.trig_rec();
794         
795         return NULL;
796 }
797
798 void seq_update_entry(const guint32 timestamp)
799 {
800         char buffer[20];
801         guint32 samples;
802         guint32 minu,sec,hun;   
803         guint32 sr;
804         
805         samples=timestamp*vtt_class::get_mix_buffer_size();
806         sr=vtt_class::get_last_sample_rate();
807         
808         if (samples>0)
809         {
810                 minu=samples/(sr*60);
811                 samples-=(sr*60)*minu;
812         
813                 sec=samples/sr;
814                 samples-=sr*sec;
815         
816                 hun=samples/(sr/100);
817         }
818         else
819         {
820                 minu=sec=hun=0;
821         }
822         
823         sprintf(buffer, "%02i:%02i.%02i", minu, sec, hun);
824         gtk_entry_set_text(GTK_ENTRY(seq_entry), buffer);
825 }
826
827 void seq_update()
828 {
829         seq_update_entry(sequencer.get_timestamp());
830         gtk_adjustment_set_value(seq_adj, sequencer.get_timestamp_as_float());
831         
832 }
833 void seq_slider_released(GtkWidget *wid, void *d)
834 {
835         seq_adj_care=0;
836         gtk_widget_set_sensitive(seq_slider, 0);        
837         sequencer.forward_to_start_timestamp(0);
838         gtk_widget_set_sensitive(seq_slider, 1);        
839         seq_adj_care=1;
840 }
841 void sequencer_move(GtkWidget *wid, void *d)
842 {
843         guint32 pos;
844         
845         if (seq_adj_care)
846         {
847                 pos=sequencer.set_start_timestamp((float) GTK_ADJUSTMENT(wid)->value);
848                 seq_update_entry(pos);  
849         }
850 }
851
852 #define add_sep();      dummy=gtk_hseparator_new ();\
853         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);\
854         gtk_widget_show(dummy);\
855
856 #define add_sep2();     dummy=gtk_hseparator_new ();\
857         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);\
858         gtk_widget_show(dummy);\
859
860 void fullscreen_toggle(GtkCheckMenuItem *item, gpointer data);
861 void display_help();
862 void display_browser();
863
864 void create_master_menu() {
865         GtkWidget *menu_item;
866         GtkWidget *sub_menu;
867         GtkAccelGroup* accel_group=gtk_accel_group_new();
868         gtk_window_add_accel_group(GTK_WINDOW(main_window), accel_group);
869
870         /* FILE */
871         menu_item = gtk_menu_item_new_with_mnemonic ("_File");
872         gtk_widget_show (menu_item);
873         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
874
875         sub_menu = gtk_menu_new ();
876         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
877
878         menu_item = gtk_image_menu_item_new_from_stock ("gtk-new", accel_group);
879         gtk_widget_show (menu_item);
880         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
881         g_signal_connect(menu_item, "activate", (GCallback) new_tables, NULL);
882
883         menu_item = gtk_image_menu_item_new_from_stock ("gtk-open", accel_group);
884         gtk_widget_show (menu_item);
885         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
886         g_signal_connect(menu_item, "activate", (GCallback) load_tables, NULL);
887
888         menu_item = gtk_image_menu_item_new_from_stock ("gtk-save", accel_group);
889         gtk_widget_show (menu_item);
890         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
891         g_signal_connect(menu_item, "activate", (GCallback) save_tables, NULL);
892
893         menu_item = gtk_image_menu_item_new_from_stock ("gtk-save-as", accel_group);
894         gtk_widget_show (menu_item);
895         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
896         g_signal_connect(menu_item, "activate", (GCallback) save_tables_as, NULL);
897
898         menu_item = gtk_menu_item_new ();
899         gtk_widget_show (menu_item);
900         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
901         gtk_widget_set_sensitive (menu_item, FALSE);
902
903         menu_item = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
904         gtk_widget_show (menu_item);
905         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
906         g_signal_connect(menu_item, "activate", (GCallback) quit, NULL);
907
908         /* Turntables */
909         menu_item = gtk_menu_item_new_with_mnemonic ("_Turntables");
910         gtk_widget_show (menu_item);
911         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
912         
913         sub_menu = gtk_menu_new ();
914         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
915
916         menu_item = gtk_menu_item_new_with_mnemonic("_Add Turntable");
917         gtk_widget_show (menu_item);
918         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
919         g_signal_connect(menu_item, "activate", (GCallback) new_table, NULL);
920
921         menu_item = gtk_check_menu_item_new_with_mnemonic("_Record Audio To Disk");
922         rec_menu_item = menu_item;
923         gtk_widget_show (menu_item);
924         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
925         g_signal_connect(menu_item, "activate", (GCallback) tape_on, NULL);
926
927         /* Options */
928         menu_item = gtk_menu_item_new_with_mnemonic ("_Options");
929         gtk_widget_show (menu_item);
930         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
931
932         sub_menu = gtk_menu_new ();
933         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
934                 
935         menu_item = gtk_check_menu_item_new_with_mnemonic("_Fullscreen");
936         fullscreen_item = menu_item;
937         gtk_widget_show (menu_item);
938         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
939
940         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), globals.fullscreen_enabled);
941         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F11, (GdkModifierType) 0, GTK_ACCEL_VISIBLE);
942         g_signal_connect(menu_item, "activate", (GCallback) fullscreen_toggle, NULL);
943         
944         menu_item = gtk_menu_item_new ();
945         gtk_widget_show (menu_item);
946         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
947         gtk_widget_set_sensitive (menu_item, FALSE);
948
949         menu_item = gtk_menu_item_new_with_mnemonic("_Preferences");
950         gtk_widget_show (menu_item);
951         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
952         g_signal_connect(menu_item, "activate", (GCallback) display_options, NULL);
953
954         /* HELP */ 
955         menu_item = gtk_menu_item_new_with_mnemonic ("_Help");
956         gtk_widget_show (menu_item);
957         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
958         gtk_menu_item_set_right_justified(GTK_MENU_ITEM(menu_item), TRUE);
959         
960         sub_menu = gtk_menu_new ();
961         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
962
963         menu_item = gtk_menu_item_new_with_mnemonic ("_Contents");
964         gtk_widget_show (menu_item);
965         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
966         g_signal_connect(menu_item, "activate", (GCallback) display_help, NULL);
967         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F1, (GdkModifierType) 0, GTK_ACCEL_VISIBLE);
968
969         menu_item = gtk_menu_item_new_with_mnemonic ("_About");
970         gtk_widget_show (menu_item);
971         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
972         g_signal_connect(menu_item, "activate", (GCallback) mplcfitx, NULL);
973         
974         menu_item = gtk_menu_item_new ();
975         gtk_widget_show (menu_item);
976         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
977         gtk_widget_set_sensitive (menu_item, FALSE);
978
979         menu_item = gtk_menu_item_new_with_mnemonic ("_Visit terminatorX.cx");
980         gtk_widget_show (menu_item);
981         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
982         g_signal_connect(menu_item, "activate", (GCallback) display_browser, NULL);
983 }
984
985 void create_mastergui(int x, int y)
986 {
987         GtkWidget *mother_of_all_boxen;
988         GtkWidget *main_vbox;
989         GtkWidget *right_hbox;
990         GtkWidget *left_hbox;
991         GtkWidget *control_box;
992         //GtkWidget *sequencer_box;
993         GtkAdjustment *dumadj;
994         GtkWidget *dummy;
995         GtkWidget *master_vol_box;
996         GtkWidget *status_box;
997         
998         static GtkTargetEntry drop_types [] = {
999                 { "text/uri-list", 0, 0}
1000         };
1001         static gint n_drop_types = sizeof (drop_types) / sizeof(drop_types[0]);
1002         
1003         gui_tooltips=gtk_tooltips_new();
1004
1005         main_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
1006
1007         gtk_window_set_wmclass(GTK_WINDOW(main_window), "terminatorX", "tX_mastergui");
1008
1009         gtk_window_set_title(GTK_WINDOW(main_window), "terminatorX");
1010         
1011         gtk_container_set_border_width(GTK_CONTAINER(main_window), 5);
1012
1013         gtk_widget_realize(main_window);
1014         
1015         mother_of_all_boxen=gtk_vbox_new(FALSE, 5);
1016         gtk_container_add(GTK_CONTAINER(main_window), mother_of_all_boxen);
1017         gtk_widget_show(mother_of_all_boxen);   
1018         
1019         main_menubar=gtk_menu_bar_new();
1020         gtk_box_pack_start(GTK_BOX(mother_of_all_boxen), main_menubar, WID_FIX);
1021         gtk_widget_show(main_menubar);  
1022         
1023         create_master_menu();
1024         
1025         main_vbox=gtk_hbox_new(FALSE, 5);
1026         gtk_box_pack_start(GTK_BOX(mother_of_all_boxen), main_vbox, WID_DYN);
1027         gtk_widget_show(main_vbox);
1028         
1029         left_hbox=gtk_vbox_new(FALSE, 5);
1030         gtk_box_pack_start(GTK_BOX(main_vbox), left_hbox, WID_DYN);
1031         gtk_widget_show(left_hbox);
1032         
1033         control_box=gtk_hbox_new(FALSE, 5);
1034         gtk_box_pack_start(GTK_BOX(left_hbox), control_box, WID_FIX);
1035         gtk_widget_show(control_box);
1036         
1037 /*      dummy=gtk_hseparator_new();
1038         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_FIX);
1039         gtk_widget_show(dummy);*/
1040
1041
1042         dummy=gtk_hseparator_new();
1043         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_FIX);
1044         gtk_widget_show(dummy);
1045
1046     /* control_box contents */
1047
1048         dummy=tx_xpm_label_box(TX_ICON_AUDIOENGINE, "Audio");
1049         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1050         gtk_widget_show(dummy);
1051         
1052         dummy=tx_xpm_button_new(TX_ICON_POWER,"Power ", 1);
1053         connect_button(dummy,audio_on, NULL);
1054         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1055         gui_set_tooltip(dummy, "Turn the audio engine on/off.");
1056         gtk_widget_show(dummy);
1057         engine_btn=dummy;
1058         
1059         grab_button=tx_xpm_button_new(TX_ICON_GRAB, "Mouse Grab ", 1);
1060         gtk_box_pack_start(GTK_BOX(control_box), grab_button, WID_FIX);
1061         connect_button(grab_button, grab_on, NULL);
1062         gui_set_tooltip(grab_button, "Enter the mouse grab mode operation. Press <ESCAPE> to exit grab mode.");
1063         gtk_widget_show(grab_button);
1064
1065         /*dummy=gtk_check_button_new_with_label("Record");
1066         rec_btn=dummy;
1067         connect_button(dummy,tape_on, NULL);
1068         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1069         gui_set_tooltip(dummy, "Record the audio the terminatorX' audio engine renders. You will be prompted to enter a name for the target wav-file.");
1070         gtk_widget_show(dummy);*/
1071
1072         dummy=gtk_vseparator_new();
1073         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1074         gtk_widget_show(dummy);
1075     
1076         dummy=tx_xpm_label_box(TX_ICON_SEQUENCER, "Seq.");
1077         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1078         gtk_widget_show(dummy);
1079
1080         dummy=tx_xpm_button_new(TX_ICON_PLAY,"Play ", 1);
1081         connect_button(dummy, seq_play, NULL);
1082         seq_play_btn=dummy;
1083         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1084         gui_set_tooltip(dummy, "Playback previously recorded events from the sequencer. This will turn on the audio engine automagically.");
1085         gtk_widget_show(dummy);
1086
1087         dummy=tx_xpm_button_new(TX_ICON_STOP,"Stop ", 0);
1088         seq_stop_btn=dummy;
1089         connect_button(dummy, seq_stop, NULL);  
1090         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1091         gui_set_tooltip(dummy, "Stop the playback of sequencer events.");
1092         gtk_widget_show(dummy);
1093
1094         dummy=tx_xpm_button_new(TX_ICON_RECORD,"Record ", 1);
1095         connect_button(dummy, seq_rec, NULL);
1096         seq_rec_btn=dummy;
1097         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1098         gui_set_tooltip(dummy, "Enable recording of *events* into the sequencer. All touched controls will be recorded. Existing events for the song-time recording will be overwritten for touched controls.");
1099         gtk_widget_show(dummy);
1100
1101         dummy=gtk_label_new("Pos:");
1102         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1103         gtk_widget_show(dummy);
1104         
1105         dummy=gtk_entry_new_with_max_length(12);
1106         seq_entry=dummy;
1107         //gtk_widget_set_usize(dummy, 65, 20);
1108         gtk_entry_set_text(GTK_ENTRY(dummy), "00:00.00");
1109         gtk_entry_set_width_chars(GTK_ENTRY(dummy), 9);
1110         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1111         gtk_widget_show(dummy);
1112
1113         dumadj=(GtkAdjustment*) gtk_adjustment_new(0, 0, 100, 0.1, 1, 1);
1114         seq_adj=dumadj;
1115         connect_adj(dumadj, sequencer_move, NULL);      
1116         dummy=gtk_hscale_new(dumadj);
1117         gtk_widget_set_usize(dummy, 65, 20);
1118         seq_slider=dummy;
1119         gtk_signal_connect(GTK_OBJECT(seq_slider), "button-release-event", (GtkSignalFunc) seq_slider_released, NULL);
1120         gtk_scale_set_draw_value(GTK_SCALE(dummy), FALSE);
1121         
1122         gui_set_tooltip(dummy, "Select the start position for the sequencer in song-time.");
1123         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_DYN);
1124         gtk_widget_show(dummy);
1125         
1126         dummy=gtk_hbox_new(FALSE,2); //gtk_hpaned_new ();
1127         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_DYN);
1128         gtk_widget_show(dummy);
1129         
1130         tt_parent=dummy;
1131
1132     panel_bar=gtk_hbox_new(TRUE,2);
1133         gtk_box_pack_start(GTK_BOX(left_hbox), panel_bar, WID_FIX);
1134
1135         control_parent=gtk_hbox_new(FALSE,0);
1136         gtk_box_pack_start(GTK_BOX(tt_parent), control_parent, WID_FIX);
1137         //gtk_paned_pack1(GTK_PANED(tt_parent), control_parent, FALSE, FALSE);
1138         gtk_widget_show(control_parent);
1139
1140         dummy=gtk_vseparator_new();
1141         gtk_box_pack_start(GTK_BOX(tt_parent), dummy, WID_FIX);
1142         gtk_widget_show(dummy);
1143
1144         audio_parent=gtk_vbox_new(FALSE,0);
1145         gtk_box_pack_start(GTK_BOX(tt_parent), audio_parent, WID_DYN);
1146         //gtk_paned_pack2(GTK_PANED(tt_parent), audio_parent, TRUE, FALSE);
1147         gtk_widget_show(audio_parent);
1148         
1149         dummy=gtk_vseparator_new();
1150         gtk_box_pack_start(GTK_BOX(main_vbox), dummy, WID_FIX);
1151         gtk_widget_show(dummy);
1152                 
1153         right_hbox=gtk_vbox_new(FALSE, 5);
1154         gtk_box_pack_start(GTK_BOX(main_vbox), right_hbox, WID_FIX);
1155         gtk_widget_show(right_hbox);
1156
1157         /* Master */
1158         
1159         dummy=gtk_label_new(NULL);
1160         //gtk_label_set_markup(GTK_LABEL(dummy),"<span foreground='#000080'>Master</span>");
1161         gtk_label_set_markup(GTK_LABEL(dummy),"<b>Master</b>");
1162         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1163         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1164         gtk_widget_show(dummy); 
1165
1166         dummy=gtk_hseparator_new();
1167         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1168         gtk_widget_show(dummy);
1169
1170          /* Pitch */
1171          
1172         /*dummy=gtk_label_new("Pitch:");
1173         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1174         gtk_widget_show(dummy);*/
1175
1176         dumadj=(GtkAdjustment*) gtk_adjustment_new(globals.pitch, -3, 3, 0.001, 0.001, 0.01);
1177         pitch_adj=dumadj;
1178         connect_adj(dumadj, master_pitch_changed, NULL);
1179         
1180         tX_extdial *pdial=new tX_extdial("Pitch", pitch_adj, true);
1181         gtk_box_pack_start(GTK_BOX(right_hbox), pdial->get_widget(), WID_FIX);
1182         gui_set_tooltip(pdial->get_entry(), "Use this dial to adjust the master pitch (affecting *all* turntables).");
1183         
1184 /*      dummy=gtk_hscale_new(dumadj);
1185         gtk_scale_set_digits(GTK_SCALE(dummy), 2);
1186         gtk_scale_set_value_pos(GTK_SCALE(dummy), GTK_POS_LEFT);
1187         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_DYN);
1188         gui_set_tooltip(dummy, "Use this scale to adjust the master pitch (affecting *all* turntables).");
1189         gtk_widget_show(dummy);
1190 */
1191         dummy=gtk_hseparator_new();
1192         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1193         gtk_widget_show(dummy);
1194         
1195         /* Volume */
1196         master_vol_box=gtk_hbox_new(FALSE, 2);
1197         gtk_box_pack_start(GTK_BOX(right_hbox), master_vol_box, WID_DYN);
1198         gtk_widget_show(master_vol_box);        
1199         
1200         dumadj=(GtkAdjustment*) gtk_adjustment_new(2.0-globals.volume, 0, 2, 0.01, 0.05, 0.005);
1201         volume_adj=dumadj;
1202
1203         connect_adj(dumadj, master_volume_changed, NULL);       
1204         dummy=gtk_vscale_new(dumadj);
1205         gtk_scale_set_draw_value(GTK_SCALE(dummy), False);
1206         gtk_box_pack_end(GTK_BOX(master_vol_box), dummy, WID_FIX);
1207         gtk_widget_show(dummy); 
1208         gui_set_tooltip(dummy, "Adjust the master volume. This parameter will effect *all* turntables in the set.");
1209         
1210         main_flash_r=gtk_tx_flash_new();
1211         gtk_box_pack_end(GTK_BOX(master_vol_box), main_flash_r, WID_DYN);
1212         gtk_widget_show(main_flash_r);
1213
1214         main_flash_l=gtk_tx_flash_new();
1215         gtk_box_pack_end(GTK_BOX(master_vol_box), main_flash_l, WID_DYN);
1216         gtk_widget_show(main_flash_l);
1217
1218         dummy=gtk_label_new("Volume");
1219         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1220         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1221         gtk_widget_show(dummy);
1222
1223         /* STATUS BOX */ 
1224         dummy=gtk_hseparator_new();
1225         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1226         gtk_widget_show(dummy);
1227         
1228         status_box=gtk_vbox_new(FALSE, 0);
1229         gtk_box_pack_start(GTK_BOX(right_hbox), status_box, WID_FIX);
1230         gtk_widget_show(status_box);
1231         
1232         dummy=gtk_label_new("0");
1233         used_mem=dummy;
1234         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1235         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1236         gtk_widget_show(dummy);
1237
1238         dummy=gtk_label_new("Mem/kB:");
1239         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1240         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1241         gtk_widget_show(dummy);
1242         
1243         add_sep2();
1244
1245         dummy=gtk_label_new("1");
1246         no_of_vtts=dummy;
1247         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1248         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1249         gtk_widget_show(dummy);
1250
1251         dummy=gtk_label_new("Vtts:");
1252         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1253         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1254         gtk_widget_show(dummy);
1255
1256         add_sep2();
1257
1258         dummy=gtk_label_new(VERSION);
1259         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1260         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1261         gtk_widget_show(dummy);
1262
1263         dummy=gtk_label_new("Release:");
1264         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1265         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1266         gtk_widget_show(dummy);
1267         
1268         add_sep2();
1269
1270         dummy=gtk_label_new(NULL);
1271         gtk_label_set_markup(GTK_LABEL(dummy), "<b>Status</b>");
1272         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1273         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1274         gtk_widget_show(dummy);
1275         
1276         /* END GUI */
1277         
1278         gtk_window_set_default_size(GTK_WINDOW(main_window), x, y);     
1279         gtk_widget_set_sensitive(grab_button, 0);
1280
1281         new_table(NULL, NULL); // to give the user something to start with ;)
1282
1283         gtk_signal_connect (GTK_OBJECT(main_window), "delete-event", (GtkSignalFunc) quit, NULL);       
1284         
1285         if (globals.tooltips) gtk_tooltips_enable(gui_tooltips);
1286         else gtk_tooltips_disable(gui_tooltips);
1287 }
1288
1289 gfloat old_percent=-1;
1290
1291 void wav_progress_update(gfloat percent)
1292 {
1293         percent=floor(percent*10.0)/10.0; //Updating statusbars with gtk-themes eats up hell of a lot CPU-time
1294                                           // which is why we update every 10% only.
1295         
1296         if (wav_progress)
1297         {
1298                 if (old_percent != percent)
1299                 {
1300                         old_percent = percent;
1301                         gtk_progress_bar_update(GTK_PROGRESS_BAR(wav_progress), percent);
1302                         while (gtk_events_pending()) gtk_main_iteration();      
1303                 }
1304         }
1305
1306 }
1307
1308 void note_destroy(GtkWidget *widget, GtkWidget *mbox)
1309 {
1310         gtk_widget_destroy(GTK_WIDGET(mbox));
1311 }
1312
1313 void tx_note(const char *message, bool isError)
1314 {
1315         char buffer[4096]="terminatorX ";
1316         if (isError) {
1317                 strcat(buffer, "note:\n\n");
1318         } else {
1319                 strcat(buffer, "error:\n\n");
1320         }
1321         
1322         strcat(buffer, message);
1323         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window),
1324                 GTK_DIALOG_DESTROY_WITH_PARENT,
1325                 isError ? GTK_MESSAGE_ERROR : GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, message);
1326         gtk_dialog_run(GTK_DIALOG(dialog));
1327         gtk_widget_destroy(dialog);     
1328 }
1329
1330
1331 void tx_l_note(const char *message)
1332 {
1333         char buffer[4096]="Plugin info:\n\n";
1334         strcat(buffer, message);
1335         
1336         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window),
1337                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, message);
1338         gtk_dialog_run(GTK_DIALOG(dialog));
1339         gtk_widget_destroy(dialog);     
1340 }
1341
1342
1343 void add_to_panel_bar(GtkWidget *button) {
1344         buttons_on_panel_bar++;
1345         gtk_box_pack_start(GTK_BOX(panel_bar), button, WID_DYN);
1346         gtk_widget_show(panel_bar);
1347 }
1348
1349 void remove_from_panel_bar(GtkWidget *button) {
1350         buttons_on_panel_bar--;
1351         gtk_container_remove(GTK_CONTAINER(panel_bar), button);
1352         if (buttons_on_panel_bar==0) gtk_widget_hide(panel_bar);
1353 }
1354
1355 /* Fullscreen code... */
1356 #define _WIN_LAYER_TOP          -1
1357 #define _WIN_LAYER_NORMAL       4
1358 #define _NET_WM_STATE_REMOVE    0
1359 #define _NET_WM_STATE_ADD       1
1360 #define _NET_WM_STATE_TOGGLE    2
1361
1362 void fullscreen_toggle(GtkCheckMenuItem *item, gpointer data) {
1363         XEvent xev;
1364         Window win=GDK_WINDOW_XID(main_window->window);
1365         Display *disp=GDK_WINDOW_XDISPLAY(main_window->window);
1366         
1367         globals.fullscreen_enabled=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(fullscreen_item));
1368         
1369         /* Top layer.. */
1370         xev.xclient.type = ClientMessage;
1371         xev.xclient.serial = 0;
1372         xev.xclient.send_event = True;
1373         xev.xclient.display = disp;
1374         xev.xclient.window = win;
1375         xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_WIN_LAYER");
1376         xev.xclient.format = 32;
1377         xev.xclient.data.l[0] = globals.fullscreen_enabled ? _WIN_LAYER_TOP : _WIN_LAYER_NORMAL ;
1378         XSendEvent(disp, GDK_WINDOW_XID (gdk_get_default_root_window ()),
1379                 False, SubstructureRedirectMask | SubstructureNotifyMask,
1380                 &xev);
1381         
1382         /* Fullscreen */
1383         xev.xclient.type = ClientMessage;
1384         xev.xclient.serial = 0;
1385         xev.xclient.send_event = True;
1386         xev.xclient.display = disp;
1387         xev.xclient.window = win;
1388         xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_NET_WM_STATE");
1389         xev.xclient.format = 32;
1390         xev.xclient.data.l[0] = globals.fullscreen_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1391         xev.xclient.data.l[1] = gdk_x11_atom_to_xatom (gdk_atom_intern ("_NET_WM_STATE_FULLSCREEN", TRUE));
1392         xev.xclient.data.l[2] = gdk_x11_atom_to_xatom (GDK_NONE);
1393         XSendEvent(gdk_display, GDK_WINDOW_XID (gdk_get_default_root_window ()),
1394                 False, SubstructureRedirectMask | SubstructureNotifyMask,
1395                 &xev);  
1396 }
1397
1398 void fullscreen_setup() {
1399         if (globals.fullscreen_enabled) {
1400                 fullscreen_toggle(NULL, NULL);
1401         }
1402 }
1403
1404 void display_mastergui()
1405 {
1406         GtkWidget *top;
1407         gtk_widget_realize(main_window);
1408         tX_set_icon(main_window, "terminatorX");
1409         load_knob_pixs(main_window);
1410         gtk_widget_show(main_window);
1411         fullscreen_setup();     
1412         top=gtk_widget_get_toplevel(main_window);
1413         xwindow=GDK_WINDOW_XWINDOW(top->window);
1414 }
1415
1416 pid_t help_child=0;
1417 GTimer *help_timer=NULL;
1418 int help_tag=-1;
1419
1420 int help_checker() {
1421         gdouble time;
1422         gulong ms;
1423         int status;
1424         int result=waitpid(help_child, &status, WNOHANG);
1425         
1426         if (result==0) {
1427                 time=g_timer_elapsed(help_timer, &ms);
1428                 if (time > 5) {
1429                         /* 5 seconds and it's still running - so we assume everything's OK. */
1430                         tX_debug("No longer waiting for gnome-help..");
1431                         gtk_idle_remove(help_tag);
1432                         help_tag=-1;
1433                 }
1434         } else {
1435                 /* We are still here and the child exited - that could mean trouble. */
1436                 tx_note("Couldn't run the gnome-help command (alias \"yelp\") to display the terminatorX manual. Please ensure that \"yelp\" is installed.", true);             
1437                 
1438                 gtk_idle_remove(help_tag);
1439                 help_tag=-1;
1440         }
1441         return TRUE;    
1442 }
1443
1444 #ifndef INSTALL_PREFIX
1445 #define INSTALL_PREFIX "/usr/local/share"
1446 #endif
1447
1448 void display_help() {   
1449         help_child=fork();
1450
1451         if (help_tag!=-1) {
1452                 gtk_idle_remove(help_tag);
1453                 if (help_timer) g_timer_destroy(help_timer);
1454                 help_child=0;
1455                 help_tag=-1;
1456                 help_timer=NULL;
1457         }
1458         
1459         if (help_child==0) {
1460                 // child
1461                 // execlp("gnome-help","gnome-help","ghelp:/" INSTALL_PREFIX "/terminatorX/doc/terminatorX-manual/C/terminatorX-manual.xml", NULL);
1462                 execlp("gnome-help","gnome-help","ghelp:/" XML_MANUAL, NULL);           
1463                 _exit(-1);
1464         } else if (help_child==-1) {
1465                 tx_note("System error: couldn't fork() to run the help process.", true);
1466         } else {
1467                 help_timer=g_timer_new();
1468                 g_timer_start(help_timer);
1469         
1470                 help_tag=gtk_idle_add((GtkFunction) help_checker, NULL);
1471         }
1472 }
1473
1474 pid_t browser_child=0;
1475 GTimer *browser_timer=NULL;
1476 int browser_tag=-1;
1477
1478 int browser_checker() {
1479         gdouble time;
1480         gulong ms;
1481         int status;
1482         int result=waitpid(browser_child, &status, WNOHANG);
1483         
1484         if (result==0) {
1485                 time=g_timer_elapsed(browser_timer, &ms);
1486                 if (time > 5) {
1487                         /* 5 seconds and it's still running - so we assume everything's OK. */
1488                         tX_debug("No longer waiting for a browser..");
1489                         gtk_idle_remove(browser_tag);
1490                         browser_tag=-1;
1491                 }
1492         } else {
1493                 /* We are still here and the child exited - that could mean trouble. */
1494                 tx_note("Failed to run a suitable web browser - if there's one installed on this system, please run it and forward yourself to:\nhttp://terminatorX.cx", true);         
1495                 
1496                 gtk_idle_remove(browser_tag);
1497                 browser_tag=-1;
1498         }
1499         return TRUE;    
1500 }
1501
1502 void display_browser() {        
1503         browser_child=fork();
1504
1505         if (browser_tag!=-1) {
1506                 gtk_idle_remove(browser_tag);
1507                 if (browser_timer) g_timer_destroy(browser_timer);
1508                 browser_child=0;
1509                 browser_tag=-1;
1510                 browser_timer=NULL;
1511         }
1512         
1513         if (browser_child==0) {
1514                 // child
1515                 execlp("mozilla","mozilla","http://terminatorX.cx", NULL);
1516                 execlp("netscape","netscape","http://terminatorX.cx", NULL);
1517                 execlp("galeon","galeon","http://terminatorX.cx", NULL);
1518                 execlp("konqueror","konqueror","http://terminatorX.cx", NULL);          
1519                 _exit(-1);
1520         } else if (browser_child==-1) {
1521                 tx_note("System error: couldn't fork() to run the browser process.", true);
1522         } else {
1523                 browser_timer=g_timer_new();
1524                 g_timer_start(browser_timer);
1525         
1526                 browser_tag=gtk_idle_add((GtkFunction) browser_checker, NULL);
1527         }
1528 }