]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/FormDocument.C
Change glob() API to accept a dir parameter.
[lyx.git] / src / frontends / xforms / FormDocument.C
index a9e0506614affe15e9cb0f9dbd2199988a18fb07..21a5dfaeca2009a75db139d96cfdfa84f870e617 100644 (file)
 #include <config.h>
 
 #include "FormDocument.h"
-#include "ControlDocument.h"
 #include "forms/form_document.h"
 
+#include "controllers/ControlDocument.h"
+
 #include "bmtable.h"
 #include "checkedwidgets.h"
 #include "ColorHandler.h"
 #include "Bullet.h"
 #include "bufferparams.h"
 #include "language.h"
-#include "LColor.h"
 #include "lyxrc.h"
 #include "lyxtextclasslist.h"
 #include "tex-strings.h"
 #include "vspace.h"
 
 #include "support/tostr.h"
-#include "support/lstrings.h" // contains_functor, getStringFromVector
+#include "support/lstrings.h" // contains, getStringFromVector
 #include "support/filetools.h" // LibFileSearch
 
 #include "lyx_xpm.h"
 
+#include <boost/bind.hpp>
+
 #include <iomanip>
 
-using lyx::support::bformat;
-using lyx::support::contains_functor;
-using lyx::support::getStringFromVector;
-using lyx::support::getVectorFromString;
-using lyx::support::LibFileSearch;
+using boost::bind;
 
-using std::bind2nd;
 using std::endl;
-
+using std::string;
 using std::vector;
 
+namespace lyx {
+
+using support::bformat;
+using support::contains;
+using support::getStringFromVector;
+using support::getVectorFromString;
+using support::LibFileSearch;
+
+namespace frontend {
 
 namespace {
 
@@ -68,14 +74,23 @@ bool const scalableTabfolders = false;
 bool const scalableTabfolders = true;
 #endif
 
+enum {
+       DEFCITE,
+       NATBIB,
+       JURABIB
+};
+
+enum GuiColors {
+       GUI_COLOR_CHOICE = FL_FREE_COL15
+};
 
 } // namespace anon
 
 
-typedef FormCB<ControlDocument, FormDB<FD_document> > base_class;
+typedef FormController<ControlDocument, FormView<FD_document> > base_class;
 
-FormDocument::FormDocument()
-       : base_class(_("Document Settings"), scalableTabfolders),
+FormDocument::FormDocument(Dialog & parent)
+       : base_class(parent, _("Document Settings"), scalableTabfolders),
          ActCell(0), Confirmed(0),
          current_bullet_panel(0), current_bullet_depth(0), fbullet(0)
 {}
@@ -170,7 +185,7 @@ void FormDocument::build()
        vector<string>::iterator ret =
                std::remove_if(units_vec.begin(),
                               units_vec.end(),
-                              bind2nd(contains_functor(), "%"));
+                              bind(contains<char>, _1, '%'));
        units_vec.erase(ret, units_vec.end());
 
        string const units = getStringFromVector(units_vec, "|");
@@ -268,19 +283,17 @@ void FormDocument::build()
        bcview().addReadOnly(language_->combox_language);
        bcview().addReadOnly(language_->choice_inputenc);
        bcview().addReadOnly(language_->choice_quotes_language);
-       bcview().addReadOnly(language_->radio_single);
-       bcview().addReadOnly(language_->radio_double);
 
        fl_addto_choice(language_->choice_inputenc,
                        "default|auto|latin1|latin2|latin3|latin4|latin5|latin9"
                        "|koi8-r|koi8-u|cp866|cp1251|iso88595|pt154");
 
-       vector<frnt::LanguagePair> const langs = frnt::getLanguageData(false);
+       vector<LanguagePair> const langs = getLanguageData(false);
        // Store the identifiers for later
        lang_ = getSecond(langs);
 
-       vector<frnt::LanguagePair>::const_iterator lit  = langs.begin();
-       vector<frnt::LanguagePair>::const_iterator lend = langs.end();
+       vector<LanguagePair>::const_iterator lit  = langs.begin();
+       vector<LanguagePair>::const_iterator lend = langs.end();
        for (; lit != lend; ++lit) {
                fl_addto_combox(language_->combox_language,
                                lit->first.c_str());
@@ -289,7 +302,7 @@ void FormDocument::build()
 
        fl_addto_choice(language_->choice_quotes_language,
                        _(" ``text'' | ''text'' | ,,text`` | ,,text'' |"
-                         " «text» | »text« ").c_str());
+                         " <<text>> | >>text<< ").c_str());
 
        // the document options form
        options_.reset(build_document_options(this));
@@ -298,25 +311,34 @@ void FormDocument::build()
        bcview().addReadOnly(options_->counter_secnumdepth);
        bcview().addReadOnly(options_->counter_tocdepth);
        bcview().addReadOnly(options_->choice_ams_math);
-       bcview().addReadOnly(options_->check_use_natbib);
-       bcview().addReadOnly(options_->choice_citation_format);
+       bcview().addReadOnly(options_->choice_cite_engine);
+       bcview().addReadOnly(options_->check_bibtopic);
        bcview().addReadOnly(options_->input_float_placement);
        bcview().addReadOnly(options_->choice_postscript_driver);
 
+       string const cite_choices =
+               _(" Basic | Natbib author-year | Natbib numerical | Jurabib ");
+       fl_addto_choice(options_->choice_cite_engine, cite_choices.c_str());
+
+       // set up the tooltips for optionss form
+       string str = _("Natbib is used often for natural sciences and arts\n"
+               "Jurabib is more common in law and humanities");
+       tooltips().init(options_->choice_cite_engine, str);
+       str = _("Select this if you want to split your bibliography into sections");
+       tooltips().init(options_->check_bibtopic, str);
+
        // trigger an input event for cut&paste with middle mouse button.
        setPrehandler(options_->input_float_placement);
 
        fl_set_input_return(options_->input_float_placement, FL_RETURN_CHANGED);
 
        fl_addto_choice(options_->choice_ams_math,
-                       _("Never | Automatically | Yes ").c_str());
+                       _(" Never | Automatically | Yes ").c_str());
 
        for (int n = 0; tex_graphics[n][0]; ++n) {
                fl_addto_choice(options_->choice_postscript_driver,
                                tex_graphics[n]);
        }
-       fl_addto_choice(options_->choice_citation_format,
-                       _(" Author-year | Numerical ").c_str());
 
        // the document bullets form
        bullets_.reset(build_document_bullet(this));
@@ -370,7 +392,7 @@ void FormDocument::build()
        bcview().addReadOnly(branch_->browser_all_branches);
 
        // set up the tooltips for branches form
-       string str = _("Enter the name of a new branch.");
+       str = _("Enter the name of a new branch.");
        tooltips().init(branch_->input_all_branches, str);
        str = _("Add a new branch to the document.");
        tooltips().init(branch_->button_add_branch, str);
@@ -507,17 +529,12 @@ ButtonPolicy::SMInput FormDocument::input(FL_OBJECT * ob, long)
                        fl_set_choice_text(class_->choice_skip_units,
                                           default_unit.c_str());
 
-       } else if (ob == options_->check_use_natbib) {
-               setEnabled(options_->choice_citation_format,
-                          fl_get_button(options_->check_use_natbib));
-
        } else if (ob == branch_->browser_all_branches ||
                        ob == branch_->browser_selection ||
                        ob == branch_->button_add_branch ||
                        ob == branch_->button_remove_branch ||
                        ob == branch_->button_modify ||
                        ob == branch_->button_select ||
-                       ob == branch_->button_deselect ||
                        ob == branch_->button_deselect) {
                branch_input(ob);
        } else if (ob == dialog_->button_save_defaults) {
@@ -670,123 +687,130 @@ ButtonPolicy::SMInput FormDocument::input(FL_OBJECT * ob, long)
 }
 
 
-void FormDocument::branch_input(FL_OBJECT * ob)
+void FormDocument::rebuild_all_branches_browser()
 {
-       BufferParams & params = controller().params();
-       std::vector<string> vec;
+       typedef BranchList::const_iterator const_iterator;
+
+       fl_clear_browser(branch_->browser_all_branches);
+
+       const_iterator const begin = branchlist_.begin();
+       const_iterator const end = branchlist_.end();
+       for (const_iterator it = begin; it != end; ++it) {
+               fl_addto_browser(branch_->browser_all_branches,
+                                it->getBranch().c_str());
+       }
+}
+
+
+void FormDocument::rebuild_selected_branches_browser()
+{
+       typedef BranchList::const_iterator const_iterator;
+
+       fl_clear_browser(branch_->browser_selection);
+
+       const_iterator const begin = branchlist_.begin();
+       const_iterator const end = branchlist_.end();
+       for (const_iterator it = begin; it != end; ++it) {
+               if (it->getSelected())
+                       fl_addto_browser(branch_->browser_selection,
+                                        it->getBranch().c_str());
+       }
+}
+
 
+namespace {
+
+RGBColor get_current_color(FL_OBJECT * browser, BranchList const & branchlist)
+{
+       BOOST_ASSERT(browser && browser->objclass == FL_BROWSER);
+
+       RGBColor color;
+
+       int const i = fl_get_browser(browser);
+       string const branch_name = fl_get_browser_line(browser, i);
+       Branch const * branch = branchlist.find(branch_name);
+       if (!branch)
+               return color;
+
+       string const x11hexname = branch->getColor();
+       if (x11hexname[0] == '#') {
+               color = RGBColor(x11hexname);
+       } else{
+               fl_getmcolor(FL_COL1, &color.r, &color.g, &color.b);
+       }
+       return color;
+}
+
+} // namespace anon
+
+
+void FormDocument::branch_input(FL_OBJECT * ob)
+{
        if (ob == branch_->button_add_branch) {
-               string new_branch = fl_get_input(branch_->input_all_branches);
-               if (!new_branch.empty()) {
-                       params.branchlist().add(new_branch);
+               string const new_branch =
+                       getString(branch_->input_all_branches);
+
+               if (!new_branch.empty() && branchlist_.add(new_branch)) {
+
                        fl_set_input(branch_->input_all_branches, "");
-                       // Update branch list
-                       string const all_branches = params.branchlist().allBranches();
-                       fl_clear_browser(branch_->browser_all_branches);
-                       vec = getVectorFromString(all_branches, "|");
-                       for (unsigned i = 0; i < vec.size(); ++i) {
-                               fl_addto_browser(branch_->browser_all_branches,
-                                                               vec[i].c_str());
-                       }
-                       LColor::color c = static_cast<LColor::color>(lcolor.size());
-                       lcolor.fill(c, new_branch, lcolor.getX11Name(LColor::background));
+                       rebuild_all_branches_browser();
                }
 
        } else if (ob == branch_->button_remove_branch) {
                unsigned i = fl_get_browser(branch_->browser_all_branches);
                string const current_branch =
                        fl_get_browser_line(branch_->browser_all_branches, i);
-               if (!current_branch.empty()) {
-                       params.branchlist().remove(current_branch);
-                       // Update branch list
-                       string const all_branches = params.branchlist().allBranches();
-                       fl_clear_browser(branch_->browser_all_branches);
-                       vec = getVectorFromString(all_branches, "|");
-                       for (unsigned i = 0; i < vec.size(); ++i) {
-                               fl_addto_browser(branch_->browser_all_branches,
-                                                               vec[i].c_str());
-                       }
-                       // Update selected-list...
-                       string const all_selected = params.branchlist().allSelected();
-                       fl_clear_browser(branch_->browser_selection);
-                       vec = getVectorFromString(all_selected, "|");
-                       for (unsigned i = 0; i < vec.size(); ++i) {
-                               fl_addto_browser(branch_->browser_selection, vec[i].c_str());
-                       }
-               }
-       } else if (ob == branch_->button_select) {
-               unsigned i = fl_get_browser(branch_->browser_all_branches);
-               string const current_branch =
-                       fl_get_browser_line(branch_->browser_all_branches, i);
-               if (!current_branch.empty()) {
-                       fl_clear_browser(branch_->browser_selection);
-                       params.branchlist().setSelected(current_branch, true);
-                       string const all_selected = params.branchlist().allSelected();
-                       vec = getVectorFromString(all_selected, "|");
-                       for (unsigned i = 0; i < vec.size(); ++i) {
-                               fl_addto_browser(branch_->browser_selection,
-                                                       vec[i].c_str());
-                       }
-               }
-       } else if (ob == branch_->button_deselect) {
-               unsigned i = fl_get_browser(branch_->browser_selection);
-               string const current_sel =
-                       fl_get_browser_line(branch_->browser_selection, i);
-               if (!current_sel.empty()) {
-                       fl_clear_browser(branch_->browser_selection);
-                       params.branchlist().setSelected(current_sel, false);
-                       string const all_selected = params.branchlist().allSelected();
-                       vec = getVectorFromString(all_selected, "|");
-                       for (unsigned i = 0; i < vec.size(); ++i) {
-                               fl_addto_browser(branch_->browser_selection,
-                                                       vec[i].c_str());
-                       }
+               if (!current_branch.empty() &&
+                   branchlist_.remove(current_branch)) {
+
+                       rebuild_all_branches_browser();
+                       rebuild_selected_branches_browser();
                }
-       } else if (ob == branch_->button_modify) {
-               unsigned i = fl_get_browser(branch_->browser_all_branches);
-               string const current_branch =
-                       fl_get_browser_line(branch_->browser_all_branches, i);
 
-               RGBColor before;
-               string x11hexname = params.branchlist().getColor(current_branch);
-               if (x11hexname[0] == '#') {
-                       before = RGBColor(x11hexname);
-               } else{
-                       fl_getmcolor(FL_COL1, &before.r, &before.g, &before.b);
+       } else if (ob == branch_->button_select ||
+                  ob == branch_->button_deselect) {
+
+               bool const selecting = ob == branch_->button_select;
+               string current_branch;
+
+               // When selecting, take highlighted item from left browser,
+               // when deselecting, from right browser:
+               if (selecting) {
+                       int const i = fl_get_browser(branch_->browser_all_branches);
+                       current_branch =
+                               fl_get_browser_line(branch_->browser_all_branches, i);
+               } else {
+                       int const i = fl_get_browser(branch_->browser_selection);
+                       current_branch =
+                               fl_get_browser_line(branch_->browser_selection, i);
                }
+               Branch * branch = branchlist_.find(current_branch);
 
-               RGBColor col = picker_->requestColor(before);
-               if (before != col) {
-                       fl_mapcolor(GUI_COLOR_CHOICE, col.r, col.g, col.b);
+               if (branch && branch->setSelected(selecting))
+                       rebuild_selected_branches_browser();
+
+       } else if (ob == branch_->button_modify) {
+               RGBColor const before =
+                       get_current_color(branch_->browser_all_branches,
+                                         branchlist_);
+               RGBColor const after = picker_->requestColor(before);
+               if (before != after) {
+                       fl_mapcolor(GUI_COLOR_CHOICE,
+                                   after.r, after.g, after.b);
                        fl_redraw_object(branch_->button_color);
-                       // Figure out here how to stash the new colour into the
-                       // LyX colour database.
-
-                       x11hexname = X11hexname(col);
-
-                       // current_branch already in database
-                       LColor::color c = lcolor.getFromLyXName(current_branch);
-                       lcolor.setColor(current_branch, x11hexname);
-                       // Make sure that new colour is also displayed ;-)
-                       lyxColorHandler->getGCForeground(c);
-                       lyxColorHandler->updateColor(c);
-                       // what about system_lcolor?
-                       // Here set colour in BranchList:
-                       params.branchlist().setColor(current_branch, x11hexname);
+
+                       string const branch_name =
+                               getString(branch_->browser_all_branches);
+                       Branch * branch = branchlist_.find(branch_name);
+                       if (branch)
+                               branch->setColor(X11hexname(after));
                }
+
        } else if (ob == branch_->browser_all_branches) {
-               unsigned i = fl_get_browser(branch_->browser_all_branches);
-               string const current_branch =
-                       fl_get_browser_line(branch_->browser_all_branches, i);
-               // make button_color track selected branch:
+               RGBColor rgb =
+                       get_current_color(branch_->browser_all_branches,
+                                         branchlist_);
 
-               RGBColor rgb;
-               string x11hexname = params.branchlist().getColor(current_branch);
-               if (x11hexname[0] == '#') {
-                       rgb = RGBColor(x11hexname);
-               } else {
-                       fl_getmcolor(FL_COL1, &rgb.r, &rgb.g, &rgb.b);
-               }
                fl_mapcolor(GUI_COLOR_CHOICE, rgb.r, rgb.g, rgb.b);
                fl_redraw_object(branch_->button_color);
        }
@@ -798,8 +822,6 @@ void FormDocument::branch_input(FL_OBJECT * ob)
                (fl_get_browser(branch_->browser_all_branches) > 0));
        setEnabled(branch_->button_modify,
                (fl_get_browser(branch_->browser_all_branches) > 0));
-
-       branchlist_ = params.branchlist();
 }
 
 
@@ -974,10 +996,6 @@ bool FormDocument::language_apply(BufferParams & params)
                break;
        }
        params.quotes_language = lga;
-       if (fl_get_button(language_->radio_single))
-               params.quotes_times = InsetQuotes::SingleQ;
-       else
-               params.quotes_times = InsetQuotes::DoubleQ;
 
        int const pos = fl_get_combox(language_->combox_language);
        Language const * new_language = languages.getLanguage(lang_[pos-1]);
@@ -998,9 +1016,24 @@ bool FormDocument::options_apply(BufferParams & params)
        params.graphicsDriver = getString(options_->choice_postscript_driver);
        params.use_amsmath = static_cast<BufferParams::AMS>(
                fl_get_choice(options_->choice_ams_math) - 1);
-       params.use_natbib  = fl_get_button(options_->check_use_natbib);
-       params.use_numerical_citations  =
-               fl_get_choice(options_->choice_citation_format) - 1;
+
+       int const cite_choice = fl_get_choice(options_->choice_cite_engine);
+       switch (cite_choice) {
+       case 1:
+               params.cite_engine = biblio::ENGINE_BASIC;
+               break;
+       case 2:
+               params.cite_engine = biblio::ENGINE_NATBIB_AUTHORYEAR;
+               break;
+       case 3:
+               params.cite_engine = biblio::ENGINE_NATBIB_NUMERICAL;
+               break;
+       case 4:
+               params.cite_engine = biblio::ENGINE_JURABIB;
+               break;
+       }
+
+       params.use_bibtopic  = fl_get_button(options_->check_bibtopic);
 
        int tmpchar = int(fl_get_counter_value(options_->counter_secnumdepth));
        if (params.secnumdepth != tmpchar)
@@ -1029,11 +1062,7 @@ void FormDocument::bullets_apply(BufferParams & params)
 
 void FormDocument::branch_apply(BufferParams & params)
 {
-       BufferParams & prms = controller().params();
-       if (branchlist_.empty())
-               branchlist_ = prms.branchlist();
        params.branchlist() = branchlist_;
-       branchlist_.clear();
 }
 
 
@@ -1159,12 +1188,6 @@ void FormDocument::language_update(BufferParams const & params)
 
        fl_set_choice_text(language_->choice_inputenc, params.inputenc.c_str());
        fl_set_choice(language_->choice_quotes_language, params.quotes_language + 1);
-       fl_set_button(language_->radio_single, 0);
-       fl_set_button(language_->radio_double, 0);
-       if (params.quotes_times == InsetQuotes::SingleQ)
-               fl_set_button(language_->radio_single, 1);
-       else
-               fl_set_button(language_->radio_double, 1);
 }
 
 
@@ -1176,10 +1199,25 @@ void FormDocument::options_update(BufferParams const & params)
        fl_set_choice_text(options_->choice_postscript_driver,
                           params.graphicsDriver.c_str());
        fl_set_choice(options_->choice_ams_math, params.use_amsmath + 1);
-       fl_set_button(options_->check_use_natbib,  params.use_natbib);
-       fl_set_choice(options_->choice_citation_format,
-                     int(params.use_numerical_citations)+1);
-       setEnabled(options_->choice_citation_format, params.use_natbib);
+
+       int cite_choice = 1;
+       switch (params.cite_engine) {
+       case biblio::ENGINE_BASIC:
+               cite_choice = 1;
+               break;
+       case biblio::ENGINE_NATBIB_AUTHORYEAR:
+               cite_choice = 2;
+               break;
+       case biblio::ENGINE_NATBIB_NUMERICAL:
+               cite_choice = 3;
+               break;
+       case biblio::ENGINE_JURABIB:
+               cite_choice = 4;
+               break;
+       }
+       fl_set_choice(options_->choice_cite_engine, cite_choice);
+
+       fl_set_button(options_->check_bibtopic,  params.use_bibtopic);
        fl_set_counter_value(options_->counter_secnumdepth, params.secnumdepth);
        fl_set_counter_value(options_->counter_tocdepth, params.tocdepth);
        if (!params.float_placement.empty())
@@ -1288,8 +1326,7 @@ void FormDocument::bullets_update(BufferParams const & params)
             (XpmVersion==4 && XpmRevision<7)))
                return;
 
-       bool const isLinuxDoc =
-               controller().docType() == ControlDocument::LINUXDOC;
+       bool const isLinuxDoc = kernel().docType() == Kernel::LINUXDOC;
        setEnabled(fbullet, !isLinuxDoc);
 
        if (isLinuxDoc) return;
@@ -1307,39 +1344,28 @@ void FormDocument::branch_update(BufferParams const & params)
        if (!branch_.get())
                return;
 
-       string const all_branches = params.branchlist().allBranches();
-       fl_clear_browser(branch_->browser_all_branches);
-       string current_branch("none");
+       branchlist_ = params.branchlist();
 
-       if (!all_branches.empty()) {
-               std::vector<string> vec = getVectorFromString(all_branches, "|");
-               for (unsigned i = 0; i < vec.size(); ++i) {
-                       fl_addto_browser(branch_->browser_all_branches, vec[i].c_str());
-               }
-               fl_select_browser_line(branch_->browser_all_branches, 1);
-               if (!vec.empty())
-                       current_branch =
-                               fl_get_browser_line(branch_->browser_all_branches, 1);
-               else
-                       current_branch = "none";
-       }
+       rebuild_all_branches_browser();
+       string const current_branch =
+               fl_get_browser_maxline(branch_->browser_all_branches) == 0 ?
+               "none" :
+               fl_get_browser_line(branch_->browser_all_branches, 1);
 
        // display proper selection...
-       string const all_selected = params.branchlist().allSelected();
-       fl_clear_browser(branch_->browser_selection);
-       if (!all_selected.empty()) {
-               std::vector<string> vec = getVectorFromString(all_selected, "|");
-               for (unsigned i = 0; i < vec.size(); ++i) {
-                       fl_addto_browser(branch_->browser_selection, vec[i].c_str());
-                       }
-       }
+       rebuild_selected_branches_browser();
+
        // display proper colour...
        RGBColor rgb;
        string x11hexname;
        if (current_branch == "none")
                x11hexname = "none";
-       else
-               x11hexname = params.branchlist().getColor(current_branch);
+       else {
+               Branch * branch = branchlist_.find(current_branch);
+               if (branch)
+                       x11hexname = branch->getColor();
+       }
+
        if (x11hexname[0] == '#') {
                rgb = RGBColor(x11hexname);
        } else {
@@ -1361,7 +1387,7 @@ void FormDocument::branch_update(BufferParams const & params)
 
 void FormDocument::checkReadOnly()
 {
-       if (bc().readOnly(controller().bufferIsReadonly())) {
+       if (bc().readOnly(kernel().isBufferReadonly())) {
                postWarning(_("Document is read-only."
                              " No changes to layout permitted."));
        } else {
@@ -1501,8 +1527,7 @@ void FormDocument::CheckChoiceClass()
 {
        BufferParams & params = controller().params();
 
-       lyx::textclass_type const tc =
-               fl_get_combox(class_->combox_class) - 1;
+       textclass_type const tc = fl_get_combox(class_->combox_class) - 1;
 
        if (controller().loadTextclass(tc)) {
                params.textclass = tc;
@@ -1535,3 +1560,6 @@ void FormDocument::UpdateLayoutDocument(BufferParams const & params)
        options_update(params);
        bullets_update(params);
 }
+
+} // namespace frontend
+} // namespace lyx