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