ARB
ed4_flags.hxx
Go to the documentation of this file.
1 // ============================================================== //
2 // //
3 // File : ed4_flags.hxx //
4 // Purpose : Representation of species-flag setup //
5 // //
6 // Coded by Ralf Westram (coder@reallysoft.de) in August 2016 //
7 // http://www.arb-home.de/ //
8 // //
9 // ============================================================== //
10 
11 #ifndef ED4_FLAGS_HXX
12 #define ED4_FLAGS_HXX
13 
14 #ifndef _GLIBCXX_STRING
15 #include <string>
16 #endif
17 #ifndef _GLIBCXX_LIST
18 #include <list>
19 #endif
20 #ifndef ED4_DEFS_HXX
21 #include "ed4_defs.hxx"
22 #endif
23 
24 class SpeciesFlag {
25  std::string shortname; // abbreviation used in flag-header
26  std::string fieldname; // name of species field
27 
28  int awar_index; // index of awars
29 
30  int xpos; // inside terminal
31  int width;
32 
33 public:
34  SpeciesFlag(const std::string& shortname_, const std::string& fieldname_, int awar_idx) :
35  shortname(shortname_),
36  fieldname(fieldname_),
37  awar_index(awar_idx),
38  xpos(-1),
39  width(-1)
40  {
41  e4_assert(!shortname.empty()); // not allowed
42  }
43 
44  const std::string& get_shortname() const { return shortname; }
45  const std::string& get_fieldname() const { return fieldname; }
46 
47  void set_dimension(int xpos_, int width_) {
48  xpos = xpos_;
49  width = width_;
50  }
51  int get_xpos() const {
52  e4_assert(xpos>=0);
53  return xpos;
54  }
55  int get_width() const {
56  e4_assert(width>=1); // empty header not allowed!
57  return width;
58  }
59 
60  double center_xpos() const {
61  return get_xpos() + get_width()*0.5;
62  }
63 
64  const char *prepare_itemfield() const;
65 };
66 
67 typedef std::list<SpeciesFlag> SpeciesFlagList;
68 typedef SpeciesFlagList::iterator SpeciesFlagIter;
69 typedef SpeciesFlagList::const_iterator SpeciesFlagCiter;
70 
71 class SpeciesFlags : public SpeciesFlagList {
72  static SpeciesFlags *SINGLETON;
73  static void create_instance();
74 
75  mutable SmartCharPtr header;
76  mutable int header_length; // characters
77 
78  int pixel_width; // width of text (plus a bit extra to avoid char-clipping + CHARACTEROFFSET)
79  int min_flag_distance;
80 
81  SpeciesFlags() :
82  header_length(-1),
83  pixel_width(-1),
84  min_flag_distance(INT_MAX)
85  {}
86  void build_header_text() const;
87 
88 public:
89 
90  static const SpeciesFlags& instance() {
91  if (!SINGLETON) create_instance();
92  e4_assert(SINGLETON);
93  return *SINGLETON;
94  }
96  return const_cast<SpeciesFlags&>(instance());
97  }
98  static void forget() {
99  delete SINGLETON;
100  SINGLETON = NULp;
101  }
102 
103  const char *get_header_text() const {
104  if (header.isNull()) build_header_text();
105  return &*header;
106  }
107  int get_header_length() const {
108  if (header_length<0) {
109  header_length = strlen(get_header_text());
110  }
111  return header_length;
112  }
113  int get_pixel_width() const {
114  e4_assert(pixel_width>0);
115  return pixel_width;
116  }
117  int get_min_flag_distance() const {
118  e4_assert(min_flag_distance != INT_MAX);
119  return min_flag_distance;
120  }
121 
122  void calculate_header_dimensions(AW_device *device, int gc);
123 };
124 
126 
127 #else
128 #error ed4_flags.hxx included twice
129 #endif // ED4_FLAGS_HXX
130 
return string(buffer, length)
const char * get_header_text() const
Definition: ed4_flags.hxx:103
static void forget()
Definition: ed4_flags.hxx:98
static const SpeciesFlags & instance()
Definition: ed4_flags.hxx:90
void set_dimension(int xpos_, int width_)
Definition: ed4_flags.hxx:47
static SpeciesFlags & mutable_instance()
Definition: ed4_flags.hxx:95
int get_header_length() const
Definition: ed4_flags.hxx:107
#define e4_assert(bed)
Definition: ed4_class.hxx:11
AW_window * ED4_configure_species_flags(AW_root *root, GBDATA *gb_main)
Definition: ED4_flags.cxx:213
int get_min_flag_distance() const
Definition: ed4_flags.hxx:117
SpeciesFlagList::iterator SpeciesFlagIter
Definition: ed4_flags.hxx:68
SpeciesFlagList::const_iterator SpeciesFlagCiter
Definition: ed4_flags.hxx:69
int get_xpos() const
Definition: ed4_flags.hxx:51
const char * prepare_itemfield() const
Definition: ED4_flags.cxx:74
const std::string & get_shortname() const
Definition: ed4_flags.hxx:44
int get_width() const
Definition: ed4_flags.hxx:55
SpeciesFlag(const std::string &shortname_, const std::string &fieldname_, int awar_idx)
Definition: ed4_flags.hxx:34
void calculate_header_dimensions(AW_device *device, int gc)
Definition: ED4_flags.cxx:153
double center_xpos() const
Definition: ed4_flags.hxx:60
#define NULp
Definition: cxxforward.h:97
GBDATA * gb_main
Definition: adname.cxx:33
std::list< SpeciesFlag > SpeciesFlagList
Definition: ed4_flags.hxx:67
int get_pixel_width() const
Definition: ed4_flags.hxx:113
const std::string & get_fieldname() const
Definition: ed4_flags.hxx:45