Repository navigation
Expand file tree
/
Copy pathHexInput.cpp
More file actions
179 lines (155 loc) · 6.13 KB
/
Copy pathHexInput.cpp
File metadata and controls
179 lines (155 loc) · 6.13 KB
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
#include "HexInput.h"
#include <FL/Fl.H>
#include <ctype.h> // Pro MSVC 6.0
#include <string>
#include <string.h>
HexInput::HexInput(int X, int Y, int W, int H, const char *L, int maxLen)
: Fl_Input(X, Y, W, H, L), maxLength(maxLen) {
}
int HexInput::handle(int event) {
switch (event) {
case FL_KEYDOWN: {
int key = Fl::event_key();
int cursorPos = this->position(); // Aktuální pozice kurzoru
std::string currentText = this->value() ? this->value() : "";
// **Povolit Ctrl+V, aby mohl být vyvolán FL_PASTE**
if (Fl::event_state(FL_CTRL) && key == 'v') {
return Fl_Input::handle(event);
}
// **Povolit Ctrl+C pro kopírování celého obsahu**
if (Fl::event_state(FL_CTRL) && key == 'c') {
Fl::copy(this->value(), strlen(this->value()), 1); // 1 = výchozí clipboard
return 1;
}
/*
* Text entered into an input must be taken from event_text(), not
* event_key(). event_key() identifies the physical/logical key and
* its printable value is backend/layout dependent (particularly on
* X11/Wayland). FLTK explicitly recommends event_text() for text
* widgets.
*
* Numeric keypad digits are normalized explicitly as well so that
* they work consistently on all supported FLTK versions/backends.
*/
char typed = 0;
if (key >= (FL_KP + '0') && key <= (FL_KP + '9')) {
typed = (char)(key - FL_KP);
} else {
const char* text = Fl::event_text();
int len = Fl::event_length();
if (text != NULL && len == 1) {
char ch = text[0];
if ((ch >= '0' && ch <= '9') ||
(ch >= 'a' && ch <= 'f') ||
(ch >= 'A' && ch <= 'F')) {
typed = ch;
}
}
}
if (typed != 0) {
if (cursorPos >= maxLength) {
return 1; // Zabrani prekroceni delky
}
if (typed >= 'a' && typed <= 'f') {
typed = (char)toupper((unsigned char)typed);
}
// Prepisovani znaku na aktualni pozici kurzoru
if (cursorPos < (int)currentText.length()) {
currentText[cursorPos] = typed;
} else {
currentText += typed;
}
this->value(currentText.c_str());
this->position(cursorPos + 1);
return 1;
}
// **Backspace: nahrazuje znak PRED kurzorem nulou a posune kurzor DOLEVA**
if (key == FL_BackSpace && cursorPos > 0) {
currentText[cursorPos - 1] = '0';
this->value(currentText.c_str());
this->position(cursorPos - 1); // Posun DOLEVA
return 1;
}
// **Delete: nahrazuje znak NA pozici kurzoru nulou a posune kurzor DOPRAVA**
if (key == FL_Delete && cursorPos < currentText.length()) {
currentText[cursorPos] = '0';
this->value(currentText.c_str());
if (cursorPos < currentText.length() - 1) {
this->position(cursorPos + 1); // Posun DOPRAVA
}
return 1;
}
// **Šipky vlevo/vpravo zustávají uvnitr inputboxu**
if (key == FL_Left) {
if (cursorPos > 0) {
this->position(cursorPos - 1);
}
return 1; // Zamezí presunu fokusu ven
}
if (key == FL_Right) {
if (cursorPos < currentText.length()) {
this->position(cursorPos + 1);
}
return 1; // Zamezí presunu fokusu ven
}
// **Klávesa Home - skok na zacátek textu**
if (key == FL_Home) {
this->position(0);
return 1;
}
// **Klávesa End - skok na konec textu**
if (key == FL_End) {
this->position(currentText.length());
return 1;
}
// **Povolit Tab a Shift+Tab**
if (key == FL_Tab) {
return Fl_Input::handle(event);
}
if (Fl::event_state(FL_SHIFT) && key == FL_Tab) {
return Fl_Input::handle(event);
}
return 1; // **Vše ostatní zahodíme**
}
case FL_PASTE: {
/* Match JNumberTextField: optional '#', hex only, fixed width. */
const char* clipboard = Fl::event_text();
std::string pasted;
unsigned int i;
if (!clipboard) return 1;
pasted = clipboard;
if (!pasted.empty() && pasted[0] == '#') {
pasted.erase(0, 1);
}
if (pasted.empty() || pasted.length() > (unsigned int)maxLength) {
return 1;
}
for (i = 0; i < pasted.length(); ++i) {
char ch = pasted[i];
if (!((ch >= '0' && ch <= '9') ||
(ch >= 'A' && ch <= 'F') ||
(ch >= 'a' && ch <= 'f'))) {
return 1;
}
if (ch >= 'a' && ch <= 'f') {
pasted[i] = (char)toupper(ch);
}
}
while (pasted.length() < (unsigned int)maxLength) {
std::string padded("0");
padded.append(pasted);
pasted = padded;
}
this->value(pasted.c_str());
this->position((int)pasted.length());
return 1;
}
case FL_UNFOCUS: {
int result = Fl_Input::handle(event);
/* Breakpoint inputs commit on focus loss. */
do_callback();
return result;
}
}
return Fl_Input::handle(event); // Predání ostatních eventu dál
}