Possibility for a FListView column to set the alignment

This commit is contained in:
Markus Gans 2017-07-28 22:18:42 +02:00
parent 30515db9ec
commit 0240d782ca
8 changed files with 198 additions and 99 deletions

View File

@ -1,3 +1,7 @@
2017-07-28 Markus Gans <guru.mail@muenster.de>
* Possibility for a FListView column to set
the alignment (left, center or right)
2017-07-23 Markus Gans <guru.mail@muenster.de> 2017-07-23 Markus Gans <guru.mail@muenster.de>
* Check an object with isInstanceOf(...) whether it is * Check an object with isInstanceOf(...) whether it is
an instance of a specified class an instance of a specified class

View File

@ -340,7 +340,7 @@ void FLabel::setHotkeyAccelerator()
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
int FLabel::getXOffset(int length) int FLabel::getAlignOffset (int length)
{ {
switch ( alignment ) switch ( alignment )
{ {
@ -358,25 +358,24 @@ int FLabel::getXOffset(int length)
return getWidth() - length; return getWidth() - length;
else else
return 0; return 0;
default:
return 0;
} }
return 0;
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FLabel::printLine ( wchar_t*& line void FLabel::printLine ( wchar_t*& line
, uInt length , uInt length
, int hotkeypos , int hotkeypos
, int xoffset ) , int align_offset )
{ {
int to_char; int to_char;
bool isActive, isNoUnderline; bool isActive, isNoUnderline;
isActive = ((flags & fc::active) != 0); isActive = ((flags & fc::active) != 0);
isNoUnderline = ((flags & fc::no_underline) != 0); isNoUnderline = ((flags & fc::no_underline) != 0);
for (int x=0; x < xoffset; x++) if ( align_offset > 0 )
print (' '); print (FString(align_offset, ' ')); // leading spaces
if ( length <= uInt(getWidth()) ) if ( length <= uInt(getWidth()) )
to_char = int(length); to_char = int(length);
@ -423,10 +422,10 @@ void FLabel::printLine ( wchar_t*& line
print (".."); print ("..");
setColor(); setColor();
} }
else else if ( align_offset + to_char < getWidth() )
{ {
for (int x=xoffset+to_char; x < getWidth(); x++) int len = getWidth() - align_offset - to_char;
print (' '); print (FString(len, ' ')); // tailing spaces
} }
if ( hasReverseMode() ) if ( hasReverseMode() )
@ -440,7 +439,7 @@ void FLabel::draw()
wchar_t* dest; wchar_t* dest;
wchar_t* LabelText; wchar_t* LabelText;
uInt length; uInt length;
int hotkeypos, xoffset; int hotkeypos, align_offset;
if ( text.isNull() || text.isEmpty() ) if ( text.isNull() || text.isEmpty() )
return; return;
@ -482,15 +481,15 @@ void FLabel::draw()
if ( hotkeypos != -1 ) if ( hotkeypos != -1 )
{ {
xoffset = getXOffset (int(length-1)); align_offset = getAlignOffset (int(length-1));
printLine (LabelText, length-1, hotkeypos, xoffset); printLine (LabelText, length-1, hotkeypos, align_offset);
hotkey_printed = true; hotkey_printed = true;
hotkeypos = -1; hotkeypos = -1;
} }
else else
{ {
xoffset = getXOffset (int(length)); align_offset = getAlignOffset (int(length));
printLine (LabelText, length, -1, xoffset); printLine (LabelText, length, -1, align_offset);
} }
y++; y++;
@ -509,8 +508,8 @@ void FLabel::draw()
length--; length--;
setPrintPos (1,1); setPrintPos (1,1);
xoffset = getXOffset (int(length)); align_offset = getAlignOffset (int(length));
printLine (LabelText, length, hotkeypos, xoffset); printLine (LabelText, length, hotkeypos, align_offset);
delete[] LabelText; delete[] LabelText;
} }

View File

@ -99,7 +99,7 @@ class FLabel : public FWidget
uChar getHotkey(); uChar getHotkey();
int getHotkeyPos (wchar_t*&, wchar_t*&, uInt); int getHotkeyPos (wchar_t*&, wchar_t*&, uInt);
void setHotkeyAccelerator(); void setHotkeyAccelerator();
int getXOffset (int); int getAlignOffset (int);
void printLine (wchar_t*&, uInt, int, int = 0); void printLine (wchar_t*&, uInt, int, int = 0);
void draw(); void draw();

View File

@ -1351,7 +1351,7 @@ void FListBox::drawList()
isFocus = ((flags & fc::focus) != 0); isFocus = ((flags & fc::focus) != 0);
start = 0; start = 0;
end = uInt(getHeight()-2); end = uInt(getHeight() - 2);
inc_len = inc_search.getLength(); inc_len = inc_search.getLength();
if ( end > getCount() ) if ( end > getCount() )

View File

@ -19,7 +19,6 @@ FListViewItem::FListViewItem (const FListViewItem& item)
: FObject(item.getParent()) : FObject(item.getParent())
, column_line(item.column_line) , column_line(item.column_line)
, data_pointer(item.data_pointer) , data_pointer(item.data_pointer)
, alignment(fc::alignLeft)
{ {
FObject* parent = getParent(); FObject* parent = getParent();
@ -27,13 +26,27 @@ FListViewItem::FListViewItem (const FListViewItem& item)
static_cast<FListView*>(parent)->insert (this); static_cast<FListView*>(parent)->insert (this);
} }
//----------------------------------------------------------------------
FListViewItem::FListViewItem (FListViewItem* item)
: FObject(item->getParent())
, column_line(item->column_line)
, data_pointer(item->data_pointer)
{
// Add the FListViewItem to the parent
FObject* parent = getParent();
if ( parent && parent->isInstanceOf("FListView") )
static_cast<FListView*>(parent)->insert (this);
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
FListViewItem::FListViewItem (FListView* parent) FListViewItem::FListViewItem (FListView* parent)
: FObject(parent) : FObject(parent)
, column_line() , column_line()
, data_pointer(0) , data_pointer(0)
, alignment(fc::alignLeft)
{ {
// Add the FListViewItem to the parent
if ( parent )
parent->insert (this); parent->insert (this);
} }
@ -44,8 +57,18 @@ FListViewItem::FListViewItem ( const std::vector<FString>& cols
: FObject(parent) : FObject(parent)
, column_line(cols) , column_line(cols)
, data_pointer(data) , data_pointer(data)
, alignment(fc::alignLeft)
{ {
// Replace the control codes characters
std::vector<FString>::iterator iter = column_line.begin();
while ( iter != column_line.end() )
{
*iter = iter->replaceControlCodes();
++iter;
}
// Add the FListViewItem to the parent
if ( parent )
parent->insert (this); parent->insert (this);
} }
@ -90,22 +113,14 @@ FListView::~FListView() // destructor
// public methods of FListView // public methods of FListView
//---------------------------------------------------------------------- //----------------------------------------------------------------------
int FListView::addColumn (const FString& label, int width) fc::text_alignment FListView::getColumnAlignment (int column)
{ {
Header column; // Get the alignment for a column
column.name = label;
column.width = width;
if ( column.width == USE_MAX_SIZE ) if ( column < 0 || header.empty() || column > int(header.size()) )
{ return fc::alignLeft;
column.fixed_width = false;
column.width = int(label.getLength());
}
else
column.fixed_width = true;
header.push_back (column); return header[uInt(column)].alignment;
return int(std::distance(header.begin(), header.end()));
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -125,6 +140,36 @@ void FListView::setGeometry (int x, int y, int w, int h, bool adjust)
} }
} }
//----------------------------------------------------------------------
void FListView::setColumnAlignment (int column, fc::text_alignment align)
{
// Set the alignment for a column
if ( column < 0 || header.empty() || column > int(header.size()) )
return;
header[uInt(column)].alignment = align;
}
//----------------------------------------------------------------------
int FListView::addColumn (const FString& label, int width)
{
Header column;
column.name = label;
column.width = width;
if ( column.width == USE_MAX_SIZE )
{
column.fixed_width = false;
column.width = int(label.getLength());
}
else
column.fixed_width = true;
header.push_back (column);
return int(std::distance(header.begin(), header.end()));
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FListView::insert (FListViewItem* item) void FListView::insert (FListViewItem* item)
{ {
@ -796,6 +841,32 @@ void FListView::init()
setRightPadding(1 + nf_offset); setRightPadding(1 + nf_offset);
} }
//----------------------------------------------------------------------
uInt FListView::getAlignOffset ( fc::text_alignment align
, uInt txt_length
, uInt width )
{
switch ( align )
{
case fc::alignLeft:
return 0;
case fc::alignCenter:
if ( txt_length < width )
return uInt((width - txt_length) / 2);
else
return 0;
case fc::alignRight:
if ( txt_length < width )
return width - txt_length;
else
return 0;
}
return 0;
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FListView::draw() void FListView::draw()
{ {
@ -898,19 +969,14 @@ void FListView::drawColumnLabels()
headerline.write (txt); headerline.write (txt);
if ( txt_length < uInt(column_width) ) if ( txt_length < uInt(column_width) )
headerline.write (' '); headerline.write (' '); // tailing space
if ( txt_length + tailing_space < uInt(column_width) ) if ( txt_length + tailing_space < uInt(column_width) )
{ {
setColor(); setColor();
FString line ( column_width - int(txt_length) - tailing_space FString line ( uInt(column_width) - tailing_space - txt_length
, wchar_t(fc::BoxDrawingsHorizontal) ); , wchar_t(fc::BoxDrawingsHorizontal) );
headerline.write (line); headerline.write (line); // horizontal line
}
else if ( txt_length + tailing_space == uInt(column_width) )
{
setColor();
headerline.write (wchar_t(fc::BoxDrawingsHorizontal));
} }
} }
else else
@ -930,10 +996,10 @@ void FListView::drawColumnLabels()
const std::vector<char_data>& h = headerline.getBuffer(); const std::vector<char_data>& h = headerline.getBuffer();
first = h.begin() + xoffset; first = h.begin() + xoffset;
if ( int(h.size()) <= getWidth() - 2 ) if ( int(h.size()) <= getClientWidth() )
last = h.end() - 1; last = h.end() - 1;
else else
last = h.begin() + getWidth() + xoffset - 2; last = h.begin() + getClientWidth() + xoffset - 1;
const std::vector<char_data> header_part (first, last); const std::vector<char_data> header_part (first, last);
setPrintPos (2, 1); setPrintPos (2, 1);
@ -952,7 +1018,7 @@ void FListView::drawList()
isFocus = ((flags & fc::focus) != 0); isFocus = ((flags & fc::focus) != 0);
start = 0; start = 0;
end = uInt(getHeight()-2); end = uInt(getHeight() - 2);
if ( end > data.size() ) if ( end > data.size() )
end = uInt(data.size()); end = uInt(data.size());
@ -1007,11 +1073,22 @@ void FListView::drawList()
FString text = (*iter)->column_line[i]; FString text = (*iter)->column_line[i];
int width = header[i].width; int width = header[i].width;
uInt txt_length = text.getLength(); uInt txt_length = text.getLength();
fc::text_alignment align = getColumnAlignment(int(i+1));
uInt align_offset = getAlignOffset (align, txt_length, uInt(width));
if ( txt_length <= uInt(width) ) if ( align_offset > 0 )
line += FString(align_offset, ' ');
if ( align_offset + txt_length <= uInt(width) )
{ {
line += text.left(width); line += text.left(width);
line += FString (leading_space + width - int(txt_length), ' '); line += FString (leading_space + width - int(align_offset + txt_length), ' ');
}
else if ( align == fc::alignRight )
{
line += FString ("..");
line += text.right(width - ellipsis_length);
line += ' ';
} }
else else
{ {

View File

@ -49,6 +49,7 @@ class FListViewItem : public FObject
{ {
public: public:
FListViewItem (const FListViewItem&); // copy constructor FListViewItem (const FListViewItem&); // copy constructor
FListViewItem (FListViewItem*);
FListViewItem (FListView*); FListViewItem (FListView*);
FListViewItem ( const std::vector<FString>& FListViewItem ( const std::vector<FString>&
, FWidget::data_ptr = 0 , FWidget::data_ptr = 0
@ -71,7 +72,6 @@ class FListViewItem : public FObject
// Data Member // Data Member
std::vector<FString> column_line; std::vector<FString> column_line;
FWidget::data_ptr data_pointer; FWidget::data_ptr data_pointer;
fc::text_alignment alignment;
}; };
#pragma pack(pop) #pragma pack(pop)
@ -110,9 +110,11 @@ class FListView : public FWidget
// Accessors // Accessors
const char* getClassName() const; const char* getClassName() const;
fc::text_alignment getColumnAlignment (int);
// Mutators // Mutators
void setGeometry (int, int, int, int, bool = true); void setGeometry (int, int, int, int, bool = true);
void setColumnAlignment (int, fc::text_alignment);
// Methods // Methods
virtual int addColumn (const FString&, int = USE_MAX_SIZE); virtual int addColumn (const FString&, int = USE_MAX_SIZE);
@ -149,6 +151,7 @@ class FListView : public FWidget
: name() : name()
, width (0) , width (0)
, fixed_width (-1) , fixed_width (-1)
, alignment (fc::alignLeft)
{ } { }
~Header() ~Header()
@ -157,6 +160,7 @@ class FListView : public FWidget
FString name; FString name;
int width; int width;
bool fixed_width; bool fixed_width;
fc::text_alignment alignment;
}; };
typedef std::vector<Header> headerItems; typedef std::vector<Header> headerItems;
@ -172,6 +176,7 @@ class FListView : public FWidget
// Methods // Methods
void init(); void init();
uInt getAlignOffset (fc::text_alignment, uInt, uInt);
void draw(); void draw();
void drawColumnLabels(); void drawColumnLabels();
void drawList(); void drawList();

View File

@ -170,6 +170,9 @@ FString::FString (const char* s)
, bufsize(0) , bufsize(0)
, c_string(0) , c_string(0)
{ {
if ( ! s )
return;
const wchar_t* wc_string; const wchar_t* wc_string;
wc_string = c_to_wc_str(s); wc_string = c_to_wc_str(s);
@ -187,6 +190,9 @@ FString::FString (const wchar_t c)
, bufsize(0) , bufsize(0)
, c_string(0) , c_string(0)
{ {
if ( c == 0 )
return;
wchar_t s[2]; wchar_t s[2];
s[0] = c; s[0] = c;
s[1] = L'\0'; s[1] = L'\0';
@ -200,6 +206,9 @@ FString::FString (const char c)
, bufsize(0) , bufsize(0)
, c_string(0) , c_string(0)
{ {
if ( c == 0 )
return;
wchar_t s[2]; wchar_t s[2];
s[0] = wchar_t(c & 0xff); s[0] = wchar_t(c & 0xff);
s[1] = L'\0'; s[1] = L'\0';

View File

@ -49,9 +49,14 @@ Listview::Listview (FWidget* parent)
// Add columns to the view // Add columns to the view
listView->addColumn ("City"); listView->addColumn ("City");
listView->addColumn ("Condition"); listView->addColumn ("Condition");
listView->addColumn ("Temp.", 7); listView->addColumn ("Temp.");
listView->addColumn ("Humidity"); listView->addColumn ("Humidity");
listView->addColumn ("Pressure"); listView->addColumn ("Pressure", 10);
// Set right alignment for the third, fourth, and fifth column
listView->setColumnAlignment (3, fc::alignRight);
listView->setColumnAlignment (4, fc::alignRight);
listView->setColumnAlignment (5, fc::alignRight);
// Populate FListView with a list of items // Populate FListView with a list of items
std::string weather[][5] = std::string weather[][5] =