File: clGotoAnythingManager.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 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 (207 lines) | stat: -rw-r--r-- 7,425 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
#include "clGotoAnythingManager.h"

#include "GotoAnythingDlg.h"
#include "bitmap_loader.h"
#include "clKeyboardManager.h"
#include "clWorkspaceManager.h"
#include "cl_command_event.h"
#include "codelite_events.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "globals.h"
#include "imanager.h"

#include <algorithm>
#include <queue>
#include <wx/menu.h>
#include <wx/stc/stc.h>
#include <wx/xrc/xmlres.h>

clGotoAnythingManager::clGotoAnythingManager()
{
    EventNotifier::Get()->Bind(wxEVT_GOTO_ANYTHING_SELECTED, &clGotoAnythingManager::OnActionSelected, this);
    EventNotifier::Get()->Bind(wxEVT_GOTO_ANYTHING_SHOWING, &clGotoAnythingManager::OnShowing, this);
}

clGotoAnythingManager::~clGotoAnythingManager()
{
    EventNotifier::Get()->Unbind(wxEVT_GOTO_ANYTHING_SELECTED, &clGotoAnythingManager::OnActionSelected, this);
    EventNotifier::Get()->Unbind(wxEVT_GOTO_ANYTHING_SHOWING, &clGotoAnythingManager::OnShowing, this);
}

clGotoAnythingManager& clGotoAnythingManager::Get()
{
    static clGotoAnythingManager manager;
    return manager;
}

void clGotoAnythingManager::OnActionSelected(clGotoEvent& e)
{
    e.Skip();
    // Trigger the action
    const clGotoEntry& entry = e.GetEntry();
    if(entry.GetResourceID() != wxID_ANY) {
        wxCommandEvent evtAction(wxEVT_MENU, entry.GetResourceID());
        if(entry.IsCheckable()) {
            evtAction.SetInt(entry.IsChecked() ? 0 : 1); // Set the opposite value
        }
        EventNotifier::Get()->TopFrame()->GetEventHandler()->AddPendingEvent(evtAction);
    }
}

void clGotoAnythingManager::ShowDialog()
{
    // Let the plugins know that we are about to display the actions
    clGotoEvent evtShowing(wxEVT_GOTO_ANYTHING_SHOWING);
    evtShowing.SetEntries(GetActions());
    EventNotifier::Get()->ProcessEvent(evtShowing);

    // Let the plugins sort the content
    clGotoEvent evtSort(wxEVT_GOTO_ANYTHING_SORT_NEEDED);
    evtSort.GetEntries().swap(evtShowing.GetEntries());
    EventNotifier::Get()->ProcessEvent(evtSort);

#ifdef __WXMSW__
    if(clGetManager()->GetActiveEditor()) {
        clGetManager()->GetActiveEditor()->SetActive();
    }
#endif

    const std::vector<clGotoEntry>& entries = evtSort.GetEntries();
    GotoAnythingDlg dlg(EventNotifier::Get()->TopFrame(), entries);
    dlg.ShowModal();
}

std::vector<clGotoEntry> clGotoAnythingManager::GetActions()
{
    Initialise();
    std::vector<clGotoEntry> actions;
    std::for_each(
        m_actions.begin(), m_actions.end(),
        [&](const std::unordered_map<wxString, clGotoEntry>::value_type& vt) { actions.push_back(vt.second); });
    std::sort(actions.begin(), actions.end(),
              [&](const clGotoEntry& a, const clGotoEntry& b) { return a.GetDesc() < b.GetDesc(); });
    return actions;
}

void clGotoAnythingManager::Initialise()
{
    // Register the core actions
    m_actions.clear();

    wxFrame* mainFrame = EventNotifier::Get()->TopFrame();
    auto mb = clGetManager()->GetMenuBar();
    if(!mb) {
        return;
    }

    // Get list of menu entries
    std::queue<std::pair<wxString, wxMenu*>> q;
    for(size_t i = 0; i < mb->GetMenuCount(); ++i) {
        q.push(std::make_pair("", mb->GetMenu(i)));
    }

    static wxBitmap defaultBitmap = clGetManager()->GetStdIcons()->LoadBitmap("placeholder");
    while(!q.empty()) {
        wxMenu* menu = q.front().second;
        wxString prefix = q.front().first;
        q.pop();

        // Call this to ensure that any checkable items are marked as "checked" if needed
        menu->UpdateUI(mainFrame->GetEventHandler());

        const wxMenuItemList& L = menu->GetMenuItems();
        wxMenuItemList::const_iterator iter = L.begin();
        for(; iter != L.end(); ++iter) {
            wxMenuItem* menuItem = *iter;
            if(menuItem->GetSubMenu()) {
                wxString labelText = menuItem->GetItemLabelText();
                if((labelText == "Recent Files") || (labelText == "Recent Workspaces")) {
                    continue;
                }
                q.push(std::make_pair(menuItem->GetItemLabelText() + " > ", menuItem->GetSubMenu()));
            } else if((menuItem->GetId() != wxNOT_FOUND) && (menuItem->GetId() != wxID_SEPARATOR)) {
                clGotoEntry entry;
                wxString desc = menuItem->GetItemLabelText();
                entry.SetDesc(prefix + desc);
                if(menuItem->IsCheck()) {
                    entry.SetFlags(clGotoEntry::kItemCheck);
                    entry.SetChecked(menuItem->IsChecked());
                }
                if(menuItem->GetAccel()) {
                    entry.SetKeyboardShortcut(menuItem->GetAccel()->ToString());
                }
                entry.SetResourceID(menuItem->GetId());
                entry.SetBitmap(menuItem->GetBitmap().IsOk() ? menuItem->GetBitmap() : defaultBitmap);
                if(!entry.GetDesc().IsEmpty()) {
                    // Dont add empty entries
                    m_actions[entry.GetDesc()] = entry;
                }
            }
        }
    }
}

void clGotoAnythingManager::DoAddCurrentTabActions(clGotoEntry::Vec_t& V)
{
    IEditor* editor = clGetManager()->GetActiveEditor();
    if(editor) {
        {
            clGotoEntry entry(_("Current Tab > Close Tabs To The Right"), "", XRCID("close_tabs_to_the_right"));
            V.push_back(entry);
        }
        {
            clGotoEntry entry(_("Current Tab > Close Other Tabs"), "", XRCID("close_other_tabs"));
            V.push_back(entry);
        }
        {
            clGotoEntry entry(_("Current Tab > Duplicate Tab"), "", XRCID("duplicate_tab"));
            V.push_back(entry);
        }
        {
            clGotoEntry entry(_("Current Tab > Make it Read Only"), "", XRCID("mark_readonly"));
            // This a checkable item
            entry.SetCheckable(true);
            entry.SetChecked(editor->GetCtrl()->GetReadOnly());
            V.push_back(entry);
        }
        {
            clGotoEntry entry(_("Current Tab > Detach"), "", XRCID("ID_DETACH_EDITOR"));
            V.push_back(entry);
        }
        {
            clGotoEntry entry(_("Current Tab > Open Shell Here"), "", XRCID("open_shell_from_filepath"));
            V.push_back(entry);
        }
        {
            clGotoEntry entry(_("Current Tab > Open in File Explorer"), "", XRCID("open_file_explorer"));
            V.push_back(entry);
        }
        {
            clGotoEntry entry(_("Current Tab > Copy file name"), "", XRCID("copy_file_name_only"));
            V.push_back(entry);
        }
        {
            clGotoEntry entry(_("Current Tab > Copy fullpath (name, extension and directory)"), "",
                              XRCID("copy_file_name"));
            V.push_back(entry);
        }
        {
            clGotoEntry entry(_("Current Tab > Copy Path only (directory part only)"), "", XRCID("copy_file_path"));
            V.push_back(entry);
        }
        if(clWorkspaceManager::Get().IsWorkspaceOpened()) {
            clGotoEntry entry(_("Current Tab > Copy full path relative to workspace"), "",
                              XRCID("copy_file_relative_path_to_workspace"));
            V.push_back(entry);
        }
    }
}

void clGotoAnythingManager::OnShowing(clGotoEvent& e)
{
    e.Skip();
    // Add core actions which are not part of the menu bar
    clGotoEntry::Vec_t& V = e.GetEntries();
    DoAddCurrentTabActions(V);
}