| 1 |
#include "wxcTreeView.h" |
| 2 |
|
| 3 |
#include "EventsEditorDlg.h" |
| 4 |
#include "allocator_mgr.h" |
| 5 |
#include "drawingutils.h" |
| 6 |
#include "event_notifier.h" |
| 7 |
#include "globals.h" |
| 8 |
#include "plugin.h" |
| 9 |
#include "workspace.h" |
| 10 |
#include "wxcrafter.h" |
| 11 |
#include "wxgui_bitmaploader.h" |
| 12 |
#include "wxgui_defs.h" |
| 13 |
#include "wxgui_helpers.h" |
| 14 |
#include "wxguicraft_main_view.h" |
| 15 |
|
| 16 |
#include <wx/aui/auibar.h> |
| 17 |
|
| 18 |
const wxEventType wxEVT_SHOW_WXCRAFTER_DESIGNER = wxNewEventType(); |
| 19 |
|
| 20 |
BEGIN_EVENT_TABLE(wxcTreeView, wxcTreeViewBaseClass) |
| 21 |
EVT_TOOL(ID_OPEN_WXGUI_PROJECT, wxcTreeView::OnOpen) |
| 22 |
EVT_UPDATE_UI(ID_OPEN_WXGUI_PROJECT, wxcTreeView::OnOpenUI) |
| 23 |
END_EVENT_TABLE() |
| 24 |
|
| 25 |
wxcTreeView::wxcTreeView(wxWindow* parent, wxCrafterPlugin* plugin) |
| 26 |
: wxcTreeViewBaseClass(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, |
| 27 |
wxCrafter::GetControlBorder() | wxTAB_TRAVERSAL) |
| 28 |
, m_plugin(plugin) |
| 29 |
, m_loadingProject(false) |
| 30 |
{ |
| 31 |
m_treeControls->SetImageList(Allocator::Instance()->GetImageList()); |
| 32 |
m_treeControls->SetSortFunction(nullptr); |
| 33 |
m_treeControls->AddRoot(_("wxCrafter Project"), 0, 0); |
| 34 |
m_eventsPane = new EventsEditorPane(m_splitterPageEvents, NULL, plugin); |
| 35 |
m_splitterPageEvents->GetSizer()->Add(m_eventsPane, 1, wxEXPAND | wxALL, 2); |
| 36 |
|
| 37 |
int treeviewSashPos = wxcSettings::Get().GetTreeviewSashPos(); |
| 38 |
if(treeviewSashPos != wxNOT_FOUND) |
| 39 |
m_splitter347->SetSashPosition(treeviewSashPos); |
| 40 |
|
| 41 |
EventNotifier::Get()->Connect(wxEVT_WXGUI_PROJECT_SAVED, wxCommandEventHandler(wxcTreeView::OnProjectSaved), NULL, |
| 42 |
this); |
| 43 |
EventNotifier::Get()->Connect(wxEVT_WXGUI_PROJECT_CLOSED, wxCommandEventHandler(wxcTreeView::OnProjectClosed), NULL, |
| 44 |
this); |
| 45 |
EventNotifier::Get()->Bind(wxEVT_WORKSPACE_LOADED, &wxcTreeView::OnWorkspaceLoaded, this); |
| 46 |
EventNotifier::Get()->Bind(wxEVT_WORKSPACE_CLOSED, &wxcTreeView::OnWorkspaceClosed, this); |
| 47 |
} |
| 48 |
|
| 49 |
wxcTreeView::~wxcTreeView() |
| 50 |
{ |
| 51 |
wxcSettings::Get().SetTreeviewSashPos(m_splitter347->GetSashPosition()); |
| 52 |
wxcSettings::Get().Save(); |
| 53 |
|
| 54 |
EventNotifier::Get()->Disconnect(wxEVT_WXGUI_PROJECT_SAVED, wxCommandEventHandler(wxcTreeView::OnProjectSaved), |
| 55 |
NULL, this); |
| 56 |
EventNotifier::Get()->Disconnect(wxEVT_WXGUI_PROJECT_CLOSED, wxCommandEventHandler(wxcTreeView::OnProjectClosed), |
| 57 |
NULL, this); |
| 58 |
EventNotifier::Get()->Unbind(wxEVT_WORKSPACE_LOADED, &wxcTreeView::OnWorkspaceLoaded, this); |
| 59 |
EventNotifier::Get()->Unbind(wxEVT_WORKSPACE_CLOSED, &wxcTreeView::OnWorkspaceClosed, this); |
| 60 |
} |
| 61 |
|
| 62 |
void wxcTreeView::OnItemSelected(wxTreeEvent& event) { event.Skip(); } |
| 63 |
|
| 64 |
void wxcTreeView::OnOpen(wxCommandEvent& e) |
| 65 |
{ |
| 66 |
wxUnusedVar(e); |
| 67 |
LoadProject(wxFileName()); |
| 68 |
} |
| 69 |
|
| 70 |
void wxcTreeView::OnOpenUI(wxUpdateUIEvent& e) { e.Enable(true); } |
| 71 |
|
| 72 |
void wxcTreeView::LoadProject(const wxFileName& filname) |
| 73 |
{ |
| 74 |
if(m_loadingProject) |
| 75 |
return; |
| 76 |
m_loadingProject = true; |
| 77 |
|
| 78 |
wxCommandEvent showDesignerEvent(wxEVT_SHOW_WXCRAFTER_DESIGNER); |
| 79 |
EventNotifier::Get()->ProcessEvent(showDesignerEvent); |
| 80 |
|
| 81 |
wxCommandEvent evtOpen(wxEVT_WXC_OPEN_PROJECT); |
| 82 |
if(filname.IsOk()) { |
| 83 |
evtOpen.SetString(filname.GetFullPath()); |
| 84 |
} |
| 85 |
EventNotifier::Get()->ProcessEvent(evtOpen); |
| 86 |
m_loadingProject = false; |
| 87 |
} |
| 88 |
|
| 89 |
void wxcTreeView::AddForm(const NewFormDetails& fd) |
| 90 |
{ |
| 91 |
// Top level window |
| 92 |
int imgId = Allocator::Instance()->GetImageId(fd.formType); |
| 93 |
|
| 94 |
wxcWidget* toplevel = Allocator::Instance()->Create(fd.formType); |
| 95 |
toplevel->SetName(fd.className); |
| 96 |
toplevel->SetFilename(fd.filename); |
| 97 |
toplevel->SetVirtualFolder(fd.virtualFolder); |
| 98 |
toplevel->SetTitle(fd.formTitle); |
| 99 |
toplevel->SetPropertyString(PROP_INHERITED_CLASS, fd.inheritedClassName); |
| 100 |
|
| 101 |
wxTreeItemId item = m_treeControls->AppendItem(m_treeControls->GetRootItem(), toplevel->GetName(), imgId, imgId, |
| 102 |
new GUICraftItemData(toplevel)); |
| 103 |
m_treeControls->SelectItem(item); |
| 104 |
|
| 105 |
wxCommandEvent evt(wxEVT_REFRESH_DESIGNER); |
| 106 |
EventNotifier::Get()->AddPendingEvent(evt); |
| 107 |
} |
| 108 |
|
| 109 |
void wxcTreeView::SaveProject() |
| 110 |
{ |
| 111 |
wxCommandEvent evtSave(wxEVT_WXC_SAVE_PROJECT); |
| 112 |
EventNotifier::Get()->ProcessEvent(evtSave); |
| 113 |
} |
| 114 |
|
| 115 |
void wxcTreeView::CloseProject(bool saveBeforeClose) |
| 116 |
{ |
| 117 |
if(wxcProjectMetadata::Get().GetProjectFile().IsEmpty()) |
| 118 |
return; |
| 119 |
|
| 120 |
if(saveBeforeClose) { |
| 121 |
SaveProject(); |
| 122 |
} |
| 123 |
|
| 124 |
wxCommandEvent closeEvent(wxEVT_WXC_CLOSE_PROJECT); |
| 125 |
EventNotifier::Get()->ProcessEvent(closeEvent); |
| 126 |
} |
| 127 |
|
| 128 |
void wxcTreeView::OnProjectSaved(wxCommandEvent& e) |
| 129 |
{ |
| 130 |
e.Skip(); |
| 131 |
m_treeControls->SetItemText(m_treeControls->GetRootItem(), wxcProjectMetadata::Get().GetProjectFile()); |
| 132 |
} |
| 133 |
|
| 134 |
void wxcTreeView::OnWxcpFileSelected(wxCommandEvent& event) |
| 135 |
{ |
| 136 |
if(!clCxxWorkspaceST::Get()->IsOpen()) |
| 137 |
return; |
| 138 |
|
| 139 |
if(m_comboBoxFiles->GetSelection() == wxNOT_FOUND) |
| 140 |
return; |
| 141 |
|
| 142 |
wxFileName wspfile = clCxxWorkspaceST::Get()->GetWorkspaceFileName(); |
| 143 |
wxFileName wxcpfile(m_comboBoxFiles->GetStringSelection()); |
| 144 |
wxcpfile.MakeAbsolute(wspfile.GetPath()); |
| 145 |
|
| 146 |
LoadProject(wxcpfile.GetFullPath()); |
| 147 |
// And restore the file list |
| 148 |
CallAfter(&wxcTreeView::DoRefreshFileList, true); |
| 149 |
} |
| 150 |
|
| 151 |
void wxcTreeView::OnRefreshWxcpFiles(wxCommandEvent& event) { DoRefreshFileList(); } |
| 152 |
|
| 153 |
void wxcTreeView::DoRefreshFileList(bool reloadFileList) |
| 154 |
{ |
| 155 |
wxBusyCursor bc; |
| 156 |
if(!clCxxWorkspaceST::Get()->IsOpen()) |
| 157 |
return; |
| 158 |
wxString filter = m_comboBoxFiles->GetValue().Lower(); |
| 159 |
wxString cbValue = m_comboBoxFiles->GetValue(); |
| 160 |
|
| 161 |
filter.Trim().Trim(false); |
| 162 |
|
| 163 |
if(m_fileList.IsEmpty() || reloadFileList) { |
| 164 |
wxArrayString allFiles; |
| 165 |
clCxxWorkspaceST::Get()->GetWorkspaceFiles(allFiles); |
| 166 |
|
| 167 |
wxArrayString options; |
| 168 |
wxFileName wspfile = clCxxWorkspaceST::Get()->GetWorkspaceFileName(); |
| 169 |
for(size_t i = 0; i < allFiles.GetCount(); ++i) { |
| 170 |
if(FileExtManager::GetType(allFiles.Item(i)) == FileExtManager::TypeWxCrafter) { |
| 171 |
wxFileName f(allFiles.Item(i)); |
| 172 |
f.MakeRelativeTo(wspfile.GetPath()); |
| 173 |
options.Add(f.GetFullPath()); |
| 174 |
} |
| 175 |
} |
| 176 |
m_fileList.swap(options); |
| 177 |
} |
| 178 |
|
| 179 |
// Filter non intersting fields |
| 180 |
wxArrayString filteredValues; |
| 181 |
for(size_t i = 0; i < m_fileList.size(); ++i) { |
| 182 |
const wxString lcValue = m_fileList.Item(i).Lower(); |
| 183 |
if(reloadFileList || filter.IsEmpty() || lcValue.Contains(filter)) { |
| 184 |
filteredValues.Add(m_fileList.Item(i)); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
m_comboBoxFiles->Clear(); |
| 189 |
m_comboBoxFiles->Append(filteredValues); |
| 190 |
} |
| 191 |
|
| 192 |
void wxcTreeView::OnWorkspaceLoaded(clWorkspaceEvent& e) |
| 193 |
{ |
| 194 |
e.Skip(); |
| 195 |
DoRefreshFileList(); |
| 196 |
} |
| 197 |
|
| 198 |
void wxcTreeView::OnWorkspaceClosed(clWorkspaceEvent& e) |
| 199 |
{ |
| 200 |
e.Skip(); |
| 201 |
m_comboBoxFiles->Clear(); |
| 202 |
} |
| 203 |
|
| 204 |
void wxcTreeView::OnWorkspaceOpenUI(wxUpdateUIEvent& event) { event.Enable(clCxxWorkspaceST::Get()->IsOpen()); } |
| 205 |
|
| 206 |
void wxcTreeView::OnItemLabelEditEnd(wxTreeEvent& event) // NB I don't think this method is ever called... |
| 207 |
{ |
| 208 |
wxString new_name = event.GetLabel(); |
| 209 |
if(new_name.IsEmpty()) { |
| 210 |
event.Veto(); |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
event.Skip(); |
| 215 |
// set the new name |
| 216 |
GUICraftItemData* itemData = dynamic_cast<GUICraftItemData*>(m_treeControls->GetItemData(event.GetItem())); |
| 217 |
CHECK_POINTER(itemData); |
| 218 |
CHECK_POINTER(itemData->m_wxcWidget); |
| 219 |
|
| 220 |
itemData->m_wxcWidget->SetName(new_name); |
| 221 |
m_treeControls->SetItemText(event.GetItem(), new_name); |
| 222 |
|
| 223 |
wxCommandEvent e(wxEVT_REFRESH_PROPERTIES_VIEW); |
| 224 |
EventNotifier::Get()->AddPendingEvent(e); |
| 225 |
} |
| 226 |
|
| 227 |
void wxcTreeView::OnProjectClosed(wxCommandEvent& event) |
| 228 |
{ |
| 229 |
event.Skip(); |
| 230 |
if(!wxcProjectMetadata::Get().IsLoaded()) { |
| 231 |
m_comboBoxFiles->SetSelection(wxNOT_FOUND); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
void wxcTreeView::OnSashPositionChanged(wxSplitterEvent& event) |
| 236 |
{ |
| 237 |
event.Skip(); |
| 238 |
m_eventsPane->SplitterPositionChanged(); |
| 239 |
} |
| 240 |
|
| 241 |
void wxcTreeView::OnChar(wxKeyEvent& event) |
| 242 |
{ |
| 243 |
if(event.GetKeyCode() == WXK_DELETE) { |
| 244 |
// Fake the corresponding menu event and throw it at the tree, which is Connect()ed to GUICraftMainPanel |
| 245 |
wxCommandEvent e(wxEVT_COMMAND_MENU_SELECTED, ID_DELETE_NODE); |
| 246 |
wxPostEvent(m_treeControls, e); |
| 247 |
} else { |
| 248 |
event.Skip(); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
void wxcTreeView::OnWxcpComboxTextEnter(wxCommandEvent& event) |
| 253 |
{ |
| 254 |
DoRefreshFileList(false); |
| 255 |
m_comboBoxFiles->CallAfter(&wxComboBox::Popup); |
| 256 |
} |