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
|
/*
* Inkscape::UI::Widget::UnitTracker
* Simple mediator to synchronize changes to unit menus
*
* Authors:
* Jon A. Cruz <jon@joncruz.org>
* Matthew Petroff <matthew@mpetroff.net>
*
* Copyright (C) 2007 Jon A. Cruz
* Copyright (C) 2013 Matthew Petroff
* Copyright (C) 2018 Tavmjong Bah
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include <algorithm>
#include <iostream>
#include "unit-tracker.h"
#include "ink-select-one-action.h"
#define COLUMN_STRING 0
using Inkscape::Util::UnitTable;
using Inkscape::Util::unit_table;
namespace Inkscape {
namespace UI {
namespace Widget {
UnitTracker::UnitTracker(UnitType unit_type) :
_active(0),
_isUpdating(false),
_activeUnit(nullptr),
_activeUnitInitialized(false),
_store(nullptr),
_priorValues()
{
UnitTable::UnitMap m = unit_table.units(unit_type);
InkSelectOneActionColumns columns;
_store = Gtk::ListStore::create(columns);
Gtk::TreeModel::Row row;
for (UnitTable::UnitMap::iterator m_iter = m.begin(); m_iter != m.end(); ++m_iter) {
Glib::ustring unit = m_iter->first;
row = *(_store->append());
row[columns.col_label ] = unit;
row[columns.col_tooltip ] = ("");
row[columns.col_icon ] = "NotUsed";
row[columns.col_sensitive] = true;
}
// Why?
gint count = _store->children().size();
if ((count > 0) && (_active > count)) {
_setActive(--count);
} else {
_setActive(_active);
}
}
UnitTracker::~UnitTracker()
{
_actionList.clear();
// Unhook weak references to GtkAdjustments
for (auto i : _adjList) {
g_object_weak_unref(G_OBJECT(i), _adjustmentFinalizedCB, this);
}
_adjList.clear();
}
bool UnitTracker::isUpdating() const
{
return _isUpdating;
}
Inkscape::Util::Unit const * UnitTracker::getActiveUnit() const
{
return _activeUnit;
}
void UnitTracker::setActiveUnit(Inkscape::Util::Unit const *unit)
{
if (unit) {
InkSelectOneActionColumns columns;
int index = 0;
for (auto& row: _store->children() ) {
Glib::ustring storedUnit = row[columns.col_label];
if (!unit->abbr.compare (storedUnit)) {
_setActive (index);
break;
}
index++;
}
}
}
void UnitTracker::setActiveUnitByAbbr(gchar const *abbr)
{
Inkscape::Util::Unit const *u = unit_table.getUnit(abbr);
setActiveUnit(u);
}
void UnitTracker::addAdjustment(GtkAdjustment *adj)
{
if (std::find(_adjList.begin(),_adjList.end(),adj) == _adjList.end()) {
g_object_weak_ref(G_OBJECT(adj), _adjustmentFinalizedCB, this);
_adjList.push_back(adj);
} else {
std::cerr << "UnitTracker::addAjustment: Ajustment already added!" << std::endl;
}
}
void UnitTracker::addUnit(Inkscape::Util::Unit const *u)
{
InkSelectOneActionColumns columns;
Gtk::TreeModel::Row row;
row = *(_store->append());
row[columns.col_label ] = u ? u->abbr.c_str() : "";
row[columns.col_tooltip ] = ("");
row[columns.col_icon ] = "NotUsed";
row[columns.col_sensitive] = true;
}
void UnitTracker::prependUnit(Inkscape::Util::Unit const *u)
{
InkSelectOneActionColumns columns;
Gtk::TreeModel::Row row;
row = *(_store->prepend());
row[columns.col_label ] = u ? u->abbr.c_str() : "";
row[columns.col_tooltip ] = ("");
row[columns.col_icon ] = "NotUsed";
row[columns.col_sensitive] = true;
/* Re-shuffle our default selection here (_active gets out of sync) */
setActiveUnit(_activeUnit);
}
void UnitTracker::setFullVal(GtkAdjustment *adj, gdouble val)
{
_priorValues[adj] = val;
}
InkSelectOneAction *UnitTracker::createAction(Glib::ustring const &name,
Glib::ustring const &label,
Glib::ustring const &tooltip)
{
InkSelectOneAction* act =
InkSelectOneAction::create( name, label, tooltip, "NotUsed", _store);
act->use_radio( false );
act->use_label( true );
act->use_icon( false );
act->use_group_label( false );
act->set_active( _active );
act->signal_changed().connect(sigc::mem_fun(*this, &UnitTracker::_unitChangedCB));
_actionList.push_back(act);
return act;
}
void UnitTracker::_unitChangedCB(int active)
{
_setActive(active);
}
void UnitTracker::_actionFinalizedCB(gpointer data, GObject *where_the_object_was)
{
if (data && where_the_object_was) {
UnitTracker *self = reinterpret_cast<UnitTracker *>(data);
self->_actionFinalized(where_the_object_was);
}
}
void UnitTracker::_adjustmentFinalizedCB(gpointer data, GObject *where_the_object_was)
{
if (data && where_the_object_was) {
UnitTracker *self = reinterpret_cast<UnitTracker *>(data);
self->_adjustmentFinalized(where_the_object_was);
}
}
void UnitTracker::_actionFinalized(GObject *where_the_object_was)
{
InkSelectOneAction* act = (InkSelectOneAction*)(where_the_object_was);
auto it = std::find(_actionList.begin(),_actionList.end(), act);
if (it != _actionList.end()) {
_actionList.erase(it);
} else {
g_warning("Received a finalization callback for unknown object %p", where_the_object_was);
}
}
void UnitTracker::_adjustmentFinalized(GObject *where_the_object_was)
{
GtkAdjustment* adj = (GtkAdjustment*)(where_the_object_was);
auto it = std::find(_adjList.begin(),_adjList.end(), adj);
if (it != _adjList.end()) {
_adjList.erase(it);
} else {
g_warning("Received a finalization callback for unknown object %p", where_the_object_was);
}
}
void UnitTracker::_setActive(gint active)
{
if ( active != _active || !_activeUnitInitialized ) {
gint oldActive = _active;
if (_store) {
// Find old and new units
InkSelectOneActionColumns columns;
int index = 0;
Glib::ustring oldAbbr( "NotFound" );
Glib::ustring newAbbr( "NotFound" );
for (auto& row: _store->children() ) {
if (index == _active) {
oldAbbr = row[columns.col_label];
}
if (index == active) {
newAbbr = row[columns.col_label];
}
if (newAbbr != "NotFound" && oldAbbr != "NotFound") break;
++index;
}
if (oldAbbr != "NotFound") {
if (newAbbr != "NotFound") {
Inkscape::Util::Unit const *oldUnit = unit_table.getUnit(oldAbbr);
Inkscape::Util::Unit const *newUnit = unit_table.getUnit(newAbbr);
_activeUnit = newUnit;
if (!_adjList.empty()) {
_fixupAdjustments(oldUnit, newUnit);
}
} else {
std::cerr << "UnitTracker::_setActive: Did not find new unit: " << active << std::endl;
}
} else {
std::cerr << "UnitTracker::_setActive: Did not find old unit: " << oldActive
<< " new: " << active << std::endl;
}
}
_active = active;
for (auto act: _actionList) {
act->set_active (active);
}
_activeUnitInitialized = true;
}
}
void UnitTracker::_fixupAdjustments(Inkscape::Util::Unit const *oldUnit, Inkscape::Util::Unit const *newUnit)
{
_isUpdating = true;
for ( auto adj : _adjList ) {
gdouble oldVal = gtk_adjustment_get_value(adj);
gdouble val = oldVal;
if ( (oldUnit->type != Inkscape::Util::UNIT_TYPE_DIMENSIONLESS)
&& (newUnit->type == Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) )
{
val = newUnit->factor * 100;
_priorValues[adj] = Inkscape::Util::Quantity::convert(oldVal, oldUnit, "px");
} else if ( (oldUnit->type == Inkscape::Util::UNIT_TYPE_DIMENSIONLESS)
&& (newUnit->type != Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) )
{
if (_priorValues.find(adj) != _priorValues.end()) {
val = Inkscape::Util::Quantity::convert(_priorValues[adj], "px", newUnit);
}
} else {
val = Inkscape::Util::Quantity::convert(oldVal, oldUnit, newUnit);
}
gtk_adjustment_set_value(adj, val);
}
_isUpdating = false;
}
} // namespace Widget
} // namespace UI
} // namespace Inkscape
/*
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:fileencoding=utf-8 :
|