File: clFileSystemWorkspaceView.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 136,244 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (321 lines) | stat: -rw-r--r-- 13,131 bytes parent folder | download | duplicates (2)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "clFileSystemWorkspaceView.hpp"

#include "StringUtils.h"
#include "clFileSystemWorkspace.hpp"
#include "clFileSystemWorkspaceDlg.h"
#include "clThemedButton.h"
#include "clToolBar.h"
#include "clWorkspaceView.h"
#include "codelite_events.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "globals.h"

#include <wx/app.h>

clFileSystemWorkspaceView::clFileSystemWorkspaceView(wxWindow* parent, const wxString& viewName)
    : clTreeCtrlPanel(parent)
    , m_config("fs-workspace-config")
{
    SetConfig(&m_config);
    SetNewFileTemplate("Untitled.cpp", wxStrlen("Untitled"));
    SetViewName(viewName);

    clBitmapList* images = GetToolBar()->GetBitmaps();
    GetToolBar()->AddTool(wxID_PREFERENCES, _("Settings"), images->Add("cog"), "", wxITEM_NORMAL);
    GetToolBar()->AddTool(XRCID("fsw_refresh_view"), _("Refresh View"), images->Add("file_reload"), "", wxITEM_NORMAL);

    GetToolBar()->Bind(wxEVT_TOOL, &clFileSystemWorkspaceView::OnSettings, this, wxID_PREFERENCES);
    GetToolBar()->AddSeparator();

    GetToolBar()->AddTool(XRCID("build_active_project"), _("Build active project"), images->Add("build"), wxEmptyString,
                          wxITEM_DROPDOWN);
    GetToolBar()->Bind(wxEVT_TOOL_DROPDOWN, &clFileSystemWorkspaceView::OnBuildActiveProjectDropdown, this,
                       XRCID("build_active_project"));

    GetToolBar()->AddTool(XRCID("stop_active_project_build"), _("Stop current build"), images->Add("stop"),
                          wxEmptyString, wxITEM_NORMAL);
    GetToolBar()->AddSeparator();

    GetToolBar()->AddTool(XRCID("execute_no_debug"), _("Run program"), images->Add("execute"));
    GetToolBar()->AddTool(XRCID("stop_executed_program"), _("Stop running program"), images->Add("stop"));

    // these events are connected using the App object (to support keyboard shortcuts)
    wxTheApp->Bind(wxEVT_TOOL, &clFileSystemWorkspaceView::OnRefreshView, this, XRCID("fsw_refresh_view"));
    wxTheApp->Bind(wxEVT_UPDATE_UI, &clFileSystemWorkspaceView::OnRefreshViewUI, this, XRCID("fsw_refresh_view"));

    GetToolBar()->Realize();

    m_choiceConfigs = new wxChoice(this, wxID_ANY);
    m_choiceConfigs->Bind(wxEVT_CHOICE, &clFileSystemWorkspaceView::OnChoiceConfigSelected, this);
    GetSizer()->Insert(0, m_choiceConfigs, 0, wxEXPAND | wxALL, 5);

    // Hide hidden folders and files
    m_options &= ~kShowHiddenFiles;
    m_options &= ~kShowHiddenFolders;

    EventNotifier::Get()->Bind(wxEVT_CONTEXT_MENU_FOLDER, &clFileSystemWorkspaceView::OnContextMenu, this);
    EventNotifier::Get()->Bind(wxEVT_BUILD_STARTED, &clFileSystemWorkspaceView::OnBuildStarted, this);
    EventNotifier::Get()->Bind(wxEVT_BUILD_ENDED, &clFileSystemWorkspaceView::OnBuildEnded, this);
    EventNotifier::Get()->Bind(wxEVT_PROGRAM_STARTED, &clFileSystemWorkspaceView::OnProgramStarted, this);
    EventNotifier::Get()->Bind(wxEVT_PROGRAM_TERMINATED, &clFileSystemWorkspaceView::OnProgramStopped, this);
    EventNotifier::Get()->Bind(wxEVT_FINDINFILES_DLG_DISMISSED, &clFileSystemWorkspaceView::OnFindInFilesDismissed,
                               this);
    EventNotifier::Get()->Bind(wxEVT_FINDINFILES_DLG_SHOWING, &clFileSystemWorkspaceView::OnFindInFilesShowing, this);
    EventNotifier::Get()->Bind(wxEVT_SYS_COLOURS_CHANGED, &clFileSystemWorkspaceView::OnThemeChanged, this);
}

clFileSystemWorkspaceView::~clFileSystemWorkspaceView()
{
    EventNotifier::Get()->Unbind(wxEVT_CONTEXT_MENU_FOLDER, &clFileSystemWorkspaceView::OnContextMenu, this);
    EventNotifier::Get()->Unbind(wxEVT_BUILD_STARTED, &clFileSystemWorkspaceView::OnBuildStarted, this);
    EventNotifier::Get()->Unbind(wxEVT_BUILD_ENDED, &clFileSystemWorkspaceView::OnBuildEnded, this);
    EventNotifier::Get()->Unbind(wxEVT_PROGRAM_STARTED, &clFileSystemWorkspaceView::OnProgramStarted, this);
    EventNotifier::Get()->Unbind(wxEVT_PROGRAM_TERMINATED, &clFileSystemWorkspaceView::OnProgramStopped, this);
    m_choiceConfigs->Unbind(wxEVT_CHOICE, &clFileSystemWorkspaceView::OnChoiceConfigSelected, this);
    GetToolBar()->Unbind(wxEVT_TOOL_DROPDOWN, &clFileSystemWorkspaceView::OnBuildActiveProjectDropdown, this,
                         XRCID("build_active_project"));
    EventNotifier::Get()->Unbind(wxEVT_FINDINFILES_DLG_DISMISSED, &clFileSystemWorkspaceView::OnFindInFilesDismissed,
                                 this);
    EventNotifier::Get()->Unbind(wxEVT_FINDINFILES_DLG_SHOWING, &clFileSystemWorkspaceView::OnFindInFilesShowing, this);
    EventNotifier::Get()->Unbind(wxEVT_SYS_COLOURS_CHANGED, &clFileSystemWorkspaceView::OnThemeChanged, this);
}

void clFileSystemWorkspaceView::OnFolderDropped(clCommandEvent& event)
{
    // Add only non existent folders to the workspace
    const wxArrayString& folders = event.GetStrings();
    if(folders.size() != 1) {
        return;
    }

    clFileSystemWorkspace::Get().New(folders.Item(0));
    ::clGetManager()->GetWorkspaceView()->SelectPage(GetViewName());
}

void clFileSystemWorkspaceView::OnContextMenu(clContextMenuEvent& event)
{
    event.Skip();
    wxMenu* menu = event.GetMenu();
    m_selectedFolders.clear();

    // these entries are added even when not fired from our workspace view, but as along as we have an opened workspace
    if(clFileSystemWorkspace::Get().IsOpen()) {
        clTreeCtrlPanel* tree = dynamic_cast<clTreeCtrlPanel*>(event.GetEventObject());
        if(tree) {
            wxArrayString selected_files;
            tree->GetSelections(m_selectedFolders, selected_files);
            wxUnusedVar(selected_files);
        }

        wxMenu* cc_menu = new wxMenu;
        if(!m_selectedFolders.empty()) {
            cc_menu->Append(XRCID("fs_add_cc_inculde"), _("Add path to code completion"), wxEmptyString, wxITEM_NORMAL);
            cc_menu->Bind(wxEVT_MENU, &clFileSystemWorkspaceView::OnAddIncludePath, this, XRCID("fs_add_cc_inculde"));
        }
        cc_menu->Append(XRCID("fs_create_compile_flags"), _("Generate compile_flags.txt file..."), wxEmptyString,
                        wxITEM_NORMAL);
        cc_menu->Bind(wxEVT_MENU, &clFileSystemWorkspaceView::OnCreateCompileFlagsFile, this,
                      XRCID("fs_create_compile_flags"));
        menu->AppendSubMenu(cc_menu, _("Code Completion"), wxEmptyString);
        menu->AppendSeparator();
        menu->Append(XRCID("fs_exclude_path"), _("Exclude this folder"), wxEmptyString, wxITEM_NORMAL);
        menu->Bind(wxEVT_MENU, &clFileSystemWorkspaceView::OnExcludePath, this, XRCID("fs_exclude_path"));
    }

    if(event.GetEventObject() == this) {
        event.Skip(false);
        menu->AppendSeparator();
        menu->Append(wxID_PREFERENCES, _("Settings..."), _("Settings"), wxITEM_NORMAL);
        menu->Bind(wxEVT_MENU, &clFileSystemWorkspaceView::OnSettings, this, wxID_PREFERENCES);
    }
}

void clFileSystemWorkspaceView::OnCloseFolder(wxCommandEvent& event)
{
    wxUnusedVar(event);
    clFileSystemWorkspace::Get().CallAfter(&clFileSystemWorkspace::Close);
}

void clFileSystemWorkspaceView::OnSettings(wxCommandEvent& event)
{
    clFileSystemWorkspaceDlg dlg(EventNotifier::Get()->TopFrame());
    if(dlg.ShowModal() != wxID_OK) {
        return;
    }
}

void clFileSystemWorkspaceView::UpdateConfigs(const wxArrayString& configs, const wxString& selectedConfig)
{
    m_configs = configs;
    m_choiceConfigs->Set(configs);
    m_choiceConfigs->SetStringSelection(selectedConfig);
}

void clFileSystemWorkspaceView::OnChoiceConfigSelected(wxCommandEvent& event)
{
    int sel = event.GetSelection();
    if(sel == wxNOT_FOUND) {
        return;
    }

    m_choiceConfigs->SetSelection(sel);
    clFileSystemWorkspace::Get().GetSettings().SetSelectedConfig(m_choiceConfigs->GetStringSelection());
    clFileSystemWorkspace::Get().Save(true);
}

void clFileSystemWorkspaceView::OnRefreshView(wxCommandEvent& event)
{
    wxUnusedVar(event);
    // refresh the entire view
    clTreeCtrlPanel::RefreshTree();
    // notify update
    clFileSystemWorkspace::Get().FileSystemUpdated();
}

void clFileSystemWorkspaceView::OnBuildStarted(clBuildEvent& event)
{
    event.Skip();
    m_buildInProgress = true;
}

void clFileSystemWorkspaceView::OnBuildEnded(clBuildEvent& event)
{
    event.Skip();
    m_buildInProgress = false;
}

void clFileSystemWorkspaceView::OnProgramStarted(clExecuteEvent& event)
{
    event.Skip();
    m_runInProgress = true;
}

void clFileSystemWorkspaceView::OnProgramStopped(clExecuteEvent& event)
{
    event.Skip();
    m_runInProgress = false;
}

void clFileSystemWorkspaceView::OnBuildActiveProjectDropdown(wxCommandEvent& event)
{
    clDEBUG() << "OnBuildActiveProjectDropdown called";

    // dont display default menu
    wxUnusedVar(event);
    // we dont allow showing the dropdown during build process
    if(m_buildInProgress) {
        return;
    }
    clGetManager()->ShowBuildMenu(m_toolbar, XRCID("build_active_project"));
}

void clFileSystemWorkspaceView::OnFindInFilesDismissed(clFindInFilesEvent& event)
{
    event.Skip();
    if(clFileSystemWorkspace::Get().IsOpen()) {
        clConfig::Get().Write("FindInFiles/FS/Mask", event.GetFileMask());
        clConfig::Get().Write("FindInFiles/FS/LookIn", event.GetPaths());
    }
}

void clFileSystemWorkspaceView::OnFindInFilesShowing(clFindInFilesEvent& event)
{
    event.Skip();
    if(clFileSystemWorkspace::Get().IsOpen()) {
        // Load the C++ workspace values from the configuration
        event.SetFileMask(clConfig::Get().Read("FindInFiles/FS/Mask",
                                               wxString("*.c;*.cpp;*.cxx;*.cc;*.h;*.hpp;*.inc;*.mm;*.m;*.xrc;"
                                                        "*.xml;*.json;*.sql;*.txt;*.plist;CMakeLists.txt;*.rc;*.iss")));
        event.SetPaths(clConfig::Get().Read("FindInFiles/FS/LookIn", wxString("<Entire Workspace>")));
    }
}

void clFileSystemWorkspaceView::OnAddIncludePath(wxCommandEvent& event)
{
    wxUnusedVar(event);

    // get list of selected folders in the UI
    wxArrayString configs = clFileSystemWorkspace::Get().GetSettings().GetConfigs();
    for(const wxString& config : configs) {
        auto config_ptr = clFileSystemWorkspace::Get().GetSettings().GetConfig(config);
        DoAddIncludePathsToConfig(config_ptr, m_selectedFolders);
    }
    clFileSystemWorkspace::Get().Save(true);
}

void clFileSystemWorkspaceView::DoAddIncludePathsToConfig(clFileSystemWorkspaceConfig::Ptr_t config,
                                                          const wxArrayString& paths)
{
    // build a map with list of folders already included
    // for this configuration
    const auto& files = config->GetCompileFlags();
    wxStringSet_t map;
    for(auto file : files) {
        if(file.StartsWith("-I")) {
            file.Remove(0, 2);
        }
        map.insert(file);
    }

    wxArrayString pathsToAdd;
    for(const auto& selected_path : paths) {
        // only add folders that do not already exist for this configuration
        if(map.count(selected_path) == 0) {
            pathsToAdd.Add("-I" + selected_path);
        }
    }

    // Add this to all the configurations
    wxArrayString compile_flags_arr = config->GetCompileFlags();
    compile_flags_arr.insert(compile_flags_arr.end(), pathsToAdd.begin(), pathsToAdd.end());
    config->SetCompileFlags(compile_flags_arr);
}

void clFileSystemWorkspaceView::OnCreateCompileFlagsFile(wxCommandEvent& event)
{
    // proxy to the workspace method
    wxUnusedVar(event);
    clFileSystemWorkspace::Get().CreateCompileFlagsFile();
}

void clFileSystemWorkspaceView::OnExcludePath(wxCommandEvent& event)
{
    // add the selected folders to the exclude path
    // get list of selected folders in the UI
    wxArrayString configs = clFileSystemWorkspace::Get().GetSettings().GetConfigs();
    for(const wxString& config : configs) {
        auto config_ptr = clFileSystemWorkspace::Get().GetSettings().GetConfig(config);

        // get the list of exclude paths and convert them into a SET to avoid duplications
        // next, we add each selected folder to the exclude list
        wxStringSet_t S;
        wxArrayString excludePathsArr = StringUtils::BuildArgv(config_ptr->GetExecludePaths());
        S.reserve(excludePathsArr.size());
        S.insert(excludePathsArr.begin(), excludePathsArr.end());
        for(wxString folder : m_selectedFolders) {
            // Make it relative to the workspace
            wxFileName fn(folder, "dummy");
            fn.MakeRelativeTo(clFileSystemWorkspace::Get().GetDir());
            folder = fn.GetPath();
            if(S.count(folder) == 0) {
                S.insert(folder);
                excludePathsArr.Add(folder);
            }
        }
        config_ptr->SetExcludePaths(::wxJoin(excludePathsArr, ';'));
    }
    clFileSystemWorkspace::Get().Save(true);
}

void clFileSystemWorkspaceView::OnThemeChanged(clCommandEvent& event)
{
    event.Skip();
    SetBackgroundColour(clSystemSettings::GetDefaultPanelColour());
    Refresh();
}

void clFileSystemWorkspaceView::OnRefreshViewUI(wxUpdateUIEvent& event)
{
    event.Enable(clFileSystemWorkspace::Get().IsOpen());
}