94ba00401a66648bfce9898de349843049f37c34
[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         return NULL;
919 }
920
921 GCallback menu_delete_all_events_for_vtt(GtkWidget *, vtt_class *vtt)
922 {       
923         if (!vtt) {
924                 tX_error("No vtt passed to menu_delete_all_events_for_vtt().");
925                 return FALSE;
926         }
927         
928         char label_str[512];
929         
930         del_dialog=create_tx_del_mode();
931         tX_set_icon(del_dialog);
932
933         del_vtt=vtt;
934         GtkWidget *label=lookup_widget(del_dialog, "delmode_label");
935         
936         menu_del_mode=ALL_EVENTS_FOR_TURNTABLE;
937         
938         sprintf(label_str, "Delete <b>all</b> events for turntable <b>%s</b>.", vtt->name);
939         gtk_label_set_markup(GTK_LABEL(label), label_str);
940         gtk_widget_show(del_dialog);
941         
942         return NULL;
943 }
944
945 GCallback menu_delete_all_events_for_sp(GtkWidget *, tX_seqpar *sp)
946 {       
947         if (!sp) {
948                 tX_error("No sp passed to menu_delete_all_events_for_sp().");
949                 return FALSE;
950         }
951         
952         char label_str[512];
953         
954         del_dialog=create_tx_del_mode();
955         tX_set_icon(del_dialog);
956         
957         GtkWidget *label=lookup_widget(del_dialog, "delmode_label");
958         
959         menu_del_mode=ALL_EVENTS_FOR_SP;
960         del_sp=sp;
961         sprintf(label_str, "Delete all <b>%s</b> events for turntable <b>%s</b>.", sp->get_name(), ((vtt_class *) sp->vtt)->name);
962         gtk_label_set_markup(GTK_LABEL(label), label_str);
963         gtk_widget_show(del_dialog);
964
965         return NULL;
966 }
967
968 static GtkWidget *table_menu=NULL;
969 static GtkWidget *table_menu_item=NULL;
970
971 GCallback create_table_sequencer_menu(GtkWidget *widget, void *param) 
972 {
973         char label[328];
974         table_menu=gtk_menu_new();
975         
976         list <vtt_class *> :: iterator vtt;
977
978         for (vtt=vtt_class::main_list.begin(); vtt!=vtt_class::main_list.end(); vtt++) {
979                 GtkWidget *menu_item=gtk_menu_item_new_with_label((*vtt)->name);
980                 gtk_container_add (GTK_CONTAINER (table_menu), menu_item);
981                 gtk_widget_show(menu_item);
982                 
983                 GtkWidget *seqpar_menu=gtk_menu_new();
984                 list <tX_seqpar *> :: iterator sp;
985                 
986                 GtkWidget *all=gtk_menu_item_new_with_label("Delete All Events");
987                 gtk_container_add (GTK_CONTAINER (seqpar_menu), all);
988                 g_signal_connect(all, "activate", (GCallback) menu_delete_all_events_for_vtt, (*vtt));
989                 gtk_widget_show(all);
990
991                 GtkWidget *sep = gtk_menu_item_new ();
992                 gtk_widget_show(sep);
993                 gtk_container_add(GTK_CONTAINER (seqpar_menu), sep);
994                 gtk_widget_set_sensitive (sep, FALSE);
995                 
996                 for (sp=tX_seqpar::all.begin(); sp!=tX_seqpar::all.end(); sp++) {
997                         if ((*sp)->vtt==(*vtt)) {
998                                 sprintf(label, "Delete '%s' Events", (*sp)->get_name());
999                                 GtkWidget *menu_item=gtk_menu_item_new_with_label(label);
1000                                 g_signal_connect(menu_item, "activate", (GCallback) menu_delete_all_events_for_sp, (*sp));
1001                                 gtk_container_add(GTK_CONTAINER(seqpar_menu), menu_item);
1002                                 gtk_widget_show(menu_item);
1003                         }
1004                 }
1005                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), seqpar_menu);
1006         }
1007         
1008         gtk_menu_item_set_submenu(GTK_MENU_ITEM(table_menu_item), table_menu);
1009         
1010         return NULL;
1011 }
1012
1013 GCallback toggle_confirm_events(GtkWidget *widget, void *dummy)
1014 {       
1015         globals.confirm_events=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
1016         
1017         return NULL;    
1018 }
1019
1020 void create_master_menu() 
1021 {
1022         GtkWidget *menu_item;
1023         GtkWidget *sub_menu;
1024         GtkAccelGroup* accel_group=gtk_accel_group_new();
1025         gtk_window_add_accel_group(GTK_WINDOW(main_window), accel_group);
1026
1027         /* FILE */
1028         menu_item = gtk_menu_item_new_with_mnemonic ("_File");
1029         gtk_widget_show (menu_item);
1030         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1031
1032         sub_menu = gtk_menu_new ();
1033         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1034
1035         menu_item = gtk_image_menu_item_new_from_stock ("gtk-new", accel_group);
1036         gtk_widget_show (menu_item);
1037         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1038         g_signal_connect(menu_item, "activate", (GCallback) new_tables, NULL);
1039
1040         menu_item = gtk_image_menu_item_new_from_stock ("gtk-open", accel_group);
1041         gtk_widget_show (menu_item);
1042         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1043         g_signal_connect(menu_item, "activate", (GCallback) load_tables, NULL);
1044
1045         menu_item = gtk_image_menu_item_new_from_stock ("gtk-save", accel_group);
1046         gtk_widget_show (menu_item);
1047         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1048         g_signal_connect(menu_item, "activate", (GCallback) save_tables, NULL);
1049
1050         menu_item = gtk_image_menu_item_new_from_stock ("gtk-save-as", accel_group);
1051         gtk_widget_show (menu_item);
1052         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1053         g_signal_connect(menu_item, "activate", (GCallback) save_tables_as, NULL);
1054
1055         menu_item = gtk_menu_item_new ();
1056         gtk_widget_show (menu_item);
1057         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1058         gtk_widget_set_sensitive (menu_item, FALSE);
1059
1060         menu_item = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
1061         gtk_widget_show (menu_item);
1062         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1063         g_signal_connect(menu_item, "activate", (GCallback) quit, NULL);
1064
1065         /* Turntables */
1066         menu_item = gtk_menu_item_new_with_mnemonic ("_Turntables");
1067         gtk_widget_show (menu_item);
1068         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1069         
1070         sub_menu = gtk_menu_new ();
1071         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1072
1073         menu_item = gtk_menu_item_new_with_mnemonic("_Add Turntable");
1074         gtk_widget_show (menu_item);
1075         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1076         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_A, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);    
1077         g_signal_connect(menu_item, "activate", (GCallback) new_table, NULL);
1078
1079         menu_item = gtk_check_menu_item_new_with_mnemonic("_Record Audio To Disk");
1080         rec_menu_item = menu_item;
1081         gtk_widget_show (menu_item);
1082         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1083         g_signal_connect(menu_item, "activate", (GCallback) tape_on, NULL);
1084
1085         /* Sequencer */
1086         
1087         menu_item = gtk_menu_item_new_with_mnemonic("_Sequencer");
1088         gtk_widget_show (menu_item);
1089         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1090
1091         sub_menu = gtk_menu_new ();
1092         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1093                 
1094         table_menu = gtk_menu_new();
1095         menu_item = gtk_menu_item_new_with_mnemonic("Delete _Events");
1096         delete_all_vtt_item = menu_item;
1097         table_menu_item = menu_item;
1098         gtk_menu_item_set_submenu(GTK_MENU_ITEM(table_menu_item), table_menu);
1099         
1100         gtk_widget_show (menu_item);
1101         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1102         g_signal_connect_swapped (G_OBJECT (menu_item), "select", G_CALLBACK (create_table_sequencer_menu), NULL);
1103         
1104         menu_item = gtk_menu_item_new_with_mnemonic("Delete _All Events");
1105         delete_all_item = menu_item;
1106         gtk_widget_show (menu_item);
1107         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1108         g_signal_connect(menu_item, "activate", (GCallback) menu_delete_all_events, NULL);
1109
1110         menu_item = gtk_menu_item_new ();
1111         gtk_widget_show (menu_item);
1112         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1113         gtk_widget_set_sensitive (menu_item, FALSE);
1114
1115         menu_item = gtk_check_menu_item_new_with_mnemonic("_Confirm Recorded Events");
1116         //rec_menu_item = menu_item;
1117         gtk_widget_show (menu_item);
1118         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1119         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), globals.confirm_events);
1120         g_signal_connect(menu_item, "activate", (GCallback) toggle_confirm_events, NULL);
1121
1122         /* Options */
1123         menu_item = gtk_menu_item_new_with_mnemonic ("_Options");
1124         gtk_widget_show (menu_item);
1125         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1126
1127         sub_menu = gtk_menu_new ();
1128         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1129                 
1130         menu_item = gtk_check_menu_item_new_with_mnemonic("_Fullscreen");
1131         fullscreen_item = menu_item;
1132         gtk_widget_show (menu_item);
1133         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1134
1135         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), globals.fullscreen_enabled);
1136         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F11, (GdkModifierType) 0, GTK_ACCEL_VISIBLE);
1137         g_signal_connect(menu_item, "activate", (GCallback) fullscreen_toggle, NULL);
1138         
1139         menu_item = gtk_menu_item_new ();
1140         gtk_widget_show (menu_item);
1141         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1142         gtk_widget_set_sensitive (menu_item, FALSE);
1143
1144         menu_item = gtk_image_menu_item_new_from_stock ("gtk-preferences", accel_group);
1145         gtk_widget_show (menu_item);
1146         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1147         g_signal_connect(menu_item, "activate", (GCallback) display_options, NULL);
1148
1149         /* HELP */ 
1150         menu_item = gtk_menu_item_new_with_mnemonic ("_Help");
1151         gtk_widget_show (menu_item);
1152         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1153         gtk_menu_item_set_right_justified(GTK_MENU_ITEM(menu_item), TRUE);
1154         
1155         sub_menu = gtk_menu_new ();
1156         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1157
1158         menu_item = gtk_menu_item_new_with_mnemonic ("_Contents");
1159         gtk_widget_show (menu_item);
1160         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1161         g_signal_connect(menu_item, "activate", (GCallback) display_help, NULL);
1162         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F1, (GdkModifierType) 0, GTK_ACCEL_VISIBLE);
1163
1164         menu_item = gtk_menu_item_new_with_mnemonic ("_About");
1165         gtk_widget_show (menu_item);
1166         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1167         g_signal_connect(menu_item, "activate", (GCallback) mplcfitx, NULL);
1168         
1169         menu_item = gtk_menu_item_new ();
1170         gtk_widget_show (menu_item);
1171         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1172         gtk_widget_set_sensitive (menu_item, FALSE);
1173
1174         menu_item = gtk_menu_item_new_with_mnemonic ("_Visit terminatorX.cx");
1175         gtk_widget_show (menu_item);
1176         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1177         g_signal_connect(menu_item, "activate", (GCallback) display_browser, NULL);
1178 }
1179
1180 void create_mastergui(int x, int y)
1181 {
1182         GtkWidget *mother_of_all_boxen;
1183         GtkWidget *main_vbox;
1184         GtkWidget *right_hbox;
1185         GtkWidget *left_hbox;
1186         GtkWidget *control_box;
1187         //GtkWidget *sequencer_box;
1188         GtkAdjustment *dumadj;
1189         GtkWidget *dummy;
1190         GtkWidget *master_vol_box;
1191         GtkWidget *status_box;
1192         
1193         gui_tooltips=gtk_tooltips_new();
1194
1195         main_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
1196
1197         gtk_window_set_wmclass(GTK_WINDOW(main_window), "terminatorX", "tX_mastergui");
1198
1199         gtk_window_set_title(GTK_WINDOW(main_window), "terminatorX");
1200         
1201         gtk_container_set_border_width(GTK_CONTAINER(main_window), 5);
1202
1203         gtk_widget_realize(main_window);
1204         
1205         mother_of_all_boxen=gtk_vbox_new(FALSE, 5);
1206         gtk_container_add(GTK_CONTAINER(main_window), mother_of_all_boxen);
1207         gtk_widget_show(mother_of_all_boxen);   
1208         
1209         main_menubar=gtk_menu_bar_new();
1210         gtk_box_pack_start(GTK_BOX(mother_of_all_boxen), main_menubar, WID_FIX);
1211         gtk_widget_show(main_menubar);  
1212         
1213         create_master_menu();
1214         
1215         main_vbox=gtk_hbox_new(FALSE, 5);
1216         gtk_box_pack_start(GTK_BOX(mother_of_all_boxen), main_vbox, WID_DYN);
1217         gtk_widget_show(main_vbox);
1218         
1219         left_hbox=gtk_vbox_new(FALSE, 5);
1220         gtk_box_pack_start(GTK_BOX(main_vbox), left_hbox, WID_DYN);
1221         gtk_widget_show(left_hbox);
1222         
1223         control_box=gtk_hbox_new(FALSE, 5);
1224         gtk_box_pack_start(GTK_BOX(left_hbox), control_box, WID_FIX);
1225         gtk_widget_show(control_box);
1226         
1227         dummy=gtk_hseparator_new();
1228         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_FIX);
1229         gtk_widget_show(dummy);
1230
1231     /* control_box contents */
1232
1233         dummy=tx_xpm_label_box(TX_ICON_AUDIOENGINE, "Audio");
1234         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1235         gtk_widget_show(dummy);
1236         
1237         dummy=tx_xpm_button_new(TX_ICON_POWER,"Power ", 1);
1238         connect_button(dummy,audio_on, NULL);
1239         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1240         gui_set_tooltip(dummy, "Turn the audio engine on/off.");
1241         gtk_widget_show(dummy);
1242         engine_btn=dummy;
1243         
1244         grab_button=tx_xpm_button_new(TX_ICON_GRAB, "Mouse Grab ", 1);
1245         gtk_box_pack_start(GTK_BOX(control_box), grab_button, WID_FIX);
1246         connect_button(grab_button, grab_on, NULL);
1247         gui_set_tooltip(grab_button, "Enter the mouse grab mode operation. Press <ESCAPE> to exit grab mode.");
1248         gtk_widget_show(grab_button);
1249
1250         dummy=gtk_vseparator_new();
1251         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1252         gtk_widget_show(dummy);
1253     
1254         dummy=tx_xpm_label_box(TX_ICON_SEQUENCER, "Seq.");
1255         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1256         gtk_widget_show(dummy);
1257
1258         dummy=tx_xpm_button_new(TX_ICON_PLAY,"Play ", 1);
1259         connect_button(dummy, seq_play, NULL);
1260         seq_play_btn=dummy;
1261         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1262         gui_set_tooltip(dummy, "Playback previously recorded events from the sequencer. This will turn on the audio engine automagically.");
1263         gtk_widget_show(dummy);
1264
1265         dummy=tx_xpm_button_new(TX_ICON_STOP,"Stop ", 0);
1266         seq_stop_btn=dummy;
1267         connect_button(dummy, seq_stop, NULL);  
1268         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1269         gui_set_tooltip(dummy, "Stop the playback of sequencer events.");
1270         gtk_widget_show(dummy);
1271
1272         dummy=tx_xpm_button_new(TX_ICON_RECORD,"Record ", 1);
1273         connect_button(dummy, seq_rec, NULL);
1274         seq_rec_btn=dummy;
1275         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1276         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.");
1277         gtk_widget_show(dummy);
1278
1279         dummy=gtk_label_new("Pos:");
1280         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1281         gtk_widget_show(dummy);
1282         
1283         dummy=gtk_entry_new();
1284         gtk_entry_set_max_length(GTK_ENTRY(dummy), 12);
1285         seq_entry=dummy;
1286         //gtk_widget_set_usize(dummy, 65, 20);
1287         gtk_entry_set_text(GTK_ENTRY(dummy), "00:00.00");
1288         gtk_entry_set_width_chars(GTK_ENTRY(dummy), 9);
1289         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1290         gtk_widget_show(dummy);
1291
1292         dumadj=(GtkAdjustment*) gtk_adjustment_new(0, 0, 100, 0.1, 1, 1);
1293         seq_adj=dumadj;
1294         connect_adj(dumadj, sequencer_move, NULL);      
1295         dummy=gtk_hscale_new(dumadj);
1296         gtk_widget_set_size_request(dummy, 65, 20);
1297         seq_slider=dummy;
1298         g_signal_connect(G_OBJECT(seq_slider), "button-release-event", (GtkSignalFunc) seq_slider_released, NULL);
1299         gtk_scale_set_draw_value(GTK_SCALE(dummy), FALSE);
1300         
1301         gui_set_tooltip(dummy, "Select the start position for the sequencer in song-time.");
1302         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_DYN);
1303         gtk_widget_show(dummy);
1304         
1305         dummy=gtk_hbox_new(FALSE,2); //gtk_hpaned_new ();
1306         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_DYN);
1307         gtk_widget_show(dummy);
1308         
1309         tt_parent=dummy;
1310
1311     panel_bar=gtk_hbox_new(TRUE,2);
1312         gtk_box_pack_start(GTK_BOX(left_hbox), panel_bar, WID_FIX);
1313
1314         control_parent=gtk_hbox_new(FALSE,0);
1315         gtk_box_pack_start(GTK_BOX(tt_parent), control_parent, WID_FIX);
1316         gtk_widget_show(control_parent);
1317
1318         dummy=gtk_vseparator_new();
1319         gtk_box_pack_start(GTK_BOX(tt_parent), dummy, WID_FIX);
1320         gtk_widget_show(dummy);
1321
1322         audio_parent=gtk_vbox_new(FALSE,0);
1323         gtk_box_pack_start(GTK_BOX(tt_parent), audio_parent, WID_DYN);
1324         gtk_widget_show(audio_parent);
1325         
1326         dummy=gtk_vseparator_new();
1327         gtk_box_pack_start(GTK_BOX(main_vbox), dummy, WID_FIX);
1328         gtk_widget_show(dummy);
1329                 
1330         right_hbox=gtk_vbox_new(FALSE, 5);
1331         gtk_box_pack_start(GTK_BOX(main_vbox), right_hbox, WID_FIX);
1332         gtk_widget_show(right_hbox);
1333
1334         /* Master */
1335         
1336         dummy=gtk_label_new(NULL);
1337         gtk_label_set_markup(GTK_LABEL(dummy),"<b>Master</b>");
1338         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1339         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1340         gtk_widget_show(dummy); 
1341
1342         dummy=gtk_hseparator_new();
1343         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1344         gtk_widget_show(dummy);
1345
1346          /* Pitch */
1347          
1348         /*dummy=gtk_label_new("Pitch:");
1349         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1350         gtk_widget_show(dummy);*/
1351
1352         dumadj=(GtkAdjustment*) gtk_adjustment_new(globals.pitch, -3, 3, 0.001, 0.001, 0.01);
1353         pitch_adj=dumadj;
1354         connect_adj(dumadj, master_pitch_changed, NULL);
1355         
1356         tX_extdial *pdial=new tX_extdial("Pitch", pitch_adj, &sp_master_pitch, true);
1357         gtk_box_pack_start(GTK_BOX(right_hbox), pdial->get_widget(), WID_FIX);
1358         gui_set_tooltip(pdial->get_entry(), "Use this dial to adjust the master pitch (affecting *all* turntables).");
1359         
1360         dummy=gtk_hseparator_new();
1361         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1362         gtk_widget_show(dummy);
1363         
1364         /* Volume */
1365         master_vol_box=gtk_hbox_new(FALSE, 2);
1366         gtk_box_pack_start(GTK_BOX(right_hbox), master_vol_box, WID_DYN);
1367         gtk_widget_show(master_vol_box);        
1368         
1369         dumadj=(GtkAdjustment*) gtk_adjustment_new(globals.volume, 0, 2, 0.01, 0.05, 0.005);
1370         volume_adj=dumadj;
1371
1372         connect_adj(dumadj, master_volume_changed, NULL);       
1373         dummy=gtk_vscale_new(dumadj);
1374         gtk_range_set_inverted(GTK_RANGE(dummy), TRUE);
1375         gtk_scale_set_draw_value(GTK_SCALE(dummy), False);
1376         g_signal_connect(G_OBJECT(dummy), "button_press_event", (GtkSignalFunc) tX_seqpar::tX_seqpar_press, &sp_master_volume); 
1377         
1378         gtk_box_pack_end(GTK_BOX(master_vol_box), dummy, WID_FIX);
1379         gtk_widget_show(dummy); 
1380         gui_set_tooltip(dummy, "Adjust the master volume. This parameter will effect *all* turntables in the set.");
1381         
1382         main_flash_r=gtk_tx_flash_new();
1383         gtk_box_pack_end(GTK_BOX(master_vol_box), main_flash_r, WID_DYN);
1384         gtk_widget_show(main_flash_r);
1385
1386         main_flash_l=gtk_tx_flash_new();
1387         gtk_box_pack_end(GTK_BOX(master_vol_box), main_flash_l, WID_DYN);
1388         gtk_widget_show(main_flash_l);
1389
1390         dummy=gtk_label_new("Volume");
1391         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1392         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1393         gtk_widget_show(dummy);
1394
1395         /* STATUS BOX */ 
1396         dummy=gtk_hseparator_new();
1397         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1398         gtk_widget_show(dummy);
1399         
1400         status_box=gtk_vbox_new(FALSE, 0);
1401         gtk_box_pack_start(GTK_BOX(right_hbox), status_box, WID_FIX);
1402         gtk_widget_show(status_box);
1403         
1404         dummy=gtk_label_new("0");
1405         used_mem=dummy;
1406         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1407         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1408         gtk_widget_show(dummy);
1409
1410         dummy=gtk_label_new("Mem/kB:");
1411         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1412         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1413         gtk_widget_show(dummy);
1414         
1415         add_sep2();
1416
1417         dummy=gtk_label_new("1");
1418         no_of_vtts=dummy;
1419         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1420         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1421         gtk_widget_show(dummy);
1422
1423         dummy=gtk_label_new("Vtts:");
1424         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1425         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1426         gtk_widget_show(dummy);
1427
1428         add_sep2();
1429
1430         dummy=gtk_label_new(VERSION);
1431         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1432         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1433         gtk_widget_show(dummy);
1434
1435         dummy=gtk_label_new("Release:");
1436         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1437         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1438         gtk_widget_show(dummy);
1439         
1440         add_sep2();
1441
1442         dummy=gtk_label_new(NULL);
1443         gtk_label_set_markup(GTK_LABEL(dummy), "<b>Status</b>");
1444         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1445         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1446         gtk_widget_show(dummy);
1447         
1448         /* END GUI */
1449         
1450         gtk_window_set_default_size(GTK_WINDOW(main_window), x, y);     
1451         gtk_widget_set_sensitive(grab_button, 0);
1452
1453         new_table(NULL, NULL); // to give the user something to start with ;)
1454
1455         g_signal_connect (G_OBJECT(main_window), "delete-event", (GtkSignalFunc) quit, NULL);   
1456         
1457         if (globals.tooltips) gtk_tooltips_enable(gui_tooltips);
1458         else gtk_tooltips_disable(gui_tooltips);
1459 }
1460
1461 gfloat old_percent=-1;
1462
1463 void note_destroy(GtkWidget *widget, GtkWidget *mbox)
1464 {
1465         gtk_widget_destroy(GTK_WIDGET(mbox));
1466 }
1467
1468 void tx_note(const char *message, bool isError)
1469 {
1470         char buffer[4096]="terminatorX ";
1471         if (isError) {
1472                 strcat(buffer, "note:\n\n");
1473         } else {
1474                 strcat(buffer, "error:\n\n");
1475         }
1476         
1477         strcat(buffer, message);
1478         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window),
1479                 GTK_DIALOG_DESTROY_WITH_PARENT,
1480                 isError ? GTK_MESSAGE_ERROR : GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, message);
1481         gtk_dialog_run(GTK_DIALOG(dialog));
1482         gtk_widget_destroy(dialog);     
1483 }
1484
1485
1486 void tx_l_note(const char *message)
1487 {
1488         char buffer[4096]="Plugin info:\n\n";
1489         strcat(buffer, message);
1490         
1491         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window),
1492                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, message);
1493         gtk_dialog_run(GTK_DIALOG(dialog));
1494         gtk_widget_destroy(dialog);     
1495 }
1496
1497
1498 void add_to_panel_bar(GtkWidget *button) {
1499         buttons_on_panel_bar++;
1500         gtk_box_pack_start(GTK_BOX(panel_bar), button, WID_DYN);
1501         gtk_widget_show(panel_bar);
1502 }
1503
1504 void remove_from_panel_bar(GtkWidget *button) {
1505         buttons_on_panel_bar--;
1506         gtk_container_remove(GTK_CONTAINER(panel_bar), button);
1507         if (buttons_on_panel_bar==0) gtk_widget_hide(panel_bar);
1508 }
1509
1510 /* Fullscreen code... */
1511 #define _WIN_LAYER_TOP          -1
1512 #define _WIN_LAYER_NORMAL       4
1513 #define _NET_WM_STATE_REMOVE    0
1514 #define _NET_WM_STATE_ADD       1
1515 #define _NET_WM_STATE_TOGGLE    2
1516
1517 void fullscreen_toggle(GtkCheckMenuItem *item, gpointer data) {
1518         XEvent xev;
1519         Window win=GDK_WINDOW_XID(main_window->window);
1520         Display *disp=GDK_WINDOW_XDISPLAY(main_window->window);
1521         
1522         globals.fullscreen_enabled=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(fullscreen_item));
1523         
1524         /* Top layer.. */
1525         xev.xclient.type = ClientMessage;
1526         xev.xclient.serial = 0;
1527         xev.xclient.send_event = True;
1528         xev.xclient.display = disp;
1529         xev.xclient.window = win;
1530         xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_WIN_LAYER");
1531         xev.xclient.format = 32;
1532         xev.xclient.data.l[0] = globals.fullscreen_enabled ? _WIN_LAYER_TOP : _WIN_LAYER_NORMAL ;
1533         XSendEvent(disp, GDK_WINDOW_XID (gdk_get_default_root_window ()),
1534                 False, SubstructureRedirectMask | SubstructureNotifyMask,
1535                 &xev);
1536         
1537         /* Fullscreen */
1538         xev.xclient.type = ClientMessage;
1539         xev.xclient.serial = 0;
1540         xev.xclient.send_event = True;
1541         xev.xclient.display = disp;
1542         xev.xclient.window = win;
1543         xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_NET_WM_STATE");
1544         xev.xclient.format = 32;
1545         xev.xclient.data.l[0] = globals.fullscreen_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1546         xev.xclient.data.l[1] = gdk_x11_atom_to_xatom (gdk_atom_intern ("_NET_WM_STATE_FULLSCREEN", TRUE));
1547         xev.xclient.data.l[2] = gdk_x11_atom_to_xatom (GDK_NONE);
1548         XSendEvent(gdk_display, GDK_WINDOW_XID (gdk_get_default_root_window ()),
1549                 False, SubstructureRedirectMask | SubstructureNotifyMask,
1550                 &xev);  
1551 }
1552
1553 void fullscreen_setup() {
1554         if (globals.fullscreen_enabled) {
1555                 fullscreen_toggle(NULL, NULL);
1556         }
1557 }
1558
1559 void display_mastergui()
1560 {
1561         GtkWidget *top;
1562         gtk_widget_realize(main_window);
1563         tX_set_icon(main_window);
1564         load_knob_pixs();
1565         gtk_widget_show(main_window);
1566         fullscreen_setup();     
1567         top=gtk_widget_get_toplevel(main_window);
1568         xwindow=GDK_WINDOW_XWINDOW(top->window);
1569 }
1570
1571 pid_t help_child=0;
1572 GTimer *help_timer=NULL;
1573 int help_tag=-1;
1574
1575 int help_checker() {
1576         gdouble time;
1577         gulong ms;
1578         int status;
1579         int result=waitpid(help_child, &status, WNOHANG);
1580         
1581         if (result==0) {
1582                 time=g_timer_elapsed(help_timer, &ms);
1583                 if (time > 5) {
1584                         /* 5 seconds and it's still running - so we assume everything's OK. */
1585                         tX_debug("No longer waiting for gnome-help..");
1586                         gtk_idle_remove(help_tag);
1587                         help_tag=-1;
1588                 }
1589         } else {
1590                 /* We are still here and the child exited - that could mean trouble. */
1591                 tx_note("Couldn't run the gnome-help command (alias \"yelp\") to display the terminatorX manual. Please ensure that \"yelp\" is installed.", true);             
1592                 
1593                 gtk_idle_remove(help_tag);
1594                 help_tag=-1;
1595         }
1596         return TRUE;    
1597 }
1598
1599 #ifndef INSTALL_PREFIX
1600 #define INSTALL_PREFIX "/usr/local/share"
1601 #endif
1602
1603 void display_help() {   
1604         help_child=fork();
1605
1606         if (help_tag!=-1) {
1607                 gtk_idle_remove(help_tag);
1608                 if (help_timer) g_timer_destroy(help_timer);
1609                 help_child=0;
1610                 help_tag=-1;
1611                 help_timer=NULL;
1612         }
1613         
1614         if (help_child==0) {
1615                 // child
1616                 // execlp("gnome-help","gnome-help","ghelp:/" INSTALL_PREFIX "/terminatorX/doc/terminatorX-manual/C/terminatorX-manual.xml", NULL);
1617                 execlp("gnome-help","gnome-help","ghelp:/" XML_MANUAL, NULL);           
1618                 _exit(-1);
1619         } else if (help_child==-1) {
1620                 tx_note("System error: couldn't fork() to run the help process.", true);
1621         } else {
1622                 help_timer=g_timer_new();
1623                 g_timer_start(help_timer);
1624         
1625                 help_tag=gtk_idle_add((GtkFunction) help_checker, NULL);
1626         }
1627 }
1628
1629 pid_t browser_child=0;
1630 GTimer *browser_timer=NULL;
1631 int browser_tag=-1;
1632
1633 int browser_checker() {
1634         gdouble time;
1635         gulong ms;
1636         int status;
1637         int result=waitpid(browser_child, &status, WNOHANG);
1638         
1639         if (result==0) {
1640                 time=g_timer_elapsed(browser_timer, &ms);
1641                 if (time > 5) {
1642                         /* 5 seconds and it's still running - so we assume everything's OK. */
1643                         tX_debug("No longer waiting for a browser..");
1644                         gtk_idle_remove(browser_tag);
1645                         browser_tag=-1;
1646                 }
1647         } else {
1648                 /* We are still here and the child exited - that could mean trouble. */
1649                 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);         
1650                 
1651                 gtk_idle_remove(browser_tag);
1652                 browser_tag=-1;
1653         }
1654         return TRUE;    
1655 }
1656
1657 void display_browser() {        
1658         browser_child=fork();
1659
1660         if (browser_tag!=-1) {
1661                 gtk_idle_remove(browser_tag);
1662                 if (browser_timer) g_timer_destroy(browser_timer);
1663                 browser_child=0;
1664                 browser_tag=-1;
1665                 browser_timer=NULL;
1666         }
1667         
1668         if (browser_child==0) {
1669                 // child
1670                 execlp("mozilla","mozilla","http://terminatorX.cx", NULL);
1671                 execlp("netscape","netscape","http://terminatorX.cx", NULL);
1672                 execlp("galeon","galeon","http://terminatorX.cx", NULL);
1673                 execlp("konqueror","konqueror","http://terminatorX.cx", NULL);          
1674                 _exit(-1);
1675         } else if (browser_child==-1) {
1676                 tx_note("System error: couldn't fork() to run the browser process.", true);
1677         } else {
1678                 browser_timer=g_timer_new();
1679                 g_timer_start(browser_timer);
1680         
1681                 browser_tag=gtk_idle_add((GtkFunction) browser_checker, NULL);
1682         }
1683 }
1684
1685
1686
1687 GdkCursor *tX_cursor::cursors[MAX_CURSOR]={NULL, NULL, NULL};
1688 tX_cursor::cursor_shape tX_cursor::current_shape=tX_cursor::DEFAULT_CURSOR;
1689
1690 void tX_cursor::set_cursor(cursor_shape shape)
1691 {
1692         switch (shape) {
1693                 case DEFAULT_CURSOR:
1694                         cursors[shape]=NULL;
1695                         break;
1696                 
1697                 case WAIT_CURSOR:
1698                         if (!cursors[shape]) cursors[shape]=gdk_cursor_new(GDK_WATCH);
1699                         break;
1700                 
1701                 case WAIT_A_SECOND_CURSOR:
1702                         /* FIXME: What's that short-time wait cursor's id? */
1703                         if (!cursors[shape]) cursors[shape]=gdk_cursor_new(GDK_WATCH);
1704                         break;
1705                 
1706                 default:
1707                         tX_debug("No such cursor shape.");
1708                         return;
1709         }
1710         
1711         /* Still here? Ok... */
1712         current_shape=shape;
1713         
1714         gdk_window_set_cursor(main_window->window, cursors[shape]);
1715 }
1716
1717 GdkCursor *tX_cursor::get_cursor()
1718 {
1719         return cursors[current_shape];
1720 }
1721
1722 void tX_cursor::reset_cursor()
1723 {
1724         current_shape=DEFAULT_CURSOR;
1725         gdk_window_set_cursor(main_window->window, cursors[current_shape]);
1726 }