/[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 1831 - (hide annotations) (download)
Tue Feb 3 19:38:19 2009 UTC (15 years, 2 months ago) by persson
File size: 31203 byte(s)
* made all visible strings translatable

1 schoenebeck 1225 /*
2 persson 1831 * Copyright (C) 2006-2009 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 persson 1831 // properties of the virtual keyboard
86 schoenebeck 1661 {
87 persson 1831 const char* choices[] = { _("normal"), _("chord"), NULL };
88 schoenebeck 1661 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 persson 1677 dimensionManager.set_region(region);
339 schoenebeck 1225 }
340    
341     bool RegionChooser::on_button_release_event(GdkEventButton* event)
342     {
343 schoenebeck 1660 const int k = int(event->x / (get_width() - 1) * 128.0);
344    
345 schoenebeck 1661 // handle-note off on virtual keyboard
346 schoenebeck 1660 if (event->type == GDK_BUTTON_RELEASE) {
347 schoenebeck 1661 int velocity = (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 :
348     int(float(event->y - REGION_BLOCK_HEIGHT) / float(KEYBOARD_HEIGHT) * 128.0f) + 1;
349     if (velocity <= 0) velocity = 1;
350     switch (m_VirtKeybModeChoice.get_value()) {
351     case VIRT_KEYBOARD_MODE_CHORD:
352     if (event->y >= REGION_BLOCK_HEIGHT)
353     keyboard_key_released_signal.emit(k, velocity);
354     break;
355     case VIRT_KEYBOARD_MODE_NORMAL:
356     default:
357     if (currentActiveKey >= 0 && currentActiveKey <= 127) {
358     keyboard_key_released_signal.emit(currentActiveKey, velocity);
359     currentActiveKey = -1;
360     }
361     break;
362 schoenebeck 1660 }
363     }
364    
365 schoenebeck 1225 if (resize.active) {
366     get_window()->pointer_ungrab(event->time);
367     resize.active = false;
368    
369     if (resize.mode == resize.moving_high_limit) {
370 persson 1261 if (resize.region->KeyRange.high != resize.pos - 1) {
371 schoenebeck 1336 instrument_struct_to_be_changed_signal.emit(instrument);
372     resize.region->SetKeyRange(
373     resize.region->KeyRange.low, // low
374     resize.pos - 1 // high
375     );
376 persson 1623 regions.update(instrument);
377 schoenebeck 1336 instrument_changed.emit();
378     instrument_struct_changed_signal.emit(instrument);
379 persson 1261 }
380 schoenebeck 1225 } else if (resize.mode == resize.moving_low_limit) {
381 persson 1261 if (resize.region->KeyRange.low != resize.pos) {
382 schoenebeck 1336 instrument_struct_to_be_changed_signal.emit(instrument);
383     resize.region->SetKeyRange(
384     resize.pos, // low
385     resize.region->KeyRange.high // high
386     );
387 persson 1623 regions.update(instrument);
388 schoenebeck 1336 instrument_changed.emit();
389     instrument_struct_changed_signal.emit(instrument);
390 persson 1261 }
391 schoenebeck 1225 }
392    
393     if (!is_in_resize_zone(event->x, event->y) && cursor_is_resize) {
394     get_window()->set_cursor();
395     cursor_is_resize = false;
396     }
397 persson 1262 } else if (move.active) {
398     get_window()->pointer_ungrab(event->time);
399     move.active = false;
400    
401     if (move.pos) {
402 schoenebeck 1336 instrument_struct_to_be_changed_signal.emit(instrument);
403     region->SetKeyRange(
404     region->KeyRange.low + move.pos,
405     region->KeyRange.high + move.pos
406     );
407 persson 1623 regions.update(instrument);
408 persson 1533 instrument_changed.emit();
409 schoenebeck 1336 instrument_struct_changed_signal.emit(instrument);
410 persson 1262 }
411    
412     if (is_in_resize_zone(event->x, event->y)) {
413     get_window()->set_cursor(Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW));
414     cursor_is_resize = true;
415     }
416 schoenebeck 1225 }
417     return true;
418     }
419    
420     bool RegionChooser::on_button_press_event(GdkEventButton* event)
421     {
422     if (!instrument) return true;
423    
424 schoenebeck 1660 const int k = int(event->x / (get_width() - 1) * 128.0);
425 schoenebeck 1225
426 schoenebeck 1660 if (event->type == GDK_BUTTON_PRESS) {
427     if (event->y >= REGION_BLOCK_HEIGHT) {
428     int velocity = (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 :
429     int(float(event->y - REGION_BLOCK_HEIGHT) / float(KEYBOARD_HEIGHT) * 128.0f) + 1;
430 schoenebeck 1661 currentActiveKey = k;
431 schoenebeck 1660 keyboard_key_hit_signal.emit(k, velocity);
432     }
433     }
434    
435     if (event->y >= REGION_BLOCK_HEIGHT) return true;
436 schoenebeck 1225 if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
437     gig::Region* r = get_region(k);
438     if (r) {
439     region = r;
440     queue_draw();
441 persson 1261 region_selected();
442 persson 1677 dimensionManager.set_region(region);
443 schoenebeck 1225 popup_menu_inside_region->popup(event->button, event->time);
444     } else {
445     new_region_pos = k;
446     popup_menu_outside_region->popup(event->button, event->time);
447     }
448     } else {
449     if (is_in_resize_zone(event->x, event->y)) {
450     get_window()->pointer_grab(false,
451     Gdk::BUTTON_RELEASE_MASK |
452     Gdk::POINTER_MOTION_MASK |
453     Gdk::POINTER_MOTION_HINT_MASK,
454 persson 1262 Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW), event->time);
455 schoenebeck 1225 resize.active = true;
456     } else {
457     gig::Region* r = get_region(k);
458     if (r) {
459     region = r;
460     queue_draw();
461 persson 1261 region_selected();
462 persson 1677 dimensionManager.set_region(region);
463 persson 1262
464     get_window()->pointer_grab(false,
465     Gdk::BUTTON_RELEASE_MASK |
466     Gdk::POINTER_MOTION_MASK |
467     Gdk::POINTER_MOTION_HINT_MASK,
468     Gdk::Cursor(Gdk::FLEUR), event->time);
469     move.active = true;
470     move.from_x = event->x;
471     move.pos = 0;
472 schoenebeck 1225 }
473     }
474     }
475     return true;
476     }
477    
478     gig::Region* RegionChooser::get_region(int key)
479     {
480 persson 1262 gig::Region* prev_region = 0;
481     gig::Region* next_region;
482 persson 1623 for (gig::Region *r = regions.first() ; r ; r = next_region) {
483     next_region = regions.next();
484 persson 1262
485 schoenebeck 1225 if (key < r->KeyRange.low) return 0;
486 persson 1262 if (key <= r->KeyRange.high) {
487     move.touch_left = prev_region && prev_region->KeyRange.high + 1 == r->KeyRange.low;
488     move.touch_right = next_region && r->KeyRange.high + 1 == next_region->KeyRange.low;
489     return r;
490     }
491     prev_region = r;
492 schoenebeck 1225 }
493     return 0;
494     }
495    
496 persson 1262 void RegionChooser::motion_resize_region(int x, int y)
497 schoenebeck 1225 {
498 persson 1623 const int w = get_width() - 1;
499 schoenebeck 1225 Glib::RefPtr<Gdk::Window> window = get_window();
500    
501 persson 1262 int k = int(double(x) / w * 128.0 + 0.5);
502 schoenebeck 1225
503 persson 1262 if (k < resize.min) k = resize.min;
504     else if (k > resize.max) k = resize.max;
505    
506     if (k != resize.pos) {
507     if (resize.mode == resize.undecided) {
508     if (k < resize.pos) {
509     // edit high limit of prev_region
510     resize.max = resize.region->KeyRange.low;
511     resize.region = resize.prev_region;
512     resize.mode = resize.moving_high_limit;
513     } else {
514     // edit low limit of region
515     resize.min = resize.prev_region->KeyRange.high + 1;
516     resize.mode = resize.moving_low_limit;
517 schoenebeck 1225 }
518 persson 1262 }
519     Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();
520     Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc();
521     if (region == resize.region) {
522     gc->set_foreground(red);
523     white = gc;
524     }
525     Glib::RefPtr<const Gdk::GC> bg = get_style()->get_bg_gc(Gtk::STATE_NORMAL);
526     int prevx = int(w * resize.pos / 128.0 + 0.5);
527     x = int(w * k / 128.0 + 0.5);
528    
529     if (resize.mode == resize.moving_high_limit) {
530     if (k > resize.pos) {
531     window->draw_rectangle(white, true, prevx, 1, x - prevx, h1 - 2);
532     window->draw_line(black, prevx, 0, x, 0);
533     window->draw_line(black, prevx, h1 - 1, x, h1 - 1);
534     } else {
535     int xx = ((resize.pos == resize.max && resize.max != 128) ? 1 : 0);
536     window->draw_rectangle(bg, true, x + 1, 0, prevx - x - xx, h1);
537 schoenebeck 1225 }
538 persson 1262 } else {
539     if (k < resize.pos) {
540     window->draw_rectangle(white, true, x + 1, 1, prevx - x, h1 - 2);
541     window->draw_line(black, x, 0, prevx, 0);
542     window->draw_line(black, x, h1 - 1, prevx, h1 - 1);
543     } else {
544     int xx = ((resize.pos == resize.min && resize.min != 0) ? 1 : 0);
545     window->draw_rectangle(bg, true, prevx + xx, 0, x - prevx - xx, h1);
546     }
547     }
548     window->draw_line(black, x, 1, x, h1 - 2);
549     resize.pos = k;
550     }
551     }
552 schoenebeck 1225
553 persson 1262 void RegionChooser::motion_move_region(int x, int y)
554     {
555 persson 1623 const int w = get_width() - 1;
556 persson 1262 Glib::RefPtr<Gdk::Window> window = get_window();
557    
558     int k = int(double(x - move.from_x) / w * 128.0 + 0.5);
559     if (k == move.pos) return;
560     int new_k;
561     bool new_touch_left;
562     bool new_touch_right;
563     int a = 0;
564     if (k > move.pos) {
565 persson 1623 for (gig::Region* r = regions.first() ; ; r = regions.next()) {
566 persson 1262 if (r != region) {
567     int b = r ? r->KeyRange.low : 128;
568    
569     // gap: from a to b (not inclusive b)
570    
571     if (region->KeyRange.high + move.pos >= b) {
572     // not found the current gap yet, just continue
573 schoenebeck 1225 } else {
574 persson 1262
575     if (a > region->KeyRange.low + k) {
576     // this gap is too far to the right, break
577     break;
578     }
579    
580     int newhigh = std::min(region->KeyRange.high + k, b - 1);
581     int newlo = newhigh - (region->KeyRange.high - region->KeyRange.low);
582    
583     if (newlo >= a) {
584     // yes it fits - it's a candidate
585     new_k = newlo - region->KeyRange.low;
586     new_touch_left = a > 0 && a == newlo;
587     new_touch_right = b < 128 && newhigh + 1 == b;
588     }
589 schoenebeck 1225 }
590 persson 1262 if (!r) break;
591     a = r->KeyRange.high + 1;
592     }
593     }
594     } else {
595 persson 1623 for (gig::Region* r = regions.first() ; ; r = regions.next()) {
596 persson 1262 if (r != region) {
597     int b = r ? r->KeyRange.low : 128;
598    
599     // gap from a to b (not inclusive b)
600    
601     if (region->KeyRange.high + k >= b) {
602     // not found the current gap yet, just continue
603 schoenebeck 1225 } else {
604 persson 1262
605     if (a > region->KeyRange.low + move.pos) {
606     // this gap is too far to the right, break
607     break;
608     }
609    
610     int newlo = std::max(region->KeyRange.low + k, a);
611     int newhigh = newlo + (region->KeyRange.high - region->KeyRange.low);
612    
613     if (newhigh < b) {
614     // yes it fits - break as the first one is the best
615     new_k = newlo - region->KeyRange.low;
616     new_touch_left = a > 0 && a == newlo;
617     new_touch_right = b < 128 && newhigh + 1 == b;
618     break;
619     }
620 schoenebeck 1225 }
621 persson 1262 if (!r) break;
622     a = r->KeyRange.high + 1;
623 schoenebeck 1225 }
624     }
625 persson 1262 }
626     k = new_k;
627     if (k == move.pos) return;
628    
629     Glib::RefPtr<const Gdk::GC> bg = get_style()->get_bg_gc(Gtk::STATE_NORMAL);
630     int prevx = int(w * (move.pos + region->KeyRange.low) / 128.0 + 0.5);
631     x = int(w * (k + region->KeyRange.low) / 128.0 + 0.5);
632     int prevx2 = int(w * (move.pos + region->KeyRange.high + 1) / 128.0 + 0.5);
633     int x2 = int(w * (k + region->KeyRange.high + 1) / 128.0 + 0.5);
634     Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();
635     gc->set_foreground(red);
636    
637     if (!new_touch_left) window->draw_line(black, x, 1, x, h1 - 2);
638     if (!new_touch_right) window->draw_line(black, x2, 1, x2, h1 - 2);
639    
640     if (k > move.pos) {
641     window->draw_rectangle(bg, true, prevx + (move.touch_left ? 1 : 0), 0,
642     std::min(x, prevx2 + 1 - (move.touch_right ? 1 : 0)) -
643     (prevx + (move.touch_left ? 1 : 0)), h1);
644    
645     window->draw_line(black, std::max(x, prevx2 + 1), 0, x2, 0);
646     window->draw_line(black, std::max(x, prevx2 + 1), h1 - 1, x2, h1 - 1);
647     window->draw_rectangle(gc, true, std::max(x + 1, prevx2), 1,
648     x2 - std::max(x + 1, prevx2), h1 - 2);
649 schoenebeck 1225 } else {
650 persson 1262 window->draw_rectangle(bg, true, std::max(x2 + 1, prevx + (move.touch_left ? 1 : 0)), 0,
651     prevx2 + 1 - (move.touch_right ? 1 : 0) -
652     std::max(x2 + 1, prevx + (move.touch_left ? 1 : 0)), h1);
653    
654     window->draw_line(black, x, 0, std::min(x2, prevx - 1), 0);
655     window->draw_line(black, x, h1 - 1, std::min(x2, prevx - 1), h1 - 1);
656    
657     window->draw_rectangle(gc, true, x + 1, 1, std::min(x2 - 1, prevx) - x, h1 - 2);
658     }
659    
660     move.pos = k;
661     move.touch_left = new_touch_left;
662     move.touch_right = new_touch_right;
663     }
664    
665    
666     bool RegionChooser::on_motion_notify_event(GdkEventMotion* event)
667     {
668     Glib::RefPtr<Gdk::Window> window = get_window();
669     int x, y;
670     Gdk::ModifierType state = Gdk::ModifierType(0);
671     window->get_pointer(x, y, state);
672    
673 schoenebeck 1661 // handle virtual MIDI keyboard
674     if (m_VirtKeybModeChoice.get_value() != VIRT_KEYBOARD_MODE_CHORD &&
675     currentActiveKey > 0 &&
676     event->y >= REGION_BLOCK_HEIGHT &&
677     event->y < REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT)
678     {
679     const int k = int(event->x / (get_width() - 1) * 128.0);
680     int velocity = (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 :
681     int(float(event->y - REGION_BLOCK_HEIGHT) / float(KEYBOARD_HEIGHT) * 128.0f) + 1;
682     if (velocity <= 0) velocity = 1;
683     keyboard_key_released_signal.emit(currentActiveKey, velocity);
684     currentActiveKey = k;
685     keyboard_key_hit_signal.emit(k, velocity);
686     }
687    
688 persson 1262 if (resize.active) {
689     motion_resize_region(x, y);
690     } else if (move.active) {
691     motion_move_region(x, y);
692     } else {
693 schoenebeck 1225 if (is_in_resize_zone(x, y)) {
694     if (!cursor_is_resize) {
695 persson 1262 window->set_cursor(Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW));
696 schoenebeck 1225 cursor_is_resize = true;
697     }
698     } else if (cursor_is_resize) {
699     window->set_cursor();
700     cursor_is_resize = false;
701     }
702     }
703    
704     return true;
705     }
706    
707     bool RegionChooser::is_in_resize_zone(double x, double y) {
708 persson 1623 const int w = get_width() - 1;
709 schoenebeck 1225
710     if (instrument && y >= 0 && y <= h1) {
711     gig::Region* prev_region = 0;
712     gig::Region* next_region;
713 persson 1623 for (gig::Region* r = regions.first(); r ; r = next_region) {
714     next_region = regions.next();
715 schoenebeck 1225
716     int lo = int(w * (r->KeyRange.low) / 128.0 + 0.5);
717     if (x <= lo - 2) break;
718     if (x < lo + 2) {
719     resize.region = r;
720     resize.pos = r->KeyRange.low;
721     resize.max = r->KeyRange.high;
722    
723     if (prev_region && prev_region->KeyRange.high + 1 == r->KeyRange.low) {
724     // we don't know yet if it's the high limit of
725     // prev_region or the low limit of r that's going
726     // to be edited
727     resize.mode = resize.undecided;
728     resize.min = prev_region->KeyRange.low + 1;
729     resize.prev_region = prev_region;
730 persson 1623 return resize.min != resize.max;
731 schoenebeck 1225 }
732    
733     // edit low limit
734     resize.mode = resize.moving_low_limit;
735     resize.min = prev_region ? prev_region->KeyRange.high + 1 : 0;
736 persson 1623 return resize.min != resize.max;
737 schoenebeck 1225 }
738     if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) {
739     int hi = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5);
740     if (x <= hi - 2) break;
741     if (x < hi + 2) {
742     // edit high limit
743     resize.region = r;
744     resize.pos = r->KeyRange.high + 1;
745     resize.mode = resize.moving_high_limit;
746     resize.min = r->KeyRange.low + 1;
747     resize.max = next_region ? next_region->KeyRange.low : 128;
748 persson 1623 return resize.min != resize.max;
749 schoenebeck 1225 }
750     }
751     prev_region = r;
752     }
753     }
754     return false;
755     }
756    
757 schoenebeck 1339 sigc::signal<void>& RegionChooser::signal_region_selected()
758 schoenebeck 1225 {
759 persson 1261 return region_selected;
760 schoenebeck 1225 }
761    
762 schoenebeck 1339 sigc::signal<void>& RegionChooser::signal_instrument_changed()
763 persson 1261 {
764     return instrument_changed;
765     }
766    
767 schoenebeck 1225 void RegionChooser::show_region_properties()
768     {
769     if (!region) return;
770 persson 1831 Gtk::Dialog dialog(_("Region Properties"), true /*modal*/);
771 schoenebeck 1225 // add "Keygroup" checkbox
772 persson 1831 Gtk::CheckButton checkBoxKeygroup(_("Member of a Keygroup (Exclusive Group)"));
773 schoenebeck 1225 checkBoxKeygroup.set_active(region->KeyGroup);
774     dialog.get_vbox()->pack_start(checkBoxKeygroup);
775     checkBoxKeygroup.show();
776     // add "Keygroup" spinbox
777     Gtk::Adjustment adjustment(1, 1, pow(2,32));
778     Gtk::SpinButton spinBox(adjustment);
779     if (region->KeyGroup) spinBox.set_value(region->KeyGroup);
780     dialog.get_vbox()->pack_start(spinBox);
781     spinBox.show();
782     // add OK and CANCEL buttons to the dialog
783     dialog.add_button(Gtk::Stock::OK, 0);
784     dialog.add_button(Gtk::Stock::CANCEL, 1);
785     dialog.show_all_children();
786     if (!dialog.run()) { // OK selected ...
787     region->KeyGroup =
788     (checkBoxKeygroup.get_active()) ? spinBox.get_value_as_int() : 0;
789     }
790     }
791    
792     void RegionChooser::add_region()
793     {
794 schoenebeck 1322 instrument_struct_to_be_changed_signal.emit(instrument);
795    
796 schoenebeck 1225 region = instrument->AddRegion();
797 schoenebeck 1336 region->SetKeyRange(new_region_pos, new_region_pos);
798 schoenebeck 1225
799 schoenebeck 1322 instrument_struct_changed_signal.emit(instrument);
800 persson 1623 regions.update(instrument);
801 schoenebeck 1322
802 schoenebeck 1225 queue_draw();
803 persson 1261 region_selected();
804 persson 1677 dimensionManager.set_region(region);
805 persson 1261 instrument_changed();
806 schoenebeck 1225 }
807    
808     void RegionChooser::delete_region()
809     {
810 schoenebeck 1322 instrument_struct_to_be_changed_signal.emit(instrument);
811 schoenebeck 1225 instrument->DeleteRegion(region);
812 schoenebeck 1322 instrument_struct_changed_signal.emit(instrument);
813 persson 1623 regions.update(instrument);
814 schoenebeck 1322
815 schoenebeck 1225 region = 0;
816     queue_draw();
817 persson 1261 region_selected();
818 persson 1677 dimensionManager.set_region(region);
819 persson 1261 instrument_changed();
820 schoenebeck 1225 }
821    
822     void RegionChooser::manage_dimensions()
823     {
824     gig::Region* region = get_region();
825     if (!region) return;
826     dimensionManager.show(region);
827     }
828    
829     void RegionChooser::on_dimension_manager_changed() {
830 persson 1261 region_selected();
831     instrument_changed();
832 schoenebeck 1225 }
833 schoenebeck 1322
834 schoenebeck 1339 sigc::signal<void, gig::Instrument*>& RegionChooser::signal_instrument_struct_to_be_changed() {
835 schoenebeck 1322 return instrument_struct_to_be_changed_signal;
836     }
837    
838 schoenebeck 1339 sigc::signal<void, gig::Instrument*>& RegionChooser::signal_instrument_struct_changed() {
839 schoenebeck 1322 return instrument_struct_changed_signal;
840     }
841    
842 schoenebeck 1339 sigc::signal<void, gig::Region*>& RegionChooser::signal_region_to_be_changed() {
843 schoenebeck 1322 return region_to_be_changed_signal;
844     }
845    
846 schoenebeck 1339 sigc::signal<void, gig::Region*>& RegionChooser::signal_region_changed_signal() {
847 schoenebeck 1322 return region_changed_signal;
848     }
849 schoenebeck 1660
850     sigc::signal<void, int/*key*/, int/*velocity*/>& RegionChooser::signal_keyboard_key_hit() {
851     return keyboard_key_hit_signal;
852     }
853    
854     sigc::signal<void, int/*key*/, int/*velocity*/>& RegionChooser::signal_keyboard_key_released() {
855     return keyboard_key_released_signal;
856     }

  ViewVC Help
Powered by ViewVC