aab78e8cd8939ce004480f03b62df2759288cb56
[terminatorX.git] / src / tX_mastergui.cc
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999-2014  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, see <http://www.gnu.org/licenses/>.
17  
18     File: tX_mastergui.cc
19  
20     Description: This implements the main (aka master) gtk+ GUI of terminatorX
21                  It serves as a container for the vtt-guis.
22 */    
23
24 #include <gtk/gtk.h>
25 #include <gdk/gdkx.h>
26 #include <X11/Xlib.h>
27 #include <math.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <gdk/gdkkeysyms.h>
31 #include "version.h"
32 #include "tX_global.h"
33 #include "tX_engine.h"
34 #include "tX_vttgui.h"
35 #include "tX_vtt.h"
36 #include "tX_flash.h"
37 #include "tX_dialog.h"
38 #include "tX_loaddlg.h"
39 #include "tX_seqpar.h"
40 #include "tX_pbutton.h"
41 #include "tX_sequencer.h"
42 #include "tX_mastergui.h"
43 #include "tX_knobloader.h"
44 #include "tX_ui_interface.h"
45 #include "tX_ui_support.h"
46 #include <sys/time.h>
47 #include <sys/wait.h>
48 #include "tX_midiin.h"
49 #include "tX_mouse.h"
50
51 #ifdef USE_SCHEDULER
52 #include <sys/resource.h>
53 #endif
54
55 #define TX_SET_ID_10 "terminatorX turntable set file - version 1.0 - data:"
56 #define TX_SET_ID_11 "terminatorX turntable set file - version 1.1 - data:"
57 #define TX_SET_ID_12 "terminatorX turntable set file - version 1.2 - data:"
58 #define TX_SET_ID_13 "terminatorX turntable set file - version 1.3 - data:"
59 #define TX_SET_ID_14 "terminatorX turntable set file - version 1.4 - data:"
60
61 int audioon=0;
62 int sequencer_ready=1;
63
64 bool tX_shutdown=false;
65 tx_mouse mouse;
66
67 GtkWidget *tt_parent;
68 GtkWidget *control_parent;
69 GtkWidget *audio_parent;
70 GtkWidget *main_window;
71 GtkWidget *grab_button;
72 GtkWidget *main_flash;
73
74 GtkWidget *seq_rec_btn;
75 GtkWidget *seq_play_btn;
76 GtkWidget *seq_stop_btn;
77 GtkAdjustment *seq_adj;
78 GtkWidget *seq_slider;
79 GtkWidget *seq_entry;
80 GtkWidget *panel_bar;
81
82 int buttons_on_panel_bar=0;
83
84 int seq_adj_care=1;
85 int seq_stop_override=0;
86
87 GtkAdjustment *volume_adj;
88 GtkAdjustment *pitch_adj;
89
90 /* seq-pars */
91 tX_seqpar_master_volume sp_master_volume;
92 tX_seqpar_master_pitch sp_master_pitch;
93
94 GtkWidget *engine_btn;
95
96 GtkWidget *main_menubar;
97 GtkWidget *rec_menu_item;
98 GtkWidget *fullscreen_item;
99
100 int rec_dont_care=0;
101 gint update_tag;
102
103 #define connect_entry(wid, func, ptr); g_signal_connect(G_OBJECT(wid), "activate", (GCallback) func, (void *) ptr);
104 #define connect_adj(wid, func, ptr); g_signal_connect(G_OBJECT(wid), "value_changed", (GCallback) func, (void *) ptr);
105 #define connect_button(wid, func, ptr); g_signal_connect(G_OBJECT(wid), "clicked", (GCallback) func, (void *) ptr);
106
107 Window x_window;
108 GdkWindow* top_window;
109 #define WID_DYN TRUE, TRUE, 0
110 #define WID_FIX FALSE, FALSE, 0
111 extern void add_vtt(GtkWidget *ctrl, GtkWidget *audio, char *fn);
112 extern void destroy_gui(vtt_class *vtt);
113 extern void gui_show_frame(vtt_class *vtt, int show);
114
115 #ifndef USE_FILECHOOSER
116 GdkWindow *save_dialog_win=NULL;
117 GdkWindow *load_dialog_win=NULL;
118
119 GtkWidget *save_dialog=NULL;
120 GtkWidget *load_dialog=NULL;
121 #endif
122
123 GdkWindow *rec_dialog_win=NULL;
124 GtkWidget *rec_dialog=NULL;
125
126 //GtkWidget *no_of_vtts=NULL;
127 GtkWidget *used_mem=NULL;
128
129 int stop_update=0;
130 int update_delay;
131
132 vtt_class *old_focus=NULL;
133
134 int grab_status=0;
135 int last_grab_status=0;
136
137 GtkWidget *delete_all_item=NULL;
138 GtkWidget *delete_all_vtt_item=NULL;
139
140 void gui_set_tooltip(GtkWidget *wid, const char *tip)
141 {
142         gtk_widget_set_tooltip_text(wid, tip);
143 }
144
145 void turn_audio_off(void)
146 {
147         if (audioon) {
148                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 0);
149                 while (gtk_events_pending()) gtk_main_iteration();              
150         }
151 }
152
153 gint pos_update(gpointer data)
154 {
155         f_prec temp,temp2;
156
157         if (stop_update) {              
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);
163                 gdk_flush();    
164                 update_tag=0;
165                 return FALSE;
166         } else {
167                 update_all_vtts();
168                 
169                 /*main vu meter */
170                 temp=vtt_class::mix_max_l;
171                 vtt_class::mix_max_l=0;
172                 temp2=vtt_class::mix_max_r;
173                 vtt_class::mix_max_r=0;
174                 gtk_tx_flash_set_level(main_flash, temp/FL_SHRT_MAX, temp2/FL_SHRT_MAX);
175
176                 if (vtt_class::focused_vtt!=old_focus) {
177                         if (old_focus) gui_show_frame(old_focus, 0);
178                         old_focus=vtt_class::focused_vtt;
179                         if (old_focus) gui_show_frame(old_focus, 1);                    
180                 }
181
182                 grab_status = mouse.is_grabbed();
183
184                 if (grab_status!=last_grab_status) {
185                         last_grab_status=grab_status;
186                         if (!grab_status) {
187                                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(grab_button), 0);
188                         }
189                 }
190                 gdk_flush();    
191                 update_delay--;
192                 
193                 if (update_delay < 0) {
194                         seq_update();
195                         tX_seqpar::update_all_graphics();
196                         update_delay=globals.update_delay;
197                 }
198                 
199                 if (tX_engine::get_instance()->check_error()) {
200                         tX_error("ouch - error while playback...");
201                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 0);                 
202                         return FALSE;
203                 }               
204                 
205                 // let the audio engine we got the chance to do something
206                 tX_engine::get_instance()->reset_cycles_ctr();
207                 
208                 return TRUE;
209         }
210 }
211
212 #define TX_MEMTAG "VmData:"
213
214 void mg_update_status()
215 {
216         FILE *procfs;
217         pid_t mypid;
218         char filename[PATH_MAX];
219         char buffer[256];
220         int found=0;    
221         int mem;
222         
223         mypid=getpid();
224         sprintf(filename, "/proc/%i/status", mypid);
225         procfs=fopen(filename, "r");
226         if (procfs) {
227                 while((!feof(procfs)) && !found) {
228                         if (fgets(buffer, 256, procfs) && (strncmp(TX_MEMTAG, buffer, sizeof(TX_MEMTAG)-1)==0)) {
229                                 found=1;
230                                 sscanf(buffer, TX_MEMTAG" %i kB", &mem);
231                                 sprintf(buffer, "%.1lf M", ((double) mem)/1024.0);
232                                 gtk_label_set_text(GTK_LABEL(used_mem), buffer);
233                         }
234                 }
235                 fclose(procfs); 
236         } else {
237                 gtk_label_set_text(GTK_LABEL(used_mem), "");
238         }
239         
240         /*sprintf(buffer, "%i", vtt_class::vtt_amount);
241         gtk_label_set_text(GTK_LABEL(no_of_vtts), buffer);*/
242 }
243
244 GCallback new_table(GtkWidget *, char *fn)
245 {
246         //turn_audio_off();
247                 
248         if (fn) {
249                 ld_create_loaddlg(TX_LOADDLG_MODE_SINGLE, 1);
250                 ld_set_filename(fn);
251         }
252         
253         add_vtt(control_parent, audio_parent, fn);                              
254         
255         if (fn) ld_destroy();           
256         mg_update_status();
257         
258 #ifdef USE_ALSA_MIDI_IN
259         if (globals.auto_assign_midi) tX_midiin::auto_assign_midi_mappings(NULL, NULL);
260 #endif
261         
262         return NULL;
263 }
264
265 bool tx_mg_have_setname=false;
266 char tx_mg_current_setname[PATH_MAX]="";
267
268 GCallback new_tables() {
269         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window), 
270                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
271                 "Are you sure you want to lose all turntables and events?");
272         
273         int res=gtk_dialog_run(GTK_DIALOG(dialog));
274         gtk_widget_destroy(dialog);
275                 
276         if (res!=GTK_RESPONSE_YES) {
277                 return NULL;
278         }
279
280         vtt_class::delete_all();
281         new_table(NULL, NULL);
282
283 #ifdef USE_ALSA_MIDI_IN
284         if (globals.auto_assign_midi) tX_midiin::auto_assign_midi_mappings(NULL, NULL);
285 #endif
286         
287         gtk_window_set_title(GTK_WINDOW(main_window), "terminatorX");
288
289         return NULL;
290 }
291
292 /* Loading saved setups */
293 #ifndef USE_FILECHOOSER
294 GCallback 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 #endif
302
303 void load_tt_part(char * buffer)
304 {
305         char wbuf[PATH_MAX];
306         xmlDocPtr doc;
307 #ifdef ENABLE_TX_LEGACY
308         FILE *in;
309 #endif  
310         turn_audio_off();
311         
312         strncpy(globals.tables_filename, buffer, sizeof(globals.tables_filename));
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                         char idbuff[256];
326                         
327                         fread(idbuff, strlen(TX_SET_ID_10), 1, in);
328                         if (strncmp(idbuff, TX_SET_ID_10, strlen(TX_SET_ID_10))==0) {
329                                 if (vtt_class::load_all_10(in, buffer)) tx_note("Error while reading set.", true);
330                         } else if (strncmp(idbuff, TX_SET_ID_11, strlen(TX_SET_ID_11))==0)      {
331                                 if (vtt_class::load_all_11(in, buffer)) tx_note("Error while reading set.", true);                      
332                         } else if (strncmp(idbuff, TX_SET_ID_12, strlen(TX_SET_ID_12))==0) {
333                                 if (vtt_class::load_all_12(in, buffer)) tx_note("Error while reading set.", true);                      
334                         } else if (strncmp(idbuff, TX_SET_ID_13, strlen(TX_SET_ID_13))==0) {
335                                 if (vtt_class::load_all_13(in, buffer)) tx_note("Error while reading set.", true);                      
336                         } else if (strncmp(idbuff, TX_SET_ID_14, strlen(TX_SET_ID_14))==0) {
337                                 if (vtt_class::load_all_14(in, buffer)) tx_note("Error while reading set.", true);                      
338                         }       else {
339                                 tx_note("This file is not a terminatorX set-file.", true);
340                                 fclose(in);
341                                 return;
342                         }
343                         fclose(in);     
344                 } else {
345                         char message[PATH_MAX+256];
346                         sprintf(message, "Failed to acesss file: \"%s\"", globals.tables_filename);
347                         tx_note(message, true);
348                         
349                         return;
350                 }
351         }
352 #else
353         else {
354                 char message[PATH_MAX+256];
355                 sprintf(message, "Failed to acesss file: \"%s\"", globals.tables_filename);
356                 tx_note(message, 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 #ifdef USE_ALSA_MIDI_IN
372         if (globals.auto_assign_midi) tX_midiin::auto_assign_midi_mappings(NULL, NULL);
373 #endif  
374         gtk_window_set_title(GTK_WINDOW(main_window), wbuf);            
375 }
376
377 #ifndef USE_FILECHOOSER
378 void do_load_tables(GtkWidget *wid)
379 {
380         char buffer[PATH_MAX];
381         
382         strcpy(buffer, gtk_file_selection_get_filename(GTK_FILE_SELECTION(load_dialog)));
383         
384         gtk_widget_destroy(load_dialog);
385         
386         load_dialog=NULL;
387         load_dialog_win=NULL;
388
389         tX_cursor::set_cursor(tX_cursor::WAIT_CURSOR);
390         load_tt_part(buffer);
391         tX_cursor::reset_cursor();
392 }
393 #endif
394
395 GCallback load_tables()
396 {
397 #ifdef USE_FILECHOOSER  
398         GtkWidget * dialog = gtk_file_chooser_dialog_new ("Open Set File",
399                 GTK_WINDOW(main_window), GTK_FILE_CHOOSER_ACTION_OPEN,
400                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,  
401             GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
402                                       
403
404         GtkFileFilter *filter=gtk_file_filter_new();
405         gtk_file_filter_add_pattern (filter, "*.tX");
406         gtk_file_filter_add_pattern (filter, "*.tx");
407         gtk_file_filter_set_name(filter, "terminatorX Set Files");
408         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter); 
409
410         filter=gtk_file_filter_new();
411         gtk_file_filter_add_pattern (filter, "*");
412         gtk_file_filter_set_name(filter, "All Files");
413         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter); 
414         
415         if (strlen(globals.tables_filename)) {
416                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), globals.tables_filename);
417         }       
418         
419         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
420         char * filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
421                 gtk_widget_hide(dialog);
422                 tX_cursor::set_cursor(tX_cursor::WAIT_CURSOR);
423                 load_tt_part(filename);
424                 tX_cursor::reset_cursor();              
425         }       
426         
427         gtk_widget_destroy(dialog);
428 #else
429         if (load_dialog_win) {
430                 gdk_window_raise(load_dialog_win);
431                 return 0;
432         }
433
434         load_dialog=gtk_file_selection_new("Load Set"); 
435         
436         gtk_file_selection_show_fileop_buttons(GTK_FILE_SELECTION(load_dialog));
437         gtk_file_selection_complete(GTK_FILE_SELECTION(load_dialog), "*.tX");
438         
439         if (strlen(globals.tables_filename)) {
440                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(load_dialog), globals.tables_filename);
441         }
442         
443         gtk_widget_show(load_dialog);
444         
445         load_dialog_win=load_dialog->window;
446         
447         g_signal_connect (G_OBJECT(GTK_FILE_SELECTION(load_dialog)->ok_button), "clicked", G_CALLBACK(do_load_tables), NULL);
448         g_signal_connect (G_OBJECT(GTK_FILE_SELECTION(load_dialog)->cancel_button), "clicked", G_CALLBACK (cancel_load_tables), NULL);  
449         g_signal_connect (G_OBJECT(load_dialog), "delete-event", G_CALLBACK(cancel_load_tables), NULL); 
450         
451 #endif
452
453         return NULL;
454 }
455
456 vtt_class* choose_vtt() {
457         GtkWidget *dialog = gtk_dialog_new_with_buttons("Select Turntable",
458                 GTK_WINDOW(main_window), GTK_DIALOG_MODAL, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
459                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);       
460
461         GtkWidget *label = gtk_label_new ("Select turntable to load audio file to:");
462         gtk_widget_show(label);
463
464         gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area(GTK_DIALOG(dialog))), label);
465         gtk_container_set_border_width(GTK_CONTAINER (gtk_dialog_get_content_area(GTK_DIALOG(dialog))), 10);
466         
467         list <GtkWidget *> radio_buttons;
468         list <vtt_class *> :: iterator iter;
469         bool first = true;
470         GtkWidget *radio;
471         vtt_class *res_vtt = NULL;
472         
473         pthread_mutex_lock(&vtt_class::render_lock);
474         
475         for (iter = vtt_class::main_list.begin(); iter!=vtt_class::main_list.end(); iter++)  {
476                 if (first) {
477                         first = false;
478                         radio = gtk_radio_button_new_with_label(NULL, (*iter)->name);
479                 } else {
480                         radio = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio), (*iter)->name);
481                 }
482                 g_object_set_data(G_OBJECT(radio), "tX_vtt", (*iter));
483                 gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area(GTK_DIALOG(dialog))), radio);
484                 gtk_widget_show(radio);
485                 radio_buttons.push_back(radio);
486         }       
487         
488         pthread_mutex_unlock(&vtt_class::render_lock);
489         
490         // Giving up the lock here is necessary if we want audio to keep running
491         // however it is also wrong. Anyway as it is a modal dialog not too many
492         // evil things can happen. This sounds like some famous last words...
493         
494         gint result = gtk_dialog_run (GTK_DIALOG (dialog));
495         
496         if (result == GTK_RESPONSE_ACCEPT) {
497                 list <GtkWidget *> :: iterator radio;
498                 
499                 for (radio = radio_buttons.begin(); radio!=radio_buttons.end(); radio++) {
500                         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON((*radio)))) {
501                                 res_vtt = (vtt_class*) g_object_get_data(G_OBJECT(*radio), "tX_vtt");
502                         }
503                 }
504         } 
505         
506         gtk_widget_destroy(dialog);
507         
508         return res_vtt;
509 }
510
511
512 GCallback load_audio() {
513         vtt_class *vtt = NULL;
514         
515         pthread_mutex_lock(&vtt_class::render_lock);
516         if (vtt_class::main_list.size()==1) {
517                 vtt=(* vtt_class::main_list.begin());
518         }
519         pthread_mutex_unlock(&vtt_class::render_lock);
520         
521         if (vtt==NULL) {
522                 vtt = choose_vtt();
523         }
524         
525         if (vtt!=NULL) load_file(NULL, vtt);
526         
527         return NULL;
528 }
529
530 GCallback drop_set(GtkWidget *widget, GdkDragContext *context,
531                 gint x, gint y, GtkSelectionData *selection_data,
532                 guint info, guint time, vtt_class *vtt)
533 {
534         char filename[PATH_MAX];
535         char *fn;
536         
537         strncpy(filename, (char *) gtk_selection_data_get_data(selection_data), (size_t) gtk_selection_data_get_length(selection_data));
538         filename[gtk_selection_data_get_length(selection_data)]=0;
539
540         fn = strchr (filename, '\r');
541         *fn=0;  
542         
543         fn = strchr (filename, ':');
544         if (fn) fn++; else fn=(char *) gtk_selection_data_get_data(selection_data);
545         
546         load_tt_part(fn);
547
548         strcpy (filename, "dont segfault workaround ;)");
549         return NULL;
550 }
551
552 /* save tables */
553
554 #ifndef USE_FILECHOOSER
555 GCallback cancel_save_tables(GtkWidget *wid)
556 {
557         gtk_widget_destroy(save_dialog);
558         save_dialog=NULL;
559         save_dialog_win=NULL;
560         return(0);
561 }
562
563 gboolean do_save_from_selection(GtkWidget *wid) {
564         char buffer[PATH_MAX];
565         
566         if (wid) {
567                 strcpy(buffer, gtk_file_selection_get_filename(GTK_FILE_SELECTION(save_dialog)));
568                 int len=strlen(buffer);
569                 if (!len || (buffer[len-1]=='/')) {                     
570                         tx_note("Invalid filename for set file.", true);                        
571                         return FALSE;
572                 }
573                 gtk_widget_destroy(save_dialog);
574                 save_dialog=NULL;
575                 save_dialog_win=NULL;
576         } else {
577                 strcpy(buffer, tx_mg_current_setname);
578         }       
579         
580         do_save_tables(buffer);
581         
582         return TRUE;
583 }
584 #endif
585
586 void do_save_tables(char *buffer)
587 {
588         FILE *out;
589         gzFile zout;
590         char wbuf[PATH_MAX];
591         char *ext;
592         
593         ext=strrchr(buffer, '.');
594         
595         if (ext) {
596                 if (strcmp(ext, ".tX")) strcat(buffer, ".tX");
597         } else {
598                 strcat(buffer, ".tX");
599         }
600
601         tx_mg_have_setname=true;
602         strcpy(tx_mg_current_setname, buffer);
603         strcpy(globals.tables_filename, buffer);
604         
605         if (globals.compress_set_files) {
606                 _store_compress_xml=1;
607                 out=NULL;
608                 zout=gzopen(buffer, "w");
609         } else {
610                 _store_compress_xml=0;
611                 out=fopen(buffer, "w");
612                 zout=NULL;
613         }
614         
615         if (out || zout) {
616                 if (vtt_class::save_all(out, zout)) tx_note("Error while saving set.", true);
617                 if (out) fclose(out); 
618                 else if (zout) gzclose(zout);
619                 sprintf(wbuf,"terminatorX - %s", strip_path(buffer));
620                 gtk_window_set_title(GTK_WINDOW(main_window), wbuf);                            
621         } else {
622                 tx_note("Failed to open file for write access.", true);
623         }
624 }
625
626 GCallback save_tables_as()
627 {
628 #ifdef USE_FILECHOOSER
629         GtkWidget * dialog = gtk_file_chooser_dialog_new ("Save Set",
630                 GTK_WINDOW(main_window), GTK_FILE_CHOOSER_ACTION_SAVE, 
631                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, 
632                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,NULL);
633         
634         if (tx_mg_have_setname) {
635                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER (dialog), tx_mg_current_setname);
636         }
637                                       
638         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
639         char buffer[PATH_MAX];
640                 char *filename=gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
641                 strcpy(buffer, filename);
642                 g_free(filename);
643                 do_save_tables(buffer);
644         }       
645         
646         gtk_widget_destroy(dialog);     
647 #else
648         if (save_dialog_win) {
649                 gtk_widget_destroy(save_dialog);
650                 save_dialog=NULL;
651                 save_dialog_win=NULL;
652         }
653         
654         save_dialog=gtk_file_selection_new("Save Set"); 
655
656         if (tx_mg_have_setname) {
657                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(save_dialog), tx_mg_current_setname);
658         }
659         
660         gtk_widget_show(save_dialog);
661         
662         save_dialog_win=save_dialog->window;
663         
664         g_signal_connect (G_OBJECT(GTK_FILE_SELECTION(save_dialog)->ok_button), "clicked", G_CALLBACK(do_save_from_selection), NULL);
665         g_signal_connect (G_OBJECT(GTK_FILE_SELECTION(save_dialog)->cancel_button), "clicked", G_CALLBACK (cancel_save_tables), NULL);  
666         g_signal_connect (G_OBJECT(save_dialog), "delete-event", G_CALLBACK(cancel_save_tables), NULL); 
667 #endif
668         return NULL;
669 }
670
671 GCallback save_tables()
672 {
673         if (!tx_mg_have_setname) {
674                 save_tables_as();
675         } else {
676                 do_save_tables(tx_mg_current_setname);
677         }
678         
679         return NULL;
680 }
681
682 GCallback master_volume_changed (GtkWidget *wid, void *d)
683 {
684         sp_master_volume.receive_gui_value((float) gtk_adjustment_get_value(GTK_ADJUSTMENT(wid)));
685         return NULL;    
686 }
687
688 GCallback master_pitch_changed(GtkWidget *wid, void *d)
689 {
690         sp_master_pitch.receive_gui_value((float) gtk_adjustment_get_value(GTK_ADJUSTMENT(wid)));       
691         return NULL;    
692 }
693
694 void mg_enable_critical_buttons(int enable)
695 {
696         gtk_widget_set_sensitive(seq_rec_btn, enable);
697         gtk_widget_set_sensitive(seq_play_btn, enable);
698         gtk_widget_set_sensitive(seq_slider, enable);
699
700         gtk_widget_set_sensitive(rec_menu_item, enable);
701         gtk_widget_set_sensitive(delete_all_item, enable);
702         gtk_widget_set_sensitive(delete_all_vtt_item, enable);
703         
704         vg_enable_critical_buttons(enable);
705 }
706
707 GCallback seq_stop(GtkWidget *w, void *);
708
709 static bool stop_override=false;
710
711 GCallback audio_on(GtkWidget *w, void *d)
712 {
713         tX_engine_error res;
714         
715         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) {               
716                 sequencer_ready=0;
717                 mg_enable_critical_buttons(0);
718                 res=tX_engine::get_instance()->run();
719
720                 if (res!=NO_ERROR) {
721                         mg_enable_critical_buttons(1);
722                         stop_override=true;
723                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 0);
724                         stop_override=false;                    
725
726                         switch(res) {
727                                 case ERROR_BUSY:
728                                 tx_note("Error starting engine: engine is already running.", true);
729                                 break;
730                                 case ERROR_AUDIO:
731                                 tx_note("Error starting engine: failed to access audiodevice.\nPlease check the audio device settings in the \"Preferences\" dialog.", true);
732                                 break;
733                                 case ERROR_TAPE:
734                                 tx_note("Error starting engine: failed to open the recording file.", true);
735                                 break;
736                                 default:tx_note("Error starting engine: Unknown error.", true);
737                         }
738                         
739                         return 0;
740                 }
741                 
742                 sequencer_ready=1;
743                 stop_update=0;
744                 audioon=1;
745                 update_delay=globals.update_delay;
746                 update_tag=g_timeout_add(globals.update_idle, (GSourceFunc) pos_update, NULL);
747                 gtk_widget_set_sensitive(grab_button, 1);
748         } else {        
749                 if (stop_override) return NULL;
750                 if (!sequencer_ready) return NULL;
751                 gtk_widget_set_sensitive(grab_button, 0);
752                 tX_engine::get_instance()->stop();
753                 stop_update=1;
754                 audioon=0;
755                 if (tX_engine::get_instance()->get_recording_request()) {
756                         tX_engine::get_instance()->set_recording_request(false);
757                         rec_dont_care=1;
758                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(rec_menu_item), 0);
759                         rec_dont_care=0;
760                 }
761                 
762                 seq_stop(NULL, NULL);
763                 mg_enable_critical_buttons(1);
764                 
765                 if (tX_engine::get_instance()->get_runtime_error()) {
766                         tx_note("Fatal: The audio device broke down while playing\nback audio. Note that that some audio devices can not\nrecover from such a breakdown.", true);
767                 }
768                 if (tX_engine::get_instance()->get_overload_error()) {
769                         tx_note("Fatal: The audio engine was stopped due to an overload\ncondition. Try reducing the amount of plugins or\nturntables.", true);
770                 }
771         }
772         
773         return NULL;
774 }
775
776 GCallback cancel_rec(GtkWidget *wid)
777 {
778         gtk_widget_destroy(rec_dialog);
779         rec_dialog=NULL;
780         rec_dialog_win=NULL;
781         rec_dont_care=0;
782         return(0);
783 }
784
785 void do_rec(GtkWidget *wid)
786 {
787         char buffer[PATH_MAX];
788         
789         strcpy(buffer, gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(rec_dialog)));
790
791         if (strlen(buffer)) {
792                 strcpy(globals.record_filename, buffer);                
793                 tX_engine::get_instance()->set_recording_request(true);
794                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(rec_menu_item), 1);
795         } 
796         
797         rec_dont_care=0;
798         
799         gtk_widget_destroy(rec_dialog);
800         
801         rec_dialog=NULL;
802         rec_dialog_win=NULL;
803 }
804
805 GCallback select_rec_file()
806 {
807 #ifdef USE_FILECHOOSER
808         GtkWidget * dialog = gtk_file_chooser_dialog_new ("Record To Disk",
809                 GTK_WINDOW(main_window), GTK_FILE_CHOOSER_ACTION_SAVE, 
810                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, 
811                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,NULL);
812         
813         if (strlen(globals.record_filename)) {
814                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER (dialog), globals.record_filename);
815         }
816                                       
817         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
818                 char *filename=gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
819                 strncpy(globals.record_filename, filename, sizeof(globals.record_filename)-1);
820                 g_free(filename);
821                 
822                 tX_engine::get_instance()->set_recording_request(true);
823                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(rec_menu_item), 1);
824         } else {
825                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(rec_menu_item), 0);
826         }
827         
828         rec_dont_care = 0;
829         
830         gtk_widget_destroy(dialog);
831         
832 #else   
833         if (rec_dialog_win) {
834                 gdk_window_raise(rec_dialog_win);
835                 return 0;
836         }
837         
838         rec_dialog=gtk_file_selection_new("Record To Disk");    
839         
840         if (strlen(globals.record_filename)) {
841                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(rec_dialog), globals.record_filename);
842         }
843         
844         gtk_widget_show(rec_dialog);
845         
846         rec_dialog_win=rec_dialog->window;
847         
848         g_signal_connect (G_OBJECT(GTK_FILE_SELECTION(rec_dialog)->ok_button), "clicked", G_CALLBACK(do_rec), NULL);
849         g_signal_connect (G_OBJECT(GTK_FILE_SELECTION(rec_dialog)->cancel_button), "clicked", G_CALLBACK (cancel_rec), NULL);   
850         g_signal_connect (G_OBJECT(rec_dialog), "delete-event", G_CALLBACK(cancel_rec), NULL);  
851         
852 #endif
853
854         return NULL;
855 }
856
857 GCallback tape_on(GtkWidget *w, void *d)
858 {
859         if (rec_dont_care) return 0;
860
861         if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(w))) {   
862                 rec_dont_care=1;
863                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(w), 0);
864                 select_rec_file();
865         } else {
866                         tX_engine::get_instance()->set_recording_request(false);
867         }
868         
869         return NULL;
870 }
871
872 void grab_on(GtkWidget *w, void *d)
873 {
874         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) {
875                 if (mouse.grab() != 0) {
876                         //tX_engine::get_instance()->set_grab_request();
877                         // TODO: handle error
878                 }
879         }
880         grab_status=1;
881 }
882
883 void grab_off()
884 {
885         grab_status=0;
886 }
887
888 gboolean quit()
889 {       
890         if (globals.quit_confirm) {
891                 GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window), 
892                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
893                 "Exit terminatorX and lose all unsaved data?");
894                 
895                 int res=gtk_dialog_run(GTK_DIALOG(dialog));
896                 gtk_widget_destroy(dialog);
897                         
898                 if (res!=GTK_RESPONSE_YES) {
899                         return TRUE;
900                 }
901         }
902         
903         tX_shutdown=true;
904         
905         turn_audio_off();
906         vtt_class::delete_all();
907
908         if (update_tag) {
909                 g_source_remove(update_tag);
910         }
911         GtkAllocation allocation;
912         gtk_widget_get_allocation(GTK_WIDGET(main_window), &allocation);
913         globals.width=allocation.width;
914         globals.height=allocation.height;
915
916         gtk_main_quit();
917         
918         return true;
919 }
920
921 void mplcfitx()
922 /* Most Probably Least Called Function In terminatorX :) */
923 {
924         show_about(0);
925 }
926
927 GCallback seq_play(GtkWidget *w, void *)
928 {
929         if ((sequencer.is_empty()) &&   (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(seq_rec_btn)))) {
930                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
931                  {
932                         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.");
933                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 0);
934                  }
935         } else {
936                 if (seq_stop_override) return NULL;
937                         
938                 seq_adj_care=0;
939                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 1);
940                 sequencer.trig_play();
941         
942                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 1);
943         }
944         
945         return NULL;
946 }
947
948 GCallback seq_stop(GtkWidget *w, void *)
949 {
950         if (!sequencer_ready) return NULL;
951         sequencer.trig_stop();
952         seq_adj_care=1;
953         seq_stop_override=1;
954         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(seq_play_btn), 0);
955         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(seq_rec_btn), 0);
956         while (gtk_events_pending()) gtk_main_iteration();              
957         seq_stop_override=0;
958         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 0); 
959         gtk_widget_set_sensitive(seq_slider, 1);        
960         gtk_widget_set_sensitive(engine_btn, 1);
961         gtk_widget_set_sensitive(seq_rec_btn, 1);
962
963         return NULL;
964 }
965
966 GCallback seq_rec(GtkWidget *w, void *)
967 {
968         seq_adj_care=0;
969         gtk_widget_set_sensitive(seq_slider, 0);
970
971         if (seq_stop_override) return NULL;
972         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 1);
973         gtk_widget_set_sensitive(engine_btn, 0);
974         gtk_widget_set_sensitive(seq_rec_btn, 0);
975         sequencer.trig_rec();
976         
977         return NULL;
978 }
979
980 void seq_update_entry(const guint32 timestamp)
981 {
982         char buffer[20];
983         guint32 samples;
984         guint32 minu,sec,hun;   
985         guint32 sr;
986         
987         samples=timestamp*vtt_class::get_mix_buffer_size();
988         sr=vtt_class::get_last_sample_rate();
989         
990         if (samples>0) {
991                 minu=samples/(sr*60);
992                 samples-=(sr*60)*minu;
993         
994                 sec=samples/sr;
995                 samples-=sr*sec;
996         
997                 hun=samples/(sr/100);
998         } else {
999                 minu=sec=hun=0;
1000         }
1001         
1002         sprintf(buffer, "%02i:%02i.%02i", minu, sec, hun);
1003         gtk_entry_set_text(GTK_ENTRY(seq_entry), buffer);
1004 }
1005
1006 void seq_update()
1007 {
1008         seq_update_entry(sequencer.get_timestamp());
1009         gtk_adjustment_set_value(seq_adj, sequencer.get_timestamp_as_float());
1010         
1011 }
1012 gboolean seq_slider_released(GtkWidget *wid, void *d)
1013 {
1014         seq_adj_care=0;
1015         gtk_widget_set_sensitive(seq_slider, 0);        
1016         sequencer.forward_to_start_timestamp(0);
1017         gtk_widget_set_sensitive(seq_slider, 1);        
1018         seq_adj_care=1;
1019         
1020         return FALSE;
1021 }
1022 void sequencer_move(GtkWidget *wid, void *d)
1023 {
1024         guint32 pos;
1025         
1026         if (seq_adj_care) {
1027                 pos=sequencer.set_start_timestamp((float) gtk_adjustment_get_value(GTK_ADJUSTMENT(wid)));
1028                 seq_update_entry(pos);  
1029         }
1030 }
1031
1032 #define add_sep();      dummy=gtk_hseparator_new ();\
1033         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);\
1034         gtk_widget_show(dummy);\
1035
1036 #define add_sep2();     dummy=gtk_hseparator_new ();\
1037         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);\
1038         gtk_widget_show(dummy);\
1039
1040 void fullscreen_toggle(GtkCheckMenuItem *item, gpointer data);
1041 void display_help();
1042 void display_browser();
1043
1044 tX_seqpar *del_sp=NULL;
1045 vtt_class *del_vtt=NULL;
1046 tx_menu_del_mode menu_del_mode=ALL_EVENTS_ALL_TURNTABLES;
1047
1048 GtkWidget *del_dialog=NULL;
1049
1050 GCallback menu_delete_all_events(GtkWidget *, void *param)
1051 {       
1052         del_dialog=create_tx_del_mode();
1053         tX_set_icon(del_dialog);
1054         
1055         GtkWidget *label=lookup_widget(del_dialog, "delmode_label");
1056         
1057         menu_del_mode=ALL_EVENTS_ALL_TURNTABLES;
1058         
1059         gtk_label_set_markup(GTK_LABEL(label), "Delete <b>all</b> events for <b>all</b> turntables.");
1060         gtk_widget_show(del_dialog);
1061         
1062         return NULL;
1063 }
1064
1065 GCallback menu_delete_all_events_for_vtt(GtkWidget *, vtt_class *vtt)
1066 {       
1067         if (!vtt) {
1068                 tX_error("No vtt passed to menu_delete_all_events_for_vtt().");
1069                 return FALSE;
1070         }
1071         
1072         char label_str[512];
1073         
1074         del_dialog=create_tx_del_mode();
1075         tX_set_icon(del_dialog);
1076
1077         del_vtt=vtt;
1078         GtkWidget *label=lookup_widget(del_dialog, "delmode_label");
1079         
1080         menu_del_mode=ALL_EVENTS_FOR_TURNTABLE;
1081         
1082         sprintf(label_str, "Delete <b>all</b> events for turntable <b>%s</b>.", vtt->name);
1083         gtk_label_set_markup(GTK_LABEL(label), label_str);
1084         gtk_widget_show(del_dialog);
1085         
1086         return NULL;
1087 }
1088
1089 GCallback menu_delete_all_events_for_sp(GtkWidget *, tX_seqpar *sp)
1090 {       
1091         if (!sp) {
1092                 tX_error("No sp passed to menu_delete_all_events_for_sp().");
1093                 return FALSE;
1094         }
1095         
1096         char label_str[512];
1097         
1098         del_dialog=create_tx_del_mode();
1099         tX_set_icon(del_dialog);
1100         
1101         GtkWidget *label=lookup_widget(del_dialog, "delmode_label");
1102         
1103         menu_del_mode=ALL_EVENTS_FOR_SP;
1104         del_sp=sp;
1105         sprintf(label_str, "Delete all <b>%s</b> events for turntable '%s'.", sp->get_name(), sp->get_vtt_name());
1106         gtk_label_set_markup(GTK_LABEL(label), label_str);
1107         gtk_widget_show(del_dialog);
1108
1109         return NULL;
1110 }
1111
1112 static GtkWidget *table_menu=NULL;
1113 static GtkWidget *table_menu_item=NULL;
1114
1115 GCallback create_table_sequencer_menu(GtkWidget *widget, void *param) 
1116 {
1117         char label[328];
1118         table_menu=gtk_menu_new();
1119         
1120         list <vtt_class *> :: iterator vtt;
1121
1122         for (vtt=vtt_class::main_list.begin(); vtt!=vtt_class::main_list.end(); vtt++) {
1123                 GtkWidget *menu_item=gtk_menu_item_new_with_label((*vtt)->name);
1124                 gtk_container_add (GTK_CONTAINER (table_menu), menu_item);
1125                 gtk_widget_show(menu_item);
1126                 
1127                 GtkWidget *seqpar_menu=gtk_menu_new();
1128                 list <tX_seqpar *> :: iterator sp;
1129                 
1130                 GtkWidget *all=gtk_menu_item_new_with_label("Delete All Events");
1131                 gtk_container_add (GTK_CONTAINER (seqpar_menu), all);
1132                 g_signal_connect(all, "activate", (GCallback) menu_delete_all_events_for_vtt, (*vtt));
1133                 gtk_widget_show(all);
1134
1135                 GtkWidget *sep = gtk_menu_item_new ();
1136                 gtk_widget_show(sep);
1137                 gtk_container_add(GTK_CONTAINER (seqpar_menu), sep);
1138                 gtk_widget_set_sensitive (sep, FALSE);
1139                 
1140                 for (sp=tX_seqpar::all->begin(); sp!=tX_seqpar::all->end(); sp++) {
1141                         if ((*sp)->vtt==(*vtt)) {
1142                                 sprintf(label, "Delete '%s' Events", (*sp)->get_name());
1143                                 GtkWidget *menu_item=gtk_menu_item_new_with_label(label);
1144                                 g_signal_connect(menu_item, "activate", (GCallback) menu_delete_all_events_for_sp, (*sp));
1145                                 gtk_container_add(GTK_CONTAINER(seqpar_menu), menu_item);
1146                                 gtk_widget_show(menu_item);
1147                         }
1148                 }
1149                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), seqpar_menu);
1150         }
1151         
1152         gtk_menu_item_set_submenu(GTK_MENU_ITEM(table_menu_item), table_menu);
1153         
1154         return NULL;
1155 }
1156
1157 GCallback toggle_confirm_events(GtkWidget *widget, void *dummy)
1158 {       
1159         globals.confirm_events=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));     
1160         return NULL;    
1161 }
1162
1163 GCallback toggle_auto_assign(GtkWidget *widget, void *dummy)
1164 {       
1165         globals.auto_assign_midi=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
1166 #ifdef USE_ALSA_MIDI_IN
1167         tX_midiin::auto_assign_midi_mappings(NULL, NULL);
1168 #endif
1169 /*      if (globals.auto_assign_midi) {
1170                 GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window), 
1171                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_YES_NO,
1172                 "Note: Enabling \"Auto Assign Default MIDI Settings\" will constantly overwrite the\
1173 MIDI mappings for the standard parameters. \
1174 Are you sure you really want this?");
1175                 
1176                 int res=gtk_dialog_run(GTK_DIALOG(dialog));
1177                 gtk_widget_destroy(dialog);
1178                         
1179                 if (res!=GTK_RESPONSE_YES) {
1180                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), 0);
1181                 }                       
1182         } */
1183         return NULL;    
1184 }
1185
1186 void create_master_menu() 
1187 {
1188         GtkWidget *menu_item;
1189         GtkWidget *sub_menu;
1190         GtkWidget *label;
1191         
1192         GtkAccelGroup* accel_group=gtk_accel_group_new();
1193         gtk_window_add_accel_group(GTK_WINDOW(main_window), accel_group);
1194
1195         /* FILE */
1196         menu_item = gtk_menu_item_new_with_mnemonic ("_File");
1197         gtk_widget_show (menu_item);
1198         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1199
1200         sub_menu = gtk_menu_new ();
1201         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1202
1203         menu_item = gtk_image_menu_item_new_from_stock ("gtk-open", accel_group);
1204         label = gtk_bin_get_child(GTK_BIN(menu_item));
1205         gtk_label_set_text(GTK_LABEL(label), "Load Audio File");
1206         // Warning: gtk+ stock hacks ahead...
1207         gtk_widget_remove_accelerator(menu_item, accel_group, GDK_O, GDK_CONTROL_MASK);
1208         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
1209         gtk_widget_show (menu_item);
1210         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1211         g_signal_connect(menu_item, "activate", (GCallback) load_audio, NULL);
1212
1213         menu_item = gtk_menu_item_new ();
1214         gtk_widget_show (menu_item);
1215         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1216         gtk_widget_set_sensitive (menu_item, FALSE);
1217
1218         menu_item = gtk_image_menu_item_new_from_stock ("gtk-new", accel_group);
1219         label = gtk_bin_get_child(GTK_BIN(menu_item));
1220         gtk_label_set_text(GTK_LABEL(label), "New Set");
1221         gtk_widget_show (menu_item);
1222         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1223         g_signal_connect(menu_item, "activate", (GCallback) new_tables, NULL);
1224
1225         menu_item = gtk_image_menu_item_new_from_stock ("gtk-open", accel_group);
1226         label = gtk_bin_get_child(GTK_BIN(menu_item));
1227         gtk_label_set_text(GTK_LABEL(label), "Open Set File");
1228         gtk_widget_show (menu_item);
1229         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1230         g_signal_connect(menu_item, "activate", (GCallback) load_tables, NULL);
1231
1232         menu_item = gtk_image_menu_item_new_from_stock ("gtk-save", accel_group);
1233         label = gtk_bin_get_child(GTK_BIN(menu_item));
1234         gtk_label_set_text(GTK_LABEL(label), "Save Set");
1235         gtk_widget_show (menu_item);
1236         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1237         g_signal_connect(menu_item, "activate", (GCallback) save_tables, NULL);
1238
1239         menu_item = gtk_image_menu_item_new_from_stock ("gtk-save-as", accel_group);
1240         label = gtk_bin_get_child(GTK_BIN(menu_item));
1241         gtk_label_set_text(GTK_LABEL(label), "Save Set As");
1242         gtk_widget_show (menu_item);
1243         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1244         g_signal_connect(menu_item, "activate", (GCallback) save_tables_as, NULL);
1245
1246         menu_item = gtk_menu_item_new ();
1247         gtk_widget_show (menu_item);
1248         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1249         gtk_widget_set_sensitive (menu_item, FALSE);
1250
1251         menu_item = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
1252         gtk_widget_show (menu_item);
1253         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1254         g_signal_connect(menu_item, "activate", (GCallback) quit, NULL);
1255
1256         /* Turntables */
1257         menu_item = gtk_menu_item_new_with_mnemonic ("_Turntables");
1258         gtk_widget_show (menu_item);
1259         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1260         
1261         sub_menu = gtk_menu_new ();
1262         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1263
1264         menu_item = gtk_menu_item_new_with_mnemonic("_Add Turntable");
1265         gtk_widget_show (menu_item);
1266         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1267         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_A, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);    
1268         g_signal_connect(menu_item, "activate", (GCallback) new_table, NULL);
1269
1270         menu_item = gtk_menu_item_new ();
1271         gtk_widget_show (menu_item);
1272         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1273         gtk_widget_set_sensitive (menu_item, FALSE);
1274
1275         menu_item = gtk_menu_item_new_with_mnemonic("Assign _Default MIDI Mappings");
1276         gtk_widget_show (menu_item);
1277         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1278         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_M, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
1279
1280 #ifdef USE_ALSA_MIDI_IN
1281         g_signal_connect(menu_item, "activate", G_CALLBACK(tX_midiin::auto_assign_midi_mappings), (void *) true);
1282 #else
1283         gtk_widget_set_sensitive(menu_item, FALSE);
1284 #endif
1285
1286         menu_item = gtk_check_menu_item_new_with_mnemonic("A_uto Assign Default MIDI Mappings");
1287         gtk_widget_show (menu_item);
1288         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1289         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), globals.auto_assign_midi);
1290 #ifdef USE_ALSA_MIDI_IN 
1291         g_signal_connect(menu_item, "activate", (GCallback) toggle_auto_assign, NULL);
1292 #else
1293         gtk_widget_set_sensitive(menu_item, FALSE);
1294 #endif
1295
1296         menu_item = gtk_menu_item_new_with_mnemonic("_Clear MIDI Mappings");
1297         gtk_widget_show (menu_item);
1298         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1299         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_C, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
1300
1301 #ifdef USE_ALSA_MIDI_IN
1302         g_signal_connect(menu_item, "activate", G_CALLBACK(tX_midiin::clear_midi_mappings), (void *) true);
1303 #else
1304         gtk_widget_set_sensitive(menu_item, FALSE);
1305 #endif
1306
1307         menu_item = gtk_menu_item_new ();
1308         gtk_widget_show (menu_item);
1309         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1310         gtk_widget_set_sensitive (menu_item, FALSE);
1311
1312         menu_item = gtk_check_menu_item_new_with_mnemonic("_Record Audio To Disk");
1313         rec_menu_item = menu_item;
1314         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_R, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
1315         gtk_widget_show (menu_item);
1316         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1317         g_signal_connect(menu_item, "activate", (GCallback) tape_on, NULL);
1318
1319         /* Sequencer */
1320         
1321         menu_item = gtk_menu_item_new_with_mnemonic("_Sequencer");
1322         gtk_widget_show (menu_item);
1323         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1324
1325         sub_menu = gtk_menu_new ();
1326         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1327                 
1328         table_menu = gtk_menu_new();
1329         menu_item = gtk_menu_item_new_with_mnemonic("Delete _Events");
1330         delete_all_vtt_item = menu_item;
1331         table_menu_item = menu_item;
1332         gtk_menu_item_set_submenu(GTK_MENU_ITEM(table_menu_item), table_menu);
1333         
1334         gtk_widget_show (menu_item);
1335         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1336         g_signal_connect_swapped (G_OBJECT (menu_item), "select", G_CALLBACK (create_table_sequencer_menu), NULL);
1337         
1338         menu_item = gtk_menu_item_new_with_mnemonic("Delete _All Events");
1339         delete_all_item = menu_item;
1340         gtk_widget_show (menu_item);
1341         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1342         g_signal_connect(menu_item, "activate", (GCallback) menu_delete_all_events, NULL);
1343
1344         menu_item = gtk_menu_item_new ();
1345         gtk_widget_show (menu_item);
1346         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1347         gtk_widget_set_sensitive (menu_item, FALSE);
1348
1349         menu_item = gtk_check_menu_item_new_with_mnemonic("_Confirm Recorded Events");
1350         //rec_menu_item = menu_item;
1351         gtk_widget_show (menu_item);
1352         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1353         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), globals.confirm_events);
1354         g_signal_connect(menu_item, "activate", (GCallback) toggle_confirm_events, NULL);
1355
1356         /* Options */
1357         menu_item = gtk_menu_item_new_with_mnemonic ("_Options");
1358         gtk_widget_show (menu_item);
1359         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1360
1361         sub_menu = gtk_menu_new ();
1362         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1363                 
1364         menu_item = gtk_check_menu_item_new_with_mnemonic("_Fullscreen");
1365         fullscreen_item = menu_item;
1366         gtk_widget_show (menu_item);
1367         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1368
1369         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), globals.fullscreen_enabled);
1370         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F11, (GdkModifierType) 0, GTK_ACCEL_VISIBLE);
1371         g_signal_connect(menu_item, "activate", (GCallback) fullscreen_toggle, NULL);
1372         
1373         menu_item = gtk_menu_item_new ();
1374         gtk_widget_show (menu_item);
1375         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1376         gtk_widget_set_sensitive (menu_item, FALSE);
1377
1378         menu_item = gtk_image_menu_item_new_from_stock ("gtk-preferences", accel_group);
1379         gtk_widget_show (menu_item);
1380         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1381         g_signal_connect(menu_item, "activate", (GCallback) display_options, NULL);
1382
1383         /* HELP */ 
1384         menu_item = gtk_menu_item_new_with_mnemonic ("_Help");
1385         gtk_widget_show (menu_item);
1386         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1387         gtk_menu_item_set_right_justified(GTK_MENU_ITEM(menu_item), TRUE);
1388         
1389         sub_menu = gtk_menu_new ();
1390         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1391
1392         menu_item = gtk_menu_item_new_with_mnemonic ("_Contents");
1393         gtk_widget_show (menu_item);
1394         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1395         g_signal_connect(menu_item, "activate", (GCallback) display_help, NULL);
1396         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F1, (GdkModifierType) 0, GTK_ACCEL_VISIBLE);
1397
1398         menu_item = gtk_menu_item_new_with_mnemonic ("_About");
1399         gtk_widget_show (menu_item);
1400         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1401         g_signal_connect(menu_item, "activate", (GCallback) mplcfitx, NULL);
1402         
1403         menu_item = gtk_menu_item_new ();
1404         gtk_widget_show (menu_item);
1405         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1406         gtk_widget_set_sensitive (menu_item, FALSE);
1407
1408         menu_item = gtk_menu_item_new_with_mnemonic ("_Visit terminatorX.org");
1409         gtk_widget_show (menu_item);
1410         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1411         g_signal_connect(menu_item, "activate", (GCallback) display_browser, NULL);
1412 }
1413
1414 void create_mastergui(int x, int y)
1415 {
1416         GtkWidget *mother_of_all_boxen;
1417         GtkWidget *main_vbox;
1418         GtkWidget *right_hbox;
1419         GtkWidget *left_hbox;
1420         GtkWidget *control_box;
1421         //GtkWidget *sequencer_box;
1422         GtkAdjustment *dumadj;
1423         GtkWidget *dummy;
1424         GtkWidget *master_vol_box;
1425         GtkWidget *status_box;
1426         GtkWidget *wrapbox;
1427         
1428         main_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
1429
1430         gtk_window_set_wmclass(GTK_WINDOW(main_window), "terminatorX", "tX_mastergui");
1431
1432         gtk_window_set_title(GTK_WINDOW(main_window), "terminatorX");
1433
1434         gtk_widget_realize(main_window);
1435
1436         wrapbox=gtk_vbox_new(FALSE, 5);
1437         gtk_container_add(GTK_CONTAINER(main_window), wrapbox);
1438         gtk_widget_show(wrapbox);
1439
1440         main_menubar=gtk_menu_bar_new();
1441         gtk_box_pack_start(GTK_BOX(wrapbox), main_menubar, WID_FIX);
1442         gtk_widget_show(main_menubar);
1443
1444         mother_of_all_boxen=gtk_vbox_new(FALSE, 5);
1445         gtk_container_set_border_width(GTK_CONTAINER(mother_of_all_boxen), 5);
1446         gtk_container_add(GTK_CONTAINER(wrapbox), mother_of_all_boxen);
1447         gtk_widget_show(mother_of_all_boxen);   
1448
1449         create_master_menu();
1450
1451         g_signal_connect(G_OBJECT(main_window), "motion_notify_event", G_CALLBACK(tx_mouse::motion_notify_wrap), &mouse);
1452         g_signal_connect(G_OBJECT(main_window), "button_press_event", G_CALLBACK(tx_mouse::button_press_wrap), &mouse);
1453         g_signal_connect(G_OBJECT(main_window), "button_release_event", G_CALLBACK(tx_mouse::button_release_wrap), &mouse);
1454         g_signal_connect(G_OBJECT(main_window), "key_press_event", G_CALLBACK(tx_mouse::key_press_wrap), &mouse);
1455         g_signal_connect(G_OBJECT(main_window), "key_release_event", G_CALLBACK(tx_mouse::key_release_wrap), &mouse);
1456         
1457         main_vbox=gtk_hbox_new(FALSE, 5);
1458         gtk_box_pack_start(GTK_BOX(mother_of_all_boxen), main_vbox, WID_DYN);
1459         gtk_widget_show(main_vbox);
1460         
1461         left_hbox=gtk_vbox_new(FALSE, 5);
1462         gtk_box_pack_start(GTK_BOX(main_vbox), left_hbox, WID_DYN);
1463         gtk_widget_show(left_hbox);
1464         
1465         control_box=gtk_hbox_new(FALSE, 5);
1466         gtk_box_pack_start(GTK_BOX(left_hbox), control_box, WID_FIX);
1467         gtk_widget_show(control_box);
1468         
1469         dummy=gtk_hseparator_new();
1470         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_FIX);
1471         gtk_widget_show(dummy);
1472
1473     /* control_box contents */
1474
1475         dummy=tx_xpm_label_box(AUDIOENGINE, "Audio");
1476         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1477         gtk_widget_show(dummy);
1478         
1479         dummy=tx_xpm_button_new(POWER,"Power ", 1);
1480         connect_button(dummy,audio_on, NULL);
1481         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1482         gui_set_tooltip(dummy, "Turn the audio engine on/off.");
1483         gtk_widget_show(dummy);
1484         engine_btn=dummy;
1485         
1486         grab_button=tx_xpm_button_new(GRAB, "Mouse Grab ", 1);
1487         gtk_box_pack_start(GTK_BOX(control_box), grab_button, WID_FIX);
1488         connect_button(grab_button, grab_on, NULL);
1489         gui_set_tooltip(grab_button, "Enter the mouse grab mode operation. Press <ESCAPE> to exit grab mode.");
1490         gtk_widget_show(grab_button);
1491
1492         dummy=gtk_vseparator_new();
1493         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1494         gtk_widget_show(dummy);
1495     
1496         dummy=tx_xpm_label_box(SEQUENCER, "Seq.");
1497         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1498         gtk_widget_show(dummy);
1499
1500         dummy=tx_xpm_button_new(PLAY,"Play ", 1);
1501         connect_button(dummy, seq_play, NULL);
1502         seq_play_btn=dummy;
1503         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1504         gui_set_tooltip(dummy, "Playback previously recorded events from the sequencer. This will turn on the audio engine automagically.");
1505         gtk_widget_show(dummy);
1506
1507         dummy=tx_xpm_button_new(STOP,"Stop ", 0);
1508         seq_stop_btn=dummy;
1509         connect_button(dummy, seq_stop, NULL);  
1510         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1511         gui_set_tooltip(dummy, "Stop the playback of sequencer events.");
1512         gtk_widget_show(dummy);
1513
1514         dummy=tx_xpm_button_new(RECORD,"Record ", 1);
1515         connect_button(dummy, seq_rec, NULL);
1516         seq_rec_btn=dummy;
1517         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1518         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.");
1519         gtk_widget_show(dummy);
1520
1521         dummy=gtk_label_new("Pos:");
1522         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1523         gtk_widget_show(dummy);
1524         
1525         dummy=gtk_entry_new();
1526         gtk_entry_set_max_length(GTK_ENTRY(dummy), 12);
1527         seq_entry=dummy;
1528         //gtk_widget_set_usize(dummy, 65, 20);
1529         gtk_entry_set_text(GTK_ENTRY(dummy), "00:00.00");
1530 #if GTK_CHECK_VERSION(2,4,0)
1531         gtk_entry_set_alignment(GTK_ENTRY(dummy), 0.5);
1532 #endif  
1533         gtk_entry_set_width_chars(GTK_ENTRY(dummy), 9);
1534         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1535         gtk_widget_show(dummy);
1536
1537         dumadj=(GtkAdjustment*) gtk_adjustment_new(0, 0, 100, 0.1, 1, 1);
1538         seq_adj=dumadj;
1539         connect_adj(dumadj, sequencer_move, NULL);      
1540         dummy=gtk_hscale_new(dumadj);
1541         gtk_widget_set_size_request(dummy, 65, 20);
1542         seq_slider=dummy;
1543         g_signal_connect(G_OBJECT(seq_slider), "button-release-event", (GCallback) seq_slider_released, NULL);
1544         gtk_scale_set_draw_value(GTK_SCALE(dummy), FALSE);
1545         
1546         gui_set_tooltip(dummy, "Select the start position for the sequencer in song-time.");
1547         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_DYN);
1548         gtk_widget_show(dummy);
1549         
1550         dummy=gtk_hbox_new(FALSE,2); //gtk_hpaned_new ();
1551         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_DYN);
1552         gtk_widget_show(dummy);
1553         
1554         tt_parent=dummy;
1555
1556     panel_bar=gtk_hbox_new(TRUE,0);
1557         gtk_box_pack_start(GTK_BOX(left_hbox), panel_bar, WID_FIX);
1558
1559         control_parent=gtk_hbox_new(FALSE,4);
1560         gtk_box_pack_start(GTK_BOX(tt_parent), control_parent, WID_FIX);
1561         gtk_widget_show(control_parent);
1562
1563         dummy=gtk_vseparator_new();
1564         gtk_box_pack_start(GTK_BOX(tt_parent), dummy, WID_FIX);
1565         gtk_widget_show(dummy);
1566
1567         audio_parent=gtk_vbox_new(FALSE,2);
1568         gtk_box_pack_start(GTK_BOX(tt_parent), audio_parent, WID_DYN);
1569         gtk_widget_show(audio_parent);
1570         
1571         dummy=gtk_vseparator_new();
1572         gtk_box_pack_start(GTK_BOX(main_vbox), dummy, WID_FIX);
1573         gtk_widget_show(dummy);
1574                 
1575         right_hbox=gtk_vbox_new(FALSE, 5);
1576         gtk_box_pack_start(GTK_BOX(main_vbox), right_hbox, WID_FIX);
1577         gtk_widget_show(right_hbox);
1578
1579         /* Master */
1580         
1581         dummy=gtk_label_new(NULL);
1582         gtk_label_set_markup(GTK_LABEL(dummy),"<b>Master</b>");
1583         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1584         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1585         gtk_widget_show(dummy); 
1586
1587         dummy=gtk_hseparator_new();
1588         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1589         gtk_widget_show(dummy);
1590
1591          /* Pitch */
1592          
1593         /*dummy=gtk_label_new("Pitch:");
1594         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1595         gtk_widget_show(dummy);*/
1596
1597         dumadj=(GtkAdjustment*) gtk_adjustment_new(globals.pitch, -3, 3, 0.001, 0.001, 0.01);
1598         pitch_adj=dumadj;
1599         connect_adj(dumadj, master_pitch_changed, NULL);
1600         
1601         tX_extdial *pdial=new tX_extdial("Pitch", pitch_adj, &sp_master_pitch, true);
1602         gtk_box_pack_start(GTK_BOX(right_hbox), pdial->get_widget(), WID_FIX);
1603         gui_set_tooltip(pdial->get_entry(), "Use this dial to adjust the master pitch (affecting *all* turntables).");
1604         
1605         dummy=gtk_hseparator_new();
1606         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1607         gtk_widget_show(dummy);
1608         
1609         /* Volume */
1610         master_vol_box=gtk_hbox_new(FALSE, 5);
1611         gtk_box_pack_start(GTK_BOX(right_hbox), master_vol_box, WID_DYN);
1612         gtk_widget_show(master_vol_box);        
1613         
1614         dumadj=(GtkAdjustment*) gtk_adjustment_new(globals.volume, 0, 2, 0.01, 0.05, 0.000);
1615         volume_adj=dumadj;
1616
1617         connect_adj(dumadj, master_volume_changed, NULL);       
1618         dummy=gtk_vscale_new(dumadj);
1619         gtk_range_set_inverted(GTK_RANGE(dummy), TRUE);
1620         gtk_scale_set_draw_value(GTK_SCALE(dummy), False);
1621         g_signal_connect(G_OBJECT(dummy), "button_press_event", (GCallback) tX_seqpar::tX_seqpar_press, &sp_master_volume);     
1622         
1623         gtk_box_pack_end(GTK_BOX(master_vol_box), dummy, WID_FIX);
1624         gtk_widget_show(dummy); 
1625         gui_set_tooltip(dummy, "Adjust the master volume. This parameter will effect *all* turntables in the set.");
1626         
1627         main_flash=gtk_tx_flash_new();
1628         gtk_box_pack_end(GTK_BOX(master_vol_box), main_flash, WID_DYN);
1629         gtk_widget_show(main_flash);
1630
1631         dummy=gtk_label_new("Volume");
1632         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1633         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1634         gtk_widget_show(dummy);
1635
1636         /* STATUS BOX */ 
1637         dummy=gtk_hseparator_new();
1638         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1639         gtk_widget_show(dummy);
1640         
1641         status_box=gtk_vbox_new(FALSE, 0);
1642         gtk_box_pack_start(GTK_BOX(right_hbox), status_box, WID_FIX);
1643         gtk_widget_show(status_box);
1644         
1645         dummy=gtk_label_new("0");
1646         used_mem=dummy;
1647         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1648         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1649         gtk_widget_show(dummy);
1650
1651 //      dummy=gtk_label_new("Mem/MB:");
1652 //      gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1653 //      gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1654 //      gtk_widget_show(dummy);
1655         
1656         /*add_sep2();
1657
1658         dummy=gtk_label_new("1");
1659         no_of_vtts=dummy;
1660         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1661         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1662         gtk_widget_show(dummy);
1663
1664         dummy=gtk_label_new("Vtts:");
1665         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1666         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1667         gtk_widget_show(dummy);*/
1668
1669         add_sep2();
1670
1671         dummy=gtk_label_new("v"VERSION);
1672         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1673         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1674         gtk_widget_show(dummy);
1675
1676         /*dummy=gtk_label_new("Release:");
1677         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1678         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1679         gtk_widget_show(dummy);*/
1680         
1681         add_sep2();
1682
1683         dummy=gtk_label_new(NULL);
1684         gtk_label_set_markup(GTK_LABEL(dummy), "<b>Status</b>");
1685         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1686         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1687         gtk_widget_show(dummy);
1688         
1689         /* END GUI */
1690         
1691         gtk_window_set_default_size(GTK_WINDOW(main_window), x, y);     
1692         gtk_widget_set_sensitive(grab_button, 0);
1693
1694         new_table(NULL, NULL); // to give the user something to start with ;)
1695
1696         g_signal_connect (G_OBJECT(main_window), "delete-event", (GCallback) quit, NULL);
1697         
1698 //      if (globals.tooltips) gtk_tooltips_enable(gui_tooltips);
1699 //      else gtk_tooltips_disable(gui_tooltips);
1700 //  TODO: Check for global enable/disable of tooltips
1701 }
1702
1703 gfloat old_percent=-1;
1704
1705 void note_destroy(GtkWidget *widget, GtkWidget *mbox)
1706 {
1707         gtk_widget_destroy(GTK_WIDGET(mbox));
1708 }
1709
1710 void tx_note(const char *message, bool isError, GtkWindow *window)
1711 {
1712         if (!window) window=GTK_WINDOW(main_window);
1713         
1714         GtkWidget *dialog=gtk_message_dialog_new(window,
1715                 GTK_DIALOG_DESTROY_WITH_PARENT,
1716                 isError ? GTK_MESSAGE_ERROR : GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "%s", message);
1717         gtk_dialog_run(GTK_DIALOG(dialog));
1718         gtk_widget_destroy(dialog);     
1719 }
1720
1721
1722 void tx_l_note(const char *message)
1723 {
1724         char buffer[4096]="Plugin info:\n\n";
1725         strcat(buffer, message);
1726         
1727         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window),
1728                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "%s", message);
1729         gtk_dialog_run(GTK_DIALOG(dialog));
1730         gtk_widget_destroy(dialog);     
1731 }
1732
1733 void add_to_panel_bar(GtkWidget *button) 
1734 {
1735         buttons_on_panel_bar++;
1736         gtk_box_pack_start(GTK_BOX(panel_bar), button, WID_DYN);
1737         gtk_widget_show(panel_bar);
1738 }
1739
1740 void remove_from_panel_bar(GtkWidget *button) 
1741 {
1742         buttons_on_panel_bar--;
1743         gtk_container_remove(GTK_CONTAINER(panel_bar), button);
1744         if (buttons_on_panel_bar==0) gtk_widget_hide(panel_bar);
1745 }
1746
1747 /* Fullscreen code... */
1748 #define _WIN_LAYER_TOP          -1
1749 #define _WIN_LAYER_NORMAL       4
1750 #define _NET_WM_STATE_REMOVE    0
1751 #define _NET_WM_STATE_ADD       1
1752 #define _NET_WM_STATE_TOGGLE    2
1753
1754 void fullscreen_toggle(GtkCheckMenuItem *item, gpointer data) {
1755         XEvent xev;
1756         Window win = GDK_WINDOW_XID(gtk_widget_get_window(main_window));
1757         Display *disp=GDK_WINDOW_XDISPLAY(gtk_widget_get_window(main_window));
1758         
1759         globals.fullscreen_enabled=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(fullscreen_item));
1760         
1761         /* Top layer.. */
1762         xev.xclient.type = ClientMessage;
1763         xev.xclient.serial = 0;
1764         xev.xclient.send_event = True;
1765         xev.xclient.display = disp;
1766         xev.xclient.window = win;
1767         xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_WIN_LAYER");
1768         xev.xclient.format = 32;
1769         xev.xclient.data.l[0] = globals.fullscreen_enabled ? _WIN_LAYER_TOP : _WIN_LAYER_NORMAL ;
1770         XSendEvent(disp, GDK_WINDOW_XID (gdk_get_default_root_window ()),
1771                 False, SubstructureRedirectMask | SubstructureNotifyMask,
1772                 &xev);
1773         
1774         /* Fullscreen */
1775         xev.xclient.type = ClientMessage;
1776         xev.xclient.serial = 0;
1777         xev.xclient.send_event = True;
1778         xev.xclient.display = disp;
1779         xev.xclient.window = win;
1780         xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_NET_WM_STATE");
1781         xev.xclient.format = 32;
1782         xev.xclient.data.l[0] = globals.fullscreen_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1783         xev.xclient.data.l[1] = gdk_x11_atom_to_xatom (gdk_atom_intern ("_NET_WM_STATE_FULLSCREEN", TRUE));
1784         xev.xclient.data.l[2] = gdk_x11_atom_to_xatom (GDK_NONE);
1785         XSendEvent(gdk_x11_get_default_xdisplay(), GDK_WINDOW_XID (gdk_get_default_root_window ()),
1786                 False, SubstructureRedirectMask | SubstructureNotifyMask,
1787                 &xev);  
1788 }
1789
1790 void fullscreen_setup() {
1791         if (globals.fullscreen_enabled) {
1792                 fullscreen_toggle(NULL, NULL);
1793         }
1794 }
1795
1796 void display_mastergui()
1797 {
1798         GtkWidget *top;
1799         gtk_widget_realize(main_window);
1800         tX_set_icon(main_window);
1801         load_knob_pixs();
1802         gtk_widget_show(main_window);
1803         fullscreen_setup();     
1804         top=gtk_widget_get_toplevel(main_window);
1805         top_window=GDK_WINDOW(gtk_widget_get_window(top));
1806         x_window=GDK_WINDOW_XWINDOW(gtk_widget_get_window(top));
1807 }
1808
1809 pid_t help_child=0;
1810 GTimer *help_timer=NULL;
1811 int help_tag=-1;
1812
1813 gboolean help_checker()
1814 {
1815         gdouble time;
1816         gulong ms;
1817         int status;
1818         int result=waitpid(help_child, &status, WNOHANG);
1819         
1820         if (result==0) {
1821                 time=g_timer_elapsed(help_timer, &ms);
1822                 if (time > 5) {
1823                         /* 5 seconds and it's still running - so we assume everything's OK. */
1824                         tX_debug("No longer waiting for gnome-help..");
1825                         g_source_remove(help_tag);
1826                         help_tag=-1;
1827                 }
1828         }  else {
1829                 //yelp waitpid status does not allow determining success
1830                 //printf("%i %i\n", WIFEXITED(status), WEXITSTATUS(status));
1831                 ///* We are still here and the child exited - that could mean trouble. */
1832                 //tx_note("Couldn't run the gnome-help command (alias \"yelp\") to display the terminatorX manual. Please ensure that \"yelp\" is installed.", true);           
1833                 
1834                 g_source_remove(help_tag);
1835                 help_tag=-1;
1836         }
1837         return TRUE;    
1838 }
1839
1840 #ifndef INSTALL_PREFIX
1841 #define INSTALL_PREFIX "/usr/local/share"
1842 #endif
1843
1844 void display_help()
1845 {       
1846         help_child=fork();
1847
1848         if (help_tag!=-1) {
1849                 g_source_remove(help_tag);
1850                 if (help_timer) g_timer_destroy(help_timer);
1851                 help_child=0;
1852                 help_tag=-1;
1853                 help_timer=NULL;
1854         }
1855         
1856         if (help_child==0) {
1857                 // child
1858                 execlp("gnome-help","gnome-help","file://" XML_MANUAL, NULL);           
1859                 _exit(-1);
1860         } else if (help_child==-1) {
1861                 tx_note("System error: couldn't fork() to run the help process.", true);
1862         } else {
1863                 help_timer=g_timer_new();
1864                 g_timer_start(help_timer);
1865         
1866                 help_tag=g_idle_add((GSourceFunc) help_checker, NULL);
1867         }
1868 }
1869
1870 pid_t browser_child=0;
1871 GTimer *browser_timer=NULL;
1872 int browser_tag=-1;
1873
1874 gboolean browser_checker()
1875 {
1876         gdouble time;
1877         gulong ms;
1878         int status;
1879         int result=waitpid(browser_child, &status, WNOHANG);
1880         
1881         if (result==0) {
1882                 time=g_timer_elapsed(browser_timer, &ms);
1883                 if (time > 5) {
1884                         /* 5 seconds and it's still running - so we assume everything's OK. */
1885                         tX_debug("No longer waiting for a browser..");
1886                         g_source_remove(browser_tag);
1887                         browser_tag=-1;
1888                 }
1889         } else {
1890                 /* We are still here and the child exited - that could mean trouble. */
1891                 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.org", true);                
1892                 
1893                 g_source_remove(browser_tag);
1894                 browser_tag=-1;
1895         }
1896         return TRUE;    
1897 }
1898
1899 void display_browser()
1900 {       
1901         browser_child=fork();
1902
1903         if (browser_tag!=-1) {
1904                 g_source_remove(browser_tag);
1905                 if (browser_timer) g_timer_destroy(browser_timer);
1906                 browser_child=0;
1907                 browser_tag=-1;
1908                 browser_timer=NULL;
1909         }
1910         
1911         if (browser_child==0) {
1912                 // child
1913                 execlp("mozilla","mozilla","http://terminatorX.org", NULL);
1914                 execlp("netscape","netscape","http://terminatorX.org", NULL);
1915                 execlp("galeon","galeon","http://terminatorX.org", NULL);
1916                 execlp("konqueror","konqueror","http://terminatorX.org", NULL);         
1917                 _exit(-1);
1918         } else if (browser_child==-1) {
1919                 tx_note("System error: couldn't fork() to run the browser process.", true);
1920         } else {
1921                 browser_timer=g_timer_new();
1922                 g_timer_start(browser_timer);
1923         
1924                 browser_tag=g_idle_add((GSourceFunc) browser_checker, NULL);
1925         }
1926 }
1927
1928
1929
1930 GdkCursor *tX_cursor::cursors[MAX_CURSOR]={NULL, NULL, NULL};
1931 tX_cursor::cursor_shape tX_cursor::current_shape=tX_cursor::DEFAULT_CURSOR;
1932
1933 void tX_cursor::set_cursor(cursor_shape shape)
1934 {
1935         switch (shape) {
1936                 case DEFAULT_CURSOR:
1937                         cursors[shape]=NULL;
1938                         break;
1939                 
1940                 case WAIT_CURSOR:
1941                         if (!cursors[shape]) cursors[shape]=gdk_cursor_new(GDK_WATCH);
1942                         break;
1943                 
1944                 case WAIT_A_SECOND_CURSOR:
1945                         /* FIXME: What's that short-time wait cursor's id? */
1946                         if (!cursors[shape]) cursors[shape]=gdk_cursor_new(GDK_WATCH);
1947                         break;
1948                 
1949                 default:
1950                         tX_debug("No such cursor shape.");
1951                         return;
1952         }
1953         
1954         /* Still here? Ok... */
1955         current_shape=shape;
1956         
1957         gdk_window_set_cursor(gtk_widget_get_window(main_window), cursors[shape]);
1958 }
1959
1960 GdkCursor *tX_cursor::get_cursor()
1961 {
1962         return cursors[current_shape];
1963 }
1964
1965 void tX_cursor::reset_cursor()
1966 {
1967         current_shape=DEFAULT_CURSOR;
1968         gdk_window_set_cursor(gtk_widget_get_window(main_window), cursors[current_shape]);
1969 }