Work on an input field.

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@272 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2005-02-13 21:47:26 +00:00
parent aec27deaeb
commit 73b1c58f4c
19 changed files with 313 additions and 25 deletions
-1
View File
@@ -9,7 +9,6 @@
#include "manager.h"
namespace core {
void
+2
View File
@@ -11,6 +11,8 @@ libsub_display_a_SOURCES = \
window_download_statusbar.h \
window_download_list.cc \
window_download_list.h \
window_input.cc \
window_input.h \
window_peer_info.cc \
window_peer_info.h \
window_peer_list.cc \
+12 -9
View File
@@ -23,6 +23,7 @@ public:
int get_y() { int x, y; getyx(m_window, y, x); return y; }
int get_width() { int x, y; getmaxyx(m_window, y, x); return x; }
int get_height() { int x, y; getmaxyx(m_window, y, x); return y; }
chtype get_background() { return getbkgd(m_window); }
void set_background(chtype c) { return wbkgdset(m_window, c); }
@@ -33,15 +34,6 @@ public:
chtype tl, chtype tr,
chtype bl, chtype br) { wborder(m_window, ls, rs, ts, bs, tl, tr, bl, br); }
// Initialize stdscr.
static void init();
static void cleanup();
static int get_screen_width() { int x, y; getmaxyx(stdscr, y, x); return x; }
static int get_screen_height() { int x, y; getmaxyx(stdscr, y, x); return y; }
static void do_update() { doupdate(); }
void print(int x, int y, const char* str) { mvwprintw(m_window, y, x, str); }
template <typename A1>
@@ -66,6 +58,17 @@ public:
void print(int x, int y, const char* str,
A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) { mvwprintw(m_window, y, x, str, a1, a2, a3, a4, a5, a6); }
void set_attr(int x, int y, int n, int attr, int color) { mvwchgat(m_window, y, x, n, attr, color, NULL); }
// Initialize stdscr.
static void init();
static void cleanup();
static int get_screen_width() { int x, y; getmaxyx(stdscr, y, x); return x; }
static int get_screen_height() { int x, y; getmaxyx(stdscr, y, x); return y; }
static void do_update() { doupdate(); }
private:
Canvas(const Canvas&);
void operator = (const Canvas&);
+9 -2
View File
@@ -8,7 +8,7 @@
namespace display {
WindowDownloadStatusbar::WindowDownloadStatusbar(core::Download* d) :
Window(new Canvas, false, 2),
Window(new Canvas, false, 3),
m_download(d) {
}
@@ -36,7 +36,14 @@ WindowDownloadStatusbar::redraw() {
(double)m_download->get_download().get_rate_down() / 1024.0,
(double)m_download->get_download().get_bytes_up() / (double)(1 << 20));
m_canvas->print(0, 1, "Tracker: [%c:%i] %s",
m_canvas->print(0, 1, "Peers: %i(%i) Min/Max: %i/%i Uploads: %i",
(int)m_download->get_download().get_peers_connected(),
(int)m_download->get_download().get_peers_not_connected(),
(int)m_download->get_download().get_peers_min(),
(int)m_download->get_download().get_peers_max(),
(int)m_download->get_download().get_uploads_max());
m_canvas->print(0, 2, "Tracker: [%c:%i] %s",
m_download->get_download().is_tracker_busy() ? 'C' : ' ',
(int)(m_download->get_download().get_tracker_timeout() / 1000000),
m_download->get_tracker_msg().c_str());
+30
View File
@@ -0,0 +1,30 @@
#include "config.h"
#include "canvas.h"
#include "window_input.h"
#include "input/text_input.h"
namespace display {
WindowInput::WindowInput(input::TextInput* input) :
Window(new Canvas, false, 1),
m_input(input),
m_focus(false) {
}
void
WindowInput::redraw() {
if (!is_dirty())
return;
m_lastDraw = Timer::cache();
m_canvas->erase();
m_canvas->print(0, 0, "> %s", m_input->c_str());
if (m_focus)
m_canvas->set_attr(m_input->get_pos() + 2, 0, 1, A_REVERSE, COLOR_PAIR(0));
}
}
+30
View File
@@ -0,0 +1,30 @@
#ifndef RTORRENT_DISPLAY_WINDOW_INPUT_H
#define RTORRENT_DISPLAY_WINDOW_INPUT_H
#include "window.h"
namespace input {
class TextInput;
}
namespace display {
class WindowInput : public Window {
public:
WindowInput(input::TextInput* input);
input::TextInput* get_input() { return m_input; }
bool get_focus() { return m_focus; }
void set_focus(bool f) { mark_dirty(); m_focus = f; }
virtual void redraw();
private:
input::TextInput* m_input;
bool m_focus;
};
}
#endif
+2 -2
View File
@@ -33,14 +33,14 @@ WindowPeerInfo::redraw() {
m_download->get_download().get_chunks_total(),
m_download->get_download().get_chunks_size());
y++;
if (*m_focus == m_list->end()) {
m_canvas->print(0, y++, "No peer in focus");
return;
}
y++;
m_canvas->print(0, y++, "DNS: %s:%hu", (*m_focus)->get_dns().c_str(), (*m_focus)->get_port());
m_canvas->print(0, y++, "Id: %s" , escape_string((*m_focus)->get_id()).c_str());
m_canvas->print(0, y++, "Snubbed: %s", (*m_focus)->get_snubbed() ? "Yes" : "No");
+5
View File
@@ -12,7 +12,12 @@ WindowTitle::WindowTitle(const std::string& s) :
void
WindowTitle::redraw() {
if (Timer::cache() - m_lastDraw < 10000000)
return;
m_lastDraw = Timer::cache();
m_canvas->erase();
m_canvas->print(std::max(0, (m_canvas->get_width() - (int)m_title.size()) / 2 - 4), 0,
"*** %s ***", m_title.c_str());
}
+11
View File
@@ -5,6 +5,17 @@
namespace func {
template <typename Iterator, typename Ftor>
inline void for_each(Iterator first, Iterator last, Ftor ftor) {
Iterator tmp;
while (first != last) {
tmp = first++;
ftor(*tmp);
}
}
template <typename Type, typename Ftor>
struct _accumulate {
_accumulate(Type& t, Ftor f) : m_t(t), m_f(f) {}
+3 -1
View File
@@ -4,6 +4,8 @@ libsub_input_a_SOURCES = \
bindings.cc \
bindings.h \
manager.cc \
manager.h
manager.h \
text_input.cc \
text_input.h
INCLUDES = -I$(srcdir) -I$(srcdir)/.. -I$(top_srcdir)
+4
View File
@@ -6,6 +6,7 @@
#include "manager.h"
#include "bindings.h"
#include "text_input.h"
namespace input {
@@ -21,6 +22,9 @@ Manager::erase(Bindings* b) {
void
Manager::pressed(int key) {
if (m_textInput != NULL && m_textInput->pressed(key))
return;
std::find_if(begin(), end(), std::bind2nd(std::mem_fun(&Bindings::pressed), key));
}
+8
View File
@@ -6,6 +6,7 @@
namespace input {
class Bindings;
class TextInput;
class Manager : private std::list<Bindings*> {
public:
@@ -24,11 +25,18 @@ public:
using Base::push_back;
using Base::push_front;
Manager() : m_textInput(NULL) {}
void erase(Bindings* b);
void pressed(int key);
void set_text_input(TextInput* input = NULL) { m_textInput = input; }
// Slot for unreacted keys.
private:
TextInput* m_textInput;
};
}
+79
View File
@@ -0,0 +1,79 @@
#include "config.h"
#include <ncurses.h>
#include "text_input.h"
#include <sstream>
namespace input {
bool
TextInput::pressed(int key) {
if (m_alt) {
m_alt = false;
switch (key) {
case 'p':
Base::insert(m_pos, "M^p");
break;
default:
return false;
}
} else if (key >= 0x20 && key < 0x7F) {
Base::insert(m_pos++, 1, key);
} else {
switch (key) {
case KEY_BACKSPACE:
if (m_pos != 0)
Base::erase(--m_pos, 1);
break;
case KEY_DC:
if (m_pos != size())
Base::erase(m_pos, 1);
break;
case KEY_LEFT:
case 0x10:
if (m_pos != 0)
--m_pos;
break;
case KEY_RIGHT:
case 0x0E:
if (m_pos != size())
++m_pos;
break;
default:
return false;
}
}
m_slotDirty();
return true;
// Testcode.
// if (key == KEY_ENTER || key == '\n')
// return false;
// std::stringstream str;
// str << "\\x" << std::hex << key;
// Base::insert(m_pos, str.str());
// m_pos += str.str().length();
// return true;
}
}
+37
View File
@@ -0,0 +1,37 @@
#ifndef RTORRENT_INPUT_TEXT_INPUT_H
#define RTORRENT_INPUT_TEXT_INPUT_H
#include <string>
#include <sigc++/slot.h>
namespace input {
class TextInput : private std::string {
public:
typedef std::string Base;
typedef sigc::slot0<void> SlotDirty;
using Base::c_str;
using Base::empty;
using Base::size;
TextInput() : m_pos(0), m_alt(false) {}
size_type get_pos() { return m_pos; }
bool pressed(int key);
void clear() { m_pos = 0; m_alt = false; Base::clear(); }
void slot_dirty(SlotDirty s) { m_slotDirty = s; }
private:
size_type m_pos;
bool m_alt;
SlotDirty m_slotDirty;
};
}
#endif
+1 -1
View File
@@ -124,7 +124,7 @@ main(int argc, char** argv) {
coreManager.get_http_queue().slot_factory(poll.get_http_factory());
torrent::initialize();
torrent::listen_open(6880, 6999);
torrent::listen_open(10000, 20000);
for (int i = 1; i < argc; ++i)
coreManager.insert(argv[i]);
+34 -6
View File
@@ -24,7 +24,7 @@ Download::Download(DPtr d, Control* c) :
m_title(c->get_display().end()),
m_window(c->get_display().end()),
m_downloadStatus(c->get_display().end()),
m_status(c->get_display().end()),
m_mainStatus(c->get_display().end()),
m_control(c),
m_bindings(new input::Bindings) {
@@ -55,7 +55,7 @@ Download::activate() {
m_window = m_control->get_display().insert(m_control->get_display().begin(), NULL);
m_title = m_control->get_display().insert(m_control->get_display().begin(), new WTitle(m_download->get_download().get_name()));
m_downloadStatus = m_control->get_display().insert(m_control->get_display().end(), new WDownloadStatus(m_download));
m_status = m_control->get_display().insert(m_control->get_display().end(), new WStatus);
m_mainStatus = m_control->get_display().insert(m_control->get_display().end(), new WMainStatus);
m_control->get_input().push_front(m_bindings);
@@ -71,14 +71,14 @@ Download::disable() {
delete *m_title;
delete *m_downloadStatus;
delete *m_status;
delete *m_mainStatus;
m_control->get_display().erase(m_window);
m_control->get_display().erase(m_title);
m_control->get_display().erase(m_downloadStatus);
m_control->get_display().erase(m_status);
m_control->get_display().erase(m_mainStatus);
m_window = m_title = m_downloadStatus = m_status = m_control->get_display().end();
m_window = m_title = m_downloadStatus = m_mainStatus = m_control->get_display().end();
m_control->get_input().erase(m_bindings);
}
@@ -180,11 +180,32 @@ Download::receive_peer_disconnected(torrent::Peer p) {
void
Download::receive_throttle(int t) {
(*m_status)->mark_dirty();
(*m_mainStatus)->mark_dirty();
torrent::set(torrent::THROTTLE_ROOT_CONST_RATE, torrent::get(torrent::THROTTLE_ROOT_CONST_RATE) + t * 1024);
}
void
Download::receive_max_uploads(int t) {
(*m_downloadStatus)->mark_dirty();
m_download->get_download().set_uploads_max(std::max(m_download->get_download().get_uploads_max() + t, (uint32_t)2));
}
void
Download::receive_min_peers(int t) {
(*m_downloadStatus)->mark_dirty();
m_download->get_download().set_peers_min(std::max(m_download->get_download().get_peers_min() + t, (uint32_t)5));
}
void
Download::receive_max_peers(int t) {
(*m_downloadStatus)->mark_dirty();
m_download->get_download().set_peers_max(std::max(m_download->get_download().get_peers_max() + t, (uint32_t)5));
}
void
Download::receive_change(Display d) {
if (d == m_state)
@@ -204,6 +225,13 @@ Download::bind_keys(input::Bindings* b) {
(*b)['d'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_throttle), 50);
(*b)['c'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_throttle), -50);
(*b)['1'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_max_uploads), -1);
(*b)['2'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_max_uploads), 1);
(*b)['3'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_min_peers), -5);
(*b)['4'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_min_peers), 5);
(*b)['5'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_max_peers), -5);
(*b)['6'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_max_peers), 5);
(*b)['t'] = sigc::bind(sigc::mem_fun(m_download->get_download(), &torrent::Download::set_tracker_timeout), 2 * 1000000);
(*b)[KEY_UP] = sigc::mem_fun(*this, &Download::receive_prev);
+5 -2
View File
@@ -27,7 +27,7 @@ public:
typedef display::WindowPeerInfo WPeerInfo;
typedef display::WindowPeerList WPeerList;
typedef display::WindowTitle WTitle;
typedef display::WindowStatusbar WStatus;
typedef display::WindowStatusbar WMainStatus;
typedef display::WindowDownloadStatusbar WDownloadStatus;
typedef core::Download* DPtr;
@@ -64,6 +64,9 @@ private:
void receive_peer_disconnected(torrent::Peer p);
void receive_throttle(int t);
void receive_max_uploads(int t);
void receive_min_peers(int t);
void receive_max_peers(int t);
void receive_change(Display d);
void bind_keys(input::Bindings* b);
@@ -79,7 +82,7 @@ private:
MItr m_title;
MItr m_window;
MItr m_downloadStatus;
MItr m_status;
MItr m_mainStatus;
Control* m_control;
input::Bindings* m_bindings;
+33 -1
View File
@@ -5,9 +5,11 @@
#include <sigc++/bind.h>
#include "input/bindings.h"
#include "input/text_input.h"
#include "display/window_title.h"
#include "display/window_download_list.h"
#include "display/window_statusbar.h"
#include "display/window_input.h"
#include "control.h"
#include "download.h"
@@ -22,11 +24,14 @@ DownloadList::DownloadList(core::DownloadList* l, Control* c) :
m_list(l),
m_focus(l->end()),
m_control(c),
m_bindings(new input::Bindings) {
m_bindings(new input::Bindings),
m_windowInput(new WInput(new input::TextInput)) {
m_window = new WList(m_list, &m_focus);
bind_keys(m_bindings);
m_windowInput->get_input()->slot_dirty(sigc::mem_fun(*m_windowInput, &WInput::mark_dirty));
}
DownloadList::~DownloadList() {
@@ -34,10 +39,14 @@ DownloadList::~DownloadList() {
delete m_title;
delete m_status;
delete m_bindings;
delete m_windowInput->get_input();
delete m_windowInput;
}
void
DownloadList::activate() {
m_control->get_display().push_back(m_windowInput);
m_control->get_display().push_back(m_status);
m_control->get_display().push_front(m_window);
m_control->get_display().push_front(m_title);
@@ -46,6 +55,7 @@ DownloadList::activate() {
void
DownloadList::disable() {
m_control->get_display().erase(m_windowInput);
m_control->get_display().erase(m_title);
m_control->get_display().erase(m_window);
m_control->get_display().erase(m_status);
@@ -109,6 +119,27 @@ DownloadList::receive_throttle(int t) {
torrent::set(torrent::THROTTLE_ROOT_CONST_RATE, torrent::get(torrent::THROTTLE_ROOT_CONST_RATE) + t * 1024);
}
void
DownloadList::receive_view_input() {
m_control->get_input().set_text_input(m_windowInput->get_input());
m_windowInput->set_focus(true);
(*m_bindings)['\n'] = sigc::mem_fun(*this, &DownloadList::receive_exit_input);
(*m_bindings)[KEY_ENTER] = sigc::mem_fun(*this, &DownloadList::receive_exit_input);
}
void
DownloadList::receive_exit_input() {
m_control->get_input().set_text_input();
m_windowInput->get_input()->clear();
m_windowInput->set_focus(false);
m_bindings->erase('\n');
m_bindings->erase(KEY_ENTER);
}
void
DownloadList::bind_keys(input::Bindings* b) {
(*b)['a'] = sigc::bind(sigc::mem_fun(*this, &DownloadList::receive_throttle), 1);
@@ -122,6 +153,7 @@ DownloadList::bind_keys(input::Bindings* b) {
(*b)[KEY_DOWN] = sigc::mem_fun(*this, &DownloadList::receive_next);
(*b)[KEY_RIGHT] = sigc::mem_fun(*this, &DownloadList::receive_view_download);
(*b)[KEY_BACKSPACE] = sigc::mem_fun(*this, &DownloadList::receive_view_input);
}
void
+8
View File
@@ -5,6 +5,7 @@ namespace display {
class WindowTitle;
class WindowDownloadList;
class WindowStatusbar;
class WindowInput;
}
namespace ui {
@@ -17,6 +18,7 @@ public:
typedef display::WindowDownloadList WList;
typedef display::WindowTitle WTitle;
typedef display::WindowStatusbar WStatus;
typedef display::WindowInput WInput;
typedef core::DownloadList DList;
// We own 'window'.
@@ -41,6 +43,9 @@ private:
void receive_throttle(int t);
void receive_view_input();
void receive_exit_input();
void bind_keys(input::Bindings* b);
void mark_dirty();
@@ -56,6 +61,9 @@ private:
Control* m_control;
input::Bindings* m_bindings;
// Test
WInput* m_windowInput;
};
}