* Starting work on ElementText for displaying generic information.

* If a storage error was thrown from make_directory in
EntryList::open, it would try to erase a FileMeta that wasn't inserted.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@753 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2006-08-09 19:55:34 +00:00
parent ba28df3ff2
commit ca58b43d8b
17 changed files with 360 additions and 67 deletions
+1
View File
@@ -38,6 +38,7 @@
#define RTORRENT_DISPLAY_TEXT_ELEMENT_H
#include <vector>
#include <inttypes.h>
#include "display/canvas.h"
+16 -3
View File
@@ -51,20 +51,33 @@ TextElementList::clear() {
char*
TextElementList::print(char* first, const char* last, Canvas::attributes_list* attributes, void* object) {
int column = m_columnWidth != NULL ? m_column : 0;
// Call print for each element even if first == last so that any
// attributes gets added to the list.
for (iterator itr = begin(); itr != end(); ++itr)
first = (*itr)->print(first, last, attributes, object);
if (column-- > 0) {
const char* columnEnd = std::min<const char*>(last, first + *m_columnWidth);
first = (*itr)->print(first, columnEnd, attributes, object);
std::memset(first, ' ', columnEnd - first);
first += columnEnd - first;
} else {
first = (*itr)->print(first, last, attributes, object);
}
return first;
}
TextElementList::extent_type
TextElementList::max_length() {
size_type length = 0;
extent_type length = 0;
int column = m_columnWidth != NULL ? m_column : 0;
for (iterator itr = begin(); itr != end(); ++itr) {
size_type l = (*itr)->max_length();
extent_type l = column-- > 0 ? std::min((*itr)->max_length(), *m_columnWidth) : (*itr)->max_length();
if (l == extent_full)
return extent_full;
+8
View File
@@ -61,13 +61,21 @@ public:
using base_type::push_back;
TextElementList() : m_column(0), m_columnWidth(0) {}
virtual ~TextElementList() { clear(); }
void clear();
void set_column(unsigned int column) { m_column = column; }
void set_column_width(extent_type* width) { m_columnWidth = width; }
virtual char* print(char* first, const char* last, Canvas::attributes_list* attributes, void* object);
virtual extent_type max_length();
private:
unsigned int m_column;
extent_type* m_columnWidth;
};
}
+21 -14
View File
@@ -36,21 +36,12 @@
#include "config.h"
#include <cstring>
#include "text_element_string.h"
namespace display {
char*
TextElementString::print(char* first, const char* last, Canvas::attributes_list* attributes, void* object) {
size_t length = std::min<size_t>(last - first, m_string.size());
if (length == 0)
return first;
std::memcpy(first, m_string.c_str(), length);
TextElementStringBase::print(char* first, const char* last, Canvas::attributes_list* attributes, void* object) {
// Move this stuff into a function in TextElement.
Attributes base = attributes->back();
Attributes current(NULL, m_attributes, Attributes::color_invalid);
@@ -65,17 +56,33 @@ TextElementString::print(char* first, const char* last, Canvas::attributes_list*
else if (current.colors() != base.colors())
current.set_position(first);
first = copy_string(first, last, object);
if (current.position() != NULL) {
attributes->push_back(current);
attributes->push_back(Attributes(first + length, base.attributes(), base.colors()));
attributes->push_back(Attributes(first, base.attributes(), base.colors()));
}
return first;
}
char*
TextElementString::copy_string(char* first, const char* last, void* object) {
extent_type length = std::min<extent_type>(last - first, m_string.size());
if (length == 0)
return first;
std::memcpy(first, m_string.c_str(), std::min<extent_type>(length, last - first));
return first + length;
}
TextElementString::extent_type
TextElementString::max_length() {
return m_string.size();
char*
TextElementCString::copy_string(char* first, const char* last, void* object) {
std::memcpy(first, m_string, std::min<extent_type>(m_length, last - first));
return first + m_length;
}
}
+83 -10
View File
@@ -37,32 +37,105 @@
#ifndef RTORRENT_DISPLAY_TEXT_ELEMENT_LIST_H
#define RTORRENT_DISPLAY_TEXT_ELEMENT_LIST_H
#include <iterator>
#include <string>
#include <cstring>
#include "text_element.h"
namespace display {
class TextElementString : public TextElement {
class TextElementStringBase : public TextElement {
public:
TextElementString() {}
TextElementString(const std::string& s, int attributes = Attributes::a_invalid) : m_string(s), m_attributes(attributes) {}
const std::string& str() const { return m_string; }
void set_str(const std::string& s) { m_string = s; }
int attributes() const { return m_attributes; }
void set_attributes(int a) { m_attributes = a; }
virtual char* print(char* first, const char* last, Canvas::attributes_list* attributes, void* object);
virtual extent_type max_length();
protected:
virtual char* copy_string(char* first, const char* last, void* object) = 0;
private:
std::string m_string;
int m_attributes;
};
class TextElementString : public TextElementStringBase {
public:
TextElementString(const std::string& s, int attributes = Attributes::a_invalid) :
m_string(s) { m_attributes = attributes; }
const std::string& str() const { return m_string; }
void set_str(const std::string& s) { m_string = s; }
private:
virtual extent_type max_length() { return m_string.size(); }
virtual char* copy_string(char* first, const char* last, void* object);
std::string m_string;
};
class TextElementCString : public TextElementStringBase {
public:
TextElementCString(const char* s, int attributes = Attributes::a_invalid) :
m_length(std::strlen(s)), m_string(s) {
m_attributes = attributes;
}
private:
virtual extent_type max_length() { return m_length; }
virtual char* copy_string(char* first, const char* last, void* object);
extent_type m_length;
const char* m_string;
};
template <typename slot_type>
class TextElementStringSlot : public TextElementStringBase {
public:
typedef typename slot_type::argument_type arg1_type;
typedef typename slot_type::result_type result_type;
TextElementStringSlot(const slot_type& slot, int attributes = Attributes::a_invalid) :
m_length(extent_full), m_slot(slot) {
m_attributes = attributes;
}
private:
virtual extent_type max_length() { return m_length; }
virtual char* copy_string(char* first, const char* last, void* object) {
arg1_type arg1 = reinterpret_cast<arg1_type>(object);
if (arg1 == NULL)
return first;
result_type result = m_slot(arg1);
extent_type length = std::min<extent_type>(result_length(&result), last - first);
std::memcpy(first, result_buffer(&result), length);
return first + length;
}
template <typename Result>
extent_type result_length(Result* result) { return result->size(); }
extent_type result_length(const char** result) { return std::strlen(*result); }
template <typename Result>
const char* result_buffer(Result* result) { return result->c_str(); }
const char* result_buffer(const char** result) { return *result; }
extent_type m_length;
slot_type m_slot;
};
template <typename slot_type>
inline TextElementStringSlot<slot_type>*
text_element_string_slot(const slot_type& slot, int attributes = Attributes::a_invalid) {
return new TextElementStringSlot<slot_type>(slot, attributes);
}
}
#endif
+9 -3
View File
@@ -58,8 +58,14 @@ WindowText::push_back(TextElement* element) {
// m_minHeight = size();
m_maxHeight = size();
if (element != NULL)
m_maxWidth = std::max(m_maxWidth, element->max_length() + 2);
if (element != NULL) {
extent_type width = element->max_length();
if (width == extent_full)
m_maxWidth = extent_full;
else
m_maxWidth = std::max(m_maxWidth, element->max_length() + m_margin);
}
// Check if active, if so do the update thingie. Or be lazy?
}
@@ -80,7 +86,7 @@ WindowText::redraw() {
Canvas::attributes_list attributes;
attributes.push_back(Attributes(buffer, Attributes::a_normal, Attributes::color_default));
char* last = (*itr)->print(buffer, buffer + m_canvas->width(), &attributes, NULL);
char* last = (*itr)->print(buffer, buffer + m_canvas->width(), &attributes, m_object);
m_canvas->print_attributes(0, position, buffer, last, &attributes);
}
+7 -1
View File
@@ -62,7 +62,8 @@ public:
using base_type::rbegin;
using base_type::rend;
WindowText() : Window(new Canvas, 0, 0, 0, extent_static, extent_static) {}
WindowText(void* object = NULL, extent_type margin = 0) :
Window(new Canvas, 0, 0, 0, extent_static, extent_static), m_object(object), m_margin(margin) {}
~WindowText() { clear(); }
void clear();
@@ -70,6 +71,11 @@ public:
void push_back(TextElement* element);
virtual void redraw();
private:
void* m_object;
extent_type m_margin;
};
}