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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1103 - (show annotations) (download)
Sun Mar 18 07:26:43 2007 UTC (17 years, 1 month ago) by persson
File size: 15251 byte(s)
* implemented add and remove region
* fixed crash in DimRegionChooser for instruments without regions

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
24 RegionChooser::RegionChooser()
25 {
26 Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap();
27
28 black = Gdk::Color("black");
29 white = Gdk::Color("white");
30 red = Gdk::Color("#8070ff");
31 blue = Gdk::Color("#c098ff");
32 green = Gdk::Color("#a088ff");
33 grey1 = Gdk::Color("red");
34
35 colormap->alloc_color(black);
36 colormap->alloc_color(white);
37 colormap->alloc_color(red);
38 colormap->alloc_color(blue);
39 colormap->alloc_color(green);
40 colormap->alloc_color(grey1);
41 instrument = 0;
42 region = 0;
43 resize.active = false;
44 cursor_is_resize = false;
45 h1 = 20;
46 width = 800;
47
48 actionGroup = Gtk::ActionGroup::create();
49 actionGroup->add(Gtk::Action::create("Properties",
50 Gtk::Stock::PROPERTIES),
51 sigc::mem_fun(*this,
52 &RegionChooser::show_region_properties));
53 actionGroup->add(Gtk::Action::create("Remove", Gtk::Stock::REMOVE),
54 sigc::mem_fun(*this, &RegionChooser::delete_region));
55 actionGroup->add(Gtk::Action::create("Add", Gtk::Stock::ADD),
56 sigc::mem_fun(*this, &RegionChooser::add_region));
57
58 uiManager = Gtk::UIManager::create();
59 uiManager->insert_action_group(actionGroup);
60 Glib::ustring ui_info =
61 "<ui>"
62 " <popup name='PopupMenuInsideRegion'>"
63 " <menuitem action='Properties'/>"
64 " <menuitem action='Remove'/>"
65 " </popup>"
66 " <popup name='PopupMenuOutsideRegion'>"
67 " <menuitem action='Add'/>"
68 " </popup>"
69 "</ui>";
70 uiManager->add_ui_from_string(ui_info);
71
72 popup_menu_inside_region = dynamic_cast<Gtk::Menu*>(
73 uiManager->get_widget("/PopupMenuInsideRegion"));
74 popup_menu_outside_region = dynamic_cast<Gtk::Menu*>(
75 uiManager->get_widget("/PopupMenuOutsideRegion"));
76
77 add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK |
78 Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK);
79 }
80
81 RegionChooser::~RegionChooser()
82 {
83 }
84
85 void RegionChooser::on_realize()
86 {
87 // We need to call the base on_realize()
88 Gtk::DrawingArea::on_realize();
89
90 // Now we can allocate any additional resources we need
91 Glib::RefPtr<Gdk::Window> window = get_window();
92 gc = Gdk::GC::create(window);
93 window->clear();
94 }
95
96 bool RegionChooser::on_expose_event(GdkEventExpose* event)
97 {
98 Glib::RefPtr<Gdk::Window> window = get_window();
99 window->clear();
100 const int h = 40;
101 const int w = width - 1;
102 const int bh = int(h * 0.55);
103
104 Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();
105 Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc();
106
107 window->draw_rectangle(black, false, 0, h1, w, h - 1);
108 window->draw_rectangle(white, true, 1, h1 + 1, w - 1, h - 2);
109 for (int i = 0 ; i < 128 ; i++) {
110 int note = (i + 3) % 12;
111 int x = int(w * i / 128.0 + 0.5);
112
113 if (note == 1 || note == 4 || note == 6 || note == 9 || note == 11) {
114 int x2 = int(w * (i + 0.5) / 128.0 + 0.5);
115 window->draw_line(black, x2, h1 + bh, x2, h1 + h);
116
117 int x3 = int(w * (i + 1) / 128.0 + 0.5);
118 window->draw_rectangle(black, true, x, h1 + 1, x3 - x + 1, bh);
119 } else if (note == 3 || note == 8) {
120 window->draw_line(black, x, h1 + 1, x, h1 + h);
121 }
122 }
123
124 if (instrument) {
125 int i = 0;
126 gig::Region *nextRegion;
127 int x3 = -1;
128 for (gig::Region *r = instrument->GetFirstRegion() ;
129 r ;
130 r = nextRegion) {
131
132 if (x3 < 0) x3 = int(w * (r->KeyRange.low) / 128.0 + 0.5);
133 nextRegion = instrument->GetNextRegion();
134 if (!nextRegion || r->KeyRange.high + 1 != nextRegion->KeyRange.low) {
135 int x2 = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5);
136 window->draw_line(black, x3, 0, x2, 0);
137 window->draw_line(black, x3, h1 - 1, x2, h1 - 1);
138 window->draw_line(black, x2, 1, x2, h1 - 2);
139 window->draw_rectangle(white, true, x3 + 1, 1, x2 - x3 - 1, h1 - 2);
140 x3 = -1;
141 }
142 i++;
143 }
144
145 for (gig::Region *r = instrument->GetFirstRegion() ;
146 r ;
147 r = instrument->GetNextRegion()) {
148 int x = int(w * (r->KeyRange.low) / 128.0 + 0.5);
149 window->draw_line(black, x, 1, x, h1 - 2);
150 }
151
152 if (region) {
153 int x1 = int(w * (region->KeyRange.low) / 128.0 + 0.5);
154 int x2 = int(w * (region->KeyRange.high + 1) / 128.0 + 0.5);
155 gc->set_foreground(red);
156 window->draw_rectangle(gc, true, x1 + 1, 1, x2 - x1 - 1, h1 - 2);
157 }
158 }
159 return true;
160 }
161
162
163 void RegionChooser::on_size_request(GtkRequisition* requisition)
164 {
165 *requisition = GtkRequisition();
166 requisition->height = 40 + 20;
167 requisition->width = 500;
168 }
169
170
171 // not used
172 void RegionChooser::draw_region(int from, int to, const Gdk::Color& color)
173 {
174 const int h = 40;
175 const int w = width;
176 const int bh = int(h * 0.55);
177
178 Glib::RefPtr<Gdk::Window> window = get_window();
179 gc->set_foreground(color);
180
181 for (int i = from ; i < to ; i++) {
182 int note = (i + 3) % 12;
183 int x = int(w * i / 128.0 + 0.5) + 1;
184 int x2 = int(w * (i + 1.5) / 128.0 + 0.5);
185 int x3 = int(w * (i + 1) / 128.0 + 0.5);
186 int x4 = int(w * (i - 0.5) / 128 + 0.5) + 1;
187 int w1 = x3 - x;
188 switch (note) {
189 case 0: case 5: case 10:
190 window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);
191 window->draw_rectangle(gc, true, x4, h1 + bh + 1, x2 - x4, h - bh - 2);
192 break;
193 case 2: case 7:
194 window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);
195 window->draw_rectangle(gc, true, x4, h1 + bh + 1, x3 - x4, h - bh - 2);
196 break;
197 case 3: case 8:
198 window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);
199 window->draw_rectangle(gc, true, x, h1 + bh + 1, x2 - x, h - bh - 2);
200 break;
201 default:
202 window->draw_rectangle(gc, true, x, h1 + 1, w1, bh - 1);
203 break;
204 }
205 }
206 }
207
208 void RegionChooser::set_instrument(gig::Instrument* instrument)
209 {
210 this->instrument = instrument;
211 region = instrument->GetFirstRegion();
212 queue_draw();
213 sel_changed_signal.emit();
214 }
215
216 void RegionChooser::set_region(gig::Region* region)
217 {
218 this->region = region;
219 queue_draw();
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 }

  ViewVC Help
Powered by ViewVC