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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
/* Petri-Foo is a fork of the Specimen audio sampler.
Original Specimen author Pete Bessman
Copyright 2005 Pete Bessman
Copyright 2011 James W. Morris
This file is part of Petri-Foo.
Petri-Foo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
Petri-Foo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Petri-Foo. If not, see <http://www.gnu.org/licenses/>.
This file is a derivative of a Specimen original, modified 2011
*/
#include <gtk/gtk.h>
#include "patchlist.h"
#include "gui.h"
#include "petri-foo.h"
#include "patch_set_and_get.h"
#include "patch_util.h"
#include <string.h>
/* magic numbers */
enum
{
LIST_WIDTH = 128,
SPACING = GUI_SCROLLSPACE,
};
/* signals */
enum
{
CHANGED,
LAST_SIGNAL,
};
/* model columns */
enum
{
PATCH_NAME,
PATCH_ID,
LAST_COLUMN,
};
typedef struct _PatchListPrivate PatchListPrivate;
#define PATCH_LIST_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE((obj), \
PATCH_LIST_TYPE, PatchListPrivate))
struct _PatchListPrivate
{
int patch;
GtkWidget* patch_tree;
GtkListStore* patch_store;
GtkWidget* vscroll;
GtkTreeSelection* select;
GtkWidget* self;
};
static int signals[LAST_SIGNAL];
G_DEFINE_TYPE(PatchList, patch_list, GTK_TYPE_HBOX);
static void patch_list_class_init(PatchListClass* klass)
{
GtkObjectClass *object_class = GTK_OBJECT_CLASS(klass);
patch_list_parent_class = g_type_class_peek_parent(klass);
signals[CHANGED] =
g_signal_new("changed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (PatchListClass, changed),
NULL, NULL,
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
klass->changed = NULL;
g_type_class_add_private(object_class, sizeof(PatchListPrivate));
}
static void select_cb(GtkTreeSelection* selection, PatchListPrivate* p)
{
GtkTreeIter iter;
GtkTreeModel *model;
if (gtk_tree_selection_get_selected(selection, &model, &iter))
gtk_tree_model_get(model, &iter, PATCH_ID, &p->patch, -1);
else
p->patch = -1;
g_signal_emit_by_name(G_OBJECT(p->self), "changed");
}
void popup_menu(GdkEventButton *event, gboolean full_menu, gpointer data)
{
(void)data;
GtkWidget *menu = gtk_menu_new();
gui_menu_add(menu, "Add Patch...",
G_CALLBACK(cb_menu_patch_add), NULL);
gui_menu_add(menu, "Add Default Patch",
G_CALLBACK(cb_menu_patch_add_default), NULL);
if (full_menu)
{
gui_menu_add(menu, "Duplicate Patch",
G_CALLBACK(cb_menu_patch_duplicate), NULL);
gui_menu_add(menu, "Rename Patch...",
G_CALLBACK(cb_menu_patch_rename), NULL);
gui_menu_add(menu, "Remove Patch",
G_CALLBACK(cb_menu_patch_remove), NULL);
}
gtk_widget_show_all(menu);
gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
(event != NULL) ? event->button : 0,
gdk_event_get_time((GdkEvent*)event));
}
static gboolean
pressed_cb(GtkWidget *treeview, GdkEventButton *event, gpointer data)
{
PatchListPrivate* p = (PatchListPrivate*)data;
if (event->type == GDK_BUTTON_PRESS && event->button == 3)
{
GtkTreePath* path;
GtkTreeSelection* selection;
gboolean full_menu = FALSE;
/* select row the click happened in */
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
if (gtk_tree_selection_count_selected_rows(selection) <= 1)
{
if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(treeview),
(gint)event->x,
(gint)event->y,
&path,
NULL, NULL, NULL))
{
g_signal_handlers_block_by_func(p->select, select_cb, p);
gtk_tree_selection_unselect_all(selection);
g_signal_handlers_unblock_by_func(p->select, select_cb, p);
gtk_tree_selection_select_path(selection, path);
full_menu = TRUE;
}
}
popup_menu(event, full_menu, data);
gtk_tree_path_free(path);
return TRUE;
}
return FALSE;
}
static gboolean popup_cb(GtkWidget *treeview, gpointer data)
{
(void)treeview;(void)data;
popup_menu(NULL, TRUE, data);
return TRUE;
}
static void patch_list_init(PatchList* self)
{
PatchListPrivate* p = PATCH_LIST_GET_PRIVATE(self);
GtkBox* box = GTK_BOX(self);
GtkTreeViewColumn* column;
GtkCellRenderer* renderer;
p->self = GTK_WIDGET(self);
gtk_box_set_spacing(box, SPACING);
/* patch list */
p->patch_store =
gtk_list_store_new(LAST_COLUMN, G_TYPE_STRING, G_TYPE_INT);
p->patch_tree =
gtk_tree_view_new_with_model(GTK_TREE_MODEL(p->patch_store));
p->vscroll = gtk_vscrollbar_new(NULL);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes( "Patch",
renderer,
"text", PATCH_NAME,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(p->patch_tree), column);
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(p->patch_tree), FALSE);
gtk_tree_view_set_vadjustment(GTK_TREE_VIEW(p->patch_tree),
gtk_range_get_adjustment(GTK_RANGE(p->vscroll)));
gtk_widget_set_size_request(p->patch_tree, LIST_WIDTH, 1);
gtk_box_pack_start(box, p->patch_tree, TRUE, TRUE, 0);
gtk_box_pack_start(box, p->vscroll, FALSE, FALSE, 0);
gtk_widget_show(p->patch_tree);
gtk_widget_show(p->vscroll);
/* connect up */
p->select = gtk_tree_view_get_selection(GTK_TREE_VIEW(p->patch_tree));
gtk_tree_selection_set_mode(p->select, GTK_SELECTION_SINGLE);
g_signal_connect(p->select, "changed",
G_CALLBACK(select_cb), (gpointer)p);
g_signal_connect(p->patch_tree, "button-press-event",
G_CALLBACK(pressed_cb), (gpointer)p);
g_signal_connect(p->patch_tree, "popup-menu",
G_CALLBACK(popup_cb), (gpointer)p);
}
GtkWidget* patch_list_new(void)
{
return (GtkWidget*) g_object_new(PATCH_LIST_TYPE, NULL);
}
void patch_list_update(PatchList* self, int target, int type)
{
PatchListPrivate* p = PATCH_LIST_GET_PRIVATE(self);
int npatches;
int* patches;
GtkTreeIter iter;
GtkTreePath* path;
const char* str;
int i;
int index = 0;
g_signal_handlers_block_by_func(p->select, select_cb, p);
npatches = patch_dump(&patches);
gtk_list_store_clear(p->patch_store);
for (i = 0; i < npatches; ++i)
{
str = patch_get_name(patches[i]);
gtk_list_store_append(p->patch_store, &iter);
gtk_list_store_set(p->patch_store, &iter, PATCH_NAME, str,
PATCH_ID, patches[i], -1);
if (type == PATCH_LIST_PATCH && patches[i] == target)
index = i;
}
g_free(patches);
if (type == PATCH_LIST_INDEX)
index = target;
path = gtk_tree_path_new_from_indices(index, -1);
gtk_tree_view_set_cursor(GTK_TREE_VIEW(p->patch_tree), path, NULL,
FALSE);
gtk_tree_path_free(path);
g_signal_handlers_unblock_by_func(p->select, select_cb, p);
g_signal_emit_by_name(G_OBJECT(p->select), "changed");
}
int patch_list_get_current_patch(PatchList* self)
{
return PATCH_LIST_GET_PRIVATE(self)->patch;
}
int patch_list_get_current_index(PatchList* self)
{
PatchListPrivate* p = PATCH_LIST_GET_PRIVATE(self);
int* i;
GtkTreePath* t;
gtk_tree_view_get_cursor(GTK_TREE_VIEW(p->patch_tree), &t, NULL);
i = gtk_tree_path_get_indices(t);
return *i;
}
|