1 | // Aseprite UI Library |
2 | // Copyright (C) 2001-2016 David Capello |
3 | // |
4 | // This file is released under the terms of the MIT license. |
5 | // Read LICENSE.txt for more information. |
6 | |
7 | #ifndef UI_ACCELERATOR_H_INCLUDED |
8 | #define UI_ACCELERATOR_H_INCLUDED |
9 | #pragma once |
10 | |
11 | #include <string> |
12 | #include <vector> |
13 | |
14 | #include "ui/keys.h" |
15 | |
16 | namespace ui { |
17 | |
18 | extern const char* kWinKeyName; |
19 | |
20 | // TODO rename this class to Shortcut |
21 | class Accelerator { |
22 | public: |
23 | Accelerator(); |
24 | Accelerator(KeyModifiers modifiers, KeyScancode scancode, int unicodeChar); |
25 | // Convert string like "Ctrl+Q" or "Alt+X" into an accelerator. |
26 | explicit Accelerator(const std::string& str); |
27 | |
28 | bool isEmpty() const; |
29 | std::string toString() const; |
30 | |
31 | bool isPressed(KeyModifiers modifiers, KeyScancode scancode, int unicodeChar) const; |
32 | |
33 | // Returns true if the key is pressed and ONLY its modifiers are |
34 | // pressed. |
35 | bool isPressed() const; |
36 | |
37 | // Returns true if the key + its modifiers are pressed (other |
38 | // modifiers are allowed too). |
39 | bool isLooselyPressed() const; |
40 | |
41 | bool operator==(const Accelerator& other) const; |
42 | bool operator!=(const Accelerator& other) const { |
43 | return !operator==(other); |
44 | } |
45 | |
46 | KeyModifiers modifiers() const { return m_modifiers; } |
47 | KeyScancode scancode() const { return m_scancode; } |
48 | int unicodeChar() const { return m_unicodeChar; } |
49 | |
50 | private: |
51 | KeyModifiers m_modifiers; |
52 | KeyScancode m_scancode; |
53 | int m_unicodeChar; |
54 | }; |
55 | |
56 | // TODO rename this class to Shortcuts |
57 | class Accelerators { |
58 | public: |
59 | typedef std::vector<Accelerator> List; |
60 | typedef List::iterator iterator; |
61 | typedef List::const_iterator const_iterator; |
62 | |
63 | iterator begin() { return m_list.begin(); } |
64 | iterator end() { return m_list.end(); } |
65 | const_iterator begin() const { return m_list.begin(); } |
66 | const_iterator end() const { return m_list.end(); } |
67 | |
68 | bool empty() const { return m_list.empty(); } |
69 | std::size_t size() const { return m_list.size(); } |
70 | |
71 | const ui::Accelerator& front() const { return m_list.front(); } |
72 | |
73 | const ui::Accelerator& operator[](int index) const { |
74 | return m_list[index]; |
75 | } |
76 | |
77 | ui::Accelerator& operator[](int index) { |
78 | return m_list[index]; |
79 | } |
80 | |
81 | void clear() { m_list.clear(); } |
82 | bool has(const Accelerator& accel) const; |
83 | void add(const Accelerator& accel); |
84 | void remove(const Accelerator& accel); |
85 | |
86 | private: |
87 | List m_list; |
88 | }; |
89 | |
90 | } // namespace ui |
91 | |
92 | #endif |
93 | |