Xe-Release/XeRelease_Gtkmm3/src/MyDialog.cc

191 lines
5.5 KiB
C++
Raw Normal View History

2021-06-23 21:03:44 +08:00
#include "MyDialog.hh"
2021-06-23 22:26:32 +08:00
2022-07-26 13:01:38 +08:00
MyPrefs::MyPrefs(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &ref_builder)
: Gtk::Box(cobject),
2022-07-24 18:23:10 +08:00
ref_Glade(ref_builder)
2021-06-23 22:26:32 +08:00
{
2022-07-26 13:01:38 +08:00
// // Initalize
// set_icon_name("XeRelease");
2022-07-24 18:23:10 +08:00
// Get Widgets
ref_builder->get_widget("entry_lts", entry_lts);
ref_builder->get_widget("entry_stable", entry_stable);
ref_builder->get_widget("entry_dev", entry_dev);
ref_builder->get_widget("entry_path", entry_path);
ref_builder->get_widget("btnpath", btnpath);
2022-07-26 13:01:38 +08:00
ref_builder->get_widget("btn_ok", btnok);
2022-07-24 18:23:10 +08:00
2022-07-26 11:00:41 +08:00
// json_file.close();
2021-12-31 22:49:10 +08:00
2022-07-24 18:23:10 +08:00
// std::string config;
// if(readCfgFile("xe_config","Longterm",config)){
// entry_lts->set_text(config);
// }
// if(readCfgFile("xe_config","Stable",config)){
// entry_stable->set_text(config);
// }
// if(readCfgFile("xe_config","Develop",config)){
// entry_dev->set_text(config);
// }
// readCfgFile("xe_config","Release_Path_Unix",config_unix);
// readCfgFile("xe_config","Release_Path_Win32",config_win32);
// Connect Signal
2022-07-26 13:01:38 +08:00
btnpath->signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnpath_clicked));
btnok->signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnok_clicked));
2021-06-23 22:26:32 +08:00
}
2022-07-26 13:01:38 +08:00
void MyPrefs::btnok_clicked()
2022-07-24 18:23:10 +08:00
{
// Save Configs to a file
2022-07-26 13:01:38 +08:00
Glib::ustring config;
std::fstream outfile;
2022-07-24 18:23:10 +08:00
2022-07-26 13:01:38 +08:00
// Initalize Config for path
if (unix_file_system_detected())
{
config_unix = entry_path->get_text();
}
else
{
2022-07-24 18:23:10 +08:00
2022-07-26 13:01:38 +08:00
config_win32 = entry_path->get_text();
}
// Open the config file
outfile.open("xe_config.json", std::ios_base::out);
/*OutPut contents to the file
Simple Contents of xe_config:
Longterm=x.x
Stable=x.x
Develop=x.x
Release_Path_Unix=/xxx/xxx
Release_Path_Win32=X:\xxx\xxx
*/
if (outfile.is_open())
{
// Create json object
json out_data = json::parse(R"(
2022-07-24 18:23:10 +08:00
{
"Longterm":"",
"Stable":"",
"Develop":"",
"Release_Path_Unix":"",
"Release_Path_Win32":""
}
)");
2022-07-26 13:01:38 +08:00
// Load config to json file
out_data["Longterm"] = entry_lts->get_text();
out_data["Stable"] = entry_stable->get_text();
out_data["Develop"] = entry_dev->get_text();
out_data["Release_Path_Unix"] = config_unix;
out_data["Release_Path_Win32"] = config_win32;
outfile << out_data;
// outfile<<"This is the config file of Xe Release"<<std::endl;
// outfile<<"See more on github.com/daleclack/Xe-Release"<<std::endl;
// outfile<<std::endl;
// config=entry_lts->get_text();
// outfile<<"Longterm="<<config<<std::endl;
// config=entry_stable->get_text();
// outfile<<"Stable="<<config<<std::endl;
// config=entry_dev->get_text();
// outfile<<"Develop="<<config<<std::endl;
// outfile<<"Release_Path_Unix="<<config_unix<<std::endl;
// outfile<<"Release_Path_Win32="<<config_win32<<std::endl;
2021-06-23 22:26:32 +08:00
}
2022-07-26 13:01:38 +08:00
outfile.close();
2021-06-23 22:26:32 +08:00
}
2021-06-23 21:03:44 +08:00
2022-07-26 13:01:38 +08:00
void MyPrefs::init_json_data(json &data1)
2022-07-26 11:00:41 +08:00
{
// Read Configs
std::string config_longterm, config_stable, config_devel;
// Open json file
if (!data1.empty())
{
// Read data from json file
// data = json::parse(json_file);
config_longterm = data1["Longterm"];
config_stable = data1["Stable"];
config_devel = data1["Develop"];
config_unix = data1["Release_Path_Unix"];
config_win32 = data1["Release_Path_Win32"];
// Set text from json file data
entry_lts->set_text(config_longterm);
entry_stable->set_text(config_stable);
entry_dev->set_text(config_devel);
// Use different path for Linux filesystem and Windows
if (unix_file_system_detected())
{
entry_path->set_text(config_unix);
}
else
{
entry_path->set_text(config_win32);
}
}
}
2022-07-26 13:01:38 +08:00
MyPrefs *MyPrefs::create()
2022-07-24 18:23:10 +08:00
{
2022-07-26 13:01:38 +08:00
// Create the config widget
2022-07-24 18:23:10 +08:00
auto builder = Gtk::Builder::create_from_resource("/org/gtk/daleclack/prefs.ui");
2022-07-26 13:01:38 +08:00
MyPrefs *box = nullptr;
builder->get_widget_derived("prefs", box);
return box;
}
2021-09-20 10:46:08 +08:00
2022-07-26 13:01:38 +08:00
void MyPrefs::set_parent_win(Gtk::Window *parent){
parent_win = parent;
2021-09-20 10:46:08 +08:00
}
2022-07-26 13:01:38 +08:00
void MyPrefs::btnpath_clicked()
2022-07-24 18:23:10 +08:00
{
// Create a Dialog
2022-07-26 13:01:38 +08:00
dialog = Gtk::FileChooserNative::create("Select a folder", *parent_win,
2022-07-24 18:23:10 +08:00
Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER, "OK", "Cancel");
2022-07-26 13:01:38 +08:00
dialog->signal_response().connect(sigc::mem_fun(*this, &MyPrefs::dialog_response));
2021-12-31 22:49:10 +08:00
dialog->show();
}
2022-07-26 13:01:38 +08:00
void MyPrefs::dialog_response(int response_id)
2022-07-24 18:23:10 +08:00
{
if (response_id == Gtk::RESPONSE_ACCEPT)
{
2021-12-31 22:49:10 +08:00
Glib::ustring path = dialog->get_filename();
entry_path->set_text(path);
}
dialog.reset();
}
2021-06-23 21:03:44 +08:00
MsgBox::MsgBox(Gtk::Window &parent)
2022-07-24 18:23:10 +08:00
: hbox(Gtk::ORIENTATION_HORIZONTAL, 5)
2021-06-23 21:03:44 +08:00
{
2022-07-24 18:23:10 +08:00
// Initalize MsgBox
2021-06-23 21:03:44 +08:00
set_icon_name("Xe-Release");
2022-07-24 18:23:10 +08:00
set_default_size(300, 150);
add_button("OK", Gtk::RESPONSE_OK);
2021-06-23 21:03:44 +08:00
set_transient_for(parent);
2022-07-24 18:23:10 +08:00
// Add Message
image.set_from_icon_name("Xe-Release", Gtk::ICON_SIZE_DIALOG);
vbox = get_content_area();
hbox.pack_start(image, Gtk::PACK_SHRINK);
hbox.pack_start(msg_label, Gtk::PACK_SHRINK);
2021-06-23 21:03:44 +08:00
vbox->pack_start(hbox);
}
2022-07-24 18:23:10 +08:00
void MsgBox::Init(Glib::ustring msg)
{
2021-06-23 21:03:44 +08:00
msg_label.set_label(msg);
}
2022-07-24 18:23:10 +08:00
void MsgBox::on_response(int response_id)
{
2021-06-23 21:03:44 +08:00
hide();
}