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