Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
Selector.cpp
Go to the documentation of this file.
1 
11 #include "stdafx.h"
12 #include "Selector.h"
13 
14 
15 using namespace kome::core;
16 
17 
18 #include <crtdbg.h>
19 #ifdef _DEBUG
20  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
21  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
22 #endif // _DEBUG
23 
24 
25 
26 // constructor
28  m_sel = -1;
29 }
30 
31 // destructor
33 }
34 
35 // clear items
37  m_items.clear();
38  m_selected.clear();
39  m_sel = -1;
40 }
41 
42 // add item
43 void Selector::addItem( const char* name ) {
44  addItem( name, name );
45 }
46 
47 // add item
48 void Selector::addItem( const char* name, const char* value ) {
49  m_items.push_back( std::make_pair( NVL( name, "" ), NVL( value, "" ) ) );
50  m_selected.push_back( false );
51 }
52 
53 // get the number of items
55  return m_items.size();
56 }
57 
58 // get item name
59 const char* Selector::getItemName( const unsigned int index ) {
60  if( index >= m_items.size() ) {
61  return NULL;
62  }
63  return m_items[ index ].first.c_str();
64 }
65 
66 // get item value
67 const char* Selector::getItemValue( const unsigned index ) {
68  if( index >= m_items.size() ) {
69  return NULL;
70  }
71  return m_items[ index ].second.c_str();
72 }
73 
74 // get selected flag value
75 bool Selector::isSelected( const unsigned int index ) {
76  if( index >= m_selected.size() ) {
77  return false;
78  }
79  return m_selected[ index ];
80 }
81 
82 // get selected item
84  if( m_sel < 0 ) {
85  for( unsigned int i = 0; i < m_selected.size() && m_sel < 0; i++ ) {
86  if( m_selected[ i ] ) {
87  m_sel = i;
88  }
89  }
90  }
91 
92  return m_sel;
93 }
94 
95 // select item
97  // initialize
98  m_sel = -1;
99 
100  // create array
101  const char** items = NULL;
102  const char** values = NULL;
103  bool* selected = NULL;
104  unsigned int num = m_items.size();
105  if( num > 0 ) {
106  items = new const char*[ num ];
107  values = new const char*[ num ];
108  selected = new bool[ num ];
109 
110  for( unsigned int i = 0; i < num; i++ ) {
111  items[ i ] = m_items[ i ].first.c_str();
112  values[ i ] = m_items[ i ].second.c_str();
113  selected[ i ] = m_selected[ i ];
114  }
115  }
116 
117  // select
118  bool ret = onSelect( num, items, values, selected );
119  for( unsigned int i = 0; i < num; i++ ) {
120  m_selected[ i ] = selected[ i ];
121  }
122 
123  // delete
124  if( items != NULL ) {
125  delete[] items;
126  }
127  if( values != NULL ) {
128  delete[] values;
129  }
130  if( selected != NULL ) {
131  delete[] selected;
132  }
133 
134  return ret;
135 }
virtual ~Selector()
destructor
Definition: Selector.cpp:32
virtual bool onSelect(const unsigned int num, const char **items, const char **values, bool *selected)=0
This method is called by select method. (abstract method)
interfaces of Selector class
Selector()
constructor
Definition: Selector.cpp:27
const char * getItemName(const unsigned int index)
gets item name
Definition: Selector.cpp:59
int getSelection()
[in] gets selected item
Definition: Selector.cpp:83
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
bool isSelected(const unsigned int index)
gets selection flag value
Definition: Selector.cpp:75
#define NULL
Definition: CoreMacros.h:18
std::vector< bool > m_selected
Definition: Selector.h:47
const char * getItemValue(const unsigned int index)
gets item value
Definition: Selector.cpp:67
std::vector< std::pair< std::string, std::string > > m_items
Definition: Selector.h:44
void clearItems()
clear items
Definition: Selector.cpp:36
bool select()
selects item
Definition: Selector.cpp:96
void addItem(const char *name)
adds item
Definition: Selector.cpp:43
unsigned int getNumberOfItems()
gets the number of items
Definition: Selector.cpp:54