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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1261 by persson, Thu Jul 5 17:12:20 2007 UTC revision 1831 by persson, Tue Feb 3 19:38:19 2009 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (C) 2006, 2007 Andreas Persson   * Copyright (C) 2006-2009 Andreas Persson
3   *   *
4   * This program is free software; you can redistribute it and/or   * This program is free software; you can redistribute it and/or
5   * modify it under the terms of the GNU General Public License as   * modify it under the terms of the GNU General Public License as
# Line 18  Line 18 
18   */   */
19    
20  #include "regionchooser.h"  #include "regionchooser.h"
21    #include <algorithm>
22  #include <gdkmm/cursor.h>  #include <gdkmm/cursor.h>
23  #include <gtkmm/stock.h>  #include <gtkmm/stock.h>
24  #include <gtkmm/spinbutton.h>  #include <gtkmm/spinbutton.h>
25  #include <gtkmm/dialog.h>  #include <gtkmm/dialog.h>
 #include <libintl.h>  
26  #include <math.h>  #include <math.h>
27    #include <sstream>
28    
29  #define _(String) gettext(String)  #include "global.h"
30    
31  RegionChooser::RegionChooser()  #define REGION_BLOCK_HEIGHT             20
32    #define KEYBOARD_HEIGHT                 40
33    
34    void SortedRegions::update(gig::Instrument* instrument) {
35        // Usually, the regions in a gig file are ordered after their key
36        // range, but there are files where they are not. The
37        // RegionChooser code needs a sorted list of regions.
38        regions.clear();
39        if (instrument) {
40            for (gig::Region *r = instrument->GetFirstRegion() ;
41                 r ;
42                 r = instrument->GetNextRegion()) {
43                regions.push_back(r);
44            }
45            sort(regions.begin(), regions.end(), *this);
46        }
47    }
48    
49    gig::Region* SortedRegions::first() {
50        region_iterator = regions.begin();
51        return region_iterator == regions.end() ? 0 : *region_iterator;
52    }
53    
54    gig::Region* SortedRegions::next() {
55        region_iterator++;
56        return region_iterator == regions.end() ? 0 : *region_iterator;
57    }
58    
59    
60    
61    RegionChooser::RegionChooser() :
62        m_VirtKeybModeChoice(_("Virtual Keyboard Mode")),
63        currentActiveKey(-1)
64  {  {
65      Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap();      Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap();
66    
     black = Gdk::Color("black");  
     white = Gdk::Color("white");  
67      red = Gdk::Color("#8070ff");      red = Gdk::Color("#8070ff");
68      blue = Gdk::Color("#c098ff");      grey1 = Gdk::Color("#b0b0b0");
69      green = Gdk::Color("#a088ff");      activeKeyColor = Gdk::Color("#ff0000");
70      grey1 = Gdk::Color("red");      white = Gdk::Color("#ffffff");
71        black = Gdk::Color("#000000");
72    
     colormap->alloc_color(black);  
     colormap->alloc_color(white);  
73      colormap->alloc_color(red);      colormap->alloc_color(red);
     colormap->alloc_color(blue);  
     colormap->alloc_color(green);  
74      colormap->alloc_color(grey1);      colormap->alloc_color(grey1);
75        colormap->alloc_color(activeKeyColor);
76        colormap->alloc_color(white);
77        colormap->alloc_color(black);
78      instrument = 0;      instrument = 0;
79      region = 0;      region = 0;
80      resize.active = false;      resize.active = false;
81        move.active = false;
82      cursor_is_resize = false;      cursor_is_resize = false;
83      h1 = 20;      h1 = REGION_BLOCK_HEIGHT;
84      width = 800;  
85        // properties of the virtual keyboard
86        {
87            const char* choices[] = { _("normal"), _("chord"), NULL };
88            static const virt_keyboard_mode_t values[] = {
89                VIRT_KEYBOARD_MODE_NORMAL,
90                VIRT_KEYBOARD_MODE_CHORD
91            };
92            m_VirtKeybModeChoice.set_choices(choices, values);
93            m_VirtKeybModeChoice.set_value(VIRT_KEYBOARD_MODE_NORMAL);
94        }
95        m_VirtKeybVelocityLabelDescr.set_text(_("Note-On Velocity:"));
96        m_VirtKeybVelocityLabel.set_text("-");
97        m_VirtKeybOffVelocityLabelDescr.set_text(_("Note-Off Velocity:"));
98        m_VirtKeybOffVelocityLabel.set_text("-");
99        m_VirtKeybPropsBox.pack_start(m_VirtKeybModeChoice.label, Gtk::PACK_SHRINK);
100        m_VirtKeybPropsBox.pack_start(m_VirtKeybModeChoice.widget, Gtk::PACK_SHRINK);
101        m_VirtKeybPropsBox.pack_start(m_VirtKeybVelocityLabelDescr, Gtk::PACK_SHRINK);
102        m_VirtKeybPropsBox.pack_start(m_VirtKeybVelocityLabel, Gtk::PACK_SHRINK);
103        m_VirtKeybPropsBox.pack_start(m_VirtKeybOffVelocityLabelDescr, Gtk::PACK_SHRINK);
104        m_VirtKeybPropsBox.pack_start(m_VirtKeybOffVelocityLabel, Gtk::PACK_SHRINK);
105        m_VirtKeybPropsBox.set_spacing(10);
106        m_VirtKeybPropsBox.show();
107    
108      actionGroup = Gtk::ActionGroup::create();      actionGroup = Gtk::ActionGroup::create();
109      actionGroup->add(Gtk::Action::create("Properties",      actionGroup->add(Gtk::Action::create("Properties",
# Line 86  RegionChooser::RegionChooser() Line 140  RegionChooser::RegionChooser()
140      add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK |      add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK |
141                 Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK);                 Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK);
142    
143      dimensionManager.articulation_changed_signal.connect(      dimensionManager.region_to_be_changed_signal.connect(
144          sigc::mem_fun(*this, &RegionChooser::on_dimension_manager_changed)          region_to_be_changed_signal.make_slot()
145        );
146        dimensionManager.region_changed_signal.connect(
147            region_changed_signal.make_slot()
148        );
149        dimensionManager.region_changed_signal.connect(
150            sigc::hide(
151                sigc::mem_fun(*this, &RegionChooser::on_dimension_manager_changed)
152            )
153        );
154        keyboard_key_hit_signal.connect(
155            sigc::mem_fun(*this, &RegionChooser::on_note_on_event)
156        );
157        keyboard_key_released_signal.connect(
158            sigc::mem_fun(*this, &RegionChooser::on_note_off_event)
159      );      );
160  }  }
161    
# Line 95  RegionChooser::~RegionChooser() Line 163  RegionChooser::~RegionChooser()
163  {  {
164  }  }
165    
166    template<class T> inline std::string ToString(T o) {
167        std::stringstream ss;
168        ss << o;
169        return ss.str();
170    }
171    
172    void RegionChooser::on_note_on_event(int key, int velocity) {
173        draw_region(key, key+1, activeKeyColor);
174        m_VirtKeybVelocityLabel.set_text(ToString(velocity));
175    }
176    
177    void RegionChooser::on_note_off_event(int key, int velocity) {
178        if (is_black_key(key))
179            draw_region(key, key+1, black);
180        else
181            draw_region(key, key+1, white);
182        m_VirtKeybOffVelocityLabel.set_text(ToString(velocity));
183    }
184    
185  void RegionChooser::on_realize()  void RegionChooser::on_realize()
186  {  {
187      // We need to call the base on_realize()      // We need to call the base on_realize()
# Line 110  bool RegionChooser::on_expose_event(GdkE Line 197  bool RegionChooser::on_expose_event(GdkE
197  {  {
198      Glib::RefPtr<Gdk::Window> window = get_window();      Glib::RefPtr<Gdk::Window> window = get_window();
199      window->clear();      window->clear();
200      const int h = 40;      const int h = KEYBOARD_HEIGHT;
201      const int w = width - 1;      const int w = get_width() - 1;
202      const int bh = int(h * 0.55);      const int bh = int(h * 0.55);
203    
204      Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();      Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();
205      Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc();      Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc();
206    
207      window->draw_rectangle(black, false, 0, h1, w, h - 1);      window->draw_rectangle(black, false, 0, h1, w, h - 1);
208      window->draw_rectangle(white, true, 1, h1 + 1, w - 1, h - 2);      gc->set_foreground(grey1);
209        int x1 = int(w * 20.5 / 128.0 + 0.5);
210        int x2 = int(w * 109.5 / 128.0 + 0.5);
211        window->draw_rectangle(gc, true, 1, h1 + 1,
212                               x1 - 1, h - 2);
213        window->draw_rectangle(white, true, x1 + 1, h1 + 1, x2 - x1 - 1, h - 2);
214        window->draw_rectangle(gc, true, x2 + 1, h1 + 1,
215                               w - x2 - 1, h - 2);
216      for (int i = 0 ; i < 128 ; i++) {      for (int i = 0 ; i < 128 ; i++) {
217          int note = (i + 3) % 12;          int note = (i + 3) % 12;
218          int x = int(w * i / 128.0 + 0.5);          int x = int(w * i / 128.0 + 0.5);
# Line 132  bool RegionChooser::on_expose_event(GdkE Line 226  bool RegionChooser::on_expose_event(GdkE
226          } else if (note == 3 || note == 8) {          } else if (note == 3 || note == 8) {
227              window->draw_line(black, x, h1 + 1, x, h1 + h);              window->draw_line(black, x, h1 + 1, x, h1 + h);
228          }          }
229            if (note == 3) draw_digit(i);
230      }      }
231    
232      if (instrument) {      if (instrument) {
233          int i = 0;          int i = 0;
234          gig::Region *nextRegion;          gig::Region *next_region;
235          int x3 = -1;          int x3 = -1;
236          for (gig::Region *r = instrument->GetFirstRegion() ;          for (gig::Region *r = regions.first() ; r ; r = next_region) {
              r ;  
              r = nextRegion) {  
237    
238              if (x3 < 0) x3 = int(w * (r->KeyRange.low) / 128.0 + 0.5);              if (x3 < 0) x3 = int(w * (r->KeyRange.low) / 128.0 + 0.5);
239              nextRegion = instrument->GetNextRegion();              next_region = regions.next();
240              if (!nextRegion || r->KeyRange.high + 1 != nextRegion->KeyRange.low) {              if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) {
241                  int x2 = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5);                  int x2 = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5);
242                  window->draw_line(black, x3, 0, x2, 0);                  window->draw_line(black, x3, 0, x2, 0);
243                  window->draw_line(black, x3, h1 - 1, x2, h1 - 1);                  window->draw_line(black, x3, h1 - 1, x2, h1 - 1);
# Line 155  bool RegionChooser::on_expose_event(GdkE Line 248  bool RegionChooser::on_expose_event(GdkE
248              i++;              i++;
249          }          }
250    
251          for (gig::Region *r = instrument->GetFirstRegion() ;          for (gig::Region *r = regions.first() ; r ; r = regions.next()) {
              r ;  
              r = instrument->GetNextRegion()) {  
252              int x = int(w * (r->KeyRange.low) / 128.0 + 0.5);              int x = int(w * (r->KeyRange.low) / 128.0 + 0.5);
253              window->draw_line(black, x, 1, x, h1 - 2);              window->draw_line(black, x, 1, x, h1 - 2);
254          }          }
# Line 176  bool RegionChooser::on_expose_event(GdkE Line 267  bool RegionChooser::on_expose_event(GdkE
267  void RegionChooser::on_size_request(GtkRequisition* requisition)  void RegionChooser::on_size_request(GtkRequisition* requisition)
268  {  {
269      *requisition = GtkRequisition();      *requisition = GtkRequisition();
270      requisition->height = 40 + 20;      requisition->height = KEYBOARD_HEIGHT + REGION_BLOCK_HEIGHT;
271      requisition->width = 500;      requisition->width = 500;
272  }  }
273    
274    bool RegionChooser::is_black_key(int key) {
275        const int note = (key + 3) % 12;
276        return note == 1 || note == 4 || note == 6 || note == 9 || note == 11;
277    }
278    
279    void RegionChooser::draw_digit(int key) {
280        const int h = KEYBOARD_HEIGHT;
281        const int w = get_width() - 1;
282        Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create(get_pango_context());
283        char buf[30];
284        sprintf(buf, "<span size=\"8000\">%d</span>", key / 12 - 1);
285        layout->set_markup(buf);
286        Pango::Rectangle rectangle = layout->get_logical_extents();
287        double text_w = double(rectangle.get_width()) / Pango::SCALE;
288        double text_h = double(rectangle.get_height()) / Pango::SCALE;
289        double x = w * (key + 0.75) / 128.0;
290        get_window()->draw_layout(get_style()->get_black_gc(), int(x - text_w / 2 + 1),
291                                  int(h1 + h - text_h + 0.5), layout);
292    }
293    
 // not used  
294  void RegionChooser::draw_region(int from, int to, const Gdk::Color& color)  void RegionChooser::draw_region(int from, int to, const Gdk::Color& color)
295  {  {
296      const int h = 40;      const int h = KEYBOARD_HEIGHT;
297      const int w = width;      const int w = get_width() - 1;
298      const int bh = int(h * 0.55);      const int bh = int(h * 0.55);
299    
300      Glib::RefPtr<Gdk::Window> window = get_window();      Glib::RefPtr<Gdk::Window> window = get_window();
# Line 196  void RegionChooser::draw_region(int from Line 305  void RegionChooser::draw_region(int from
305          int x = int(w * i / 128.0 + 0.5) + 1;          int x = int(w * i / 128.0 + 0.5) + 1;
306          int x2 = int(w * (i + 1.5) / 128.0 + 0.5);          int x2 = int(w * (i + 1.5) / 128.0 + 0.5);
307          int x3 = int(w * (i + 1) / 128.0 + 0.5);          int x3 = int(w * (i + 1) / 128.0 + 0.5);
308          int x4 = int(w * (i - 0.5) / 128 + 0.5) + 1;          int x4 = int(w * (i - 0.5) / 128.0 + 0.5);
309          int w1 = x3 - x;          int w1 = x3 - x;
310          switch (note) {          switch (note) {
311          case 0: case 5: case 10:          case 0: case 5: case 10:
312              window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);              window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);
313              window->draw_rectangle(gc, true, x4, h1 + bh + 1, x2 - x4, h - bh - 2);              window->draw_rectangle(gc, true, x4 + 1, h1 + bh + 1, x2 - x4 - 1, h - bh - 2);
314              break;              break;
315          case 2: case 7:          case 2: case 7:
316              window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);              window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);
317              window->draw_rectangle(gc, true, x4, h1 + bh + 1, x3 - x4, h - bh - 2);              window->draw_rectangle(gc, true, x4 + 1, h1 + bh + 1, x3 - x4 - 1, h - bh - 2);
318              break;              break;
319          case 3: case 8:          case 3: case 8:
320              window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);              window->draw_rectangle(gc, true, x, h1 + 1, w1, bh);
321              window->draw_rectangle(gc, true, x, h1 + bh + 1, x2 - x, h - bh - 2);              window->draw_rectangle(gc, true, x, h1 + bh + 1, x2 - x, h - bh - 2);
322                if (note == 3) draw_digit(i);
323              break;              break;
324          default:          default:
325              window->draw_rectangle(gc, true, x, h1 + 1, w1, bh - 1);              window->draw_rectangle(gc, true, x, h1 + 1, w1, bh - 1);
# Line 221  void RegionChooser::draw_region(int from Line 331  void RegionChooser::draw_region(int from
331  void RegionChooser::set_instrument(gig::Instrument* instrument)  void RegionChooser::set_instrument(gig::Instrument* instrument)
332  {  {
333      this->instrument = instrument;      this->instrument = instrument;
334      region = instrument ? instrument->GetFirstRegion() : 0;      regions.update(instrument);
335        region = regions.first();
336      queue_draw();      queue_draw();
337      region_selected();      region_selected();
338        dimensionManager.set_region(region);
339  }  }
340    
341  bool RegionChooser::on_button_release_event(GdkEventButton* event)  bool RegionChooser::on_button_release_event(GdkEventButton* event)
342  {  {
343        const int k = int(event->x / (get_width() - 1) * 128.0);
344    
345        // handle-note off on virtual keyboard
346        if (event->type == GDK_BUTTON_RELEASE) {
347            int velocity = (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 :
348                           int(float(event->y - REGION_BLOCK_HEIGHT) / float(KEYBOARD_HEIGHT) * 128.0f) + 1;
349            if (velocity <= 0) velocity = 1;
350            switch (m_VirtKeybModeChoice.get_value()) {
351                case VIRT_KEYBOARD_MODE_CHORD:
352                    if (event->y >= REGION_BLOCK_HEIGHT)
353                        keyboard_key_released_signal.emit(k, velocity);
354                    break;
355                case VIRT_KEYBOARD_MODE_NORMAL:
356                default:
357                    if (currentActiveKey >= 0 && currentActiveKey <= 127) {
358                        keyboard_key_released_signal.emit(currentActiveKey, velocity);
359                        currentActiveKey = -1;
360                    }
361                    break;
362            }
363        }
364    
365      if (resize.active) {      if (resize.active) {
366          get_window()->pointer_ungrab(event->time);          get_window()->pointer_ungrab(event->time);
367          resize.active = false;          resize.active = false;
368    
369          if (resize.mode == resize.moving_high_limit) {          if (resize.mode == resize.moving_high_limit) {
370              if (resize.region->KeyRange.high != resize.pos - 1) {              if (resize.region->KeyRange.high != resize.pos - 1) {
371                  resize.region->KeyRange.high = resize.pos - 1;                  instrument_struct_to_be_changed_signal.emit(instrument);
372                  instrument_changed();                  resize.region->SetKeyRange(
373                        resize.region->KeyRange.low, // low
374                        resize.pos - 1 // high
375                    );
376                    regions.update(instrument);
377                    instrument_changed.emit();
378                    instrument_struct_changed_signal.emit(instrument);
379              }              }
380          } else if (resize.mode == resize.moving_low_limit) {          } else if (resize.mode == resize.moving_low_limit) {
381              if (resize.region->KeyRange.low != resize.pos) {              if (resize.region->KeyRange.low != resize.pos) {
382                  resize.region->KeyRange.low = resize.pos;                  instrument_struct_to_be_changed_signal.emit(instrument);
383                  instrument_changed();                  resize.region->SetKeyRange(
384                        resize.pos, // low
385                        resize.region->KeyRange.high // high
386                    );
387                    regions.update(instrument);
388                    instrument_changed.emit();
389                    instrument_struct_changed_signal.emit(instrument);
390              }              }
391          }          }
392    
# Line 248  bool RegionChooser::on_button_release_ev Line 394  bool RegionChooser::on_button_release_ev
394              get_window()->set_cursor();              get_window()->set_cursor();
395              cursor_is_resize = false;              cursor_is_resize = false;
396          }          }
397        } else if (move.active) {
398            get_window()->pointer_ungrab(event->time);
399            move.active = false;
400    
401            if (move.pos) {
402                instrument_struct_to_be_changed_signal.emit(instrument);
403                region->SetKeyRange(
404                    region->KeyRange.low  + move.pos,
405                    region->KeyRange.high + move.pos
406                );
407                regions.update(instrument);
408                instrument_changed.emit();
409                instrument_struct_changed_signal.emit(instrument);
410            }
411    
412            if (is_in_resize_zone(event->x, event->y)) {
413                get_window()->set_cursor(Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW));
414                cursor_is_resize = true;
415            }
416      }      }
417      return true;      return true;
418  }  }
# Line 256  bool RegionChooser::on_button_press_even Line 421  bool RegionChooser::on_button_press_even
421  {  {
422      if (!instrument) return true;      if (!instrument) return true;
423    
424      int k = int(event->x / (width - 1) * 128.0);      const int k = int(event->x / (get_width() - 1) * 128.0);
425    
426        if (event->type == GDK_BUTTON_PRESS) {
427            if (event->y >= REGION_BLOCK_HEIGHT) {
428                int velocity = (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 :
429                               int(float(event->y - REGION_BLOCK_HEIGHT) / float(KEYBOARD_HEIGHT) * 128.0f) + 1;
430                currentActiveKey = k;
431                keyboard_key_hit_signal.emit(k, velocity);
432            }
433        }
434    
435        if (event->y >= REGION_BLOCK_HEIGHT) return true;
436      if (event->type == GDK_BUTTON_PRESS && event->button == 3) {      if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
437          gig::Region* r = get_region(k);          gig::Region* r = get_region(k);
438          if (r) {          if (r) {
439              region = r;              region = r;
440              queue_draw();              queue_draw();
441              region_selected();              region_selected();
442                dimensionManager.set_region(region);
443              popup_menu_inside_region->popup(event->button, event->time);              popup_menu_inside_region->popup(event->button, event->time);
444          } else {          } else {
445              new_region_pos = k;              new_region_pos = k;
# Line 271  bool RegionChooser::on_button_press_even Line 447  bool RegionChooser::on_button_press_even
447          }          }
448      } else {      } else {
449          if (is_in_resize_zone(event->x, event->y)) {          if (is_in_resize_zone(event->x, event->y)) {
             Gdk::Cursor double_arrow(Gdk::SB_H_DOUBLE_ARROW);  
450              get_window()->pointer_grab(false,              get_window()->pointer_grab(false,
451                                         Gdk::BUTTON_RELEASE_MASK |                                         Gdk::BUTTON_RELEASE_MASK |
452                                         Gdk::POINTER_MOTION_MASK |                                         Gdk::POINTER_MOTION_MASK |
453                                         Gdk::POINTER_MOTION_HINT_MASK,                                         Gdk::POINTER_MOTION_HINT_MASK,
454                                         double_arrow, event->time);                                         Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW), event->time);
455              resize.active = true;              resize.active = true;
456          } else {          } else {
457              gig::Region* r = get_region(k);              gig::Region* r = get_region(k);
# Line 284  bool RegionChooser::on_button_press_even Line 459  bool RegionChooser::on_button_press_even
459                  region = r;                  region = r;
460                  queue_draw();                  queue_draw();
461                  region_selected();                  region_selected();
462                    dimensionManager.set_region(region);
463    
464                    get_window()->pointer_grab(false,
465                                               Gdk::BUTTON_RELEASE_MASK |
466                                               Gdk::POINTER_MOTION_MASK |
467                                               Gdk::POINTER_MOTION_HINT_MASK,
468                                               Gdk::Cursor(Gdk::FLEUR), event->time);
469                    move.active = true;
470                    move.from_x = event->x;
471                    move.pos = 0;
472              }              }
473          }          }
474      }      }
# Line 292  bool RegionChooser::on_button_press_even Line 477  bool RegionChooser::on_button_press_even
477    
478  gig::Region* RegionChooser::get_region(int key)  gig::Region* RegionChooser::get_region(int key)
479  {  {
480      for (gig::Region *r = instrument->GetFirstRegion() ; r ;      gig::Region* prev_region = 0;
481           r = instrument->GetNextRegion()) {      gig::Region* next_region;
482        for (gig::Region *r = regions.first() ; r ; r = next_region) {
483            next_region = regions.next();
484    
485          if (key < r->KeyRange.low) return 0;          if (key < r->KeyRange.low) return 0;
486          if (key <= r->KeyRange.high) return r;          if (key <= r->KeyRange.high) {
487                move.touch_left = prev_region && prev_region->KeyRange.high + 1 == r->KeyRange.low;
488                move.touch_right = next_region && r->KeyRange.high + 1 == next_region->KeyRange.low;
489                return r;
490            }
491            prev_region = r;
492      }      }
493      return 0;      return 0;
494  }  }
495    
496  bool RegionChooser::on_motion_notify_event(GdkEventMotion* event)  void RegionChooser::motion_resize_region(int x, int y)
497  {  {
498      const int w = width - 1;      const int w = get_width() - 1;
499      Glib::RefPtr<Gdk::Window> window = get_window();      Glib::RefPtr<Gdk::Window> window = get_window();
     int x, y;  
     Gdk::ModifierType state = Gdk::ModifierType(0);  
     window->get_pointer(x, y, state);  
     if (resize.active) {  
         int k = int(double(x) / w * 128.0 + 0.5);  
500    
501          if (k < resize.min) k = resize.min;      int k = int(double(x) / w * 128.0 + 0.5);
         else if (k > resize.max) k = resize.max;  
502    
503          if (k != resize.pos) {      if (k < resize.min) k = resize.min;
504              if (resize.mode == resize.undecided) {      else if (k > resize.max) k = resize.max;
505                  if (k < resize.pos) {  
506                      // edit high limit of prev_region      if (k != resize.pos) {
507                      resize.max = resize.region->KeyRange.low;          if (resize.mode == resize.undecided) {
508                      resize.region = resize.prev_region;              if (k < resize.pos) {
509                      resize.mode = resize.moving_high_limit;                  // edit high limit of prev_region
510                  } else {                  resize.max = resize.region->KeyRange.low;
511                      // edit low limit of region                  resize.region = resize.prev_region;
512                      resize.min = resize.prev_region->KeyRange.high + 1;                  resize.mode = resize.moving_high_limit;
513                      resize.mode = resize.moving_low_limit;              } else {
514                  }                  // edit low limit of region
515                    resize.min = resize.prev_region->KeyRange.high + 1;
516                    resize.mode = resize.moving_low_limit;
517                }
518            }
519            Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();
520            Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc();
521            if (region == resize.region) {
522                gc->set_foreground(red);
523                white = gc;
524            }
525            Glib::RefPtr<const Gdk::GC> bg = get_style()->get_bg_gc(Gtk::STATE_NORMAL);
526            int prevx = int(w * resize.pos / 128.0 + 0.5);
527            x = int(w * k / 128.0 + 0.5);
528    
529            if (resize.mode == resize.moving_high_limit) {
530                if (k > resize.pos) {
531                    window->draw_rectangle(white, true, prevx, 1, x - prevx, h1 - 2);
532                    window->draw_line(black, prevx, 0, x, 0);
533                    window->draw_line(black, prevx, h1 - 1, x, h1 - 1);
534                } else {
535                    int xx = ((resize.pos == resize.max && resize.max != 128) ? 1 : 0);
536                    window->draw_rectangle(bg, true, x + 1, 0, prevx - x - xx, h1);
537                }
538            } else {
539                if (k < resize.pos) {
540                    window->draw_rectangle(white, true, x + 1, 1, prevx - x, h1 - 2);
541                    window->draw_line(black, x, 0, prevx, 0);
542                    window->draw_line(black, x, h1 - 1, prevx, h1 - 1);
543                } else {
544                    int xx = ((resize.pos == resize.min && resize.min != 0) ? 1 : 0);
545                    window->draw_rectangle(bg, true, prevx + xx, 0, x - prevx - xx, h1);
546              }              }
547              Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();          }
548              Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc();          window->draw_line(black, x, 1, x, h1 - 2);
549              if (region == resize.region) {          resize.pos = k;
550                  gc->set_foreground(red);      }
551                  white = gc;  }
552              }  
553              Glib::RefPtr<const Gdk::GC> bg = get_style()->get_bg_gc(Gtk::STATE_NORMAL);  void RegionChooser::motion_move_region(int x, int y)
554              int prevx = int(w * resize.pos / 128.0 + 0.5);  {
555              x = int(w * k / 128.0 + 0.5);      const int w = get_width() - 1;
556        Glib::RefPtr<Gdk::Window> window = get_window();
557              if (resize.mode == resize.moving_high_limit) {  
558                  if (k > resize.pos) {      int k = int(double(x - move.from_x) / w * 128.0 + 0.5);
559                      window->draw_rectangle(white, true, prevx, 1, x - prevx, h1 - 2);      if (k == move.pos) return;
560                      window->draw_line(black, prevx, 0, x, 0);      int new_k;
561                      window->draw_line(black, prevx, h1 - 1, x, h1 - 1);      bool new_touch_left;
562        bool new_touch_right;
563        int a = 0;
564        if (k > move.pos) {
565            for (gig::Region* r = regions.first() ; ; r = regions.next()) {
566                if (r != region) {
567                    int b = r ? r->KeyRange.low : 128;
568    
569                    // gap: from a to b (not inclusive b)
570    
571                    if (region->KeyRange.high + move.pos >= b) {
572                        // not found the current gap yet, just continue
573                  } else {                  } else {
574                      int xx = ((resize.pos == resize.max && resize.max != 128) ? 1 : 0);  
575                      window->draw_rectangle(bg, true, x + 1, 0, prevx - x - xx, h1);                      if (a > region->KeyRange.low + k) {
576                            // this gap is too far to the right, break
577                            break;
578                        }
579    
580                        int newhigh = std::min(region->KeyRange.high + k, b - 1);
581                        int newlo = newhigh - (region->KeyRange.high - region->KeyRange.low);
582    
583                        if (newlo >= a) {
584                            // yes it fits - it's a candidate
585                            new_k = newlo - region->KeyRange.low;
586                            new_touch_left = a > 0 && a == newlo;
587                            new_touch_right = b < 128 && newhigh + 1 == b;
588                        }
589                  }                  }
590              } else {                  if (!r) break;
591                  if (k < resize.pos) {                  a = r->KeyRange.high + 1;
592                      window->draw_rectangle(white, true, x + 1, 1, prevx - x, h1 - 2);              }
593                      window->draw_line(black, x, 0, prevx, 0);          }
594                      window->draw_line(black, x, h1 - 1, prevx, h1 - 1);      } else {
595            for (gig::Region* r = regions.first() ; ; r = regions.next()) {
596                if (r != region) {
597                    int b = r ? r->KeyRange.low : 128;
598    
599                    // gap from a to b (not inclusive b)
600    
601                    if (region->KeyRange.high + k >= b) {
602                        // not found the current gap yet, just continue
603                  } else {                  } else {
604                      int xx = ((resize.pos == resize.min && resize.min != 0) ? 1 : 0);  
605                      window->draw_rectangle(bg, true, prevx + xx, 0, x - prevx - xx, h1);                      if (a > region->KeyRange.low + move.pos) {
606                            // this gap is too far to the right, break
607                            break;
608                        }
609    
610                        int newlo = std::max(region->KeyRange.low + k, a);
611                        int newhigh = newlo + (region->KeyRange.high - region->KeyRange.low);
612    
613                        if (newhigh < b) {
614                            // yes it fits - break as the first one is the best
615                            new_k = newlo - region->KeyRange.low;
616                            new_touch_left = a > 0 && a == newlo;
617                            new_touch_right = b < 128 && newhigh + 1 == b;
618                            break;
619                        }
620                  }                  }
621                    if (!r) break;
622                    a = r->KeyRange.high + 1;
623              }              }
             window->draw_line(black, x, 1, x, h1 - 2);  
             resize.pos = k;  
624          }          }
625        }
626        k = new_k;
627        if (k == move.pos) return;
628    
629        Glib::RefPtr<const Gdk::GC> bg = get_style()->get_bg_gc(Gtk::STATE_NORMAL);
630        int prevx = int(w * (move.pos + region->KeyRange.low) / 128.0 + 0.5);
631        x = int(w * (k + region->KeyRange.low) / 128.0 + 0.5);
632        int prevx2 = int(w * (move.pos + region->KeyRange.high + 1) / 128.0 + 0.5);
633        int x2 = int(w * (k + region->KeyRange.high + 1) / 128.0 + 0.5);
634        Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();
635        gc->set_foreground(red);
636    
637        if (!new_touch_left) window->draw_line(black, x, 1, x, h1 - 2);
638        if (!new_touch_right) window->draw_line(black, x2, 1, x2, h1 - 2);
639    
640        if (k > move.pos) {
641            window->draw_rectangle(bg, true, prevx + (move.touch_left ? 1 : 0), 0,
642                                   std::min(x, prevx2 + 1 - (move.touch_right ? 1 : 0)) -
643                                   (prevx + (move.touch_left ? 1 : 0)), h1);
644    
645            window->draw_line(black, std::max(x, prevx2 + 1), 0, x2, 0);
646            window->draw_line(black, std::max(x, prevx2 + 1), h1 - 1, x2, h1 - 1);
647            window->draw_rectangle(gc, true, std::max(x + 1, prevx2), 1,
648                                   x2 - std::max(x + 1, prevx2), h1 - 2);
649        } else {
650            window->draw_rectangle(bg, true, std::max(x2 + 1, prevx + (move.touch_left ? 1 : 0)), 0,
651                                   prevx2 + 1 - (move.touch_right ? 1 : 0) -
652                                   std::max(x2 + 1, prevx + (move.touch_left ? 1 : 0)), h1);
653    
654            window->draw_line(black, x, 0, std::min(x2, prevx - 1), 0);
655            window->draw_line(black, x, h1 - 1, std::min(x2, prevx - 1), h1 - 1);
656    
657            window->draw_rectangle(gc, true, x + 1, 1, std::min(x2 - 1, prevx) - x, h1 - 2);
658        }
659    
660        move.pos = k;
661        move.touch_left = new_touch_left;
662        move.touch_right = new_touch_right;
663    }
664    
665    
666    bool RegionChooser::on_motion_notify_event(GdkEventMotion* event)
667    {
668        Glib::RefPtr<Gdk::Window> window = get_window();
669        int x, y;
670        Gdk::ModifierType state = Gdk::ModifierType(0);
671        window->get_pointer(x, y, state);
672    
673        // handle virtual MIDI keyboard
674        if (m_VirtKeybModeChoice.get_value() != VIRT_KEYBOARD_MODE_CHORD &&
675            currentActiveKey > 0 &&
676            event->y >= REGION_BLOCK_HEIGHT &&
677            event->y < REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT)
678        {
679            const int k = int(event->x / (get_width() - 1) * 128.0);
680            int velocity = (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 :
681                           int(float(event->y - REGION_BLOCK_HEIGHT) / float(KEYBOARD_HEIGHT) * 128.0f) + 1;
682            if (velocity <= 0) velocity = 1;
683            keyboard_key_released_signal.emit(currentActiveKey, velocity);
684            currentActiveKey = k;
685            keyboard_key_hit_signal.emit(k, velocity);
686        }
687    
688        if (resize.active) {
689            motion_resize_region(x, y);
690        } else if (move.active) {
691            motion_move_region(x, y);
692      } else {      } else {
693          if (is_in_resize_zone(x, y)) {          if (is_in_resize_zone(x, y)) {
694              if (!cursor_is_resize) {              if (!cursor_is_resize) {
695                  Gdk::Cursor double_arrow(Gdk::SB_H_DOUBLE_ARROW);                  window->set_cursor(Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW));
                 window->set_cursor(double_arrow);  
696                  cursor_is_resize = true;                  cursor_is_resize = true;
697              }              }
698          } else if (cursor_is_resize) {          } else if (cursor_is_resize) {
# Line 375  bool RegionChooser::on_motion_notify_eve Line 705  bool RegionChooser::on_motion_notify_eve
705  }  }
706    
707  bool RegionChooser::is_in_resize_zone(double x, double y) {  bool RegionChooser::is_in_resize_zone(double x, double y) {
708      const int w = width - 1;      const int w = get_width() - 1;
709    
710      if (instrument && y >= 0 && y <= h1) {      if (instrument && y >= 0 && y <= h1) {
711          gig::Region* prev_region = 0;          gig::Region* prev_region = 0;
712          gig::Region* next_region;          gig::Region* next_region;
713          for (gig::Region* r = instrument->GetFirstRegion() ; r ; r = next_region) {          for (gig::Region* r = regions.first(); r ; r = next_region) {
714              next_region = instrument->GetNextRegion();              next_region = regions.next();
715    
716              int lo = int(w * (r->KeyRange.low) / 128.0 + 0.5);              int lo = int(w * (r->KeyRange.low) / 128.0 + 0.5);
717              if (x <= lo - 2) break;              if (x <= lo - 2) break;
# Line 397  bool RegionChooser::is_in_resize_zone(do Line 727  bool RegionChooser::is_in_resize_zone(do
727                      resize.mode = resize.undecided;                      resize.mode = resize.undecided;
728                      resize.min = prev_region->KeyRange.low + 1;                      resize.min = prev_region->KeyRange.low + 1;
729                      resize.prev_region = prev_region;                      resize.prev_region = prev_region;
730                      return true;                      return resize.min != resize.max;
731                  }                  }
732    
733                  // edit low limit                  // edit low limit
734                  resize.mode = resize.moving_low_limit;                  resize.mode = resize.moving_low_limit;
735                  resize.min = prev_region ? prev_region->KeyRange.high + 1 : 0;                  resize.min = prev_region ? prev_region->KeyRange.high + 1 : 0;
736                  return true;                  return resize.min != resize.max;
737              }              }
738              if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) {              if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) {
739                  int hi = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5);                  int hi = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5);
# Line 415  bool RegionChooser::is_in_resize_zone(do Line 745  bool RegionChooser::is_in_resize_zone(do
745                      resize.mode = resize.moving_high_limit;                      resize.mode = resize.moving_high_limit;
746                      resize.min = r->KeyRange.low + 1;                      resize.min = r->KeyRange.low + 1;
747                      resize.max = next_region ? next_region->KeyRange.low : 128;                      resize.max = next_region ? next_region->KeyRange.low : 128;
748                      return true;                      return resize.min != resize.max;
749                  }                  }
750              }              }
751              prev_region = r;              prev_region = r;
# Line 424  bool RegionChooser::is_in_resize_zone(do Line 754  bool RegionChooser::is_in_resize_zone(do
754      return false;      return false;
755  }  }
756    
757  sigc::signal<void> RegionChooser::signal_region_selected()  sigc::signal<void>& RegionChooser::signal_region_selected()
758  {  {
759      return region_selected;      return region_selected;
760  }  }
761    
762  sigc::signal<void> RegionChooser::signal_instrument_changed()  sigc::signal<void>& RegionChooser::signal_instrument_changed()
763  {  {
764      return instrument_changed;      return instrument_changed;
765  }  }
# Line 437  sigc::signal<void> RegionChooser::signal Line 767  sigc::signal<void> RegionChooser::signal
767  void RegionChooser::show_region_properties()  void RegionChooser::show_region_properties()
768  {  {
769      if (!region) return;      if (!region) return;
770      Gtk::Dialog dialog("Region Properties", true /*modal*/);      Gtk::Dialog dialog(_("Region Properties"), true /*modal*/);
771      // add "Keygroup" checkbox      // add "Keygroup" checkbox
772      Gtk::CheckButton checkBoxKeygroup("Member of a Keygroup (Exclusive Group)");      Gtk::CheckButton checkBoxKeygroup(_("Member of a Keygroup (Exclusive Group)"));
773      checkBoxKeygroup.set_active(region->KeyGroup);      checkBoxKeygroup.set_active(region->KeyGroup);
774      dialog.get_vbox()->pack_start(checkBoxKeygroup);      dialog.get_vbox()->pack_start(checkBoxKeygroup);
775      checkBoxKeygroup.show();      checkBoxKeygroup.show();
# Line 461  void RegionChooser::show_region_properti Line 791  void RegionChooser::show_region_properti
791    
792  void RegionChooser::add_region()  void RegionChooser::add_region()
793  {  {
794      gig::Region* r;      instrument_struct_to_be_changed_signal.emit(instrument);
     for (r = instrument->GetFirstRegion() ; r ; r = instrument->GetNextRegion()) {  
         if (r->KeyRange.low > new_region_pos) break;  
     }  
795    
796      region = instrument->AddRegion();      region = instrument->AddRegion();
797      region->KeyRange.low = region->KeyRange.high = new_region_pos;      region->SetKeyRange(new_region_pos, new_region_pos);
798    
799        instrument_struct_changed_signal.emit(instrument);
800        regions.update(instrument);
801    
     instrument->MoveRegion(region, r);  
802      queue_draw();      queue_draw();
803      region_selected();      region_selected();
804        dimensionManager.set_region(region);
805      instrument_changed();      instrument_changed();
806  }  }
807    
808  void RegionChooser::delete_region()  void RegionChooser::delete_region()
809  {  {
810        instrument_struct_to_be_changed_signal.emit(instrument);
811      instrument->DeleteRegion(region);      instrument->DeleteRegion(region);
812        instrument_struct_changed_signal.emit(instrument);
813        regions.update(instrument);
814    
815      region = 0;      region = 0;
816      queue_draw();      queue_draw();
817      region_selected();      region_selected();
818        dimensionManager.set_region(region);
819      instrument_changed();      instrument_changed();
820  }  }
821    
# Line 495  void RegionChooser::on_dimension_manager Line 830  void RegionChooser::on_dimension_manager
830      region_selected();      region_selected();
831      instrument_changed();      instrument_changed();
832  }  }
833    
834    sigc::signal<void, gig::Instrument*>& RegionChooser::signal_instrument_struct_to_be_changed() {
835        return instrument_struct_to_be_changed_signal;
836    }
837    
838    sigc::signal<void, gig::Instrument*>& RegionChooser::signal_instrument_struct_changed() {
839        return instrument_struct_changed_signal;
840    }
841    
842    sigc::signal<void, gig::Region*>& RegionChooser::signal_region_to_be_changed() {
843        return region_to_be_changed_signal;
844    }
845    
846    sigc::signal<void, gig::Region*>& RegionChooser::signal_region_changed_signal() {
847        return region_changed_signal;
848    }
849    
850    sigc::signal<void, int/*key*/, int/*velocity*/>& RegionChooser::signal_keyboard_key_hit() {
851        return keyboard_key_hit_signal;
852    }
853    
854    sigc::signal<void, int/*key*/, int/*velocity*/>& RegionChooser::signal_keyboard_key_released() {
855        return keyboard_key_released_signal;
856    }

Legend:
Removed from v.1261  
changed lines
  Added in v.1831

  ViewVC Help
Powered by ViewVC