aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMathias Gumz <akira at fluxbox dot org>2011-10-21 06:42:48 (GMT)
committerMathias Gumz <akira at fluxbox dot org>2011-10-21 06:42:48 (GMT)
commit3f76e117bf7735058f2b135beb72e015658f97eb (patch)
treeb0a5ffe0385ab9338def8315dc1c64cfcb19ab06 /src
parent8dd11efc76cf4e1438d634fc456495d5cbd9be76 (diff)
downloadfluxbox-3f76e117bf7735058f2b135beb72e015658f97eb.zip
fluxbox-3f76e117bf7735058f2b135beb72e015658f97eb.tar.bz2
refactored MenuCreator
make public only what needs to be public
Diffstat (limited to 'src')
-rw-r--r--src/MenuCreator.cc148
-rw-r--r--src/MenuCreator.hh39
2 files changed, 85 insertions, 102 deletions
diff --git a/src/MenuCreator.cc b/src/MenuCreator.cc
index 74968e3..41dcf73 100644
--- a/src/MenuCreator.cc
+++ b/src/MenuCreator.cc
@@ -64,12 +64,79 @@ using std::list;
64using std::less; 64using std::less;
65using FbTk::AutoReloadHelper; 65using FbTk::AutoReloadHelper;
66 66
67list<string> MenuCreator::encoding_stack; 67namespace {
68list<size_t> MenuCreator::stacksize_stack;
69 68
70FbTk::StringConvertor MenuCreator::m_stringconvertor(FbTk::StringConvertor::ToFbString); 69FbTk::StringConvertor s_stringconvertor(FbTk::StringConvertor::ToFbString);
70
71list<string> s_encoding_stack;
72list<size_t> s_stacksize_stack;
73
74/**
75 * Push the encoding onto the stack, and make it active.
76 */
77void startEncoding(const string &encoding) {
78 // we push it regardless of whether it's valid, since we
79 // need to stay balanced with the endEncodings.
80 s_encoding_stack.push_back(encoding);
81
82 // this won't change if it doesn't succeed
83 s_stringconvertor.setSource(encoding);
84}
85
86/**
87 * Pop the encoding from the stack, unless we are at our stacksize limit.
88 * Restore the previous (valid) encoding.
89 */
90void endEncoding() {
91 size_t min_size = s_stacksize_stack.back();
92 if (s_encoding_stack.size() <= min_size) {
93 _FB_USES_NLS;
94 cerr<<_FB_CONSOLETEXT(Menu, ErrorEndEncoding, "Warning: unbalanced [encoding] tags", "User menu file had unbalanced [encoding] tags")<<endl;
95 return;
96 }
97
98 s_encoding_stack.pop_back();
99 s_stringconvertor.reset();
100
101 list<string>::reverse_iterator it = s_encoding_stack.rbegin();
102 list<string>::reverse_iterator it_end = s_encoding_stack.rend();
103 while (it != it_end && !s_stringconvertor.setSource(*it))
104 ++it;
105
106 if (it == it_end)
107 s_stringconvertor.setSource("");
108}
109
110
111/* push our encoding-stacksize onto the stack */
112void startFile() {
113 if (s_encoding_stack.empty())
114 s_stringconvertor.setSource("");
115 s_stacksize_stack.push_back(s_encoding_stack.size());
116}
117
118/**
119 * Pop necessary encodings from the stack
120 * (and endEncoding the final one) to our matching encoding-stacksize.
121 */
122void endFile() {
123 size_t target_size = s_stacksize_stack.back();
124 size_t curr_size = s_encoding_stack.size();
125
126 if (target_size != curr_size) {
127 _FB_USES_NLS;
128 cerr<<_FB_CONSOLETEXT(Menu, ErrorEndEncoding, "Warning: unbalanced [encoding] tags", "User menu file had unbalanced [encoding] tags")<<endl;
129 }
130
131 for (; curr_size > (target_size+1); --curr_size)
132 s_encoding_stack.pop_back();
133
134 if (curr_size == (target_size+1))
135 endEncoding();
136
137 s_stacksize_stack.pop_back();
138}
71 139
72namespace {
73 140
74void createStyleMenu(FbTk::Menu &parent, const string &label, 141void createStyleMenu(FbTk::Menu &parent, const string &label,
75 AutoReloadHelper *reloader, const string &directory) { 142 AutoReloadHelper *reloader, const string &directory) {
@@ -112,6 +179,7 @@ void createStyleMenu(FbTk::Menu &parent, const string &label,
112void createRootCmdMenu(FbTk::Menu &parent, const string &label, 179void createRootCmdMenu(FbTk::Menu &parent, const string &label,
113 const string &directory, AutoReloadHelper *reloader, 180 const string &directory, AutoReloadHelper *reloader,
114 const string &cmd) { 181 const string &cmd) {
182
115 // perform shell style ~ home directory expansion 183 // perform shell style ~ home directory expansion
116 string rootcmddir(FbTk::StringUtil::expandFilename(directory)); 184 string rootcmddir(FbTk::StringUtil::expandFilename(directory));
117 185
@@ -320,9 +388,9 @@ void translateMenuItem(FbTk::Parser &parse, ParseItem &pitem,
320 } else if (str_key == "separator") { 388 } else if (str_key == "separator") {
321 menu.insert(new FbTk::MenuSeparator()); 389 menu.insert(new FbTk::MenuSeparator());
322 } else if (str_key == "encoding") { 390 } else if (str_key == "encoding") {
323 MenuCreator::startEncoding(str_cmd); 391 startEncoding(str_cmd);
324 } else if (str_key == "endencoding") { 392 } else if (str_key == "endencoding") {
325 MenuCreator::endEncoding(); 393 endEncoding();
326 } else if (!MenuCreator::createWindowMenuItem(str_key, str_label, menu)) { 394 } else if (!MenuCreator::createWindowMenuItem(str_key, str_label, menu)) {
327 // if we didn't find any special menu item we try with command parser 395 // if we didn't find any special menu item we try with command parser
328 // we need to attach command to arguments so command parser can parse it 396 // we need to attach command to arguments so command parser can parse it
@@ -393,7 +461,7 @@ bool MenuCreator::createFromFile(const string &filename,
393 startFile(); 461 startFile();
394 if (begin) { 462 if (begin) {
395 string label; 463 string label;
396 if (!getStart(parser, label, m_stringconvertor)) { 464 if (!getStart(parser, label, s_stringconvertor)) {
397 endFile(); 465 endFile();
398 return false; 466 return false;
399 } 467 }
@@ -404,7 +472,7 @@ bool MenuCreator::createFromFile(const string &filename,
404 if (reloader) 472 if (reloader)
405 reloader->addFile(real_filename); 473 reloader->addFile(real_filename);
406 474
407 parseMenu(parser, inject_into, m_stringconvertor, reloader); 475 parseMenu(parser, inject_into, s_stringconvertor, reloader);
408 endFile(); 476 endFile();
409 477
410 return true; 478 return true;
@@ -552,68 +620,4 @@ bool MenuCreator::createWindowMenuItem(const string &type,
552 return true; 620 return true;
553} 621}
554 622
555/* push our encoding-stacksize onto the stack */
556void MenuCreator::startFile() {
557 if (encoding_stack.empty())
558 m_stringconvertor.setSource("");
559 stacksize_stack.push_back(encoding_stack.size());
560}
561
562/**
563 * Pop necessary encodings from the stack
564 * (and endEncoding the final one) to our matching encoding-stacksize.
565 */
566void MenuCreator::endFile() {
567 size_t target_size = stacksize_stack.back();
568 size_t curr_size = encoding_stack.size();
569
570 if (target_size != curr_size) {
571 _FB_USES_NLS;
572 cerr<<_FB_CONSOLETEXT(Menu, ErrorEndEncoding, "Warning: unbalanced [encoding] tags", "User menu file had unbalanced [encoding] tags")<<endl;
573 }
574
575 for (; curr_size > (target_size+1); --curr_size)
576 encoding_stack.pop_back();
577
578 if (curr_size == (target_size+1))
579 endEncoding();
580
581 stacksize_stack.pop_back();
582}
583
584/**
585 * Push the encoding onto the stack, and make it active.
586 */
587void MenuCreator::startEncoding(const string &encoding) {
588 // we push it regardless of whether it's valid, since we
589 // need to stay balanced with the endEncodings.
590 encoding_stack.push_back(encoding);
591
592 // this won't change if it doesn't succeed
593 m_stringconvertor.setSource(encoding);
594}
595
596/**
597 * Pop the encoding from the stack, unless we are at our stacksize limit.
598 * Restore the previous (valid) encoding.
599 */
600void MenuCreator::endEncoding() {
601 size_t min_size = stacksize_stack.back();
602 if (encoding_stack.size() <= min_size) {
603 _FB_USES_NLS;
604 cerr<<_FB_CONSOLETEXT(Menu, ErrorEndEncoding, "Warning: unbalanced [encoding] tags", "User menu file had unbalanced [encoding] tags")<<endl;
605 return;
606 }
607
608 encoding_stack.pop_back();
609 m_stringconvertor.reset();
610
611 list<string>::reverse_iterator it = encoding_stack.rbegin();
612 list<string>::reverse_iterator it_end = encoding_stack.rend();
613 while (it != it_end && !m_stringconvertor.setSource(*it))
614 ++it;
615
616 if (it == it_end)
617 m_stringconvertor.setSource("");
618}
619 623
diff --git a/src/MenuCreator.hh b/src/MenuCreator.hh
index a9fb649..91add47 100644
--- a/src/MenuCreator.hh
+++ b/src/MenuCreator.hh
@@ -35,37 +35,16 @@ class Menu;
35class FbMenu; 35class FbMenu;
36class FluxboxWindow; 36class FluxboxWindow;
37 37
38class MenuCreator { 38namespace MenuCreator {
39public: 39
40 static FbMenu *createMenu(const std::string &label, int screen_num); 40 FbMenu *createMenu(const std::string &label, int screen_num);
41 static FbMenu *createMenuType(const std::string &label, int screen_num); 41 FbMenu *createMenuType(const std::string &label, int screen_num);
42 static bool createFromFile(const std::string &filename, 42 bool createFromFile(const std::string &filename,
43 FbTk::Menu &inject_into, 43 FbTk::Menu &inject_into,
44 FbTk::AutoReloadHelper *reloader = NULL, 44 FbTk::AutoReloadHelper *reloader = NULL,
45 bool begin = true); 45 bool begin = true);
46 static bool createWindowMenuItem(const std::string &type, const std::string &label, 46 bool createWindowMenuItem(const std::string &type, const std::string &label,
47 FbTk::Menu &inject_into); 47 FbTk::Menu &inject_into);
48
49 /**
50 * Encoding-related helpers (encoding, aka codeset)
51 */
52
53 // Files are guaranteed to be "balanced", unlike user-created [encoding] tags.
54 static void startFile();
55 static void endFile();
56
57 static void startEncoding(const std::string &encoding);
58 static void endEncoding();
59
60private:
61 // stack of encodings
62 static std::list<std::string> encoding_stack;
63 // stack of ints, representing stack size as each file is entered
64 // (a file should never end more encodings than it starts)
65 static std::list<size_t> stacksize_stack;
66
67 static FbTk::StringConvertor m_stringconvertor;
68
69}; 48};
70 49
71#endif // MENUCREATOR_HH 50#endif // MENUCREATOR_HH