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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1107 - (show annotations) (download)
Thu Mar 22 20:00:10 2007 UTC (12 years, 8 months ago) by schoenebeck
File size: 15568 byte(s)
* added dialog for managing dimensions of a selected region
  (Add and Remove buttons not yet implemented)

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
87 RegionChooser::~RegionChooser()
88 {
89 }
90
91 void RegionChooser::on_realize()
92 {
93 // We need to call the base on_realize()
94 Gtk::DrawingArea::on_realize();
95
96 // Now we can allocate any additional resources we need
97 Glib::RefPtr<Gdk::Window> window = get_window();
98 gc = Gdk::GC::create(window);
99 window->clear();
100 }
101
102 bool RegionChooser::on_expose_event(GdkEventExpose* event)
103 {
104 Glib::RefPtr<Gdk::Window> window = get_window();
105 window->clear();
106 const int h = 40;
107 const int w = width - 1;
108 const int bh = int(h * 0.55);
109
110 Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();
111 Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc();
112
113 window->draw_rectangle(black, false, 0, h1, w, h - 1);
114 window->draw_rectangle(white, true, 1, h1 + 1, w - 1, h - 2);
115 for (int i = 0 ; i < 128 ; i++) {
116 int note = (i + 3) % 12;
117 int x = int(w * i / 128.0 + 0.5);
118
119 if (note == 1 || note == 4 || note == 6 || note == 9 || note == 11) {
120 int x2 = int(w * (i + 0.5) / 128.0 + 0.5);
121 window->draw_line(black, x2, h1 + bh, x2, h1 + h);
122
123 int x3 = int(w * (i + 1) / 128.0 + 0.5);
124 window->draw_rectangle(black, true, x, h1 + 1, x3 - x + 1, bh);
125 } else if (note == 3 || note == 8) {
126 window->draw_line(black, x, h1 + 1, x, h1 + h);
127 }
128 }
129
130 if (instrument) {
131 int i = 0;
132 gig::Region *nextRegion;
133 int x3 = -1;
134 for (gig::Region *r = instrument->GetFirstRegion() ;
135 r ;
136 r = nextRegion) {
137
138 if (x3 < 0) x3 = int(w * (r->KeyRange.low) / 128.0 + 0.5);
139 nextRegion = instrument->GetNextRegion();
140 if (!nextRegion || r->KeyRange.high + 1 != nextRegion->KeyRange.low) {
141 int x2 = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5);
142 window->draw_line(black, x3, 0, x2, 0);
143 window->draw_line(black, x3, h1 - 1, x2, h1 - 1);
144 window->draw_line(black, x2, 1, x2, h1 - 2);
145 window->draw_rectangle(white, true, x3 + 1, 1, x2 - x3 - 1, h1 - 2);
146 x3 = -1;
147 }
148 i++;
149 }
150
151 for (gig::Region *r = instrument->GetFirstRegion() ;
152 r ;
153 r = instrument->GetNextRegion()) {
154 int x = int(w * (r->KeyRange.low) / 128.0 + 0.5);
155 window->draw_line(black, x, 1, x, h1 - 2);
156 }
157
158 if (region) {
159 int x1 = int(w * (region->KeyRange.low) / 128.0 + 0.5);
160 int x2 = int(w * (region->KeyRange.high + 1) / 128.0 + 0.5);
161 gc->set_foreground(red);
162 window->draw_rectangle(gc, true, x1 + 1, 1, x2 - x1 - 1, h1 - 2);
163 }
164 }
165 return true;
166 }
167
168
169 void RegionChooser::on_size_request(GtkRequisition* requisition)
170 {
171 *requisition = GtkRequisition();
172 requisition->height = 40 + 20;
173 requisition->width = 500;
174 }
175
176
177 // not used
178 void RegionChooser::draw_region(int from, int to, const Gdk::Color& color)
179 {
180 const int h = 40;
181 const int w = width;
182 const int bh = int(h * 0.55);
183
184 Glib::RefPtr<Gdk::Window> window = get_window();
185 gc->set_foreground(color);
186
187 for (int i = from ; i < to ; i++) {
188 int note = (i + 3) % 12;
189 int x = int(w * i / 128.0 + 0.5) + 1;
190 int x2 = int(w * (i + 1.5) / 128.0 + 0.5);
191 int x3 = int(w * (i + 1) / 128.0 + 0.5);
192 int x4 = int(w * (i - 0.5) / 128 + 0.5) + 1;
193 int w1 = x3 - x;
194 switch (note) {
195 case 0: case 5: case 10:
196 window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);
197 window->draw_rectangle(gc, true, x4, h1 + bh + 1, x2 - x4, h - bh - 2);
198 break;
199 case 2: case 7:
200 window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);
201 window->draw_rectangle(gc, true, x4, h1 + bh + 1, x3 - x4, h - bh - 2);
202 break;
203 case 3: case 8:
204 window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);
205 window->draw_rectangle(gc, true, x, h1 + bh + 1, x2 - x, h - bh - 2);
206 break;
207 default:
208 window->draw_rectangle(gc, true, x, h1 + 1, w1, bh - 1);
209 break;
210 }
211 }
212 }
213
214 void RegionChooser::set_instrument(gig::Instrument* instrument)
215 {
216 this->instrument = instrument;
217 region = instrument ? instrument->GetFirstRegion() : 0;
218 queue_draw();
219 sel_changed_signal.emit();
220 }
221
222 bool RegionChooser::on_button_release_event(GdkEventButton* event)
223 {
224 if (resize.active) {
225 get_window()->pointer_ungrab(event->time);
226 resize.active = false;
227
228 if (resize.mode == resize.moving_high_limit) {
229 resize.region->KeyRange.high = resize.pos - 1;
230 } else if (resize.mode == resize.moving_low_limit) {
231 resize.region->KeyRange.low = resize.pos;
232 }
233
234 if (!is_in_resize_zone(event->x, event->y) && cursor_is_resize) {
235 get_window()->set_cursor();
236 cursor_is_resize = false;
237 }
238 }
239 return true;
240 }
241
242 bool RegionChooser::on_button_press_event(GdkEventButton* event)
243 {
244 if (!instrument) return true;
245
246 int k = int(event->x / (width - 1) * 128.0);
247
248 if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
249 gig::Region* r = get_region(k);
250 if (r) {
251 region = r;
252 queue_draw();
253 sel_changed_signal.emit();
254 popup_menu_inside_region->popup(event->button, event->time);
255 } else {
256 new_region_pos = k;
257 popup_menu_outside_region->popup(event->button, event->time);
258 }
259 } else {
260 if (is_in_resize_zone(event->x, event->y)) {
261 Gdk::Cursor double_arrow(Gdk::SB_H_DOUBLE_ARROW);
262 get_window()->pointer_grab(false,
263 Gdk::BUTTON_RELEASE_MASK |
264 Gdk::POINTER_MOTION_MASK |
265 Gdk::POINTER_MOTION_HINT_MASK,
266 double_arrow, event->time);
267 resize.active = true;
268 } else {
269 gig::Region* r = get_region(k);
270 if (r) {
271 region = r;
272 queue_draw();
273 sel_changed_signal.emit();
274 }
275 }
276 }
277 return true;
278 }
279
280 gig::Region* RegionChooser::get_region(int key)
281 {
282 for (gig::Region *r = instrument->GetFirstRegion() ; r ;
283 r = instrument->GetNextRegion()) {
284 if (key < r->KeyRange.low) return 0;
285 if (key <= r->KeyRange.high) return r;
286 }
287 return 0;
288 }
289
290 bool RegionChooser::on_motion_notify_event(GdkEventMotion* event)
291 {
292 const int w = width - 1;
293 Glib::RefPtr<Gdk::Window> window = get_window();
294 int x, y;
295 Gdk::ModifierType state = Gdk::ModifierType(0);
296 window->get_pointer(x, y, state);
297 if (resize.active) {
298 int k = int(double(x) / w * 128.0 + 0.5);
299
300 if (k < resize.min) k = resize.min;
301 else if (k > resize.max) k = resize.max;
302
303 if (k != resize.pos) {
304 if (resize.mode == resize.undecided) {
305 if (k < resize.pos) {
306 // edit high limit of prev_region
307 resize.max = resize.region->KeyRange.low;
308 resize.region = resize.prev_region;
309 resize.mode = resize.moving_high_limit;
310 } else {
311 // edit low limit of region
312 resize.min = resize.prev_region->KeyRange.high + 1;
313 resize.mode = resize.moving_low_limit;
314 }
315 }
316 Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();
317 Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc();
318 if (region == resize.region) {
319 gc->set_foreground(red);
320 white = gc;
321 }
322 Glib::RefPtr<const Gdk::GC> bg = get_style()->get_bg_gc(Gtk::STATE_NORMAL);
323 int prevx = int(w * resize.pos / 128.0 + 0.5);
324 x = int(w * k / 128.0 + 0.5);
325
326 if (resize.mode == resize.moving_high_limit) {
327 if (k > resize.pos) {
328 window->draw_rectangle(white, true, prevx, 1, x - prevx, h1 - 2);
329 window->draw_line(black, prevx, 0, x, 0);
330 window->draw_line(black, prevx, h1 - 1, x, h1 - 1);
331 } else {
332 int xx = ((resize.pos == resize.max && resize.max != 128) ? 1 : 0);
333 window->draw_rectangle(bg, true, x + 1, 0, prevx - x - xx, h1);
334 }
335 } else {
336 if (k < resize.pos) {
337 window->draw_rectangle(white, true, x + 1, 1, prevx - x, h1 - 2);
338 window->draw_line(black, x, 0, prevx, 0);
339 window->draw_line(black, x, h1 - 1, prevx, h1 - 1);
340 } else {
341 int xx = ((resize.pos == resize.min && resize.min != 0) ? 1 : 0);
342 window->draw_rectangle(bg, true, prevx + xx, 0, x - prevx - xx, h1);
343 }
344 }
345 window->draw_line(black, x, 1, x, h1 - 2);
346 resize.pos = k;
347 }
348 } else {
349 if (is_in_resize_zone(x, y)) {
350 if (!cursor_is_resize) {
351 Gdk::Cursor double_arrow(Gdk::SB_H_DOUBLE_ARROW);
352 window->set_cursor(double_arrow);
353 cursor_is_resize = true;
354 }
355 } else if (cursor_is_resize) {
356 window->set_cursor();
357 cursor_is_resize = false;
358 }
359 }
360
361 return true;
362 }
363
364 bool RegionChooser::is_in_resize_zone(double x, double y) {
365 const int w = width - 1;
366
367 if (instrument && y >= 0 && y <= h1) {
368 gig::Region* prev_region = 0;
369 gig::Region* next_region;
370 for (gig::Region* r = instrument->GetFirstRegion() ; r ; r = next_region) {
371 next_region = instrument->GetNextRegion();
372
373 int lo = int(w * (r->KeyRange.low) / 128.0 + 0.5);
374 if (x <= lo - 2) break;
375 if (x < lo + 2) {
376 resize.region = r;
377 resize.pos = r->KeyRange.low;
378 resize.max = r->KeyRange.high;
379
380 if (prev_region && prev_region->KeyRange.high + 1 == r->KeyRange.low) {
381 // we don't know yet if it's the high limit of
382 // prev_region or the low limit of r that's going
383 // to be edited
384 resize.mode = resize.undecided;
385 resize.min = prev_region->KeyRange.low + 1;
386 resize.prev_region = prev_region;
387 return true;
388 }
389
390 // edit low limit
391 resize.mode = resize.moving_low_limit;
392 resize.min = prev_region ? prev_region->KeyRange.high + 1 : 0;
393 return true;
394 }
395 if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) {
396 int hi = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5);
397 if (x <= hi - 2) break;
398 if (x < hi + 2) {
399 // edit high limit
400 resize.region = r;
401 resize.pos = r->KeyRange.high + 1;
402 resize.mode = resize.moving_high_limit;
403 resize.min = r->KeyRange.low + 1;
404 resize.max = next_region ? next_region->KeyRange.low : 128;
405 return true;
406 }
407 }
408 prev_region = r;
409 }
410 }
411 return false;
412 }
413
414 sigc::signal<void> RegionChooser::signal_sel_changed()
415 {
416 return sel_changed_signal;
417 }
418
419 void RegionChooser::show_region_properties()
420 {
421 }
422
423 void RegionChooser::add_region()
424 {
425 gig::Region* r;
426 for (r = instrument->GetFirstRegion() ; r ; r = instrument->GetNextRegion()) {
427 if (r->KeyRange.low > new_region_pos) break;
428 }
429
430 region = instrument->AddRegion();
431 region->KeyRange.low = region->KeyRange.high = new_region_pos;
432
433 instrument->MoveRegion(region, r);
434 queue_draw();
435 sel_changed_signal.emit();
436 }
437
438 void RegionChooser::delete_region()
439 {
440 instrument->DeleteRegion(region);
441 region = 0;
442 queue_draw();
443 sel_changed_signal.emit();
444 }
445
446 void RegionChooser::manage_dimensions()
447 {
448 gig::Region* region = get_region();
449 if (!region) return;
450 dimensionManager.show(region);
451 }

  ViewVC Help
Powered by ViewVC