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