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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
|
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
* @brief A dialog for CSS selectors
*/
/* Authors:
* Kamalpreet Kaur Grewal
* Tavmjong Bah
*
* Copyright (C) Kamalpreet Kaur Grewal 2016 <grewalkamal005@gmail.com>
* Copyright (C) Tavmjong Bah 2017 <tavmjong@free.fr>
*
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
*/
#include "styledialog.h"
#include "message-context.h"
#include "message-stack.h"
#include "selection.h"
#include "style-internal.h"
#include "style.h"
#include "ui/icon-loader.h"
#include "ui/widget/iconrenderer.h"
#include "verbs.h"
#include "xml/attribute-record.h"
#include "xml/node-event-vector.h"
#include <glibmm/i18n.h>
#include <glibmm/i18n.h>
static void on_attr_changed(Inkscape::XML::Node *repr, const gchar *name, const gchar * /*old_value*/,
const gchar *new_value, bool /*is_interactive*/, gpointer data)
{
CSS_DIALOG(data)->onAttrChanged(repr, name, new_value);
CSS_DIALOG(data)->styledialog = new Inkscape::UI::Dialog::StyleDialog();
}
Inkscape::XML::NodeEventVector css_repr_events = {
nullptr, /* child_added */
nullptr, /* child_removed */
on_attr_changed, nullptr, /* content_changed */
nullptr /* order_changed */
};
namespace Inkscape {
namespace UI {
namespace Dialog {
/**
* Constructor
* A treeview whose each row corresponds to a CSS property of selector selected.
* New CSS property can be added by clicking '+' at bottom of the CSS pane. '-'
* in front of the CSS property row can be clicked to delete the CSS property.
* Besides clicking on an already selected property row makes the property editable
* and clicking 'Enter' updates the property with changes reflected in the
* drawing.
*/
StyleDialog::StyleDialog(bool stylemode) :
UI::Widget::Panel("/dialogs/style", SP_VERB_DIALOG_STYLE),
_updating(false),
_textNode(nullptr),
_desktopTracker(),
_stylemode(stylemode)
{
set_size_request(20, 15);
_treeView.set_headers_visible(true);
_treeView.enable_model_drag_source();
_treeView.enable_model_drag_dest( Gdk::ACTION_MOVE );
int addCol = _treeView.append_column("", *addRenderer) - 1;
Gtk::TreeViewColumn *col = _treeView.get_column(addCol);
if ( col ) {
col->add_attribute(addRenderer->property_icon(), _mColumns._colType);
}
_treeView.append_column("CSS Selector", _mColumns._colSelector);
_treeView.set_expander_column(*(_treeView.get_column(1)));
// Pack widgets
_paned.set_orientation(Gtk::ORIENTATION_VERTICAL);
_paned.pack1(_mainBox, Gtk::SHRINK);
_mainBox.set_orientation(Gtk::ORIENTATION_VERTICAL);
if (!_stylemode) {
_mainBox.pack_start(_scrolledWindow, Gtk::PACK_EXPAND_WIDGET);
_scrolledWindow.add(_treeView);
_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
create = manage( new Gtk::Button() );
_styleButton(*create, "list-add", "Add a new CSS Selector");
create->signal_clicked().connect(sigc::mem_fun(*this, &StyleDialog::_addSelector));
del = manage( new Gtk::Button() );
_styleButton(*del, "list-remove", "Remove a CSS Selector");
del->signal_clicked().connect(sigc::mem_fun(*this, &StyleDialog::_delSelector));
del->set_sensitive(false);
_mainBox.pack_end(_buttonBox, Gtk::PACK_SHRINK);
_buttonBox.pack_start(*create, Gtk::PACK_SHRINK);
_buttonBox.pack_start(*del, Gtk::PACK_SHRINK);
} else {
_mainBox.pack_start(_treeView, Gtk::PACK_EXPAND_WIDGET);
}
_getContents()->pack_start(_paned, Gtk::PACK_EXPAND_WIDGET);
// Signal handlers
_treeView.signal_button_release_event().connect( // Needs to be release, not press.
sigc::mem_fun(*this, &StyleDialog::_handleButtonEvent),
false);
_treeView.signal_button_release_event().connect_notify(
sigc::mem_fun(*this, &StyleDialog::_buttonEventsSelectObjs),
false);
_treeView.signal_row_expanded().connect(sigc::mem_fun(*this, &StyleDialog::_rowExpand));
_treeView.signal_row_collapsed().connect(sigc::mem_fun(*this, &StyleDialog::_rowCollapse));
// Document & Desktop
_desktop_changed_connection = _desktopTracker.connectDesktopChanged(
sigc::mem_fun(*this, &StyleDialog::_handleDesktopChanged) );
_desktopTracker.connect(GTK_WIDGET(gobj()));
_document_replaced_connection = getDesktop()->connectDocumentReplaced(
sigc::mem_fun(this, &StyleDialog::_handleDocumentReplaced));
_selection_changed_connection = getDesktop()->getSelection()->connectChanged(
sigc::hide(sigc::mem_fun(this, &StyleDialog::_handleSelectionChanged)));
// Add watchers
_updateWatchers();
Inkscape::UI::Widget::IconRenderer *addRenderer = manage(new Inkscape::UI::Widget::IconRenderer());
addRenderer->add_icon("edit-delete");
addRenderer->signal_activated().connect(sigc::mem_fun(*this, &StyleDialog::onPropertyDelete));
if (!_stylemode && !_store->children().empty()) {
del->set_sensitive(true);
}
int addCol = _treeView.append_column("", *addRenderer) - 1;
Gtk::TreeViewColumn *col = _treeView.get_column(addCol);
if (col) {
col->add_attribute(addRenderer->property_visible(), _cssColumns.deleteButton);
Gtk::Image *add_icon = Gtk::manage(sp_get_icon_image("list-add", Gtk::ICON_SIZE_SMALL_TOOLBAR));
col->set_clickable(true);
col->set_widget(*add_icon);
add_icon->set_tooltip_text(_("Add a new style property"));
add_icon->show();
// This gets the GtkButton inside the GtkBox, inside the GtkAlignment, inside the GtkImage icon.
auto button = add_icon->get_parent()->get_parent()->get_parent();
// Assign the button event so that create happens BEFORE delete. If this code
// isn't in this exact way, the onAttrDelete is called when the header lines are pressed.
button->signal_button_release_event().connect(sigc::mem_fun(*this, &StyleDialog::onPropertyCreate), false);
}
Gtk::CellRendererText *renderer = Gtk::manage(new Gtk::CellRendererText());
_treeView.set_reorderable(false);
renderer->property_editable() = true;
int nameColNum = _treeView.append_column("Property", *renderer) - 1;
_propCol = _treeView.get_column(nameColNum);
if (_propCol) {
_propCol->add_attribute(renderer->property_text(), _cssColumns.label);
_propCol->add_attribute(renderer->property_foreground_rgba(), _cssColumns.label_color);
}
renderer->signal_edited().connect(sigc::mem_fun(*this, &CssDialog::nameEdited));
renderer = Gtk::manage(new Gtk::CellRendererText());
renderer->property_editable() = true;
int attrColNum = _treeView.append_column("Value", *renderer) - 1;
_attrCol = _treeView.get_column(attrColNum);
if (_attrCol) {
_attrCol->add_attribute(renderer->property_text(), _cssColumns._styleAttrVal);
_attrCol->add_attribute(renderer->property_foreground_rgba(), _cssColumns.attr_color);
}
renderer->signal_edited().connect(sigc::mem_fun(*this, &CssDialog::valueEdited));
renderer = Gtk::manage(new Gtk::CellRendererText());
renderer->property_editable() = true;
// Set the initial sort column (and direction) to place real attributes at the top.
_store->set_sort_column(_cssColumns.label, Gtk::SORT_ASCENDING);
_getContents()->pack_start(*_scrolledWindow, Gtk::PACK_EXPAND_WIDGET);
css_reset_context(0);
setDesktop(getDesktop());
}
/**
* @brief StyleDialog::~StyleDialog
* Class destructor
*/
StyleDialog::~StyleDialog()
{
setDesktop(nullptr);
_repr = nullptr;
_message_changed_connection.disconnect();
_message_context = nullptr;
_message_stack = nullptr;
_message_changed_connection.~connection();
}
void StyleDialog::_set_status_message(Inkscape::MessageType /*type*/, const gchar *message, GtkWidget *widget)
{
if (widget) {
gtk_label_set_markup(GTK_LABEL(widget), message ? message : "");
}
}
/**
* @brief StyleDialog::setDesktop
* @param desktop
* This function sets the 'desktop' for the CSS pane.
*/
void StyleDialog::setDesktop(SPDesktop* desktop)
{
g_debug("StyleDialog::_readStyleElement: updating %s", (_updating ? "true" : "false"));
if (_updating) return; // Don't read if we wrote style element.
_updating = true;
Inkscape::XML::Node * textNode = _getStyleTextNode();
if (textNode == nullptr) {
std::cerr << "StyleDialog::_readStyleElement: No text node!" << std::endl;
}
// Get content from style text node.
std::string content = (textNode->content() ? textNode->content() : "");
// Remove end-of-lines (check it works on Windoze).
content.erase(std::remove(content.begin(), content.end(), '\n'), content.end());
// Remove comments (/* xxx */)
while(content.find("/*") != std::string::npos) {
size_t start = content.find("/*");
content.erase(start, (content.find("*/", start) - start) +2);
}
// First split into selector/value chunks.
// An attempt to use Glib::Regex failed. A C++11 version worked but
// reportedly has problems on Windows. Using split_simple() is simpler
// and probably faster.
//
// Glib::RefPtr<Glib::Regex> regex1 =
// Glib::Regex::create("([^\\{]+)\\{([^\\{]+)\\}");
//
// Glib::MatchInfo minfo;
// regex1->match(content, minfo);
// Split on curly brackets. Even tokens are selectors, odd are values.
std::cout << content << std::endl;
std::cout << "aaaaaaaaaaaaaaaaaaaaaaaaaa" << std::endl;
std::vector<Glib::ustring> tokens = Glib::Regex::split_simple("[}{]", content);
// If text node is empty, return (avoids problem with negative below).
if (tokens.size() == 0) {
_updating = false;
return;
}
std::vector<std::pair<Glib::ustring, bool>> expanderstatus;
for (unsigned i = 0; i < tokens.size() - 1; i += 2) {
Glib::ustring selector = tokens[i];
REMOVE_SPACES(selector); // Remove leading/trailing spaces
for (auto &row : _store->children()) {
Glib::ustring selectorold = row[_mColumns._colSelector];
if (selectorold == selector) {
expanderstatus.emplace_back(selector, row[_mColumns._colExpand]);
}
}
}
_store->clear();
for (unsigned i = 0; i < tokens.size()-1; i += 2) {
Glib::ustring selector = tokens[i];
REMOVE_SPACES(selector); // Remove leading/trailing spaces
std::vector<Glib::ustring> tokensplus = Glib::Regex::split_simple("[,]+", selector);
coltype colType = SELECTOR;
for (auto tok : tokensplus) {
REMOVE_SPACES(tok);
if (tok.find(" ") != -1 || tok.erase(0, 1).find(".") != -1) {
colType = UNHANDLED;
}
}
// Get list of objects selector matches
std::vector<SPObject *> objVec = _getObjVec( selector );
Glib::ustring properties;
// Check to make sure we do have a value to match selector.
if ((i+1) < tokens.size()) {
properties = tokens[i+1];
} else {
std::cerr << "StyleDialog::_readStyleElement: Missing values "
"for last selector!" << std::endl;
}
REMOVE_SPACES(properties);
bool colExpand = false;
for (auto rowstatus : expanderstatus) {
if (selector == rowstatus.first) {
colExpand = rowstatus.second;
}
}
Gtk::TreeModel::Row row = *(_store->append());
row[_mColumns._colSelector] = selector;
row[_mColumns._colExpand] = colExpand;
row[_mColumns._colType] = colType;
row[_mColumns._colObj] = objVec;
row[_mColumns._colProperties] = properties;
if (colType == SELECTOR) {
// Add as children, objects that match selector.
for (auto &obj : objVec) {
Gtk::TreeModel::Row childrow = *(_store->append(row->children()));
childrow[_mColumns._colSelector] = "#" + Glib::ustring(obj->getId());
childrow[_mColumns._colExpand] = false;
childrow[_mColumns._colType] = OBJECT;
childrow[_mColumns._colObj] = std::vector<SPObject *>(1, obj);
childrow[_mColumns._colProperties] = ""; // Unused
}
}
}
_updating = false;
}
/**
* @brief StyleDialog::setRepr
*
* Set the internal xml object that I'm working on right now.
*/
void StyleDialog::setRepr(Inkscape::XML::Node *repr)
{
if (repr == _repr)
return;
if (_repr) {
_store->clear();
_repr->removeListenerByData(this);
Inkscape::GC::release(_repr);
_repr = nullptr;
}
_repr = repr;
if (repr) {
Inkscape::GC::anchor(_repr);
_repr->addListener(&css_repr_events, this);
_repr->synthesizeEvents(&css_repr_events, this);
}
}
/**
* @brief StyleDialog::parseStyle
*
* Convert a style string into a vector map. This should be moved to style.cpp
*
*/
std::map<Glib::ustring, Glib::ustring> StyleDialog::parseStyle(Glib::ustring style_string)
{
std::map<Glib::ustring, Glib::ustring> ret;
REMOVE_SPACES(style_string); // We'd use const, but we need to trip spaces
std::vector<Glib::ustring> props = r_props->split(style_string);
for (auto const token : props) {
if (token.empty())
break;
std::vector<Glib::ustring> pair = r_pair->split(token);
if (pair.size() > 1) {
ret[pair[0]] = pair[1];
}
}
return ret;
}
/**
* @brief StyleDialog::compileStyle
*
* Turn a vector map back into a style string.
*
*/
Glib::ustring StyleDialog::compileStyle(std::map<Glib::ustring, Glib::ustring> props)
{
auto ret = Glib::ustring("");
for (auto const pair : props) {
if (!pair.first.empty() && !pair.second.empty()) {
ret += pair.first;
ret += ":";
ret += pair.second;
ret += ";";
}
}
return ret;
}
/**
* @brief StyleDialog::onAttrChanged
*
* This is called when the XML has an updated attribute (we only care about style)
*/
void StyleDialog::onAttrChanged(Inkscape::XML::Node *repr, const gchar *name, const gchar *new_value)
{
if (strcmp(name, "style") != 0)
return;
// Clear the list and return if the new_value is empty
_store->clear();
if (!new_value || new_value[0] == 0)
return;
// Get the object's style attribute and it's calculated properties
SPDocument *document = this->_desktop->doc();
SPObject *obj = document->getObjectByRepr(repr);
// std::vector<SPIBase *> calc_prop = obj->style->properties();
// Get a dictionary lookup of the style in the attribute
std::map<Glib::ustring, Glib::ustring> attr_prop = parseStyle(new_value);
for (auto iter : obj->style->properties()) {
if (iter->style_src != SP_STYLE_SRC_UNSET) {
if (attr_prop.count(iter->name)) {
Gtk::TreeModel::Row row = *(_store->append());
// Delete is available to attribute properties only in attr mode.
row[_cssColumns.deleteButton] = iter->style_src == SP_STYLE_SRC_ATTRIBUTE;
row[_cssColumns.label] = iter->name;
row[_cssColumns._styleAttrVal] = attr_prop[iter->name];
row[_cssColumns.deleteButton] = true;
/* } else if (iter->style) {
Gtk::TreeModel::Row row = *(_store->append());
// Delete is available to attribute properties only in attr mode.
row[_cssColumns.deleteButton] = iter->style_src == SP_STYLE_SRC_ATTRIBUTE;
row[_cssColumns.label] = iter->name;
row[_cssColumns._styleAttrVal] = iter->get_value();
row[_cssColumns.label_color] = Gdk::RGBA("gray");
row[_cssColumns.attr_color] = Gdk::RGBA("gray");
row[_cssColumns.deleteButton] = false;
*/
}
}
}
}
/*
* Sets the CSSDialog status bar, depending on which attr is selected.
*/
void StyleDialog::css_reset_context(gint css)
{
g_debug("StyleDialog::_selectObjects: %d, %d", eventX, eventY);
getDesktop()->selection->clear();
Gtk::TreeViewColumn *col = _treeView.get_column(1);
Gtk::TreeModel::Path path;
int x2 = 0;
int y2 = 0;
// To do: We should be able to do this via passing in row.
if (_treeView.get_path_at_pos(eventX, eventY, path, col, x2, y2)) {
if (col == _treeView.get_column(1)) {
Gtk::TreeModel::iterator iter = _store->get_iter(path);
if (iter) {
Gtk::TreeModel::Row row = *iter;
Gtk::TreeModel::Children children = row.children();
if (children.empty() && !_stylemode) {
del->set_sensitive(true);
}
std::vector<SPObject *> objVec = row[_mColumns._colObj];
for (auto obj : objVec) {
getDesktop()->selection->add(obj);
}
}
}
}
}
/**
* @brief StyleDialog::setStyleProperty
*
* Set or delete a single property in the style attribute.
*/
bool StyleDialog::setStyleProperty(Glib::ustring name, Glib::ustring value)
{
g_debug("StyleDialog::_addSelector: Entrance");
// Store list of selected elements on desktop (not to be confused with selector).
Inkscape::Selection* selection = getDesktop()->getSelection();
std::vector<SPObject *> objVec( selection->objects().begin(),
selection->objects().end() );
// ==== Create popup dialog ====
Gtk::Dialog *textDialogPtr = new Gtk::Dialog();
textDialogPtr->add_button(_("Cancel"), Gtk::RESPONSE_CANCEL);
textDialogPtr->add_button(_("Add"), Gtk::RESPONSE_OK);
Gtk::Entry *textEditPtr = manage ( new Gtk::Entry() );
textEditPtr->signal_activate().connect(
sigc::bind<Gtk::Dialog *>(sigc::mem_fun(*this, &StyleDialog::_closeDialog), textDialogPtr));
textDialogPtr->get_content_area()->pack_start(*textEditPtr, Gtk::PACK_SHRINK);
Gtk::Label *textLabelPtr = manage ( new Gtk::Label(
_("Invalid entry: Not an id (#), class (.), or element CSS selector.")
) );
textDialogPtr->get_content_area()->pack_start(*textLabelPtr, Gtk::PACK_SHRINK);
/**
* By default, the entrybox contains 'Class1' as text. However, if object(s)
* is(are) selected and user clicks '+' at the bottom of dialog, the
* entrybox will have the id(s) of the selected objects as text.
*/
if (getDesktop()->getSelection()->isEmpty()) {
textEditPtr->set_text(".Class1");
} else {
textEditPtr->set_text(_getIdList(objVec));
}
Gtk::Requisition sreq1, sreq2;
textDialogPtr->get_preferred_size(sreq1, sreq2);
int minWidth = 200;
int minHeight = 100;
minWidth = (sreq2.width > minWidth ? sreq2.width : minWidth );
minHeight = (sreq2.height > minHeight ? sreq2.height : minHeight);
textDialogPtr->set_size_request(minWidth, minHeight);
textEditPtr->show();
textLabelPtr->hide();
textDialogPtr->show();
// ==== Get response ====
int result = -1;
bool invalid = true;
Glib::ustring selectorValue;
bool handled = true;
while (invalid) {
result = textDialogPtr->run();
if (result != Gtk::RESPONSE_OK) { // Cancel, close dialog, etc.
textDialogPtr->hide();
delete textDialogPtr;
return;
}
/**
* @brief selectorName
* This string stores selector name. The text from entrybox is saved as name
* for selector. If the entrybox is empty, the text (thus selectorName) is
* set to ".Class1"
*/
selectorValue = textEditPtr->get_text();
Glib::ustring firstWord = selectorValue.substr(0, selectorValue.find_first_of(" >+~"));
if (firstWord != selectorValue) {
handled = false;
}
if (!_stylemode) {
del->set_sensitive(true);
}
if (selectorValue[0] == '.' || selectorValue[0] == '#' || selectorValue[0] == '*' ||
SPAttributeRelSVG::isSVGElement(selectorValue)) {
invalid = false;
} else {
textLabelPtr->show();
}
} else if (properties.count(name)) {
// Delete value
properties.erase(name);
updated = true;
}
if (updated) {
auto new_styles = this->compileStyle(properties);
this->_repr->setAttribute("style", new_styles, false);
// this->setUndo(_("Delete style property"));
}
return updated;
}
/**
* @brief StyleDialog::onPropertyDelete
*
* This function is a slot to signal_activated for '-' button panel.
*/
void StyleDialog::onPropertyDelete(Glib::ustring path)
{
g_debug("StyleDialog::_delSelector");
Glib::RefPtr<Gtk::TreeSelection> refTreeSelection = _treeView.get_selection();
_treeView.get_selection()->set_mode(Gtk::SELECTION_SINGLE);
Gtk::TreeModel::iterator iter = refTreeSelection->get_selected();
if (iter) {
Gtk::TreeModel::Row row = *iter;
if (!row.children().empty()) {
return;
}
_updating = true;
_store->erase(iter);
_updating = false;
_writeStyleElement();
if (!_stylemode) {
del->set_sensitive(false);
}
}
}
/**
* @brief StyleDialog::onPropertyCreate
* This function is a slot to signal_clicked for '+' button panel.
*/
bool StyleDialog::onPropertyCreate(GdkEventButton *event)
{
if (event->type == GDK_BUTTON_RELEASE && event->button == 1 && this->_repr) {
Gtk::TreeIter iter = _store->append();
Gtk::TreeModel::Path path = (Gtk::TreeModel::Path)iter;
_treeView.set_cursor(path, *_propCol, true);
grab_focus();
return true;
}
return false;
}
/**
* @brief CssDialog::onKeyPressed
* @param event_description
* @return
* Send an undo message and mark this point for undo
*/
void CssDialog::setUndo(Glib::ustring const &event_description)
{
SPDocument *document = this->_desktop->doc();
DocumentUndo::done(document, SP_VERB_DIALOG_XML_EDITOR, event_description);
}
/**
* @brief CssDialog::nameEdited
* @param event
* This function detects single or double click on a selector in any row. Clicking
* on a selector selects the matching objects on the desktop. A double click will
* in addition open the CSS dialog.
*/
void StyleDialog::_buttonEventsSelectObjs(GdkEventButton* event )
{
g_debug("StyleDialog::_buttonEventsSelectObjs");
_treeView.get_selection()->set_mode(Gtk::SELECTION_SINGLE);
_updating = true;
if (!_stylemode) {
del->set_sensitive(true);
}
if (event->type == GDK_BUTTON_RELEASE && event->button == 1) {
int x = static_cast<int>(event->x);
int y = static_cast<int>(event->y);
_selectObjects(x, y);
}
_updating = false;
}
/**
* @brief StyleDialog::_selectRow
* This function selects the row in treeview corresponding to an object selected
* in the drawing. If more than one row matches, the first is chosen.
*/
void CssDialog::nameEdited (const Glib::ustring& path, const Glib::ustring& name)
{
g_debug("StyleDialog::_selectRow: updating: %s", (_updating ? "true" : "false"));
if (!_stylemode) {
del->set_sensitive(false);
}
if (_updating || !getDesktop()) return; // Avoid updating if we have set row via dialog.
if (SP_ACTIVE_DESKTOP != getDesktop()) {
std::cerr << "StyleDialog::_selectRow: SP_ACTIVE_DESKTOP != getDesktop()" << std::endl;
return;
}
_treeView.get_selection()->unselect_all();
Gtk::TreeModel::Children children = _store->children();
Inkscape::Selection* selection = getDesktop()->getSelection();
SPObject *obj = nullptr;
if (!selection->isEmpty()) {
obj = selection->objects().back();
}
std::vector<Gtk::TreeModel::Row> toshow;
for (auto row : children) {
std::vector<SPObject *> objVec = row[_mColumns._colObj];
if (obj) {
for (auto & i : objVec) {
if (obj->getId() == i->getId()) {
if (!_stylemode) {
_treeView.get_selection()->select(row);
} else {
toshow.push_back(row);
}
}
}
}
if (!name.empty()) {
row[_cssColumns.label] = name;
}
this->setUndo(_("Rename CSS attribute"));
}
if (_stylemode) {
_store->clear();
for (auto row:toshow) {
_store->clear();
_store->insert(row);
if (row[_mColumns._colExpand]) {
_treeView.expand_to_path(Gtk::TreePath(row));
}
}
}
/* if (_stylemode) {
_store->foreach_iter(sigc::bind<std::vector<Gtk::TreeModel::Row> >(sigc::mem_fun(*this, &StyleDialog::_showStyleSelectors), toshow));
} */
}
/* bool StyleDialog::_showStyleSelectors(const Gtk::TreeModel::iterator& iter, std::vector<Gtk::TreeModel::Row> toshow)
{
Gtk::TreeModel::Row row = *iter;
if (std::find(toshow.begin(), toshow.end(), row)!= toshow.end()) {
std::cout << "fasdsfaasfasfasfasfasf" << std::endl;
} else {
}
_store->erase(row);
std::cout << "1111111111111111111" << std::endl;
return true;
} */
/**
* @brief CssDialog::valueEdited
* @param event
* @return
* Called when the value is edited in the TreeView editable column
*/
void CssDialog::valueEdited (const Glib::ustring& path, const Glib::ustring& value)
{
Gtk::TreeModel::Row row = *_store->get_iter(path);
if(row && this->_repr) {
Glib::ustring name = row[_cssColumns.label];
if(name.empty()) return;
setStyleProperty(name, value);
if(!value.empty()) {
row[_cssColumns._styleAttrVal] = value;
}
this->setUndo(_("Change attribute value"));
}
}
} // namespace Dialog
} // namespace UI
} // namespace Inkscape
|