First implementation of a tree view in the FListView class

This commit is contained in:
Markus Gans 2017-10-14 22:20:19 +02:00
parent 1543d042cb
commit 2e506a1367
5 changed files with 1020 additions and 372 deletions

View File

@ -1,5 +1,8 @@
2017-10-02 Markus Gans <guru.mail@muenster.de> 2017-10-14 Markus Gans <guru.mail@muenster.de>
* Fixed a bug in the FObject check of parent objects * Fixed a bug in the FObject check of parent objects
* Replace the deprecated readdir_r function (CVE-2013-4237)
* First implementation of a tree view in the FListView
class (alpha state)
2017-10-02 Markus Gans <guru.mail@muenster.de> 2017-10-02 Markus Gans <guru.mail@muenster.de>
* DECSCUSR - Set Cursor Style support for VTE >= 0.40.0 and * DECSCUSR - Set Cursor Style support for VTE >= 0.40.0 and

View File

@ -1,4 +1,23 @@
// File: treeview.cpp /************************************************************************
* treeview.cpp - Example of a FListView widget with a tree hierarchy *
* *
* This file is part of the Final Cut widget toolkit *
* *
* Copyright 2017 Markus Gans *
* *
* The Final Cut 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 3 of the License, or *
* (at your option) any later version. *
* *
* The Final Cut 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, see <http://www.gnu.org/licenses/>. *
************************************************************************/
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@ -27,29 +46,64 @@ class Treeview : public FDialog
~Treeview(); ~Treeview();
private: private:
// Typedefs
struct TreeItem; // forward declaration
// Disable copy constructor // Disable copy constructor
Treeview (const Treeview&); Treeview (const Treeview&);
// Disable assignment operator (=) // Disable assignment operator (=)
Treeview& operator = (const Treeview&); Treeview& operator = (const Treeview&);
// Method
void adjustSize();
// Event handlers // Event handlers
void onClose (FCloseEvent*); void onClose (FCloseEvent*);
// Callback methods // Callback methods
void cb_exitApp (FWidget*, data_ptr); void cb_exitApp (FWidget*, data_ptr);
// Data Members
FListView* listView;
FButton* Quit;
};
#pragma pack(pop)
//----------------------------------------------------------------------
// struct Treeview::TreeItem
//----------------------------------------------------------------------
#pragma pack(push)
#pragma pack(1)
struct Treeview::TreeItem
{
const char* const* begin() const
{ return &name; }
const char* const* end() const
{ return &density + 1; }
// Data Members
const char* name;
const char* population;
const char* density;
TreeItem* child_element;
}; };
#pragma pack(pop) #pragma pack(pop)
//---------------------------------------------------------------------- //----------------------------------------------------------------------
Treeview::Treeview (FWidget* parent) Treeview::Treeview (FWidget* parent)
: FDialog(parent) : FDialog(parent)
, listView()
, Quit()
{ {
// Create FListView object // Create FListView object
FListView* listView = new FListView (this); listView = new FListView (this);
listView->setGeometry(2, 1, 33, 14); listView->setGeometry(2, 1, 53, 14);
// Add columns to the view // Add columns to the view
listView->addColumn ("Name"); listView->addColumn ("Name", 23);
listView->addColumn ("Population"); listView->addColumn ("Population");
listView->addColumn ("Density/km²"); listView->addColumn ("Density/km²");
@ -61,40 +115,165 @@ Treeview::Treeview (FWidget* parent)
listView->setTreeView(); listView->setTreeView();
// Populate FListView with a list of items // Populate FListView with a list of items
std::string continent[][3] = static TreeItem africa[] =
{ {
{ "Africa", "944,000,000", "31.2" }, { "Algeria", "40,400,000", "15.9", 0 },
{ "Asia", "4,010,000,000", "90.3" }, { "Angola", "25,789,024", "20.69", 0 },
{ "Europe", "733,000,000", "69.9" }, { "Botswana", "2,250,260", "3.7", 0 },
{ "North America", "523,000,000", "21" }, { "Cameroon", "22,534,532", "39.7", 0 },
{ "South America", "381,000,000", "21.4" }, { "Chad", "13,670,084", "8.6", 0 },
{ "Antarctica", "1000", "0" }, { "Egypt", "94,666,000", "87", 0 },
{ "Australia/Oceania", "34,000,000", "4" } { "Ethiopia", "102,374,044", "92.7", 0 },
{ "Ivory Coast", "23,740,424", "63.9", 0 },
{ "Libya", "6,541,948", "3.55", 0 },
{ "Madagascar", "24,430,325", "35.2", 0 },
{ "Mali", "14,517,176", "11.7", 0 },
{ "Mauritania", "4,301,018", "3.4", 0 },
{ "Mozambique", "24,692,144", "28.7", 0 },
{ "Namibia", "2,113,077", "2.54", 0 },
{ "Niger", "20,672,987", "12.1", 0 },
{ "Nigeria", "185,989,640", "197.2", 0 },
{ "Somalia", "14,317,996", "19.31", 0 },
{ "South Africa", "54,956,900", "42.4", 0 },
{ "South Sudan", "12,340,000", "13.33", 0 },
{ "Sudan", "39,578,828", "21.3", 0 },
{ "Tanzania", "51,820,00", "47.5", 0 },
{ "Zambia", "16,212,000", "17.2", 0 },
{ 0, 0, 0, 0 }
}; };
const int lastItem = int(sizeof(continent) / sizeof(continent[0])) - 1; static TreeItem asia[] =
for (int i = 0; i <= lastItem; i++)
{ {
FStringList line (&continent[i][0], &continent[i][0] + 3); { "Afghanistan", "34,656,032", "49.88", 0 },
listView->insert (line); { "China", "1,403,500,365", "145.0", 0 },
{ "India", "1,324,171,354", "393.9", 0 },
{ "Indonesia", "261,115,456", "124.66", 0 },
{ "Iran", "80,829,192", "48.0", 0 },
{ "Iraq", "37,202,572", "82.7", 0 },
{ "Japan", "126,740,000", "336.0", 0 },
{ "Kazakhstan", "17,987,736", "6.49", 0 },
{ "Mongolia", "3,081,677", "1.97", 0 },
{ "Myanmar", "51,486,253", "76.0", 0 },
{ "Pakistan", "207,774,520", "244.4", 0 },
{ "Russia", "144,463,451", "8.4", 0 },
{ "Saudi Arabia", "33,000,000", "15.0", 0 },
{ "Thailand", "68,863,514", "132.1", 0 },
{ "Turkey", "79,814,871", "102.0", 0 },
{ "Turkmenistan", "5,662,544", "10.5", 0 },
{ "Uzbekistan", "32,979,000", "70.5", 0 },
{ "Vietnam", "94,569,072", "276.03", 0 },
{ "Yemen", "27,584,213", "44.7", 0 },
{ 0, 0, 0, 0 }
};
static TreeItem europe[] =
{
{ "Austria", "8,794,267", "104.0", 0 },
{ "Belarus", "9,498,700", "45.8", 0 },
{ "Bulgaria", "7,101,859", "64.9", 0 },
{ "Czech Republic", "10,610,947", "134.0", 0 },
{ "Finland", "5,506,312", "16.0", 0 },
{ "France", "66,991,000", "103.0", 0 },
{ "Germany", "82,175,700", "227.0", 0 },
{ "Greece", "11,183,716", "82.0", 0 },
{ "Hungary", "9,797,561", "105.3", 0 },
{ "Iceland", "332,529", "3.2", 0 },
{ "Italy", "60,589,445", "201.3", 0 },
{ "Norway", "5,267,146", "15.8", 0 },
{ "Poland", "38,634,007", "123.0", 0 },
{ "Portugal", "10,309,573", "115.0", 0 },
{ "Romania", "19,638,000", "84.4", 0 },
{ "Serbia", "7,058,322", "91.1", 0 },
{ "Spain", "46,468,102", "92.0", 0 },
{ "Sweden", "10,065,389", "22.0", 0 },
{ "United Kingdom", "65,648,000", "270.7", 0 },
{ 0, 0, 0, 0 }
};
static TreeItem north_america[] =
{
{ "Canada", "35,151,728", "3.92", 0 },
{ "Cuba", "11,239,224", "102.3", 0 },
{ "Greenland", "56,483", "0.028", 0 },
{ "Guatemala", "16,582,469", "129.0", 0 },
{ "Honduras", "9,112,867", "64.0", 0 },
{ "Mexico", "119,530,753", "61.0", 0 },
{ "Nicaragua", "6,167,237", "51.0", 0 },
{ "USA", "325,365,189", "35.0", 0 },
{ 0, 0, 0, 0 }
};
static TreeItem south_america[] =
{
{ "Argentina", "43,847,430", "14.4", 0 },
{ "Bolivia", "11,410,651", "10.4", 0 },
{ "Brazil", "208,064,000", "24.35", 0 },
{ "Chile", "18,006,407", "24.0", 0 },
{ "Colombia", "49,364,592", "40.74", 0 },
{ "Ecuador", "16,385,068", "58.95", 0 },
{ "Guyana", "773,303", "3.502", 0 },
{ "Paraguay", "6,725,308", "17.2", 0 },
{ "Peru", "31,826,018", "23.0", 0 },
{ "Venezuela", "31,568,179", "33.75", 0 },
{ 0, 0, 0, 0 }
};
static TreeItem oceania[] =
{
{ "Australia", "24,675,900", "3.2", 0 },
{ "Papua New Guinea", "7,059,653", "15.0", 0 },
{ "Papua", "3,486,432", "11.0", 0 },
{ "New Zealand", "4,823,090", "17.5", 0 },
{ "West Papua", "877,437", "6.3", 0 },
{ "Solomon Islands", "599,419", "18.1", 0 },
{ "New Caledonia", "268,767", "14.5", 0 },
{ "Fiji", "898,76", "46.4", 0 },
{ "Hawaii", "1,428,557", "82.6", 0 },
{ "Vanuatu", "270,402", "19.7", 0 },
{ "French Polynesia", "280,208", "76.0", 0 },
{ "Samoa", "192,342", "68.0", 0 },
{ "Kiribati", "110,136", "152.0", 0 },
{ 0, 0, 0, 0 }
};
static TreeItem continent[] =
{
{ "Africa", "944,000,000", "31.2", africa },
{ "Asia", "4,010,000,000", "90.3", asia },
{ "Europe", "733,000,000", "69.9", europe },
{ "North America", "523,000,000", "21", north_america },
{ "South America", "381,000,000", "21.4", south_america },
{ "Antarctica", "1000", "0", 0 },
{ "Australia/Oceania", "34,000,000", "4", oceania },
{ 0, 0, 0, 0 }
};
TreeItem* continent_list = continent;
while ( continent_list->name )
{
TreeItem* country_list = continent_list->child_element;
FStringList continent_line (continent_list->begin(), continent_list->end());
FObjectIterator iter = listView->insert (continent_line);
while ( country_list && country_list->name )
{
FStringList country_line (country_list->begin(), country_list->end());
listView->insert (country_line, iter);
country_list++;
}
continent_list++;
} }
FObjectIterator iter = listView->beginOfList();
FObjectIterator iter_africa = listView->beginOfList(); FObjectIterator iter_africa = iter++;
std::string egypt[3] = { "Egypt", "94,666,000", "87" };
FStringList egypt_line (&egypt[0], &egypt[0] + 3);
//FObjectIterator iter_africa = listView->insert (egypt_line);
listView->insert (egypt_line, iter_africa);
FListViewItem* item_africa = static_cast<FListViewItem*>(*iter_africa); FListViewItem* item_africa = static_cast<FListViewItem*>(*iter_africa);
FObjectIterator iter_egypt = item_africa->begin(); item_africa->expand();
FListViewItem* item_egypt = static_cast<FListViewItem*>(*iter_egypt);
item_egypt = item_egypt;
item_africa->expand();
// Quit button // Quit button
FButton* Quit = new FButton (this); Quit = new FButton (this);
Quit->setGeometry(24, 16, 10, 1); Quit->setGeometry(24, 16, 10, 1);
Quit->setText (L"&Quit"); Quit->setText (L"&Quit");
@ -110,6 +289,27 @@ item_africa->expand();
Treeview::~Treeview() // destructor Treeview::~Treeview() // destructor
{ } { }
//----------------------------------------------------------------------
void Treeview::adjustSize()
{
int h = getParentWidget()->getHeight() - 4;
setHeight (h, false);
int X = int((getParentWidget()->getWidth() - getWidth()) / 2);
if ( X < 1 )
X = 1;
setX (X, false);
if ( listView )
listView->setHeight (getHeight() - 6, false);
if ( Quit )
Quit->setY(getHeight() - 4);
FDialog::adjustSize();
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void Treeview::onClose (FCloseEvent* ev) void Treeview::onClose (FCloseEvent* ev)
{ {
@ -142,7 +342,7 @@ int main (int argc, char* argv[])
// Create main dialog object // Create main dialog object
Treeview d(&app); Treeview d(&app);
d.setText (L"Continents"); d.setText (L"Continents");
d.setGeometry (int(1 + (app.getWidth() - 37) / 2), 3, 37, 20); d.setGeometry (int(1 + (app.getWidth() - 57) / 2), 3, 57, 20);
d.setShadow(); d.setShadow();
// Set dialog d as main widget // Set dialog d as main widget

View File

@ -1,31 +1,48 @@
// File: flistview.h /************************************************************************
// Provides: class FListViewItem * flistview.h - Widget FListView and FListViewItem *
// class FListView * *
// * This file is part of the Final Cut widget toolkit *
// Inheritance diagram * *
// ═══════════════════ * Copyright 2017 Markus Gans *
// * *
// ▕▔▔▔▔▔▔▔▔▔▏ ▕▔▔▔▔▔▔▔▔▔▏ * The Final Cut is free software; you can redistribute it and/or modify *
// ▕ FObject ▏ ▕ FTerm ▏ * it under the terms of the GNU General Public License as published by *
// ▕▁▁▁▁▁▁▁▁▁▏ ▕▁▁▁▁▁▁▁▁▁▏ * the Free Software Foundation; either version 3 of the License, or *
// ▲ ▲ * (at your option) any later version. *
// │ │ * *
// └─────┬─────┘ * The Final Cut 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 *
// ▕ FVTerm ▏ * 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, see <http://www.gnu.org/licenses/>. *
// ▕▔▔▔▔▔▔▔▔▔▏ ▕▔▔▔▔▔▔▔▔▔▏ ************************************************************************/
// ▕ FWidget ▏ ▕ FObject ▏
// ▕▁▁▁▁▁▁▁▁▁▏ ▕▁▁▁▁▁▁▁▁▁▏ /* Inheritance diagram
// ▲ ▲ *
// │ │ *
// ▕▔▔▔▔▔▔▔▔▔▔▔▏1 *▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏ *
// ▕ FListView ▏- - - -▕ FListViewItem ▏ * FObject FTerm
// ▕▁▁▁▁▁▁▁▁▁▁▁▏ ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏ *
// *
*
*
*
*
* FVTerm
*
*
*
*
* FWidget FObject
*
*
*
* 1 *
* FListView - - - - FListViewItem
*
*/
#ifndef FLISTVIEW_H #ifndef FLISTVIEW_H
#define FLISTVIEW_H #define FLISTVIEW_H
@ -90,6 +107,7 @@ class FListViewItem : public FObject
FObjectIterator appendItem (FListViewItem*); FObjectIterator appendItem (FListViewItem*);
void replaceControlCodes(); void replaceControlCodes();
int getVisibleLines(); int getVisibleLines();
void resetVisibleLineCounter();
// Data Member // Data Member
FStringList column_list; FStringList column_list;
@ -100,6 +118,7 @@ class FListViewItem : public FObject
// Friend class // Friend class
friend class FListView; friend class FListView;
friend class FListViewIterator;
}; };
#pragma pack(pop) #pragma pack(pop)
@ -122,6 +141,86 @@ inline bool FListViewItem::isExpandable()
{ return expandable; } { return expandable; }
//----------------------------------------------------------------------
// class FListViewIterator
//----------------------------------------------------------------------
#pragma pack(push)
#pragma pack(1)
class FListViewIterator
{
public:
// Typedefs
typedef std::list<FObject*> FObjectList;
typedef FObjectList::iterator FObjectIterator;
typedef std::stack<FObjectIterator> FObjectIteratorStack;
// Constructor
FListViewIterator ();
FListViewIterator (FObjectIterator);
// Destructor
~FListViewIterator();
// Overloaded operators
FListViewIterator& operator ++ (); // prefix
FListViewIterator operator ++ (int); // postfix
FListViewIterator& operator -- (); // prefix
FListViewIterator operator -- (int); // postfix
FListViewIterator& operator += (int);
FListViewIterator& operator -= (int);
FObject*& operator * () const;
FObject* operator -> () const;
bool operator == (const FListViewIterator&) const;
bool operator != (const FListViewIterator&) const;
// Accessor
const char* getClassName() const;
int getPosition() const;
// Methods
void parentElement();
private:
// Methods
void nextElement (FObjectIterator&);
void prevElement (FObjectIterator&);
// Data Members
FObjectIteratorStack iter_path;
FObjectIterator node;
int position;
};
#pragma pack(pop)
// FListViewIterator inline functions
//----------------------------------------------------------------------
inline FObject*& FListViewIterator::operator * () const
{ return *node; }
//----------------------------------------------------------------------
inline FObject* FListViewIterator::operator -> () const
{ return *node; }
//----------------------------------------------------------------------
inline bool FListViewIterator::operator == (const FListViewIterator& rhs) const
{ return node == rhs.node; }
//----------------------------------------------------------------------
inline bool FListViewIterator::operator != (const FListViewIterator& rhs) const
{ return node != rhs.node; }
//----------------------------------------------------------------------
inline const char* FListViewIterator::getClassName() const
{ return "FListViewIterator"; }
//----------------------------------------------------------------------
inline int FListViewIterator::getPosition() const
{ return position; }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// class FListView // class FListView
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -143,7 +242,7 @@ class FListView : public FWidget
// Accessors // Accessors
const char* getClassName() const; const char* getClassName() const;
uInt getCount() const; uInt getCount();
fc::text_alignment getColumnAlignment (int) const; fc::text_alignment getColumnAlignment (int) const;
FString getColumnText (int) const; FString getColumnText (int) const;
FListViewItem* getCurrentItem(); FListViewItem* getCurrentItem();
@ -190,32 +289,13 @@ class FListView : public FWidget
protected: protected:
// Methods // Methods
void adjustYOffset(); void adjustViewport();
void adjustSize(); void adjustSize();
private: private:
// Typedef // Typedef
struct Header struct Header; // forward declaration
{
public:
Header()
: name()
, width (0)
, fixed_width (false)
, alignment (fc::alignLeft)
{ }
~Header()
{ }
FString name;
int width;
bool fixed_width;
fc::text_alignment alignment;
};
typedef std::vector<Header> headerItems; typedef std::vector<Header> headerItems;
typedef std::stack<FObjectIterator> FObjectIteratorStack;
// Constants // Constants
static const int USE_MAX_SIZE = -1; static const int USE_MAX_SIZE = -1;
@ -238,8 +318,14 @@ class FListView : public FWidget
FObjectIterator appendItem (FListViewItem*); FObjectIterator appendItem (FListViewItem*);
void processClick(); void processClick();
void processChanged(); void processChanged();
FObjectIterator index2iterator (int); void stepForward();
void nextElement (FObjectIterator&); void stepBackward();
void stepForward (int);
void stepBackward (int);
void scrollToX (int);
void scrollToY (int);
void scrollTo (const FPoint &);
void scrollTo (int, int);
// Callback methods // Callback methods
void cb_VBarChange (FWidget*, data_ptr); void cb_VBarChange (FWidget*, data_ptr);
@ -249,7 +335,9 @@ class FListView : public FWidget
FObjectIterator root; FObjectIterator root;
FObjectList selflist; FObjectList selflist;
FObjectList itemlist; FObjectList itemlist;
FObjectIteratorStack iter_path; FListViewIterator current_iter;
FListViewIterator first_visible_line;
FListViewIterator last_visible_line;
headerItems header; headerItems header;
FTermBuffer headerline; FTermBuffer headerline;
FScrollbar* vbar; FScrollbar* vbar;
@ -259,9 +347,7 @@ class FListView : public FWidget
int scroll_distance; int scroll_distance;
bool scroll_timer; bool scroll_timer;
bool tree_view; bool tree_view;
int current;
int xoffset; int xoffset;
int yoffset;
int nf_offset; int nf_offset;
int max_line_width; int max_line_width;
@ -271,22 +357,41 @@ class FListView : public FWidget
#pragma pack(pop) #pragma pack(pop)
//----------------------------------------------------------------------
// struct FListView::Header
//----------------------------------------------------------------------
#pragma pack(push)
#pragma pack(1)
struct FListView::Header
{
public:
Header()
: name()
, width (0)
, fixed_width (false)
, alignment (fc::alignLeft)
{ }
~Header()
{ }
FString name;
int width;
bool fixed_width;
fc::text_alignment alignment;
};
#pragma pack(pop)
// FListView inline functions // FListView inline functions
//---------------------------------------------------------------------- //----------------------------------------------------------------------
inline const char* FListView::getClassName() const inline const char* FListView::getClassName() const
{ return "FListView"; } { return "FListView"; }
//----------------------------------------------------------------------
inline uInt FListView::getCount() const
{ return uInt(itemlist.size()); }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
inline FListViewItem* FListView::getCurrentItem() inline FListViewItem* FListView::getCurrentItem()
{ { return static_cast<FListViewItem*>(*current_iter); }
FObjectIterator iter = itemlist.begin();
std::advance (iter, current - 1);
return static_cast<FListViewItem*>(*iter);
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
inline bool FListView::setTreeView (bool on) inline bool FListView::setTreeView (bool on)
@ -335,11 +440,7 @@ inline FObject::FObjectIterator FListView::endOfList()
{ return itemlist.end(); } { return itemlist.end(); }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
inline FObject::FObjectIterator FListView::index2iterator (int index) inline void FListView::scrollTo (const FPoint& pos)
{ { scrollTo(pos.getX(), pos.getY()); }
FObjectIterator iter = itemlist.begin();
std::advance (iter, index);
return iter;
}
#endif // FLISTVIEW_H #endif // FLISTVIEW_H

View File

@ -1212,7 +1212,7 @@ void FListBox::adjustYOffset()
int element_count = int(getCount()); int element_count = int(getCount());
if ( getClientHeight() < 0 ) if ( getClientHeight() < 0 )
beep(); return;
if ( element_count == 0 || getClientHeight() <= 0 ) if ( element_count == 0 || getClientHeight() <= 0 )
return; return;

File diff suppressed because it is too large Load Diff