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