/[svn]/gigedit/trunk/src/gigedit/scriptslots.cpp
ViewVC logotype

Contents of /gigedit/trunk/src/gigedit/scriptslots.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3225 - (show annotations) (download)
Fri May 26 22:10:16 2017 UTC (6 years, 11 months ago) by schoenebeck
File size: 6538 byte(s)
* Assigned more useful default dimensions (and default position) for various
  windows and dialogs (if auto-restore of user's own custom window
  dimensions is disabled).
* Bumped version (1.0.0.svn51).

1 /*
2 Copyright (c) 2014 - 2017 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 "scriptslots.h"
9 #include "global.h"
10
11 ScriptSlots::ScriptSlots() :
12 m_closeButton(Gtk::Stock::CLOSE)
13 {
14 m_instrument = NULL;
15
16 if (!Settings::singleton()->autoRestoreWindowDimension) {
17 set_default_size(460,300);
18 set_position(Gtk::WIN_POS_MOUSE);
19 }
20
21 add(m_vbox);
22
23 m_generalInfoLabel.set_text(_(
24 "Each row (\"slot\") references one instrument script that shall be "
25 "executed by the sampler for currently selected instrument. Slots are "
26 "executed consecutively from top down."
27 ));
28 m_generalInfoLabel.set_line_wrap();
29 m_vbox.pack_start(m_generalInfoLabel, Gtk::PACK_SHRINK);
30
31 m_dragHintLabel.set_text(_(
32 "Drag & drop a script from main window to this window to add a new "
33 "script slot for this instrument."
34 ));
35 m_dragHintLabel.set_line_wrap();
36 m_scrolledWindow.add(m_vboxSlots);
37 m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
38 m_vbox.pack_start(m_scrolledWindow);
39
40 m_vbox.pack_start(m_dragHintLabel, Gtk::PACK_SHRINK);
41
42 m_buttonBox.set_layout(Gtk::BUTTONBOX_END);
43 m_buttonBox.pack_start(m_closeButton);
44 m_closeButton.set_can_default();
45 m_closeButton.grab_focus();
46 m_vbox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);
47
48 m_closeButton.signal_clicked().connect(
49 sigc::mem_fun(*this, &ScriptSlots::onButtonClose)
50 );
51
52 signal_hide().connect(
53 sigc::mem_fun(*this, &ScriptSlots::onWindowHide)
54 );
55
56 // establish drag&drop between scripts tree view on main diwno and this
57 // ScriptSlots window
58 std::vector<Gtk::TargetEntry> drag_target_gig_script;
59 drag_target_gig_script.push_back(Gtk::TargetEntry("gig::Script"));
60 drag_dest_set(drag_target_gig_script);
61 signal_drag_data_received().connect(
62 sigc::mem_fun(*this, &ScriptSlots::onScriptDragNDropDataReceived)
63 );
64
65 show_all_children();
66 }
67
68 ScriptSlots::~ScriptSlots() {
69 //printf("ScriptSlots destruct\n");
70 clearSlots();
71 }
72
73 void ScriptSlots::clearSlots() {
74 for (int i = 0; i < m_slots.size(); ++i) {
75 delete m_slots[i].deleteButton;
76 delete m_slots[i].downButton;
77 delete m_slots[i].upButton;
78 delete m_slots[i].label;
79 delete m_slots[i].hbox;
80 }
81 m_slots.clear();
82 }
83
84 void ScriptSlots::setInstrument(gig::Instrument* instrument) {
85 m_instrument = instrument;
86 if (!m_instrument) {
87 set_title(_("No Instrument"));
88 return;
89 }
90
91 set_title(std::string(_("Script Slots of Instrument")) + " - \"" + instrument->pInfo->Name + "\"");
92 clearSlots();
93 for (int i = 0; i < instrument->ScriptSlotCount(); ++i) {
94 gig::Script* script = instrument->GetScriptOfSlot(i);
95 if (!script) continue;
96 //printf("script '%s'\n", script->Name.c_str());
97 appendNewSlot(script);
98 }
99 }
100
101 void ScriptSlots::refreshSlots() {
102 clearSlots();
103 setInstrument(m_instrument);
104 }
105
106 void ScriptSlots::onScriptDragNDropDataReceived(
107 const Glib::RefPtr<Gdk::DragContext>& context, int, int,
108 const Gtk::SelectionData& selection_data, guint, guint time)
109 {
110 gig::Script* script = *((gig::Script**) selection_data.get_data());
111 if (script && selection_data.get_length() == sizeof(gig::Script*)) {
112 std::cout << "Drop received script \"" << script->Name << "\"" << std::endl;
113 m_instrument->AddScriptSlot(script);
114 appendNewSlot(script);
115 // drop success
116 context->drop_reply(true, time);
117 // inform i.e. main window
118 script_slots_changed_signal.emit(m_instrument);
119 } else {
120 // drop failed
121 context->drop_reply(false, time);
122 }
123 }
124
125 void ScriptSlots::appendNewSlot(gig::Script* script) {
126 static int slotID = 0;
127
128 Row row;
129 row.id = slotID++;
130 row.hbox = new Gtk::HBox;
131 row.label = new Gtk::Label;
132 row.downButton = new Gtk::Button(Gtk::Stock::GO_DOWN);
133 row.upButton = new Gtk::Button(Gtk::Stock::GO_UP);
134 row.deleteButton = new Gtk::Button(Gtk::Stock::DELETE);
135 row.script = script;
136
137 row.hbox->pack_start(*row.label);
138 row.hbox->pack_start(*row.downButton, Gtk::PACK_SHRINK);
139 row.hbox->pack_start(*row.upButton, Gtk::PACK_SHRINK);
140 row.hbox->pack_start(*row.deleteButton, Gtk::PACK_SHRINK);
141
142 row.label->set_text(ToString(m_slots.size()+1) + ". \"" + script->Name + "\"");
143 //row.label->set_alignment(Gtk::ALIGN_START, Gtk::ALIGN_CENTER);
144
145 row.upButton->signal_clicked().connect(
146 sigc::bind(
147 sigc::mem_fun(*this, &ScriptSlots::moveSlotUp), row.id
148 )
149 );
150 row.downButton->signal_clicked().connect(
151 sigc::bind(
152 sigc::mem_fun(*this, &ScriptSlots::moveSlotDown), row.id
153 )
154 );
155 row.deleteButton->signal_clicked().connect(
156 sigc::bind(
157 sigc::mem_fun(*this, &ScriptSlots::deleteSlot), row.id
158 )
159 );
160
161 m_vboxSlots.add(*row.hbox);
162 m_scrolledWindow.show_all_children();
163
164 m_slots.push_back(row);
165 }
166
167 void ScriptSlots::moveSlotUp(int slotID) {
168 for (int i = 0; i < m_instrument->ScriptSlotCount(); ++i) {
169 if (m_slots[i].id == slotID) {
170 if (i != 0) {
171 m_instrument->SwapScriptSlots(i, i-1);
172 refreshSlots();
173 script_slots_changed_signal.emit(m_instrument);
174 }
175 break;
176 }
177 }
178 }
179
180 void ScriptSlots::moveSlotDown(int slotID) {
181 for (int i = 0; i < m_instrument->ScriptSlotCount(); ++i) {
182 if (m_slots[i].id == slotID) {
183 if (i < m_instrument->ScriptSlotCount() - 1) {
184 m_instrument->SwapScriptSlots(i, i+1);
185 refreshSlots();
186 script_slots_changed_signal.emit(m_instrument);
187 }
188 break;
189 }
190 }
191 }
192
193 void ScriptSlots::deleteSlot(int slotID) {
194 for (int i = 0; i < m_instrument->ScriptSlotCount(); ++i) {
195 if (m_slots[i].id == slotID) {
196 m_instrument->RemoveScriptSlot(i);
197 refreshSlots();
198 script_slots_changed_signal.emit(m_instrument);
199 break;
200 }
201 }
202 }
203
204 sigc::signal<void, gig::Instrument*>& ScriptSlots::signal_script_slots_changed() {
205 return script_slots_changed_signal;
206 }
207
208 void ScriptSlots::onButtonClose() {
209 hide();
210 }
211
212 void ScriptSlots::onWindowHide() {
213 delete this; // this is the end, my friend
214 }
215

  ViewVC Help
Powered by ViewVC