1 |
/* |
2 |
Copyright (c) MMXVII Christian Schoenebeck |
3 |
|
4 |
This file is part of "gigedit" and released under the terms of the |
5 |
GNU General Public License version 2. |
6 |
*/ |
7 |
|
8 |
#include "MacroEditor.h" |
9 |
#include "global.h" |
10 |
#include <assert.h> |
11 |
|
12 |
MacroEditor::MacroEditor() : |
13 |
m_macroOriginal(NULL), |
14 |
m_statusLabel("", Gtk::ALIGN_START), |
15 |
m_applyButton(_("_Apply"), true), |
16 |
m_cancelButton(_("_Cancel"), true) |
17 |
{ |
18 |
add(m_vbox); |
19 |
|
20 |
set_default_size(800, 600); |
21 |
|
22 |
// create Macro treeview (including its data model) |
23 |
m_treeStoreMacro = MacroTreeStore::create(m_treeModelMacro); |
24 |
m_treeViewMacro.set_model(m_treeStoreMacro); |
25 |
m_treeViewMacro.get_selection()->set_mode(Gtk::SELECTION_MULTIPLE); |
26 |
//m_treeViewMacro.set_tooltip_text(_("")); |
27 |
m_treeViewMacro.append_column(_("Key"), m_treeModelMacro.m_col_name); |
28 |
m_treeViewMacro.append_column(_("Type"), m_treeModelMacro.m_col_type); |
29 |
m_treeViewMacro.append_column_editable(_("Value"), m_treeModelMacro.m_col_value); |
30 |
/*{ |
31 |
Gtk::TreeViewColumn* column = m_treeViewMacro.get_column(0); |
32 |
Gtk::CellRendererText* cellrenderer = |
33 |
dynamic_cast<Gtk::CellRendererText*>(column->get_first_cell()); |
34 |
column->add_attribute( |
35 |
cellrenderer->property_foreground(), m_SamplesModel.m_color |
36 |
); |
37 |
}*/ |
38 |
/*{ |
39 |
Gtk::TreeViewColumn* column = m_treeViewMacro.get_column(1); |
40 |
Gtk::CellRendererText* cellrenderer = |
41 |
dynamic_cast<Gtk::CellRendererText*>(column->get_first_cell()); |
42 |
column->add_attribute( |
43 |
cellrenderer->property_foreground(), m_SamplesModel.m_color |
44 |
); |
45 |
}*/ |
46 |
m_treeViewMacro.set_headers_visible(true); |
47 |
/*m_treeViewMacro.signal_button_press_event().connect_notify( |
48 |
sigc::mem_fun(*this, &MainWindow::on_sample_treeview_button_release) |
49 |
);*/ |
50 |
/*m_refSamplesTreeModel->signal_row_changed().connect( |
51 |
sigc::mem_fun(*this, &MainWindow::sample_name_changed) |
52 |
);*/ |
53 |
|
54 |
m_scrolledWindow.add(m_treeViewMacro); |
55 |
m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); |
56 |
m_vbox.pack_start(m_scrolledWindow); |
57 |
|
58 |
m_buttonBox.set_layout(Gtk::BUTTONBOX_END); |
59 |
m_buttonBox.pack_start(m_applyButton); |
60 |
m_buttonBox.pack_start(m_cancelButton); |
61 |
m_applyButton.set_can_default(); |
62 |
m_applyButton.set_sensitive(false); |
63 |
m_applyButton.grab_focus(); |
64 |
|
65 |
#if GTKMM_MAJOR_VERSION >= 3 |
66 |
m_statusLabel.set_margin_left(6); |
67 |
m_statusLabel.set_margin_right(6); |
68 |
#else |
69 |
m_statusHBox.set_spacing(6); |
70 |
#endif |
71 |
|
72 |
m_statusHBox.pack_start(m_statusLabel); |
73 |
m_statusHBox.show_all_children(); |
74 |
|
75 |
m_footerHBox.pack_start(m_statusHBox); |
76 |
m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK); |
77 |
|
78 |
m_vbox.pack_start(m_footerHBox, Gtk::PACK_SHRINK); |
79 |
|
80 |
m_applyButton.signal_clicked().connect( |
81 |
sigc::mem_fun(*this, &MacroEditor::onButtonApply) |
82 |
); |
83 |
|
84 |
m_cancelButton.signal_clicked().connect( |
85 |
sigc::mem_fun(*this, &MacroEditor::onButtonCancel) |
86 |
); |
87 |
|
88 |
signal_hide().connect( |
89 |
sigc::mem_fun(*this, &MacroEditor::onWindowHide) |
90 |
); |
91 |
|
92 |
signal_delete_event().connect( |
93 |
sigc::mem_fun(*this, &MacroEditor::onWindowDelete) |
94 |
); |
95 |
|
96 |
show_all_children(); |
97 |
updateStatus(); |
98 |
} |
99 |
|
100 |
MacroEditor::~MacroEditor() { |
101 |
printf("MacroEditor destruct\n"); |
102 |
} |
103 |
|
104 |
void MacroEditor::setMacro(Serialization::Archive* macro) { |
105 |
m_macroOriginal = macro; |
106 |
if (!macro) { |
107 |
set_title(_("No Macro")); |
108 |
return; |
109 |
} |
110 |
|
111 |
//set_title(std::string(_("Macro Editor:")) + " \"" + macro->name() + "\""); |
112 |
set_title(std::string(_("Macro Editor:"))); |
113 |
|
114 |
// copy for non-destructive editing |
115 |
m_macro = *macro; |
116 |
|
117 |
reloadTreeView(); |
118 |
} |
119 |
|
120 |
void MacroEditor::buildTreeView(const Gtk::TreeModel::Row& parentRow, const Serialization::Object& parentObject) { |
121 |
for (int iMember = 0; iMember < parentObject.members().size(); ++iMember) { |
122 |
const Serialization::Member& member = parentObject.members()[iMember]; |
123 |
const Serialization::Object& object = m_macro.objectByUID(member.uid()); |
124 |
Gtk::TreeModel::iterator iterRow = m_treeStoreMacro->append(parentRow.children()); |
125 |
Gtk::TreeModel::Row row = *iterRow; |
126 |
row[m_treeModelMacro.m_col_name] = gig_to_utf8(member.name()); |
127 |
row[m_treeModelMacro.m_col_type] = gig_to_utf8(member.type().asLongDescr()); |
128 |
row[m_treeModelMacro.m_col_uid] = object.uid(); |
129 |
if (object.type().isClass()) { |
130 |
row[m_treeModelMacro.m_col_value] = "(class)"; |
131 |
buildTreeView(row, object); |
132 |
} else { |
133 |
row[m_treeModelMacro.m_col_value] = m_macro.valueAsString(object); |
134 |
} |
135 |
} |
136 |
} |
137 |
|
138 |
void MacroEditor::reloadTreeView() { |
139 |
m_treeStoreMacro->clear(); |
140 |
|
141 |
const Serialization::Object& rootObject = m_macro.rootObject(); |
142 |
|
143 |
Gtk::TreeModel::iterator iterRoot = m_treeStoreMacro->append(); |
144 |
Gtk::TreeModel::Row rowRoot = *iterRoot; |
145 |
rowRoot[m_treeModelMacro.m_col_name] = "(Root)"; |
146 |
rowRoot[m_treeModelMacro.m_col_type] = gig_to_utf8(rootObject.type().asLongDescr()); |
147 |
rowRoot[m_treeModelMacro.m_col_value] = ""; |
148 |
rowRoot[m_treeModelMacro.m_col_uid] = rootObject.uid(); |
149 |
|
150 |
buildTreeView(rowRoot, rootObject); |
151 |
|
152 |
m_treeViewMacro.expand_all(); |
153 |
|
154 |
updateStatus(); |
155 |
} |
156 |
|
157 |
void MacroEditor::updateStatus() { |
158 |
m_applyButton.set_sensitive(isModified()); |
159 |
updateStatusBar(); |
160 |
} |
161 |
|
162 |
void MacroEditor::updateStatusBar() { |
163 |
// update status text |
164 |
std::string txt; |
165 |
m_statusLabel.set_markup(txt); |
166 |
} |
167 |
|
168 |
bool MacroEditor::onWindowDelete(GdkEventAny* e) { |
169 |
//printf("onWindowDelete\n"); |
170 |
|
171 |
if (!isModified()) return false; // propagate event further (which will close this window) |
172 |
|
173 |
//gchar* msg = g_strdup_printf(_("Apply changes to macro \"%s\" before closing?"), |
174 |
// m_macroOriginal->Name.c_str()); |
175 |
gchar* msg = g_strdup_printf(_("Apply changes to macro before closing?")); |
176 |
Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE); |
177 |
g_free(msg); |
178 |
dialog.set_secondary_text(_("If you close without applying, your changes will be lost.")); |
179 |
dialog.add_button(_("Close _Without Applying"), Gtk::RESPONSE_NO); |
180 |
dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL); |
181 |
dialog.add_button(_("_Apply"), Gtk::RESPONSE_YES); |
182 |
dialog.set_default_response(Gtk::RESPONSE_YES); |
183 |
int response = dialog.run(); |
184 |
dialog.hide(); |
185 |
|
186 |
// user decided to close macro editor without saving |
187 |
if (response == Gtk::RESPONSE_NO) |
188 |
return false; // propagate event further (which will close this window) |
189 |
|
190 |
// user cancelled dialog, thus don't close macro editor |
191 |
if (response == Gtk::RESPONSE_CANCEL) { |
192 |
show(); |
193 |
return true; // drop event (prevents closing this window) |
194 |
} |
195 |
|
196 |
// user wants to apply the changes, afterwards close window |
197 |
if (response == Gtk::RESPONSE_YES) { |
198 |
onButtonApply(); |
199 |
return false; // propagate event further (which will close this window) |
200 |
} |
201 |
|
202 |
// should never ever make it to this point actually |
203 |
return false; |
204 |
} |
205 |
|
206 |
bool MacroEditor::isModified() const { |
207 |
return m_macro.isModified(); |
208 |
} |
209 |
|
210 |
void MacroEditor::onButtonCancel() { |
211 |
bool dropEvent = onWindowDelete(NULL); |
212 |
if (dropEvent) return; |
213 |
hide(); |
214 |
} |
215 |
|
216 |
void MacroEditor::onButtonApply() { |
217 |
//m_macro.encode(); |
218 |
*m_macroOriginal = m_macro; |
219 |
} |
220 |
|
221 |
void MacroEditor::onWindowHide() { |
222 |
delete this; // this is the end, my friend |
223 |
} |