-
Notifications
You must be signed in to change notification settings - Fork 265
/
StatusBarColorPaletteWidget.cs
294 lines (240 loc) · 10.3 KB
/
StatusBarColorPaletteWidget.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//
// StatusBarColorPaletteWidget.cs
//
// Author:
// Jonathan Pobst <monkey@jpobst.com>
//
// Copyright (c) 2020 Jonathan Pobst
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Linq;
using Cairo;
using Pinta.Core;
namespace Pinta.Gui.Widgets
{
public class StatusBarColorPaletteWidget : Gtk.DrawingArea
{
private Rectangle primary_rect = new Rectangle (4, 3, 24, 24);
private Rectangle secondary_rect = new Rectangle (17, 16, 24, 24);
private Rectangle swap_rect = new Rectangle (27, 2, 15, 15);
private Rectangle reset_rect = new Rectangle (2, 27, 15, 15);
private Rectangle palette_rect;
private Rectangle recent_palette_rect;
const int PALETTE_ROWS = 2;
const int SWATCH_SIZE = 19;
const int WIDGET_HEIGHT = 42;
const int PALETTE_MARGIN = 10;
private readonly Gdk.Pixbuf swap_icon;
private readonly Gdk.Pixbuf reset_icon;
public StatusBarColorPaletteWidget ()
{
AddEvents ((int) Gdk.EventMask.ButtonPressMask);
swap_icon = PintaCore.Resources.GetIcon ("ColorPalette.SwapIcon.png");
reset_icon = PintaCore.Resources.GetIcon ("ColorPalette.ResetIcon.png");
HasTooltip = true;
QueryTooltip += HandleQueryTooltip;
PintaCore.Palette.PrimaryColorChanged += new EventHandler (Palette_ColorChanged);
PintaCore.Palette.SecondaryColorChanged += new EventHandler (Palette_ColorChanged);
PintaCore.Palette.RecentColorsChanged += new EventHandler (Palette_ColorChanged);
PintaCore.Palette.CurrentPalette.PaletteChanged += new EventHandler (Palette_ColorChanged);
HeightRequest = WIDGET_HEIGHT;
}
protected override bool OnButtonPressEvent (Gdk.EventButton ev)
{
var point = ev.GetPoint ();
switch (GetElementAtPoint (point)) {
case WidgetElement.PrimaryColor:
PintaCore.Palette.PrimaryColor = GetUserChosenColor (PintaCore.Palette.PrimaryColor, Translations.GetString ("Choose Primary Color"));
break;
case WidgetElement.SecondaryColor:
PintaCore.Palette.SecondaryColor = GetUserChosenColor (PintaCore.Palette.SecondaryColor, Translations.GetString ("Choose Secondary Color"));
break;
case WidgetElement.SwapColors:
var temp = PintaCore.Palette.PrimaryColor;
// Swapping should not trigger adding colors to recently used palette
PintaCore.Palette.SetColor (true, PintaCore.Palette.SecondaryColor, false);
PintaCore.Palette.SetColor (false, temp, false);
break;
case WidgetElement.ResetColors:
PintaCore.Palette.PrimaryColor = new Color (0, 0, 0);
PintaCore.Palette.SecondaryColor = new Color (1, 1, 1);
break;
case WidgetElement.Palette:
var index = GetSwatchAtLocation (point);
if (index < 0)
break;
if (ev.Button == 3)
PintaCore.Palette.SecondaryColor = PintaCore.Palette.CurrentPalette[index];
else if (ev.Button == 1)
PintaCore.Palette.PrimaryColor = PintaCore.Palette.CurrentPalette[index];
else
PintaCore.Palette.CurrentPalette[index] = GetUserChosenColor (PintaCore.Palette.CurrentPalette[index], Translations.GetString ("Choose Palette Color"));
break;
case WidgetElement.RecentColorsPalette:
var recent_index = GetSwatchAtLocation (point, true);
if (recent_index < 0)
break;
if (ev.Button == 3)
PintaCore.Palette.SetColor (false, PintaCore.Palette.RecentlyUsedColors.ElementAt (recent_index), false);
else if (ev.Button == 1)
PintaCore.Palette.SetColor (true, PintaCore.Palette.RecentlyUsedColors.ElementAt (recent_index), false);
break;
}
return base.OnButtonPressEvent (ev);
}
protected override bool OnDrawn (Context g)
{
base.OnDrawn (g);
// Draw Secondary color swatch
g.FillRectangle (secondary_rect, PintaCore.Palette.SecondaryColor);
g.DrawRectangle (new Rectangle (secondary_rect.X + 1, secondary_rect.Y + 1, secondary_rect.Width - 2, secondary_rect.Height - 2), new Color (1, 1, 1), 1);
g.DrawRectangle (secondary_rect, new Color (0, 0, 0), 1);
// Draw Primary color swatch
g.FillRectangle (primary_rect, PintaCore.Palette.PrimaryColor);
g.DrawRectangle (new Rectangle (primary_rect.X + 1, primary_rect.Y + 1, primary_rect.Width - 2, primary_rect.Height - 2), new Color (1, 1, 1), 1);
g.DrawRectangle (primary_rect, new Color (0, 0, 0), 1);
// Draw swatch icons
g.DrawPixbuf (swap_icon, swap_rect.Location ());
g.DrawPixbuf (reset_icon, reset_rect.Location ());
// Draw recently used color swatches
var recent = PintaCore.Palette.RecentlyUsedColors;
for (var i = 0; i < recent.Count (); i++)
g.FillRectangle (GetSwatchBounds (i, true), recent.ElementAt (i));
// Draw color swatches
var palette = PintaCore.Palette.CurrentPalette;
for (var i = 0; i < palette.Count; i++)
g.FillRectangle (GetSwatchBounds (i), palette[i]);
return true;
}
protected override void OnSizeAllocated (Gdk.Rectangle allocation)
{
base.OnSizeAllocated (allocation);
// Store the bounds allocated for our palette
var recent_cols = PintaCore.Palette.MaxRecentlyUsedColor / PALETTE_ROWS;
recent_palette_rect = new Rectangle (50, 2, SWATCH_SIZE * recent_cols, SWATCH_SIZE * PALETTE_ROWS);
palette_rect = new Rectangle (recent_palette_rect.GetRight () + PALETTE_MARGIN, 2, AllocatedWidth - recent_palette_rect.GetRight () - PALETTE_MARGIN, SWATCH_SIZE * PALETTE_ROWS);
}
private Rectangle GetSwatchBounds (int index, bool recentColorPalette = false)
{
// Normal swatches are layed out like this:
// 0 | 2 | 4 | 6
// 1 | 3 | 5 | 7
// Recent swatches are layed out like this (it's less visually jarring as they change):
// 0 | 1 | 2 | 3
// 4 | 5 | 6 | 7
// First we need to figure out what row and column the color is
var recent_cols = PintaCore.Palette.MaxRecentlyUsedColor / PALETTE_ROWS;
var row = recentColorPalette ? index / recent_cols : index % PALETTE_ROWS;
var col = recentColorPalette ? index % recent_cols : index / PALETTE_ROWS;
// Now we need to construct the bounds of that row/column
var palette_bounds = recentColorPalette ? recent_palette_rect : palette_rect;
var x = palette_bounds.X + (col * SWATCH_SIZE);
var y = palette_bounds.Y + (row * SWATCH_SIZE);
return new Rectangle (x, y, SWATCH_SIZE, SWATCH_SIZE);
}
private int GetSwatchAtLocation (PointD point, bool recentColorPalette = false)
{
var max = recentColorPalette ? PintaCore.Palette.RecentlyUsedColors.Count () : PintaCore.Palette.CurrentPalette.Count;
// This could be more efficient, but is good enough for now
for (var i = 0; i < max; i++)
if (GetSwatchBounds (i, recentColorPalette).ContainsPoint (point))
return i;
return -1;
}
/// <summary>
/// Provide a custom tooltip based on the cursor location.
/// </summary>
private void HandleQueryTooltip (object o, Gtk.QueryTooltipArgs args)
{
string? text = null;
var point = new PointD (args.X, args.Y);
switch (GetElementAtPoint (point)) {
case WidgetElement.Palette:
if (GetSwatchAtLocation (point) >= 0)
text = Translations.GetString ("Left click to set primary color. Right click to set secondary color. Middle click to choose palette color.");
break;
case WidgetElement.RecentColorsPalette:
if (GetSwatchAtLocation (point, true) >= 0)
text = Translations.GetString ("Left click to set primary color. Right click to set secondary color.");
break;
case WidgetElement.PrimaryColor:
text = Translations.GetString ("Click to select primary color.");
break;
case WidgetElement.SecondaryColor:
text = Translations.GetString ("Click to select secondary color.");
break;
case WidgetElement.SwapColors:
var label = Translations.GetString ("Click to switch between primary and secondary color.");
var shortcut_label = Translations.GetString ("Shortcut key");
text = $"{label} {shortcut_label}: {"X"}";
break;
case WidgetElement.ResetColors:
text = Translations.GetString ("Click to reset primary and secondary color.");
break;
}
args.Tooltip.Text = text;
args.RetVal = (text != null);
}
private void Palette_ColorChanged (object? sender, EventArgs e)
{
// Color change events may be received while the widget is minimized,
// so we only call Invalidate() if the widget is shown.
if (IsRealized)
Window.Invalidate ();
}
private Color GetUserChosenColor (Color initialColor, string title)
{
using (var ccd = new Gtk.ColorChooserDialog (title, PintaCore.Chrome.MainWindow)) {
ccd.UseAlpha = true;
ccd.Rgba = initialColor.ToGdkRGBA ();
var response = (Gtk.ResponseType) ccd.Run ();
if (response == Gtk.ResponseType.Ok)
return ccd.Rgba.ToCairoColor ();
return initialColor;
}
}
private WidgetElement GetElementAtPoint (PointD point)
{
if (palette_rect.ContainsPoint (point))
return WidgetElement.Palette;
if (recent_palette_rect.ContainsPoint (point))
return WidgetElement.RecentColorsPalette;
if (primary_rect.ContainsPoint (point))
return WidgetElement.PrimaryColor;
if (secondary_rect.ContainsPoint (point))
return WidgetElement.SecondaryColor;
if (swap_rect.ContainsPoint (point))
return WidgetElement.SwapColors;
if (reset_rect.ContainsPoint (point))
return WidgetElement.ResetColors;
return WidgetElement.Nothing;
}
private enum WidgetElement
{
Nothing,
Palette,
RecentColorsPalette,
PrimaryColor,
SecondaryColor,
SwapColors,
ResetColors
}
}
}