cada8ed3612bf7a77ad4ef85560fac8c3dde02d3
[terminatorX.git] / src / tX_mastergui.cc
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999-2004  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, 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                         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
524 GtkSignalFunc drop_set(GtkWidget *widget, GdkDragContext *context,
525                 gint x, gint y, GtkSelectionData *selection_data,
526                 guint info, guint time, vtt_class *vtt)
527 {
528         char filename[PATH_MAX];
529         char *fn;
530         
531         strncpy(filename, (char *) selection_data->data, (size_t) selection_data->length);
532         filename[selection_data->length]=0;
533
534         fn = strchr (filename, '\r');
535         *fn=0;  
536         
537         fn = strchr (filename, ':');
538         if (fn) fn++; else fn=(char *) selection_data->data;
539         
540         load_tt_part(fn);
541
542         strcpy (filename, "dont segfault workaround ;)");
543         return NULL;
544 }
545
546 /* save tables */
547
548 #ifndef USE_FILECHOOSER
549 GtkSignalFunc cancel_save_tables(GtkWidget *wid)
550 {
551         gtk_widget_destroy(save_dialog);
552         save_dialog=NULL;
553         save_dialog_win=NULL;
554         return(0);
555 }
556
557 gboolean do_save_from_selection(GtkWidget *wid) {
558         char buffer[PATH_MAX];
559         
560         if (wid) {
561                 strcpy(buffer, gtk_file_selection_get_filename(GTK_FILE_SELECTION(save_dialog)));
562                 int len=strlen(buffer);
563                 if (!len || (buffer[len-1]=='/')) {                     
564                         tx_note("Invalid filename for set file.", true);                        
565                         return FALSE;
566                 }
567                 gtk_widget_destroy(save_dialog);
568                 save_dialog=NULL;
569                 save_dialog_win=NULL;
570         } else {
571                 strcpy(buffer, tx_mg_current_setname);
572         }       
573         
574         do_save_tables(buffer);
575         
576         return TRUE;
577 }
578 #endif
579
580 void do_save_tables(char *buffer)
581 {
582         FILE *out;
583         gzFile zout;
584         char wbuf[PATH_MAX];
585         char *ext;
586         
587         ext=strrchr(buffer, '.');
588         
589         if (ext) {
590                 if (strcmp(ext, ".tX")) strcat(buffer, ".tX");
591         } else {
592                 strcat(buffer, ".tX");
593         }
594
595         tx_mg_have_setname=true;
596         strcpy(tx_mg_current_setname, buffer);
597         strcpy(globals.tables_filename, buffer);
598         
599         if (globals.compress_set_files) {
600                 _store_compress_xml=1;
601                 out=NULL;
602                 zout=gzopen(buffer, "w");
603         } else {
604                 _store_compress_xml=0;
605                 out=fopen(buffer, "w");
606                 zout=NULL;
607         }
608         
609         if (out || zout) {
610                 if (vtt_class::save_all(out, zout)) tx_note("Error while saving set.", true);
611                 if (out) fclose(out); 
612                 else if (zout) gzclose(zout);
613                 sprintf(wbuf,"terminatorX - %s", strip_path(buffer));
614                 gtk_window_set_title(GTK_WINDOW(main_window), wbuf);                            
615         } else {
616                 tx_note("Failed to open file for write access.", true);
617         }
618 }
619
620 GtkSignalFunc save_tables_as()
621 {
622 #ifdef USE_FILECHOOSER
623         GtkWidget * dialog = gtk_file_chooser_dialog_new ("Save Set",
624                 GTK_WINDOW(main_window), GTK_FILE_CHOOSER_ACTION_SAVE, 
625                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, 
626                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,NULL);
627         
628         if (tx_mg_have_setname) {
629                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER (dialog), tx_mg_current_setname);
630         }
631                                       
632         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
633         char buffer[PATH_MAX];
634                 char *filename=gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
635                 strcpy(buffer, filename);
636                 g_free(filename);
637                 do_save_tables(buffer);
638         }       
639         
640         gtk_widget_destroy(dialog);     
641 #else
642         if (save_dialog_win) {
643                 gtk_widget_destroy(save_dialog);
644                 save_dialog=NULL;
645                 save_dialog_win=NULL;
646         }
647         
648         save_dialog=gtk_file_selection_new("Save Set"); 
649
650         if (tx_mg_have_setname) {
651                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(save_dialog), tx_mg_current_setname);
652         }
653         
654         gtk_widget_show(save_dialog);
655         
656         save_dialog_win=save_dialog->window;
657         
658         g_signal_connect (G_OBJECT(GTK_FILE_SELECTION(save_dialog)->ok_button), "clicked", G_CALLBACK(do_save_from_selection), NULL);
659         g_signal_connect (G_OBJECT(GTK_FILE_SELECTION(save_dialog)->cancel_button), "clicked", G_CALLBACK (cancel_save_tables), NULL);  
660         g_signal_connect (G_OBJECT(save_dialog), "delete-event", G_CALLBACK(cancel_save_tables), NULL); 
661 #endif
662         return NULL;
663 }
664
665 GtkSignalFunc save_tables()
666 {
667         if (!tx_mg_have_setname) {
668                 save_tables_as();
669         } else {
670                 do_save_tables(tx_mg_current_setname);
671         }
672         
673         return NULL;
674 }
675
676 GtkSignalFunc master_volume_changed (GtkWidget *wid, void *d)
677 {
678         sp_master_volume.receive_gui_value((float) GTK_ADJUSTMENT(wid)->value);
679         return NULL;    
680 }
681
682 GtkSignalFunc master_pitch_changed(GtkWidget *wid, void *d)
683 {
684         sp_master_pitch.receive_gui_value((float) GTK_ADJUSTMENT(wid)->value);  
685         return NULL;    
686 }
687
688 void mg_enable_critical_buttons(int enable)
689 {
690         gtk_widget_set_sensitive(seq_rec_btn, enable);
691         gtk_widget_set_sensitive(seq_play_btn, enable);
692         gtk_widget_set_sensitive(seq_slider, enable);
693
694         gtk_widget_set_sensitive(rec_menu_item, enable);
695         gtk_widget_set_sensitive(delete_all_item, enable);
696         gtk_widget_set_sensitive(delete_all_vtt_item, enable);
697         
698         vg_enable_critical_buttons(enable);
699 }
700
701 GtkSignalFunc seq_stop(GtkWidget *w, void *);
702
703 static bool stop_override=false;
704
705 GtkSignalFunc audio_on(GtkWidget *w, void *d)
706 {
707         tX_engine_error res;
708         
709         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) {               
710                 sequencer_ready=0;
711                 mg_enable_critical_buttons(0);
712                 res=tX_engine::get_instance()->run();
713
714                 if (res!=NO_ERROR) {
715                         mg_enable_critical_buttons(1);
716                         stop_override=true;
717                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 0);
718                         stop_override=false;                    
719
720                         switch(res) {
721                                 case ERROR_BUSY:
722                                 tx_note("Error starting engine: engine is already running.", true);
723                                 break;
724                                 case ERROR_AUDIO:
725                                 tx_note("Error starting engine: failed to access audiodevice.\nPlease check the audio device settings in the \"Preferences\" dialog.", true);
726                                 break;
727                                 case ERROR_TAPE:
728                                 tx_note("Error starting engine: failed to open the recording file.", true);
729                                 break;
730                                 default:tx_note("Error starting engine: Unknown error.", true);
731                         }
732                         
733                         return 0;
734                 }
735                 
736                 sequencer_ready=1;
737                 stop_update=0;
738                 audioon=1;
739                 update_delay=globals.update_delay;
740                 update_tag=gtk_timeout_add(globals.update_idle, (GtkFunction) pos_update, NULL);
741                 gtk_widget_set_sensitive(grab_button, 1);
742         } else {        
743                 if (stop_override) return NULL;
744                 if (!sequencer_ready) return NULL;
745                 gtk_widget_set_sensitive(grab_button, 0);
746                 tX_engine::get_instance()->stop();
747                 stop_update=1;
748                 audioon=0;
749                 if (tX_engine::get_instance()->get_recording_request()) {
750                         tX_engine::get_instance()->set_recording_request(false);
751                         rec_dont_care=1;
752                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(rec_menu_item), 0);
753                         rec_dont_care=0;
754                 }
755                 
756                 seq_stop(NULL, NULL);
757                 mg_enable_critical_buttons(1);
758                 
759                 if (tX_engine::get_instance()->get_runtime_error()) {
760                         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);
761                 }
762                 if (tX_engine::get_instance()->get_overload_error()) {
763                         tx_note("Fatal: The audio engine was stopped due to an overload\ncondition. Try reducing the amount of plugins or\nturntables.", true);
764                 }
765         }
766         
767         return NULL;
768 }
769
770 GtkSignalFunc cancel_rec(GtkWidget *wid)
771 {
772         gtk_widget_destroy(rec_dialog);
773         rec_dialog=NULL;
774         rec_dialog_win=NULL;
775         rec_dont_care=0;
776         return(0);
777 }
778
779 void do_rec(GtkWidget *wid)
780 {
781         char buffer[PATH_MAX];
782         
783         strcpy(buffer, gtk_file_selection_get_filename(GTK_FILE_SELECTION(rec_dialog)));
784
785         if (strlen(buffer)) {
786                 strcpy(globals.record_filename, buffer);                
787                 tX_engine::get_instance()->set_recording_request(true);
788                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(rec_menu_item), 1);
789         } 
790         
791         rec_dont_care=0;
792         
793         gtk_widget_destroy(rec_dialog);
794         
795         rec_dialog=NULL;
796         rec_dialog_win=NULL;
797 }
798
799 GtkSignalFunc select_rec_file()
800 {
801 #ifdef USE_FILECHOOSER
802         GtkWidget * dialog = gtk_file_chooser_dialog_new ("Record To Disk",
803                 GTK_WINDOW(main_window), GTK_FILE_CHOOSER_ACTION_SAVE, 
804                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, 
805                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,NULL);
806         
807         if (strlen(globals.record_filename)) {
808                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER (dialog), globals.record_filename);
809         }
810                                       
811         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
812                 char *filename=gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
813                 strncpy(globals.record_filename, filename, sizeof(globals.record_filename)-1);
814                 g_free(filename);
815                 
816                 tX_engine::get_instance()->set_recording_request(true);
817                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(rec_menu_item), 1);
818         } else {
819                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(rec_menu_item), 0);
820         }
821         
822         rec_dont_care = 0;
823         
824         gtk_widget_destroy(dialog);
825         
826 #else   
827         if (rec_dialog_win) {
828                 gdk_window_raise(rec_dialog_win);
829                 return 0;
830         }
831         
832         rec_dialog=gtk_file_selection_new("Record To Disk");    
833         
834         if (strlen(globals.record_filename)) {
835                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(rec_dialog), globals.record_filename);
836         }
837         
838         gtk_widget_show(rec_dialog);
839         
840         rec_dialog_win=rec_dialog->window;
841         
842         g_signal_connect (G_OBJECT(GTK_FILE_SELECTION(rec_dialog)->ok_button), "clicked", G_CALLBACK(do_rec), NULL);
843         g_signal_connect (G_OBJECT(GTK_FILE_SELECTION(rec_dialog)->cancel_button), "clicked", G_CALLBACK (cancel_rec), NULL);   
844         g_signal_connect (G_OBJECT(rec_dialog), "delete-event", G_CALLBACK(cancel_rec), NULL);  
845         
846 #endif
847
848         return NULL;
849 }
850
851 GtkSignalFunc tape_on(GtkWidget *w, void *d)
852 {
853         if (rec_dont_care) return 0;
854
855         if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(w))) {   
856                 rec_dont_care=1;
857                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(w), 0);
858                 select_rec_file();
859         } else {
860                         tX_engine::get_instance()->set_recording_request(false);
861         }
862         
863         return NULL;
864 }
865
866 void grab_on(GtkWidget *w, void *d)
867 {
868         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) {
869                 tX_engine::get_instance()->set_grab_request();
870         }
871         grab_status=1;
872 }
873
874 void grab_off()
875 {
876         grab_status=0;
877 }
878
879 gboolean quit()
880 {       
881         if (globals.quit_confirm) {
882                 GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window), 
883                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
884                 "Exit terminatorX and loose all unsaved data?");
885                 
886                 int res=gtk_dialog_run(GTK_DIALOG(dialog));
887                 gtk_widget_destroy(dialog);
888                         
889                 if (res!=GTK_RESPONSE_YES) {
890                         return TRUE;
891                 }
892         }
893         
894         tX_shutdown=true;
895         
896         turn_audio_off();
897         vtt_class::delete_all();
898
899         if (update_tag)
900         gtk_timeout_remove(update_tag);
901         globals.width=main_window->allocation.width;
902         globals.height=main_window->allocation.height;
903
904         gtk_main_quit();
905         
906         return true;
907 }
908
909 void mplcfitx()
910 /* Most Probably Least Called Function In terminatorX :) */
911 {
912         show_about(0);
913 }
914
915 GtkSignalFunc seq_play(GtkWidget *w, void *)
916 {
917         if ((sequencer.is_empty()) &&   (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(seq_rec_btn)))) {
918                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
919                  {
920                         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.");
921                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 0);
922                  }
923         } else {
924                 if (seq_stop_override) return NULL;
925                         
926                 seq_adj_care=0;
927                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 1);
928                 sequencer.trig_play();
929         
930                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 1);
931         }
932         
933         return NULL;
934 }
935
936 GtkSignalFunc seq_stop(GtkWidget *w, void *)
937 {
938         if (!sequencer_ready) return NULL;
939         sequencer.trig_stop();
940         seq_adj_care=1;
941         seq_stop_override=1;
942         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(seq_play_btn), 0);
943         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(seq_rec_btn), 0);
944         while (gtk_events_pending()) gtk_main_iteration();              
945         seq_stop_override=0;
946         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 0); 
947         gtk_widget_set_sensitive(seq_slider, 1);        
948         gtk_widget_set_sensitive(engine_btn, 1);
949         gtk_widget_set_sensitive(seq_rec_btn, 1);
950
951         return NULL;
952 }
953
954 GtkSignalFunc seq_rec(GtkWidget *w, void *)
955 {
956         seq_adj_care=0;
957         gtk_widget_set_sensitive(seq_slider, 0);
958
959         if (seq_stop_override) return NULL;
960         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 1);
961         gtk_widget_set_sensitive(engine_btn, 0);
962         gtk_widget_set_sensitive(seq_rec_btn, 0);
963         sequencer.trig_rec();
964         
965         return NULL;
966 }
967
968 void seq_update_entry(const guint32 timestamp)
969 {
970         char buffer[20];
971         guint32 samples;
972         guint32 minu,sec,hun;   
973         guint32 sr;
974         
975         samples=timestamp*vtt_class::get_mix_buffer_size();
976         sr=vtt_class::get_last_sample_rate();
977         
978         if (samples>0) {
979                 minu=samples/(sr*60);
980                 samples-=(sr*60)*minu;
981         
982                 sec=samples/sr;
983                 samples-=sr*sec;
984         
985                 hun=samples/(sr/100);
986         } else {
987                 minu=sec=hun=0;
988         }
989         
990         sprintf(buffer, "%02i:%02i.%02i", minu, sec, hun);
991         gtk_entry_set_text(GTK_ENTRY(seq_entry), buffer);
992 }
993
994 void seq_update()
995 {
996         seq_update_entry(sequencer.get_timestamp());
997         gtk_adjustment_set_value(seq_adj, sequencer.get_timestamp_as_float());
998         
999 }
1000 gboolean seq_slider_released(GtkWidget *wid, void *d)
1001 {
1002         seq_adj_care=0;
1003         gtk_widget_set_sensitive(seq_slider, 0);        
1004         sequencer.forward_to_start_timestamp(0);
1005         gtk_widget_set_sensitive(seq_slider, 1);        
1006         seq_adj_care=1;
1007         
1008         return FALSE;
1009 }
1010 void sequencer_move(GtkWidget *wid, void *d)
1011 {
1012         guint32 pos;
1013         
1014         if (seq_adj_care) {
1015                 pos=sequencer.set_start_timestamp((float) GTK_ADJUSTMENT(wid)->value);
1016                 seq_update_entry(pos);  
1017         }
1018 }
1019
1020 #define add_sep();      dummy=gtk_hseparator_new ();\
1021         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);\
1022         gtk_widget_show(dummy);\
1023
1024 #define add_sep2();     dummy=gtk_hseparator_new ();\
1025         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);\
1026         gtk_widget_show(dummy);\
1027
1028 void fullscreen_toggle(GtkCheckMenuItem *item, gpointer data);
1029 void display_help();
1030 void display_browser();
1031
1032 tX_seqpar *del_sp=NULL;
1033 vtt_class *del_vtt=NULL;
1034 tx_menu_del_mode menu_del_mode=ALL_EVENTS_ALL_TURNTABLES;
1035
1036 GtkWidget *del_dialog=NULL;
1037
1038 GCallback menu_delete_all_events(GtkWidget *, void *param)
1039 {       
1040         del_dialog=create_tx_del_mode();
1041         tX_set_icon(del_dialog);
1042         
1043         GtkWidget *label=lookup_widget(del_dialog, "delmode_label");
1044         
1045         menu_del_mode=ALL_EVENTS_ALL_TURNTABLES;
1046         
1047         gtk_label_set_markup(GTK_LABEL(label), "Delete <b>all</b> events for <b>all</b> turntables.");
1048         gtk_widget_show(del_dialog);
1049         
1050         return NULL;
1051 }
1052
1053 GCallback menu_delete_all_events_for_vtt(GtkWidget *, vtt_class *vtt)
1054 {       
1055         if (!vtt) {
1056                 tX_error("No vtt passed to menu_delete_all_events_for_vtt().");
1057                 return FALSE;
1058         }
1059         
1060         char label_str[512];
1061         
1062         del_dialog=create_tx_del_mode();
1063         tX_set_icon(del_dialog);
1064
1065         del_vtt=vtt;
1066         GtkWidget *label=lookup_widget(del_dialog, "delmode_label");
1067         
1068         menu_del_mode=ALL_EVENTS_FOR_TURNTABLE;
1069         
1070         sprintf(label_str, "Delete <b>all</b> events for turntable <b>%s</b>.", vtt->name);
1071         gtk_label_set_markup(GTK_LABEL(label), label_str);
1072         gtk_widget_show(del_dialog);
1073         
1074         return NULL;
1075 }
1076
1077 GCallback menu_delete_all_events_for_sp(GtkWidget *, tX_seqpar *sp)
1078 {       
1079         if (!sp) {
1080                 tX_error("No sp passed to menu_delete_all_events_for_sp().");
1081                 return FALSE;
1082         }
1083         
1084         char label_str[512];
1085         
1086         del_dialog=create_tx_del_mode();
1087         tX_set_icon(del_dialog);
1088         
1089         GtkWidget *label=lookup_widget(del_dialog, "delmode_label");
1090         
1091         menu_del_mode=ALL_EVENTS_FOR_SP;
1092         del_sp=sp;
1093         sprintf(label_str, "Delete all <b>%s</b> events for turntable '%s'.", sp->get_name(), sp->get_vtt_name());
1094         gtk_label_set_markup(GTK_LABEL(label), label_str);
1095         gtk_widget_show(del_dialog);
1096
1097         return NULL;
1098 }
1099
1100 static GtkWidget *table_menu=NULL;
1101 static GtkWidget *table_menu_item=NULL;
1102
1103 GCallback create_table_sequencer_menu(GtkWidget *widget, void *param) 
1104 {
1105         char label[328];
1106         table_menu=gtk_menu_new();
1107         
1108         list <vtt_class *> :: iterator vtt;
1109
1110         for (vtt=vtt_class::main_list.begin(); vtt!=vtt_class::main_list.end(); vtt++) {
1111                 GtkWidget *menu_item=gtk_menu_item_new_with_label((*vtt)->name);
1112                 gtk_container_add (GTK_CONTAINER (table_menu), menu_item);
1113                 gtk_widget_show(menu_item);
1114                 
1115                 GtkWidget *seqpar_menu=gtk_menu_new();
1116                 list <tX_seqpar *> :: iterator sp;
1117                 
1118                 GtkWidget *all=gtk_menu_item_new_with_label("Delete All Events");
1119                 gtk_container_add (GTK_CONTAINER (seqpar_menu), all);
1120                 g_signal_connect(all, "activate", (GCallback) menu_delete_all_events_for_vtt, (*vtt));
1121                 gtk_widget_show(all);
1122
1123                 GtkWidget *sep = gtk_menu_item_new ();
1124                 gtk_widget_show(sep);
1125                 gtk_container_add(GTK_CONTAINER (seqpar_menu), sep);
1126                 gtk_widget_set_sensitive (sep, FALSE);
1127                 
1128                 for (sp=tX_seqpar::all.begin(); sp!=tX_seqpar::all.end(); sp++) {
1129                         if ((*sp)->vtt==(*vtt)) {
1130                                 sprintf(label, "Delete '%s' Events", (*sp)->get_name());
1131                                 GtkWidget *menu_item=gtk_menu_item_new_with_label(label);
1132                                 g_signal_connect(menu_item, "activate", (GCallback) menu_delete_all_events_for_sp, (*sp));
1133                                 gtk_container_add(GTK_CONTAINER(seqpar_menu), menu_item);
1134                                 gtk_widget_show(menu_item);
1135                         }
1136                 }
1137                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), seqpar_menu);
1138         }
1139         
1140         gtk_menu_item_set_submenu(GTK_MENU_ITEM(table_menu_item), table_menu);
1141         
1142         return NULL;
1143 }
1144
1145 GCallback toggle_confirm_events(GtkWidget *widget, void *dummy)
1146 {       
1147         globals.confirm_events=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));     
1148         return NULL;    
1149 }
1150
1151 GCallback toggle_auto_assign(GtkWidget *widget, void *dummy)
1152 {       
1153         globals.auto_assign_midi=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
1154 #ifdef USE_ALSA_MIDI_IN
1155         tX_midiin::auto_assign_midi_mappings(NULL, NULL);
1156 #endif
1157 /*      if (globals.auto_assign_midi) {
1158                 GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window), 
1159                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_YES_NO,
1160                 "Note: Enabling \"Auto Assign Default MIDI Settings\" will constantly overwrite the\
1161 MIDI mappings for the standard parameters. \
1162 Are you sure you really want this?");
1163                 
1164                 int res=gtk_dialog_run(GTK_DIALOG(dialog));
1165                 gtk_widget_destroy(dialog);
1166                         
1167                 if (res!=GTK_RESPONSE_YES) {
1168                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), 0);
1169                 }                       
1170         } */
1171         return NULL;    
1172 }
1173
1174 void create_master_menu() 
1175 {
1176         GtkWidget *menu_item;
1177         GtkWidget *sub_menu;
1178         GtkWidget *label;
1179         
1180         GtkAccelGroup* accel_group=gtk_accel_group_new();
1181         gtk_window_add_accel_group(GTK_WINDOW(main_window), accel_group);
1182
1183         /* FILE */
1184         menu_item = gtk_menu_item_new_with_mnemonic ("_File");
1185         gtk_widget_show (menu_item);
1186         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1187
1188         sub_menu = gtk_menu_new ();
1189         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1190
1191         menu_item = gtk_image_menu_item_new_from_stock ("gtk-open", accel_group);
1192         label = gtk_bin_get_child(GTK_BIN(menu_item));
1193         gtk_label_set_text(GTK_LABEL(label), "Load Audio File");
1194         // Warning: gtk+ stock hacks ahead...
1195         gtk_widget_remove_accelerator(menu_item, accel_group, GDK_O, GDK_CONTROL_MASK);
1196         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
1197         gtk_widget_show (menu_item);
1198         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1199         g_signal_connect(menu_item, "activate", (GCallback) load_audio, NULL);
1200
1201         menu_item = gtk_menu_item_new ();
1202         gtk_widget_show (menu_item);
1203         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1204         gtk_widget_set_sensitive (menu_item, FALSE);
1205
1206         menu_item = gtk_image_menu_item_new_from_stock ("gtk-new", accel_group);
1207         label = gtk_bin_get_child(GTK_BIN(menu_item));
1208         gtk_label_set_text(GTK_LABEL(label), "New Set");
1209         gtk_widget_show (menu_item);
1210         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1211         g_signal_connect(menu_item, "activate", (GCallback) new_tables, NULL);
1212
1213         menu_item = gtk_image_menu_item_new_from_stock ("gtk-open", accel_group);
1214         label = gtk_bin_get_child(GTK_BIN(menu_item));
1215         gtk_label_set_text(GTK_LABEL(label), "Open Set File");
1216         gtk_widget_show (menu_item);
1217         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1218         g_signal_connect(menu_item, "activate", (GCallback) load_tables, NULL);
1219
1220         menu_item = gtk_image_menu_item_new_from_stock ("gtk-save", accel_group);
1221         label = gtk_bin_get_child(GTK_BIN(menu_item));
1222         gtk_label_set_text(GTK_LABEL(label), "Save Set");
1223         gtk_widget_show (menu_item);
1224         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1225         g_signal_connect(menu_item, "activate", (GCallback) save_tables, NULL);
1226
1227         menu_item = gtk_image_menu_item_new_from_stock ("gtk-save-as", accel_group);
1228         label = gtk_bin_get_child(GTK_BIN(menu_item));
1229         gtk_label_set_text(GTK_LABEL(label), "Save Set As");
1230         gtk_widget_show (menu_item);
1231         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1232         g_signal_connect(menu_item, "activate", (GCallback) save_tables_as, NULL);
1233
1234         menu_item = gtk_menu_item_new ();
1235         gtk_widget_show (menu_item);
1236         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1237         gtk_widget_set_sensitive (menu_item, FALSE);
1238
1239         menu_item = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
1240         gtk_widget_show (menu_item);
1241         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1242         g_signal_connect(menu_item, "activate", (GCallback) quit, NULL);
1243
1244         /* Turntables */
1245         menu_item = gtk_menu_item_new_with_mnemonic ("_Turntables");
1246         gtk_widget_show (menu_item);
1247         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1248         
1249         sub_menu = gtk_menu_new ();
1250         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1251
1252         menu_item = gtk_menu_item_new_with_mnemonic("_Add Turntable");
1253         gtk_widget_show (menu_item);
1254         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1255         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_A, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);    
1256         g_signal_connect(menu_item, "activate", (GCallback) new_table, NULL);
1257
1258         menu_item = gtk_menu_item_new ();
1259         gtk_widget_show (menu_item);
1260         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1261         gtk_widget_set_sensitive (menu_item, FALSE);
1262
1263         menu_item = gtk_menu_item_new_with_mnemonic("Assign _Default MIDI Mappings");
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_M, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
1267
1268 #ifdef USE_ALSA_MIDI_IN
1269         g_signal_connect(menu_item, "activate", G_CALLBACK(tX_midiin::auto_assign_midi_mappings), (void *) true);
1270 #else
1271         gtk_widget_set_sensitive(menu_item, FALSE);
1272 #endif
1273
1274         menu_item = gtk_check_menu_item_new_with_mnemonic("A_uto Assign Default MIDI Mappings");
1275         gtk_widget_show (menu_item);
1276         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1277         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), globals.auto_assign_midi);
1278 #ifdef USE_ALSA_MIDI_IN 
1279         g_signal_connect(menu_item, "activate", (GCallback) toggle_auto_assign, NULL);
1280 #else
1281         gtk_widget_set_sensitive(menu_item, FALSE);
1282 #endif
1283
1284         menu_item = gtk_menu_item_new_with_mnemonic("_Clear MIDI Mappings");
1285         gtk_widget_show (menu_item);
1286         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1287         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_C, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
1288
1289 #ifdef USE_ALSA_MIDI_IN
1290         g_signal_connect(menu_item, "activate", G_CALLBACK(tX_midiin::clear_midi_mappings), (void *) true);
1291 #else
1292         gtk_widget_set_sensitive(menu_item, FALSE);
1293 #endif
1294
1295         menu_item = gtk_menu_item_new ();
1296         gtk_widget_show (menu_item);
1297         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1298         gtk_widget_set_sensitive (menu_item, FALSE);
1299
1300         menu_item = gtk_check_menu_item_new_with_mnemonic("_Record Audio To Disk");
1301         rec_menu_item = menu_item;
1302         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_R, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
1303         gtk_widget_show (menu_item);
1304         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1305         g_signal_connect(menu_item, "activate", (GCallback) tape_on, NULL);
1306
1307         /* Sequencer */
1308         
1309         menu_item = gtk_menu_item_new_with_mnemonic("_Sequencer");
1310         gtk_widget_show (menu_item);
1311         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1312
1313         sub_menu = gtk_menu_new ();
1314         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1315                 
1316         table_menu = gtk_menu_new();
1317         menu_item = gtk_menu_item_new_with_mnemonic("Delete _Events");
1318         delete_all_vtt_item = menu_item;
1319         table_menu_item = menu_item;
1320         gtk_menu_item_set_submenu(GTK_MENU_ITEM(table_menu_item), table_menu);
1321         
1322         gtk_widget_show (menu_item);
1323         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1324         g_signal_connect_swapped (G_OBJECT (menu_item), "select", G_CALLBACK (create_table_sequencer_menu), NULL);
1325         
1326         menu_item = gtk_menu_item_new_with_mnemonic("Delete _All Events");
1327         delete_all_item = menu_item;
1328         gtk_widget_show (menu_item);
1329         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1330         g_signal_connect(menu_item, "activate", (GCallback) menu_delete_all_events, NULL);
1331
1332         menu_item = gtk_menu_item_new ();
1333         gtk_widget_show (menu_item);
1334         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1335         gtk_widget_set_sensitive (menu_item, FALSE);
1336
1337         menu_item = gtk_check_menu_item_new_with_mnemonic("_Confirm Recorded Events");
1338         //rec_menu_item = menu_item;
1339         gtk_widget_show (menu_item);
1340         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1341         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), globals.confirm_events);
1342         g_signal_connect(menu_item, "activate", (GCallback) toggle_confirm_events, NULL);
1343
1344         /* Options */
1345         menu_item = gtk_menu_item_new_with_mnemonic ("_Options");
1346         gtk_widget_show (menu_item);
1347         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1348
1349         sub_menu = gtk_menu_new ();
1350         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1351                 
1352         menu_item = gtk_check_menu_item_new_with_mnemonic("_Fullscreen");
1353         fullscreen_item = menu_item;
1354         gtk_widget_show (menu_item);
1355         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1356
1357         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), globals.fullscreen_enabled);
1358         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F11, (GdkModifierType) 0, GTK_ACCEL_VISIBLE);
1359         g_signal_connect(menu_item, "activate", (GCallback) fullscreen_toggle, NULL);
1360         
1361         menu_item = gtk_menu_item_new ();
1362         gtk_widget_show (menu_item);
1363         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1364         gtk_widget_set_sensitive (menu_item, FALSE);
1365
1366         menu_item = gtk_image_menu_item_new_from_stock ("gtk-preferences", accel_group);
1367         gtk_widget_show (menu_item);
1368         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1369         g_signal_connect(menu_item, "activate", (GCallback) display_options, NULL);
1370
1371         /* HELP */ 
1372         menu_item = gtk_menu_item_new_with_mnemonic ("_Help");
1373         gtk_widget_show (menu_item);
1374         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1375         gtk_menu_item_set_right_justified(GTK_MENU_ITEM(menu_item), TRUE);
1376         
1377         sub_menu = gtk_menu_new ();
1378         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1379
1380         menu_item = gtk_menu_item_new_with_mnemonic ("_Contents");
1381         gtk_widget_show (menu_item);
1382         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1383         g_signal_connect(menu_item, "activate", (GCallback) display_help, NULL);
1384         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F1, (GdkModifierType) 0, GTK_ACCEL_VISIBLE);
1385
1386         menu_item = gtk_menu_item_new_with_mnemonic ("_About");
1387         gtk_widget_show (menu_item);
1388         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1389         g_signal_connect(menu_item, "activate", (GCallback) mplcfitx, NULL);
1390         
1391         menu_item = gtk_menu_item_new ();
1392         gtk_widget_show (menu_item);
1393         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1394         gtk_widget_set_sensitive (menu_item, FALSE);
1395
1396         menu_item = gtk_menu_item_new_with_mnemonic ("_Visit terminatorX.cx");
1397         gtk_widget_show (menu_item);
1398         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1399         g_signal_connect(menu_item, "activate", (GCallback) display_browser, NULL);
1400 }
1401
1402 void create_mastergui(int x, int y)
1403 {
1404         GtkWidget *mother_of_all_boxen;
1405         GtkWidget *main_vbox;
1406         GtkWidget *right_hbox;
1407         GtkWidget *left_hbox;
1408         GtkWidget *control_box;
1409         //GtkWidget *sequencer_box;
1410         GtkAdjustment *dumadj;
1411         GtkWidget *dummy;
1412         GtkWidget *master_vol_box;
1413         GtkWidget *status_box;
1414         
1415         gui_tooltips=gtk_tooltips_new();
1416
1417         main_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
1418
1419         gtk_window_set_wmclass(GTK_WINDOW(main_window), "terminatorX", "tX_mastergui");
1420
1421         gtk_window_set_title(GTK_WINDOW(main_window), "terminatorX");
1422         
1423         gtk_container_set_border_width(GTK_CONTAINER(main_window), 5);
1424
1425         gtk_widget_realize(main_window);
1426         
1427         mother_of_all_boxen=gtk_vbox_new(FALSE, 5);
1428         gtk_container_add(GTK_CONTAINER(main_window), mother_of_all_boxen);
1429         gtk_widget_show(mother_of_all_boxen);   
1430         
1431         main_menubar=gtk_menu_bar_new();
1432         gtk_box_pack_start(GTK_BOX(mother_of_all_boxen), main_menubar, WID_FIX);
1433         gtk_widget_show(main_menubar);  
1434         
1435         create_master_menu();
1436         
1437         main_vbox=gtk_hbox_new(FALSE, 5);
1438         gtk_box_pack_start(GTK_BOX(mother_of_all_boxen), main_vbox, WID_DYN);
1439         gtk_widget_show(main_vbox);
1440         
1441         left_hbox=gtk_vbox_new(FALSE, 5);
1442         gtk_box_pack_start(GTK_BOX(main_vbox), left_hbox, WID_DYN);
1443         gtk_widget_show(left_hbox);
1444         
1445         control_box=gtk_hbox_new(FALSE, 5);
1446         gtk_box_pack_start(GTK_BOX(left_hbox), control_box, WID_FIX);
1447         gtk_widget_show(control_box);
1448         
1449         dummy=gtk_hseparator_new();
1450         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_FIX);
1451         gtk_widget_show(dummy);
1452
1453     /* control_box contents */
1454
1455         dummy=tx_xpm_label_box(AUDIOENGINE, "Audio");
1456         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1457         gtk_widget_show(dummy);
1458         
1459         dummy=tx_xpm_button_new(POWER,"Power ", 1);
1460         connect_button(dummy,audio_on, NULL);
1461         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1462         gui_set_tooltip(dummy, "Turn the audio engine on/off.");
1463         gtk_widget_show(dummy);
1464         engine_btn=dummy;
1465         
1466         grab_button=tx_xpm_button_new(GRAB, "Mouse Grab ", 1);
1467         gtk_box_pack_start(GTK_BOX(control_box), grab_button, WID_FIX);
1468         connect_button(grab_button, grab_on, NULL);
1469         gui_set_tooltip(grab_button, "Enter the mouse grab mode operation. Press <ESCAPE> to exit grab mode.");
1470         gtk_widget_show(grab_button);
1471
1472         dummy=gtk_vseparator_new();
1473         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1474         gtk_widget_show(dummy);
1475     
1476         dummy=tx_xpm_label_box(SEQUENCER, "Seq.");
1477         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1478         gtk_widget_show(dummy);
1479
1480         dummy=tx_xpm_button_new(PLAY,"Play ", 1);
1481         connect_button(dummy, seq_play, NULL);
1482         seq_play_btn=dummy;
1483         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1484         gui_set_tooltip(dummy, "Playback previously recorded events from the sequencer. This will turn on the audio engine automagically.");
1485         gtk_widget_show(dummy);
1486
1487         dummy=tx_xpm_button_new(STOP,"Stop ", 0);
1488         seq_stop_btn=dummy;
1489         connect_button(dummy, seq_stop, NULL);  
1490         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1491         gui_set_tooltip(dummy, "Stop the playback of sequencer events.");
1492         gtk_widget_show(dummy);
1493
1494         dummy=tx_xpm_button_new(RECORD,"Record ", 1);
1495         connect_button(dummy, seq_rec, NULL);
1496         seq_rec_btn=dummy;
1497         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1498         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.");
1499         gtk_widget_show(dummy);
1500
1501         dummy=gtk_label_new("Pos:");
1502         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1503         gtk_widget_show(dummy);
1504         
1505         dummy=gtk_entry_new();
1506         gtk_entry_set_max_length(GTK_ENTRY(dummy), 12);
1507         seq_entry=dummy;
1508         //gtk_widget_set_usize(dummy, 65, 20);
1509         gtk_entry_set_text(GTK_ENTRY(dummy), "00:00.00");
1510         gtk_entry_set_width_chars(GTK_ENTRY(dummy), 9);
1511         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1512         gtk_widget_show(dummy);
1513
1514         dumadj=(GtkAdjustment*) gtk_adjustment_new(0, 0, 100, 0.1, 1, 1);
1515         seq_adj=dumadj;
1516         connect_adj(dumadj, sequencer_move, NULL);      
1517         dummy=gtk_hscale_new(dumadj);
1518         gtk_widget_set_size_request(dummy, 65, 20);
1519         seq_slider=dummy;
1520         g_signal_connect(G_OBJECT(seq_slider), "button-release-event", (GtkSignalFunc) seq_slider_released, NULL);
1521         gtk_scale_set_draw_value(GTK_SCALE(dummy), FALSE);
1522         
1523         gui_set_tooltip(dummy, "Select the start position for the sequencer in song-time.");
1524         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_DYN);
1525         gtk_widget_show(dummy);
1526         
1527         dummy=gtk_hbox_new(FALSE,2); //gtk_hpaned_new ();
1528         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_DYN);
1529         gtk_widget_show(dummy);
1530         
1531         tt_parent=dummy;
1532
1533     panel_bar=gtk_hbox_new(TRUE,2);
1534         gtk_box_pack_start(GTK_BOX(left_hbox), panel_bar, WID_FIX);
1535
1536         control_parent=gtk_hbox_new(FALSE,0);
1537         gtk_box_pack_start(GTK_BOX(tt_parent), control_parent, WID_FIX);
1538         gtk_widget_show(control_parent);
1539
1540         dummy=gtk_vseparator_new();
1541         gtk_box_pack_start(GTK_BOX(tt_parent), dummy, WID_FIX);
1542         gtk_widget_show(dummy);
1543
1544         audio_parent=gtk_vbox_new(FALSE,2);
1545         gtk_box_pack_start(GTK_BOX(tt_parent), audio_parent, WID_DYN);
1546         gtk_widget_show(audio_parent);
1547         
1548         dummy=gtk_vseparator_new();
1549         gtk_box_pack_start(GTK_BOX(main_vbox), dummy, WID_FIX);
1550         gtk_widget_show(dummy);
1551                 
1552         right_hbox=gtk_vbox_new(FALSE, 5);
1553         gtk_box_pack_start(GTK_BOX(main_vbox), right_hbox, WID_FIX);
1554         gtk_widget_show(right_hbox);
1555
1556         /* Master */
1557         
1558         dummy=gtk_label_new(NULL);
1559         gtk_label_set_markup(GTK_LABEL(dummy),"<b>Master</b>");
1560         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1561         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1562         gtk_widget_show(dummy); 
1563
1564         dummy=gtk_hseparator_new();
1565         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1566         gtk_widget_show(dummy);
1567
1568          /* Pitch */
1569          
1570         /*dummy=gtk_label_new("Pitch:");
1571         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1572         gtk_widget_show(dummy);*/
1573
1574         dumadj=(GtkAdjustment*) gtk_adjustment_new(globals.pitch, -3, 3, 0.001, 0.001, 0.01);
1575         pitch_adj=dumadj;
1576         connect_adj(dumadj, master_pitch_changed, NULL);
1577         
1578         tX_extdial *pdial=new tX_extdial("Pitch", pitch_adj, &sp_master_pitch, true);
1579         gtk_box_pack_start(GTK_BOX(right_hbox), pdial->get_widget(), WID_FIX);
1580         gui_set_tooltip(pdial->get_entry(), "Use this dial to adjust the master pitch (affecting *all* turntables).");
1581         
1582         dummy=gtk_hseparator_new();
1583         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1584         gtk_widget_show(dummy);
1585         
1586         /* Volume */
1587         master_vol_box=gtk_hbox_new(FALSE, 5);
1588         gtk_box_pack_start(GTK_BOX(right_hbox), master_vol_box, WID_DYN);
1589         gtk_widget_show(master_vol_box);        
1590         
1591         dumadj=(GtkAdjustment*) gtk_adjustment_new(globals.volume, 0, 2, 0.01, 0.05, 0.000);
1592         volume_adj=dumadj;
1593
1594         connect_adj(dumadj, master_volume_changed, NULL);       
1595         dummy=gtk_vscale_new(dumadj);
1596         gtk_range_set_inverted(GTK_RANGE(dummy), TRUE);
1597         gtk_scale_set_draw_value(GTK_SCALE(dummy), False);
1598         g_signal_connect(G_OBJECT(dummy), "button_press_event", (GtkSignalFunc) tX_seqpar::tX_seqpar_press, &sp_master_volume); 
1599         
1600         gtk_box_pack_end(GTK_BOX(master_vol_box), dummy, WID_FIX);
1601         gtk_widget_show(dummy); 
1602         gui_set_tooltip(dummy, "Adjust the master volume. This parameter will effect *all* turntables in the set.");
1603         
1604         main_flash=gtk_tx_flash_new();
1605         gtk_box_pack_end(GTK_BOX(master_vol_box), main_flash, WID_DYN);
1606         gtk_widget_show(main_flash);
1607
1608         dummy=gtk_label_new("Volume");
1609         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1610         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1611         gtk_widget_show(dummy);
1612
1613         /* STATUS BOX */ 
1614         dummy=gtk_hseparator_new();
1615         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1616         gtk_widget_show(dummy);
1617         
1618         status_box=gtk_vbox_new(FALSE, 0);
1619         gtk_box_pack_start(GTK_BOX(right_hbox), status_box, WID_FIX);
1620         gtk_widget_show(status_box);
1621         
1622         dummy=gtk_label_new("0");
1623         used_mem=dummy;
1624         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1625         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1626         gtk_widget_show(dummy);
1627
1628         dummy=gtk_label_new("Mem/kB:");
1629         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1630         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1631         gtk_widget_show(dummy);
1632         
1633         /*add_sep2();
1634
1635         dummy=gtk_label_new("1");
1636         no_of_vtts=dummy;
1637         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1638         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1639         gtk_widget_show(dummy);
1640
1641         dummy=gtk_label_new("Vtts:");
1642         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1643         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1644         gtk_widget_show(dummy);*/
1645
1646         add_sep2();
1647
1648         dummy=gtk_label_new("Rel. "VERSION);
1649         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1650         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1651         gtk_widget_show(dummy);
1652
1653         /*dummy=gtk_label_new("Release:");
1654         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1655         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1656         gtk_widget_show(dummy);*/
1657         
1658         add_sep2();
1659
1660         dummy=gtk_label_new(NULL);
1661         gtk_label_set_markup(GTK_LABEL(dummy), "<b>Status</b>");
1662         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1663         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1664         gtk_widget_show(dummy);
1665         
1666         /* END GUI */
1667         
1668         gtk_window_set_default_size(GTK_WINDOW(main_window), x, y);     
1669         gtk_widget_set_sensitive(grab_button, 0);
1670
1671         new_table(NULL, NULL); // to give the user something to start with ;)
1672
1673         g_signal_connect (G_OBJECT(main_window), "delete-event", (GtkSignalFunc) quit, NULL);
1674         
1675         if (globals.tooltips) gtk_tooltips_enable(gui_tooltips);
1676         else gtk_tooltips_disable(gui_tooltips);
1677 }
1678
1679 gfloat old_percent=-1;
1680
1681 void note_destroy(GtkWidget *widget, GtkWidget *mbox)
1682 {
1683         gtk_widget_destroy(GTK_WIDGET(mbox));
1684 }
1685
1686 void tx_note(const char *message, bool isError, GtkWindow *window)
1687 {
1688         char buffer[4096]="terminatorX ";
1689         if (isError) {
1690                 strcat(buffer, "note:\n\n");
1691         } else {
1692                 strcat(buffer, "error:\n\n");
1693         }
1694         
1695         if (!window) window==GTK_WINDOW(main_window);
1696         
1697         GtkWidget *dialog=gtk_message_dialog_new(window,
1698                 GTK_DIALOG_DESTROY_WITH_PARENT,
1699                 isError ? GTK_MESSAGE_ERROR : GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "%s", message);
1700         gtk_dialog_run(GTK_DIALOG(dialog));
1701         gtk_widget_destroy(dialog);     
1702 }
1703
1704
1705 void tx_l_note(const char *message)
1706 {
1707         char buffer[4096]="Plugin info:\n\n";
1708         strcat(buffer, message);
1709         
1710         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window),
1711                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "%s", message);
1712         gtk_dialog_run(GTK_DIALOG(dialog));
1713         gtk_widget_destroy(dialog);     
1714 }
1715
1716 void add_to_panel_bar(GtkWidget *button) 
1717 {
1718         buttons_on_panel_bar++;
1719         gtk_box_pack_start(GTK_BOX(panel_bar), button, WID_DYN);
1720         gtk_widget_show(panel_bar);
1721 }
1722
1723 void remove_from_panel_bar(GtkWidget *button) 
1724 {
1725         buttons_on_panel_bar--;
1726         gtk_container_remove(GTK_CONTAINER(panel_bar), button);
1727         if (buttons_on_panel_bar==0) gtk_widget_hide(panel_bar);
1728 }
1729
1730 /* Fullscreen code... */
1731 #define _WIN_LAYER_TOP          -1
1732 #define _WIN_LAYER_NORMAL       4
1733 #define _NET_WM_STATE_REMOVE    0
1734 #define _NET_WM_STATE_ADD       1
1735 #define _NET_WM_STATE_TOGGLE    2
1736
1737 void fullscreen_toggle(GtkCheckMenuItem *item, gpointer data) {
1738         XEvent xev;
1739         Window win=GDK_WINDOW_XID(main_window->window);
1740         Display *disp=GDK_WINDOW_XDISPLAY(main_window->window);
1741         
1742         globals.fullscreen_enabled=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(fullscreen_item));
1743         
1744         /* Top layer.. */
1745         xev.xclient.type = ClientMessage;
1746         xev.xclient.serial = 0;
1747         xev.xclient.send_event = True;
1748         xev.xclient.display = disp;
1749         xev.xclient.window = win;
1750         xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_WIN_LAYER");
1751         xev.xclient.format = 32;
1752         xev.xclient.data.l[0] = globals.fullscreen_enabled ? _WIN_LAYER_TOP : _WIN_LAYER_NORMAL ;
1753         XSendEvent(disp, GDK_WINDOW_XID (gdk_get_default_root_window ()),
1754                 False, SubstructureRedirectMask | SubstructureNotifyMask,
1755                 &xev);
1756         
1757         /* Fullscreen */
1758         xev.xclient.type = ClientMessage;
1759         xev.xclient.serial = 0;
1760         xev.xclient.send_event = True;
1761         xev.xclient.display = disp;
1762         xev.xclient.window = win;
1763         xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_NET_WM_STATE");
1764         xev.xclient.format = 32;
1765         xev.xclient.data.l[0] = globals.fullscreen_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1766         xev.xclient.data.l[1] = gdk_x11_atom_to_xatom (gdk_atom_intern ("_NET_WM_STATE_FULLSCREEN", TRUE));
1767         xev.xclient.data.l[2] = gdk_x11_atom_to_xatom (GDK_NONE);
1768         XSendEvent(gdk_display, GDK_WINDOW_XID (gdk_get_default_root_window ()),
1769                 False, SubstructureRedirectMask | SubstructureNotifyMask,
1770                 &xev);  
1771 }
1772
1773 void fullscreen_setup() {
1774         if (globals.fullscreen_enabled) {
1775                 fullscreen_toggle(NULL, NULL);
1776         }
1777 }
1778
1779 void display_mastergui()
1780 {
1781         GtkWidget *top;
1782         gtk_widget_realize(main_window);
1783         tX_set_icon(main_window);
1784         load_knob_pixs();
1785         gtk_widget_show(main_window);
1786         fullscreen_setup();     
1787         top=gtk_widget_get_toplevel(main_window);
1788         xwindow=GDK_WINDOW_XWINDOW(top->window);
1789 }
1790
1791 pid_t help_child=0;
1792 GTimer *help_timer=NULL;
1793 int help_tag=-1;
1794
1795 int help_checker()
1796 {
1797         gdouble time;
1798         gulong ms;
1799         int status;
1800         int result=waitpid(help_child, &status, WNOHANG);
1801         
1802         if (result==0) {
1803                 time=g_timer_elapsed(help_timer, &ms);
1804                 if (time > 5) {
1805                         /* 5 seconds and it's still running - so we assume everything's OK. */
1806                         tX_debug("No longer waiting for gnome-help..");
1807                         gtk_idle_remove(help_tag);
1808                         help_tag=-1;
1809                 }
1810         } else {
1811                 /* We are still here and the child exited - that could mean trouble. */
1812                 tx_note("Couldn't run the gnome-help command (alias \"yelp\") to display the terminatorX manual. Please ensure that \"yelp\" is installed.", true);             
1813                 
1814                 gtk_idle_remove(help_tag);
1815                 help_tag=-1;
1816         }
1817         return TRUE;    
1818 }
1819
1820 #ifndef INSTALL_PREFIX
1821 #define INSTALL_PREFIX "/usr/local/share"
1822 #endif
1823
1824 void display_help()
1825 {       
1826         help_child=fork();
1827
1828         if (help_tag!=-1) {
1829                 gtk_idle_remove(help_tag);
1830                 if (help_timer) g_timer_destroy(help_timer);
1831                 help_child=0;
1832                 help_tag=-1;
1833                 help_timer=NULL;
1834         }
1835         
1836         if (help_child==0) {
1837                 // child
1838                 // execlp("gnome-help","gnome-help","ghelp:/" INSTALL_PREFIX "/terminatorX/doc/terminatorX-manual/C/terminatorX-manual.xml", NULL);
1839                 execlp("gnome-help","gnome-help","ghelp://" XML_MANUAL, NULL);          
1840                 _exit(-1);
1841         } else if (help_child==-1) {
1842                 tx_note("System error: couldn't fork() to run the help process.", true);
1843         } else {
1844                 help_timer=g_timer_new();
1845                 g_timer_start(help_timer);
1846         
1847                 help_tag=gtk_idle_add((GtkFunction) help_checker, NULL);
1848         }
1849 }
1850
1851 pid_t browser_child=0;
1852 GTimer *browser_timer=NULL;
1853 int browser_tag=-1;
1854
1855 int browser_checker()
1856 {
1857         gdouble time;
1858         gulong ms;
1859         int status;
1860         int result=waitpid(browser_child, &status, WNOHANG);
1861         
1862         if (result==0) {
1863                 time=g_timer_elapsed(browser_timer, &ms);
1864                 if (time > 5) {
1865                         /* 5 seconds and it's still running - so we assume everything's OK. */
1866                         tX_debug("No longer waiting for a browser..");
1867                         gtk_idle_remove(browser_tag);
1868                         browser_tag=-1;
1869                 }
1870         } else {
1871                 /* We are still here and the child exited - that could mean trouble. */
1872                 tx_note("Failed to run a suitable web browser - if there's one installed on this system, please run it and forward yourself to:\nhttp://terminatorX.cx", true);         
1873                 
1874                 gtk_idle_remove(browser_tag);
1875                 browser_tag=-1;
1876         }
1877         return TRUE;    
1878 }
1879
1880 void display_browser()
1881 {       
1882         browser_child=fork();
1883
1884         if (browser_tag!=-1) {
1885                 gtk_idle_remove(browser_tag);
1886                 if (browser_timer) g_timer_destroy(browser_timer);
1887                 browser_child=0;
1888                 browser_tag=-1;
1889                 browser_timer=NULL;
1890         }
1891         
1892         if (browser_child==0) {
1893                 // child
1894                 execlp("mozilla","mozilla","http://terminatorX.cx", NULL);
1895                 execlp("netscape","netscape","http://terminatorX.cx", NULL);
1896                 execlp("galeon","galeon","http://terminatorX.cx", NULL);
1897                 execlp("konqueror","konqueror","http://terminatorX.cx", NULL);          
1898                 _exit(-1);
1899         } else if (browser_child==-1) {
1900                 tx_note("System error: couldn't fork() to run the browser process.", true);
1901         } else {
1902                 browser_timer=g_timer_new();
1903                 g_timer_start(browser_timer);
1904         
1905                 browser_tag=gtk_idle_add((GtkFunction) browser_checker, NULL);
1906         }
1907 }
1908
1909
1910
1911 GdkCursor *tX_cursor::cursors[MAX_CURSOR]={NULL, NULL, NULL};
1912 tX_cursor::cursor_shape tX_cursor::current_shape=tX_cursor::DEFAULT_CURSOR;
1913
1914 void tX_cursor::set_cursor(cursor_shape shape)
1915 {
1916         switch (shape) {
1917                 case DEFAULT_CURSOR:
1918                         cursors[shape]=NULL;
1919                         break;
1920                 
1921                 case WAIT_CURSOR:
1922                         if (!cursors[shape]) cursors[shape]=gdk_cursor_new(GDK_WATCH);
1923                         break;
1924                 
1925                 case WAIT_A_SECOND_CURSOR:
1926                         /* FIXME: What's that short-time wait cursor's id? */
1927                         if (!cursors[shape]) cursors[shape]=gdk_cursor_new(GDK_WATCH);
1928                         break;
1929                 
1930                 default:
1931                         tX_debug("No such cursor shape.");
1932                         return;
1933         }
1934         
1935         /* Still here? Ok... */
1936         current_shape=shape;
1937         
1938         gdk_window_set_cursor(main_window->window, cursors[shape]);
1939 }
1940
1941 GdkCursor *tX_cursor::get_cursor()
1942 {
1943         return cursors[current_shape];
1944 }
1945
1946 void tX_cursor::reset_cursor()
1947 {
1948         current_shape=DEFAULT_CURSOR;
1949         gdk_window_set_cursor(main_window->window, cursors[current_shape]);
1950 }