/[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 1261 - (hide annotations) (download)
Thu Jul 5 17:12:20 2007 UTC (16 years, 9 months ago) by persson
File size: 17138 byte(s)
* a changed file is now marked with an asterisk in the window title
* added close confirmation dialog, shown if file is changed
* "save" means "save as" for new files
* enabled acceleration keys
* add .gig to filename in "save as" if it's not already there
* filename character encodings other than utf-8 supported

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

  ViewVC Help
Powered by ViewVC