MCPcopy Create free account
hub / github.com/TankOs/SFGUI / main

Function main

examples/ComboBox.cpp:9–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7#include <SFML/Graphics.hpp>
8
9int main() {
10 // Create the main SFML window
11 sf::RenderWindow app_window( sf::VideoMode( { 800, 600 } ), "SFGUI Combo Box Example", sf::Style::Titlebar | sf::Style::Close );
12
13 // Create an SFGUI. This is required before doing anything with SFGUI.
14 sfg::SFGUI sfgui;
15
16 // We have to do this because we don't use SFML to draw.
17 app_window.resetGLStates();
18
19 // Create our main SFGUI window
20 auto window = sfg::Window::Create();
21 window->SetTitle( "Title" );
22
23 // Create the combo box itself.
24 auto combo_box = sfg::ComboBox::Create();
25
26 // Set the entries of the combo box.
27 combo_box->AppendItem( "Bar" );
28 combo_box->PrependItem( "Foo" );
29
30 auto sel_label = sfg::Label::Create( L"Please select an item!" );
31
32 auto add_button = sfg::Button::Create( L"Add item" );
33 auto remove_button = sfg::Button::Create( L"Remove first item" );
34 auto clear_button = sfg::Button::Create( L"Clear items" );
35
36 auto hbox = sfg::Box::Create( sfg::Box::Orientation::HORIZONTAL, 5 );
37 hbox->Pack( combo_box );
38 hbox->Pack( add_button, false );
39 hbox->Pack( remove_button, false );
40 hbox->Pack( clear_button, false );
41
42 auto vbox = sfg::Box::Create( sfg::Box::Orientation::VERTICAL, 5 );
43 vbox->Pack( hbox, false );
44 vbox->Pack( sel_label, true );
45
46 // Add the combo box to the window
47 window->Add( vbox );
48
49 // So that our combo box has a meaningful purpose (besides just looking
50 // awesome :P) we need to tell it to connect to a callback of our choosing to
51 // notify us when it is clicked.
52 combo_box->GetSignal( sfg::ComboBox::OnSelect ).Connect( [&sel_label, &combo_box] {
53 sel_label->SetText( "Item " + std::to_string( combo_box->GetSelectedItem() ) + " selected with text \"" + combo_box->GetSelectedText() + "\"" );
54 } );
55
56 add_button->GetSignal( sfg::Widget::OnLeftClick ).Connect( [&combo_box] {
57 static int counter( 0 );
58
59 std::stringstream sstr;
60 sstr << "Item " << counter;
61 combo_box->AppendItem( sstr.str() );
62
63 ++counter;
64 } );
65
66 remove_button->GetSignal( sfg::Widget::OnLeftClick ).Connect( [&combo_box] { combo_box->RemoveItem( 0 ); } );

Callers

nothing calls this directly

Calls 15

SetTitleMethod · 0.80
AppendItemMethod · 0.80
PrependItemMethod · 0.80
PackMethod · 0.80
ConnectMethod · 0.80
GetSelectedItemMethod · 0.80
RemoveItemMethod · 0.80
AddMethod · 0.45
SetTextMethod · 0.45
strMethod · 0.45
ClearMethod · 0.45
HandleEventMethod · 0.45

Tested by

no test coverage detected