File: sample-selector.c

package info (click to toggle)
specimen 0.5.2rc3-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,656 kB
  • sloc: ansic: 10,874; sh: 3,482; makefile: 60
file content (151 lines) | stat: -rw-r--r-- 3,629 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <gtk/gtk.h>
#include "gui.h"
#include "specimen.h"
#include "patch.h"
#include "mixer.h"

static int patch;
static char *old_file = NULL;
static GtkWidget *filesel = NULL;

static void cb_load (GtkWidget * filesel)
{
     GtkWidget *msg;
     char *name =
	  (char *)
	  gtk_file_selection_get_filename (GTK_FILE_SELECTION (filesel));
     char *curname = patch_get_sample_name (patch);
     int val;

     /* don't load the sample if it's already loaded */
     if (strcmp (name, curname) == 0)
     {
	  if (curname != NULL)
	       free (curname);
	  return;
     }

     if ((val = patch_sample_load (patch, name)) < 0)
     {
	  errmsg ("Failed to load sample %s for patch %d (%s)\n", name,
		  patch, patch_strerror (val));
	  msg = gtk_message_dialog_new (GTK_WINDOW (filesel),
					GTK_DIALOG_DESTROY_WITH_PARENT,
					GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
					"Failed to load sample %s", name);
	  gtk_dialog_run (GTK_DIALOG (msg));
	  gtk_widget_destroy (msg);
     }
     else
     {
	  debug ("Successfully loaded sample %s\n", name);
     }

     if (curname != NULL)
	  free (curname);
}

static void cb_preview (GtkWidget * filesel)
{
     char *name;

     name =
	  (char *)
	  gtk_file_selection_get_filename (GTK_FILE_SELECTION (filesel));
     mixer_preview (name);

     return;
}

static void cb_cancel ( )
{
     char *cur_file;

     if (old_file == NULL)
     {
	  return;
     }
     else if (strcmp (old_file, "\0") == 0)
     {
	  patch_sample_unload (patch);
     }
     else
     {				/* set patch to old sample if another one is currently loaded */
	  cur_file = patch_get_sample_name (patch);
	  if (strcmp (old_file, cur_file) != 0)
	       patch_sample_load (patch, old_file);
	  if (cur_file != NULL)
	       free (cur_file);
     }

     return;
}

static void filesel_init ( )
{
     GtkWidget *load, *preview;
     char *filename;

     filesel = gtk_file_selection_new ("Load Sample");
     filename = patch_get_sample_name (patch);
     gtk_file_selection_set_filename (GTK_FILE_SELECTION (filesel),
				      filename);

     gtk_window_set_modal (GTK_WINDOW (filesel), TRUE);

     if (filename != NULL)
	  free (filename);

     g_signal_connect_swapped (G_OBJECT
			       (GTK_FILE_SELECTION (filesel)->ok_button),
			       "clicked", G_CALLBACK (cb_load),
			       G_OBJECT (filesel));
     g_signal_connect (G_OBJECT
		       (GTK_FILE_SELECTION (filesel)->cancel_button),
		       "clicked", G_CALLBACK (cb_cancel), NULL);

     load = gtk_dialog_add_button (GTK_DIALOG (filesel), "Load", 0);
     g_signal_connect_swapped (G_OBJECT (load), "clicked",
			       G_CALLBACK (cb_load), G_OBJECT (filesel));

     preview = gtk_dialog_add_button (GTK_DIALOG (filesel), "Preview", 0);
     g_signal_connect_swapped (G_OBJECT (preview), "clicked",
			       G_CALLBACK (cb_preview), G_OBJECT (filesel));
}

int sample_selector_show (int id)
{
     int response;

     patch = id;
     
     /* create filselector if this is our first time */
     if (filesel == NULL)
	  filesel_init ( );

     old_file = patch_get_sample_name (patch);

     /* "I've been singing to myself, 'There's got to be a better way.'" */
     while (1)
     {
	  response = gtk_dialog_run (GTK_DIALOG (filesel));
	  if (response != 0)
	  {
	       gtk_widget_hide (filesel);
	       break;
	  }
     }				/* maybe I'm just a retard, but I think the dialog/filesel
				 * scheme in GTK sucks and I hope it gets fixed */

     if (old_file != NULL)
     {
	  free (old_file);
	  old_file = NULL;
     }

     return 0;
}