|
0 |
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
1 |
/**
|
|
2 |
* @file
|
|
3 |
* Measure aux toolbar
|
|
4 |
*/
|
|
5 |
/* Authors:
|
|
6 |
* Sol Bekic <s+inkscape@s-ol.nu>
|
|
7 |
* Copyright (C) 2019 Authors
|
|
8 |
*
|
|
9 |
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
|
|
10 |
*/
|
|
11 |
|
|
12 |
#include "livecode-toolbar.h"
|
|
13 |
|
|
14 |
#include <glibmm/i18n.h>
|
|
15 |
#include <giomm/file.h>
|
|
16 |
#include <gtkmm/liststore.h>
|
|
17 |
#include <gtkmm/filechooserdialog.h>
|
|
18 |
|
|
19 |
#include "desktop.h"
|
|
20 |
#include "inkscape.h"
|
|
21 |
|
|
22 |
#include "ui/icon-names.h"
|
|
23 |
#include "ui/tools/livecode-tool.h"
|
|
24 |
#include "ui/widget/combo-box-entry-tool-item.h"
|
|
25 |
#include "ui/widget/label-tool-item.h"
|
|
26 |
|
|
27 |
using Inkscape::UI::Widget::ComboBoxEntryToolItem;
|
|
28 |
using Inkscape::UI::Tools::LivecodeTool;
|
|
29 |
|
|
30 |
/** Temporary hack: Returns the node tool in the active desktop.
|
|
31 |
* Will go away during tool refactoring. */
|
|
32 |
static LivecodeTool *get_livecode_tool()
|
|
33 |
{
|
|
34 |
LivecodeTool *tool = nullptr;
|
|
35 |
if (SP_ACTIVE_DESKTOP) {
|
|
36 |
Inkscape::UI::Tools::ToolBase *ec = SP_ACTIVE_DESKTOP->event_context;
|
|
37 |
if (SP_IS_LIVECODE_CONTEXT(ec)) {
|
|
38 |
tool = static_cast<LivecodeTool*>(ec);
|
|
39 |
}
|
|
40 |
}
|
|
41 |
return tool;
|
|
42 |
}
|
|
43 |
|
|
44 |
|
|
45 |
static void update_script_list(GtkListStore *model_files) {
|
|
46 |
gtk_list_store_clear(model_files);
|
|
47 |
auto prefs = Inkscape::Preferences::get();
|
|
48 |
Glib::ustring library_path= prefs->getString("/tools/livecode/library_dir");
|
|
49 |
Glib::RefPtr<Gio::File> library_dir = Gio::File::create_for_path(library_path);
|
|
50 |
|
|
51 |
auto enumerator = library_dir->enumerate_children();
|
|
52 |
if (!enumerator) {
|
|
53 |
g_error("couldn't find livecode library dir: %s", library_path.c_str());
|
|
54 |
return;
|
|
55 |
}
|
|
56 |
|
|
57 |
auto file_info = enumerator->next_file();
|
|
58 |
while (file_info)
|
|
59 |
{
|
|
60 |
if (file_info->get_file_type() == Gio::FILE_TYPE_REGULAR) {
|
|
61 |
GtkTreeIter iter;
|
|
62 |
gtk_list_store_append(model_files, &iter);
|
|
63 |
gtk_list_store_set(model_files, &iter, 0, file_info->get_name().c_str(), -1);
|
|
64 |
}
|
|
65 |
file_info = enumerator->next_file();
|
|
66 |
}
|
|
67 |
}
|
|
68 |
|
|
69 |
namespace Inkscape {
|
|
70 |
namespace UI {
|
|
71 |
namespace Toolbar {
|
|
72 |
LivecodeToolbar::LivecodeToolbar(SPDesktop *desktop)
|
|
73 |
: Toolbar(desktop)
|
|
74 |
{
|
|
75 |
auto prefs = Inkscape::Preferences::get();
|
|
76 |
|
|
77 |
/* active script */
|
|
78 |
{
|
|
79 |
GtkListStore* model_files = gtk_list_store_new( 1, G_TYPE_STRING );
|
|
80 |
update_script_list(model_files);
|
|
81 |
|
|
82 |
_active_script = Gtk::manage(new ComboBoxEntryToolItem("active_script",
|
|
83 |
_("active script"),
|
|
84 |
_("the currently running script"),
|
|
85 |
GTK_TREE_MODEL(model_files)));
|
|
86 |
_active_script->set_tooltip_text(_("Choose where scripts can be found"));
|
|
87 |
_active_script->signal_changed().connect(sigc::mem_fun(*this, &LivecodeToolbar::active_script_value_changed));
|
|
88 |
_active_script->focus_on_click(false);
|
|
89 |
add(*_active_script);
|
|
90 |
}
|
|
91 |
|
|
92 |
add_separator();
|
|
93 |
|
|
94 |
/* select library directory */
|
|
95 |
{
|
|
96 |
_select_library = Gtk::manage(new Gtk::ToolButton(_("Select Library Directory")));
|
|
97 |
_select_library->set_tooltip_text(_("Choose where scripts can be found"));
|
|
98 |
_select_library->set_icon_name(INKSCAPE_ICON("gears"));
|
|
99 |
_select_library->signal_clicked().connect(sigc::mem_fun(*this, &LivecodeToolbar::select_library_pressed));
|
|
100 |
add(*_select_library);
|
|
101 |
}
|
|
102 |
|
|
103 |
show_all();
|
|
104 |
}
|
|
105 |
|
|
106 |
GtkWidget *LivecodeToolbar::create(SPDesktop *desktop)
|
|
107 |
{
|
|
108 |
auto toolbar = new LivecodeToolbar(desktop);
|
|
109 |
return GTK_WIDGET(toolbar->gobj());
|
|
110 |
}
|
|
111 |
|
|
112 |
void LivecodeToolbar::active_script_value_changed()
|
|
113 |
{
|
|
114 |
// quit if run by the _changed callbacks
|
|
115 |
if (_freeze) {
|
|
116 |
return;
|
|
117 |
}
|
|
118 |
_freeze = true;
|
|
119 |
|
|
120 |
gchar *text = _active_script->get_active_text();
|
|
121 |
|
|
122 |
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
|
|
123 |
Glib::ustring path = prefs->getString("/tools/livecode/library_dir");
|
|
124 |
|
|
125 |
|
|
126 |
// _font_size_item->set_active_text( os.str().c_str() );
|
|
127 |
|
|
128 |
// @TODO: this can't be right, but I can't find a Gio:: path join helper right now
|
|
129 |
path += "/";
|
|
130 |
path += text;
|
|
131 |
|
|
132 |
LivecodeTool *tool = get_livecode_tool();
|
|
133 |
if (tool) {
|
|
134 |
// tool->loadScript(path);
|
|
135 |
}
|
|
136 |
|
|
137 |
_freeze = false;
|
|
138 |
g_free( text );
|
|
139 |
}
|
|
140 |
|
|
141 |
void LivecodeToolbar::select_library_pressed()
|
|
142 |
{
|
|
143 |
Gtk::FileChooserDialog dialog("Please choose the library directory folder",
|
|
144 |
Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
|
|
145 |
dialog.set_transient_for( *(INKSCAPE.active_desktop()->getToplevel()) );
|
|
146 |
|
|
147 |
dialog.add_button("Cancel", Gtk::RESPONSE_CANCEL);
|
|
148 |
dialog.add_button("Select", Gtk::RESPONSE_OK);
|
|
149 |
|
|
150 |
int result = dialog.run();
|
|
151 |
|
|
152 |
switch(result) {
|
|
153 |
case Gtk::RESPONSE_OK: {
|
|
154 |
auto prefs = Inkscape::Preferences::get();
|
|
155 |
prefs->setString("/tools/livecode/library_dir", dialog.get_filename());
|
|
156 |
update_script_list(GTK_LIST_STORE(_active_script->get_model()));
|
|
157 |
break;
|
|
158 |
}
|
|
159 |
case Gtk::RESPONSE_CANCEL:
|
|
160 |
break;
|
|
161 |
default:
|
|
162 |
g_message("Unexpected response from FileChooserDialog");
|
|
163 |
break;
|
|
164 |
}
|
|
165 |
}
|
|
166 |
|
|
167 |
|
|
168 |
}
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
|
|
173 |
/*
|
|
174 |
Local Variables:
|
|
175 |
mode:c++
|
|
176 |
c-file-style:"stroustrup"
|
|
177 |
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
|
|
178 |
indent-tabs-mode:nil
|
|
179 |
fill-column:99
|
|
180 |
End:
|
|
181 |
*/
|
|
182 |
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
|