Skip to content

Commit d4f439c

Browse files
committed
ListBox widget added.
1 parent c01dd76 commit d4f439c

File tree

6 files changed

+398
-0
lines changed

6 files changed

+398
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ set(
5656
"${INCLUDE_PATH}/SFGUI/Frame.hpp"
5757
"${INCLUDE_PATH}/SFGUI/Image.hpp"
5858
"${INCLUDE_PATH}/SFGUI/Label.hpp"
59+
"${INCLUDE_PATH}/SFGUI/ListBox.hpp"
5960
"${INCLUDE_PATH}/SFGUI/Misc.hpp"
6061
"${INCLUDE_PATH}/SFGUI/Notebook.hpp"
6162
"${INCLUDE_PATH}/SFGUI/Object.hpp"
@@ -135,6 +136,7 @@ set(
135136
"${SOURCE_PATH}/SFGUI/GLLoader.hpp"
136137
"${SOURCE_PATH}/SFGUI/Image.cpp"
137138
"${SOURCE_PATH}/SFGUI/Label.cpp"
139+
"${SOURCE_PATH}/SFGUI/ListBox.cpp"
138140
"${SOURCE_PATH}/SFGUI/Misc.cpp"
139141
"${SOURCE_PATH}/SFGUI/Notebook.cpp"
140142
"${SOURCE_PATH}/SFGUI/Object.cpp"

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ build_example( "ProgressBar" "ProgressBar.cpp" )
3838
build_example( "SpinButton" "SpinButton.cpp" )
3939
build_example( "Canvas" "Canvas.cpp" )
4040
build_example( "CustomWidget" "CustomWidget.cpp" )
41+
build_example( "ListBox" "ListBox.cpp" )
4142
build_example( "SFGUI-Test" "Test.cpp" )
4243

4344
# Copy data directory to build cache directory to be able to run examples from

examples/ListBox.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Always include the necessary header files.
2+
// Including SFGUI/Widgets.hpp includes everything
3+
// you can possibly need automatically.
4+
#include <SFGUI/SFGUI.hpp>
5+
#include <SFGUI/Widgets.hpp>
6+
7+
#include <SFML/Graphics.hpp>
8+
9+
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+
90+
return 0;
91+
}

include/SFGUI/ListBox.hpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#pragma once
2+
3+
#include <SFGUI/Bin.hpp>
4+
5+
#include <SFML/System/String.hpp>
6+
#include <memory>
7+
#include <vector>
8+
9+
namespace sfg {
10+
11+
class SFGUI_API ListBox : public Widget {
12+
public:
13+
typedef std::shared_ptr<ListBox> Ptr; //!< Shared pointer.
14+
typedef std::shared_ptr<const ListBox> PtrConst; //!< Shared pointer.
15+
16+
/** Create button.
17+
* @return ListBox.
18+
*/
19+
static Ptr Create( );
20+
21+
const std::string& GetName() const override;
22+
23+
unsigned GetSelectedItemsCount();
24+
unsigned GetSelectedItemIndex(unsigned n=0);
25+
const std::string& GetSelectedItemText(unsigned n=0);
26+
27+
void AppendItem(std::string str);
28+
void PrependItem(std::string str);
29+
void RemoveItem(unsigned index);
30+
void Clear();
31+
32+
// Signals.
33+
static Signal::SignalID OnSelect; //!< Fired when an entry is selected.
34+
35+
bool resize_automatically = true;
36+
bool multiselect = false;
37+
38+
protected:
39+
/** Ctor.
40+
*/
41+
ListBox() = default;
42+
43+
std::unique_ptr<RenderQueue> InvalidateImpl() const override;
44+
sf::Vector2f CalculateRequisition() override;
45+
46+
private:
47+
void HandleMouseEnter( int x, int y ) override;
48+
void HandleMouseLeave( int x, int y ) override;
49+
void HandleMouseMoveEvent( int x, int y ) override;
50+
void HandleMouseButtonEvent( sf::Mouse::Button button, bool press, int x, int y ) override;
51+
52+
std::vector<std::string> m_entries;
53+
int selection_begin = -1, selection_end = -1;
54+
int hovered_element = -1; bool pressed_on_widget = false;
55+
std::vector<bool> selection_odds;
56+
57+
bool widget_hover = false;
58+
};
59+
60+
}

include/SFGUI/Widgets.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <SFGUI/Frame.hpp>
2020
#include <SFGUI/Image.hpp>
2121
#include <SFGUI/Label.hpp>
22+
#include <SFGUI/ListBox.hpp>
2223
#include <SFGUI/Notebook.hpp>
2324
#include <SFGUI/ProgressBar.hpp>
2425
#include <SFGUI/RadioButton.hpp>

0 commit comments

Comments
 (0)