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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1071 - (show annotations) (download)
Mon Mar 5 17:58:24 2007 UTC (13 years, 4 months ago) by persson
File size: 14999 byte(s)
* added support for gig v3 custom split points

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 "dimregionchooser.h"
21
22 DimRegionChooser::DimRegionChooser()
23 {
24 // get_window() would return 0 because the Gdk::Window has not yet been realized
25 // So we can only allocate the colors here - the rest will happen in on_realize().
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("blue");
32 green = Gdk::Color("green");
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 instrument = 0;
40 region = 0;
41 dimregno = -1;
42 focus_line = 0;
43 set_flags(Gtk::CAN_FOCUS);
44 add_events(Gdk::BUTTON_PRESS_MASK);
45
46 for (int i = 0 ; i < 256 ; i++) {
47 dimvalue_from[i] = 0;
48 dimvalue_to[i] = 1;
49 }
50 }
51
52 DimRegionChooser::~DimRegionChooser()
53 {
54 }
55
56 void DimRegionChooser::on_realize()
57 {
58 // We need to call the base on_realize()
59 Gtk::DrawingArea::on_realize();
60
61 // Now we can allocate any additional resources we need
62 Glib::RefPtr<Gdk::Window> window = get_window();
63 gc = Gdk::GC::create(window);
64 }
65
66 bool DimRegionChooser::on_expose_event(GdkEventExpose* event)
67 {
68 if (!region) return true;
69
70 // This is where we draw on the window
71 Glib::RefPtr<Gdk::Window> window = get_window();
72 Glib::RefPtr<Pango::Context> context = get_pango_context();
73
74 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create(context);
75
76 window->clear();
77 const int h = 20;
78 const int w = 800;
79 const int w1 = 100;
80 int y = 0;
81 int bitpos = 0;
82 for (int i = 0 ; i < region->Dimensions ; i++) {
83
84 int nbZones = region->pDimensionDefinitions[i].zones;
85 if (nbZones) {
86 char* dstr;
87 char dstrbuf[10];
88 switch (region->pDimensionDefinitions[i].dimension) {
89 case gig::dimension_none: dstr="none"; break;
90 case gig::dimension_samplechannel: dstr="samplechannel"; break;
91 case gig::dimension_layer: dstr="layer"; break;
92 case gig::dimension_velocity: dstr="velocity"; break;
93 case gig::dimension_channelaftertouch: dstr="channelaftertouch"; break;
94 case gig::dimension_releasetrigger: dstr="releasetrigger"; break;
95 case gig::dimension_keyboard: dstr="keyboard"; break;
96 case gig::dimension_roundrobin: dstr="roundrobin"; break;
97 case gig::dimension_random: dstr="random"; break;
98 case gig::dimension_modwheel: dstr="modwheel"; break;
99 case gig::dimension_breath: dstr="breath"; break;
100 case gig::dimension_foot: dstr="foot"; break;
101 case gig::dimension_portamentotime: dstr="portamentotime"; break;
102 case gig::dimension_effect1: dstr="effect1"; break;
103 case gig::dimension_effect2: dstr="effect2"; break;
104 case gig::dimension_genpurpose1: dstr="genpurpose1"; break;
105 case gig::dimension_genpurpose2: dstr="genpurpose2"; break;
106 case gig::dimension_genpurpose3: dstr="genpurpose3"; break;
107 case gig::dimension_genpurpose4: dstr="genpurpose4"; break;
108 case gig::dimension_sustainpedal: dstr="sustainpedal"; break;
109 case gig::dimension_portamento: dstr="portamento"; break;
110 case gig::dimension_sostenutopedal: dstr="sostenutopedal"; break;
111 case gig::dimension_softpedal: dstr="softpedal"; break;
112 case gig::dimension_genpurpose5: dstr="genpurpose5"; break;
113 case gig::dimension_genpurpose6: dstr="genpurpose6"; break;
114 case gig::dimension_genpurpose7: dstr="genpurpose7"; break;
115 case gig::dimension_genpurpose8: dstr="genpurpose8"; break;
116 case gig::dimension_effect1depth: dstr="effect1depth"; break;
117 case gig::dimension_effect2depth: dstr="effect2depth"; break;
118 case gig::dimension_effect3depth: dstr="effect3depth"; break;
119 case gig::dimension_effect4depth: dstr="effect4depth"; break;
120 case gig::dimension_effect5depth: dstr="effect5depth"; break;
121 default:
122 sprintf(dstrbuf, "%d",
123 region->pDimensionDefinitions[i].dimension);
124 dstr = dstrbuf;
125 break;
126 }
127 layout->set_text(dstr);
128
129 Pango::Rectangle rectangle = layout->get_logical_extents();
130 double text_h = double(rectangle.get_height()) / Pango::SCALE;
131 Glib::RefPtr<const Gdk::GC> fg = get_style()->get_fg_gc(get_state());
132 window->draw_layout(fg, 4, int(y + (h - text_h) / 2 + 0.5), layout);
133
134 if (has_focus() && focus_line == i) {
135 Gdk::Rectangle farea(0, y, 50, 20);
136 get_style()->paint_focus(window, get_state(), farea, *this, "hejsan", 0, y, 50, 20);
137 }
138
139 Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc();
140 window->draw_line(black, w1, y, w - 1, y);
141 window->draw_line(black, w - 1, y + h - 1, w1, y + h - 1);
142 window->draw_rectangle(get_style()->get_white_gc(), true, w1 + 1, y + 1, (w - w1 - 2), h - 2);
143
144 int c = 0;
145 if (dimregno >= 0) {
146 int mask = ~(((1 << region->pDimensionDefinitions[i].bits) - 1) << bitpos);
147 c = dimregno & mask; // mask away this dimension
148 }
149 bool customsplits =
150 ((region->pDimensionDefinitions[i].split_type == gig::split_type_normal &&
151 region->pDimensionRegions[c]->DimensionUpperLimits[i]) ||
152 (region->pDimensionDefinitions[i].dimension == gig::dimension_velocity &&
153 region->pDimensionRegions[c]->VelocityUpperLimit));
154
155 if (customsplits) {
156 window->draw_line(black, w1, y + 1, w1, y + h - 2);
157 for (int j = 0 ; j < nbZones ; j++) {
158 gig::DimensionRegion *d = region->pDimensionRegions[c + (j << bitpos)];
159 int upperLimit = d->DimensionUpperLimits[i];
160 if (!upperLimit) upperLimit = d->VelocityUpperLimit;
161 int v = upperLimit + 1;
162 int x = int((w - w1 - 1) * v / 128.0 + 0.5);
163 window->draw_line(black, w1 + x, y + 1, w1 + x, y + h - 2);
164 }
165 } else {
166 for (int j = 0 ; j <= nbZones ; j++) {
167 int x = int((w - w1 - 1) * j / double(nbZones) + 0.5);
168 window->draw_line(black, w1 + x, y + 1, w1 + x, y + h - 2);
169 }
170 }
171
172 if (dimregno >= 0) {
173 gc->set_foreground(red);
174 int dr = (dimregno >> bitpos) & ((1 << region->pDimensionDefinitions[i].bits) - 1);
175 if (customsplits) {
176 int x1 = 0;
177 for (int j = 0 ; j < nbZones ; j++) {
178 gig::DimensionRegion *d = region->pDimensionRegions[c + (j << bitpos)];
179 int upperLimit = d->DimensionUpperLimits[i];
180 if (!upperLimit) upperLimit = d->VelocityUpperLimit;
181 int v = upperLimit + 1;
182 int x2 = int((w - w1 - 1) * v / 128.0 + 0.5);
183 if (j == dr && x1 < x2) {
184 window->draw_rectangle(gc, true, w1 + x1 + 1, y + 1, (x2 - x1) - 1, h - 2);
185 break;
186 }
187 x1 = x2;
188 }
189 } else {
190 if (dr < nbZones) {
191 int x1 = int((w - w1 - 1) * dr / double(nbZones) + 0.5);
192 int x2 = int((w - w1 - 1) * (dr + 1) / double(nbZones) + 0.5);
193 window->draw_rectangle(gc, true, w1 + x1 + 1, y + 1, (x2 - x1) - 1, h - 2);
194 }
195 }
196 }
197
198 y += h;
199 }
200 bitpos += region->pDimensionDefinitions[i].bits;
201 }
202
203 return true;
204 }
205
206
207 void DimRegionChooser::on_size_request(GtkRequisition* requisition)
208 {
209 printf("DimRegionChooser::on_size_request\n");
210 *requisition = GtkRequisition();
211 requisition->height = region ? region->Dimensions * 20 : 0;
212 requisition->width = 800;
213 }
214
215 void DimRegionChooser::set_instrument(gig::Instrument* instrument)
216 {
217 this->instrument = instrument;
218 this->region = 0;
219 this->dimregno = -1;
220 queue_draw();
221 }
222
223 void DimRegionChooser::set_region(gig::Region* region)
224 {
225 this->region = region;
226 dimregno = 0;
227 int bitcount = 0;
228 for (int dim = 0 ; dim < region->Dimensions ; dim++) {
229 int from = dimvalue_from[region->pDimensionDefinitions[dim].dimension];
230 int to = dimvalue_to[region->pDimensionDefinitions[dim].dimension];
231 int z;
232 switch (region->pDimensionDefinitions[dim].split_type) {
233 case gig::split_type_normal:
234 z = int((to + from) / 2.0 / region->pDimensionDefinitions[dim].zone_size);
235 break;
236 case gig::split_type_bit:
237 z = std::min(from, region->pDimensionDefinitions[dim].zones - 1);
238 break;
239 }
240 int mask =
241 ~(((1 << region->pDimensionDefinitions[dim].bits) - 1) <<
242 bitcount);
243 dimregno &= mask;
244 dimregno |= (z << bitcount);
245 bitcount += region->pDimensionDefinitions[dim].bits;
246 }
247 dimreg = region->pDimensionRegions[dimregno];
248 sel_changed_signal.emit();
249 queue_resize();
250 }
251
252 /*
253 void DimRegionChooser::set_dimregno(int x) {
254 this->dimregno = x;
255 queue_draw();
256 }
257 */
258
259 bool DimRegionChooser::on_button_press_event(GdkEventButton* event)
260 {
261 const int h = 20;
262 const int w = 800;
263 const int w1 = 100;
264
265 if (region) {
266 if (event->y < region->Dimensions * h &&
267 event->x >= w1 && event->x < w) {
268
269 int dim = int(event->y / h);
270 int nbZones = region->pDimensionDefinitions[dim].zones;
271
272 int z = -1;
273 int bitpos = 0;
274 for (int i = 0 ; i < dim ; i++) {
275 bitpos += region->pDimensionDefinitions[i].bits;
276 }
277
278 int i = dim;
279 if (dimregno < 0) dimregno = 0;
280 int mask = ~(((1 << region->pDimensionDefinitions[i].bits) - 1) << bitpos);
281 int c = dimregno & mask; // mask away this dimension
282
283 bool customsplits =
284 ((region->pDimensionDefinitions[i].split_type == gig::split_type_normal &&
285 region->pDimensionRegions[c]->DimensionUpperLimits[i]) ||
286 (region->pDimensionDefinitions[i].dimension == gig::dimension_velocity &&
287 region->pDimensionRegions[c]->VelocityUpperLimit));
288 if (customsplits) {
289 int val = int((event->x - w1) * 128 / (w - w1 - 1));
290
291 if (region->pDimensionRegions[c]->DimensionUpperLimits[i]) {
292 for (z = 0 ; z < nbZones ; z++) {
293 gig::DimensionRegion *d = region->pDimensionRegions[c + (z << bitpos)];
294 if (val <= d->DimensionUpperLimits[i]) break;
295 }
296 } else {
297 for (z = 0 ; z < nbZones ; z++) {
298 gig::DimensionRegion *d = region->pDimensionRegions[c + (z << bitpos)];
299 if (val <= d->VelocityUpperLimit) break;
300 }
301 }
302 } else {
303 z = int((event->x - w1) * nbZones / (w - w1 - 1));
304 }
305
306 printf("dim=%d z=%d dimensionsource=%d split_type=%d zones=%d zone_size=%f\n", dim, z,
307 region->pDimensionDefinitions[dim].dimension,
308 region->pDimensionDefinitions[dim].split_type,
309 region->pDimensionDefinitions[dim].zones,
310 region->pDimensionDefinitions[dim].zone_size);
311 #if 0
312 switch (region->pDimensionDefinitions[dim].split_type) {
313 case gig::split_type_normal:
314 dimvalue_from[region->pDimensionDefinitions[dim].dimension] =
315 int(z * region->pDimensionDefinitions[dim].zone_size);
316 dimvalue_to[region->pDimensionDefinitions[dim].dimension] =
317 int((z + 1) * region->pDimensionDefinitions[dim].zone_size) - 1;
318 break;
319 case gig::split_type_bit:
320 dimvalue_from[region->pDimensionDefinitions[dim].dimension] = z;
321 dimvalue_to[region->pDimensionDefinitions[dim].dimension] = z + 1;
322 break;
323 }
324 #endif
325
326 dimregno = c | (z << bitpos);
327
328 focus_line = dim;
329 if (has_focus()) queue_draw();
330 else grab_focus();
331 dimreg = region->pDimensionRegions[dimregno];
332 sel_changed_signal.emit();
333 }
334 }
335 return true;
336 }
337
338 sigc::signal<void> DimRegionChooser::signal_sel_changed()
339 {
340 return sel_changed_signal;
341 }
342
343 bool DimRegionChooser::on_focus(Gtk::DirectionType direction)
344 {
345 // TODO: kolla att region finns osv, dvs att det g�r att s�tta
346 // fokus.
347 if (direction == Gtk::DIR_TAB_FORWARD ||
348 direction == Gtk::DIR_DOWN) {
349 if (!has_focus()) {
350 focus_line = 0;
351 grab_focus();
352 return true;
353 } else {
354 if (focus_line + 1 < region->Dimensions) {
355 focus_line++;
356 queue_draw();
357 return true;
358 } else {
359 return false;
360 }
361 }
362 } else if (direction == Gtk::DIR_TAB_BACKWARD ||
363 direction == Gtk::DIR_UP) {
364 if (!has_focus()) {
365 focus_line = region->Dimensions - 1;
366 grab_focus();
367 return true;
368 } else {
369 if (focus_line > 0) {
370 focus_line--;
371 queue_draw();
372 return true;
373 } else {
374 return false;
375 }
376 }
377 } else if (!has_focus()) {
378 // TODO: kolla att focus_line finns!
379 grab_focus();
380 return true;
381 } else {
382 // TODO: �ka eller minska v�rde!
383 }
384 }

  ViewVC Help
Powered by ViewVC