diff options
author | Pete Beardmore <pete.beardmore@msn.com> | 2018-03-04 22:56:44 (GMT) |
---|---|---|
committer | Mathias Gumz <akira@fluxbox.org> | 2018-03-14 06:49:50 (GMT) |
commit | ed7ea14167e531dc3d7cc3db931f299fcc8c7ba4 (patch) | |
tree | 40cfa615803858c0f17ed35f1f6da435ab143651 | |
parent | ca0995bdec7c96452291639c9736ed7212f96ca2 (diff) | |
download | fluxbox-ed7ea14167e531dc3d7cc3db931f299fcc8c7ba4.zip fluxbox-ed7ea14167e531dc3d7cc3db931f299fcc8c7ba4.tar.bz2 |
fbtk, textbox, support horizontal padding
-add 'setPadding' function
-rw-r--r-- | src/FbTk/TextBox.cc | 47 | ||||
-rw-r--r-- | src/FbTk/TextBox.hh | 2 |
2 files changed, 36 insertions, 13 deletions
diff --git a/src/FbTk/TextBox.cc b/src/FbTk/TextBox.cc index a83347d..ee3396d 100644 --- a/src/FbTk/TextBox.cc +++ b/src/FbTk/TextBox.cc | |||
@@ -58,7 +58,8 @@ TextBox::TextBox(int screen_num, | |||
58 | m_start_pos(0), | 58 | m_start_pos(0), |
59 | m_end_pos(0), | 59 | m_end_pos(0), |
60 | m_select_pos(std::string::npos), | 60 | m_select_pos(std::string::npos), |
61 | m_xic(0) { | 61 | m_padding(0), |
62 | m_xic(0){ | ||
62 | 63 | ||
63 | if (App::instance()->inputModule()) | 64 | if (App::instance()->inputModule()) |
64 | m_xic = XCreateIC(App::instance()->inputModule(), XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, window(), NULL); | 65 | m_xic = XCreateIC(App::instance()->inputModule(), XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, window(), NULL); |
@@ -75,7 +76,8 @@ TextBox::TextBox(const FbWindow &parent, | |||
75 | m_start_pos(0), | 76 | m_start_pos(0), |
76 | m_end_pos(0), | 77 | m_end_pos(0), |
77 | m_select_pos(std::string::npos), | 78 | m_select_pos(std::string::npos), |
78 | m_xic(0) { | 79 | m_padding(0), |
80 | m_xic(0){ | ||
79 | if (App::instance()->inputModule()) | 81 | if (App::instance()->inputModule()) |
80 | m_xic = XCreateIC(App::instance()->inputModule(), XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, window(), NULL); | 82 | m_xic = XCreateIC(App::instance()->inputModule(), XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, window(), NULL); |
81 | FbTk::EventManager::instance()->add(*this, *this); | 83 | FbTk::EventManager::instance()->add(*this, *this); |
@@ -98,6 +100,12 @@ void TextBox::setFont(const Font &font) { | |||
98 | m_font = &font; | 100 | m_font = &font; |
99 | } | 101 | } |
100 | 102 | ||
103 | void TextBox::setPadding(int padding) { | ||
104 | m_padding = padding; | ||
105 | adjustPos(); | ||
106 | clear(); | ||
107 | } | ||
108 | |||
101 | void TextBox::setGC(GC gc) { | 109 | void TextBox::setGC(GC gc) { |
102 | m_gc = gc; | 110 | m_gc = gc; |
103 | } | 111 | } |
@@ -240,12 +248,24 @@ void TextBox::clear() { | |||
240 | if (gc() == 0) | 248 | if (gc() == 0) |
241 | setGC(DefaultGC(dpy, screenNumber())); | 249 | setGC(DefaultGC(dpy, screenNumber())); |
242 | 250 | ||
243 | |||
244 | int cursor_pos = font().textWidth(m_text.visual().c_str() + m_start_pos, m_cursor_pos); | 251 | int cursor_pos = font().textWidth(m_text.visual().c_str() + m_start_pos, m_cursor_pos); |
245 | 252 | ||
253 | int char_length = m_end_pos - m_start_pos; | ||
254 | int text_width = font().textWidth(m_text.visual().c_str() + m_start_pos, char_length); | ||
255 | int char_draw_length = char_length; | ||
256 | if (text_width > static_cast<signed>(width()) - 2*m_padding) { | ||
257 | // draw less text | ||
258 | int char_start_current = m_start_pos; | ||
259 | int char_end_current = m_end_pos; | ||
260 | adjustPos(); | ||
261 | char_draw_length = m_end_pos - m_start_pos; | ||
262 | m_start_pos = char_start_current; | ||
263 | m_end_pos = char_end_current; | ||
264 | } | ||
265 | |||
246 | font().drawText(*this, screenNumber(), gc(), | 266 | font().drawText(*this, screenNumber(), gc(), |
247 | m_text.visual().c_str() + m_start_pos, | 267 | m_text.visual().c_str() + m_start_pos, |
248 | m_end_pos - m_start_pos, -1, center_pos); // pos | 268 | char_draw_length, m_padding, center_pos); // pos |
249 | 269 | ||
250 | if (hasSelection()) { | 270 | if (hasSelection()) { |
251 | int select_pos = m_select_pos <= m_start_pos ? 0 : | 271 | int select_pos = m_select_pos <= m_start_pos ? 0 : |
@@ -253,14 +273,15 @@ void TextBox::clear() { | |||
253 | m_select_pos - m_start_pos); | 273 | m_select_pos - m_start_pos); |
254 | int start = std::max(m_start_pos, std::min(m_start_pos + m_cursor_pos, m_select_pos)); | 274 | int start = std::max(m_start_pos, std::min(m_start_pos + m_cursor_pos, m_select_pos)); |
255 | int length = std::max(m_start_pos + m_cursor_pos, m_select_pos) - start; | 275 | int length = std::max(m_start_pos + m_cursor_pos, m_select_pos) - start; |
256 | int x = std::min(select_pos, cursor_pos); | 276 | length = std::min(length, char_draw_length); |
257 | int width = std::abs(select_pos - cursor_pos); | 277 | int x = m_padding + std::min(select_pos, cursor_pos); |
278 | int select_width = std::min(std::abs(select_pos - cursor_pos), text_width); | ||
258 | 279 | ||
259 | XGCValues backup; | 280 | XGCValues backup; |
260 | XGetGCValues(dpy, gc(), GCForeground|GCBackground, &backup); | 281 | XGetGCValues(dpy, gc(), GCForeground|GCBackground, &backup); |
261 | XSetForeground(dpy, gc(), backup.foreground); | 282 | XSetForeground(dpy, gc(), backup.foreground); |
262 | 283 | ||
263 | fillRectangle(gc(), x, (height()-font().height())/2, width, font().height()); | 284 | fillRectangle(gc(), x, (height()-font().height())/2, select_width, font().height()); |
264 | 285 | ||
265 | XColor c; | 286 | XColor c; |
266 | c.pixel = backup.foreground; | 287 | c.pixel = backup.foreground; |
@@ -275,7 +296,7 @@ void TextBox::clear() { | |||
275 | 296 | ||
276 | 297 | ||
277 | // draw cursor position | 298 | // draw cursor position |
278 | drawLine(gc(), cursor_pos, height()/2 + font().ascent()/2, cursor_pos, height()/2 - font().ascent()/2); | 299 | drawLine(gc(), m_padding + cursor_pos, height()/2 + font().ascent()/2, m_padding + cursor_pos, height()/2 - font().ascent()/2); |
279 | } | 300 | } |
280 | 301 | ||
281 | void TextBox::moveResize(int x, int y, | 302 | void TextBox::moveResize(int x, int y, |
@@ -302,7 +323,7 @@ void TextBox::buttonPressEvent(XButtonEvent &event) { | |||
302 | int tmp = 0; | 323 | int tmp = 0; |
303 | for(i = m_start_pos; i <= m_end_pos; i++) { | 324 | for(i = m_start_pos; i <= m_end_pos; i++) { |
304 | tmp = abs(static_cast<int> | 325 | tmp = abs(static_cast<int> |
305 | (event.x - font().textWidth(m_text.visual().c_str() + m_start_pos, i - m_start_pos))); | 326 | (event.x - font().textWidth(m_text.visual().c_str() + m_start_pos, i - m_start_pos) - m_padding)); |
306 | 327 | ||
307 | if (tmp < delta) { | 328 | if (tmp < delta) { |
308 | delta = tmp; | 329 | delta = tmp; |
@@ -490,7 +511,7 @@ void TextBox::handleEvent(XEvent &event) { | |||
490 | 511 | ||
491 | void TextBox::setCursorPosition(int pos) { | 512 | void TextBox::setCursorPosition(int pos) { |
492 | m_cursor_pos = pos < 0 ? 0 : pos; | 513 | m_cursor_pos = pos < 0 ? 0 : pos; |
493 | if (m_cursor_pos > text().size()) | 514 | if (m_cursor_pos > text().size() - 2*m_padding) |
494 | cursorEnd(); | 515 | cursorEnd(); |
495 | } | 516 | } |
496 | 517 | ||
@@ -498,7 +519,7 @@ void TextBox::adjustEndPos() { | |||
498 | m_end_pos = text().size(); | 519 | m_end_pos = text().size(); |
499 | m_start_pos = std::min(m_start_pos, m_end_pos); | 520 | m_start_pos = std::min(m_start_pos, m_end_pos); |
500 | int text_width = font().textWidth(text().c_str() + m_start_pos, m_end_pos - m_start_pos); | 521 | int text_width = font().textWidth(text().c_str() + m_start_pos, m_end_pos - m_start_pos); |
501 | while (text_width > static_cast<signed>(width())) { | 522 | while (text_width > (static_cast<signed>(width()) - 2*m_padding)) { |
502 | m_end_pos--; | 523 | m_end_pos--; |
503 | text_width = font().textWidth(text().c_str() + m_start_pos, m_end_pos - m_start_pos); | 524 | text_width = font().textWidth(text().c_str() + m_start_pos, m_end_pos - m_start_pos); |
504 | } | 525 | } |
@@ -509,11 +530,11 @@ void TextBox::adjustStartPos() { | |||
509 | const char* visual = m_text.visual().c_str(); | 530 | const char* visual = m_text.visual().c_str(); |
510 | 531 | ||
511 | int text_width = font().textWidth(visual, m_end_pos); | 532 | int text_width = font().textWidth(visual, m_end_pos); |
512 | if (m_cursor_pos >= 0 && text_width < static_cast<signed>(width())) | 533 | if (m_cursor_pos >= 0 && text_width < (static_cast<signed>(width()) - 2*m_padding)) |
513 | return; | 534 | return; |
514 | 535 | ||
515 | int start_pos = 0; | 536 | int start_pos = 0; |
516 | while (text_width > static_cast<signed>(width())) { | 537 | while (text_width > (static_cast<signed>(width()) - 2 * m_padding)) { |
517 | start_pos++; | 538 | start_pos++; |
518 | text_width = font().textWidth(visual + start_pos, m_end_pos - start_pos); | 539 | text_width = font().textWidth(visual + start_pos, m_end_pos - start_pos); |
519 | } | 540 | } |
diff --git a/src/FbTk/TextBox.hh b/src/FbTk/TextBox.hh index 20f3610..10741a8 100644 --- a/src/FbTk/TextBox.hh +++ b/src/FbTk/TextBox.hh | |||
@@ -37,6 +37,7 @@ public: | |||
37 | virtual ~TextBox(); | 37 | virtual ~TextBox(); |
38 | 38 | ||
39 | void setText(const FbTk::BiDiString &text); | 39 | void setText(const FbTk::BiDiString &text); |
40 | void setPadding(int padding); | ||
40 | void setFont(const Font &font); | 41 | void setFont(const Font &font); |
41 | void setGC(GC gc); | 42 | void setGC(GC gc); |
42 | void setCursorPosition(int cursor); | 43 | void setCursorPosition(int cursor); |
@@ -87,6 +88,7 @@ private: | |||
87 | BiDiString m_text; | 88 | BiDiString m_text; |
88 | GC m_gc; | 89 | GC m_gc; |
89 | std::string::size_type m_cursor_pos, m_start_pos, m_end_pos, m_select_pos; | 90 | std::string::size_type m_cursor_pos, m_start_pos, m_end_pos, m_select_pos; |
91 | int m_padding; | ||
90 | XIC m_xic; | 92 | XIC m_xic; |
91 | }; | 93 | }; |
92 | 94 | ||