]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetinclude.C
prepare for 1.1.6pre2
[lyx.git] / src / insets / insetinclude.C
index 6bccf7dfd3fab885aa1ca31b93e28ffd12edb452..60cc1b2269be721721a06f08f7e3095e7ddb35e0 100644 (file)
@@ -1,3 +1,4 @@
+
 #include <config.h>
 
 #include <cstdlib>
 #include "include_form.h"
 #include "support/FileInfo.h"
 #include "layout.h"
+#include "lyxfunc.h"
+
+using std::ostream;
+using std::endl;
+using std::vector;
+using std::pair;
 
 extern BufferView * current_view;
 
-extern LyXRC * lyxrc;
 extern BufferList bufferlist;
-extern void UpdateInset(Inset * inset, bool mark_dirty = true);
 
 
 FD_include * create_form_include(void)
@@ -89,7 +94,8 @@ FD_include * create_form_include(void)
 
 FD_include * form = 0;
 
-extern "C" void include_cb(FL_OBJECT *, long arg)
+extern "C"
+void include_cb(FL_OBJECT *, long arg)
 {
     
        InsetInclude * inset = static_cast<InsetInclude*>(form->include->u_vdata);
@@ -98,7 +104,7 @@ extern "C" void include_cb(FL_OBJECT *, long arg)
        {
                // Should browsing too be disabled in RO-mode?
                LyXFileDlg fileDlg;
-               string mpath = OnlyPath(inset->getMasterFilename());
+               string const mpath = OnlyPath(inset->getMasterFilename());
                 string ext;
     
                if (fl_get_button(form->flag2)) // Use Input Button
@@ -108,18 +114,19 @@ extern "C" void include_cb(FL_OBJECT *, long arg)
                 else
                         ext = "*.lyx";
                // launches dialog
-               fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
+               fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
     
                // Use by default the master's path
-               string filename = fileDlg.Select(_("Select Child Document"),
-                                                 mpath, ext, 
-                                                 inset->getContents());
+               string const filename =
+                       fileDlg.Select(_("Select Child Document"),
+                                      mpath, ext, 
+                                      inset->getContents());
                XFlush(fl_get_display());
  
                // check selected filename
                if (!filename.empty()) {
-                       string filename2 = MakeRelPath(filename,
-                                                       mpath);
+                       string const filename2 = MakeRelPath(filename,
+                                                            mpath);
                        if (prefixIs(filename2, ".."))
                                fl_set_input(form->input,
                                             filename.c_str());
@@ -131,7 +138,7 @@ extern "C" void include_cb(FL_OBJECT *, long arg)
        }
 
        case 1:
-               if(!current_view->buffer()->isReadonly()) {
+               if (!current_view->buffer()->isReadonly()) {
                        inset->setContents(fl_get_input(form->input));
                        // don't typeset
                        inset->setNoLoad(fl_get_button(form->flag1));
@@ -145,7 +152,7 @@ extern "C" void include_cb(FL_OBJECT *, long arg)
                        }
                        
                        fl_hide_form(form->include);
-                       UpdateInset(inset);
+                       current_view->updateInset(inset, true);
                        break;
                } // fall through
                
@@ -153,7 +160,7 @@ extern "C" void include_cb(FL_OBJECT *, long arg)
                fl_hide_form(form->include);
                break;
        case 5:
-               if(!current_view->buffer()->isReadonly()) {
+               if (!current_view->buffer()->isReadonly()) {
                        inset->setContents(fl_get_input(form->input));
                        inset->setNoLoad(fl_get_button(form->flag1));
                        if (fl_get_button(form->flag2))
@@ -166,8 +173,8 @@ extern "C" void include_cb(FL_OBJECT *, long arg)
                        }
                        
                        fl_hide_form(form->include);
-                       UpdateInset(inset);
-                       current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents().c_str());
+                       current_view->updateInset(inset, true);
+                       current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents());
                 }
                 break;
                
@@ -184,13 +191,24 @@ extern "C" void include_cb(FL_OBJECT *, long arg)
 }
 
 
-InsetInclude::InsetInclude(string const & fname, Buffer * bf)
-       : InsetCommand("include") 
+static inline
+string unique_id() {
+       static unsigned int seed = 1000;
+
+       std::ostringstream ost;
+       ost << "file" << ++seed;
+
+       // Needed if we use lyxstring.
+       return ost.str().c_str();
+}
+
+
+InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & bf)
+       : InsetCommand(p), master(&bf)
 {
-       master = bf;
-       setContents(fname);
        flag = InsetInclude::INCLUDE;
        noload = false;
+       include_label = unique_id();
 }
 
 
@@ -211,9 +229,9 @@ InsetInclude::~InsetInclude()
 }
 
 
-Inset * InsetInclude::Clone() const
+Inset * InsetInclude::Clone(Buffer const & buffer) const
 { 
-       InsetInclude * ii = new InsetInclude (contents, master); 
+       InsetInclude * ii = new InsetInclude (params(), buffer); 
        ii->setNoLoad(isNoLoad());
        // By default, the newly created inset is of `include' type,
        // so we do not test this case.
@@ -227,18 +245,18 @@ Inset * InsetInclude::Clone() const
 }
 
 
-void InsetInclude::Edit(int, int)
+void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
 {
-       if(current_view->buffer()->isReadonly())
-               WarnReadonly(current_view->buffer()->fileName());
+       if (bv->buffer()->isReadonly())
+               WarnReadonly(bv->buffer()->fileName());
 
        if (!form) {
                 form = create_form_include();
-               fl_set_form_atclose(form->include, IgnoreCloseBoxCB, 0);
+               fl_set_form_atclose(form->include, CancelCloseBoxCB, 0);
        }
         form->include->u_vdata = this;
     
-        fl_set_input(form->input, contents.c_str());
+        fl_set_input(form->input, getContents().c_str());
        fl_set_button(form->flag1, int(isNoLoad()));
        fl_set_button(form->flag2, int(isInput()));
        fl_set_button(form->flag3, int(isInclude()));
@@ -254,21 +272,22 @@ void InsetInclude::Edit(int, int)
         if (form->include->visible) {
                fl_raise_form(form->include);
        } else {
-               fl_show_form(form->include, FL_PLACE_MOUSE, FL_FULLBORDER,
+               fl_show_form(form->include,
+                            FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
                             _("Include"));
        }
 }
 
 
-void InsetInclude::Write(ostream & os)
+void InsetInclude::Write(Buffer const *, ostream & os) const
 {
        os << "Include " << getCommand() << "\n";
 }
 
 
-void InsetInclude::Read(LyXLex & lex)
+void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
 {
-       InsetCommand::Read(lex);
+       InsetCommand::Read(buf, lex);
     
        if (getCmdName() == "include")
                setInclude();
@@ -282,7 +301,13 @@ void InsetInclude::Read(LyXLex & lex)
 }
 
 
-string InsetInclude::getScreenLabel() const
+bool InsetInclude::display() const 
+{
+       return !isInput();
+}
+
+
+string const InsetInclude::getScreenLabel() const
 {
        string temp;
        if (isInput())
@@ -293,15 +318,28 @@ string InsetInclude::getScreenLabel() const
        } else temp += _("Include");
        temp += ": ";
        
-       if (contents.empty()) {
+       if (getContents().empty()) {
                temp+= "???";
        } else {
-               temp+= contents;
+               temp+= getContents();
        }
        return temp;
 }
 
 
+string const InsetInclude::getFileName() const
+{
+       return MakeAbsPath(getContents(), 
+                          OnlyPath(getMasterFilename()));
+}
+
+
+string const InsetInclude::getMasterFilename() const
+{
+       return master->fileName();
+}
+
+
 bool InsetInclude::loadIfNeeded() const
 {
        if (isNoLoad() || isVerb()) return false;
@@ -311,100 +349,175 @@ bool InsetInclude::loadIfNeeded() const
        
        // the readonly flag can/will be wrong, not anymore I think.
        FileInfo finfo(getFileName());
-       bool ro = !finfo.writable();
-       return ( bufferlist.readFile(getFileName(), ro) != 0 );
-}
-
-
-int InsetInclude::Latex(ostream & os, signed char /*fragile*/)
-{
-       string include_file;
-       signed char dummy = 0;
-       Latex(include_file, dummy);
-       os << include_file;
-       return 0;
+       bool const ro = !finfo.writable();
+       return bufferlist.readFile(getFileName(), ro) != 0;
 }
 
 
-int InsetInclude::Latex(string & file, signed char /*fragile*/)
+int InsetInclude::Latex(Buffer const * buffer, ostream & os,
+                       bool /*fragile*/, bool /*fs*/) const
 {
-       string writefile, incfile;
-
+       string incfile(getContents());
+       
        // Do nothing if no file name has been specified
-       if (contents.empty())
+       if (incfile.empty())
                return 0;
     
-       // Use += to force a copy of contents (JMarc)
-       incfile += contents;
-
        if (loadIfNeeded()) {
                Buffer * tmp = bufferlist.getBuffer(getFileName());
 
-               if (tmp->params.textclass != master->params.textclass) {
+               if (tmp->params.textclass != buffer->params.textclass) {
                        lyxerr << "ERROR: Cannot handle include file `"
                               << MakeDisplayPath(getFileName())
                               << "' which has textclass `"
                               << textclasslist.NameOfClass(tmp->params.textclass)
                               << "' instead of `"
-                              << textclasslist.NameOfClass(master->params.textclass)
+                              << textclasslist.NameOfClass(buffer->params.textclass)
                               << "'." << endl;
                        return 0;
                }
                
                // write it to a file (so far the complete file)
-               writefile = ChangeExtension(getFileName(), ".tex", false);
-               if (!master->tmppath.empty()
-                   && !master->niceFile) {
+               string writefile = ChangeExtension(getFileName(), ".tex");
+               if (!buffer->tmppath.empty()
+                   && !buffer->niceFile) {
                        incfile = subst(incfile, '/','@');
 #ifdef __EMX__
                        incfile = subst(incfile, ':', '$');
 #endif
-                       writefile = AddName(master->tmppath, incfile);
+                       writefile = AddName(buffer->tmppath, incfile);
                } else
                        writefile = getFileName();
-               writefile = ChangeExtension(writefile, ".tex", false);
+               writefile = ChangeExtension(writefile, ".tex");
                lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
                lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
                
-               tmp->markDepClean(master->tmppath);
+               tmp->markDepClean(buffer->tmppath);
                
                tmp->makeLaTeXFile(writefile,
                                   OnlyPath(getMasterFilename()), 
-                                  master->niceFile, true);
+                                  buffer->niceFile, true);
        } 
 
        if (isVerb()) {
-               file += '\\';
-               file += command + '{';
-               file += incfile + '}';
-       } 
-       else if (isInput()) {
+               os << '\\' << getCmdName() << '{' << incfile << '}';
+       } else if (isInput()) {
                // \input wants file with extension (default is .tex)
                if (!IsLyXFilename(getFileName())) {
-                       file += '\\';
-                       file += command + '{';
-                       file += incfile + '}';
+                       os << '\\' << getCmdName() << '{' << incfile << '}';
                } else {
-                       file += '\\';
-                       file += command + '{';
-                       file += ChangeExtension(incfile, ".tex", false)
-                               + '}';
+                       os << '\\' << getCmdName() << '{'
+                          << ChangeExtension(incfile, ".tex")
+                          <<  '}';
                }
        } else {
                // \include don't want extension and demands that the
                // file really have .tex
-               file += '\\';
-               file += command + '{';
-               file +=         ChangeExtension(incfile, string(), false)
-                       + '}';
+               os << '\\' << getCmdName() << '{'
+                  << ChangeExtension(incfile, string())
+                  << '}';
        }
 
        return 0;
 }
 
 
+int InsetInclude::Linuxdoc(Buffer const * buffer, ostream & os) const
+{
+       string incfile(getContents());
+       
+       // Do nothing if no file name has been specified
+       if (incfile.empty())
+               return 0;
+    
+       if (loadIfNeeded()) {
+               Buffer * tmp = bufferlist.getBuffer(getFileName());
+
+               // write it to a file (so far the complete file)
+               string writefile = ChangeExtension(getFileName(), ".sgml");
+               if (!buffer->tmppath.empty() && !buffer->niceFile) {
+                       incfile = subst(incfile, '/','@');
+                       writefile = AddName(buffer->tmppath, incfile);
+               } else
+                       writefile = getFileName();
+
+               if (IsLyXFilename(getFileName()))
+                       writefile = ChangeExtension(writefile, ".sgml");
+
+               lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
+               lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
+               
+               tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
+       } 
+
+       if (isVerb()) {
+               os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
+                  << "\" format=\"linespecific\">"
+                  << "</inlinegraphic>";
+       } else 
+               os << '&' << include_label << ';';
+       
+       return 0;
+}
+
+
+int InsetInclude::DocBook(Buffer const * buffer, ostream & os) const
+{
+       string incfile(getContents());
+
+       // Do nothing if no file name has been specified
+       if (incfile.empty())
+               return 0;
+    
+       if (loadIfNeeded()) {
+               Buffer * tmp = bufferlist.getBuffer(getFileName());
+
+               // write it to a file (so far the complete file)
+               string writefile = ChangeExtension(getFileName(), ".sgml");
+               if (!buffer->tmppath.empty() && !buffer->niceFile) {
+                       incfile = subst(incfile, '/','@');
+                       writefile = AddName(buffer->tmppath, incfile);
+               } else
+                       writefile = getFileName();
+               if (IsLyXFilename(getFileName()))
+                       writefile = ChangeExtension(writefile, ".sgml");
+
+               lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
+               lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
+               
+               tmp->makeDocBookFile(writefile, buffer->niceFile, true);
+       } 
+
+       if (isVerb()) {
+               os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
+                  << "\" format=\"linespecific\">"
+                  << "</inlinegraphic>";
+       } else 
+               os << '&' << include_label << ';';
+       
+       return 0;
+}
+
+
 void InsetInclude::Validate(LaTeXFeatures & features) const
 {
+
+       string incfile(getContents());
+       string writefile; // = ChangeExtension(getFileName(), ".sgml");
+
+       if (!master->tmppath.empty() && !master->niceFile) {
+               incfile = subst(incfile, '/','@');
+               writefile = AddName(master->tmppath, incfile);
+       } else
+               writefile = getFileName();
+               // Use the relative path.
+               //writefile = incfile;
+
+       if (IsLyXFilename(getFileName()))
+               writefile = ChangeExtension(writefile, ".sgml");
+
+       features.IncludedFiles[include_label] = writefile;
+
        if (isVerb())
                features.verbatim = true;
 
@@ -419,49 +532,31 @@ void InsetInclude::Validate(LaTeXFeatures & features) const
 }
 
 
-string InsetInclude::getLabel(int) const
+vector<string> const InsetInclude::getLabelList() const
 {
-    string label;
-    string parentname;
-       
-       
-    if (loadIfNeeded()) {
-       Buffer * tmp = bufferlist.getBuffer(getFileName());
-       tmp->setParentName(""); 
-       label =  tmp->getReferenceList('\n');
-       tmp->setParentName(getMasterFilename());
-    }
-
-    return label;
-}
+       vector<string> l;
 
+       if (loadIfNeeded()) {
+               Buffer * tmp = bufferlist.getBuffer(getFileName());
+               tmp->setParentName(""); 
+               l = tmp->getLabelList();
+               tmp->setParentName(getMasterFilename());
+       }
 
-int InsetInclude::GetNumberOfLabels() const
-{
-    string label;
-
-    if (loadIfNeeded()) {
-       Buffer * tmp = bufferlist.getBuffer(getFileName());
-       tmp->setParentName("");    
-       label = tmp->getReferenceList('\n');
-       tmp->setParentName(getMasterFilename());
-    }
-    int nl = (label.empty())? 0: 1;
-       
-    return nl;
+       return l;
 }
 
 
-string InsetInclude::getKeys(char delim) const
+vector<pair<string,string> > const InsetInclude::getKeys() const
 {
-       string lst;
+       vector<pair<string,string> > keys;
        
        if (loadIfNeeded()) {
-               Buffer *tmp = bufferlist.getBuffer(getFileName());
+               Buffer * tmp = bufferlist.getBuffer(getFileName());
                tmp->setParentName(""); 
-               lst =  tmp->getBibkeyList(delim);
+               keys = tmp->getBibkeyList();
                tmp->setParentName(getMasterFilename());
        }
        
-       return lst;
+       return keys;
 }