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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1104 - (show annotations) (download)
Sun Mar 18 17:15:00 2007 UTC (17 years, 1 month ago) by persson
File size: 15166 byte(s)
* DimRegionEdit is now disbaled when no region is selected (fixes
  crash for "new file")
* instrument is selected after it is created
* minor code cleanup

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 ? instrument->GetFirstRegion() : 0;
212 queue_draw();
213 sel_changed_signal.emit();
214 }
215
216 bool RegionChooser::on_button_release_event(GdkEventButton* event)
217 {
218 if (resize.active) {
219 get_window()->pointer_ungrab(event->time);
220 resize.active = false;
221
222 if (resize.mode == resize.moving_high_limit) {
223 resize.region->KeyRange.high = resize.pos - 1;
224 } else if (resize.mode == resize.moving_low_limit) {
225 resize.region->KeyRange.low = resize.pos;
226 }
227
228 if (!is_in_resize_zone(event->x, event->y) && cursor_is_resize) {
229 get_window()->set_cursor();
230 cursor_is_resize = false;
231 }
232 }
233 return true;
234 }
235
236 bool RegionChooser::on_button_press_event(GdkEventButton* event)
237 {
238 if (!instrument) return true;
239
240 int k = int(event->x / (width - 1) * 128.0);
241
242 if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
243 gig::Region* r = get_region(k);
244 if (r) {
245 region = r;
246 queue_draw();
247 sel_changed_signal.emit();
248 popup_menu_inside_region->popup(event->button, event->time);
249 } else {
250 new_region_pos = k;
251 popup_menu_outside_region->popup(event->button, event->time);
252 }
253 } else {
254 if (is_in_resize_zone(event->x, event->y)) {
255 Gdk::Cursor double_arrow(Gdk::SB_H_DOUBLE_ARROW);
256 get_window()->pointer_grab(false,
257 Gdk::BUTTON_RELEASE_MASK |
258 Gdk::POINTER_MOTION_MASK |
259 Gdk::POINTER_MOTION_HINT_MASK,
260 double_arrow, event->time);
261 resize.active = true;
262 } else {
263 gig::Region* r = get_region(k);
264 if (r) {
265 region = r;
266 queue_draw();
267 sel_changed_signal.emit();
268 }
269 }
270 }
271 return true;
272 }
273
274 gig::Region* RegionChooser::get_region(int key)
275 {
276 for (gig::Region *r = instrument->GetFirstRegion() ; r ;
277 r = instrument->GetNextRegion()) {
278 if (key < r->KeyRange.low) return 0;
279 if (key <= r->KeyRange.high) return r;
280 }
281 return 0;
282 }
283
284 bool RegionChooser::on_motion_notify_event(GdkEventMotion* event)
285 {
286 const int w = width - 1;
287 Glib::RefPtr<Gdk::Window> window = get_window();
288 int x, y;
289 Gdk::ModifierType state = Gdk::ModifierType(0);
290 window->get_pointer(x, y, state);
291 if (resize.active) {
292 int k = int(double(x) / w * 128.0 + 0.5);
293
294 if (k < resize.min) k = resize.min;
295 else if (k > resize.max) k = resize.max;
296
297 if (k != resize.pos) {
298 if (resize.mode == resize.undecided) {
299 if (k < resize.pos) {
300 // edit high limit of prev_region
301 resize.max = resize.region->KeyRange.low;
302 resize.region = resize.prev_region;
303 resize.mode = resize.moving_high_limit;
304 } else {
305 // edit low limit of region
306 resize.min = resize.prev_region->KeyRange.high + 1;
307 resize.mode = resize.moving_low_limit;
308 }
309 }
310 Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();
311 Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc();
312 if (region == resize.region) {
313 gc->set_foreground(red);
314 white = gc;
315 }
316 Glib::RefPtr<const Gdk::GC> bg = get_style()->get_bg_gc(Gtk::STATE_NORMAL);
317 int prevx = int(w * resize.pos / 128.0 + 0.5);
318 x = int(w * k / 128.0 + 0.5);
319
320 if (resize.mode == resize.moving_high_limit) {
321 if (k > resize.pos) {
322 window->draw_rectangle(white, true, prevx, 1, x - prevx, h1 - 2);
323 window->draw_line(black, prevx, 0, x, 0);
324 window->draw_line(black, prevx, h1 - 1, x, h1 - 1);
325 } else {
326 int xx = ((resize.pos == resize.max && resize.max != 128) ? 1 : 0);
327 window->draw_rectangle(bg, true, x + 1, 0, prevx - x - xx, h1);
328 }
329 } else {
330 if (k < resize.pos) {
331 window->draw_rectangle(white, true, x + 1, 1, prevx - x, h1 - 2);
332 window->draw_line(black, x, 0, prevx, 0);
333 window->draw_line(black, x, h1 - 1, prevx, h1 - 1);
334 } else {
335 int xx = ((resize.pos == resize.min && resize.min != 0) ? 1 : 0);
336 window->draw_rectangle(bg, true, prevx + xx, 0, x - prevx - xx, h1);
337 }
338 }
339 window->draw_line(black, x, 1, x, h1 - 2);
340 resize.pos = k;
341 }
342 } else {
343 if (is_in_resize_zone(x, y)) {
344 if (!cursor_is_resize) {
345 Gdk::Cursor double_arrow(Gdk::SB_H_DOUBLE_ARROW);
346 window->set_cursor(double_arrow);
347 cursor_is_resize = true;
348 }
349 } else if (cursor_is_resize) {
350 window->set_cursor();
351 cursor_is_resize = false;
352 }
353 }
354
355 return true;
356 }
357
358 bool RegionChooser::is_in_resize_zone(double x, double y) {
359 const int w = width - 1;
360
361 if (instrument && y >= 0 && y <= h1) {
362 gig::Region* prev_region = 0;
363 gig::Region* next_region;
364 for (gig::Region* r = instrument->GetFirstRegion() ; r ; r = next_region) {
365 next_region = instrument->GetNextRegion();
366
367 int lo = int(w * (r->KeyRange.low) / 128.0 + 0.5);
368 if (x <= lo - 2) break;
369 if (x < lo + 2) {
370 resize.region = r;
371 resize.pos = r->KeyRange.low;
372 resize.max = r->KeyRange.high;
373
374 if (prev_region && prev_region->KeyRange.high + 1 == r->KeyRange.low) {
375 // we don't know yet if it's the high limit of
376 // prev_region or the low limit of r that's going
377 // to be edited
378 resize.mode = resize.undecided;
379 resize.min = prev_region->KeyRange.low + 1;
380 resize.prev_region = prev_region;
381 return true;
382 }
383
384 // edit low limit
385 resize.mode = resize.moving_low_limit;
386 resize.min = prev_region ? prev_region->KeyRange.high + 1 : 0;
387 return true;
388 }
389 if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) {
390 int hi = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5);
391 if (x <= hi - 2) break;
392 if (x < hi + 2) {
393 // edit high limit
394 resize.region = r;
395 resize.pos = r->KeyRange.high + 1;
396 resize.mode = resize.moving_high_limit;
397 resize.min = r->KeyRange.low + 1;
398 resize.max = next_region ? next_region->KeyRange.low : 128;
399 return true;
400 }
401 }
402 prev_region = r;
403 }
404 }
405 return false;
406 }
407
408 sigc::signal<void> RegionChooser::signal_sel_changed()
409 {
410 return sel_changed_signal;
411 }
412
413 void RegionChooser::show_region_properties()
414 {
415 }
416
417 void RegionChooser::add_region()
418 {
419 gig::Region* r;
420 for (r = instrument->GetFirstRegion() ; r ; r = instrument->GetNextRegion()) {
421 if (r->KeyRange.low > new_region_pos) break;
422 }
423
424 region = instrument->AddRegion();
425 region->KeyRange.low = region->KeyRange.high = new_region_pos;
426
427 instrument->MoveRegion(region, r);
428 queue_draw();
429 sel_changed_signal.emit();
430 }
431
432 void RegionChooser::delete_region()
433 {
434 instrument->DeleteRegion(region);
435 region = 0;
436 queue_draw();
437 sel_changed_signal.emit();
438 }

  ViewVC Help
Powered by ViewVC