rtorrent: Working on display.

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@237 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2005-01-27 20:14:21 +00:00
parent f622f45f0b
commit 74b154f07e
21 changed files with 1438 additions and 71 deletions
+5 -2
View File
@@ -3,7 +3,10 @@ noinst_LIBRARIES = libsub_display.a
libsub_display_a_SOURCES = \
canvas.cc \
canvas.h \
layers.cc \
layers.h
manager.cc \
manager.h \
manager_element.h \
window.h \
window.cc \
INCLUDES = -I$(srcdir) -I$(srcdir)/.. -I$(top_srcdir)
+6 -1
View File
@@ -1,10 +1,15 @@
#include "config.h"
#include "canvas.h"
#include <stdarg.h>
namespace display {
void
Canvas::resize(int x, int y, int w, int h) {
delwin(m_window);
m_window = newwin(h, w, y, x);
}
void
Canvas::init() {
initscr();
+6 -3
View File
@@ -8,20 +8,20 @@ namespace display {
class Canvas {
public:
Canvas(int x, int y, int width = 0, int height = 0) :
Canvas(int x = 0, int y = 0, int width = 0, int height = 0) :
m_window(newwin(height, width, y, x)) {}
~Canvas() { delwin(m_window); }
void refresh() { wnoutrefresh(m_window); }
void redraw() { redrawwin(m_window); }
void update_size(int w, int h) { wresize(m_window, h, w); }
void resize(int w, int h) { wresize(m_window, h, w); }
void resize(int x, int y, int w, int h);
int get_x() { int x, y; getyx(m_window, y, x); return x; }
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); }
@@ -44,6 +44,9 @@ public:
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:
-18
View File
@@ -1,18 +0,0 @@
#include "config.h"
#include "layers.h"
#include "canvas.h"
#include <functional>
#include <algorithm>
namespace display {
void
Layers::do_update() {
std::for_each(rbegin(), rend(), std::mem_fun(&Canvas::refresh));
Canvas::do_update();
}
}
-33
View File
@@ -1,33 +0,0 @@
#ifndef RTORRENT_DISPLAY_LAYERS_H
#define RTORRENT_DISPLAY_LAYERS_H
#include <list>
namespace display {
class Canvas;
class Layers : private std::list<Canvas*> {
public:
typedef std::list<Canvas*> Base;
using Base::iterator;
using Base::const_iterator;
using Base::reverse_iterator;
using Base::const_reverse_iterator;
using Base::begin;
using Base::end;
using Base::rbegin;
using Base::rend;
using Base::push_front;
using Base::push_back;
using Base::pop_front;
using Base::pop_back;
void do_update();
};
}
#endif
+51
View File
@@ -0,0 +1,51 @@
#include "config.h"
#include <stdexcept>
#include <algo/call.h>
#include <algo/entry.h>
#include <algo/lambda.h>
#include "canvas.h"
#include "manager.h"
#include "window_base.h"
namespace display {
using namespace algo;
void
Manager::adjust_layout() {
adjust_row(begin(), end(), 0, 0, display::Canvas::get_screen_width(), display::Canvas::get_screen_height());
}
void
Manager::adjust_row(iterator bItr, iterator eItr, int x, int y, int w, int h) {
int t;
int dist = std::distance(bItr, eItr);
switch (dist) {
case 1:
bItr->get_window()->resize(x, y, w, h);
break;
case 2:
t = w / 2 + w % 2;
bItr->get_window()->resize(x, y, t, h);
++bItr;
bItr->get_window()->resize(x + t, y, w - t, h);
break;
default:
throw std::logic_error("Manager::adjust_row(...) got a range with invalid number of elements");
}
}
void
Manager::do_update() {
std::for_each(begin(), end(), mem_fun(*mem_fun(_0(), &WindowBase::get_canvas), &Canvas::refresh));
Canvas::do_update();
}
}
+41
View File
@@ -0,0 +1,41 @@
#ifndef RTORRENT_DISPLAY_MANAGER_H
#define RTORRENT_DISPLAY_MANAGER_H
#include <list>
#include "manager_element.h"
namespace display {
class WindowBase;
class Manager : private std::list<ManagerElement> {
public:
typedef std::list<ManagerElement> Base;
using Base::iterator;
using Base::const_iterator;
using Base::reverse_iterator;
using Base::const_reverse_iterator;
using Base::begin;
using Base::end;
using Base::rbegin;
using Base::rend;
enum {
NEW_ROW = 0x1
};
Base::iterator add(WindowBase* w, int flags = 0) { return Base::insert(end(), ManagerElement(w, flags)); }
void adjust_layout();
void do_update();
private:
// Functions for adjusting ranges in different directions.
void adjust_row(iterator bItr, iterator eItr, int x, int y, int w, int h);
};
}
#endif
+25
View File
@@ -0,0 +1,25 @@
#ifndef RTORRENT_DISPLAY_MANAGER_ELEMENT_H
#define RTORRENT_DISPLAY_MANAGER_ELEMENT_H
namespace display {
class WindowBase;
class ManagerElement {
public:
ManagerElement(WindowBase* w, int flags) : m_window(w), m_flags(flags) {}
WindowBase* get_window() { return m_window; }
void set_window(WindowBase* w) { m_window = w; }
int get_flags() { return m_flags; }
void set_flags(int flags) { m_flags = flags; }
private:
WindowBase* m_window;
int m_flags;
};
}
#endif
+18
View File
@@ -0,0 +1,18 @@
#include "config.h"
#include "window_base.h"
#include "canvas.h"
namespace display {
void
Window::refresh() {
m_canvas->refresh();
}
void
Window::resize(int x, int y, int w, int h) {
m_canvas->resize(x, y, w, h);
}
}
+28
View File
@@ -0,0 +1,28 @@
#ifndef RTORRENT_WINDOW_BASE_H
#define RTORRENT_WINDOW_BASE_H
namespace display {
class Canvas;
class WindowBase {
public:
WindowBase() : m_canvas(NULL) {}
virtual ~WindowBase() {}
Canvas* get_canvas() { return m_canvas; }
void set_canvas(Canvas* c) { m_canvas = c; }
void refresh();
void resize(int x, int y, int w, int h);
virtual void do_update() = 0;
protected:
Canvas* m_canvas;
};
}
#endif
+34 -14
View File
@@ -1,30 +1,50 @@
#include <string>
#include "display/window_base.h"
#include "display/canvas.h"
#include "display/layers.h"
#include "display/manager.h"
class WindowTest : public display::WindowBase {
public:
WindowTest(const std::string& str) : m_str(str) {}
virtual void do_update() {
if (m_canvas == NULL)
return;
m_canvas->erase();
m_canvas->print_border('|', '|', '-', '-', '+', '+', '+', '+');
m_canvas->print(1, 1, "%s", m_str.c_str());
}
private:
std::string m_str;
};
int main(int argc, char** argv) {
display::Canvas::init();
display::Layers layers;
display::Manager manager;
display::Canvas canvas1(0, 0, 20, 0);
display::Canvas canvas2(15, 5, 20, 10);
WindowTest window1("This is window 1");
WindowTest window2("This is window 2");
layers.push_front(&canvas1);
layers.push_front(&canvas2);
window1.set_canvas(new display::Canvas());
window2.set_canvas(new display::Canvas());
canvas1.erase();
canvas2.erase();
canvas1.set_background(A_REVERSE);
manager.add(&window1);
manager.add(&window2);
canvas1.print_border('|', '|', '-', '-', '+', '+', '+', '+');
canvas2.print_border('|', '|', '-', '-', '+', '+', '+', '+');
window1.get_canvas()->set_background(A_REVERSE);
canvas1.print(2, 5, "test %i %s", 42, "bar");
layers.do_update();
manager.adjust_layout();
manager.do_update();
while (true);
display::Canvas::cleanup();
delete window1.get_canvas();
delete window2.get_canvas();
return 0;
}