This repository was archived by the owner on Jul 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSourcePanel.cpp
More file actions
143 lines (128 loc) · 3.84 KB
/
SourcePanel.cpp
File metadata and controls
143 lines (128 loc) · 3.84 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
#include "SourcePanel.hpp"
#include "SourceDialog.hpp"
#include "common.hpp"
BEGIN_EVENT_TABLE(SourcePanel, wxPanel)
EVT_SIZE(SourcePanel::on_resize)
EVT_LIST_COL_BEGIN_DRAG(ID_URIList, SourcePanel::on_col_resize)
EVT_BUTTON(ID_URIAdd, SourcePanel::on_add)
EVT_BUTTON(ID_URIEdit, SourcePanel::on_edit)
EVT_BUTTON(ID_URIDel, SourcePanel::on_del)
EVT_LIST_ITEM_ACTIVATED(ID_URIList, SourcePanel::on_activate)
EVT_LIST_KEY_DOWN(ID_URIList, SourcePanel::on_key)
END_EVENT_TABLE()
SourcePanel::SourcePanel(wxWindow* parent, MetalinkEditor& editor)
: wxPanel(parent), editor_(editor)
{
create_widgets();
update();
}
void SourcePanel::create_widgets()
{
// List
list_ = new wxListCtrl(this, ID_URIList, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_VRULES | wxLC_HRULES);
list_->InsertColumn(0, wxT("URI"));
list_->InsertColumn(1, wxT("Location"));
list_->InsertColumn(2, wxT("Priority"));
// Buttons
wxButton* btn_add = new wxButton(this, ID_URIAdd, wxT("Add"));
wxButton* btn_edit = new wxButton(this, ID_URIEdit, wxT("Edit"));
wxButton* btn_delete = new wxButton(this, ID_URIDel, wxT("Delete"));
// Layout
wxBoxSizer* sizer2 = new wxBoxSizer(wxHORIZONTAL);
sizer2->AddStretchSpacer();
sizer2->Add(btn_add);
sizer2->Add(btn_edit);
sizer2->Add(btn_delete);
wxBoxSizer* sizer1 = new wxBoxSizer(wxVERTICAL);
sizer1->Add(list_, 1, wxEXPAND, 0);
sizer1->Add(sizer2, 0, wxEXPAND, 0);
SetSizerAndFit(sizer1);
Layout();
}
long SourcePanel::get_selected()
{
return list_->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
}
void SourcePanel::on_resize(wxSizeEvent& event)
{
Layout();
// This is needed to get rid of some visual artifacts (wxGTK).
update_layout();
}
void SourcePanel::update_layout()
{
list_->SetColumnWidth(0, wxLIST_AUTOSIZE);
list_->SetColumnWidth(1, 80);
list_->SetColumnWidth(2, 80);
if(list_->GetColumnWidth(0) < 80) {
list_->SetColumnWidth(0, 80);
}
}
void SourcePanel::on_col_resize(wxListEvent& event)
{
event.Veto();
}
void SourcePanel::on_activate(wxListEvent& event)
{
edit();
}
void SourcePanel::on_key(wxListEvent& event)
{
if(event.GetKeyCode() == WXK_DELETE) delete_sources();
}
void SourcePanel::on_add(wxCommandEvent& event)
{
SourceDialog dlg(wxT("Add source"), MetalinkSource());
if(dlg.ShowModal() == wxID_OK) {
MetalinkSource source = dlg.get_source();
MetalinkFile file = editor_.get_file();
file.add_source(source);
editor_.set_file(file);
}
}
void SourcePanel::on_edit(wxCommandEvent& event)
{
edit();
}
void SourcePanel::on_del(wxCommandEvent& event)
{
delete_sources();
}
void SourcePanel::delete_sources()
{
long index = -1;
long deleted = 0;
MetalinkFile file = editor_.get_file();
while(true) {
index = list_->GetNextItem(index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
if(index == -1) break;
file.del_source(index - deleted);
deleted++;
}
editor_.set_file(file);
}
void SourcePanel::edit()
{
long selected = get_selected();
if(selected == -1) return;
MetalinkFile file = editor_.get_file();
SourceDialog dlg(wxT("Edit source"), file.get_source(selected));
if(dlg.ShowModal() == wxID_OK) {
MetalinkSource source = dlg.get_source();
file.set_source(selected, source);
editor_.set_file(file);
}
}
void SourcePanel::update()
{
list_->DeleteAllItems();
MetalinkFile file = editor_.get_file();
std::vector<MetalinkSource> sources = file.get_sources();
for(int i = 0; i < sources.size(); i++) {
MetalinkSource& source = sources.at(i);
list_->InsertItem(i, source.get_uri());
list_->SetItem(i, 1, source.get_location());
list_->SetItem(i, 2, source.get_prioritystr());
}
update_layout();
}