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