File: clKeyboardManager.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 (569 lines) | stat: -rw-r--r-- 16,829 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#include "clKeyboardManager.h"

#include "clKeyboardBindingConfig.h"
#include "cl_standard_paths.h"
#include "codelite_events.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "fileutils.h"
#include "globals.h"
#include "imanager.h"
#include "macros.h"
#include "newkeyshortcutdlg.h"
#include "wxCustomControls.hpp"

#include <algorithm>
#include <wx/app.h>
#include <wx/log.h>
#include <wx/menu.h>
#include <wx/tokenzr.h>
#include <wx/xrc/xmlres.h>

wxDEFINE_EVENT(wxEVT_KEYBOARD_ACCEL_INIT_DONE, clCommandEvent);

clKeyboardManager::clKeyboardManager()
{
    EventNotifier::Get()->Bind(wxEVT_INIT_DONE, &clKeyboardManager::OnStartupCompleted, this);

    // A-Z
    for(size_t i = 65; i < 91; ++i) {
        char asciiCode = (char)i;
        m_keyCodes.insert(wxString() << asciiCode);
    }

    // 0-9
    for(size_t i = 48; i < 58; ++i) {
        char asciiCode = (char)i;
        m_keyCodes.insert(wxString() << asciiCode);
    }

    // Special chars
    m_keyCodes.insert("`");
    m_keyCodes.insert("-");
    m_keyCodes.insert("*");
    m_keyCodes.insert("=");
    m_keyCodes.insert("BACK");
    m_keyCodes.insert("TAB");
    m_keyCodes.insert("[");
    m_keyCodes.insert("]");
    m_keyCodes.insert("ENTER");
    m_keyCodes.insert("CAPITAL");
    m_keyCodes.insert("SCROLL_LOCK");
    m_keyCodes.insert("PAUSE");
    m_keyCodes.insert(";");
    m_keyCodes.insert("'");
    m_keyCodes.insert("\\");
    m_keyCodes.insert(",");
    m_keyCodes.insert(".");
    m_keyCodes.insert("/");
    m_keyCodes.insert("SPACE");
    m_keyCodes.insert("INS");
    m_keyCodes.insert("HOME");
    m_keyCodes.insert("PGUP");
    m_keyCodes.insert("PGDN");
    m_keyCodes.insert("DEL");
    m_keyCodes.insert("END");
    m_keyCodes.insert("UP");
    m_keyCodes.insert("DOWN");
    m_keyCodes.insert("RIGHT");
    m_keyCodes.insert("LEFT");
    m_keyCodes.insert("F1");
    m_keyCodes.insert("F2");
    m_keyCodes.insert("F3");
    m_keyCodes.insert("F4");
    m_keyCodes.insert("F5");
    m_keyCodes.insert("F6");
    m_keyCodes.insert("F7");
    m_keyCodes.insert("F8");
    m_keyCodes.insert("F9");
    m_keyCodes.insert("F10");
    m_keyCodes.insert("F11");
    m_keyCodes.insert("F12");

    // There can be the following options:
    // Ctrl-Alt-Shift
    // Ctrl-Alt
    // Ctrl-Shift
    // Ctrl
    // Alt-Shift
    // Alt
    // Shift
    for(const wxString& keyCode : m_keyCodes) {
        m_allShortcuts.insert("Ctrl-Alt-Shift-" + keyCode);
        m_allShortcuts.insert("Ctrl-Alt-" + keyCode);
        m_allShortcuts.insert("Ctrl-Shift-" + keyCode);
        m_allShortcuts.insert("Ctrl-" + keyCode);
        m_allShortcuts.insert("Alt-Shift-" + keyCode);
        m_allShortcuts.insert("Alt-" + keyCode);
        m_allShortcuts.insert("Shift-" + keyCode);
    }
}

clKeyboardManager::~clKeyboardManager()
{
    Save();
    EventNotifier::Get()->Unbind(wxEVT_INIT_DONE, &clKeyboardManager::OnStartupCompleted, this);
}

static clKeyboardManager* m_mgr = NULL;
clKeyboardManager* clKeyboardManager::Get()
{
    if(NULL == m_mgr) {
        m_mgr = new clKeyboardManager();
    }
    return m_mgr;
}

void clKeyboardManager::Release()
{
    if(m_mgr) {
        delete m_mgr;
    }
    m_mgr = NULL;
}

void clKeyboardManager::DoGetFrames(wxFrame* parent, clKeyboardManager::FrameList_t& frames)
{
    frames.push_back(parent);
    const wxWindowList& children = parent->GetChildren();
    wxWindowList::const_iterator iter = children.begin();
    for(; iter != children.end(); ++iter) {
        wxFrame* frameChild = dynamic_cast<wxFrame*>(*iter);
        if(frameChild) {
            if(std::find(frames.begin(), frames.end(), frameChild) == frames.end()) {
                frames.push_back(frameChild);
                DoGetFrames(frameChild, frames);
            }
        }
    }
}

void clKeyboardManager::DoUpdateMenu(wxMenu* menu, MenuItemDataIntMap_t& accels, std::vector<wxAcceleratorEntry>& table)
{
    wxMenuItemList items = menu->GetMenuItems();
    wxMenuItemList::iterator iter = items.begin();
    for(; iter != items.end(); iter++) {
        wxMenuItem* item = *iter;
        if(item->GetSubMenu()) {
            DoUpdateMenu(item->GetSubMenu(), accels, table);
            continue;
        }

        MenuItemDataIntMap_t::iterator where = accels.find(item->GetId());
        if(where != accels.end()) {
            wxString itemText = item->GetItemLabel();
            // remove the old shortcut
            itemText = itemText.BeforeFirst('\t');
            itemText << "\t" << where->second.accel.ToString();

            // Replace the item text (mnemonics + accel + label)
            item->SetItemLabel(itemText);

            // remove the matches entry from the accels map
            accels.erase(where);
        }

        wxAcceleratorEntry* a = wxAcceleratorEntry::Create(item->GetItemLabel());
        if(a) {
            a->Set(a->GetFlags(), a->GetKeyCode(), item->GetId());
            table.push_back(*a);
            wxDELETE(a);
        }
    }
}

void clKeyboardManager::DoUpdateFrame(wxFrame* frame, MenuItemDataIntMap_t& accels)
{
    std::vector<wxAcceleratorEntry> table;

    // Update menus. If a match is found remove it from the 'accel' table
    auto menuBar = clGetManager()->GetMenuBar();
    if(!menuBar) {
        clDEBUG() << "No menu bar found!" << clEndl;
        return;
    }

    for(size_t i = 0; i < menuBar->GetMenuCount(); ++i) {
        wxMenu* menu = menuBar->GetMenu(i);
        LOG_IF_TRACE { clDEBUG1() << "clKeyboardManager: updating menu" << menuBar->GetMenuLabel(i) << clEndl; }
        DoUpdateMenu(menu, accels, table);
    }

    if(!table.empty() || !accels.empty()) {
        wxAcceleratorEntry* entries = new wxAcceleratorEntry[table.size() + accels.size()];
        // append the globals
        clDEBUG() << "clKeyboardManager: appending global entries" << clEndl;
        for(MenuItemDataIntMap_t::iterator iter = accels.begin(); iter != accels.end(); ++iter) {
            wxString dummyText;
            dummyText << iter->second.action << "\t" << iter->second.accel.ToString();
            wxAcceleratorEntry* entry = wxAcceleratorEntry::Create(dummyText);
            if(entry) {
                entry->Set(entry->GetFlags(), entry->GetKeyCode(), wxXmlResource::GetXRCID(iter->second.resourceID));
                table.push_back(*entry);
                wxDELETE(entry);
            }
        }

        for(size_t i = 0; i < table.size(); ++i) {
            entries[i] = table.at(i);
        }

        wxAcceleratorTable acceleTable(table.size(), entries);
        frame->SetAcceleratorTable(acceleTable);
        wxDELETEA(entries);
    }
}

void clKeyboardManager::Save()
{
    clKeyboardBindingConfig config;
    config.SetBindings(m_accelTable).Save();
}

void clKeyboardManager::Initialize()
{
    m_accelTable.clear();
    clDEBUG() << "Keyboard manager: Initializing keyboard manager" << endl;
    // Load old format
    clKeyboardBindingConfig config;
    if(config.Exists()) {
        config.Load();
        m_accelTable = config.GetBindings();
    }

    // Load the default settings and add any new entries
    for(const MenuItemDataMap_t::value_type& vt : m_defaultAccelTable) {
        MenuItemDataMap_t::iterator iter = m_accelTable.find(vt.first);
        if(iter == m_accelTable.end()) {
            // Add new entry
            MenuItemData mid = vt.second;
            if(Exists(mid.accel)) {
                // This shortcut is already in use, disable it
                mid.accel.Clear();
            }
            m_accelTable.emplace(mid.resourceID, mid);
        } else {
            // Update the description to reflect localization changes
            iter->second.parentMenu = vt.second.parentMenu;
            iter->second.action = vt.second.action;
        }
    }

    // Store the correct configuration
    config.SetBindings(m_accelTable).Save();

    // And apply the changes
    Update();

    m_initialized = true;
}

void clKeyboardManager::GetAllAccelerators(MenuItemDataMap_t& accels) const { accels = m_accelTable; }

void clKeyboardManager::SetAccelerators(const MenuItemDataMap_t& accels)
{
    m_accelTable = accels;

    Update();
    Save();
}

void clKeyboardManager::Update(wxFrame* frame)
{
    // Since we keep the accelerators with their original resource ID in the form of string
    // we need to convert the map into a different integer with integer as the resource ID
    MenuItemDataIntMap_t intAccels;
    DoConvertToIntMap(m_accelTable, intAccels);

    if(!frame) {
        // update all frames
        frame = dynamic_cast<wxFrame*>(wxTheApp->GetTopWindow());
        CHECK_PTR_RET(frame);
    }
    // update only the requested frame
    DoUpdateFrame(frame, intAccels);
}

int clKeyboardManager::PopupNewKeyboardShortcutDlg(wxWindow* parent, MenuItemData& menuItemData)
{
    NewKeyShortcutDlg dlg(parent, menuItemData);
    if(dlg.ShowModal() == wxID_OK) {
        menuItemData.accel = dlg.GetAccel();
        return wxID_OK;
    }
    return wxID_CANCEL;
}

bool clKeyboardManager::Exists(const clKeyboardShortcut& accel) const
{
    if(!accel.IsOk()) {
        return false;
    }

    MenuItemDataMap_t::const_iterator iter = m_accelTable.begin();
    for(; iter != m_accelTable.end(); ++iter) {
        if(iter->second.accel == accel) {
            return true;
        }
    }
    return false;
}

void clKeyboardManager::AddAccelerator(const wxString& resourceID, const wxString& parentMenu, const wxString& action,
                                       const clKeyboardShortcut& accel)
{
    wxASSERT_MSG(m_defaultAccelTable.count(resourceID) == 0, "An accelerator with this resourceID already exists");

    MenuItemData mid;
    mid.resourceID = resourceID;
    mid.parentMenu = parentMenu;
    mid.action = action;
    mid.accel = accel;

    // Is the keyboard manager already initialized?
    // (this may indicate that the accelerator was dynamically generated from e.g. the SnipWiz plugin)
    if(m_initialized) {
        // We assume this accelerator as a 'non-persistent' accelerator,
        // which will be gone when the user restores default accelerators
        if(Exists(mid.accel)) {
            mid.accel.Clear();
        }
        m_accelTable[mid.resourceID] = mid;
    } else {
        // Otherwise, make it a persistent accelerator
        m_defaultAccelTable.emplace(mid.resourceID, mid);
    }
}

void clKeyboardManager::AddAccelerator(const wxString& parentMenu, const std::vector<AddAccelData>& table)
{
    for(const AddAccelData& data : table) {
        AddAccelerator(data.m_resourceID, parentMenu, data.m_action, data.m_accel);
    }
}

void clKeyboardManager::RestoreDefaults()
{
    // Decide which file we want to load, take the user settings file first
    wxFileName fnOldSettings(clStandardPaths::Get().GetUserDataDir(), "accelerators.conf");
    fnOldSettings.AppendDir("config");

    wxFileName fnNewSettings(clStandardPaths::Get().GetUserDataDir(), "keybindings.conf");
    fnNewSettings.AppendDir("config");

    wxLogNull nl;
    if(fnOldSettings.Exists()) {
        clRemoveFile(fnOldSettings.GetFullPath());
    }

    if(fnNewSettings.Exists()) {
        clRemoveFile(fnNewSettings.GetFullPath());
    }

    m_initialized = false;

    // Call initialize again
    Initialize();
}

void clKeyboardManager::OnStartupCompleted(wxCommandEvent& event)
{
    event.Skip();
    this->Initialize();
}

void clKeyboardManager::DoConvertToIntMap(const MenuItemDataMap_t& strMap, MenuItemDataIntMap_t& intMap)
{
    // Convert the string map into int based map
    MenuItemDataMap_t::const_iterator iter = strMap.begin();
    for(; iter != strMap.end(); ++iter) {
        intMap.insert(std::make_pair(wxXmlResource::GetXRCID(iter->second.resourceID), iter->second));
    }
}

clKeyboardShortcut::Vec_t clKeyboardManager::GetAllUnassignedKeyboardShortcuts() const
{
    clKeyboardShortcut::Set_t usedShortcuts;
    for(std::pair<wxString, MenuItemData> p : m_accelTable) {
        if(p.second.accel.IsOk()) {
            usedShortcuts.insert(p.second.accel);
        }
    }

    // Remove all duplicate entries
    clKeyboardShortcut::Vec_t allUnassigned;
    std::set_difference(m_allShortcuts.begin(), m_allShortcuts.end(), usedShortcuts.begin(), usedShortcuts.end(),
                        std::back_inserter(allUnassigned));
    return allUnassigned;
}

MenuItemDataMap_t clKeyboardManager::DoLoadAccelerators(const wxFileName& filename) const
{
    MenuItemDataMap_t entries;
    if(filename.Exists()) {
        wxString content;
        if(!FileUtils::ReadFileContent(filename, content)) {
            return entries;
        }
        wxArrayString lines = ::wxStringTokenize(content, "\r\n", wxTOKEN_STRTOK);
        for(size_t i = 0; i < lines.GetCount(); ++i) {
            wxArrayString parts = ::wxStringTokenize(lines.Item(i), "|", wxTOKEN_RET_EMPTY);
            if(parts.GetCount() < 3) {
                continue;
            }
            MenuItemData binding;
            binding.resourceID = parts.Item(0);
            binding.parentMenu = parts.Item(1);
            binding.action = parts.Item(2);
            if(parts.GetCount() == 4) {
                binding.accel.FromString(parts.Item(3));
            }
            entries.insert(std::make_pair(binding.resourceID, binding));
        }
    }
    return entries;
}

bool clKeyboardShortcut::operator==(const clKeyboardShortcut& rhs) const
{
    return this->GetControl() == rhs.GetControl() && this->GetAlt() == rhs.GetAlt() &&
           this->GetShift() == rhs.GetShift() && this->GetKeyCode() == rhs.GetKeyCode();
}

bool clKeyboardShortcut::operator<(const clKeyboardShortcut& rhs) const
{
    if(this->GetShift() != rhs.GetShift()) {
        return this->GetShift() < rhs.GetShift();
    }
    if(this->GetAlt() != rhs.GetAlt()) {
        return this->GetAlt() < rhs.GetAlt();
    }
    if(this->GetControl() != rhs.GetControl()) {
        return this->GetControl() < rhs.GetControl();
    }
    return this->GetKeyCode() < rhs.GetKeyCode();
}

bool clKeyboardShortcut::IsOk() const { return !m_keyCode.IsEmpty(); }

void clKeyboardShortcut::Clear()
{
    m_control_type = WXK_NONE;
    m_alt = false;
    m_shift = false;
    m_keyCode.Clear();
}

wxArrayString clKeyboardShortcut::Tokenize(const wxString& accelString) const
{
    /**
     * For example, "Ctrl-Alt-1" will be tokenized into:
     *   [0] Ctrl
     *   [1] -
     *   [2] Alt
     *   [3] -
     *   [4] 1
     *
     * Note that we also need the delimiter itself (in this case, '-')
     * to properly interpret accelerators like "Ctrl--" or just "-",
     * so we can't use wxStringTokenizer here
     */
    wxString token;
    wxArrayString tokens;
    for(const wxUniChar& ch : accelString) {
        if(ch == '-' || ch == '+') {
            if(!token.IsEmpty()) {
                tokens.Add(token);
                token.Clear();
            }
            tokens.Add(ch);
        } else {
            token << ch;
        }
    }
    if(!token.IsEmpty()) {
        tokens.Add(token);
    }
    return tokens;
}

void clKeyboardShortcut::FromString(const wxString& accelString)
{
    Clear();
    if(accelString.IsEmpty()) {
        return;
    }

    wxArrayString tokens = Tokenize(accelString);
    for(size_t i = 0; i < tokens.GetCount(); ++i) {
        wxString token = tokens.Item(i);
        if(token.IsSameAs("rawctrl", false)) {
            // WXK_RAW_CONTROL == WXK_CONTROL on non macOS
            m_control_type = WXK_RAW_CONTROL;
            ++i;

        } else if(token.IsSameAs("ctrl", false)) {
            // CMD or Control
            m_control_type = WXK_CONTROL;
            ++i;

        } else if(token.IsSameAs("alt", false)) {
            m_alt = true;
            ++i;

        } else if(token.IsSameAs("shift", false)) {
            m_shift = true;
            ++i;

        } else {
            m_keyCode = token.MakeUpper();
        }
    }
}

wxString clKeyboardShortcut::to_string(bool for_ui) const
{
    wxUnusedVar(for_ui);

    // An accelerator must contain a key code
    if(!IsOk()) {
        return "";
    }

    wxString str;
    if(m_control_type == WXK_CONTROL) {
#ifdef __WXMAC__
        if(for_ui) {
            str << "Cmd-";

        } else {
            str << "Ctrl-";
        }
#else
        str << "Ctrl-";
#endif

    } else if(m_control_type == WXK_RAW_CONTROL) {
#ifdef __WXMAC__
        if(for_ui) {
            str << "Ctrl-";
        } else {
            str << "RawCtrl-";
        }
#else
        str << "Ctrl-";
#endif
    }

    if(m_alt) {
        str << "Alt-";
    }
    if(m_shift) {
        str << "Shift-";
    }
    str << m_keyCode;
    return str;
}

wxString clKeyboardShortcut::ToString() const { return to_string(false); }
wxString clKeyboardShortcut::DisplayString() const { return to_string(true); }