/[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 1654 - (show annotations) (download)
Wed Jan 30 02:20:48 2008 UTC (16 years, 2 months ago) by schoenebeck
File size: 26698 byte(s)
* first step to make the virtual keyboard interactive: active keys of the
  sampler (in live-mode only of course) are highlighted on the virtual
  keyboard - NOTE: yet inaccurate draw of the keys and this mechanism
  yet only works on the first gigedit invocation by the sampler process,
  so this still has to be fixed

1 /*
2 * Copyright (C) 2006-2008 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 <algorithm>
22 #include <gdkmm/cursor.h>
23 #include <gtkmm/stock.h>
24 #include <gtkmm/spinbutton.h>
25 #include <gtkmm/dialog.h>
26 #include <math.h>
27
28 #include "global.h"
29
30 void SortedRegions::update(gig::Instrument* instrument) {
31 // Usually, the regions in a gig file are ordered after their key
32 // range, but there are files where they are not. The
33 // RegionChooser code needs a sorted list of regions.
34 regions.clear();
35 if (instrument) {
36 for (gig::Region *r = instrument->GetFirstRegion() ;
37 r ;
38 r = instrument->GetNextRegion()) {
39 regions.push_back(r);
40 }
41 sort(regions.begin(), regions.end(), *this);
42 }
43 }
44
45 gig::Region* SortedRegions::first() {
46 region_iterator = regions.begin();
47 return region_iterator == regions.end() ? 0 : *region_iterator;
48 }
49
50 gig::Region* SortedRegions::next() {
51 region_iterator++;
52 return region_iterator == regions.end() ? 0 : *region_iterator;
53 }
54
55
56
57 RegionChooser::RegionChooser()
58 {
59 Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap();
60
61 red = Gdk::Color("#8070ff");
62 grey1 = Gdk::Color("#b0b0b0");
63 activeKeyColor = Gdk::Color("#ff0000");
64 white = Gdk::Color("#ffffff");
65 black = Gdk::Color("#000000");
66
67 colormap->alloc_color(red);
68 colormap->alloc_color(grey1);
69 colormap->alloc_color(activeKeyColor);
70 colormap->alloc_color(white);
71 colormap->alloc_color(black);
72 instrument = 0;
73 region = 0;
74 resize.active = false;
75 move.active = false;
76 cursor_is_resize = false;
77 h1 = 20;
78
79 actionGroup = Gtk::ActionGroup::create();
80 actionGroup->add(Gtk::Action::create("Properties",
81 Gtk::Stock::PROPERTIES),
82 sigc::mem_fun(*this,
83 &RegionChooser::show_region_properties));
84 actionGroup->add(Gtk::Action::create("Remove", Gtk::Stock::REMOVE),
85 sigc::mem_fun(*this, &RegionChooser::delete_region));
86 actionGroup->add(Gtk::Action::create("Add", Gtk::Stock::ADD),
87 sigc::mem_fun(*this, &RegionChooser::add_region));
88 actionGroup->add(Gtk::Action::create("Dimensions", _("Dimensions...")),
89 sigc::mem_fun(*this, &RegionChooser::manage_dimensions));
90
91 uiManager = Gtk::UIManager::create();
92 uiManager->insert_action_group(actionGroup);
93 Glib::ustring ui_info =
94 "<ui>"
95 " <popup name='PopupMenuInsideRegion'>"
96 " <menuitem action='Properties'/>"
97 " <menuitem action='Dimensions'/>"
98 " <menuitem action='Remove'/>"
99 " </popup>"
100 " <popup name='PopupMenuOutsideRegion'>"
101 " <menuitem action='Add'/>"
102 " </popup>"
103 "</ui>";
104 uiManager->add_ui_from_string(ui_info);
105
106 popup_menu_inside_region = dynamic_cast<Gtk::Menu*>(
107 uiManager->get_widget("/PopupMenuInsideRegion"));
108 popup_menu_outside_region = dynamic_cast<Gtk::Menu*>(
109 uiManager->get_widget("/PopupMenuOutsideRegion"));
110
111 add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK |
112 Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK);
113
114 dimensionManager.region_to_be_changed_signal.connect(
115 region_to_be_changed_signal.make_slot()
116 );
117 dimensionManager.region_changed_signal.connect(
118 region_changed_signal.make_slot()
119 );
120 dimensionManager.region_changed_signal.connect(
121 sigc::hide(
122 sigc::mem_fun(*this, &RegionChooser::on_dimension_manager_changed)
123 )
124 );
125 }
126
127 RegionChooser::~RegionChooser()
128 {
129 }
130
131 void RegionChooser::on_note_on_event(int key, int velocity) {
132 draw_region(key, key+1, activeKeyColor);
133 }
134
135 void RegionChooser::on_note_off_event(int key, int velocity) {
136 if (is_black_key(key))
137 draw_region(key, key+1, black);
138 else
139 draw_region(key, key+1, white);
140 }
141
142 void RegionChooser::on_realize()
143 {
144 // We need to call the base on_realize()
145 Gtk::DrawingArea::on_realize();
146
147 // Now we can allocate any additional resources we need
148 Glib::RefPtr<Gdk::Window> window = get_window();
149 gc = Gdk::GC::create(window);
150 window->clear();
151 }
152
153 bool RegionChooser::on_expose_event(GdkEventExpose* event)
154 {
155 Glib::RefPtr<Gdk::Window> window = get_window();
156 window->clear();
157 const int h = 40;
158 const int w = get_width() - 1;
159 const int bh = int(h * 0.55);
160
161 Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();
162 Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc();
163
164 Glib::RefPtr<Pango::Context> context = get_pango_context();
165 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create(context);
166
167 window->draw_rectangle(black, false, 0, h1, w, h - 1);
168 gc->set_foreground(grey1);
169 int x1 = int(w * 20.5 / 128.0 + 0.5);
170 int x2 = int(w * 109.5 / 128.0 + 0.5);
171 window->draw_rectangle(gc, true, 1, h1 + 1,
172 x1 - 1, h - 2);
173 window->draw_rectangle(white, true, x1 + 1, h1 + 1, x2 - x1 - 1, h - 2);
174 window->draw_rectangle(gc, true, x2 + 1, h1 + 1,
175 w - x2 - 1, h - 2);
176 int octave = -1;
177 for (int i = 0 ; i < 128 ; i++) {
178 int note = (i + 3) % 12;
179 int x = int(w * i / 128.0 + 0.5);
180
181 if (note == 1 || note == 4 || note == 6 || note == 9 || note == 11) {
182 int x2 = int(w * (i + 0.5) / 128.0 + 0.5);
183 window->draw_line(black, x2, h1 + bh, x2, h1 + h);
184
185 int x3 = int(w * (i + 1) / 128.0 + 0.5);
186 window->draw_rectangle(black, true, x, h1 + 1, x3 - x + 1, bh);
187 } else if (note == 3 || note == 8) {
188 window->draw_line(black, x, h1 + 1, x, h1 + h);
189 }
190 if (note == 3) {
191 char buf[30];
192 sprintf(buf, "<span size=\"8000\">%d</span>", octave);
193 layout->set_markup(buf);
194 Pango::Rectangle rectangle = layout->get_logical_extents();
195 double text_w = double(rectangle.get_width()) / Pango::SCALE;
196 double text_h = double(rectangle.get_height()) / Pango::SCALE;
197 double x2 = w * (i + 0.75) / 128.0;
198 window->draw_layout(black, int(x2 - text_w / 2 + 1),
199 int(h1 + h - text_h + 0.5), layout);
200 octave++;
201 }
202 }
203
204 if (instrument) {
205 int i = 0;
206 gig::Region *next_region;
207 int x3 = -1;
208 for (gig::Region *r = regions.first() ; r ; r = next_region) {
209
210 if (x3 < 0) x3 = int(w * (r->KeyRange.low) / 128.0 + 0.5);
211 next_region = regions.next();
212 if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) {
213 int x2 = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5);
214 window->draw_line(black, x3, 0, x2, 0);
215 window->draw_line(black, x3, h1 - 1, x2, h1 - 1);
216 window->draw_line(black, x2, 1, x2, h1 - 2);
217 window->draw_rectangle(white, true, x3 + 1, 1, x2 - x3 - 1, h1 - 2);
218 x3 = -1;
219 }
220 i++;
221 }
222
223 for (gig::Region *r = regions.first() ; r ; r = regions.next()) {
224 int x = int(w * (r->KeyRange.low) / 128.0 + 0.5);
225 window->draw_line(black, x, 1, x, h1 - 2);
226 }
227
228 if (region) {
229 int x1 = int(w * (region->KeyRange.low) / 128.0 + 0.5);
230 int x2 = int(w * (region->KeyRange.high + 1) / 128.0 + 0.5);
231 gc->set_foreground(red);
232 window->draw_rectangle(gc, true, x1 + 1, 1, x2 - x1 - 1, h1 - 2);
233 }
234 }
235 return true;
236 }
237
238
239 void RegionChooser::on_size_request(GtkRequisition* requisition)
240 {
241 *requisition = GtkRequisition();
242 requisition->height = 40 + 20;
243 requisition->width = 500;
244 }
245
246 bool RegionChooser::is_black_key(int key) {
247 const int note = (key + 3) % 12;
248 return note == 1 || note == 4 || note == 6 || note == 9 || note == 11;
249 }
250
251 void RegionChooser::draw_region(int from, int to, const Gdk::Color& color)
252 {
253 const int h = 40;
254 const int w = get_width();
255 const int bh = int(h * 0.55);
256
257 Glib::RefPtr<Gdk::Window> window = get_window();
258 gc->set_foreground(color);
259
260 for (int i = from ; i < to ; i++) {
261 int note = (i + 3) % 12;
262 int x = int(w * i / 128.0 + 0.5) + 1;
263 int x2 = int(w * (i + 1.5) / 128.0 + 0.5);
264 int x3 = int(w * (i + 1) / 128.0 + 0.5);
265 int x4 = int(w * (i - 0.5) / 128 + 0.5) + 1;
266 int w1 = x3 - x;
267 switch (note) {
268 case 0: case 5: case 10:
269 window->draw_rectangle(gc, true, x - 1, h1 + 1, w1, bh);
270 window->draw_rectangle(gc, true, x4, h1 + bh + 1, x2 - x4 - 1, h - bh - 2);
271 break;
272 case 2: case 7:
273 window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);
274 window->draw_rectangle(gc, true, x4 - 1, h1 + bh + 1, x3 - x4 + 1, h - bh - 2);
275 break;
276 case 3: case 8:
277 window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);
278 window->draw_rectangle(gc, true, x, h1 + bh + 1, x2 - x, h - bh - 2);
279 break;
280 default:
281 window->draw_rectangle(gc, true, x, h1 + 1, w1 - 1, bh - 1);
282 break;
283 }
284 }
285 }
286
287 void RegionChooser::set_instrument(gig::Instrument* instrument)
288 {
289 this->instrument = instrument;
290 regions.update(instrument);
291 region = regions.first();
292 queue_draw();
293 region_selected();
294 }
295
296 bool RegionChooser::on_button_release_event(GdkEventButton* event)
297 {
298 if (resize.active) {
299 get_window()->pointer_ungrab(event->time);
300 resize.active = false;
301
302 if (resize.mode == resize.moving_high_limit) {
303 if (resize.region->KeyRange.high != resize.pos - 1) {
304 instrument_struct_to_be_changed_signal.emit(instrument);
305 resize.region->SetKeyRange(
306 resize.region->KeyRange.low, // low
307 resize.pos - 1 // high
308 );
309 regions.update(instrument);
310 instrument_changed.emit();
311 instrument_struct_changed_signal.emit(instrument);
312 }
313 } else if (resize.mode == resize.moving_low_limit) {
314 if (resize.region->KeyRange.low != resize.pos) {
315 instrument_struct_to_be_changed_signal.emit(instrument);
316 resize.region->SetKeyRange(
317 resize.pos, // low
318 resize.region->KeyRange.high // high
319 );
320 regions.update(instrument);
321 instrument_changed.emit();
322 instrument_struct_changed_signal.emit(instrument);
323 }
324 }
325
326 if (!is_in_resize_zone(event->x, event->y) && cursor_is_resize) {
327 get_window()->set_cursor();
328 cursor_is_resize = false;
329 }
330 } else if (move.active) {
331 get_window()->pointer_ungrab(event->time);
332 move.active = false;
333
334 if (move.pos) {
335 instrument_struct_to_be_changed_signal.emit(instrument);
336 region->SetKeyRange(
337 region->KeyRange.low + move.pos,
338 region->KeyRange.high + move.pos
339 );
340 regions.update(instrument);
341 instrument_changed.emit();
342 instrument_struct_changed_signal.emit(instrument);
343 }
344
345 if (is_in_resize_zone(event->x, event->y)) {
346 get_window()->set_cursor(Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW));
347 cursor_is_resize = true;
348 }
349 }
350 return true;
351 }
352
353 bool RegionChooser::on_button_press_event(GdkEventButton* event)
354 {
355 if (!instrument) return true;
356
357 int k = int(event->x / (get_width() - 1) * 128.0);
358
359 if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
360 gig::Region* r = get_region(k);
361 if (r) {
362 region = r;
363 queue_draw();
364 region_selected();
365 popup_menu_inside_region->popup(event->button, event->time);
366 } else {
367 new_region_pos = k;
368 popup_menu_outside_region->popup(event->button, event->time);
369 }
370 } else {
371 if (is_in_resize_zone(event->x, event->y)) {
372 get_window()->pointer_grab(false,
373 Gdk::BUTTON_RELEASE_MASK |
374 Gdk::POINTER_MOTION_MASK |
375 Gdk::POINTER_MOTION_HINT_MASK,
376 Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW), event->time);
377 resize.active = true;
378 } else {
379 gig::Region* r = get_region(k);
380 if (r) {
381 region = r;
382 queue_draw();
383 region_selected();
384
385 get_window()->pointer_grab(false,
386 Gdk::BUTTON_RELEASE_MASK |
387 Gdk::POINTER_MOTION_MASK |
388 Gdk::POINTER_MOTION_HINT_MASK,
389 Gdk::Cursor(Gdk::FLEUR), event->time);
390 move.active = true;
391 move.from_x = event->x;
392 move.pos = 0;
393 }
394 }
395 }
396 return true;
397 }
398
399 gig::Region* RegionChooser::get_region(int key)
400 {
401 gig::Region* prev_region = 0;
402 gig::Region* next_region;
403 for (gig::Region *r = regions.first() ; r ; r = next_region) {
404 next_region = regions.next();
405
406 if (key < r->KeyRange.low) return 0;
407 if (key <= r->KeyRange.high) {
408 move.touch_left = prev_region && prev_region->KeyRange.high + 1 == r->KeyRange.low;
409 move.touch_right = next_region && r->KeyRange.high + 1 == next_region->KeyRange.low;
410 return r;
411 }
412 prev_region = r;
413 }
414 return 0;
415 }
416
417 void RegionChooser::motion_resize_region(int x, int y)
418 {
419 const int w = get_width() - 1;
420 Glib::RefPtr<Gdk::Window> window = get_window();
421
422 int k = int(double(x) / w * 128.0 + 0.5);
423
424 if (k < resize.min) k = resize.min;
425 else if (k > resize.max) k = resize.max;
426
427 if (k != resize.pos) {
428 if (resize.mode == resize.undecided) {
429 if (k < resize.pos) {
430 // edit high limit of prev_region
431 resize.max = resize.region->KeyRange.low;
432 resize.region = resize.prev_region;
433 resize.mode = resize.moving_high_limit;
434 } else {
435 // edit low limit of region
436 resize.min = resize.prev_region->KeyRange.high + 1;
437 resize.mode = resize.moving_low_limit;
438 }
439 }
440 Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();
441 Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc();
442 if (region == resize.region) {
443 gc->set_foreground(red);
444 white = gc;
445 }
446 Glib::RefPtr<const Gdk::GC> bg = get_style()->get_bg_gc(Gtk::STATE_NORMAL);
447 int prevx = int(w * resize.pos / 128.0 + 0.5);
448 x = int(w * k / 128.0 + 0.5);
449
450 if (resize.mode == resize.moving_high_limit) {
451 if (k > resize.pos) {
452 window->draw_rectangle(white, true, prevx, 1, x - prevx, h1 - 2);
453 window->draw_line(black, prevx, 0, x, 0);
454 window->draw_line(black, prevx, h1 - 1, x, h1 - 1);
455 } else {
456 int xx = ((resize.pos == resize.max && resize.max != 128) ? 1 : 0);
457 window->draw_rectangle(bg, true, x + 1, 0, prevx - x - xx, h1);
458 }
459 } else {
460 if (k < resize.pos) {
461 window->draw_rectangle(white, true, x + 1, 1, prevx - x, h1 - 2);
462 window->draw_line(black, x, 0, prevx, 0);
463 window->draw_line(black, x, h1 - 1, prevx, h1 - 1);
464 } else {
465 int xx = ((resize.pos == resize.min && resize.min != 0) ? 1 : 0);
466 window->draw_rectangle(bg, true, prevx + xx, 0, x - prevx - xx, h1);
467 }
468 }
469 window->draw_line(black, x, 1, x, h1 - 2);
470 resize.pos = k;
471 }
472 }
473
474 void RegionChooser::motion_move_region(int x, int y)
475 {
476 const int w = get_width() - 1;
477 Glib::RefPtr<Gdk::Window> window = get_window();
478
479 int k = int(double(x - move.from_x) / w * 128.0 + 0.5);
480 if (k == move.pos) return;
481 int new_k;
482 bool new_touch_left;
483 bool new_touch_right;
484 int a = 0;
485 if (k > move.pos) {
486 for (gig::Region* r = regions.first() ; ; r = regions.next()) {
487 if (r != region) {
488 int b = r ? r->KeyRange.low : 128;
489
490 // gap: from a to b (not inclusive b)
491
492 if (region->KeyRange.high + move.pos >= b) {
493 // not found the current gap yet, just continue
494 } else {
495
496 if (a > region->KeyRange.low + k) {
497 // this gap is too far to the right, break
498 break;
499 }
500
501 int newhigh = std::min(region->KeyRange.high + k, b - 1);
502 int newlo = newhigh - (region->KeyRange.high - region->KeyRange.low);
503
504 if (newlo >= a) {
505 // yes it fits - it's a candidate
506 new_k = newlo - region->KeyRange.low;
507 new_touch_left = a > 0 && a == newlo;
508 new_touch_right = b < 128 && newhigh + 1 == b;
509 }
510 }
511 if (!r) break;
512 a = r->KeyRange.high + 1;
513 }
514 }
515 } else {
516 for (gig::Region* r = regions.first() ; ; r = regions.next()) {
517 if (r != region) {
518 int b = r ? r->KeyRange.low : 128;
519
520 // gap from a to b (not inclusive b)
521
522 if (region->KeyRange.high + k >= b) {
523 // not found the current gap yet, just continue
524 } else {
525
526 if (a > region->KeyRange.low + move.pos) {
527 // this gap is too far to the right, break
528 break;
529 }
530
531 int newlo = std::max(region->KeyRange.low + k, a);
532 int newhigh = newlo + (region->KeyRange.high - region->KeyRange.low);
533
534 if (newhigh < b) {
535 // yes it fits - break as the first one is the best
536 new_k = newlo - region->KeyRange.low;
537 new_touch_left = a > 0 && a == newlo;
538 new_touch_right = b < 128 && newhigh + 1 == b;
539 break;
540 }
541 }
542 if (!r) break;
543 a = r->KeyRange.high + 1;
544 }
545 }
546 }
547 k = new_k;
548 if (k == move.pos) return;
549
550 Glib::RefPtr<const Gdk::GC> bg = get_style()->get_bg_gc(Gtk::STATE_NORMAL);
551 int prevx = int(w * (move.pos + region->KeyRange.low) / 128.0 + 0.5);
552 x = int(w * (k + region->KeyRange.low) / 128.0 + 0.5);
553 int prevx2 = int(w * (move.pos + region->KeyRange.high + 1) / 128.0 + 0.5);
554 int x2 = int(w * (k + region->KeyRange.high + 1) / 128.0 + 0.5);
555 Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();
556 gc->set_foreground(red);
557
558 if (!new_touch_left) window->draw_line(black, x, 1, x, h1 - 2);
559 if (!new_touch_right) window->draw_line(black, x2, 1, x2, h1 - 2);
560
561 if (k > move.pos) {
562 window->draw_rectangle(bg, true, prevx + (move.touch_left ? 1 : 0), 0,
563 std::min(x, prevx2 + 1 - (move.touch_right ? 1 : 0)) -
564 (prevx + (move.touch_left ? 1 : 0)), h1);
565
566 window->draw_line(black, std::max(x, prevx2 + 1), 0, x2, 0);
567 window->draw_line(black, std::max(x, prevx2 + 1), h1 - 1, x2, h1 - 1);
568 window->draw_rectangle(gc, true, std::max(x + 1, prevx2), 1,
569 x2 - std::max(x + 1, prevx2), h1 - 2);
570 } else {
571 window->draw_rectangle(bg, true, std::max(x2 + 1, prevx + (move.touch_left ? 1 : 0)), 0,
572 prevx2 + 1 - (move.touch_right ? 1 : 0) -
573 std::max(x2 + 1, prevx + (move.touch_left ? 1 : 0)), h1);
574
575 window->draw_line(black, x, 0, std::min(x2, prevx - 1), 0);
576 window->draw_line(black, x, h1 - 1, std::min(x2, prevx - 1), h1 - 1);
577
578 window->draw_rectangle(gc, true, x + 1, 1, std::min(x2 - 1, prevx) - x, h1 - 2);
579 }
580
581 move.pos = k;
582 move.touch_left = new_touch_left;
583 move.touch_right = new_touch_right;
584 }
585
586
587 bool RegionChooser::on_motion_notify_event(GdkEventMotion* event)
588 {
589 Glib::RefPtr<Gdk::Window> window = get_window();
590 int x, y;
591 Gdk::ModifierType state = Gdk::ModifierType(0);
592 window->get_pointer(x, y, state);
593
594 if (resize.active) {
595 motion_resize_region(x, y);
596 } else if (move.active) {
597 motion_move_region(x, y);
598 } else {
599 if (is_in_resize_zone(x, y)) {
600 if (!cursor_is_resize) {
601 window->set_cursor(Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW));
602 cursor_is_resize = true;
603 }
604 } else if (cursor_is_resize) {
605 window->set_cursor();
606 cursor_is_resize = false;
607 }
608 }
609
610 return true;
611 }
612
613 bool RegionChooser::is_in_resize_zone(double x, double y) {
614 const int w = get_width() - 1;
615
616 if (instrument && y >= 0 && y <= h1) {
617 gig::Region* prev_region = 0;
618 gig::Region* next_region;
619 for (gig::Region* r = regions.first(); r ; r = next_region) {
620 next_region = regions.next();
621
622 int lo = int(w * (r->KeyRange.low) / 128.0 + 0.5);
623 if (x <= lo - 2) break;
624 if (x < lo + 2) {
625 resize.region = r;
626 resize.pos = r->KeyRange.low;
627 resize.max = r->KeyRange.high;
628
629 if (prev_region && prev_region->KeyRange.high + 1 == r->KeyRange.low) {
630 // we don't know yet if it's the high limit of
631 // prev_region or the low limit of r that's going
632 // to be edited
633 resize.mode = resize.undecided;
634 resize.min = prev_region->KeyRange.low + 1;
635 resize.prev_region = prev_region;
636 return resize.min != resize.max;
637 }
638
639 // edit low limit
640 resize.mode = resize.moving_low_limit;
641 resize.min = prev_region ? prev_region->KeyRange.high + 1 : 0;
642 return resize.min != resize.max;
643 }
644 if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) {
645 int hi = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5);
646 if (x <= hi - 2) break;
647 if (x < hi + 2) {
648 // edit high limit
649 resize.region = r;
650 resize.pos = r->KeyRange.high + 1;
651 resize.mode = resize.moving_high_limit;
652 resize.min = r->KeyRange.low + 1;
653 resize.max = next_region ? next_region->KeyRange.low : 128;
654 return resize.min != resize.max;
655 }
656 }
657 prev_region = r;
658 }
659 }
660 return false;
661 }
662
663 sigc::signal<void>& RegionChooser::signal_region_selected()
664 {
665 return region_selected;
666 }
667
668 sigc::signal<void>& RegionChooser::signal_instrument_changed()
669 {
670 return instrument_changed;
671 }
672
673 void RegionChooser::show_region_properties()
674 {
675 if (!region) return;
676 Gtk::Dialog dialog("Region Properties", true /*modal*/);
677 // add "Keygroup" checkbox
678 Gtk::CheckButton checkBoxKeygroup("Member of a Keygroup (Exclusive Group)");
679 checkBoxKeygroup.set_active(region->KeyGroup);
680 dialog.get_vbox()->pack_start(checkBoxKeygroup);
681 checkBoxKeygroup.show();
682 // add "Keygroup" spinbox
683 Gtk::Adjustment adjustment(1, 1, pow(2,32));
684 Gtk::SpinButton spinBox(adjustment);
685 if (region->KeyGroup) spinBox.set_value(region->KeyGroup);
686 dialog.get_vbox()->pack_start(spinBox);
687 spinBox.show();
688 // add OK and CANCEL buttons to the dialog
689 dialog.add_button(Gtk::Stock::OK, 0);
690 dialog.add_button(Gtk::Stock::CANCEL, 1);
691 dialog.show_all_children();
692 if (!dialog.run()) { // OK selected ...
693 region->KeyGroup =
694 (checkBoxKeygroup.get_active()) ? spinBox.get_value_as_int() : 0;
695 }
696 }
697
698 void RegionChooser::add_region()
699 {
700 instrument_struct_to_be_changed_signal.emit(instrument);
701
702 region = instrument->AddRegion();
703 region->SetKeyRange(new_region_pos, new_region_pos);
704
705 instrument_struct_changed_signal.emit(instrument);
706 regions.update(instrument);
707
708 queue_draw();
709 region_selected();
710 instrument_changed();
711 }
712
713 void RegionChooser::delete_region()
714 {
715 instrument_struct_to_be_changed_signal.emit(instrument);
716 instrument->DeleteRegion(region);
717 instrument_struct_changed_signal.emit(instrument);
718 regions.update(instrument);
719
720 region = 0;
721 queue_draw();
722 region_selected();
723 instrument_changed();
724 }
725
726 void RegionChooser::manage_dimensions()
727 {
728 gig::Region* region = get_region();
729 if (!region) return;
730 dimensionManager.show(region);
731 }
732
733 void RegionChooser::on_dimension_manager_changed() {
734 region_selected();
735 instrument_changed();
736 }
737
738 sigc::signal<void, gig::Instrument*>& RegionChooser::signal_instrument_struct_to_be_changed() {
739 return instrument_struct_to_be_changed_signal;
740 }
741
742 sigc::signal<void, gig::Instrument*>& RegionChooser::signal_instrument_struct_changed() {
743 return instrument_struct_changed_signal;
744 }
745
746 sigc::signal<void, gig::Region*>& RegionChooser::signal_region_to_be_changed() {
747 return region_to_be_changed_signal;
748 }
749
750 sigc::signal<void, gig::Region*>& RegionChooser::signal_region_changed_signal() {
751 return region_changed_signal;
752 }

  ViewVC Help
Powered by ViewVC