-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiMEDWindow.cpp
More file actions
402 lines (354 loc) · 10 KB
/
BiMEDWindow.cpp
File metadata and controls
402 lines (354 loc) · 10 KB
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
/*!
* \project_name BiM Encoder/Decoder
* \file BiMEDWindow.cpp
* \brief Gtk:Window specifications of the BiM Encoder/Decoder
* \authors Marco Dos Santos Oliveira
* \version 1.0.0
* \date 2 August 2013
*
* This software is published in MPLv2.0
*
*/
//#include <glibmm/miscutils.h>
#include "BiMEDWindow.hpp"
BiMEDWindow::BiMEDWindow
(
void
)
{
// configure the window
set_title("BiMED : The EBU BiM Encoder/Decoder");
#ifdef __unix__
set_icon_from_file(Glib::get_current_dir()+"/img/BiMLogo.png");
#endif
#ifdef _WIN32
set_icon_from_file(Glib::get_current_dir()+"\\img\\BiMLogo.png");
#endif
// instantiate the widgets
Gtk::Box * vbox = manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL,5));
Gtk::ScrolledWindow * SWforTextEnc = manage(new Gtk::ScrolledWindow());
Gtk::ScrolledWindow * SWforTextDec = manage(new Gtk::ScrolledWindow());
Gtk::Separator * S1 = manage(new Gtk::Separator(Gtk::ORIENTATION_HORIZONTAL));
Gtk::Separator * S2 = manage(new Gtk::Separator(Gtk::ORIENTATION_HORIZONTAL));
Gtk::Separator * S3 = manage(new Gtk::Separator(Gtk::ORIENTATION_HORIZONTAL));
Gtk::Button * encodeButton = manage(new Gtk::Button("Encode"));
Gtk::Button * decodeButton = manage(new Gtk::Button("Decode"));
Gtk::TextView * toEncode = manage (new Gtk::TextView());
Gtk::TextView * toDecode = manage (new Gtk::TextView());
// instantiate window menu
refActionGroup = Gtk::ActionGroup::create();
//File|New sub menu:
refActionGroup->add(Gtk::Action::create("FileOpenToDecode",
Gtk::Stock::OPEN, "Open BiM", "Open a BiM file to decode"),
sigc::mem_fun(*this, &BiMEDWindow::on_open_to_decode));
refActionGroup->add(Gtk::Action::create("FileOpenToEncode",
Gtk::Stock::OPEN, "Open XML", "Open an XML file to encode"),
sigc::mem_fun(*this, &BiMEDWindow::on_open_to_encode));
refActionGroup->add(Gtk::Action::create("FileSaveDecoded",
Gtk::Stock::SAVE_AS, "Save XML", "Save the decoded result"),
sigc::mem_fun(*this, &BiMEDWindow::on_save_decoded));
refActionGroup->add(Gtk::Action::create("FileSaveEncoded",
Gtk::Stock::SAVE_AS, "Save BiM", "Save the encoded result"),
sigc::mem_fun(*this, &BiMEDWindow::on_save_encoded));
//File menu:
refActionGroup->add(Gtk::Action::create("FileMenu", "File"));
//Sub-menu.
refActionGroup->add(Gtk::Action::create("FileOpen", Gtk::Stock::OPEN));
refActionGroup->add(Gtk::Action::create("FileSave", Gtk::Stock::SAVE_AS));
refActionGroup->add(Gtk::Action::create("FileQuit", Gtk::Stock::QUIT),
sigc::mem_fun(*this, &BiMEDWindow::on_leave)
);
//Edit menu:
refActionGroup->add(Gtk::Action::create("ActionMenu", "Action"));
refActionGroup->add(Gtk::Action::create("Encode",
Gtk::Stock::EXECUTE, "Encode", "Encode the data text"),
sigc::mem_fun(*this, &BiMEDWindow::on_encode_clicked));
refActionGroup->add(Gtk::Action::create("Decode",
Gtk::Stock::EXECUTE, "Decode", "Decode the binary data"),
sigc::mem_fun(*this, &BiMEDWindow::on_decode_clicked));
//Help menu:
refActionGroup->add( Gtk::Action::create("HelpMenu", "Help") );
refActionGroup->add( Gtk::Action::create("HelpAbout", Gtk::Stock::HELP, "About BiMED", "Get more details about this application"),
sigc::mem_fun(*this, &BiMEDWindow::on_about) );
refUIManager = Gtk::UIManager::create();
refUIManager->insert_action_group(refActionGroup);
add_accel_group(refUIManager->get_accel_group());
//Layout the actions in a menubar and toolbar:
Glib::ustring ui_info =
"<ui>"
" <menubar name='MenuBar'>"
" <menu action='FileMenu'>"
" <menu action='FileOpen'>"
" <menuitem action='FileOpenToDecode'/>"
" <menuitem action='FileOpenToEncode'/>"
" </menu>"
" <separator/>"
" <menu action='FileSave'>"
" <menuitem action='FileSaveDecoded'/>"
" <menuitem action='FileSaveEncoded'/>"
" </menu>"
" <separator/>"
" <menuitem action='FileQuit'/>"
" </menu>"
" <menu action='ActionMenu'>"
" <menuitem action='Encode'/>"
" <menuitem action='Decode'/>"
" </menu>"
" <menu action='HelpMenu'>"
" <menuitem action='HelpAbout'/>"
" </menu>"
" </menubar>"
"</ui>";
try
{
refUIManager->add_ui_from_string(ui_info);
}
catch(const Glib::Error& ex)
{
std::cerr << "building menus failed: " << ex.what();
}
Gtk::Widget* pMenubar = refUIManager->get_widget("/MenuBar");
// configure the widgets
set_default_size(340, 550);
set_has_resize_grip(true);
vbox->set_spacing(5);
vbox->set_homogeneous(false);
vbox->set_border_width(8);
SWforTextEnc->set_size_request(320, 198);
SWforTextDec->set_size_request(320, 198);
// configure the TextViews
toEncode->set_pixels_above_lines(3);
toEncode->set_left_margin(3);
toEncode->set_right_margin(3);
toEncode->set_editable(true);
toEncode->set_cursor_visible(true);
toEncode->set_wrap_mode(Gtk::WRAP_WORD_CHAR);
toEncodeTextBuffer = Gtk::TextBuffer::create();
toEncodeTextBuffer->set_text("Set the text to encode here");
toEncode->set_buffer(toEncodeTextBuffer);
//////////////////////////////////////////
toDecode->set_pixels_above_lines(2);
toDecode->set_left_margin(2);
toDecode->set_right_margin(2);
toDecode->set_editable(true);
toDecode->set_cursor_visible(true);
toDecode->set_wrap_mode(Gtk::WRAP_WORD_CHAR);
toDecodeTextBuffer = Gtk::TextBuffer::create();
toDecodeTextBuffer->set_text("Set the text to decode here");
toDecode->set_buffer(toDecodeTextBuffer);
// configure the buttons
encodeButton->signal_clicked().connect
(
sigc::mem_fun
(
*this,
&BiMEDWindow::on_encode_clicked
)
);
decodeButton->signal_clicked().connect
(
sigc::mem_fun
(
*this,
&BiMEDWindow::on_decode_clicked
)
);
// set the widget in his relative
vbox->pack_start(*pMenubar, Gtk::PACK_SHRINK, 0);
vbox->pack_start(*S1,Gtk::PACK_SHRINK, 0);
vbox->pack_start(*SWforTextEnc,Gtk::PACK_EXPAND_WIDGET,0);
vbox->pack_start(*encodeButton,Gtk::PACK_SHRINK,0);
vbox->pack_start(*S2,Gtk::PACK_SHRINK,0);
vbox->pack_start(*SWforTextDec,Gtk::PACK_EXPAND_WIDGET,0);
vbox->pack_start(*decodeButton,Gtk::PACK_SHRINK,0);
vbox->pack_start(*S3,Gtk::PACK_SHRINK,0);
add(*vbox);
SWforTextEnc->add(*toEncode);
SWforTextDec->add(*toDecode);
// show the widgets
vbox->show();
SWforTextEnc->show();
SWforTextDec->show();
S1->show();
S2->show();
S3->show();
encodeButton->show();
decodeButton->show();
toEncode->show();
toDecode->show();
// show window's widgets
}
BiMEDWindow::~BiMEDWindow
(
void
)
{
}
void BiMEDWindow::on_open_to_encode
(
void
)
{
// instanciate a new filechooser
filechooserDialog * FC = new filechooserDialog
(
*this,
"Select an XML file to encode",
Gtk::FILE_CHOOSER_ACTION_OPEN,
Glib::get_home_dir(),
false,
0
);
// get back the response
if (FC->getResponse() == Gtk::RESPONSE_OK)
{
toEncodeTextBuffer->set_text(BiMEDEngine::readFile(FC->getPathToFile()));
}
delete FC;
}
void BiMEDWindow::on_open_to_decode
(
void
)
{
// instanciate a new filechooser
filechooserDialog * FC = new filechooserDialog
(
*this,
"Select a BiM file to decode",
Gtk::FILE_CHOOSER_ACTION_OPEN,
Glib::get_home_dir(),
false,
1
);
// get back the response
if (FC->getResponse() == Gtk::RESPONSE_OK)
{
toDecodeTextBuffer->set_text(BiMEDEngine::readBinaryFile(FC->getPathToFile()));
}
delete FC;
}
void BiMEDWindow::on_save_decoded
(
void
)
{
// instanciate a new filechooser
filechooserDialog * FC = new filechooserDialog
(
*this,
"Save the decoded BiM file into an XML file",
Gtk::FILE_CHOOSER_ACTION_SAVE,
Glib::get_home_dir(),
false,
0
);
// get back the response
if (FC->getResponse() == Gtk::RESPONSE_OK)
{
BiMEDEngine::writeFile
(
(
(genericFeatures::isExtension(FC->getPathToFile(),"xml"))
? FC->getPathToFile()
: FC->getPathToFile()+".xml"
)
, toEncodeTextBuffer->get_text()
);
}
delete FC;
}
void BiMEDWindow::on_save_encoded
(
void
)
{
if (!toDecodeTextBuffer->get_text().empty() and genericFeatures::isHexadecimal(toDecodeTextBuffer->get_text())) {
// instanciate a new filechooser
filechooserDialog * FC = new filechooserDialog
(
*this,
"Save the encoded XML file into a BiM file",
Gtk::FILE_CHOOSER_ACTION_SAVE,
Glib::get_home_dir(),
false,
1
);
// get back the response
if (FC->getResponse() == Gtk::RESPONSE_OK)
{
BiMEDEngine::writeBinaryFile
(
(
(genericFeatures::isExtension(FC->getPathToFile(),"bin"))
? FC->getPathToFile()
: FC->getPathToFile()+".bin"
)
,
(
(genericFeatures::hasHexadecimalPrefix(toDecodeTextBuffer->get_text()))
? toDecodeTextBuffer->get_text().substr(2, toDecodeTextBuffer->get_text().size()-2)
: toDecodeTextBuffer->get_text()
)
);
}
delete FC;
} else {
genericAlertWindow * AC = new genericAlertWindow
(
*this,
"Error : Unable to save the binary file" ,
"Please, check the hexadecimal text to save. Maybe, the text area is empty or contains some non hexadecimal characters."
);
delete AC;
}
}
void BiMEDWindow::on_leave
(
void
)
{
hide();
}
void BiMEDWindow::on_encode_clicked
(
void
)
{
if (toEncodeTextBuffer->size() > 0) {
toDecodeTextBuffer->set_text(BiMEDEngine::convertTxtToBinary(toEncodeTextBuffer->get_text()));
} else {
genericAlertWindow * AC = new genericAlertWindow
(
*this,
"Error : Unable to encode the text data" ,
"Please, type something in the text area to encode. It cannot stay empty."
);
delete AC;
}
}
void BiMEDWindow::on_decode_clicked
(
void
)
{
if (genericFeatures::isHexadecimal(toDecodeTextBuffer->get_text())) {
toEncodeTextBuffer->set_text(BiMEDEngine::convertBinaryToTxt(toDecodeTextBuffer->get_text()));
} else {
genericAlertWindow * AC = new genericAlertWindow
(
*this,
"Error : Unable to decode the binary data" ,
"Please, check the hexadecimal text. Maybe, the text area is empty or contains non hexadecimal characters."
);
delete AC;
}
}
void BiMEDWindow::on_about
(
void
)
{
AboutBiMED * AB = new AboutBiMED();
delete AB;
}