1+ #include " CountryWindow.h"
2+ #include " ../countries.h" ;
3+
4+ #define WIDTH (400 )
5+ #define HEIGHT (300 )
6+
7+ #define TEXTHEIGHT (24 )
8+ #define FULLWIDTH (WIDTH - 40 )
9+ #define LEFT (20 )
10+ #define ROW (x ) (40 + ((x - 1 ) * 45 ))
11+ #define TEXTROW (x ) (52 + ((x - 1 ) * 45 ))
12+
13+ CountryWindow::CountryWindow (nana::form* parent, DataService& dataService, std::string current) :
14+ sdrwindow(parent, " COUNTRIES" , WIDTH, HEIGHT, dataService),
15+ m_ituCode(current)
16+ {
17+ }
18+
19+ nana::listbox::oresolver& operator <<(nana::listbox::oresolver& ores, const staticdata::country_t & country)
20+ {
21+ return ores << country.code << country.name ;
22+ }
23+
24+ bool findSearchTextInCountry (const std::string& searchText, const staticdata::country_t country)
25+ {
26+ if (searchText.empty ())
27+ return true ;
28+
29+ auto it = std::search (
30+ country.name .begin (), country.name .end (),
31+ searchText.begin (), searchText.end (),
32+ [](char ch1, char ch2) { return std::toupper (ch1) == std::toupper (ch2); }
33+ );
34+ return (it != country.name .end ());
35+ }
36+
37+ void CountryWindow::createWidgets ()
38+ {
39+ // Search
40+ m_lblSearchC = makeLabelTitle (LEFT, ROW (1 ), FULLWIDTH, " COUNTRY NAME" );
41+ m_txtSearch = makeTextbox (LEFT, TEXTROW (1 ), FULLWIDTH, TEXTHEIGHT, false , " Start typing country name ..." );
42+
43+ // Results
44+ m_lblResultsC = makeLabelTitle (LEFT, ROW (2 ), FULLWIDTH, " RESULTS" );
45+ m_lstResults = makeListbox (LEFT, TEXTROW (2 ), FULLWIDTH, 150 );
46+ m_lstResults->append_header (" ITU" , 60 );
47+ m_lstResults->append_header (" NAME" , 260 );
48+
49+ // Buttons
50+ m_btnOk = makeButton (20 , 260 , " OK" , " Accept selected country" );
51+ if (m_ituCode.empty ())
52+ m_btnOk->enabled (false );
53+
54+ m_btnCancel = makeButton (84 , 260 , " CANCEL" , " Cancel selection" );
55+
56+ // Events
57+ m_lstResults->events ().selected ([&](const nana::arg_listbox& arg) {
58+ if (arg.item .selected ()) {
59+ m_ituCode = arg.item .text (0 );
60+ m_btnOk->enabled (true );
61+ }
62+ else
63+ {
64+ m_ituCode = " " ;
65+ m_btnOk->enabled (false );
66+ }
67+ });
68+
69+ m_lstResults->events ().dbl_click ([&] {
70+ if (!m_ituCode.empty ())
71+ Close ();
72+ });
73+
74+ m_btnOk->events ().click ([&] {
75+ Close ();
76+ });
77+
78+ m_btnCancel->events ().click ([&] {
79+ m_ituCode = " " ;
80+ Close ();
81+ });
82+
83+ m_txtSearch->events ().text_changed ([&] {
84+ updateList (m_txtSearch->caption ());
85+ });
86+
87+ updateList (" " );
88+ }
89+
90+ std::string CountryWindow::GetITUCode ()
91+ {
92+ return m_ituCode;
93+ }
94+
95+ void CountryWindow::updateList (std::string searchText)
96+ {
97+ m_lstResults->clear ();
98+ auto list = m_lstResults->at (0 );
99+ for (auto country : staticdata::countries) {
100+ if (findSearchTextInCountry (searchText, country)) {
101+ auto item = list.append (country);
102+ if (country.code == m_ituCode)
103+ item.select (true , true );
104+ }
105+ }
106+ }
0 commit comments