From 3e99a0752783eeb0de19eb07be47f8e1128d70f7 Mon Sep 17 00:00:00 2001 From: fluxgen Date: Wed, 27 Aug 2003 00:20:19 +0000 Subject: using FbTk TextBox --- util/fbrun/FbRun.cc | 227 ++++++++-------------------------------------------- util/fbrun/FbRun.hh | 25 ++---- 2 files changed, 38 insertions(+), 214 deletions(-) diff --git a/util/fbrun/FbRun.cc b/util/fbrun/FbRun.cc index caf51ce..701f985 100644 --- a/util/fbrun/FbRun.cc +++ b/util/fbrun/FbRun.cc @@ -19,7 +19,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -// $Id: FbRun.cc,v 1.17 2003/08/25 01:18:00 fluxgen Exp $ +// $Id: FbRun.cc,v 1.18 2003/08/27 00:20:19 fluxgen Exp $ #include "FbRun.hh" @@ -50,10 +50,8 @@ using namespace std; FbRun::FbRun(int x, int y, size_t width): - FbTk::FbWindow((int)DefaultScreen(FbTk::App::instance()->display()), - x, y, - width, 10, - KeyPressMask | ExposureMask), + FbTk::TextBox(DefaultScreen(FbTk::App::instance()->display()), + m_font, ""), m_font("fixed"), m_display(FbTk::App::instance()->display()), m_bevel(4), @@ -61,15 +59,13 @@ FbRun::FbRun(int x, int y, size_t width): m_end(false), m_current_history_item(0), m_cursor(XCreateFontCursor(FbTk::App::instance()->display(), XC_xterm)), - m_start_pos(0), - m_end_pos(0), - m_cursor_pos(0), m_pixmap(0) { + setGC(m_gc); setCursor(m_cursor); // setting nomaximize in local resize - resize(width, m_font.height()); - FbTk::EventManager::instance()->add(*this, *this); + resize(width, font().height() + m_bevel); + // setup class name XClassHint *class_hint = XAllocClassHint(); if (class_hint == 0) @@ -91,10 +87,12 @@ FbRun::FbRun(int x, int y, size_t width): XFreePixmap(m_display, mask); #endif // HAVE_XPM - XWMHints wmhints; - wmhints.flags = IconPixmapHint; - wmhints.icon_pixmap = m_pixmap; - XSetWMHints(m_display, window(), &wmhints); + if (m_pixmap) { + XWMHints wmhints; + wmhints.flags = IconPixmapHint; + wmhints.icon_pixmap = m_pixmap; + XSetWMHints(m_display, window(), &wmhints); + } } @@ -118,18 +116,18 @@ void FbRun::run(const std::string &command) { hide(); // hide gui // save command history to file - if (m_runtext.size() != 0) { // no need to save empty command + if (text().size() != 0) { // no need to save empty command // don't allow duplicates into the history file, first // look for a duplicate if (m_current_history_item < m_history.size() - && m_runtext == m_history[m_current_history_item]) { + && text() == m_history[m_current_history_item]) { // m_current_history_item is the duplicate } else { m_current_history_item = 0; for (; m_current_history_item < m_history.size(); ++m_current_history_item) { - if (m_history[m_current_history_item] == m_runtext) + if (m_history[m_current_history_item] == text()) break; } } @@ -147,7 +145,7 @@ void FbRun::run(const std::string &command) { inoutfile<end(); // end program break; case XK_Return: - run(m_runtext); - m_runtext = ""; // clear text - break; - case XK_BackSpace: - backspace(); - break; - case XK_Home: - cursorHome(); - break; - case XK_End: - cursorEnd(); + run(text()); break; case XK_Up: prevHistoryItem(); @@ -304,24 +247,12 @@ void FbRun::keyPressEvent(XKeyEvent &ke) { case XK_Down: nextHistoryItem(); break; - case XK_Left: - cursorLeft(); - break; - case XK_Right: - cursorRight(); - break; case XK_Tab: tabCompleteHistory(); break; - default: - if (isprint(keychar[0])) insertCharacter(ks, keychar); } } - redrawLabel(); -} - -void FbRun::exposeEvent(XExposeEvent &ev) { - redrawLabel(); + clear(); } void FbRun::setNoMaximize() { @@ -340,9 +271,7 @@ void FbRun::prevHistoryItem() { XBell(m_display, 0); } else { m_current_history_item--; - m_runtext = m_history[m_current_history_item]; - m_cursor_pos = m_end_pos = m_runtext.size(); - adjustStartPos(); + setText(m_history[m_current_history_item]); } } @@ -353,13 +282,9 @@ void FbRun::nextHistoryItem() { m_current_history_item++; if (m_current_history_item == m_history.size()) { m_current_history_item = m_history.size(); - m_runtext = ""; - m_start_pos = m_cursor_pos = m_end_pos = 0; - } else { - m_runtext = m_history[m_current_history_item]; - m_cursor_pos = m_end_pos = m_runtext.size(); - adjustStartPos(); - } + setText(""); + } else + setText(m_history[m_current_history_item]); } } @@ -368,9 +293,7 @@ void FbRun::firstHistoryItem() { XBell(m_display, 0); } else { m_current_history_item = 0; - m_runtext = m_history[m_current_history_item]; - m_cursor_pos = m_end_pos = m_runtext.size(); - adjustStartPos(); + setText(m_history[m_current_history_item]); } } @@ -380,8 +303,7 @@ void FbRun::lastHistoryItem() { XBell(m_display, 0); } else { m_current_history_item = m_history.size(); - m_runtext = ""; - m_start_pos = m_cursor_pos = m_end_pos = 0; + setText(""); } } @@ -390,12 +312,11 @@ void FbRun::tabCompleteHistory() { XBell(m_display, 0); } else { int history_item = m_current_history_item - 1; - string prefix = m_runtext.substr(0, m_cursor_pos); + string prefix = text().substr(0, cursorPosition()); while (history_item > - 1) { if (m_history[history_item].find(prefix) == 0) { m_current_history_item = history_item; - m_runtext = m_history[m_current_history_item]; - adjustEndPos(); + setText(m_history[m_current_history_item]); break; } history_item--; @@ -404,92 +325,8 @@ void FbRun::tabCompleteHistory() { } } -void FbRun::cursorLeft() { - if (m_cursor_pos) - m_cursor_pos--; - else if (m_start_pos) { - m_start_pos--; - adjustEndPos(); - } -} - -void FbRun::cursorRight() { - if (m_start_pos + m_cursor_pos < m_end_pos) - m_cursor_pos++; - else if (m_end_pos < m_runtext.size()) { - m_cursor_pos++; - m_end_pos++; - adjustStartPos(); - } -} - -void FbRun::cursorHome() { - m_start_pos = m_cursor_pos = 0; - adjustEndPos(); +void FbRun::insertCharacter(char keychar) { + char val[2] = {keychar, 0}; + insertText(val); } -void FbRun::cursorEnd() { - m_cursor_pos = m_end_pos = m_runtext.size(); - adjustStartPos(); -} - -void FbRun::backspace() { - if (m_start_pos || m_cursor_pos) { - m_runtext.erase(m_start_pos + m_cursor_pos - 1, 1); - if (m_cursor_pos) - m_cursor_pos--; - else - m_start_pos--; - adjustEndPos(); - } -} - -void FbRun::deleteForward() { - if (m_start_pos + m_cursor_pos < m_end_pos) { - m_runtext.erase(m_start_pos + m_cursor_pos, 1); - adjustEndPos(); - } -} - -void FbRun::killToEnd() { - if (m_start_pos + m_cursor_pos < m_end_pos) { - m_runtext.erase(m_start_pos + m_cursor_pos); - adjustEndPos(); - } -} - -void FbRun::insertCharacter(KeySym ks, char *keychar) { - char in_char[2] = {keychar[0], 0}; - m_runtext.insert(m_start_pos + m_cursor_pos, in_char); - m_cursor_pos++; - m_end_pos++; - if (m_start_pos + m_cursor_pos < m_end_pos) - adjustEndPos(); - else - adjustStartPos(); -} - -void FbRun::adjustEndPos() { - m_end_pos = m_runtext.size(); - const char *text = m_runtext.c_str(); - int text_width = m_font.textWidth(text + m_start_pos, m_end_pos - m_start_pos); - while (text_width > width()) { - m_end_pos--; - text_width = m_font.textWidth(text + m_start_pos, m_end_pos - m_start_pos); - } -} - -void FbRun::adjustStartPos() { - const char *text = m_runtext.c_str(); - int text_width = m_font.textWidth(text + m_start_pos, m_end_pos - m_start_pos); - if (text_width < width()) return; - int start_pos = 0; - text_width = m_font.textWidth(text + start_pos, m_end_pos - start_pos); - while (text_width > width()) { - start_pos++; - text_width = m_font.textWidth(text + start_pos, m_end_pos - start_pos); - } - // adjust m_cursor_pos according relative to change to m_start_pos - m_cursor_pos -= start_pos - m_start_pos; - m_start_pos = start_pos; -} diff --git a/util/fbrun/FbRun.hh b/util/fbrun/FbRun.hh index dfc74cc..f6a544c 100644 --- a/util/fbrun/FbRun.hh +++ b/util/fbrun/FbRun.hh @@ -19,7 +19,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -// $Id: FbRun.hh,v 1.12 2003/08/25 01:16:41 fluxgen Exp $ +// $Id: FbRun.hh,v 1.13 2003/08/27 00:19:57 fluxgen Exp $ #ifndef FBRUN_HH #define FBRUN_HH @@ -27,6 +27,7 @@ #include "EventHandler.hh" #include "Font.hh" #include "FbWindow.hh" +#include "TextBox.hh" #include #include @@ -34,19 +35,17 @@ /** Creates and managed a run window */ -class FbRun: public FbTk::EventHandler, public FbTk::FbWindow { +class FbRun: public FbTk::TextBox { public: FbRun(int x = 0, int y = 0, size_t width = 200); ~FbRun(); void handleEvent(XEvent * const ev); - void setText(const std::string &text); void setTitle(const std::string &title); - void resize(size_t width, size_t height); + void resize(unsigned int width, unsigned int height); /// load and reconfigure for new font bool loadFont(const std::string &fontname); void setForegroundColor(const FbTk::Color &color); - void setBackgroundColor(const FbTk::Color &color); void setAntialias(bool val) { m_font.setAntialias(val); } const FbTk::Font &font() const { return m_font; } /// execute command and exit @@ -62,15 +61,12 @@ public: @name events */ ///@{ - void exposeEvent(XExposeEvent &ev); void keyPressEvent(XKeyEvent &ev); ///@} private: void nextHistoryItem(); void prevHistoryItem(); - void cursorLeft(); - void cursorRight(); void drawString(int x, int y, const char *text, size_t len); void getSize(size_t &width, size_t &height); void createWindow(int x, int y, size_t width, size_t height); @@ -78,12 +74,7 @@ private: /// set no maximizable for this window void setNoMaximize(); - void cursorHome(); - void cursorEnd(); - void backspace(); - void deleteForward(); - void killToEnd(); - void insertCharacter(KeySym ks, char *keychar); + void insertCharacter(char key); void adjustStartPos(); void adjustEndPos(); void firstHistoryItem(); @@ -92,8 +83,7 @@ private: FbTk::Font m_font; ///< font used to draw command text Display *m_display; ///< display connection - std::string m_runtext; ///< command to execute - int m_bevel; ///< distance to window edge from font in pixels + int m_bevel; GC m_gc; ///< graphic context bool m_end; ///< marks when this object is done std::vector m_history; ///< history list of commands @@ -101,9 +91,6 @@ private: std::string m_history_file; ///< holds filename for command history file Cursor m_cursor; - int m_start_pos; ///< start position of portion of text to display - int m_cursor_pos; ///< relative to m_start_pos - int m_end_pos; ///< end postition of portion of text to display Pixmap m_pixmap; }; -- cgit v0.11.2