1 |
/* |
/* |
2 |
* Copyright (C) 2006-2009 Andreas Persson |
* Copyright (C) 2006-2010 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 |
18 |
*/ |
*/ |
19 |
|
|
20 |
#include "dimregionchooser.h" |
#include "dimregionchooser.h" |
21 |
|
#include <cairomm/context.h> |
22 |
#include <gdkmm/cursor.h> |
#include <gdkmm/cursor.h> |
23 |
|
#include <gdkmm/general.h> |
24 |
|
|
25 |
#include "global.h" |
#include "global.h" |
26 |
|
|
27 |
|
#include "global.h" |
28 |
|
|
29 |
|
#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 10) || GTKMM_MAJOR_VERSION < 2 |
30 |
|
|
31 |
|
#define create_cairo_context() \ |
32 |
|
gobj() ? Cairo::RefPtr<Cairo::Context>( \ |
33 |
|
new Cairo::Context(gdk_cairo_create(get_window()->gobj()))) : \ |
34 |
|
Cairo::RefPtr<Cairo::Context>() |
35 |
|
|
36 |
|
namespace Gdk { |
37 |
|
namespace Cairo { |
38 |
|
void set_source_color(const ::Cairo::RefPtr< ::Cairo::Context >& cr, |
39 |
|
const Gdk::Color& color); |
40 |
|
} |
41 |
|
} |
42 |
|
#endif |
43 |
|
|
44 |
DimRegionChooser::DimRegionChooser() |
DimRegionChooser::DimRegionChooser() |
45 |
{ |
{ |
46 |
// get_window() would return 0 because the Gdk::Window has not yet been realized |
// get_window() would return 0 because the Gdk::Window has not yet been realized |
65 |
resize.active = false; |
resize.active = false; |
66 |
cursor_is_resize = false; |
cursor_is_resize = false; |
67 |
h = 20; |
h = 20; |
68 |
|
#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 18) || GTKMM_MAJOR_VERSION < 2 |
69 |
set_flags(Gtk::CAN_FOCUS); |
set_flags(Gtk::CAN_FOCUS); |
70 |
|
#else |
71 |
|
set_can_focus(); |
72 |
|
#endif |
73 |
add_events(Gdk::BUTTON_PRESS_MASK | Gdk::POINTER_MOTION_MASK | |
add_events(Gdk::BUTTON_PRESS_MASK | Gdk::POINTER_MOTION_MASK | |
74 |
Gdk::POINTER_MOTION_HINT_MASK); |
Gdk::POINTER_MOTION_HINT_MASK); |
75 |
|
|
80 |
{ |
{ |
81 |
} |
} |
82 |
|
|
|
void DimRegionChooser::on_realize() |
|
|
{ |
|
|
// We need to call the base on_realize() |
|
|
Gtk::DrawingArea::on_realize(); |
|
|
|
|
|
// Now we can allocate any additional resources we need |
|
|
Glib::RefPtr<Gdk::Window> window = get_window(); |
|
|
gc = Gdk::GC::create(window); |
|
|
} |
|
|
|
|
83 |
bool DimRegionChooser::on_expose_event(GdkEventExpose* event) |
bool DimRegionChooser::on_expose_event(GdkEventExpose* event) |
84 |
{ |
{ |
85 |
if (!region) return true; |
if (!region) return true; |
90 |
Glib::RefPtr<Pango::Context> context = get_pango_context(); |
Glib::RefPtr<Pango::Context> context = get_pango_context(); |
91 |
|
|
92 |
Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create(context); |
Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create(context); |
93 |
|
Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context(); |
94 |
|
cr->set_line_width(1); |
95 |
|
|
96 |
window->clear(); |
window->clear(); |
97 |
|
|
150 |
double text_w = double(rectangle.get_width()) / Pango::SCALE; |
double text_w = double(rectangle.get_width()) / Pango::SCALE; |
151 |
if (text_w > maxwidth) maxwidth = text_w; |
if (text_w > maxwidth) maxwidth = text_w; |
152 |
double text_h = double(rectangle.get_height()) / Pango::SCALE; |
double text_h = double(rectangle.get_height()) / Pango::SCALE; |
153 |
Glib::RefPtr<const Gdk::GC> fg = get_style()->get_fg_gc(get_state()); |
const Gdk::Color fg = get_style()->get_fg(get_state()); |
154 |
window->draw_layout(fg, 4, int(y + (h - text_h) / 2 + 0.5), layout); |
Gdk::Cairo::set_source_color(cr, fg); |
155 |
|
cr->move_to(4, int(y + (h - text_h) / 2 + 0.5)); |
156 |
|
#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 16) || GTKMM_MAJOR_VERSION < 2 |
157 |
|
pango_cairo_show_layout(cr->cobj(), layout->gobj()); |
158 |
|
#else |
159 |
|
layout->show_in_cairo_context(cr); |
160 |
|
#endif |
161 |
} |
} |
162 |
y += h; |
y += h; |
163 |
} |
} |
176 |
0, y, label_width, 20); |
0, y, label_width, 20); |
177 |
} |
} |
178 |
|
|
|
Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc(); |
|
179 |
// draw top and bottom lines of dimension's zones |
// draw top and bottom lines of dimension's zones |
180 |
window->draw_line(black, label_width, y, w - 1, y); |
Gdk::Cairo::set_source_color(cr, black); |
181 |
window->draw_line(black, w - 1, y + h - 1, label_width, y + h - 1); |
cr->move_to(label_width, y + 0.5); |
182 |
|
cr->line_to(w, y + 0.5); |
183 |
|
cr->move_to(w, y + h - 0.5); |
184 |
|
cr->line_to(label_width, y + h - 0.5); |
185 |
|
cr->stroke(); |
186 |
|
|
187 |
// erase whole dimension's zones area |
// erase whole dimension's zones area |
188 |
window->draw_rectangle(get_style()->get_white_gc(), true, |
Gdk::Cairo::set_source_color(cr, white); |
189 |
label_width + 1, y + 1, (w - label_width - 2), h - 2); |
cr->rectangle(label_width + 1, y + 1, (w - label_width - 2), h - 2); |
190 |
|
cr->fill(); |
191 |
|
|
192 |
int c = 0; |
int c = 0; |
193 |
if (dimregno >= 0) { |
if (dimregno >= 0) { |
201 |
region->pDimensionRegions[c]->VelocityUpperLimit)); |
region->pDimensionRegions[c]->VelocityUpperLimit)); |
202 |
|
|
203 |
// draw dimension's zone borders |
// draw dimension's zone borders |
204 |
|
Gdk::Cairo::set_source_color(cr, black); |
205 |
if (customsplits) { |
if (customsplits) { |
206 |
window->draw_line(black, label_width, y + 1, label_width, y + h - 2); |
cr->move_to(label_width + 0.5, y + 1); |
207 |
|
cr->line_to(label_width + 0.5, y + h - 1); |
208 |
|
|
209 |
for (int j = 0 ; j < nbZones ; j++) { |
for (int j = 0 ; j < nbZones ; j++) { |
210 |
gig::DimensionRegion *d = region->pDimensionRegions[c + (j << bitpos)]; |
gig::DimensionRegion *d = region->pDimensionRegions[c + (j << bitpos)]; |
211 |
int upperLimit = d->DimensionUpperLimits[i]; |
int upperLimit = d->DimensionUpperLimits[i]; |
212 |
if (!upperLimit) upperLimit = d->VelocityUpperLimit; |
if (!upperLimit) upperLimit = d->VelocityUpperLimit; |
213 |
int v = upperLimit + 1; |
int v = upperLimit + 1; |
214 |
int x = int((w - label_width - 1) * v / 128.0 + 0.5); |
int x = int((w - label_width - 1) * v / 128.0 + 0.5); |
215 |
window->draw_line(black, label_width + x, y + 1, label_width + x, y + h - 2); |
cr->move_to(label_width + x + 0.5, y + 1); |
216 |
|
cr->line_to(label_width + x + 0.5, y + h - 1); |
217 |
} |
} |
218 |
} else { |
} else { |
219 |
for (int j = 0 ; j <= nbZones ; j++) { |
for (int j = 0 ; j <= nbZones ; j++) { |
220 |
int x = int((w - label_width - 1) * j / double(nbZones) + 0.5); |
int x = int((w - label_width - 1) * j / double(nbZones) + 0.5); |
221 |
window->draw_line(black, label_width + x, y + 1, label_width + x, y + h - 2); |
cr->move_to(label_width + x + 0.5, y + 1); |
222 |
|
cr->line_to(label_width + x + 0.5, y + h - 1); |
223 |
} |
} |
224 |
} |
} |
225 |
|
cr->stroke(); |
226 |
|
|
227 |
// draw fill for currently selected zone |
// draw fill for currently selected zone |
228 |
if (dimregno >= 0) { |
if (dimregno >= 0) { |
229 |
gc->set_foreground(red); |
Gdk::Cairo::set_source_color(cr, red); |
230 |
int dr = (dimregno >> bitpos) & ((1 << region->pDimensionDefinitions[i].bits) - 1); |
int dr = (dimregno >> bitpos) & ((1 << region->pDimensionDefinitions[i].bits) - 1); |
231 |
if (customsplits) { |
if (customsplits) { |
232 |
int x1 = 0; |
int x1 = 0; |
237 |
int v = upperLimit + 1; |
int v = upperLimit + 1; |
238 |
int x2 = int((w - label_width - 1) * v / 128.0 + 0.5); |
int x2 = int((w - label_width - 1) * v / 128.0 + 0.5); |
239 |
if (j == dr && x1 < x2) { |
if (j == dr && x1 < x2) { |
240 |
window->draw_rectangle(gc, true, label_width + x1 + 1, y + 1, |
cr->rectangle(label_width + x1 + 1, y + 1, |
241 |
(x2 - x1) - 1, h - 2); |
(x2 - x1) - 1, h - 2); |
242 |
|
cr->fill(); |
243 |
break; |
break; |
244 |
} |
} |
245 |
x1 = x2; |
x1 = x2; |
248 |
if (dr < nbZones) { |
if (dr < nbZones) { |
249 |
int x1 = int((w - label_width - 1) * dr / double(nbZones) + 0.5); |
int x1 = int((w - label_width - 1) * dr / double(nbZones) + 0.5); |
250 |
int x2 = int((w - label_width - 1) * (dr + 1) / double(nbZones) + 0.5); |
int x2 = int((w - label_width - 1) * (dr + 1) / double(nbZones) + 0.5); |
251 |
window->draw_rectangle(gc, true, label_width + x1 + 1, y + 1, |
cr->rectangle(label_width + x1 + 1, y + 1, |
252 |
(x2 - x1) - 1, h - 2); |
(x2 - x1) - 1, h - 2); |
253 |
|
cr->fill(); |
254 |
} |
} |
255 |
} |
} |
256 |
} |
} |
499 |
if (k < 2) k = 2; // k is upper limit + 1, upper limit 0 is forbidden |
if (k < 2) k = 2; // k is upper limit + 1, upper limit 0 is forbidden |
500 |
|
|
501 |
if (k != resize.pos) { |
if (k != resize.pos) { |
502 |
Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc(); |
Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context(); |
503 |
Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc(); |
cr->set_line_width(1); |
504 |
|
|
505 |
int prevx = int((w - label_width - 1) * resize.pos / 128.0 + 0.5) + label_width; |
int prevx = int((w - label_width - 1) * resize.pos / 128.0 + 0.5) + label_width; |
506 |
int x = int((w - label_width - 1) * k / 128.0 + 0.5) + label_width; |
int x = int((w - label_width - 1) * k / 128.0 + 0.5) + label_width; |
508 |
|
|
509 |
if (resize.selected == resize.none) { |
if (resize.selected == resize.none) { |
510 |
if (resize.pos != resize.min && resize.pos != resize.max) { |
if (resize.pos != resize.min && resize.pos != resize.max) { |
511 |
window->draw_line(white, prevx, y + 1, prevx, y + h - 2); |
Gdk::Cairo::set_source_color(cr, white); |
512 |
|
cr->move_to(prevx + 0.5, y + 1); |
513 |
|
cr->line_to(prevx + 0.5, y + h - 1); |
514 |
|
cr->stroke(); |
515 |
} |
} |
516 |
} else { |
} else { |
517 |
gc->set_foreground(red); |
Gdk::Color left; |
518 |
|
Gdk::Color right; |
|
Glib::RefPtr<const Gdk::GC> left; |
|
|
Glib::RefPtr<const Gdk::GC> right; |
|
519 |
if (resize.selected == resize.left) { |
if (resize.selected == resize.left) { |
520 |
left = gc; |
left = red; |
521 |
right = white; |
right = white; |
522 |
} else { |
} else { |
523 |
left = white; |
left = white; |
524 |
right = gc; |
right = red; |
525 |
} |
} |
526 |
|
|
527 |
if (k > resize.pos) { |
if (k > resize.pos) { |
528 |
int xx = resize.pos == resize.min ? 1 : 0; |
int xx = resize.pos == resize.min ? 1 : 0; |
529 |
window->draw_rectangle(left, true, |
Gdk::Cairo::set_source_color(cr, left); |
530 |
prevx + xx, y + 1, x - prevx - xx, h - 2); |
cr->rectangle(prevx + xx, y + 1, x - prevx - xx, h - 2); |
531 |
} else { |
} else { |
532 |
int xx = resize.pos == resize.max ? 0 : 1; |
int xx = resize.pos == resize.max ? 0 : 1; |
533 |
window->draw_rectangle(right, true, |
Gdk::Cairo::set_source_color(cr, right); |
534 |
x, y + 1, prevx - x + xx, h - 2); |
cr->rectangle(x, y + 1, prevx - x + xx, h - 2); |
535 |
} |
} |
536 |
|
cr->fill(); |
537 |
} |
} |
538 |
window->draw_line(black, x, y + 1, x, y + h - 2); |
Gdk::Cairo::set_source_color(cr, black); |
539 |
|
cr->move_to(x + 0.5, y + 1); |
540 |
|
cr->line_to(x + 0.5, y + h - 1); |
541 |
|
cr->stroke(); |
542 |
|
|
543 |
resize.pos = k; |
resize.pos = k; |
544 |
} |
} |
545 |
} else { |
} else { |