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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1661 - (hide annotations) (download)
Sun Feb 3 14:10:47 2008 UTC (16 years, 2 months ago) by schoenebeck
File size: 30967 byte(s)
* implemented alternative behavior for the virtual MIDI keyboard
  (selectable by combobox below the keyboard)
* show absolute velocity value of note-on & note-off events below
  the virtual MIDI keyboard

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

  ViewVC Help
Powered by ViewVC