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