| 1 |
/* |
| 2 |
* Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras) |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 3 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
private class DesktopFolder.MenuItemColor : Gtk.MenuItem { |
| 19 |
private new bool has_focus; |
| 20 |
private int height; |
| 21 |
public signal void color_changed (int ncolor); |
| 22 |
public signal void custom_changed (string custom); |
| 23 |
|
| 24 |
private string[] tags_colors; |
| 25 |
private string custom; |
| 26 |
private const int XPAD = 17; |
| 27 |
|
| 28 |
public MenuItemColor (string[] tags_colors, string ? custom) { |
| 29 |
this.tags_colors = tags_colors; |
| 30 |
this.custom = custom; |
| 31 |
set_size_request (160, 20); |
| 32 |
height = 20; |
| 33 |
|
| 34 |
button_press_event.connect (button_pressed_cb); |
| 35 |
draw.connect (on_draw); |
| 36 |
|
| 37 |
select.connect (() => { |
| 38 |
has_focus = true; |
| 39 |
}); |
| 40 |
|
| 41 |
deselect.connect (() => { |
| 42 |
has_focus = false; |
| 43 |
}); |
| 44 |
} |
| 45 |
|
| 46 |
private bool button_pressed_cb (Gdk.EventButton event) { |
| 47 |
determine_button_pressed_event (event); |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
private void determine_button_pressed_event (Gdk.EventButton event) { |
| 52 |
int i; |
| 53 |
int btnw = 10; |
| 54 |
int btnh = 10; |
| 55 |
int y0 = (height - btnh) / 2; |
| 56 |
int x0 = btnw + 5; |
| 57 |
int xpad = XPAD; |
| 58 |
|
| 59 |
if (event.y >= y0 && event.y <= y0 + btnh) { |
| 60 |
for (i = 1; i <= this.tags_colors.length + 1; i++) { |
| 61 |
if (event.x >= xpad + x0 * i && event.x <= xpad + x0 * i + btnw) { |
| 62 |
if (i > this.tags_colors.length) { |
| 63 |
Gtk.ColorSelectionDialog dialog = new Gtk.ColorSelectionDialog (DesktopFolder.Lang.MENU_COLOR_DIALOG_TITLE); |
| 64 |
dialog.set_transient_for ((Gtk.Window)this.get_toplevel()); |
| 65 |
dialog.get_color_selection ().set_has_opacity_control (true); |
| 66 |
Gdk.RGBA _rgba = Gdk.RGBA (); |
| 67 |
_rgba.parse (this.custom); |
| 68 |
dialog.get_color_selection ().set_current_rgba (_rgba); |
| 69 |
dialog.get_color_selection ().set_previous_rgba (_rgba); |
| 70 |
if (dialog.run () == Gtk.ResponseType.OK) { |
| 71 |
unowned Gtk.ColorSelection widget = dialog.get_color_selection (); |
| 72 |
string rgba = widget.current_rgba.to_string (); |
| 73 |
custom_changed (rgba); |
| 74 |
// uint alpha = widget.current_alpha; |
| 75 |
// stdout.puts ("Selection\n"); |
| 76 |
// stdout.printf (" %s\n", rgba); |
| 77 |
// stdout.printf (" %u\n", alpha); |
| 78 |
} |
| 79 |
dialog.close (); |
| 80 |
} else { |
| 81 |
color_changed (i - 1); |
| 82 |
} |
| 83 |
break; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
protected bool on_draw (Cairo.Context cr) { |
| 90 |
int i; |
| 91 |
int btnw = 10; |
| 92 |
int btnh = 10; |
| 93 |
int y0 = (height - btnh) / 2; |
| 94 |
int x0 = btnw + 5; |
| 95 |
int xpad = XPAD; |
| 96 |
|
| 97 |
for (i = 1; i <= this.tags_colors.length + 1; i++) { |
| 98 |
if (i == 1) { |
| 99 |
DrawCross (cr, xpad + x0 * i, y0 + 1, btnw - 2, btnh - 2); |
| 100 |
} else if (i > this.tags_colors.length) { |
| 101 |
if (this.custom != null) { |
| 102 |
Gdk.RGBA rgba = Gdk.RGBA (); |
| 103 |
rgba.parse (this.custom); |
| 104 |
rgba.alpha = 1; |
| 105 |
string custom_without_alpha = rgba.to_string (); |
| 106 |
DrawRoundedRectangle (cr, xpad + x0 * i, y0, btnw, btnh, "stroke", custom_without_alpha); |
| 107 |
DrawRoundedRectangle (cr, xpad + x0 * i, y0, btnw, btnh, "fill", custom_without_alpha); |
| 108 |
DrawGradientOverlay (cr, xpad + x0 * i, y0, btnw, btnh); |
| 109 |
DrawInterrogation (cr, xpad + x0 * i, y0 + 1, btnw - 2, btnh - 2); |
| 110 |
} |
| 111 |
} else { |
| 112 |
DrawRoundedRectangle (cr, xpad + x0 * i, y0, btnw, btnh, "stroke", this.tags_colors[i - 1]); |
| 113 |
DrawRoundedRectangle (cr, xpad + x0 * i, y0, btnw, btnh, "fill", this.tags_colors[i - 1]); |
| 114 |
DrawGradientOverlay (cr, xpad + x0 * i, y0, btnw, btnh); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
private void DrawCross (Cairo.Context cr, int x, int y, int w, int h) { |
| 122 |
cr.new_path (); |
| 123 |
cr.set_line_width (2.0); |
| 124 |
cr.move_to (x, y); |
| 125 |
cr.rel_line_to (w, h); |
| 126 |
cr.move_to (x, y + h); |
| 127 |
cr.rel_line_to (w, -h); |
| 128 |
cr.set_source_rgba (0, 0, 0, 0.6); |
| 129 |
cr.stroke (); |
| 130 |
|
| 131 |
cr.close_path (); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* @name DrawInterrogation |
| 136 |
* @description draw an interrogation character over the back custom color |
| 137 |
* @param {Cairo.Context} cr the context to use |
| 138 |
* @param {int} the X position |
| 139 |
* @param {int} the Y position |
| 140 |
* @param {int} the Width of the rectangle |
| 141 |
* @param {int} the Height of the rectangle |
| 142 |
*/ |
| 143 |
private void DrawInterrogation (Cairo.Context cr, int x, int y, int w, int h) { |
| 144 |
string utf8 = "?"; |
| 145 |
|
| 146 |
cr.set_source_rgba (1, 1, 1, 1); |
| 147 |
cr.select_font_face ("Dialog", Cairo.FontSlant.NORMAL, Cairo.FontWeight.BOLD); |
| 148 |
cr.set_font_size (12.0); |
| 149 |
|
| 150 |
Cairo.TextExtents extents; |
| 151 |
cr.text_extents (utf8, out extents); |
| 152 |
double sx = x + (w / 2) - (extents.width / 2 + extents.x_bearing); |
| 153 |
double sy = y + (y / 2) - (extents.height / 2 + extents.y_bearing); |
| 154 |
|
| 155 |
cr.move_to (sx, sy); |
| 156 |
cr.show_text (utf8); |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
* Create a rounded rectangle using the Bezier curve. |
| 161 |
* Adapted from http://cairographics.org/cookbook/roundedrectangles/ |
| 162 |
*/ |
| 163 |
private void DrawRoundedRectangle (Cairo.Context cr, int x, int y, int w, int h, string style, string color) { |
| 164 |
int radius_x = 2; |
| 165 |
int radius_y = 2; |
| 166 |
double ARC_TO_BEZIER = 0.55228475; |
| 167 |
|
| 168 |
if (radius_x > w - radius_x) |
| 169 |
radius_x = w / 2; |
| 170 |
|
| 171 |
if (radius_y > h - radius_y) |
| 172 |
radius_y = h / 2; |
| 173 |
|
| 174 |
/* approximate (quite close) the arc using a bezier curve */ |
| 175 |
double ca = ARC_TO_BEZIER * radius_x; |
| 176 |
double cb = ARC_TO_BEZIER * radius_y; |
| 177 |
|
| 178 |
cr.new_path (); |
| 179 |
cr.set_line_width (0.7); |
| 180 |
cr.set_tolerance (0.1); |
| 181 |
cr.move_to (x + radius_x, y); |
| 182 |
cr.rel_line_to (w - 2 * radius_x, 0.0); |
| 183 |
cr.rel_curve_to (ca, 0.0, radius_x, cb, radius_x, radius_y); |
| 184 |
cr.rel_line_to (0, h - 2 * radius_y); |
| 185 |
cr.rel_curve_to (0.0, cb, ca - radius_x, radius_y, -radius_x, radius_y); |
| 186 |
cr.rel_line_to (-w + 2 * radius_x, 0); |
| 187 |
cr.rel_curve_to (-ca, 0, -radius_x, -cb, -radius_x, -radius_y); |
| 188 |
cr.rel_line_to (0, -h + 2 * radius_y); |
| 189 |
cr.rel_curve_to (0.0, -cb, radius_x - ca, -radius_y, radius_x, -radius_y); |
| 190 |
|
| 191 |
switch (style) { |
| 192 |
default: |
| 193 |
case "fill": |
| 194 |
Gdk.RGBA rgba = Gdk.RGBA (); |
| 195 |
rgba.parse (color); |
| 196 |
Gdk.cairo_set_source_rgba (cr, rgba); |
| 197 |
cr.fill (); |
| 198 |
break; |
| 199 |
case "stroke": |
| 200 |
cr.set_source_rgba (0, 0, 0, 0.5); |
| 201 |
cr.stroke (); |
| 202 |
break; |
| 203 |
} |
| 204 |
|
| 205 |
cr.close_path (); |
| 206 |
} |
| 207 |
|
| 208 |
/* |
| 209 |
* Draw the overlaying gradient |
| 210 |
*/ |
| 211 |
private void DrawGradientOverlay (Cairo.Context cr, int x, int y, int w, int h) { |
| 212 |
var radial = new Cairo.Pattern.radial (w, h, 1, 0.0, 0.0, 0.0); |
| 213 |
radial.add_color_stop_rgba (0, 0.3, 0.3, 0.3, 0.0); |
| 214 |
radial.add_color_stop_rgba (1, 0.0, 0.0, 0.0, 0.5); |
| 215 |
|
| 216 |
cr.set_source (radial); |
| 217 |
cr.rectangle (x, y, w, h); |
| 218 |
cr.fill (); |
| 219 |
} |
| 220 |
|
| 221 |
} |