diff options
author | Mathias Gumz <akira at fluxbox dot org> | 2010-09-17 12:44:58 (GMT) |
---|---|---|
committer | Mathias Gumz <akira at fluxbox dot org> | 2010-09-17 12:44:58 (GMT) |
commit | 041d586972db4da170123f5468d7b11d4eda8ae5 (patch) | |
tree | 2481268e38587c324939a7989ce2fae51743a926 /src/Toolbar.cc | |
parent | 0b1f160830f048ed6d499b774b5addab059f9917 (diff) | |
download | fluxbox_pavel-041d586972db4da170123f5468d7b11d4eda8ae5.zip fluxbox_pavel-041d586972db4da170123f5468d7b11d4eda8ae5.tar.bz2 |
code simplification by reducing if-else hell
a lot of the parsing-related and menu-building stuff is just plain
stupid boiler plate code. putting the data into a const array
separates the boiler plate data from the actual code.
Diffstat (limited to 'src/Toolbar.cc')
-rw-r--r-- | src/Toolbar.cc | 104 |
1 files changed, 38 insertions, 66 deletions
diff --git a/src/Toolbar.cc b/src/Toolbar.cc index 79e8929..1f401c9 100644 --- a/src/Toolbar.cc +++ b/src/Toolbar.cc | |||
@@ -53,6 +53,7 @@ | |||
53 | #include "FbTk/SimpleObserver.hh" | 53 | #include "FbTk/SimpleObserver.hh" |
54 | #include "FbTk/MemFun.hh" | 54 | #include "FbTk/MemFun.hh" |
55 | #include "FbTk/STLUtil.hh" | 55 | #include "FbTk/STLUtil.hh" |
56 | #include "FbTk/Util.hh" | ||
56 | 57 | ||
57 | // use GNU extensions | 58 | // use GNU extensions |
58 | #ifndef _GNU_SOURCE | 59 | #ifndef _GNU_SOURCE |
@@ -81,82 +82,53 @@ using std::list; | |||
81 | 82 | ||
82 | using FbTk::STLUtil::forAll; | 83 | using FbTk::STLUtil::forAll; |
83 | 84 | ||
85 | namespace { | ||
86 | |||
87 | struct ToolbarPlacementString { | ||
88 | Toolbar::Placement placement; | ||
89 | const char* str; | ||
90 | }; | ||
91 | |||
92 | const ToolbarPlacementString placement_strings[] = { | ||
93 | { Toolbar::TOPLEFT, "TopLeft" }, | ||
94 | { Toolbar::TOPCENTER, "TopCenter" }, | ||
95 | { Toolbar::TOPRIGHT, "TopRight" }, | ||
96 | { Toolbar::BOTTOMLEFT, "BottomLeft" }, | ||
97 | { Toolbar::BOTTOMCENTER, "BottomCenter" }, | ||
98 | { Toolbar::BOTTOMRIGHT, "BottomRight" }, | ||
99 | { Toolbar::LEFTBOTTOM, "LeftBottom" }, | ||
100 | { Toolbar::LEFTCENTER, "LeftCenter" }, | ||
101 | { Toolbar::LEFTTOP, "LeftTop" }, | ||
102 | { Toolbar::RIGHTCENTER, "RightCenter" }, | ||
103 | { Toolbar::RIGHTBOTTOM, "RightBottom" }, | ||
104 | { Toolbar::RIGHTTOP, "RightTop" } | ||
105 | }; | ||
106 | |||
107 | } | ||
108 | |||
84 | namespace FbTk { | 109 | namespace FbTk { |
85 | 110 | ||
86 | template<> | 111 | template<> |
87 | string FbTk::Resource<Toolbar::Placement>:: | 112 | string FbTk::Resource<Toolbar::Placement>:: |
88 | getString() const { | 113 | getString() const { |
89 | switch (m_value) { | 114 | |
90 | case Toolbar::TOPLEFT: | 115 | size_t i = (m_value == FbTk::Util::clamp(m_value, Toolbar::TOPLEFT, Toolbar::RIGHTTOP) |
91 | return string("TopLeft"); | 116 | ? m_value |
92 | break; | 117 | : Toolbar::DEFAULT) - 1; |
93 | case Toolbar::BOTTOMLEFT: | 118 | return placement_strings[i].str; |
94 | return string("BottomLeft"); | ||
95 | break; | ||
96 | case Toolbar::TOPCENTER: | ||
97 | return string("TopCenter"); | ||
98 | break; | ||
99 | case Toolbar::BOTTOMCENTER: | ||
100 | return string("BottomCenter"); | ||
101 | break; | ||
102 | case Toolbar::TOPRIGHT: | ||
103 | return string("TopRight"); | ||
104 | break; | ||
105 | case Toolbar::BOTTOMRIGHT: | ||
106 | return string("BottomRight"); | ||
107 | break; | ||
108 | case Toolbar::LEFTTOP: | ||
109 | return string("LeftTop"); | ||
110 | break; | ||
111 | case Toolbar::LEFTCENTER: | ||
112 | return string("LeftCenter"); | ||
113 | break; | ||
114 | case Toolbar::LEFTBOTTOM: | ||
115 | return string("LeftBottom"); | ||
116 | break; | ||
117 | case Toolbar::RIGHTTOP: | ||
118 | return string("RightTop"); | ||
119 | break; | ||
120 | case Toolbar::RIGHTCENTER: | ||
121 | return string("RightCenter"); | ||
122 | break; | ||
123 | case Toolbar::RIGHTBOTTOM: | ||
124 | return string("RightBottom"); | ||
125 | break; | ||
126 | } | ||
127 | //default string | ||
128 | return string("BottomCenter"); | ||
129 | } | 119 | } |
130 | 120 | ||
131 | template<> | 121 | template<> |
132 | void FbTk::Resource<Toolbar::Placement>:: | 122 | void FbTk::Resource<Toolbar::Placement>:: |
133 | setFromString(const char *strval) { | 123 | setFromString(const char *strval) { |
134 | if (strcasecmp(strval, "TopLeft")==0) | 124 | size_t i; |
135 | m_value = Toolbar::TOPLEFT; | 125 | for (i = 0; i < sizeof(placement_strings)/sizeof(ToolbarPlacementString); ++i) { |
136 | else if (strcasecmp(strval, "BottomLeft")==0) | 126 | if (strcasecmp(strval, placement_strings[i].str) == 0) { |
137 | m_value = Toolbar::BOTTOMLEFT; | 127 | m_value = placement_strings[i].placement; |
138 | else if (strcasecmp(strval, "TopCenter")==0) | 128 | return; |
139 | m_value = Toolbar::TOPCENTER; | 129 | } |
140 | else if (strcasecmp(strval, "BottomCenter")==0) | 130 | } |
141 | m_value = Toolbar::BOTTOMCENTER; | 131 | setDefaultValue(); |
142 | else if (strcasecmp(strval, "TopRight")==0) | ||
143 | m_value = Toolbar::TOPRIGHT; | ||
144 | else if (strcasecmp(strval, "BottomRight")==0) | ||
145 | m_value = Toolbar::BOTTOMRIGHT; | ||
146 | else if (strcasecmp(strval, "LeftTop") == 0) | ||
147 | m_value = Toolbar::LEFTTOP; | ||
148 | else if (strcasecmp(strval, "LeftCenter") == 0) | ||
149 | m_value = Toolbar::LEFTCENTER; | ||
150 | else if (strcasecmp(strval, "LeftBottom") == 0) | ||
151 | m_value = Toolbar::LEFTBOTTOM; | ||
152 | else if (strcasecmp(strval, "RightTop") == 0) | ||
153 | m_value = Toolbar::RIGHTTOP; | ||
154 | else if (strcasecmp(strval, "RightCenter") == 0) | ||
155 | m_value = Toolbar::RIGHTCENTER; | ||
156 | else if (strcasecmp(strval, "RightBottom") == 0) | ||
157 | m_value = Toolbar::RIGHTBOTTOM; | ||
158 | else | ||
159 | setDefaultValue(); | ||
160 | } | 132 | } |
161 | 133 | ||
162 | } // end namespace FbTk | 134 | } // end namespace FbTk |