using FbTk TextBox

This commit is contained in:
fluxgen 2003-08-27 00:20:19 +00:00
parent 450c1d80f6
commit 3e99a07527
2 changed files with 38 additions and 214 deletions

View file

@ -19,7 +19,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // 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" #include "FbRun.hh"
@ -50,10 +50,8 @@
using namespace std; using namespace std;
FbRun::FbRun(int x, int y, size_t width): FbRun::FbRun(int x, int y, size_t width):
FbTk::FbWindow((int)DefaultScreen(FbTk::App::instance()->display()), FbTk::TextBox(DefaultScreen(FbTk::App::instance()->display()),
x, y, m_font, ""),
width, 10,
KeyPressMask | ExposureMask),
m_font("fixed"), m_font("fixed"),
m_display(FbTk::App::instance()->display()), m_display(FbTk::App::instance()->display()),
m_bevel(4), m_bevel(4),
@ -61,15 +59,13 @@ FbRun::FbRun(int x, int y, size_t width):
m_end(false), m_end(false),
m_current_history_item(0), m_current_history_item(0),
m_cursor(XCreateFontCursor(FbTk::App::instance()->display(), XC_xterm)), m_cursor(XCreateFontCursor(FbTk::App::instance()->display(), XC_xterm)),
m_start_pos(0),
m_end_pos(0),
m_cursor_pos(0),
m_pixmap(0) { m_pixmap(0) {
setGC(m_gc);
setCursor(m_cursor); setCursor(m_cursor);
// setting nomaximize in local resize // setting nomaximize in local resize
resize(width, m_font.height()); resize(width, font().height() + m_bevel);
FbTk::EventManager::instance()->add(*this, *this);
// setup class name // setup class name
XClassHint *class_hint = XAllocClassHint(); XClassHint *class_hint = XAllocClassHint();
if (class_hint == 0) if (class_hint == 0)
@ -91,10 +87,12 @@ FbRun::FbRun(int x, int y, size_t width):
XFreePixmap(m_display, mask); XFreePixmap(m_display, mask);
#endif // HAVE_XPM #endif // HAVE_XPM
XWMHints wmhints; if (m_pixmap) {
wmhints.flags = IconPixmapHint; XWMHints wmhints;
wmhints.icon_pixmap = m_pixmap; wmhints.flags = IconPixmapHint;
XSetWMHints(m_display, window(), &wmhints); wmhints.icon_pixmap = m_pixmap;
XSetWMHints(m_display, window(), &wmhints);
}
} }
@ -118,18 +116,18 @@ void FbRun::run(const std::string &command) {
hide(); // hide gui hide(); // hide gui
// save command history to file // 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 // don't allow duplicates into the history file, first
// look for a duplicate // look for a duplicate
if (m_current_history_item < m_history.size() 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 // m_current_history_item is the duplicate
} else { } else {
m_current_history_item = 0; m_current_history_item = 0;
for (; m_current_history_item < m_history.size(); for (; m_current_history_item < m_history.size();
++m_current_history_item) { ++m_current_history_item) {
if (m_history[m_current_history_item] == m_runtext) if (m_history[m_current_history_item] == text())
break; break;
} }
} }
@ -147,7 +145,7 @@ void FbRun::run(const std::string &command) {
inoutfile<<m_history[i]<<endl; inoutfile<<m_history[i]<<endl;
// and append the current one back to the end // and append the current one back to the end
inoutfile<<m_runtext<<endl; inoutfile<<text()<<endl;
} else } else
cerr<<"FbRun Warning: Can't write command history to file: "<<m_history_file<<endl; cerr<<"FbRun Warning: Can't write command history to file: "<<m_history_file<<endl;
} }
@ -184,23 +182,12 @@ bool FbRun::loadFont(const string &fontname) {
return false; return false;
// resize to fit new font height // resize to fit new font height
resize(width(), m_font.height() + m_bevel); resize(width(), font().height() + m_bevel);
return true; return true;
} }
void FbRun::setForegroundColor(const FbTk::Color &color) { void FbRun::setForegroundColor(const FbTk::Color &color) {
XSetForeground(m_display, m_gc, color.pixel()); XSetForeground(m_display, m_gc, color.pixel());
redrawLabel();
}
void FbRun::setBackgroundColor(const FbTk::Color &color) {
FbTk::FbWindow::setBackgroundColor(color);
redrawLabel();
}
void FbRun::setText(const string &text) {
m_runtext = text;
redrawLabel();
} }
void FbRun::setTitle(const string &title) { void FbRun::setTitle(const string &title) {
@ -208,30 +195,16 @@ void FbRun::setTitle(const string &title) {
} }
void FbRun::resize(size_t width, size_t height) { void FbRun::resize(size_t width, size_t height) {
FbTk::FbWindow::resize(width, height); FbTk::TextBox::resize(width, height);
setNoMaximize(); setNoMaximize();
} }
void FbRun::redrawLabel() { void FbRun::redrawLabel() {
clear(); clear();
drawString(m_bevel/2, m_font.ascent() + m_bevel/2,
m_runtext.c_str(), m_runtext.size());
}
void FbRun::drawString(int x, int y,
const char *text, size_t len) {
assert(m_gc);
m_font.drawText(window(), screenNumber(),
m_gc, text + m_start_pos,
m_end_pos - m_start_pos, x, y - 2);
// draw cursor position
int cursor_pos = m_font.textWidth(text + m_start_pos, m_cursor_pos) + 1;
drawLine(m_gc, cursor_pos, 0, cursor_pos, m_font.height());
} }
void FbRun::keyPressEvent(XKeyEvent &ke) { void FbRun::keyPressEvent(XKeyEvent &ke) {
FbTk::TextBox::keyPressEvent(ke);
KeySym ks; KeySym ks;
char keychar[1]; char keychar[1];
XLookupString(&ke, keychar, 1, &ks, 0); XLookupString(&ke, keychar, 1, &ks, 0);
@ -241,30 +214,12 @@ void FbRun::keyPressEvent(XKeyEvent &ke) {
if (ke.state) { // a modifier key is down if (ke.state) { // a modifier key is down
if (ke.state == ControlMask) { if (ke.state == ControlMask) {
switch (ks) { switch (ks) {
case XK_b:
cursorLeft();
break;
case XK_f:
cursorRight();
break;
case XK_p: case XK_p:
prevHistoryItem(); prevHistoryItem();
break; break;
case XK_n: case XK_n:
nextHistoryItem(); nextHistoryItem();
break; break;
case XK_a:
cursorHome();
break;
case XK_e:
cursorEnd();
break;
case XK_d:
deleteForward();
break;
case XK_k:
killToEnd();
break;
} }
} else if (ke.state == (Mod1Mask | ShiftMask)) { } else if (ke.state == (Mod1Mask | ShiftMask)) {
switch (ks) { switch (ks) {
@ -275,8 +230,6 @@ void FbRun::keyPressEvent(XKeyEvent &ke) {
lastHistoryItem(); lastHistoryItem();
break; break;
} }
} else if (ke.state == ShiftMask) {
if (isprint(keychar[0]))insertCharacter(ks, keychar);
} }
} else { // no modifier key } else { // no modifier key
switch (ks) { switch (ks) {
@ -286,17 +239,7 @@ void FbRun::keyPressEvent(XKeyEvent &ke) {
FbTk::App::instance()->end(); // end program FbTk::App::instance()->end(); // end program
break; break;
case XK_Return: case XK_Return:
run(m_runtext); run(text());
m_runtext = ""; // clear text
break;
case XK_BackSpace:
backspace();
break;
case XK_Home:
cursorHome();
break;
case XK_End:
cursorEnd();
break; break;
case XK_Up: case XK_Up:
prevHistoryItem(); prevHistoryItem();
@ -304,24 +247,12 @@ void FbRun::keyPressEvent(XKeyEvent &ke) {
case XK_Down: case XK_Down:
nextHistoryItem(); nextHistoryItem();
break; break;
case XK_Left:
cursorLeft();
break;
case XK_Right:
cursorRight();
break;
case XK_Tab: case XK_Tab:
tabCompleteHistory(); tabCompleteHistory();
break; break;
default:
if (isprint(keychar[0])) insertCharacter(ks, keychar);
} }
} }
redrawLabel(); clear();
}
void FbRun::exposeEvent(XExposeEvent &ev) {
redrawLabel();
} }
void FbRun::setNoMaximize() { void FbRun::setNoMaximize() {
@ -340,9 +271,7 @@ void FbRun::prevHistoryItem() {
XBell(m_display, 0); XBell(m_display, 0);
} else { } else {
m_current_history_item--; m_current_history_item--;
m_runtext = m_history[m_current_history_item]; setText(m_history[m_current_history_item]);
m_cursor_pos = m_end_pos = m_runtext.size();
adjustStartPos();
} }
} }
@ -353,13 +282,9 @@ void FbRun::nextHistoryItem() {
m_current_history_item++; m_current_history_item++;
if (m_current_history_item == m_history.size()) { if (m_current_history_item == m_history.size()) {
m_current_history_item = m_history.size(); m_current_history_item = m_history.size();
m_runtext = ""; setText("");
m_start_pos = m_cursor_pos = m_end_pos = 0; } else
} else { setText(m_history[m_current_history_item]);
m_runtext = m_history[m_current_history_item];
m_cursor_pos = m_end_pos = m_runtext.size();
adjustStartPos();
}
} }
} }
@ -368,9 +293,7 @@ void FbRun::firstHistoryItem() {
XBell(m_display, 0); XBell(m_display, 0);
} else { } else {
m_current_history_item = 0; m_current_history_item = 0;
m_runtext = m_history[m_current_history_item]; setText(m_history[m_current_history_item]);
m_cursor_pos = m_end_pos = m_runtext.size();
adjustStartPos();
} }
} }
@ -380,8 +303,7 @@ void FbRun::lastHistoryItem() {
XBell(m_display, 0); XBell(m_display, 0);
} else { } else {
m_current_history_item = m_history.size(); m_current_history_item = m_history.size();
m_runtext = ""; setText("");
m_start_pos = m_cursor_pos = m_end_pos = 0;
} }
} }
@ -390,12 +312,11 @@ void FbRun::tabCompleteHistory() {
XBell(m_display, 0); XBell(m_display, 0);
} else { } else {
int history_item = m_current_history_item - 1; 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) { while (history_item > - 1) {
if (m_history[history_item].find(prefix) == 0) { if (m_history[history_item].find(prefix) == 0) {
m_current_history_item = history_item; m_current_history_item = history_item;
m_runtext = m_history[m_current_history_item]; setText(m_history[m_current_history_item]);
adjustEndPos();
break; break;
} }
history_item--; history_item--;
@ -404,92 +325,8 @@ void FbRun::tabCompleteHistory() {
} }
} }
void FbRun::cursorLeft() { void FbRun::insertCharacter(char keychar) {
if (m_cursor_pos) char val[2] = {keychar, 0};
m_cursor_pos--; insertText(val);
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::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;
}

View file

@ -19,7 +19,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // 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 #ifndef FBRUN_HH
#define FBRUN_HH #define FBRUN_HH
@ -27,6 +27,7 @@
#include "EventHandler.hh" #include "EventHandler.hh"
#include "Font.hh" #include "Font.hh"
#include "FbWindow.hh" #include "FbWindow.hh"
#include "TextBox.hh"
#include <string> #include <string>
#include <vector> #include <vector>
@ -34,19 +35,17 @@
/** /**
Creates and managed a run window Creates and managed a run window
*/ */
class FbRun: public FbTk::EventHandler, public FbTk::FbWindow { class FbRun: public FbTk::TextBox {
public: public:
FbRun(int x = 0, int y = 0, size_t width = 200); FbRun(int x = 0, int y = 0, size_t width = 200);
~FbRun(); ~FbRun();
void handleEvent(XEvent * const ev); void handleEvent(XEvent * const ev);
void setText(const std::string &text);
void setTitle(const std::string &title); 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 /// load and reconfigure for new font
bool loadFont(const std::string &fontname); bool loadFont(const std::string &fontname);
void setForegroundColor(const FbTk::Color &color); void setForegroundColor(const FbTk::Color &color);
void setBackgroundColor(const FbTk::Color &color);
void setAntialias(bool val) { m_font.setAntialias(val); } void setAntialias(bool val) { m_font.setAntialias(val); }
const FbTk::Font &font() const { return m_font; } const FbTk::Font &font() const { return m_font; }
/// execute command and exit /// execute command and exit
@ -62,15 +61,12 @@ public:
@name events @name events
*/ */
///@{ ///@{
void exposeEvent(XExposeEvent &ev);
void keyPressEvent(XKeyEvent &ev); void keyPressEvent(XKeyEvent &ev);
///@} ///@}
private: private:
void nextHistoryItem(); void nextHistoryItem();
void prevHistoryItem(); void prevHistoryItem();
void cursorLeft();
void cursorRight();
void drawString(int x, int y, const char *text, size_t len); void drawString(int x, int y, const char *text, size_t len);
void getSize(size_t &width, size_t &height); void getSize(size_t &width, size_t &height);
void createWindow(int x, int y, 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 /// set no maximizable for this window
void setNoMaximize(); void setNoMaximize();
void cursorHome(); void insertCharacter(char key);
void cursorEnd();
void backspace();
void deleteForward();
void killToEnd();
void insertCharacter(KeySym ks, char *keychar);
void adjustStartPos(); void adjustStartPos();
void adjustEndPos(); void adjustEndPos();
void firstHistoryItem(); void firstHistoryItem();
@ -92,8 +83,7 @@ private:
FbTk::Font m_font; ///< font used to draw command text FbTk::Font m_font; ///< font used to draw command text
Display *m_display; ///< display connection Display *m_display; ///< display connection
std::string m_runtext; ///< command to execute int m_bevel;
int m_bevel; ///< distance to window edge from font in pixels
GC m_gc; ///< graphic context GC m_gc; ///< graphic context
bool m_end; ///< marks when this object is done bool m_end; ///< marks when this object is done
std::vector<std::string> m_history; ///< history list of commands std::vector<std::string> m_history; ///< history list of commands
@ -101,9 +91,6 @@ private:
std::string m_history_file; ///< holds filename for command history file std::string m_history_file; ///< holds filename for command history file
Cursor m_cursor; 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; Pixmap m_pixmap;
}; };