Skip to content

Commit 825b2aa

Browse files
Rewrite ListBox widget
1 parent 372d8cb commit 825b2aa

File tree

8 files changed

+862
-331
lines changed

8 files changed

+862
-331
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ set(
116116
"${SOURCE_PATH}/SFGUI/Engines/BREW/Frame.cpp"
117117
"${SOURCE_PATH}/SFGUI/Engines/BREW/Image.cpp"
118118
"${SOURCE_PATH}/SFGUI/Engines/BREW/Label.cpp"
119+
"${SOURCE_PATH}/SFGUI/Engines/BREW/ListBox.cpp"
119120
"${SOURCE_PATH}/SFGUI/Engines/BREW/Notebook.cpp"
120121
"${SOURCE_PATH}/SFGUI/Engines/BREW/ProgressBar.cpp"
121122
"${SOURCE_PATH}/SFGUI/Engines/BREW/Scale.cpp"
@@ -269,7 +270,7 @@ elseif( APPLE )
269270
elseif( "${CMAKE_SYSTEM_NAME}" MATCHES "Linux" )
270271
target_link_libraries( sfgui ${SFML_LIBRARIES} ${SFML_DEPENDENCIES} ${OPENGL_gl_LIBRARY} ${X11_LIBRARIES} )
271272
set( SHARE_PATH "${CMAKE_INSTALL_PREFIX}/share/SFGUI" )
272-
273+
273274
if( LIB_SUFFIX )
274275
set( LIB_PATH "lib${LIB_SUFFIX}" )
275276
else()

examples/ListBox.cpp

Lines changed: 121 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,90 +2,129 @@
22
// Including SFGUI/Widgets.hpp includes everything
33
// you can possibly need automatically.
44
#include <SFGUI/SFGUI.hpp>
5-
#include <SFGUI/Widgets.hpp>
6-
5+
#include <SFGUI/Widgets.hpp>
6+
77
#include <SFML/Graphics.hpp>
88

99
int main() {
10-
sfg::SFGUI sfgui;
11-
sf::RenderWindow window(sf::VideoMode(640, 480), "ListBox Example");
12-
window.setVerticalSyncEnabled(true);
13-
window.setFramerateLimit(30);
14-
15-
sfg::Desktop desktop;
16-
17-
auto sfg_window = sfg::Window::Create();
18-
sfg_window->SetTitle( "ListBoxExample" );
19-
20-
auto box_outer = sfg::Box::Create( sfg::Box::Orientation::HORIZONTAL, 15.0f );
21-
auto box_inner1 = sfg::Box::Create( sfg::Box::Orientation::VERTICAL, 5.0f );
22-
auto box_inner2 = sfg::Box::Create( sfg::Box::Orientation::VERTICAL, 5.0f );
23-
24-
auto label1 = sfg::Label::Create("I'm single-select list.");
25-
auto label2 = sfg::Label::Create("I'm multi-select list.");
26-
27-
auto input1 = sfg::Entry::Create("");
28-
auto input2 = sfg::Entry::Create("");
29-
30-
auto list1 = sfg::ListBox::Create();
31-
list1->AppendItem( "Item1" );
32-
list1->AppendItem( "Item2" );
33-
list1->AppendItem( "Item3" );
34-
list1->GetSignal( sfg::ListBox::OnSelect ).Connect( std::bind( [list1, input1](){ if(list1->GetSelectedItemsCount()) input1->SetText(list1->GetSelectedItemText()); else input1->SetText(""); } ) );
35-
36-
auto list2 = sfg::ListBox::Create();
37-
list2->AppendItem( "Item1" );
38-
list2->AppendItem( "Item2" );
39-
list2->AppendItem( "Item3" );
40-
list2->multiselect = true;
41-
list2->GetSignal( sfg::ListBox::OnSelect ).Connect( std::bind( [list2, input2](){
42-
std::string str = "";
43-
for(unsigned i=0; i<list2->GetSelectedItemsCount(); i++)
44-
{
45-
str += list2->GetSelectedItemText(i);
46-
str += ' ';
47-
}
48-
input2->SetText(str);
49-
} ) );
50-
51-
box_outer->Pack(box_inner1);
52-
box_outer->Pack(box_inner2);
53-
54-
box_inner1->Pack(label1);
55-
box_inner1->Pack(list1);
56-
box_inner1->Pack(input1);
57-
58-
box_inner2->Pack(label2);
59-
box_inner2->Pack(list2);
60-
box_inner2->Pack(input2);
61-
62-
sfg_window->Add( box_outer );
63-
desktop.Add( sfg_window );
64-
65-
sfg_window->SetPosition(sf::Vector2f(window.getSize().x/2-sfg_window->GetRequisition().x/2, window.getSize().y/2-sfg_window->GetRequisition().y/2));
66-
67-
sf::Event event;
68-
sf::Clock clock;
69-
70-
window.resetGLStates();
71-
72-
while (window.isOpen())
73-
{
74-
while (window.pollEvent(event))
75-
{
76-
desktop.HandleEvent( event );
77-
switch(event.type)
78-
{
79-
case sf::Event::Closed:
80-
window.close();
81-
break;
82-
}
83-
}
84-
desktop.Update( clock.restart().asSeconds() );
85-
window.clear();
86-
sfgui.Display( window );
87-
window.display();
88-
}
89-
10+
sfg::SFGUI sfgui;
11+
sf::RenderWindow window(sf::VideoMode(800, 600), "ListBox Example");
12+
window.setVerticalSyncEnabled(true);
13+
window.setFramerateLimit(30);
14+
15+
sfg::Desktop desktop;
16+
17+
auto window1 = sfg::Window::Create();
18+
window1->SetTitle( "ListBox with ItemTextPolicy::RESIZE_LISTBOX" );
19+
20+
auto box1 = sfg::Box::Create( sfg::Box::Orientation::VERTICAL );
21+
box1->SetSpacing( 5.f );
22+
box1->PackEnd( sfg::Label::Create( "The minimum width\nof this ListBox\ncorresponds to the largest\nitem's text width." ), false, true );
23+
24+
auto listbox1 = sfg::ListBox::Create();
25+
listbox1->AppendItem( "This is the first item" );
26+
listbox1->AppendItem( "Second item" );
27+
listbox1->AppendItem( "Third item which is a bit large" );
28+
listbox1->AppendItem( "Fourth item" );
29+
listbox1->AppendItem( "Fifth item" );
30+
listbox1->AppendItem( "Sixth item" );
31+
listbox1->AppendItem( "Last one !" );
32+
box1->PackEnd( listbox1 );
33+
34+
window1->Add( box1 );
35+
36+
auto window2 = sfg::Window::Create();
37+
window2->SetTitle( "ListBox with ItemTextPolicy::SHRINK" );
38+
39+
auto box2 = sfg::Box::Create( sfg::Box::Orientation::VERTICAL );
40+
box2->SetSpacing( 5.f );
41+
auto label2 = sfg::Label::Create( "The items' texts\nare shrinked if the\nListBox is not big\nenough." );
42+
box2->PackEnd( label2, false, true );
43+
44+
auto listbox2 = sfg::ListBox::Create();
45+
listbox2->AppendItem( "This is the first item (long text)" );
46+
listbox2->AppendItem( "Second item" );
47+
listbox2->AppendItem( "Third item which is very long !" );
48+
listbox2->AppendItem( "Fourth item" );
49+
listbox2->AppendItem( "Fifth item" );
50+
listbox2->AppendItem( "Sixth item, again it's too large !" );
51+
listbox2->AppendItem( "Last one !" );
52+
listbox2->SetItemTextPolicy( sfg::ListBox::ItemTextPolicy::SHRINK );
53+
box2->PackEnd( listbox2 );
54+
55+
window2->Add( box2 );
56+
57+
auto window3 = sfg::Window::Create();
58+
window3->SetTitle( "ListBox with ItemTextPolicy::SHRINK" );
59+
60+
auto box3 = sfg::Box::Create( sfg::Box::Orientation::VERTICAL );
61+
box3->SetSpacing( 5.f );
62+
auto label3 = sfg::Label::Create( "You can select multiple\nitems in this ListBox." );
63+
box3->PackEnd( label3, false, true );
64+
65+
auto listbox3 = sfg::ListBox::Create();
66+
listbox3->AppendItem( "First item" );
67+
listbox3->AppendItem( "Second item" );
68+
listbox3->AppendItem( "Third item" );
69+
listbox3->AppendItem( "Fourth item" );
70+
listbox3->AppendItem( "Fifth item" );
71+
listbox3->AppendItem( "Sixth item" );
72+
listbox3->AppendItem( "Last one !" );
73+
listbox3->SetSelectionMode( sfg::ListBox::SelectionMode::MULTI_SELECTION );
74+
listbox3->SetSelection( {1, 3, 4, 5} );
75+
box3->PackEnd( listbox3 );
76+
77+
window3->Add( box3 );
78+
79+
desktop.Add( window1 );
80+
desktop.Add( window2 );
81+
desktop.Add( window3 );
82+
83+
sf::Vector2f windowSize( static_cast<float>( window.getSize().x ), static_cast<float>( window.getSize().y ) );
84+
85+
window2->SetPosition(sf::Vector2f(windowSize.x/2.f - window2->GetRequisition().x/2.f, windowSize.y/2.f - window2->GetRequisition().y/2.f));
86+
window3->SetPosition(sf::Vector2f(windowSize.x - window3->GetRequisition().x - 100.f, windowSize.y - window3->GetRequisition().y - 100.f));
87+
88+
sf::Event event;
89+
sf::Clock clock;
90+
91+
window.resetGLStates();
92+
93+
int i = 0;
94+
95+
while (window.isOpen())
96+
{
97+
while (window.pollEvent(event))
98+
{
99+
desktop.HandleEvent( event );
100+
switch(event.type)
101+
{
102+
case sf::Event::Closed:
103+
window.close();
104+
break;
105+
case sf::Event::KeyPressed:
106+
if( event.key.code == sf::Keyboard::R ) {
107+
listbox3->RemoveItem(2);
108+
} else if( event.key.code == sf::Keyboard::I ) {
109+
listbox3->InsertItem(3, "Inserted item #" + std::to_string(i));
110+
++i;
111+
} else if( event.key.code == sf::Keyboard::A) {
112+
listbox3->AppendItem("Appended item #" + std::to_string(i));
113+
++i;
114+
} else if( event.key.code == sf::Keyboard::P) {
115+
listbox3->PrependItem("Prepended item #" + std::to_string(i));
116+
++i;
117+
}
118+
break;
119+
default:
120+
break;
121+
}
122+
}
123+
desktop.Update( clock.restart().asSeconds() );
124+
window.clear();
125+
sfgui.Display( window );
126+
window.display();
127+
}
128+
90129
return 0;
91130
}

include/SFGUI/Engine.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Notebook;
4040
class Spinner;
4141
class ComboBox;
4242
class SpinButton;
43+
class ListBox;
4344

4445
class Selector;
4546
class RenderQueue;
@@ -164,6 +165,12 @@ class SFGUI_API Engine {
164165
*/
165166
virtual std::unique_ptr<RenderQueue> CreateSpinButtonDrawable( std::shared_ptr<const SpinButton> spinbutton ) const = 0;
166167

168+
/** Create drawable for listbox widgets.
169+
* @param listbox Widget.
170+
* @return New drawable object (unmanaged memory!).
171+
*/
172+
virtual std::unique_ptr<RenderQueue> CreateListBoxDrawable( std::shared_ptr<const ListBox> listbox ) const = 0;
173+
167174
/** Get maximum line height.
168175
* @param font Font.
169176
* @param font_size Font size.

include/SFGUI/Engines/BREW.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class SFGUI_API BREW : public Engine {
4343
std::unique_ptr<RenderQueue> CreateSpinnerDrawable( std::shared_ptr<const Spinner> spinner ) const override;
4444
std::unique_ptr<RenderQueue> CreateComboBoxDrawable( std::shared_ptr<const ComboBox> combo_box ) const override;
4545
std::unique_ptr<RenderQueue> CreateSpinButtonDrawable( std::shared_ptr<const SpinButton> spinbutton ) const override;
46+
std::unique_ptr<RenderQueue> CreateListBoxDrawable( std::shared_ptr<const ListBox> listbox ) const override;
4647

4748
private:
4849
static std::unique_ptr<RenderQueue> CreateBorder( const sf::FloatRect& rect, float border_width, const sf::Color& light_color, const sf::Color& dark_color );

0 commit comments

Comments
 (0)