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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1111 - (show annotations) (download)
Fri Mar 23 00:22:44 2007 UTC (17 years, 1 month ago) by schoenebeck
File size: 15796 byte(s)
* implemented adding and removing dimensions
  (adding dimensions yet segfaults, probably a libgig bug)

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

  ViewVC Help
Powered by ViewVC