]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/xforms_helpers.C
xforms alert fixes
[lyx.git] / src / frontends / xforms / xforms_helpers.C
index 4872b9b84fd3c50c2faca5fbd50342a2b7e075fb..67055a5c1a5425e78446ff0ce11ee70d4a986a95 100644 (file)
@@ -1,68 +1,66 @@
 /**
  * \file xforms_helpers.C
- * Copyright 2000-2002 The LyX Team.
- * See the file COPYING.
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * \author Angus Leeming, a.leeming@ic.ac.uk
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS
  */
 
 #include <config.h>
 
-#include FORMS_H_LOCATION
-
-#include <fstream> // ofstream
-#include <vector>
-
-#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 <fstream>
+
+#include FORMS_H_LOCATION
+
 using std::ofstream;
 using std::pair;
 using std::vector;
+using std::make_pair;
 
-// Extract shortcut from <ident>|<shortcut> string
-char const * flyx_shortcut_extract(char const * sc)
+bool isActive(FL_OBJECT * ob)
 {
-       // 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;
-               return sd;
-       }
-       return "";
+       return ob && ob->active > 0;
 }
 
-
-// Extract identifier from <ident>|<shortcut> string
-char const * flyx_ident_extract(char const * sc)
+std::pair<string, string> parse_shortcut(string const & str)
 {
-       register char const * se = sc;
-       while (se[0]!= 0 && se[0] != '|') ++se;
+       string::size_type i = str.find_first_of("&");
+       if (i == string::npos || i == str.length() - 1)
+               return make_pair(str, string());
 
-       if (se[0] == 0) return sc;
+       // FIXME: handle &&
 
-       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;
+       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;
 }
 
 
@@ -71,7 +69,7 @@ void setEnabled(FL_OBJECT * ob, bool enable)
 {
        if (enable) {
                fl_activate_object(ob);
-               fl_set_object_lcol(ob, FL_BLACK);
+               fl_set_object_lcol(ob, FL_LCOL);
        } else {
                fl_deactivate_object(ob);
                fl_set_object_lcol(ob, FL_INACTIVE);
@@ -79,95 +77,82 @@ 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_input, return its contents.
-string const getStringFromInput(FL_OBJECT * ob)
+///
+string const getString(FL_OBJECT * ob, int line)
 {
-       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();
-}
+       // Negative line value does not make sense.
+       lyx::Assert(line >= 0);
 
-// 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();
+       char const * tmp = 0;
+       switch (ob->objclass) {
+       case FL_INPUT:
+               tmp = fl_get_input(ob);
+               break;
 
-       int const line = fl_get_browser(ob);
-       if (line < 1 || line > fl_get_browser_maxline(ob))
-               return string();
+       case FL_BROWSER:
+               if (line == 0)
+                       line = fl_get_browser(ob);
 
-       if (!fl_isselected_browser_line(ob, line))
-               return string();
+               if (line >= 1 && line <= fl_get_browser_maxline(ob))
+                       tmp = fl_get_browser_line(ob, line);
+               break;
 
-       char const * tmp = fl_get_browser_line(ob, line);
-       return (tmp) ? tmp : string();
-}
+       case FL_CHOICE:
+               if (line == 0)
+                       line = fl_get_choice(ob);
 
+               if (line >= 1 && line <= fl_get_choice_maxitems(ob))
+                       tmp = fl_get_choice_item_text(ob, line);
+               break;
 
-// 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)));
+       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();
 
-       //don't return unit-from-choice if the input(field) contains a unit
+       // 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)));
+       string unit = trim(fl_get_choice_text(choice));
        unit = subst(unit, "%%", "%");
 
        return length + unit;
@@ -183,7 +168,7 @@ void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
                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.
+               // No need to check for its presence in the choice, therefore.
                fl_set_choice_text(choice, default_unit.c_str());
        } else {
                updateWidgetsFromLength(input, choice,
@@ -200,7 +185,7 @@ void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
        lyx::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 {
@@ -212,7 +197,7 @@ void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
                // Else set the choice to the default unit.
                string const unit = subst(stringFromUnit(len.unit()),"%","%%");
 
-               vector<string> const vec = getVectorFromChoice(choice);
+               vector<string> const vec = getVector(choice);
                vector<string>::const_iterator it =
                        std::find(vec.begin(), vec.end(), unit);
                if (it != vec.end()) {
@@ -235,7 +220,7 @@ string formatted(string const & sin, int w, int size, int style)
        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);
+               string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
 
                string const word = nxtpos == string::npos ?
                        sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
@@ -362,18 +347,11 @@ bool XformsColor::write(string const & filename)
        if (!os)
                return false;
 
-       os << "### This file is part of\n"
-          << "### ========================================================\n"
-          << "###          LyX, The Document Processor\n"
-          << "###\n"
-          << "###          Copyright 1995 Matthias Ettrich\n"
-          << "###          Copyright 1995-2002 The LyX Team.\n"
-          << "###\n"
-          << "### ========================================================\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";
+       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';
 
        for (int i = 0; i < xformCount; ++i) {
                string const tag  = xformTags[i].tag;
@@ -382,8 +360,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;
@@ -397,18 +375,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.isOK() || !tp.isDir()) {
-               error_message = N_("Directory does not exist.");
+               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;
        }
 
@@ -421,18 +399,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.isOK() || !tp.isDir()) {
-               error_message = N_("Directory does not exist.");
+               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;
        }
 
@@ -450,13 +428,13 @@ 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;
        }
 
@@ -467,23 +445,23 @@ bool RWInfo::WriteableFile(string const & name)
        }
 
        if (!d.isOK() || !d.isDir()) {
-               error_message = N_("Directory does not exist.");
+               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.isOK() && f.isDir())) {
-               error_message = N_("A file is required, not a directory.");
+               error_message = _("A file is required, not a directory.");
                return false;
        }
 
        if (f.isOK() && f.exist() && !f.writable()) {
-               error_message = N_("Cannot write to this file.");
+               error_message = _("Cannot write to this file.");
                return false;
        }
 
@@ -496,13 +474,13 @@ 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;
        }
 
@@ -513,28 +491,28 @@ bool RWInfo::ReadableFile(string const & name)
        }
 
        if (!d.isOK() || !d.isDir()) {
-               error_message = N_("Directory does not exist.");
+               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.isOK() && f.isDir())) {
-               error_message = N_("A file is required, not a directory.");
+               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;
        }