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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Gio::Actions for changing the canvas display mode. Tied to a particular InkscapeWindow.
*
* Copyright (C) 2020 Tavmjong Bah
*
* The contents of this file may be used under the GNU General Public License Version 2 or later.
*
*/
#include <iostream>
#include <giomm.h> // Not <gtkmm.h>! To eventually allow a headless version!
#include <glibmm/i18n.h>
#include "ui/interface.h"
#include "ui/view/view.h"
#include "actions-canvas-mode.h"
#include "inkscape-application.h"
#include "inkscape-window.h"
#include "display/rendermode.h"
#include "display/drawing.h" // Setting gray scale parameters.
#include "display/control/canvas-item-drawing.h"
#include "ui/widget/canvas.h"
// TODO: Use action state rather than set variable in Canvas (via Desktop).
// TODO: Move functions from Desktop to Canvas.
// TODO: Canvas actions should belong to canvas (not window)!
/**
* Helper function to set display mode.
*/
void
canvas_set_display_mode(Inkscape::RenderMode value, InkscapeWindow *win, Glib::RefPtr<Gio::SimpleAction> saction)
{
g_assert(value != Inkscape::RenderMode::size);
saction->change_state((int)value);
// Save value as a preference
Inkscape::Preferences *pref = Inkscape::Preferences::get();
pref->setInt("/options/displaymode", (int)value);
SPDesktop* dt = win->get_desktop();
auto canvas = dt->getCanvas();
canvas->set_render_mode(Inkscape::RenderMode(value));
}
/**
* Set display mode.
*/
void
canvas_display_mode(int value, InkscapeWindow *win)
{
if (value < 0 || value >= (int)Inkscape::RenderMode::size) {
std::cerr << "canvas_display_mode: value out of bound! : " << value << std::endl;
return;
}
auto action = win->lookup_action("canvas-display-mode");
if (!action) {
std::cerr << "canvas_display_mode: action 'canvas-display-mode' missing!" << std::endl;
return;
}
auto saction = Glib::RefPtr<Gio::SimpleAction>::cast_dynamic(action);
if (!saction) {
std::cerr << "canvas_display_mode: action 'canvas-display-mode' not SimpleAction!" << std::endl;
return;
}
canvas_set_display_mode(Inkscape::RenderMode(value), win, saction);
}
/**
* Cycle between values.
*/
void
canvas_display_mode_cycle(InkscapeWindow *win)
{
auto action = win->lookup_action("canvas-display-mode");
if (!action) {
std::cerr << "canvas_display_mode_cycle: action 'canvas-display-mode' missing!" << std::endl;
return;
}
auto saction = Glib::RefPtr<Gio::SimpleAction>::cast_dynamic(action);
if (!saction) {
std::cerr << "canvas_display_mode_cycle: action 'canvas-display-mode' not SimpleAction!" << std::endl;
return;
}
int value = -1;
saction->get_state(value);
value++;
value %= (int)Inkscape::RenderMode::size;
canvas_set_display_mode((Inkscape::RenderMode)value, win, saction);
}
/**
* Toggle between normal and last set other value.
*/
void
canvas_display_mode_toggle(InkscapeWindow *win)
{
auto action = win->lookup_action("canvas-display-mode");
if (!action) {
std::cerr << "canvas_display_mode_toggle: action 'canvas-display-mode' missing!" << std::endl;
return;
}
auto saction = Glib::RefPtr<Gio::SimpleAction>::cast_dynamic(action);
if (!saction) {
std::cerr << "canvas_display_mode_toogle: action 'canvas-display-mode' not SimpleAction!" << std::endl;
return;
}
static Inkscape::RenderMode old_value = Inkscape::RenderMode::OUTLINE;
int value = -1;
saction->get_state(value);
if (value == (int)Inkscape::RenderMode::NORMAL) {
canvas_set_display_mode(old_value, win, saction);
} else {
old_value = Inkscape::RenderMode(value);
canvas_set_display_mode(Inkscape::RenderMode::NORMAL, win, saction);
}
}
/**
* Set split mode.
*/
void
canvas_split_mode(int value, InkscapeWindow *win)
{
if (value < 0 || value >= (int)Inkscape::SplitMode::size) {
std::cerr << "canvas_split_mode: value out of bound! : " << value << std::endl;
return;
}
auto action = win->lookup_action("canvas-split-mode");
if (!action) {
std::cerr << "canvas_split_mode: action 'canvas-split-mode' missing!" << std::endl;
return;
}
auto saction = Glib::RefPtr<Gio::SimpleAction>::cast_dynamic(action);
if (!saction) {
std::cerr << "canvas_split_mode: action 'canvas-split-mode' not SimpleAction!" << std::endl;
return;
}
// If split mode is already set to the requested mode, turn it off.
int old_value = -1;
saction->get_state(old_value);
if (value == old_value) {
value = (int)Inkscape::SplitMode::NORMAL;
}
saction->change_state(value);
SPDesktop* dt = win->get_desktop();
auto canvas = dt->getCanvas();
canvas->set_split_mode(Inkscape::SplitMode(value));
}
/**
* Set gray scale for canvas.
*/
void
canvas_color_mode_gray(InkscapeWindow *win)
{
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
gdouble r = prefs->getDoubleLimited("/options/rendering/grayscale/red-factor", 0.21, 0.0, 1.0);
gdouble g = prefs->getDoubleLimited("/options/rendering/grayscale/green-factor", 0.72, 0.0, 1.0);
gdouble b = prefs->getDoubleLimited("/options/rendering/grayscale/blue-factor", 0.072, 0.0, 1.0);
gdouble grayscale_value_matrix[20] =
{ r, g, b, 0, 0,
r, g, b, 0, 0,
r, g, b, 0, 0,
0, 0, 0, 1, 0 };
SPDesktop* dt = win->get_desktop();
dt->getCanvasDrawing()->get_drawing()->setGrayscaleMatrix(grayscale_value_matrix);
}
/**
* Toggle Gray scale on/off.
*/
void
canvas_color_mode_toggle(InkscapeWindow *win)
{
auto action = win->lookup_action("canvas-color-mode");
if (!action) {
std::cerr << "canvas_color_mode_toggle: action missing!" << std::endl;
return;
}
auto saction = Glib::RefPtr<Gio::SimpleAction>::cast_dynamic(action);
if (!saction) {
std::cerr << "canvas_color_mode_toggle: action not SimpleAction!" << std::endl;
return;
}
bool state = false;
saction->get_state(state);
state = !state;
saction->change_state(state);
if (state) {
// Set gray scale parameters.
canvas_color_mode_gray(win);
}
SPDesktop* dt = win->get_desktop();
auto canvas = dt->getCanvas();
canvas->set_color_mode(state ? Inkscape::ColorMode::GRAYSCALE : Inkscape::ColorMode::NORMAL);
}
/**
* Toggle Color management on/off.
*/
void
canvas_color_manage_toggle(InkscapeWindow *win)
{
auto action = win->lookup_action("canvas-color-manage");
if (!action) {
std::cerr << "canvas_color_manage_toggle: action missing!" << std::endl;
return;
}
auto saction = Glib::RefPtr<Gio::SimpleAction>::cast_dynamic(action);
if (!saction) {
std::cerr << "canvas_color_manage_toggle: action not SimpleAction!" << std::endl;
return;
}
bool state = false;
saction->get_state(state);
state = !state;
saction->change_state(state);
// Save value as a preference
Inkscape::Preferences *pref = Inkscape::Preferences::get();
pref->setBool("/options/displayprofile/enable", state);
SPDesktop* dt = win->get_desktop();
auto canvas = dt->getCanvas();
canvas->set_cms_active(state);
canvas->redraw_all();
}
std::vector<std::vector<Glib::ustring>> raw_data_canvas_mode =
{
// clang-format off
{"win.canvas-display-mode(0)", N_("Display Mode: Normal"), "Canvas Display", N_("Use normal rendering mode") },
{"win.canvas-display-mode(1)", N_("Display Mode: Outline"), "Canvas Display", N_("Show only object outlines") },
{"win.canvas-display-mode(2)", N_("Display Mode: No Filters"), "Canvas Display", N_("Do not render filters (for speed)") },
{"win.canvas-display-mode(3)", N_("Display Mode: Hairlines"), "Canvas Display", N_("Render thin lines visibly") },
{"win.canvas-display-mode-cycle", N_("Display Mode Cycle"), "Canvas Display", N_("Cycle through display modes") },
{"win.canvas-display-mode-toggle", N_("Display Mode Toggle"), "Canvas Display", N_("Toggle between normal and last non-normal mode") },
{"win.canvas-split-mode(0)", N_("Split Mode: Normal"), "Canvas Display", N_("Do not split canvas") },
{"win.canvas-split-mode(1)", N_("Split Mode: Split"), "Canvas Display", N_("Render part of the canvas in outline mode") },
{"win.canvas-split-mode(2)", N_("Split Mode: X-Ray"), "Canvas Display", N_("Render a circular area in outline mode") },
{"win.canvas-color-mode", N_("Color Mode"), "Canvas Display", N_("Toggle between normal and grayscale modes") },
{"win.canvas-color-manage", N_("Color Managed Mode"), "Canvas Display", N_("Toggle between normal and color managed modes") }
// clang-format on
};
void
add_actions_canvas_mode(InkscapeWindow* win)
{
// Sync action with desktop variables. TODO: Remove!
auto prefs = Inkscape::Preferences::get();
// Initial States of Actions
int display_mode = prefs->getIntLimited("/options/displaymode", 0, 0, 4); // Default, minimum, maximum
bool color_manage = prefs->getBool("/options/displayprofile/enable");
SPDesktop* dt = win->get_desktop();
if (dt) {
auto canvas = dt->getCanvas();
canvas->set_render_mode(Inkscape::RenderMode(display_mode));
canvas->set_cms_active(color_manage);
} else {
std::cerr << "add_actions_canvas_mode: no desktop!" << std::endl;
}
// clang-format off
win->add_action_radio_integer ("canvas-display-mode", sigc::bind<InkscapeWindow*>(sigc::ptr_fun(&canvas_display_mode), win), display_mode);
win->add_action( "canvas-display-mode-cycle", sigc::bind<InkscapeWindow*>(sigc::ptr_fun(&canvas_display_mode_cycle), win));
win->add_action( "canvas-display-mode-toggle", sigc::bind<InkscapeWindow*>(sigc::ptr_fun(&canvas_display_mode_toggle), win));
win->add_action_radio_integer ("canvas-split-mode", sigc::bind<InkscapeWindow*>(sigc::ptr_fun(&canvas_split_mode), win), (int)Inkscape::SplitMode::NORMAL);
win->add_action_bool( "canvas-color-mode", sigc::bind<InkscapeWindow*>(sigc::ptr_fun(&canvas_color_mode_toggle), win));
win->add_action_bool( "canvas-color-manage", sigc::bind<InkscapeWindow*>(sigc::ptr_fun(&canvas_color_manage_toggle), win), color_manage);
// clang-format on
auto app = InkscapeApplication::instance();
if (!app) {
std::cerr << "add_actions_canvas_mode: no app!" << std::endl;
return;
}
app->get_action_extra_data().add_data(raw_data_canvas_mode);
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
|