* UI refactoring, added display::Frame and started cleaning up the

code.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@747 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2006-07-26 21:41:09 +00:00
parent ef90f0f51e
commit fc76a11d71
41 changed files with 889 additions and 471 deletions
+2
View File
@@ -5,6 +5,8 @@ libsub_display_a_SOURCES = \
canvas.h \
client_info.cc \
client_info.h \
frame.cc \
frame.h \
manager.cc \
manager.h \
utils.cc \
+239
View File
@@ -0,0 +1,239 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2006, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#include "config.h"
#include <algorithm>
#include <functional>
#include <rak/algorithm.h>
#include <torrent/exceptions.h>
#include "frame.h"
#include "window.h"
namespace display {
Frame::Frame() :
m_type(TYPE_NONE),
m_positionX(0),
m_positionY(0),
m_width(0),
m_height(0) {
}
bool
Frame::is_dynamic() const {
switch (m_type) {
case TYPE_NONE:
return false;
case TYPE_WINDOW:
return m_window->is_active() && m_window->is_dynamic();
case TYPE_ROW:
case TYPE_COLUMN:
for (size_type i = 0; i < m_containerSize; ++i)
if (m_container[i]->is_dynamic())
return true;
return false;
}
return false;
}
Frame::pair_type
Frame::preferred_size() const {
switch (m_type) {
case TYPE_NONE:
return pair_type(0, 0);
case TYPE_WINDOW:
return m_window->is_active() ? pair_type(0, m_window->get_min_height()) : pair_type(0, 0);
case TYPE_ROW:
case TYPE_COLUMN:
{
pair_type accum(0, 0);
for (size_type i = 0; i < m_containerSize; ++i) {
pair_type p = m_container[i]->preferred_size();
accum.first += p.first;
accum.second += p.second;
}
return accum;
}
}
return pair_type(0, 0);
}
void
Frame::set_container_size(size_type size) {
if ((m_type != TYPE_ROW && m_type != TYPE_COLUMN) || size >= max_size)
throw torrent::client_error("Frame::set_container_size(...) Bad state.");
while (m_containerSize > size) {
delete m_container[--m_containerSize];
m_container[m_containerSize] = NULL;
}
while (m_containerSize < size) {
m_container[m_containerSize++] = new Frame();
}
}
void
Frame::initialize_window(Window* window) {
if (m_type != TYPE_NONE)
throw torrent::client_error("Frame::initialize_window(...) m_type != TYPE_NONE.");
m_type = TYPE_WINDOW;
m_window = window;
}
void
Frame::initialize_container(Type containerType, size_type size) {
if (m_type != TYPE_NONE || (containerType != TYPE_ROW && containerType != TYPE_COLUMN))
throw torrent::client_error("Frame::initialize_container(...) Invalid state.");
if (size > max_size)
throw torrent::client_error("Frame::initialize_container(...) size >= max_size.");
m_type = containerType;
m_containerSize = size;
for (size_type i = 0; i < m_containerSize; ++i)
m_container[i] = new Frame();
}
void
Frame::clear() {
switch (m_type) {
case TYPE_ROW:
case TYPE_COLUMN:
for (size_type i = 0; i < m_containerSize; ++i) {
m_container[i]->clear();
delete m_container[i];
}
break;
default:
break;
}
m_type = TYPE_NONE;
}
void
Frame::refresh() {
switch (m_type) {
case TYPE_NONE:
break;
case TYPE_WINDOW:
if (m_window->is_active())
m_window->refresh();
break;
case TYPE_ROW:
case TYPE_COLUMN:
for (Frame **itr = m_container, **last = m_container + m_containerSize; itr != last; ++itr)
(*itr)->refresh();
break;
}
}
void
Frame::balance(uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
m_positionX = x;
m_positionY = y;
m_width = width;
m_height = height;
if (m_type == TYPE_NONE)
return;
if (m_type == TYPE_WINDOW) {
if (m_window->is_active())
m_window->resize(x, y, width, height);
return;
}
uint32_t size;
if (m_type == TYPE_ROW)
size = height - std::min(height, preferred_size().second);
else
size = width - std::min(width, preferred_size().first);
uint32_t dynamicCount = std::count_if(m_container, m_container + m_containerSize, std::mem_fun(&Frame::is_dynamic));
for (Frame **itr = m_container, **last = m_container + m_containerSize; itr != last; ++itr) {
uint32_t s;
if ((*itr)->is_dynamic()) {
s = (size + dynamicCount - 1) / dynamicCount;
size -= s;
dynamicCount--;
} else {
s = 0;
}
if (m_type == TYPE_ROW) {
s += (*itr)->preferred_size().second;
(*itr)->balance(x, y, m_width, s);
y += s;
} else {
s += (*itr)->preferred_size().first;
(*itr)->balance(x, y, s, height);
x += s;
}
}
}
}
+110
View File
@@ -0,0 +1,110 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2006, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_DISPLAY_FRAME_H
#define RTORRENT_DISPLAY_FRAME_H
#include <inttypes.h>
namespace display {
class Window;
class Frame {
public:
enum Type {
TYPE_NONE,
TYPE_WINDOW,
TYPE_ROW,
TYPE_COLUMN
};
typedef uint32_t size_type;
typedef std::pair<uint32_t, uint32_t> pair_type;
static const size_type max_size = 5;
Frame();
bool is_dynamic() const;
pair_type preferred_size() const;
uint32_t position_x() const { return m_positionX; }
uint32_t position_y() const { return m_positionY; }
uint32_t width() const { return m_width; }
uint32_t height() const { return m_height; }
Type get_type() const { return m_type; }
void set_type(Type t);
Window* window() const { return m_window; }
Frame* frame(size_type idx) { return m_container[idx]; }
size_type container_size() const { return m_containerSize; }
void set_container_size(size_type size);
void initialize_window(Window* window);
void initialize_container(Type containerType, size_type size);
void clear();
void refresh();
void balance(uint32_t x, uint32_t y, uint32_t width, uint32_t height);
private:
Type m_type;
uint32_t m_positionX;
uint32_t m_positionY;
uint32_t m_width;
uint32_t m_height;
union {
Window* m_window;
struct {
size_type m_containerSize;
Frame* m_container[max_size];
};
};
};
}
#endif
+5 -59
View File
@@ -49,6 +49,7 @@ namespace display {
Manager::Manager() :
m_forceRedraw(false) {
m_taskUpdate.set_slot(rak::mem_fn(this, &Manager::receive_update));
}
@@ -61,36 +62,6 @@ Manager::force_redraw() {
m_forceRedraw = true;
}
Manager::iterator
Manager::insert(iterator pos, Window* w) {
return Base::insert(pos, w);
}
// Swap with the function below.
Manager::iterator
Manager::erase(iterator pos) {
if (pos != end())
return erase(*pos);
else
return end();
}
Manager::iterator
Manager::erase(Window* w) {
iterator itr = std::find(begin(), end(), w);
if (itr == end())
throw std::logic_error("Manager::erase(...) did not find the window");
return Base::erase(itr);
}
Manager::iterator
Manager::find(Window* w) {
return std::find(begin(), end(), w);
}
void
Manager::schedule(Window* w, rak::timer t) {
rak::priority_queue_erase(&m_scheduler, w->task_update());
@@ -106,34 +77,7 @@ Manager::unschedule(Window* w) {
void
Manager::adjust_layout() {
int staticHeight = std::for_each(begin(), end(),
rak::if_then(std::mem_fun(&Window::is_active),
rak::accumulate(0, std::mem_fun(&Window::get_min_height)))).m_then.result;
int countDynamic = std::for_each(begin(), end(),
rak::if_then(std::mem_fun(&Window::is_active),
rak::accumulate(0, std::mem_fun(&Window::is_dynamic)))).m_then.result;
int dynamic = std::max(0, Canvas::get_screen_height() - staticHeight);
int height = 0, h;
for (iterator itr = begin(); itr != end(); ++itr, height += h) {
h = 0;
if (!(*itr)->is_active())
continue;
if ((*itr)->is_dynamic()) {
dynamic -= h = (dynamic + countDynamic - 1) / countDynamic;
countDynamic--;
} else {
h = 0;
}
h += (*itr)->get_min_height();
(*itr)->resize(0, height, Canvas::get_screen_width(), h);
(*itr)->mark_dirty();
}
m_rootFrame.balance(0, 0, Canvas::get_screen_width(), Canvas::get_screen_height());
}
void
@@ -149,7 +93,9 @@ Manager::receive_update() {
Canvas::refresh_std();
rak::priority_queue_perform(&m_scheduler, cachedTime);
std::for_each(begin(), end(), rak::if_then(std::mem_fun(&Window::is_active), std::mem_fun(&Window::refresh)));
// std::for_each(begin(), end(), rak::if_then(std::mem_fun(&Window::is_active), std::mem_fun(&Window::refresh)));
m_rootFrame.refresh();
Canvas::do_update();
+9 -22
View File
@@ -40,44 +40,28 @@
#include <list>
#include <rak/priority_queue_default.h>
#include "frame.h"
namespace display {
class Window;
class Manager : private std::list<Window*> {
class Manager {
public:
typedef std::list<Window*> 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;
Manager();
~Manager();
void force_redraw();
iterator insert(iterator pos, Window* w);
iterator erase(iterator pos);
iterator erase(Window* w);
iterator find(Window* w);
void schedule(Window* w, rak::timer t);
void unschedule(Window* w);
void adjust_layout();
void receive_update();
// New interface.
Frame* root_frame() { return &m_rootFrame; }
private:
void schedule_update();
@@ -86,6 +70,9 @@ private:
rak::priority_queue_default m_scheduler;
rak::priority_item m_taskUpdate;
// New interface.
Frame m_rootFrame;
};
}
+2 -5
View File
@@ -83,9 +83,7 @@ WindowHttpQueue::redraw() {
m_canvas->print(pos, 0, "%s ---%%", itr->m_name.c_str());
else
m_canvas->print(pos, 0, "%s %3i%%",
itr->m_name.c_str(),
(int)(100.0 * itr->m_http->size_done() / itr->m_http->size_total()));
m_canvas->print(pos, 0, "%s %3i%%", itr->m_name.c_str(), (int)(100.0 * itr->m_http->size_done() / itr->m_http->size_total()));
pos += itr->m_name.size() + 6;
++itr;
@@ -140,8 +138,7 @@ WindowHttpQueue::receive_insert(core::CurlGet* h) {
void
WindowHttpQueue::receive_erase(core::CurlGet* h) {
Container::iterator itr = std::find_if(m_container.begin(), m_container.end(),
rak::equal(h, std::mem_fun_ref(&Node::get_http)));
Container::iterator itr = std::find_if(m_container.begin(), m_container.end(), rak::equal(h, std::mem_fun_ref(&Node::get_http)));
if (itr == m_container.end())
throw std::logic_error("WindowHttpQueue::receive_erase(...) tried to remove an object we don't have");
+1 -7
View File
@@ -43,16 +43,10 @@
namespace display {
WindowInput::WindowInput(input::TextInput* input) :
Window(new Canvas, false, 1),
m_input(input),
m_focus(false) {
}
void
WindowInput::redraw() {
m_canvas->erase();
m_canvas->print(0, 0, "> %s", m_input->c_str());
m_canvas->print(0, 0, "> %s", m_input != NULL ? m_input->c_str() : "<NULL>");
if (m_focus)
m_canvas->set_attr(m_input->get_pos() + 2, 0, 1, A_REVERSE, COLOR_PAIR(0));
+8 -7
View File
@@ -47,18 +47,19 @@ namespace display {
class WindowInput : public Window {
public:
WindowInput(input::TextInput* input);
WindowInput() : Window(new Canvas, false, 1), m_input(NULL), m_focus(false) {}
input::TextInput* get_input() { return m_input; }
input::TextInput* input() { return m_input; }
void set_input(input::TextInput* input) { m_input = input; }
bool get_focus() { return m_focus; }
void set_focus(bool f) { mark_dirty(); m_focus = f; }
bool focus() const { return m_focus; }
void set_focus(bool f) { mark_dirty(); m_focus = f; }
virtual void redraw();
virtual void redraw();
private:
input::TextInput* m_input;
bool m_focus;
input::TextInput* m_input;
bool m_focus;
};
}
+1 -1
View File
@@ -67,7 +67,7 @@ WindowLogComplete::redraw() {
int pos = 0;
m_canvas->print(std::max(0, (int)m_canvas->get_width() / 2 - 5), pos++, "*** Log ***");
// m_canvas->print(std::max(0, (int)m_canvas->get_width() / 2 - 5), pos++, "*** Log ***");
for (core::Log::iterator itr = m_log->begin(), e = m_log->end(); itr != e && pos < m_canvas->get_height(); ++itr) {
char buffer[16];
+2 -9
View File
@@ -46,12 +46,6 @@
namespace display {
WindowStatusbar::WindowStatusbar(Control* c) :
Window(new Canvas, false, 1),
m_lastTick(0),
m_control(c) {
}
void
WindowStatusbar::redraw() {
m_slotSchedule(this, (cachedTime + rak::timer::from_seconds(1)).round_seconds());
@@ -101,12 +95,11 @@ WindowStatusbar::redraw() {
last = last - (position - buffer);
if (last > buffer) {
position = print_status_extra(buffer, last, m_control);
position = print_status_extra(buffer, last, control);
m_canvas->print(m_canvas->get_width() - (position - buffer), 0, "%s", buffer);
}
m_lastTick = m_control->tick();
m_lastTick = control->tick();
}
}
+1 -4
View File
@@ -41,19 +41,16 @@
#include "window.h"
class Control;
namespace display {
class WindowStatusbar : public Window {
public:
WindowStatusbar(Control* c);
WindowStatusbar() : Window(new Canvas, false, 1), m_lastTick(0) {}
virtual void redraw();
private:
uint64_t m_lastTick;
Control* m_control;
};
}
+1 -6
View File
@@ -41,18 +41,13 @@
namespace display {
WindowTitle::WindowTitle(const std::string& s) :
Window(new Canvas, false, 1),
m_title(s) {
}
void
WindowTitle::redraw() {
m_slotSchedule(this, (cachedTime + rak::timer::from_seconds(1)).round_seconds());
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());
"*** %s ***", m_title.c_str());
}
}
+6 -3
View File
@@ -44,12 +44,15 @@ namespace display {
class WindowTitle : public Window {
public:
WindowTitle(const std::string& s);
WindowTitle() : Window(new Canvas, false, 1) {}
virtual void redraw();
const std::string& title() const { return m_title; }
void set_title(const std::string& title) { m_title = title; mark_dirty(); }
virtual void redraw();
private:
std::string m_title;
std::string m_title;
};
}