/[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 1654 by schoenebeck, Wed Jan 30 02:20:48 2008 UTC revision 1661 by schoenebeck, Sun Feb 3 14:10:47 2008 UTC
# Line 24  Line 24 
24  #include <gtkmm/spinbutton.h>  #include <gtkmm/spinbutton.h>
25  #include <gtkmm/dialog.h>  #include <gtkmm/dialog.h>
26  #include <math.h>  #include <math.h>
27    #include <sstream>
28    
29  #include "global.h"  #include "global.h"
30    
31    #define REGION_BLOCK_HEIGHT             20
32    #define KEYBOARD_HEIGHT                 40
33    
34  void SortedRegions::update(gig::Instrument* instrument) {  void SortedRegions::update(gig::Instrument* instrument) {
35      // Usually, the regions in a gig file are ordered after their key      // Usually, the regions in a gig file are ordered after their key
36      // range, but there are files where they are not. The      // range, but there are files where they are not. The
# Line 54  gig::Region* SortedRegions::next() { Line 58  gig::Region* SortedRegions::next() {
58    
59    
60    
61  RegionChooser::RegionChooser()  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    
# Line 74  RegionChooser::RegionChooser() Line 80  RegionChooser::RegionChooser()
80      resize.active = false;      resize.active = false;
81      move.active = false;      move.active = false;
82      cursor_is_resize = false;      cursor_is_resize = false;
83      h1 = 20;      h1 = REGION_BLOCK_HEIGHT;
84    
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 122  RegionChooser::RegionChooser() Line 151  RegionChooser::RegionChooser()
151              sigc::mem_fun(*this, &RegionChooser::on_dimension_manager_changed)              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    
162  RegionChooser::~RegionChooser()  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) {  void RegionChooser::on_note_on_event(int key, int velocity) {
173      draw_region(key, key+1, activeKeyColor);      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) {  void RegionChooser::on_note_off_event(int key, int velocity) {
# Line 137  void RegionChooser::on_note_off_event(in Line 179  void RegionChooser::on_note_off_event(in
179          draw_region(key, key+1, black);          draw_region(key, key+1, black);
180      else      else
181          draw_region(key, key+1, white);          draw_region(key, key+1, white);
182        m_VirtKeybOffVelocityLabel.set_text(ToString(velocity));
183  }  }
184    
185  void RegionChooser::on_realize()  void RegionChooser::on_realize()
# Line 154  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 = get_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    
     Glib::RefPtr<Pango::Context> context = get_pango_context();  
     Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create(context);  
   
207      window->draw_rectangle(black, false, 0, h1, w, h - 1);      window->draw_rectangle(black, false, 0, h1, w, h - 1);
208      gc->set_foreground(grey1);      gc->set_foreground(grey1);
209      int x1 = int(w * 20.5 / 128.0 + 0.5);      int x1 = int(w * 20.5 / 128.0 + 0.5);
# Line 173  bool RegionChooser::on_expose_event(GdkE Line 213  bool RegionChooser::on_expose_event(GdkE
213      window->draw_rectangle(white, true, x1 + 1, h1 + 1, x2 - x1 - 1, h - 2);      window->draw_rectangle(white, true, x1 + 1, h1 + 1, x2 - x1 - 1, h - 2);
214      window->draw_rectangle(gc, true, x2 + 1, h1 + 1,      window->draw_rectangle(gc, true, x2 + 1, h1 + 1,
215                             w - x2 - 1, h - 2);                             w - x2 - 1, h - 2);
     int octave = -1;  
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 187  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) {          if (note == 3) draw_digit(i);
             char buf[30];  
             sprintf(buf, "<span size=\"8000\">%d</span>", octave);  
             layout->set_markup(buf);  
             Pango::Rectangle rectangle = layout->get_logical_extents();  
             double text_w = double(rectangle.get_width()) / Pango::SCALE;  
             double text_h = double(rectangle.get_height()) / Pango::SCALE;  
             double x2 = w * (i + 0.75) / 128.0;  
             window->draw_layout(black, int(x2 - text_w / 2 + 1),  
                                 int(h1 + h - text_h + 0.5), layout);  
             octave++;  
         }  
230      }      }
231    
232      if (instrument) {      if (instrument) {
# Line 239  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    
# Line 248  bool RegionChooser::is_black_key(int key Line 276  bool RegionChooser::is_black_key(int key
276      return note == 1 || note == 4 || note == 6 || note == 9 || note == 11;      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    
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 = get_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 262  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 - 1, 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 - 1, 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 - 1, h1 + bh + 1, x3 - x4 + 1, 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 - 1, bh - 1);              window->draw_rectangle(gc, true, x, h1 + 1, w1, bh - 1);
326              break;              break;
327          }          }
328      }      }
# Line 295  void RegionChooser::set_instrument(gig:: Line 339  void RegionChooser::set_instrument(gig::
339    
340  bool RegionChooser::on_button_release_event(GdkEventButton* event)  bool RegionChooser::on_button_release_event(GdkEventButton* event)
341  {  {
342        const int k = int(event->x / (get_width() - 1) * 128.0);
343    
344        // handle-note off on virtual keyboard
345        if (event->type == GDK_BUTTON_RELEASE) {
346            int velocity = (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 :
347                           int(float(event->y - REGION_BLOCK_HEIGHT) / float(KEYBOARD_HEIGHT) * 128.0f) + 1;
348            if (velocity <= 0) velocity = 1;
349            switch (m_VirtKeybModeChoice.get_value()) {
350                case VIRT_KEYBOARD_MODE_CHORD:
351                    if (event->y >= REGION_BLOCK_HEIGHT)
352                        keyboard_key_released_signal.emit(k, velocity);
353                    break;
354                case VIRT_KEYBOARD_MODE_NORMAL:
355                default:
356                    if (currentActiveKey >= 0 && currentActiveKey <= 127) {
357                        keyboard_key_released_signal.emit(currentActiveKey, velocity);
358                        currentActiveKey = -1;
359                    }
360                    break;
361            }
362        }
363    
364      if (resize.active) {      if (resize.active) {
365          get_window()->pointer_ungrab(event->time);          get_window()->pointer_ungrab(event->time);
366          resize.active = false;          resize.active = false;
# Line 354  bool RegionChooser::on_button_press_even Line 420  bool RegionChooser::on_button_press_even
420  {  {
421      if (!instrument) return true;      if (!instrument) return true;
422    
423      int k = int(event->x / (get_width() - 1) * 128.0);      const int k = int(event->x / (get_width() - 1) * 128.0);
424    
425        if (event->type == GDK_BUTTON_PRESS) {
426            if (event->y >= REGION_BLOCK_HEIGHT) {
427                int velocity = (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 :
428                               int(float(event->y - REGION_BLOCK_HEIGHT) / float(KEYBOARD_HEIGHT) * 128.0f) + 1;
429                currentActiveKey = k;
430                keyboard_key_hit_signal.emit(k, velocity);
431            }
432        }
433    
434        if (event->y >= REGION_BLOCK_HEIGHT) return true;
435      if (event->type == GDK_BUTTON_PRESS && event->button == 3) {      if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
436          gig::Region* r = get_region(k);          gig::Region* r = get_region(k);
437          if (r) {          if (r) {
# Line 591  bool RegionChooser::on_motion_notify_eve Line 667  bool RegionChooser::on_motion_notify_eve
667      Gdk::ModifierType state = Gdk::ModifierType(0);      Gdk::ModifierType state = Gdk::ModifierType(0);
668      window->get_pointer(x, y, state);      window->get_pointer(x, y, state);
669    
670        // handle virtual MIDI keyboard
671        if (m_VirtKeybModeChoice.get_value() != VIRT_KEYBOARD_MODE_CHORD &&
672            currentActiveKey > 0 &&
673            event->y >= REGION_BLOCK_HEIGHT &&
674            event->y < REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT)
675        {
676            const int k = int(event->x / (get_width() - 1) * 128.0);
677            int velocity = (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 :
678                           int(float(event->y - REGION_BLOCK_HEIGHT) / float(KEYBOARD_HEIGHT) * 128.0f) + 1;
679            if (velocity <= 0) velocity = 1;
680            keyboard_key_released_signal.emit(currentActiveKey, velocity);
681            currentActiveKey = k;
682            keyboard_key_hit_signal.emit(k, velocity);
683        }
684    
685      if (resize.active) {      if (resize.active) {
686          motion_resize_region(x, y);          motion_resize_region(x, y);
687      } else if (move.active) {      } else if (move.active) {
# Line 750  sigc::signal<void, gig::Region*>& Region Line 841  sigc::signal<void, gig::Region*>& Region
841  sigc::signal<void, gig::Region*>& RegionChooser::signal_region_changed_signal() {  sigc::signal<void, gig::Region*>& RegionChooser::signal_region_changed_signal() {
842      return region_changed_signal;      return region_changed_signal;
843  }  }
844    
845    sigc::signal<void, int/*key*/, int/*velocity*/>& RegionChooser::signal_keyboard_key_hit() {
846        return keyboard_key_hit_signal;
847    }
848    
849    sigc::signal<void, int/*key*/, int/*velocity*/>& RegionChooser::signal_keyboard_key_released() {
850        return keyboard_key_released_signal;
851    }

Legend:
Removed from v.1654  
changed lines
  Added in v.1661

  ViewVC Help
Powered by ViewVC