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