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