fluxbox/util/fbrun/FbRun.cc

278 lines
7.8 KiB
C++
Raw Normal View History

2002-08-20 02:05:17 +00:00
// FbRun.hh
// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
2003-04-27 02:26:21 +00:00
// $Id: FbRun.cc,v 1.11 2003/04/27 02:26:21 rathnor Exp $
2002-08-20 02:05:17 +00:00
#include "FbRun.hh"
2002-11-26 17:13:36 +00:00
#include "App.hh"
#include "EventManager.hh"
2002-12-05 00:07:39 +00:00
#include "Color.hh"
2002-08-20 02:05:17 +00:00
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/Xutil.h>
2003-03-22 11:33:04 +00:00
#include <X11/cursorfont.h>
2002-08-20 02:05:17 +00:00
#include <unistd.h>
#include <iostream>
2002-11-12 19:20:31 +00:00
#include <fstream>
2003-04-27 02:26:21 +00:00
#include <cassert>
2002-08-20 02:05:17 +00:00
using namespace std;
FbRun::FbRun(int x, int y, size_t width):
2002-12-05 00:07:39 +00:00
m_font("fixed"),
m_win((int)0, x, y, //screen num and position
2003-03-22 11:33:04 +00:00
width + m_bevel, m_font.height() + 2, // size
2002-12-05 00:07:39 +00:00
KeyPressMask|ExposureMask), // eventmask
m_display(FbTk::App::instance()->display()),
m_bevel(4),
m_gc(DefaultGC(m_display, DefaultScreen(m_display))),
m_end(false),
2003-03-22 11:33:04 +00:00
m_current_history_item(0),
m_cursor(XCreateFontCursor(FbTk::App::instance()->display(), XC_xterm)),
m_cursor_pos(0) {
XDefineCursor(FbTk::App::instance()->display(), m_win.window(), m_cursor);
2002-12-05 00:07:39 +00:00
// setting nomaximize in local resize
resize(width, m_font.height());
FbTk::EventManager::instance()->registerEventHandler(*this, m_win.window());
2002-08-20 02:05:17 +00:00
}
FbRun::~FbRun() {
2002-12-05 00:07:39 +00:00
hide();
FbTk::EventManager::instance()->unregisterEventHandler(m_win.window());
2002-08-20 02:05:17 +00:00
}
void FbRun::run(const std::string &command) {
2002-12-05 00:07:39 +00:00
//fork and execute program
if (!fork()) {
setsid();
execl("/bin/sh", "/bin/sh", "-c", command.c_str(), 0);
exit(0); //exit child
}
hide(); // hide gui
2002-11-12 19:20:31 +00:00
2002-12-05 00:07:39 +00:00
// save command history to file
if (m_runtext.size() != 0) { // no need to save empty command
// open file in append mode
ofstream outfile(m_history_file.c_str(), ios::app);
if (outfile)
outfile<<m_runtext<<endl;
else
cerr<<"FbRun Warning: Can't write command history to file: "<<m_history_file<<endl;
}
FbTk::App::instance()->end(); // end application
m_end = true; // mark end of processing
2002-08-20 02:05:17 +00:00
}
2002-11-12 19:20:31 +00:00
bool FbRun::loadHistory(const char *filename) {
2002-12-05 00:07:39 +00:00
if (filename == 0)
return false;
ifstream infile(filename);
if (!infile) {
//even though we fail to load file, we should try save to it
m_history_file = filename;
return false;
}
// clear old history and load new one from file
m_history.clear();
// each line is a command
string line;
while (!infile.eof()) {
getline(infile, line);
if (line.size()) // don't add empty lines
m_history.push_back(line);
}
// set no current histor to display
m_current_history_item = m_history.size();
// set history file
m_history_file = filename;
return true;
2002-11-12 19:20:31 +00:00
}
2002-08-20 02:05:17 +00:00
bool FbRun::loadFont(const string &fontname) {
2002-12-05 00:07:39 +00:00
if (!m_font.load(fontname.c_str()))
return false;
2002-08-20 02:05:17 +00:00
2002-12-05 00:07:39 +00:00
// resize to fit new font height
resize(m_win.width(), m_font.height() + m_bevel);
return true;
2002-08-20 02:05:17 +00:00
}
2002-12-05 00:07:39 +00:00
void FbRun::setForeground(const FbTk::Color &color) {
XSetForeground(m_display, m_gc, color.pixel());
redrawLabel();
2002-08-20 02:05:17 +00:00
}
2002-12-05 00:07:39 +00:00
void FbRun::setBackground(const FbTk::Color &color) {
m_win.setBackgroundColor(color);
redrawLabel();
2002-08-20 02:05:17 +00:00
}
void FbRun::setText(const string &text) {
2002-12-05 00:07:39 +00:00
m_runtext = text;
redrawLabel();
2002-08-20 02:05:17 +00:00
}
void FbRun::setTitle(const string &title) {
2002-12-05 00:07:39 +00:00
m_win.setName(title.c_str());
2002-08-20 02:05:17 +00:00
}
void FbRun::move(int x, int y) {
2002-12-05 00:07:39 +00:00
m_win.move(x, y);
2002-08-20 02:05:17 +00:00
}
void FbRun::resize(size_t width, size_t height) {
2002-12-05 00:07:39 +00:00
m_win.resize(width, height);
setNoMaximize();
2002-08-20 02:05:17 +00:00
}
void FbRun::show() {
2002-12-05 00:07:39 +00:00
m_win.show();
2002-08-20 02:05:17 +00:00
}
void FbRun::hide() {
2002-12-05 00:07:39 +00:00
m_win.hide();
2002-08-20 02:05:17 +00:00
}
void FbRun::redrawLabel() {
2002-12-05 00:07:39 +00:00
m_win.clear();
drawString(m_bevel/2, m_font.ascent() + m_bevel/2,
m_runtext.c_str(), m_runtext.size());
2002-08-20 02:05:17 +00:00
}
void FbRun::drawString(int x, int y,
2002-12-05 00:07:39 +00:00
const char *text, size_t len) {
assert(m_gc);
2002-08-20 02:05:17 +00:00
2002-12-05 00:07:39 +00:00
// check right boundary and adjust text drawing
size_t text_width = m_font.textWidth(text, len);
size_t startpos = 0;
if (text_width > m_win.width()) {
for (; startpos < len; ++startpos) {
if (m_font.textWidth(text+startpos, len-startpos) < m_win.width())
break;
}
}
2002-08-20 02:05:17 +00:00
2003-03-22 11:33:04 +00:00
m_font.drawText(m_win.window(), DefaultScreen(m_display), m_gc, text + startpos, len-startpos, x, y - 2);
int cursor_pos = m_font.textWidth(text + m_cursor_pos, len - startpos) + 1;
// draw cursor position
XDrawLine(FbTk::App::instance()->display(), m_win.window(), m_gc,
cursor_pos, 0,
cursor_pos, m_font.height());
2002-08-20 02:05:17 +00:00
}
void FbRun::keyPressEvent(XKeyEvent &ke) {
2002-12-05 00:07:39 +00:00
KeySym ks;
char keychar[1];
XLookupString(&ke, keychar, 1, &ks, 0);
if (ks == XK_Escape) {
m_end = true;
hide();
FbTk::App::instance()->end(); // end program
} else if (ks == XK_Return) {
run(m_runtext);
m_runtext = ""; // clear text
} else if (ks == XK_BackSpace) {
if (m_runtext.size() != 0) { // we can't erase what we don't have ;)
m_runtext.erase(m_runtext.size()-1);
redrawLabel();
2003-03-22 11:33:04 +00:00
m_cursor_pos--;
2002-12-05 00:07:39 +00:00
}
2003-03-22 11:33:04 +00:00
} else if (! IsModifierKey(ks) && !IsCursorKey(ks)) { // insert normal character at cursor pos
char in_char[2] = {keychar[0], 0};
m_runtext.insert(m_cursor_pos, in_char);
m_cursor_pos++;
2002-12-05 00:07:39 +00:00
redrawLabel();
} else if (IsCursorKey(ks)) {
2002-12-05 00:07:39 +00:00
switch (ks) {
case XK_Up:
prevHistoryItem();
break;
case XK_Down:
nextHistoryItem();
break;
2003-03-22 11:33:04 +00:00
case XK_Left:
cursorLeft();
break;
case XK_Right:
cursorRight();
break;
2002-12-05 00:07:39 +00:00
}
redrawLabel();
2003-03-22 11:33:04 +00:00
} else if (ks == XK_End) {
m_cursor_pos = m_runtext.size() - 1;
2002-12-05 00:07:39 +00:00
}
2002-08-20 02:05:17 +00:00
}
void FbRun::exposeEvent(XExposeEvent &ev) {
2002-12-05 00:07:39 +00:00
redrawLabel();
}
2002-08-20 02:05:17 +00:00
void FbRun::setNoMaximize() {
2002-12-05 00:07:39 +00:00
// we don't need to maximize this window
XSizeHints sh;
sh.flags = PMaxSize | PMinSize;
sh.max_width = m_win.width();
sh.max_height = m_win.height();
sh.min_width = m_win.width();
sh.min_height = m_win.height();
XSetWMNormalHints(m_display, m_win.window(), &sh);
2002-08-20 02:05:17 +00:00
}
2002-11-12 19:20:31 +00:00
void FbRun::prevHistoryItem() {
2002-12-05 00:07:39 +00:00
if (m_current_history_item > 0 && m_history.size() > 0)
m_current_history_item--;
if (m_current_history_item < m_history.size())
m_runtext = m_history[m_current_history_item];
2002-11-12 19:20:31 +00:00
}
void FbRun::nextHistoryItem() {
2002-12-05 00:07:39 +00:00
m_current_history_item++;
if (m_current_history_item >= m_history.size()) {
m_current_history_item = m_history.size();
m_runtext = "";
return;
} else
m_runtext = m_history[m_current_history_item];
2002-11-12 19:20:31 +00:00
}
2003-03-22 11:33:04 +00:00
void FbRun::cursorLeft() {
if (m_cursor_pos > 0)
m_cursor_pos--;
}
void FbRun::cursorRight() {
m_cursor_pos++;
if (m_cursor_pos >= m_runtext.size())
m_cursor_pos = m_runtext.size() - 1;
}