aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMathias Gumz <akira at fluxbox dot org>2010-09-17 12:44:58 (GMT)
committerMathias Gumz <akira at fluxbox dot org>2010-09-17 12:44:58 (GMT)
commit041d586972db4da170123f5468d7b11d4eda8ae5 (patch)
tree2481268e38587c324939a7989ce2fae51743a926 /src
parent0b1f160830f048ed6d499b774b5addab059f9917 (diff)
downloadfluxbox_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')
-rw-r--r--src/FbWinFrame.hh4
-rw-r--r--src/Screen.cc162
-rw-r--r--src/Toolbar.cc104
-rw-r--r--src/Toolbar.hh4
4 files changed, 110 insertions, 164 deletions
diff --git a/src/FbWinFrame.hh b/src/FbWinFrame.hh
index 80791ba..7cdf9bb 100644
--- a/src/FbWinFrame.hh
+++ b/src/FbWinFrame.hh
@@ -67,7 +67,9 @@ public:
67 BOTTOMLEFT, BOTTOM, BOTTOMRIGHT, 67 BOTTOMLEFT, BOTTOM, BOTTOMRIGHT,
68 // left and right placement 68 // left and right placement
69 LEFTBOTTOM, LEFT, LEFTTOP, 69 LEFTBOTTOM, LEFT, LEFTTOP,
70 RIGHTBOTTOM, RIGHT, RIGHTTOP 70 RIGHTBOTTOM, RIGHT, RIGHTTOP,
71
72 DEFAULT = TOPLEFT
71 }; 73 };
72 74
73 /// create a top level window 75 /// create a top level window
diff --git a/src/Screen.cc b/src/Screen.cc
index 0677a14..daa87dc 100644
--- a/src/Screen.cc
+++ b/src/Screen.cc
@@ -183,7 +183,7 @@ int calcSquareDistance(int x1, int y1, int x2, int y2) {
183 183
184class TabPlacementMenuItem: public FbTk::RadioMenuItem { 184class TabPlacementMenuItem: public FbTk::RadioMenuItem {
185public: 185public:
186 TabPlacementMenuItem(FbTk::FbString & label, BScreen &screen, 186 TabPlacementMenuItem(const FbTk::FbString & label, BScreen &screen,
187 FbWinFrame::TabPlacement place, 187 FbWinFrame::TabPlacement place,
188 FbTk::RefCount<FbTk::Command<void> > &cmd): 188 FbTk::RefCount<FbTk::Command<void> > &cmd):
189 FbTk::RadioMenuItem(label, cmd), 189 FbTk::RadioMenuItem(label, cmd),
@@ -208,6 +208,28 @@ void clampMenuDelay(int& delay) {
208 delay = FbTk::Util::clamp(delay, 0, 5000); 208 delay = FbTk::Util::clamp(delay, 0, 5000);
209} 209}
210 210
211
212struct TabPlacementString {
213 FbWinFrame::TabPlacement placement;
214 const char* str;
215};
216
217const TabPlacementString placement_strings[] = {
218 { FbWinFrame::TOPLEFT, "TopLeft" },
219 { FbWinFrame::TOP, "Top" },
220 { FbWinFrame::TOPRIGHT, "TopRight" },
221 { FbWinFrame::BOTTOMLEFT, "BottomLeft" },
222 { FbWinFrame::BOTTOM, "Bottom" },
223 { FbWinFrame::BOTTOMRIGHT, "BottomRight" },
224 { FbWinFrame::LEFTBOTTOM, "LeftBottom" },
225 { FbWinFrame::LEFT, "Left" },
226 { FbWinFrame::LEFTTOP, "LeftTop" },
227 { FbWinFrame::RIGHTBOTTOM, "RightBottom" },
228 { FbWinFrame::RIGHT, "Right" },
229 { FbWinFrame::RIGHTTOP, "RightTop" }
230};
231
232
211} // end anonymous namespace 233} // end anonymous namespace
212 234
213 235
@@ -217,77 +239,25 @@ namespace FbTk {
217template<> 239template<>
218string FbTk::Resource<FbWinFrame::TabPlacement>:: 240string FbTk::Resource<FbWinFrame::TabPlacement>::
219getString() const { 241getString() const {
220 switch (m_value) { 242
221 case FbWinFrame::TOPLEFT: 243 size_t i = (m_value == FbTk::Util::clamp(m_value, FbWinFrame::TOPLEFT, FbWinFrame::RIGHTTOP)
222 return string("TopLeft"); 244 ? m_value
223 break; 245 : FbWinFrame::DEFAULT) - 1;
224 case FbWinFrame::BOTTOMLEFT: 246 return placement_strings[i].str;
225 return string("BottomLeft");
226 break;
227 case FbWinFrame::TOP:
228 return string("Top");
229 break;
230 case FbWinFrame::BOTTOM:
231 return string("Bottom");
232 break;
233 case FbWinFrame::TOPRIGHT:
234 return string("TopRight");
235 break;
236 case FbWinFrame::BOTTOMRIGHT:
237 return string("BottomRight");
238 break;
239 case FbWinFrame::LEFTTOP:
240 return string("LeftTop");
241 break;
242 case FbWinFrame::LEFT:
243 return string("Left");
244 break;
245 case FbWinFrame::LEFTBOTTOM:
246 return string("LeftBottom");
247 break;
248 case FbWinFrame::RIGHTTOP:
249 return string("RightTop");
250 break;
251 case FbWinFrame::RIGHT:
252 return string("Right");
253 break;
254 case FbWinFrame::RIGHTBOTTOM:
255 return string("RightBottom");
256 break;
257 }
258 //default string
259 return string("TopLeft");
260} 247}
261 248
262template<> 249template<>
263void FbTk::Resource<FbWinFrame::TabPlacement>:: 250void FbTk::Resource<FbWinFrame::TabPlacement>::
264setFromString(const char *strval) { 251setFromString(const char *strval) {
265 if (strcasecmp(strval, "TopLeft") == 0) 252
266 m_value = FbWinFrame::TOPLEFT; 253 size_t i;
267 else if (strcasecmp(strval, "BottomLeft") == 0) 254 for (i = 0; i < sizeof(placement_strings)/sizeof(TabPlacementString); ++i) {
268 m_value = FbWinFrame::BOTTOMLEFT; 255 if (strcasecmp(strval, placement_strings[i].str) == 0) {
269 else if (strcasecmp(strval, "Top") == 0) 256 m_value = placement_strings[i].placement;
270 m_value = FbWinFrame::TOP; 257 return;
271 else if (strcasecmp(strval, "Bottom") == 0) 258 }
272 m_value = FbWinFrame::BOTTOM; 259 }
273 else if (strcasecmp(strval, "TopRight") == 0) 260 setDefaultValue();
274 m_value = FbWinFrame::TOPRIGHT;
275 else if (strcasecmp(strval, "BottomRight") == 0)
276 m_value = FbWinFrame::BOTTOMRIGHT;
277 else if (strcasecmp(strval, "LeftTop") == 0)
278 m_value = FbWinFrame::LEFTTOP;
279 else if (strcasecmp(strval, "Left") == 0)
280 m_value = FbWinFrame::LEFT;
281 else if (strcasecmp(strval, "LeftBottom") == 0)
282 m_value = FbWinFrame::LEFTBOTTOM;
283 else if (strcasecmp(strval, "RightTop") == 0)
284 m_value = FbWinFrame::RIGHTTOP;
285 else if (strcasecmp(strval, "Right") == 0)
286 m_value = FbWinFrame::RIGHT;
287 else if (strcasecmp(strval, "RightBottom") == 0)
288 m_value = FbWinFrame::RIGHTBOTTOM;
289 else
290 setDefaultValue();
291} 261}
292 262
293} // end namespace FbTk 263} // end namespace FbTk
@@ -1649,41 +1619,41 @@ void BScreen::setupConfigmenu(FbTk::Menu &menu) {
1649 tab_width_item->setCommand(save_and_reconftabs); 1619 tab_width_item->setCommand(save_and_reconftabs);
1650 tab_menu->insert(tab_width_item); 1620 tab_menu->insert(tab_width_item);
1651 1621
1652
1653 typedef pair<FbTk::FbString, FbWinFrame::TabPlacement> PlacementP;
1654 typedef list<PlacementP> Placements;
1655 Placements place_menu;
1656
1657 // menu is 3 wide, 5 down 1622 // menu is 3 wide, 5 down
1658 place_menu.push_back(PlacementP(_FB_XTEXT(Align, TopLeft, "Top Left", "Top Left"), FbWinFrame::TOPLEFT)); 1623 struct PlacementP {
1659 place_menu.push_back(PlacementP(_FB_XTEXT(Align, LeftTop, "Left Top", "Left Top"), FbWinFrame::LEFTTOP)); 1624 const FbTk::FbString label;
1660 place_menu.push_back(PlacementP(_FB_XTEXT(Align, LeftCenter, "Left Center", "Left Center"), FbWinFrame::LEFT)); 1625 FbWinFrame::TabPlacement placement;
1661 place_menu.push_back(PlacementP(_FB_XTEXT(Align, LeftBottom, "Left Bottom", "Left Bottom"), FbWinFrame::LEFTBOTTOM)); 1626 };
1662 place_menu.push_back(PlacementP(_FB_XTEXT(Align, BottomLeft, "Bottom Left", "Bottom Left"), FbWinFrame::BOTTOMLEFT)); 1627 static const PlacementP place_menu[] = {
1663 place_menu.push_back(PlacementP(_FB_XTEXT(Align, TopCenter, "Top Center", "Top Center"), FbWinFrame::TOP)); 1628
1664 place_menu.push_back(PlacementP("", FbWinFrame::TOPLEFT)); 1629 { _FB_XTEXT(Align, TopLeft, "Top Left", "Top Left"), FbWinFrame::TOPLEFT},
1665 place_menu.push_back(PlacementP("", FbWinFrame::TOPLEFT)); 1630 { _FB_XTEXT(Align, LeftTop, "Left Top", "Left Top"), FbWinFrame::LEFTTOP},
1666 place_menu.push_back(PlacementP("", FbWinFrame::TOPLEFT)); 1631 { _FB_XTEXT(Align, LeftCenter, "Left Center", "Left Center"), FbWinFrame::LEFT},
1667 place_menu.push_back(PlacementP(_FB_XTEXT(Align, BottomCenter, "Bottom Center", "Bottom Center"), FbWinFrame::BOTTOM)); 1632 { _FB_XTEXT(Align, LeftBottom, "Left Bottom", "Left Bottom"), FbWinFrame::LEFTBOTTOM},
1668 place_menu.push_back(PlacementP(_FB_XTEXT(Align, TopRight, "Top Right", "Top Right"), FbWinFrame::TOPRIGHT)); 1633 { _FB_XTEXT(Align, BottomLeft, "Bottom Left", "Bottom Left"), FbWinFrame::BOTTOMLEFT},
1669 place_menu.push_back(PlacementP(_FB_XTEXT(Align, RightTop, "Right Top", "Right Top"), FbWinFrame::RIGHTTOP)); 1634 { _FB_XTEXT(Align, TopCenter, "Top Center", "Top Center"), FbWinFrame::TOP},
1670 place_menu.push_back(PlacementP(_FB_XTEXT(Align, RightCenter, "Right Center", "Right Center"), FbWinFrame::RIGHT)); 1635 { "", FbWinFrame::TOPLEFT},
1671 place_menu.push_back(PlacementP(_FB_XTEXT(Align, RightBottom, "Right Bottom", "Right Bottom"), FbWinFrame::RIGHTBOTTOM)); 1636 { "", FbWinFrame::TOPLEFT},
1672 place_menu.push_back(PlacementP(_FB_XTEXT(Align, BottomRight, "Bottom Right", "Bottom Right"), FbWinFrame::BOTTOMRIGHT)); 1637 { "", FbWinFrame::TOPLEFT},
1638 { _FB_XTEXT(Align, BottomCenter, "Bottom Center", "Bottom Center"), FbWinFrame::BOTTOM},
1639 { _FB_XTEXT(Align, TopRight, "Top Right", "Top Right"), FbWinFrame::TOPRIGHT},
1640 { _FB_XTEXT(Align, RightTop, "Right Top", "Right Top"), FbWinFrame::RIGHTTOP},
1641 { _FB_XTEXT(Align, RightCenter, "Right Center", "Right Center"), FbWinFrame::RIGHT},
1642 { _FB_XTEXT(Align, RightBottom, "Right Bottom", "Right Bottom"), FbWinFrame::RIGHTBOTTOM},
1643 { _FB_XTEXT(Align, BottomRight, "Bottom Right", "Bottom Right"), FbWinFrame::BOTTOMRIGHT}
1644 };
1673 1645
1674 tabplacement_menu->setMinimumSublevels(3); 1646 tabplacement_menu->setMinimumSublevels(3);
1675 // create items in sub menu 1647 // create items in sub menu
1676 for (size_t i=0; i<15; ++i) { 1648 for (size_t i=0; i< sizeof(place_menu)/sizeof(PlacementP); ++i) {
1677 FbTk::FbString &str = place_menu.front().first; 1649 const PlacementP& p = place_menu[i];
1678 FbWinFrame::TabPlacement placement = place_menu.front().second; 1650 if (p.label == "") {
1679 if (str == "") { 1651 tabplacement_menu->insert(p.label);
1680 tabplacement_menu->insert("");
1681 tabplacement_menu->setItemEnabled(i, false); 1652 tabplacement_menu->setItemEnabled(i, false);
1682 } else { 1653 } else
1683 tabplacement_menu->insert(new TabPlacementMenuItem(str, *this, placement, save_and_reconftabs)); 1654 tabplacement_menu->insert(new TabPlacementMenuItem(p.label, *this, p.placement, save_and_reconftabs));
1684 }
1685 place_menu.pop_front();
1686 } 1655 }
1656
1687 tabplacement_menu->updateMenu(); 1657 tabplacement_menu->updateMenu();
1688 1658
1689 menu.insert(tabmenu_label, tab_menu); 1659 menu.insert(tabmenu_label, tab_menu);
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
82using FbTk::STLUtil::forAll; 83using FbTk::STLUtil::forAll;
83 84
85namespace {
86
87struct ToolbarPlacementString {
88 Toolbar::Placement placement;
89 const char* str;
90};
91
92const 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
84namespace FbTk { 109namespace FbTk {
85 110
86template<> 111template<>
87string FbTk::Resource<Toolbar::Placement>:: 112string FbTk::Resource<Toolbar::Placement>::
88getString() const { 113getString() 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
131template<> 121template<>
132void FbTk::Resource<Toolbar::Placement>:: 122void FbTk::Resource<Toolbar::Placement>::
133setFromString(const char *strval) { 123setFromString(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
diff --git a/src/Toolbar.hh b/src/Toolbar.hh
index f631c41..ed77335 100644
--- a/src/Toolbar.hh
+++ b/src/Toolbar.hh
@@ -68,7 +68,9 @@ public:
68 BOTTOMLEFT, BOTTOMCENTER, BOTTOMRIGHT, 68 BOTTOMLEFT, BOTTOMCENTER, BOTTOMRIGHT,
69 // left and right placement 69 // left and right placement
70 LEFTBOTTOM, LEFTCENTER, LEFTTOP, 70 LEFTBOTTOM, LEFTCENTER, LEFTTOP,
71 RIGHTBOTTOM, RIGHTCENTER, RIGHTTOP 71 RIGHTBOTTOM, RIGHTCENTER, RIGHTTOP,
72
73 DEFAULT = BOTTOMRIGHT
72 }; 74 };
73 75
74 /// Create a toolbar on the screen with specific width 76 /// Create a toolbar on the screen with specific width