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