]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/xforms_helpers.C
Nothing but a changed email address (trying to factor my tree in in small steps)
[lyx.git] / src / frontends / xforms / xforms_helpers.C
index 9e1373e08ca72fdf0cd68da3258d7158fa1a22d1..dafce7d8a9342540b76713aefe0e7991a4cd4abe 100644 (file)
@@ -1,65 +1,38 @@
-/** Collection of some useful xform helper functions
+/**
+ * \file xforms_helpers.C
+ * Copyright 2000-2002 The LyX Team.
+ * See the file COPYING.
+ *
+ * \author Angus Leeming <leeming@lyx.org>
  */
 
 #include <config.h>
 
-#include FORMS_H_LOCATION
-
-#include <fstream> // ofstream
-#include <vector>
-
-#ifdef __GNUG_
+#ifdef __GNUG__
 #pragma implementation
 #endif
+
 #include "xforms_helpers.h"
+
 #include "lyxlex.h"
+#include "gettext.h"
+#include "lyxlength.h"
+#include "lyxgluelength.h"
+
+#include "support/LAssert.h"
 #include "support/FileInfo.h"
 #include "support/filetools.h"
 #include "support/lstrings.h" // frontStrip, strip
-#include "gettext.h"
-#include "support/LAssert.h"
+
+#include <algorithm>
+#include <fstream>
+#include <vector>
+#include FORMS_H_LOCATION
 
 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)
 {
@@ -72,195 +45,219 @@ void setEnabled(FL_OBJECT * ob, bool enable)
        }
 }
 
-       
-// Given an fl_choice, create a vector of its entries
-vector<string> const getVectorFromChoice(FL_OBJECT * ob)
+
+// Given an fl_choice or an fl_browser, create a vector of its entries
+vector<string> const getVector(FL_OBJECT * ob)
 {
-       vector<string> vec;
-       if (!ob || ob->objclass != FL_CHOICE)
-               return vec;
+       vector <string> 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)));
+       switch (ob->objclass) {
+       case FL_CHOICE:
+               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(trim(text));
+               }
+               break;
+       case FL_BROWSER:
+               for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
+                       string const text = fl_get_browser_line(ob, i+1);
+                       vec.push_back(trim(text));
+               }
+               break;
+       default:
+               lyx::Assert(0);
        }
 
        return vec;
 }
 
 
-// Given an fl_browser, create a vector of its entries
-vector<string> const getVectorFromBrowser(FL_OBJECT * ob)
+///
+string const getString(FL_OBJECT * ob, int line)
 {
-       vector<string> vec;
-       if (!ob || ob->objclass != FL_BROWSER)
-               return vec;
+       char const * tmp = 0;
+
+       switch (ob->objclass) {
+       case FL_INPUT:
+               lyx::Assert(line == -1);
+               tmp = fl_get_input(ob);
+               break;
+       case FL_BROWSER:
+               if (line == -1)
+                       line = fl_get_browser(ob);
+
+               if (line >= 1 && line <= fl_get_browser_maxline(ob))
+                       tmp = fl_get_browser_line(ob, line);
+               break;
 
-       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)));
+       case FL_CHOICE:
+               if (line == -1)
+                       line = fl_get_choice(ob);
+
+               if (line >= 1 && line <= fl_get_choice_maxitems(ob))
+                       tmp = fl_get_choice_item_text(ob, line);
+               break;
+
+       default:
+               lyx::Assert(0);
        }
 
-       return vec;
+       return (tmp) ? trim(tmp) : string();
 }
 
-
 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)));
+       string const length = trim(fl_get_input(input));
        if (length.empty())
                return string();
 
-       string unit = strip(frontStrip(fl_get_choice_text(choice)));
+       //don't return unit-from-choice if the input(field) contains a unit
+       if (isValidGlueLength(length))
+               return length;
+
+       string unit = trim(fl_get_choice_text(choice));
        unit = subst(unit, "%%", "%");
 
        return length + unit;
 }
-       
+
 
 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
                                   string const & str,
                                   string const & default_unit)
+{
+       // use input field only for gluelengths
+       if (!isValidLength(str) && !isStrDbl(str)) {
+               fl_set_input(input, str.c_str());
+               // we assume that "default_unit" is in the choice as "we"
+               // have control over that!
+               // No need to check for it's precence in the choice, therefore.
+               fl_set_choice_text(choice, default_unit.c_str());
+       } else {
+               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 (str.empty()) {
+       if (len.zero()) {
                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;
-
+               fl_set_choice_text(choice, default_unit.c_str());
        } 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;
-                       }
+               ostringstream buffer;
+               buffer << len.value();
+               fl_set_input(input, buffer.str().c_str());
+
+               // Set the choice to the desired unit, if present in the choice.
+               // Else set the choice to the default unit.
+               string const unit = subst(stringFromUnit(len.unit()),"%","%%");
+
+               vector<string> const vec = getVector(choice);
+               vector<string>::const_iterator it =
+                       std::find(vec.begin(), vec.end(), unit);
+               if (it != vec.end()) {
+                       fl_set_choice_text(choice, unit.c_str());
+               } else {
+                       fl_set_choice_text(choice, default_unit.c_str());
                }
        }
-       
-       fl_set_input(input,   len.c_str());
-       fl_set_choice(choice, unitpos);
 }
+
+
 // 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;
 
-       // breaks in 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') {
-                       if (!word.empty()) {
-                               sentence.push_back(word);
-                               word.erase();
-                       }
-                       if ((*sit) == '\n') word += '\n';
-                       
-               } else {
-                       word += (*sit);
-               }
-       }
+       string::size_type curpos = 0;
+       string line;
+       for(;;) {
+               string::size_type const nxtpos1 = sin.find(' ',  curpos);
+               string::size_type const nxtpos2 = sin.find('\n', curpos);
+               string::size_type const nxtpos = std::min(nxtpos1, nxtpos1);
 
-       // Flush remaining contents of word
-       if (!word.empty() ) sentence.push_back(word);
+               string const word = nxtpos == string::npos ?
+                       sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
 
-       string line;
-       string line_plus_word;
-       for (vector<string>::const_iterator vit = sentence.begin();
-            vit != sentence.end(); ++vit) {
-               string word(*vit);
+               bool const newline = (nxtpos2 != string::npos &&
+                                     nxtpos2 < nxtpos1);
 
-               char c = word[0];
-               if (c == '\n') {
-                       sout += line + '\n';
-                       word.erase(0,1);
-                       line_plus_word.erase();
-                       line.erase();
-               }
+               string const line_plus_word =
+                       line.empty() ? word : line + ' ' + word;
 
-               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()));
 
-               int const length = fl_get_string_width(style, size,
-                                                      line_plus_word.c_str(),
-                                                      int(line_plus_word.length()));
                if (length >= w) {
                        sout += line + '\n';
-                       line_plus_word = word;
+                       if (newline) {
+                               sout += word + '\n';
+                               line.erase();
+                       } else {
+                               line = word;
+                       }
+
+               } else if (newline) {
+                       sout += line_plus_word + '\n';
+                       line.erase();
+
+               } else {
+                       if (!line.empty())
+                               line += ' ';
+                       line += word;
                }
 
-               line = line_plus_word;
-       }
-       // Flush remaining contents of line
-       if (!line.empty()) {
-               sout += line;
-       }
+               if (nxtpos == string::npos) {
+                       if (!line.empty())
+                               sout += line;
+                       break;
+               }
 
-       if (sout[sout.length() - 1] == '\n')
-               sout.erase(sout.length() - 1);
+               curpos = nxtpos+1;
+       }
 
        return sout;
 }
 
 
+void setCursorColor(int color)
+{
+       fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
+       fl_set_cursor_color(XC_xterm,          color, FL_WHITE);
+       fl_set_cursor_color(XC_watch,          color, FL_WHITE);
+       fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
+}
+
+
 namespace {
 
 // sorted by hand to prevent LyXLex from complaining on read().
 keyword_item xformTags[] = {
-       { "\\gui_background", FL_COL1 },
+       { "\\gui_background",   FL_COL1 },
        { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
-       { "\\gui_buttonleft", FL_LEFT_BCOL },
-       { "\\gui_buttonright", FL_RIGHT_BCOL },
-       { "\\gui_buttontop", FL_TOP_BCOL },
-       { "\\gui_inactive", FL_INACTIVE },
-       { "\\gui_push_button", FL_YELLOW },
-       { "\\gui_selected", FL_MCOL },  
-       { "\\gui_text", FL_BLACK }
+       { "\\gui_buttonleft",   FL_LEFT_BCOL },
+       { "\\gui_buttonright",  FL_RIGHT_BCOL },
+       { "\\gui_buttontop",    FL_TOP_BCOL },
+       { "\\gui_inactive",     FL_INACTIVE },
+       { "\\gui_pointer",      FL_FREE_COL16 },
+       { "\\gui_push_button",  FL_YELLOW },
+       { "\\gui_selected",     FL_MCOL },
+       { "\\gui_text",         FL_BLACK }
 };
 
 
@@ -281,12 +278,14 @@ bool XformsColor::read(string const & filename)
                switch (le) {
                case LyXLex::LEX_UNDEF:
                        lexrc.printError("Unknown tag `$$Token'");
-                       continue; 
+                       continue;
                case LyXLex::LEX_FEOF:
                        continue;
                default: break;
                }
 
+               string const tag = lexrc.getString();
+
                RGBColor col;
 
                if (!lexrc.next()) break;
@@ -299,8 +298,12 @@ bool XformsColor::read(string const & filename)
                col.b = lexrc.getInteger();
 
                fl_mapcolor(le, col.r, col.g, col.b);
+
+               if (tag == "\\gui_pointer") {
+                       setCursorColor(FL_FREE_COL16);
+               }
        }
-               
+
        return true;
 }
 
@@ -316,7 +319,7 @@ bool XformsColor::write(string const & filename)
           << "###          LyX, The Document Processor\n"
           << "###\n"
           << "###          Copyright 1995 Matthias Ettrich\n"
-          << "###          Copyright 1995-2001 The LyX Team.\n"
+          << "###          Copyright 1995-2002 The LyX Team.\n"
           << "###\n"
           << "### ========================================================\n"
           << "\n"
@@ -346,18 +349,18 @@ bool RWInfo::WriteableDir(string const & name)
        error_message.erase();
 
        if (!AbsolutePath(name)) {
-               error_message = N_("The absolute path is required.");
+               error_message = _("The absolute path is required.");
                return false;
        }
 
        FileInfo const tp(name);
-       if (!tp.isDir()) {
-               error_message = N_("Directory does not exist.");
+       if (!tp.isOK() || !tp.isDir()) {
+               error_message = _("Directory does not exist.");
                return false;
        }
 
        if (!tp.writable()) {
-               error_message = N_("Cannot write to this directory.");
+               error_message = _("Cannot write to this directory.");
                return false;
        }
 
@@ -370,18 +373,18 @@ bool RWInfo::ReadableDir(string const & name)
        error_message.erase();
 
        if (!AbsolutePath(name)) {
-               error_message = N_("The absolute path is required.");
+               error_message = _("The absolute path is required.");
                return false;
        }
 
        FileInfo const tp(name);
-       if (!tp.isDir()) {
-               error_message = N_("Directory does not exist.");
+       if (!tp.isOK() || !tp.isDir()) {
+               error_message = _("Directory does not exist.");
                return false;
        }
 
        if (!tp.readable()) {
-               error_message = N_("Cannot read this directory.");
+               error_message = _("Cannot read this directory.");
                return false;
        }
 
@@ -399,42 +402,43 @@ bool RWInfo::WriteableFile(string const & name)
        error_message.erase();
 
        if (name.empty()) {
-               error_message = N_("No file input.");
+               error_message = _("No file input.");
                return false;
        }
 
        string const dir = OnlyPath(name);
        if (!AbsolutePath(dir)) {
-               error_message = N_("The absolute path is required.");
+               error_message = _("The absolute path is required.");
                return false;
        }
 
        FileInfo d(name);
-       if (!d.isDir()) {
+
+       if (!d.isOK() || !d.isDir()) {
                d.newFile(dir);
        }
 
-       if (!d.isDir()) {
-               error_message = N_("Directory does not exist.");
+       if (!d.isOK() || !d.isDir()) {
+               error_message = _("Directory does not exist.");
                return false;
        }
-       
+
        if (!d.writable()) {
-               error_message = N_("Cannot write to this directory.");
+               error_message = _("Cannot write to this directory.");
                return false;
        }
 
        FileInfo f(name);
-       if (dir == name || f.isDir()) {
-               error_message = N_("A file is required, not a directory.");
+       if (dir == name || (f.isOK() && f.isDir())) {
+               error_message = _("A file is required, not a directory.");
                return false;
        }
 
-       if (f.exist() && !f.writable()) {
-               error_message = N_("Cannot write to this file.");
+       if (f.isOK() && f.exist() && !f.writable()) {
+               error_message = _("Cannot write to this file.");
                return false;
        }
-       
+
        return true;
 }
 
@@ -444,44 +448,45 @@ bool RWInfo::ReadableFile(string const & name)
        error_message.erase();
 
        if (name.empty()) {
-               error_message = N_("No file input.");
+               error_message = _("No file input.");
                return false;
        }
 
        string const dir = OnlyPath(name);
        if (!AbsolutePath(dir)) {
-               error_message = N_("The absolute path is required.");
+               error_message = _("The absolute path is required.");
                return false;
        }
 
        FileInfo d(name);
-       if (!d.isDir()) {
+
+       if (!d.isOK() && !d.isDir()) {
                d.newFile(dir);
        }
 
-       if (!d.isDir()) {
-               error_message = N_("Directory does not exist.");
+       if (!d.isOK() || !d.isDir()) {
+               error_message = _("Directory does not exist.");
                return false;
        }
-       
+
        if (!d.readable()) {
-               error_message = N_("Cannot read from this directory.");
+               error_message = _("Cannot read from this directory.");
                return false;
        }
 
        FileInfo f(name);
-       if (dir == name || f.isDir()) {
-               error_message = N_("A file is required, not a directory.");
+       if (dir == name || (f.isOK() && f.isDir())) {
+               error_message = _("A file is required, not a directory.");
                return false;
        }
 
        if (!f.exist()) {
-               error_message = N_("File does not exist.");
+               error_message = _("File does not exist.");
                return false;
        }
-       
+
        if (!f.readable()) {
-               error_message = N_("Cannot read from this file.");
+               error_message = _("Cannot read from this file.");
                return false;
        }