LinuxPack/InstallWidget.cpp
2025-01-17 11:43:11 +08:00

138 lines
5.0 KiB
C++

//
// Created by taynpg on 24-8-6.
//
#include "InstallWidget.h"
CInstallDialog::CInstallDialog(wxWindow* parent, const wxString& title)
: wxDialog(parent, wxID_ANY, title, wxDefaultPosition)
{
handle_ = new CFunInstall();
InitInstall();
}
CInstallDialog::~CInstallDialog()
{
delete handle_;
}
void CInstallDialog::InitInstall()
{
panel_ = new wxPanel(this);
top_sizer_ = new wxBoxSizer(wxVERTICAL);
label_binary_ = new wxStaticText(panel_, wxID_ANY, wxT("二进制文件:"));
top_sizer_->Add(label_binary_, 0, wxALL, g_Border2);
auto* hs1 = new wxBoxSizer(wxHORIZONTAL);
text_binary_file_ = new wxTextCtrl(panel_, wxID_ANY);
btn_select_binary_ = new wxButton(panel_, wxID_ANY, wxT("选择"));
btn_select_binary_->Bind(wxEVT_BUTTON, &CInstallDialog::selectBinaryFile, this);
hs1->Add(text_binary_file_, 1, wxALL | wxEXPAND, g_Border2);
hs1->Add(btn_select_binary_, 0, wxALL, g_Border2);
top_sizer_->Add(hs1, 0, wxALL | wxEXPAND, g_Border2);
use_default_ico_ = new wxCheckBox(panel_, wxID_ANY, wxT("使用默认图标"));
use_default_ico_->Bind(wxEVT_CHECKBOX, &CInstallDialog::checkChange, this);
top_sizer_->Add(use_default_ico_, 0, wxALL | wxEXPAND, g_Border2);
label_ico_file_ = new wxStaticText(panel_, wxID_ANY, wxT("图标文件:"));
top_sizer_->Add(label_ico_file_, 0, wxALL, g_Border2);
auto* hs2 = new wxBoxSizer(wxHORIZONTAL);
text_ico_file_ = new wxTextCtrl(panel_, wxID_ANY);
btn_select_ico_ = new wxButton(panel_, wxID_ANY, wxT("选择"));
btn_select_ico_->Bind(wxEVT_BUTTON, &CInstallDialog::selectIcoFile, this);
hs2->Add(text_ico_file_, 1, wxALL | wxEXPAND, g_Border2);
hs2->Add(btn_select_ico_, 0, wxALL, g_Border2);
top_sizer_->Add(hs2, 0, wxALL | wxEXPAND, g_Border2);
auto* hs3 = new wxBoxSizer(wxHORIZONTAL);
label_category_ = new wxStaticText(panel_, wxID_ANY, wxT("程序类别:"));
choice_category_ = new wxChoice(panel_, wxID_ANY);
choice_category_->Append(wxT("Audio"));
choice_category_->Append(wxT("Video"));
choice_category_->Append(wxT("Development"));
choice_category_->Append(wxT("Education"));
choice_category_->Append(wxT("Game"));
choice_category_->Append(wxT("Graphics"));
choice_category_->Append(wxT("Office"));
choice_category_->Append(wxT("Science"));
choice_category_->Append(wxT("Settings"));
choice_category_->Append(wxT("Utility"));
choice_category_->SetSelection(9);
hs3->Add(label_category_, 0, wxALL | wxALIGN_CENTER_VERTICAL, g_Border2);
hs3->Add(choice_category_, 0, wxALL, g_Border2);
top_sizer_->Add(hs3, 0, wxALL | wxEXPAND, g_Border2);
btn_install_ = new wxButton(panel_, wxID_ANY, wxT("安装"));
btn_install_->Bind(wxEVT_BUTTON, &CInstallDialog::installToDesktop, this);
top_sizer_->Add(btn_install_, 0, wxALL | wxALIGN_RIGHT, g_Border2);
panel_->SetSizer(top_sizer_);
auto* dialogSizer = new wxBoxSizer(wxVERTICAL);
dialogSizer->Add(panel_, 1, wxEXPAND | wxALL, g_Border2);
// 设置对话框的 sizer
SetSizerAndFit(dialogSizer);
}
void CInstallDialog::installToDesktop(wxCommandEvent& event)
{
wxString ico = text_ico_file_->GetValue();
if (!text_ico_file_->IsEnabled()) {
ico.clear();
}
if (handle_->install(text_binary_file_->GetValue(), choice_category_->GetStringSelection(), ico)) {
// 显示消息框
wxMessageBox(wxT("成功"), // 消息内容
wxT("成功"), // 标题
wxOK | wxICON_INFORMATION, // 按钮和图标
this // 父窗口
);
return;
}
wxMessageBox(wxT("失败"), // 消息内容
wxT("失败"), // 标题
wxOK | wxICON_INFORMATION, // 按钮和图标
this // 父窗口
);
}
void CInstallDialog::checkChange(wxCommandEvent& event)
{
auto* checkBox = dynamic_cast<wxCheckBox*>(event.GetEventObject());
if (checkBox) {
if (checkBox->IsChecked()) {
text_ico_file_->Enable(false);
btn_select_ico_->Enable(false);
} else {
text_ico_file_->Enable(true);
btn_select_ico_->Enable(true);
}
}
}
void CInstallDialog::selectBinaryFile(wxCommandEvent& event)
{
wxFileDialog openFileDialog(this, wxT("选择binary文件"), "", "", "*", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (openFileDialog.ShowModal() == wxID_CANCEL)
return;
wxString filePath = openFileDialog.GetPath();
text_binary_file_->SetValue(filePath);
}
void CInstallDialog::selectIcoFile(wxCommandEvent& event)
{
wxFileDialog openFileDialog(this, _("Open file"), "", "",
"ICO (*.ico)|*.ico|PNG (*.png)|*.png|SVG (*.svg)|*.svg|All files (*.*)|*.*",
wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (openFileDialog.ShowModal() == wxID_CANCEL)
return;
wxString filePath = openFileDialog.GetPath();
text_ico_file_->SetValue(filePath);
}