1 /*
2  * Copyright (C) 2002 2003 2005 2007, Magnus Hjorth
3  *
4  * This file is part of mhWaveEdit.
5  *
6  * mhWaveEdit is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * mhWaveEdit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mhWaveEdit; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 
22 #include <config.h>
23 
24 #include <gtk/gtk.h>
25 #include "rawdialog.h"
26 #include "inifile.h"
27 #include "formatselector.h"
28 #include "int_box.h"
29 #include "main.h"
30 #include "gettext.h"
31 #include "mainloop.h"
32 
33 static gboolean ok_flag, destroy_flag;
34 static Dataformat fmt;
35 static FormatSelector *fs;
36 static Intbox *offset_box;
37 static gint maxhdrsize;
38 
rawdialog_ok(GtkButton * button,gpointer user_data)39 static void rawdialog_ok(GtkButton *button, gpointer user_data)
40 {
41      if (format_selector_check(fs) ||
42 	 intbox_check_limit(offset_box,0,maxhdrsize,_("header size"))) {
43           gtk_signal_emit_stop_by_name(GTK_OBJECT(button),"clicked");
44           return;
45      }
46      format_selector_get(fs,&fmt);
47      ok_flag = TRUE;
48 }
49 
rawdialog_destroy(GtkObject * object,gpointer user_data)50 static void rawdialog_destroy(GtkObject *object, gpointer user_data)
51 {
52      destroy_flag = TRUE;
53 }
54 
rawdialog_execute(gchar * filename,gint filesize,guint * offset)55 Dataformat *rawdialog_execute(gchar *filename, gint filesize, guint *offset)
56 {
57      GtkWindow *w;
58      GtkWidget *a,*b,*c;
59      gchar *ch;
60 
61      memset(&fmt,0,sizeof(fmt));
62      fmt.type = DATAFORMAT_PCM;
63      fmt.samplerate = 22050;
64      fmt.channels = 1;
65      fmt.samplesize = 1;
66      fmt.samplebytes= fmt.samplesize * fmt.channels;
67      fmt.sign = FALSE;
68      fmt.bigendian = IS_BIGENDIAN;
69      dataformat_get_from_inifile("rawDialog",TRUE,&fmt);
70      maxhdrsize = filesize;
71 
72      w = GTK_WINDOW(gtk_window_new(GTK_WINDOW_DIALOG));
73      gtk_window_set_title(w,_("Unknown file format"));
74      gtk_window_set_modal(w,TRUE);
75      gtk_container_set_border_width(GTK_CONTAINER(w),5);
76      gtk_signal_connect(GTK_OBJECT(w),"destroy",
77           GTK_SIGNAL_FUNC(rawdialog_destroy),NULL);
78      a = gtk_vbox_new(FALSE,3);
79      gtk_container_add(GTK_CONTAINER(w),a);
80      ch = g_strdup_printf(_("The format of file '%s' could not be recognized.\n\n"
81 			  "Please specify the sample format below."),filename);
82      b = gtk_label_new(ch);
83      gtk_label_set_line_wrap(GTK_LABEL(b),TRUE);
84      gtk_box_pack_start(GTK_BOX(a),b,TRUE,FALSE,0);
85      g_free(ch);
86      b = format_selector_new(TRUE);
87      fs = FORMAT_SELECTOR(b);
88      format_selector_set(fs,&fmt);
89      gtk_container_add(GTK_CONTAINER(a),b);
90      b = gtk_hbox_new(FALSE,0);
91      gtk_container_add(GTK_CONTAINER(a),b);
92      c = gtk_label_new(_("Header bytes: "));
93      gtk_box_pack_start(GTK_BOX(b),c,FALSE,FALSE,0);
94      c = intbox_new(inifile_get_guint32("rawDialog_offset",0));
95      offset_box = INTBOX(c);
96      gtk_box_pack_start(GTK_BOX(b),c,FALSE,FALSE,0);
97      b = gtk_hseparator_new();
98      gtk_container_add(GTK_CONTAINER(a),b);
99      b = gtk_hbutton_box_new();
100      gtk_container_add(GTK_CONTAINER(a),b);
101      c = gtk_button_new_with_label(_("OK"));
102      gtk_signal_connect(GTK_OBJECT(c),"clicked",GTK_SIGNAL_FUNC(rawdialog_ok),
103           NULL);
104      gtk_signal_connect_object(GTK_OBJECT(c),"clicked",
105           GTK_SIGNAL_FUNC(gtk_widget_destroy),GTK_OBJECT(w));
106      gtk_container_add(GTK_CONTAINER(b),c);
107      c = gtk_button_new_with_label(_("Cancel"));
108      gtk_signal_connect_object(GTK_OBJECT(c),"clicked",
109           GTK_SIGNAL_FUNC(gtk_widget_destroy),GTK_OBJECT(w));
110      gtk_container_add(GTK_CONTAINER(b),c);
111 
112      ok_flag = destroy_flag = FALSE;
113 
114      gtk_widget_show_all(GTK_WIDGET(w));
115      while (!destroy_flag) mainloop();
116 
117      if (!ok_flag) return NULL;
118 
119      *offset = (guint) offset_box->val;
120      dataformat_save_to_inifile("rawDialog",&fmt,TRUE);
121      inifile_set_guint32("rawDialog_offset",*offset);
122      return &fmt;
123 }
124