-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwin-emporiums.cpp
108 lines (97 loc) · 2.82 KB
/
mainwin-emporiums.cpp
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
#include "mainwin.h"
#include "emporium.h"
#include <exception>
#include <stdexcept>
void Mainwin::on_file_open_click() {
try {
std::ifstream ifs{"emporium.emp", std::ifstream::in};
_emp = new Mice::Emporium{ifs};
} catch (std::exception& e) {
Gtk::MessageDialog dialog{*this, "Unable to open emporium.emp"};
dialog.set_secondary_text(e.what());
dialog.run();
dialog.close();
}
}
void Mainwin::on_file_save_click() {
try {
std::ofstream ofs{"emporium.emp", std::ofstream::out};
_emp->save(ofs);
} catch (std::exception& e) {
Gtk::MessageDialog dialog{*this, "Unable to save emporium.emp"};
dialog.set_secondary_text(e.what());
dialog.run();
dialog.close();
}
}
void Mainwin::on_file_save_as_click(){
Gtk::Dialog dialog;
dialog.set_title("Save As");
dialog.set_transient_for(*this);
Gtk::HBox b_name;
Gtk::Label l_name{"File Name?"};
l_name.set_width_chars(20);
b_name.pack_start(l_name, Gtk::PACK_SHRINK);
Gtk::Entry e_name;
e_name.set_max_length(20*4);
b_name.pack_start(e_name, Gtk::PACK_SHRINK);
dialog.get_vbox()->pack_start(b_name, Gtk::PACK_SHRINK);
dialog.add_button("Cancel", 0);
dialog.add_button("Enter", 1);
dialog.show_all();
if (dialog.run() == 0)
{
return;
}
dialog.close();
std::string name = e_name.get_text();
std::regex str_num{"^[:alnum:]"}; // REGEX
if (std::regex_match(name, str_num)) // REGEX
{
Gtk::MessageDialog dialog_f{*this, "Unable to save " + e_name.get_text()};
dialog_f.run();
dialog_f.close();
}
try {
std::ofstream ofs{e_name.get_text(), std::ofstream::out};
_emp->save(ofs);
} catch (std::exception& e) {
Gtk::MessageDialog dialog_f{*this, "Unable to save " + e_name.get_text()};
dialog_f.set_secondary_text(e.what());
dialog_f.run();
dialog_f.close();
}
}
void Mainwin::on_file_open_file_click(){
Gtk::Dialog dialog;
dialog.set_title("Open File");
dialog.set_transient_for(*this);
Gtk::HBox b_name;
Gtk::Label l_name{"File Name?"};
l_name.set_width_chars(20);
b_name.pack_start(l_name, Gtk::PACK_SHRINK);
Gtk::Entry e_name;
e_name.set_max_length(20*4);
b_name.pack_start(e_name, Gtk::PACK_SHRINK);
dialog.get_vbox()->pack_start(b_name, Gtk::PACK_SHRINK);
dialog.add_button("Cancel", 0);
dialog.add_button("Enter", 1);
dialog.show_all();
if (dialog.run() == 0)
{
return;
}
dialog.close();
try {
std::ifstream ifs{e_name.get_text(), std::ifstream::in};
_emp = new Mice::Emporium{ifs};
} catch (std::exception& e) {
Gtk::MessageDialog dialog_f{*this, "Unable to open emporium.emp"};
dialog_f.set_secondary_text(e.what());
dialog_f.run();
dialog_f.close();
}
}
void Mainwin::on_file_new_emporium_click(){
emporiums.push_back(_emp);
}