]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/xforms_helpers.C
get rid of broken_header.h and some unneeded tests
[lyx.git] / src / frontends / xforms / xforms_helpers.C
index fab98d9d8d704ed4687613ab726da3918fc93d3f..50fb6409d4fb85485fd65cbb49a8008bd56552fa 100644 (file)
@@ -5,36 +5,59 @@
  *
  * \author Angus Leeming
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 #include "xforms_helpers.h"
 
-#include "lyxlex.h"
+#include "debug.h"
 #include "gettext.h"
-#include "lyxlength.h"
+#include "lengthcommon.h"
 #include "lyxgluelength.h"
+#include "lyxlex.h"
 
-#include "support/LAssert.h"
 #include "support/FileInfo.h"
 #include "support/filetools.h"
 #include "support/lstrings.h" // frontStrip, strip
+#include "support/tostr.h"
+
+#include "lyx_forms.h"
+#include "combox.h"
+
+#include <boost/assert.hpp>
 
-#include <algorithm>
 #include <fstream>
-#include <vector>
 
-#include FORMS_H_LOCATION
+using std::make_pair;
 
 using std::ofstream;
-using std::pair;
 using std::vector;
+using std::string;
+
+namespace lyx {
+
+using support::AbsolutePath;
+using support::FileInfo;
+using support::isStrDbl;
+using support::OnlyPath;
+using support::subst;
+using support::trim;
+
+namespace frontend {
+
+std::string const buildChoiceLengthString()
+{
+       string data;
+       for (int i = 0; i != num_units; ++i) {
+               if (i != 0)
+                       data += "|";
+               data += subst(unit_name_gui[i], "%", "%%");
+       }
+       return data;
+}
+
 
 bool isActive(FL_OBJECT * ob)
 {
@@ -42,6 +65,33 @@ bool isActive(FL_OBJECT * ob)
 }
 
 
+std::pair<string, string> parse_shortcut(string const & str)
+{
+       string::size_type i = str.find_first_of("&");
+       if (i == string::npos || i == str.length() - 1)
+               return make_pair(str, string());
+
+       // FIXME: handle &&
+
+       string::value_type c = str[i + 1];
+       return make_pair(str.substr(0, i) + str.substr(i + 1),
+                        string("#") + c);
+}
+
+
+// A wrapper for the xforms routine, but this one accepts uint args
+unsigned long fl_getmcolor(int i,
+                          unsigned int * r, unsigned int * g, unsigned int * b)
+{
+       int r2, g2, b2;
+       unsigned long ret_val = ::fl_getmcolor(i, &r2, &g2, &b2);
+       *r = r2;
+       *g = g2;
+       *b = b2;
+       return ret_val;
+}
+
+
 // Set an FL_OBJECT to activated or deactivated
 void setEnabled(FL_OBJECT * ob, bool enable)
 {
@@ -74,7 +124,7 @@ vector<string> const getVector(FL_OBJECT * ob)
                }
                break;
        default:
-               lyx::Assert(0);
+               BOOST_ASSERT(false);
        }
 
        return vec;
@@ -85,7 +135,7 @@ vector<string> const getVector(FL_OBJECT * ob)
 string const getString(FL_OBJECT * ob, int line)
 {
        // Negative line value does not make sense.
-       lyx::Assert(line >= 0);
+       BOOST_ASSERT(line >= 0);
 
        char const * tmp = 0;
        switch (ob->objclass) {
@@ -109,8 +159,12 @@ string const getString(FL_OBJECT * ob, int line)
                        tmp = fl_get_choice_item_text(ob, line);
                break;
 
+       case FL_COMBOX:
+               tmp = fl_get_combox_text(ob);
+               break;
+
        default:
-               lyx::Assert(0);
+               BOOST_ASSERT(false);
        }
 
        return tmp ? trim(tmp) : string();
@@ -119,7 +173,7 @@ string const getString(FL_OBJECT * ob, int line)
 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
 {
        // Paranoia check
-       lyx::Assert(input  && input->objclass  == FL_INPUT &&
+       BOOST_ASSERT(input  && input->objclass  == FL_INPUT &&
                    choice && choice->objclass == FL_CHOICE);
 
        string const length = trim(fl_get_input(input));
@@ -141,6 +195,10 @@ void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
                                   string const & str,
                                   string const & default_unit)
 {
+       // Paranoia check
+       BOOST_ASSERT(input  && input->objclass  == FL_INPUT &&
+                    choice && choice->objclass == FL_CHOICE);
+
        // use input field only for gluelengths
        if (!isValidLength(str) && !isStrDbl(str)) {
                fl_set_input(input, str.c_str());
@@ -150,7 +208,7 @@ void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
                fl_set_choice_text(choice, default_unit.c_str());
        } else {
                updateWidgetsFromLength(input, choice,
-                               LyXLength(str), default_unit);
+                                       LyXLength(str), default_unit);
        }
 }
 
@@ -160,16 +218,14 @@ void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
                             string const & default_unit)
 {
        // Paranoia check
-       lyx::Assert(input  && input->objclass  == FL_INPUT &&
-                   choice && choice->objclass == FL_CHOICE);
+       BOOST_ASSERT(input  && input->objclass  == FL_INPUT &&
+                    choice && choice->objclass == FL_CHOICE);
 
-       if (len.zero()) {
+       if (len.empty()) {
                fl_set_input(input, "");
                fl_set_choice_text(choice, default_unit.c_str());
        } else {
-               ostringstream buffer;
-               buffer << len.value();
-               fl_set_input(input, buffer.str().c_str());
+               fl_set_input(input, tostr(len.value()).c_str());
 
                // Set the choice to the desired unit, if present in the choice.
                // Else set the choice to the default unit.
@@ -191,7 +247,8 @@ void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
 string formatted(string const & sin, int w, int size, int style)
 {
        string sout;
-       if (sin.empty()) return sout;
+       if (sin.empty())
+               return sout;
 
        string::size_type curpos = 0;
        string line;
@@ -279,9 +336,13 @@ const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
 
 bool XformsColor::read(string const & filename)
 {
+       FileInfo const f(filename);
        LyXLex lexrc(xformTags, xformCount);
-       if (!lexrc.setFile(filename))
+       if (f.readable() && !lexrc.setFile(filename)) {
+               lyxerr << "XformsColor::read(" << filename << ")\n"
+                      << _("Failed to open file.") << std::endl;
                return false;
+       }
 
        while (lexrc.isOK()) {
                int const le = lexrc.lex();
@@ -322,14 +383,17 @@ bool XformsColor::read(string const & filename)
 bool XformsColor::write(string const & filename)
 {
        ofstream os(filename.c_str());
-       if (!os)
+       if (!os) {
+               lyxerr << "XformsColor::write(" << filename << ")\n"
+                      << _("Failed to open file.") << std::endl;
                return false;
+       }
 
        os << "###"
           << "### file " << filename << "\n\n"
           << "### This file is written by LyX, if you want to make your own\n"
           << "### modifications you should do them from inside LyX and save\n"
-          << "\n";
+          << '\n';
 
        for (int i = 0; i < xformCount; ++i) {
                string const tag  = xformTags[i].tag;
@@ -338,8 +402,8 @@ bool XformsColor::write(string const & filename)
 
                fl_getmcolor(colorID, &color.r, &color.g, &color.b);
 
-               os << tag << " "
-                  << color.r << " " << color.g << " " << color.b << "\n";
+               os << tag << ' '
+                  << color.r << ' ' << color.g << ' ' << color.b << '\n';
        }
 
        return true;
@@ -496,3 +560,6 @@ bool RWInfo::ReadableFile(string const & name)
 
        return true;
 }
+
+} // namespace frontend
+} // namespace lyx