]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/xforms_helpers.C
Bugfixes: checkboxes to radiobuttons (from J�rgen S) and remove a little
[lyx.git] / src / frontends / xforms / xforms_helpers.C
index 02d3a69e365cb5de410ccfcddc1fe6c2cd967491..4525e091482db11009691a2be37e657b19b949f5 100644 (file)
  
 #include "xforms_helpers.h"
 #include "lyxlex.h"
-#include "frontends/FileDialog.h"
 #include "support/FileInfo.h"
 #include "support/filetools.h"
-#include "lyx_gui_misc.h" // WriteAlert
+#include "support/lstrings.h" // frontStrip, strip
 #include "gettext.h"
+#include "support/LAssert.h"
+#include "lyxlength.h"
+#include "lyxgluelength.h"
 
 using std::ofstream;
 using std::pair;
 using std::vector;
 
+// Extract shortcut from <ident>|<shortcut> string
+char const * flyx_shortcut_extract(char const * sc)
+{
+       // Find '|' in the sc and return the string after that.
+       register char const * sd = sc;
+       while (sd[0]!= 0 && sd[0] != '|') ++sd;
+
+       if (sd[0] == '|') {
+               ++sd;
+               //lyxerr << sd << endl;
+               return sd;
+       }
+       return "";
+}
+
+
+// Extract identifier from <ident>|<shortcut> string
+char const * flyx_ident_extract(char const * sc)
+{
+       register char const * se = sc;
+       while (se[0]!= 0 && se[0] != '|') ++se;
+
+       if (se[0] == 0) return sc;
+       
+       char * sb = new char[se - sc + 1];
+       int index = 0;
+       register char const * sd = sc;
+       while (sd != se) {
+               sb[index] = sd[0];
+               ++index; ++sd;
+       }
+       sb[index] = 0;
+       return sb;
+}
+
+
 // Set an FL_OBJECT to activated or deactivated
 void setEnabled(FL_OBJECT * ob, bool enable)
 {
@@ -37,87 +75,278 @@ void setEnabled(FL_OBJECT * ob, bool enable)
 }
 
        
+// Given an fl_choice, create a vector of its entries
+vector<string> const getVectorFromChoice(FL_OBJECT * ob)
+{
+       vector<string> vec;
+       if (!ob || ob->objclass != FL_CHOICE)
+               return vec;
+
+       for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
+               string const text = fl_get_choice_item_text(ob, i+1);
+               vec.push_back(strip(frontStrip(text)));
+       }
+
+       return vec;
+}
+
+
+/// Given an fl_input, return its contents.
+string const getStringFromInput(FL_OBJECT * ob)
+{
+       if (!ob || ob->objclass != FL_INPUT)
+               return string();
+
+       char const * tmp = fl_get_input(ob);
+       return (tmp) ? tmp : string();
+}
+
+
+// Given an fl_browser, return the contents of line
+string const getStringFromBrowser(FL_OBJECT * ob, int line)
+{
+       if (!ob || ob->objclass != FL_BROWSER || 
+           line < 1 || line > fl_get_browser_maxline(ob))
+               return string();
+
+       char const * tmp = fl_get_browser_line(ob, line);
+       return (tmp) ? tmp : string();
+}
+
+// Given an fl_browser, return the contents of the currently
+// highlighted line.
+// If nothing is selected, return an empty string
+string const getSelectedStringFromBrowser(FL_OBJECT * ob)
+{
+       if (!ob || ob->objclass != FL_BROWSER)
+               return string();
+
+       int const line = fl_get_browser(ob);
+       if (line < 1 || line > fl_get_browser_maxline(ob))
+               return string();
+
+       if (!fl_isselected_browser_line(ob, line))
+               return string();
+
+       char const * tmp = fl_get_browser_line(ob, line);
+       return (tmp) ? tmp : string();
+}
+
+
+// Given an fl_browser, create a vector of its entries
+vector<string> const getVectorFromBrowser(FL_OBJECT * ob)
+{
+       vector<string> vec;
+       if (!ob || ob->objclass != FL_BROWSER)
+               return vec;
+
+       for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
+               string const text = fl_get_browser_line(ob, i+1);
+               vec.push_back(strip(frontStrip(text)));
+       }
+
+       return vec;
+}
+
+
+string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
+{
+       // Paranoia check
+       lyx::Assert(input  && input->objclass  == FL_INPUT &&
+                   choice && choice->objclass == FL_CHOICE);
+
+       string const length = strip(frontStrip(fl_get_input(input)));
+       if (length.empty())
+               return string();
+
+       //don't return unit-from-choice if the input(field) contains a unit
+       if (isValidGlueLength(length))
+               return length;
+
+       string unit = strip(frontStrip(fl_get_choice_text(choice)));
+       unit = subst(unit, "%%", "%");
+
+       return length + unit;
+}
+       
+
+#if 1
+// this should definitely be the other way around!!!
+void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
+                            LyXLength const & len,
+                            string const & default_unit)
+{
+       if (len.zero())
+               updateWidgetsFromLengthString(input, choice,
+                                             string(), default_unit);
+       else
+               updateWidgetsFromLengthString(input, choice,
+                                             len.asString(), default_unit);
+
+}
+
+
+// Most of the code here is a poor duplication of the parser code
+// which is in LyXLength. Use that instead
+void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
+                                  string const & str,
+                                  string const & default_unit)
+{
+       // Paranoia check
+       lyx::Assert(input  && input->objclass  == FL_INPUT &&
+                   choice && choice->objclass == FL_CHOICE);
+
+       if (str.empty()) {
+               fl_set_input(input, "");
+               int unitpos = 1; // xforms has Fortran-style indexing
+               for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
+                       string const text = fl_get_choice_item_text(choice,i+1);
+                       if (default_unit ==
+                           lowercase(strip(frontStrip(text)))) {
+                               unitpos = i+1;
+                               break;
+                       }
+               }
+               fl_set_choice(choice, unitpos);
+               return;
+       }
+
+       // The unit is presumed to begin at the first char a-z
+       string const tmp = lowercase(strip(frontStrip(str)));
+
+       string::const_iterator p = tmp.begin();
+       for (; p != tmp.end(); ++p) {
+               if (*p >= 'a' && *p <= 'z')
+                       break;
+       }
+
+       string len = "0";
+       int unitpos = 1; // xforms has Fortran-style indexing
+
+       if (p == tmp.end()) {
+               if (isStrDbl(tmp))
+                       len = tmp;
+
+       } else {
+               string tmplen = string(tmp.begin(), p);
+               if (isStrDbl(tmplen))
+                       len = tmplen;
+               string unit = string(p, tmp.end());
+               unit = subst(unit, "%", "%%");
+
+               for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
+                       string const text = fl_get_choice_item_text(choice,i+1);
+                       if (unit == lowercase(strip(frontStrip(text)))) {
+                               unitpos = i+1;
+                               break;
+                       }
+               }
+       }
+       
+       fl_set_input(input,   len.c_str());
+       fl_set_choice(choice, unitpos);
+}
+#else
+void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
+                                  string const & str,
+                                  string const & default_unit)
+{
+       updateWidgetsFromLength(input, choice,
+                               LyXLength(str), default_unit);
+}
+
+
+void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
+                            LyXLength const & len,
+                            string const & default_unit)
+{
+       // Paranoia check
+       lyx::Assert(input  && input->objclass  == FL_INPUT &&
+                   choice && choice->objclass == FL_CHOICE);
+
+       if (len.zero()) {
+               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_choice_text(choice, stringFromUnit(len.unit()));
+       }
+}
+#endif
+
+
 // Take a string and add breaks so that it fits into a desired label width, w
 string formatted(string const & sin, int w, int size, int style)
 {
+       // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
+
        string sout;
        if (sin.empty()) return sout;
 
-       // break sin up into a vector of individual words
+       // breakin up into a vector of individual words
        vector<string> sentence;
        string word;
        for (string::const_iterator sit = sin.begin();
             sit != sin.end(); ++sit) {
                if ((*sit) == ' ' || (*sit) == '\n') {
-                       sentence.push_back(word);
-                       word.erase();
+                       if (!word.empty()) {
+                               sentence.push_back(word);
+                               word.erase();
+                       }
+                       if ((*sit) == '\n') word += '\n';
+                       
                } else {
                        word += (*sit);
                }
        }
+
        // Flush remaining contents of word
        if (!word.empty() ) sentence.push_back(word);
 
-       string line, l1;
+       string line;
+       string line_plus_word;
        for (vector<string>::const_iterator vit = sentence.begin();
             vit != sentence.end(); ++vit) {
-               if (!l1.empty() ) l1 += ' ';
-               l1 += (*vit);
-               int length = fl_get_string_width(style, size, l1.c_str(),
-                                                int(l1.length()));
+               string word(*vit);
+
+               char c = word[0];
+               if (c == '\n') {
+                       sout += line + '\n';
+                       word.erase(0,1);
+                       line_plus_word.erase();
+                       line.erase();
+               }
+
+               if (!line_plus_word.empty() ) line_plus_word += ' ';
+               line_plus_word += word;
+
+               int const length = fl_get_string_width(style, size,
+                                                      line_plus_word.c_str(),
+                                                      int(line_plus_word.length()));
                if (length >= w) {
-                       if (!sout.empty() ) sout += '\n';
-                       sout += line;
-                       l1 = (*vit);
+                       sout += line + '\n';
+                       line_plus_word = word;
                }
 
-               line = l1;
+               line = line_plus_word;
        }
        // Flush remaining contents of line
        if (!line.empty()) {
-               if (!sout.empty() ) sout += '\n';
                sout += line;
        }
-       
-       return sout;
-}
-
-
-string const browseFile(LyXView * lv, string const & filename,
-                       string const & title,
-                       string const & pattern, 
-                       pair<string,string> const & dir1,
-                       pair<string,string> const & dir2)
-{
-       string lastPath = ".";
-       if (!filename.empty()) lastPath = OnlyPath(filename);
-
-       FileDialog fileDlg(lv, title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
-
-       FileDialog::Result result;
-       while (1) {
-               result = fileDlg.Select(lastPath, pattern, OnlyFilename(filename));
 
-               if (result.second.empty()) 
-                       return result.second;
+       if (sout[sout.length() - 1] == '\n')
+               sout.erase(sout.length() - 1);
 
-               lastPath = OnlyPath(result.second);
-
-               if (result.second.find_first_of("#~$% ") == string::npos)
-                       break; 
-               WriteAlert(_("Filename can't contain any "
-                       "of these characters:"),
-                       _("space, '#', '~', '$' or '%'."));
-       }
-
-       return result.second;
+       return sout;
 }
 
 
+namespace {
+
 // sorted by hand to prevent LyXLex from complaining on read().
-static
 keyword_item xformTags[] = {
        { "\\gui_background", FL_COL1 },
        { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
@@ -131,7 +360,9 @@ keyword_item xformTags[] = {
 };
 
 
-static const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
+const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
+
+} // namespace anon
 
 
 bool XformsColor::read(string const & filename)
@@ -140,7 +371,7 @@ bool XformsColor::read(string const & filename)
        if (!lexrc.setFile(filename))
                return false;
 
-       while (lexrc.IsOK()) {
+       while (lexrc.isOK()) {
                int const le = lexrc.lex();
 
                switch (le) {
@@ -155,13 +386,13 @@ bool XformsColor::read(string const & filename)
                RGBColor col;
 
                if (!lexrc.next()) break;
-               col.r = lexrc.GetInteger();
+               col.r = lexrc.getInteger();
 
                if (!lexrc.next()) break;
-               col.g = lexrc.GetInteger();
+               col.g = lexrc.getInteger();
 
                if (!lexrc.next()) break;
-               col.b = lexrc.GetInteger();
+               col.b = lexrc.getInteger();
 
                fl_mapcolor(le, col.r, col.g, col.b);
        }
@@ -181,7 +412,7 @@ bool XformsColor::write(string const & filename)
           << "###          LyX, The Document Processor\n"
           << "###\n"
           << "###          Copyright 1995 Matthias Ettrich\n"
-          << "###          Copyright 1995-2000 The LyX Team.\n"
+          << "###          Copyright 1995-2001 The LyX Team.\n"
           << "###\n"
           << "### ========================================================\n"
           << "\n"
@@ -216,7 +447,7 @@ bool RWInfo::WriteableDir(string const & name)
        }
 
        FileInfo const tp(name);
-       if (!tp.isDir()) {
+       if (!tp.isOK() || !tp.isDir()) {
                error_message = N_("Directory does not exist.");
                return false;
        }
@@ -240,7 +471,7 @@ bool RWInfo::ReadableDir(string const & name)
        }
 
        FileInfo const tp(name);
-       if (!tp.isDir()) {
+       if (!tp.isOK() || !tp.isDir()) {
                error_message = N_("Directory does not exist.");
                return false;
        }
@@ -275,11 +506,12 @@ bool RWInfo::WriteableFile(string const & name)
        }
 
        FileInfo d(name);
-       if (!d.isDir()) {
+
+       if (!d.isOK() || !d.isDir()) {
                d.newFile(dir);
        }
 
-       if (!d.isDir()) {
+       if (!d.isOK() || !d.isDir()) {
                error_message = N_("Directory does not exist.");
                return false;
        }
@@ -290,12 +522,12 @@ bool RWInfo::WriteableFile(string const & name)
        }
 
        FileInfo f(name);
-       if (dir == name || f.isDir()) {
+       if (dir == name || (f.isOK() && f.isDir())) {
                error_message = N_("A file is required, not a directory.");
                return false;
        }
 
-       if (f.exist() && !f.writable()) {
+       if (f.isOK() && f.exist() && !f.writable()) {
                error_message = N_("Cannot write to this file.");
                return false;
        }
@@ -320,11 +552,12 @@ bool RWInfo::ReadableFile(string const & name)
        }
 
        FileInfo d(name);
-       if (!d.isDir()) {
+
+       if (!d.isOK() && !d.isDir()) {
                d.newFile(dir);
        }
 
-       if (!d.isDir()) {
+       if (!d.isOK() || !d.isDir()) {
                error_message = N_("Directory does not exist.");
                return false;
        }
@@ -335,7 +568,7 @@ bool RWInfo::ReadableFile(string const & name)
        }
 
        FileInfo f(name);
-       if (dir == name || f.isDir()) {
+       if (dir == name || (f.isOK() && f.isDir())) {
                error_message = N_("A file is required, not a directory.");
                return false;
        }