File: acceltabledlg.cpp

package info (click to toggle)
codelite 12.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (237 lines) | stat: -rw-r--r-- 8,288 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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2008 by Eran Ifrah
// file name            : acceltabledlg.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "pluginmanager.h"
#include <wx/stdpaths.h>
#include <wx/tokenzr.h>
#include "globals.h"
#include <wx/ffile.h>
#include "newkeyshortcutdlg.h"
#include "acceltabledlg.h"
#include "manager.h"
#include <wx/imaglist.h>
#include <algorithm>
#include <wx/imaglist.h>
#include "windowattrmanager.h"
#include <wx/tokenzr.h>
#include <wx/tokenzr.h>
#include "fileutils.h"

//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------

AccelTableDlg::AccelTableDlg(wxWindow* parent)
    : AccelTableBaseDlg(parent)
{
    wxImageList* imageList = new wxImageList(16, 16);
    imageList->Add(PluginManager::Get()->GetStdIcons()->LoadBitmap(wxT("list-control/16/sort_down")));
    imageList->Add(PluginManager::Get()->GetStdIcons()->LoadBitmap(wxT("list-control/16/sort_up")));

    clKeyboardManager::Get()->GetAllAccelerators(m_accelMap);
    PopulateTable("");

    // center the dialog
    CentreOnParent();

    m_textCtrlFilter->SetFocus();

    SetName("AccelTableDlg");
    WindowAttrManager::Load(this);
}

void AccelTableDlg::PopulateTable(const wxString& filter)
{
    m_dvListCtrl->DeleteAllItems();

    MenuItemDataMap_t filteredMap;
    if(filter.IsEmpty()) {
        filteredMap = m_accelMap;
    } else {
        for(MenuItemDataMap_t::iterator iter = m_accelMap.begin(); iter != m_accelMap.end(); ++iter) {
            if(!IsMatchesFilter(filter, iter->second)) continue;
            filteredMap.insert(std::make_pair(iter->first, iter->second));
        }
    }

    if(filteredMap.empty()) return;

    // Add core entries
    for(MenuItemDataMap_t::const_iterator iter = filteredMap.begin(); iter != filteredMap.end(); ++iter) {
        const MenuItemData& mid = iter->second;

        wxVector<wxVariant> cols;
        wxString parentMenu = mid.parentMenu.BeforeFirst(':');
        if(parentMenu.IsEmpty()) {
            parentMenu = "<Global>";
        }

        cols.push_back(parentMenu);                // Parent menu
        cols.push_back(mid.action.AfterLast(':')); // Action description
        cols.push_back(mid.accel);                 // shortcut
        m_dvListCtrl->AppendItem(cols, (wxUIntPtr) new AccelItemData(mid));
    }

    m_dvListCtrl->GetColumn(0)->SetSortable(true);
    m_dvListCtrl->GetColumn(1)->SetSortable(true);
    m_dvListCtrl->GetColumn(2)->SetSortable(true);
}

void AccelTableDlg::OnButtonOk(wxCommandEvent& e)
{
    // apply changes
    clKeyboardManager::Get()->SetAccelerators(m_accelMap);
    clKeyboardManager::Get()->Update();
    EndModal(wxID_OK);
}

void AccelTableDlg::OnButtonDefaults(wxCommandEvent& e)
{
    // re-load the default key bindings settings
    m_accelMap.clear();
    m_textCtrlFilter->ChangeValue(""); // Clear the filter
    clKeyboardManager::Get()->RestoreDefaults();
    clKeyboardManager::Get()->GetAllAccelerators(m_accelMap);
    CallAfter(&AccelTableDlg::PopulateTable, wxString());
}

void AccelTableDlg::OnEditButton(wxCommandEvent& e)
{
    wxUnusedVar(e);
    DoItemActivated();
}

void AccelTableDlg::DoItemActivated()
{
    wxDataViewItem sel = m_dvListCtrl->GetSelection();
    CHECK_ITEM_RET(sel);

    AccelItemData* itemData = DoGetItemData(sel);
    if(!itemData) return;

    // build the selected entry
    MenuItemData mid = itemData->m_menuItemData;
    if(clKeyboardManager::Get()->PopupNewKeyboardShortcutDlg(this, mid) == wxID_OK) {
        // search the list for similar accelerator
        MenuItemData who;
        if(HasAccelerator(mid.accel, who)) {
            if(who.action == mid.action) {
                return;
            }
            if(wxMessageBox(wxString::Format(_("'%s' is already assigned to: '%s'\nWould you like to replace it?"),
                                mid.accel, who.action),
                   _("CodeLite"), wxYES_NO | wxCENTER | wxICON_QUESTION, this) != wxYES) {
                return;
            }

            // Remove the old entry
            wxDataViewItem oldItem = FindAccel(mid);
            if(oldItem.IsOk()) {
                AccelItemData* cd = DoGetItemData(oldItem);
                if(cd) {
                    cd->m_menuItemData.accel.Clear();
                    int row = m_dvListCtrl->ItemToRow(oldItem);
                    m_dvListCtrl->SetValue(wxString(), row, 2);

                    MenuItemDataMap_t::iterator iter = m_accelMap.find(cd->m_menuItemData.resourceID);
                    if(iter != m_accelMap.end()) {
                        iter->second.accel.Clear(); // Clear the accelerator
                    }
                }
            }
        }

        // Update the client data
        itemData->m_menuItemData = mid;

        // Update the UI
        int row = m_dvListCtrl->ItemToRow(sel);
        if(row == wxNOT_FOUND) return;
        m_dvListCtrl->SetValue(mid.accel, row, 2);

        // and update the map
        MenuItemDataMap_t::iterator iter = m_accelMap.find(itemData->m_menuItemData.resourceID);
        if(iter != m_accelMap.end()) {
            iter->second.accel = itemData->m_menuItemData.accel;
        }
    }
}

void AccelTableDlg::OnText(wxCommandEvent& event)
{
    wxUnusedVar(event);
    CallAfter(&AccelTableDlg::PopulateTable, m_textCtrlFilter->GetValue());
}

AccelTableDlg::~AccelTableDlg() {}

void AccelTableDlg::OnDVItemActivated(wxDataViewEvent& event)
{
    wxUnusedVar(event);
    DoItemActivated();
}

void AccelTableDlg::OnEditUI(wxUpdateUIEvent& event) { event.Enable(m_dvListCtrl->GetSelectedItemsCount()); }

bool AccelTableDlg::IsMatchesFilter(const wxString& filter, const MenuItemData& item)
{
    wxString lcFilter = filter.Lower();
    lcFilter.Trim().Trim(false);
    if(lcFilter.IsEmpty()) return true;

    wxString parentMenu = item.parentMenu;

    wxString haystack;
    haystack << parentMenu << " " << item.accel << " " << item.action;
    return FileUtils::FuzzyMatch(filter, haystack);
}

bool AccelTableDlg::HasAccelerator(const wxString& accel, MenuItemData& who)
{
    if(accel.IsEmpty()) return false;
    for(MenuItemDataMap_t::iterator iter = m_accelMap.begin(); iter != m_accelMap.end(); ++iter) {
        if(iter->second.accel == accel) {
            who = iter->second;
            return true;
        }
    }
    return false;
}

AccelItemData* AccelTableDlg::DoGetItemData(const wxDataViewItem& item)
{
    return reinterpret_cast<AccelItemData*>(m_dvListCtrl->GetItemData(item));
}

wxDataViewItem AccelTableDlg::FindAccel(const MenuItemData& mid)
{
    for(int i = 0; i < m_dvListCtrl->GetItemCount(); ++i) {
        wxDataViewItem item = m_dvListCtrl->RowToItem(i);
        if(!item.IsOk()) continue;

        AccelItemData* cd = DoGetItemData(item);
        if(cd && cd->m_menuItemData.accel == mid.accel) return item;
    }
    return wxDataViewItem();
}