/[svn]/gigedit/trunk/src/gigedit/regionchooser.cpp
ViewVC logotype

Contents of /gigedit/trunk/src/gigedit/regionchooser.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1225 - (show annotations) (download)
Sun Jun 10 10:56:11 2007 UTC (16 years, 9 months ago) by schoenebeck
File size: 16770 byte(s)
moved gigedit sources from src/ -> src/gigedit/

1 /*
2 * Copyright (C) 2006, 2007 Andreas Persson
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2, or (at
7 * your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with program; see the file COPYING. If not, write to the Free
16 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17 * 02110-1301 USA.
18 */
19
20 #include "regionchooser.h"
21 #include <gdkmm/cursor.h>
22 #include <gtkmm/stock.h>
23 #include <gtkmm/spinbutton.h>
24 #include <gtkmm/dialog.h>
25 #include <libintl.h>
26 #include <math.h>
27
28 #define _(String) gettext(String)
29
30 RegionChooser::RegionChooser()
31 {
32 Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap();
33
34 black = Gdk::Color("black");
35 white = Gdk::Color("white");
36 red = Gdk::Color("#8070ff");
37 blue = Gdk::Color("#c098ff");
38 green = Gdk::Color("#a088ff");
39 grey1 = Gdk::Color("red");
40
41 colormap->alloc_color(black);
42 colormap->alloc_color(white);
43 colormap->alloc_color(red);
44 colormap->alloc_color(blue);
45 colormap->alloc_color(green);
46 colormap->alloc_color(grey1);
47 instrument = 0;
48 region = 0;
49 resize.active = false;
50 cursor_is_resize = false;
51 h1 = 20;
52 width = 800;
53
54 actionGroup = Gtk::ActionGroup::create();
55 actionGroup->add(Gtk::Action::create("Properties",
56 Gtk::Stock::PROPERTIES),
57 sigc::mem_fun(*this,
58 &RegionChooser::show_region_properties));
59 actionGroup->add(Gtk::Action::create("Remove", Gtk::Stock::REMOVE),
60 sigc::mem_fun(*this, &RegionChooser::delete_region));
61 actionGroup->add(Gtk::Action::create("Add", Gtk::Stock::ADD),
62 sigc::mem_fun(*this, &RegionChooser::add_region));
63 actionGroup->add(Gtk::Action::create("Dimensions", _("Dimensions...")),
64 sigc::mem_fun(*this, &RegionChooser::manage_dimensions));
65
66 uiManager = Gtk::UIManager::create();
67 uiManager->insert_action_group(actionGroup);
68 Glib::ustring ui_info =
69 "<ui>"
70 " <popup name='PopupMenuInsideRegion'>"
71 " <menuitem action='Properties'/>"
72 " <menuitem action='Dimensions'/>"
73 " <menuitem action='Remove'/>"
74 " </popup>"
75 " <popup name='PopupMenuOutsideRegion'>"
76 " <menuitem action='Add'/>"
77 " </popup>"
78 "</ui>";
79 uiManager->add_ui_from_string(ui_info);
80
81 popup_menu_inside_region = dynamic_cast<Gtk::Menu*>(
82 uiManager->get_widget("/PopupMenuInsideRegion"));
83 popup_menu_outside_region = dynamic_cast<Gtk::Menu*>(
84 uiManager->get_widget("/PopupMenuOutsideRegion"));
85
86 add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK |
87 Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK);
88
89 dimensionManager.articulation_changed_signal.connect(
90 sigc::mem_fun(*this, &RegionChooser::on_dimension_manager_changed)
91 );
92 }
93
94 RegionChooser::~RegionChooser()
95 {
96 }
97
98 void RegionChooser::on_realize()
99 {
100 // We need to call the base on_realize()
101 Gtk::DrawingArea::on_realize();
102
103 // Now we can allocate any additional resources we need
104 Glib::RefPtr<Gdk::Window> window = get_window();
105 gc = Gdk::GC::create(window);
106 window->clear();
107 }
108
109 bool RegionChooser::on_expose_event(GdkEventExpose* event)
110 {
111 Glib::RefPtr<Gdk::Window> window = get_window();
112 window->clear();
113 const int h = 40;
114 const int w = width - 1;
115 const int bh = int(h * 0.55);
116
117 Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();
118 Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc();
119
120 window->draw_rectangle(black, false, 0, h1, w, h - 1);
121 window->draw_rectangle(white, true, 1, h1 + 1, w - 1, h - 2);
122 for (int i = 0 ; i < 128 ; i++) {
123 int note = (i + 3) % 12;
124 int x = int(w * i / 128.0 + 0.5);
125
126 if (note == 1 || note == 4 || note == 6 || note == 9 || note == 11) {
127 int x2 = int(w * (i + 0.5) / 128.0 + 0.5);
128 window->draw_line(black, x2, h1 + bh, x2, h1 + h);
129
130 int x3 = int(w * (i + 1) / 128.0 + 0.5);
131 window->draw_rectangle(black, true, x, h1 + 1, x3 - x + 1, bh);
132 } else if (note == 3 || note == 8) {
133 window->draw_line(black, x, h1 + 1, x, h1 + h);
134 }
135 }
136
137 if (instrument) {
138 int i = 0;
139 gig::Region *nextRegion;
140 int x3 = -1;
141 for (gig::Region *r = instrument->GetFirstRegion() ;
142 r ;
143 r = nextRegion) {
144
145 if (x3 < 0) x3 = int(w * (r->KeyRange.low) / 128.0 + 0.5);
146 nextRegion = instrument->GetNextRegion();
147 if (!nextRegion || r->KeyRange.high + 1 != nextRegion->KeyRange.low) {
148 int x2 = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5);
149 window->draw_line(black, x3, 0, x2, 0);
150 window->draw_line(black, x3, h1 - 1, x2, h1 - 1);
151 window->draw_line(black, x2, 1, x2, h1 - 2);
152 window->draw_rectangle(white, true, x3 + 1, 1, x2 - x3 - 1, h1 - 2);
153 x3 = -1;
154 }
155 i++;
156 }
157
158 for (gig::Region *r = instrument->GetFirstRegion() ;
159 r ;
160 r = instrument->GetNextRegion()) {
161 int x = int(w * (r->KeyRange.low) / 128.0 + 0.5);
162 window->draw_line(black, x, 1, x, h1 - 2);
163 }
164
165 if (region) {
166 int x1 = int(w * (region->KeyRange.low) / 128.0 + 0.5);
167 int x2 = int(w * (region->KeyRange.high + 1) / 128.0 + 0.5);
168 gc->set_foreground(red);
169 window->draw_rectangle(gc, true, x1 + 1, 1, x2 - x1 - 1, h1 - 2);
170 }
171 }
172 return true;
173 }
174
175
176 void RegionChooser::on_size_request(GtkRequisition* requisition)
177 {
178 *requisition = GtkRequisition();
179 requisition->height = 40 + 20;
180 requisition->width = 500;
181 }
182
183
184 // not used
185 void RegionChooser::draw_region(int from, int to, const Gdk::Color& color)
186 {
187 const int h = 40;
188 const int w = width;
189 const int bh = int(h * 0.55);
190
191 Glib::RefPtr<Gdk::Window> window = get_window();
192 gc->set_foreground(color);
193
194 for (int i = from ; i < to ; i++) {
195 int note = (i + 3) % 12;
196 int x = int(w * i / 128.0 + 0.5) + 1;
197 int x2 = int(w * (i + 1.5) / 128.0 + 0.5);
198 int x3 = int(w * (i + 1) / 128.0 + 0.5);
199 int x4 = int(w * (i - 0.5) / 128 + 0.5) + 1;
200 int w1 = x3 - x;
201 switch (note) {
202 case 0: case 5: case 10:
203 window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);
204 window->draw_rectangle(gc, true, x4, h1 + bh + 1, x2 - x4, h - bh - 2);
205 break;
206 case 2: case 7:
207 window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);
208 window->draw_rectangle(gc, true, x4, h1 + bh + 1, x3 - x4, h - bh - 2);
209 break;
210 case 3: case 8:
211 window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);
212 window->draw_rectangle(gc, true, x, h1 + bh + 1, x2 - x, h - bh - 2);
213 break;
214 default:
215 window->draw_rectangle(gc, true, x, h1 + 1, w1, bh - 1);
216 break;
217 }
218 }
219 }
220
221 void RegionChooser::set_instrument(gig::Instrument* instrument)
222 {
223 this->instrument = instrument;
224 region = instrument ? instrument->GetFirstRegion() : 0;
225 queue_draw();
226 sel_changed_signal.emit();
227 }
228
229 bool RegionChooser::on_button_release_event(GdkEventButton* event)
230 {
231 if (resize.active) {
232 get_window()->pointer_ungrab(event->time);
233 resize.active = false;
234
235 if (resize.mode == resize.moving_high_limit) {
236 resize.region->KeyRange.high = resize.pos - 1;
237 } else if (resize.mode == resize.moving_low_limit) {
238 resize.region->KeyRange.low = resize.pos;
239 }
240
241 if (!is_in_resize_zone(event->x, event->y) && cursor_is_resize) {
242 get_window()->set_cursor();
243 cursor_is_resize = false;
244 }
245 }
246 return true;
247 }
248
249 bool RegionChooser::on_button_press_event(GdkEventButton* event)
250 {
251 if (!instrument) return true;
252
253 int k = int(event->x / (width - 1) * 128.0);
254
255 if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
256 gig::Region* r = get_region(k);
257 if (r) {
258 region = r;
259 queue_draw();
260 sel_changed_signal.emit();
261 popup_menu_inside_region->popup(event->button, event->time);
262 } else {
263 new_region_pos = k;
264 popup_menu_outside_region->popup(event->button, event->time);
265 }
266 } else {
267 if (is_in_resize_zone(event->x, event->y)) {
268 Gdk::Cursor double_arrow(Gdk::SB_H_DOUBLE_ARROW);
269 get_window()->pointer_grab(false,
270 Gdk::BUTTON_RELEASE_MASK |
271 Gdk::POINTER_MOTION_MASK |
272 Gdk::POINTER_MOTION_HINT_MASK,
273 double_arrow, event->time);
274 resize.active = true;
275 } else {
276 gig::Region* r = get_region(k);
277 if (r) {
278 region = r;
279 queue_draw();
280 sel_changed_signal.emit();
281 }
282 }
283 }
284 return true;
285 }
286
287 gig::Region* RegionChooser::get_region(int key)
288 {
289 for (gig::Region *r = instrument->GetFirstRegion() ; r ;
290 r = instrument->GetNextRegion()) {
291 if (key < r->KeyRange.low) return 0;
292 if (key <= r->KeyRange.high) return r;
293 }
294 return 0;
295 }
296
297 bool RegionChooser::on_motion_notify_event(GdkEventMotion* event)
298 {
299 const int w = width - 1;
300 Glib::RefPtr<Gdk::Window> window = get_window();
301 int x, y;
302 Gdk::ModifierType state = Gdk::ModifierType(0);
303 window->get_pointer(x, y, state);
304 if (resize.active) {
305 int k = int(double(x) / w * 128.0 + 0.5);
306
307 if (k < resize.min) k = resize.min;
308 else if (k > resize.max) k = resize.max;
309
310 if (k != resize.pos) {
311 if (resize.mode == resize.undecided) {
312 if (k < resize.pos) {
313 // edit high limit of prev_region
314 resize.max = resize.region->KeyRange.low;
315 resize.region = resize.prev_region;
316 resize.mode = resize.moving_high_limit;
317 } else {
318 // edit low limit of region
319 resize.min = resize.prev_region->KeyRange.high + 1;
320 resize.mode = resize.moving_low_limit;
321 }
322 }
323 Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();
324 Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc();
325 if (region == resize.region) {
326 gc->set_foreground(red);
327 white = gc;
328 }
329 Glib::RefPtr<const Gdk::GC> bg = get_style()->get_bg_gc(Gtk::STATE_NORMAL);
330 int prevx = int(w * resize.pos / 128.0 + 0.5);
331 x = int(w * k / 128.0 + 0.5);
332
333 if (resize.mode == resize.moving_high_limit) {
334 if (k > resize.pos) {
335 window->draw_rectangle(white, true, prevx, 1, x - prevx, h1 - 2);
336 window->draw_line(black, prevx, 0, x, 0);
337 window->draw_line(black, prevx, h1 - 1, x, h1 - 1);
338 } else {
339 int xx = ((resize.pos == resize.max && resize.max != 128) ? 1 : 0);
340 window->draw_rectangle(bg, true, x + 1, 0, prevx - x - xx, h1);
341 }
342 } else {
343 if (k < resize.pos) {
344 window->draw_rectangle(white, true, x + 1, 1, prevx - x, h1 - 2);
345 window->draw_line(black, x, 0, prevx, 0);
346 window->draw_line(black, x, h1 - 1, prevx, h1 - 1);
347 } else {
348 int xx = ((resize.pos == resize.min && resize.min != 0) ? 1 : 0);
349 window->draw_rectangle(bg, true, prevx + xx, 0, x - prevx - xx, h1);
350 }
351 }
352 window->draw_line(black, x, 1, x, h1 - 2);
353 resize.pos = k;
354 }
355 } else {
356 if (is_in_resize_zone(x, y)) {
357 if (!cursor_is_resize) {
358 Gdk::Cursor double_arrow(Gdk::SB_H_DOUBLE_ARROW);
359 window->set_cursor(double_arrow);
360 cursor_is_resize = true;
361 }
362 } else if (cursor_is_resize) {
363 window->set_cursor();
364 cursor_is_resize = false;
365 }
366 }
367
368 return true;
369 }
370
371 bool RegionChooser::is_in_resize_zone(double x, double y) {
372 const int w = width - 1;
373
374 if (instrument && y >= 0 && y <= h1) {
375 gig::Region* prev_region = 0;
376 gig::Region* next_region;
377 for (gig::Region* r = instrument->GetFirstRegion() ; r ; r = next_region) {
378 next_region = instrument->GetNextRegion();
379
380 int lo = int(w * (r->KeyRange.low) / 128.0 + 0.5);
381 if (x <= lo - 2) break;
382 if (x < lo + 2) {
383 resize.region = r;
384 resize.pos = r->KeyRange.low;
385 resize.max = r->KeyRange.high;
386
387 if (prev_region && prev_region->KeyRange.high + 1 == r->KeyRange.low) {
388 // we don't know yet if it's the high limit of
389 // prev_region or the low limit of r that's going
390 // to be edited
391 resize.mode = resize.undecided;
392 resize.min = prev_region->KeyRange.low + 1;
393 resize.prev_region = prev_region;
394 return true;
395 }
396
397 // edit low limit
398 resize.mode = resize.moving_low_limit;
399 resize.min = prev_region ? prev_region->KeyRange.high + 1 : 0;
400 return true;
401 }
402 if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) {
403 int hi = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5);
404 if (x <= hi - 2) break;
405 if (x < hi + 2) {
406 // edit high limit
407 resize.region = r;
408 resize.pos = r->KeyRange.high + 1;
409 resize.mode = resize.moving_high_limit;
410 resize.min = r->KeyRange.low + 1;
411 resize.max = next_region ? next_region->KeyRange.low : 128;
412 return true;
413 }
414 }
415 prev_region = r;
416 }
417 }
418 return false;
419 }
420
421 sigc::signal<void> RegionChooser::signal_sel_changed()
422 {
423 return sel_changed_signal;
424 }
425
426 void RegionChooser::show_region_properties()
427 {
428 if (!region) return;
429 Gtk::Dialog dialog("Region Properties", true /*modal*/);
430 // add "Keygroup" checkbox
431 Gtk::CheckButton checkBoxKeygroup("Member of a Keygroup (Exclusive Group)");
432 checkBoxKeygroup.set_active(region->KeyGroup);
433 dialog.get_vbox()->pack_start(checkBoxKeygroup);
434 checkBoxKeygroup.show();
435 // add "Keygroup" spinbox
436 Gtk::Adjustment adjustment(1, 1, pow(2,32));
437 Gtk::SpinButton spinBox(adjustment);
438 if (region->KeyGroup) spinBox.set_value(region->KeyGroup);
439 dialog.get_vbox()->pack_start(spinBox);
440 spinBox.show();
441 // add OK and CANCEL buttons to the dialog
442 dialog.add_button(Gtk::Stock::OK, 0);
443 dialog.add_button(Gtk::Stock::CANCEL, 1);
444 dialog.show_all_children();
445 if (!dialog.run()) { // OK selected ...
446 region->KeyGroup =
447 (checkBoxKeygroup.get_active()) ? spinBox.get_value_as_int() : 0;
448 }
449 }
450
451 void RegionChooser::add_region()
452 {
453 gig::Region* r;
454 for (r = instrument->GetFirstRegion() ; r ; r = instrument->GetNextRegion()) {
455 if (r->KeyRange.low > new_region_pos) break;
456 }
457
458 region = instrument->AddRegion();
459 region->KeyRange.low = region->KeyRange.high = new_region_pos;
460
461 instrument->MoveRegion(region, r);
462 queue_draw();
463 sel_changed_signal.emit();
464 }
465
466 void RegionChooser::delete_region()
467 {
468 instrument->DeleteRegion(region);
469 region = 0;
470 queue_draw();
471 sel_changed_signal.emit();
472 }
473
474 void RegionChooser::manage_dimensions()
475 {
476 gig::Region* region = get_region();
477 if (!region) return;
478 dimensionManager.show(region);
479 }
480
481 void RegionChooser::on_dimension_manager_changed() {
482 sel_changed_signal.emit();
483 }

  ViewVC Help
Powered by ViewVC