home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lyx-0.13.2.tar.gz / lyx-0.13.2.tar / lyx-0.13.2 / src / insetinclude.C < prev    next >
C/C++ Source or Header  |  1998-04-23  |  10KB  |  380 lines

  1. #include <config.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5. #ifdef __GNUG__
  6. #pragma implementation
  7. #endif
  8.  
  9. #include FORMS_H_LOCATION 
  10. #include "filedlg.h" 
  11. #include "insetinclude.h"
  12. #include "buffer.h"
  13. #include "bufferlist.h"
  14. #include "error.h"
  15. #include "filetools.h"
  16. #include "lyxrc.h"
  17. #include "LyXView.h"
  18. #include "LaTeXFeatures.h"
  19. #include "lyx_gui_misc.h" // CancelCloseBoxCB
  20. #include "gettext.h"
  21. #include "include_form.h"
  22. #include "FileInfo.h"
  23.  
  24. extern BufferView *current_view;
  25.  
  26. extern LyXRC *lyxrc;
  27. extern BufferList bufferlist;
  28. extern void UpdateInset(Inset* inset, bool mark_dirty = true);
  29.  
  30.  
  31. FD_include *create_form_include(void)
  32. {
  33.   FL_OBJECT *obj;
  34.   FD_include *fdui = (FD_include *) fl_calloc(1, sizeof(*fdui));
  35.  
  36.   fdui->include = fl_bgn_form(FL_NO_BOX, 340, 210);
  37.   obj = fl_add_box(FL_UP_BOX,0,0,340,210,"");
  38.   obj = fl_add_frame(FL_ENGRAVED_FRAME,10,70,160,90,"");
  39.   fdui->browsebt = obj = fl_add_button(FL_NORMAL_BUTTON,230,30,100,30,idex(_("Browse|#B")));
  40.     fl_set_button_shortcut(obj,scex(_("Browse|#B")),1);
  41.     fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  42.     fl_set_object_callback(obj,include_cb,0);
  43.   fdui->flag1 = obj = fl_add_checkbutton(FL_PUSH_BUTTON,180,70,150,30,idex(_("Don't typeset|#D")));
  44.     fl_set_button_shortcut(obj,scex(_("Don't typeset|#D")),1);
  45.     fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  46.   obj = fl_add_button(FL_RETURN_BUTTON,120,170,100,30,_("OK"));
  47.     fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  48.     fl_set_object_callback(obj,include_cb,1);
  49.   obj = fl_add_button(FL_NORMAL_BUTTON,230,170,100,30,idex(_("Cancel|^[")));
  50.     fl_set_button_shortcut(obj,scex(_("Cancel|^[")),1);
  51.     fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  52.     fl_set_object_callback(obj,include_cb,2);
  53.   obj = fl_add_button(FL_NORMAL_BUTTON,230,130,100,30,idex(_("Load|#L")));
  54.     fl_set_button_shortcut(obj,scex(_("Load|#L")),1);
  55.     fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  56.     fl_set_object_callback(obj,include_cb,5);
  57.   fdui->input = obj = fl_add_input(FL_NORMAL_INPUT,10,30,210,30,idex(_("File name:|#F")));
  58.     fl_set_input_shortcut(obj,scex(_("File name:|#F")),1);
  59.     fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  60.     fl_set_object_lalign(obj,FL_ALIGN_TOP_LEFT);
  61.   fdui->flag41 = obj = fl_add_checkbutton(FL_PUSH_BUTTON,180,100,150,30,idex(_("Visible space|#s")));
  62.     fl_set_button_shortcut(obj,scex(_("Visible space|#s")),1);
  63.     fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  64.  
  65.   fdui->include_grp = fl_bgn_group();
  66.   fdui->flag4 = obj = fl_add_checkbutton(FL_RADIO_BUTTON,10,130,160,30,idex(_("Verbatim|#V")));
  67.     fl_set_button_shortcut(obj,scex(_("Verbatim|#V")),1);
  68.     fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  69.     fl_set_object_callback(obj,include_cb,10);
  70.   fdui->flag2 = obj = fl_add_checkbutton(FL_RADIO_BUTTON,10,100,160,30,idex(_("Use input|#i")));
  71.     fl_set_button_shortcut(obj,scex(_("Use input|#i")),1);
  72.     fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  73.     fl_set_object_callback(obj,include_cb,11);
  74.   fdui->flag3 = obj = fl_add_checkbutton(FL_RADIO_BUTTON,10,70,160,30,idex(_("Use include|#U")));
  75.     fl_set_button_shortcut(obj,scex(_("Use include|#U")),1);
  76.     fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  77.     fl_set_object_callback(obj,include_cb,11);
  78.   fl_end_group();
  79.  
  80.   fl_end_form();
  81.  
  82.   //fdui->include->fdui = fdui;
  83.  
  84.   return fdui;
  85. }
  86. /*---------------------------------------*/
  87.  
  88.  
  89. FD_include *form = 0;
  90.  
  91. void include_cb(FL_OBJECT *, long arg)
  92. {
  93.     
  94.     InsetInclude *inset = (InsetInclude*)form->vdata;
  95.     switch (arg) {
  96.     case 0:
  97.     {
  98.         // Should browsing too be disabled in RO-mode?
  99.         LyXFileDlg fileDlg;
  100.         LString mpath = OnlyPath(inset->getMasterFilename());
  101.                 LString ext;
  102.     
  103.         if (fl_get_button(form->flag2)) // Use Input Button
  104.             ext = "*.tex";
  105.         else if (fl_get_button(form->flag4)) // Verbatim all files
  106.             ext = "*";
  107.                 else
  108.                         ext = "*.lyx";
  109.         // launches dialog
  110.         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
  111.     
  112.         // Use by default the master's path
  113.         LString filename = fileDlg.Select(_("Select Child Document"),
  114.                           mpath, ext, 
  115.                           inset->getContents());
  116.         XFlush(fl_get_display());
  117.  
  118.         // check selected filename
  119.         if (!filename.empty()) {
  120.             LString filename2 = MakeRelPath(filename,
  121.                             mpath);
  122.             if (filename2.prefixIs(".."))
  123.                 fl_set_input(form->input,
  124.                          filename.c_str());
  125.             else
  126.                 fl_set_input(form->input,
  127.                          filename2.c_str());
  128.         }
  129.         break;
  130.     }
  131.  
  132.     case 1:
  133.         if(!current_view->currentBuffer()->isReadonly()) {
  134.             inset->setContents(fl_get_input(form->input));
  135.             // don't typeset
  136.             inset->setNoLoad(fl_get_button(form->flag1));
  137.             if (fl_get_button(form->flag2))
  138.                 inset->setInput();
  139.             else if (fl_get_button(form->flag3))
  140.                 inset->setInclude();
  141.             else if (fl_get_button(form->flag4)) {
  142.                 inset->setVerb();
  143.                 inset->setVisibleSpace(fl_get_button(form->flag41));
  144.             }
  145.             
  146.             fl_hide_form(form->include);
  147.             UpdateInset(inset);
  148.             break;
  149.         } // fall through
  150.         
  151.     case 2:
  152.         fl_hide_form(form->include);
  153.         break;
  154.     case 5:
  155.         if(!current_view->currentBuffer()->isReadonly()) {
  156.             inset->setContents(fl_get_input(form->input));
  157.             inset->setNoLoad(fl_get_button(form->flag1));
  158.             if (fl_get_button(form->flag2))
  159.                 inset->setInput();
  160.             else if (fl_get_button(form->flag3))
  161.                 inset->setInclude();
  162.             else if (fl_get_button(form->flag4)) {
  163.                 inset->setVerb();
  164.                 inset->setVisibleSpace(fl_get_button(form->flag41));
  165.             }
  166.             
  167.             fl_hide_form(form->include);
  168.             UpdateInset(inset);
  169.             current_view->getOwner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents().c_str());
  170.                 }
  171.                 break;
  172.         
  173.         case 10:
  174.                 fl_activate_object(form->flag41);
  175.                 fl_set_object_lcol(form->flag41, FL_BLACK); 
  176.                 break;
  177.         case 11:
  178.                 fl_deactivate_object(form->flag41);
  179.                 fl_set_object_lcol(form->flag41, FL_INACTIVE);
  180.             fl_set_button(form->flag41, 0);
  181.                 break;
  182.     }
  183. }
  184.  
  185.  
  186. InsetInclude::InsetInclude(LString const & fname, Buffer *bf)
  187.     : InsetCommand("include") 
  188. {
  189.     master = bf;
  190.     setContents(fname);
  191.     flag = InsetInclude::INCLUDE;
  192.     noload = false;
  193. }
  194.  
  195.  
  196. InsetInclude::~InsetInclude()
  197. {
  198. }
  199.  
  200.  
  201. void InsetInclude::Edit(int, int)
  202. {
  203.     if(current_view->currentBuffer()->isReadonly())
  204.         WarnReadonly();
  205.  
  206.     if (!form) {
  207.                 form = create_form_include();
  208.         fl_set_form_atclose(form->include, IgnoreCloseBoxCB, NULL);
  209.     }
  210.         form->vdata = this; 
  211.     
  212.         fl_set_input(form->input, contents.c_str());
  213.     fl_set_button(form->flag1, int(isNoLoad()));
  214.     fl_set_button(form->flag2, int(isInput()));
  215.     fl_set_button(form->flag3, int(isInclude()));
  216.     fl_set_button(form->flag4, int(isVerb()));
  217.         if (isVerb()) 
  218.             fl_set_button(form->flag41, int(isVerbVisibleSpace()));
  219.         else {
  220.         fl_set_button(form->flag41, 0);
  221.             fl_deactivate_object(form->flag41);
  222.         fl_set_object_lcol(form->flag41, FL_INACTIVE);
  223.     }
  224.     
  225.         if (form->include->visible) {
  226.         fl_raise_form(form->include);
  227.     } else {
  228.         fl_show_form(form->include,FL_PLACE_MOUSE, FL_FULLBORDER,
  229.                  _("Include"));
  230.     }
  231. }
  232.  
  233.  
  234. void InsetInclude::Write(FILE *file)
  235. {
  236.     fprintf(file, "Include %s\n", getCommand().c_str());
  237. }
  238.  
  239.  
  240. void InsetInclude::Read(LyXLex &lex)
  241. {
  242.     InsetCommand::Read(lex);
  243.     
  244.     if (getCmdName()=="include")
  245.         setInclude();
  246.     else if (getCmdName() == "input")
  247.         setInput();
  248.     else if (getCmdName().contains("verbatim")) {
  249.         setVerb();
  250.         if (getCmdName() == "verbatiminput*")
  251.             setVisibleSpace(true);
  252.     }
  253. }
  254.  
  255.  
  256. LString InsetInclude::getScreenLabel() const
  257. {
  258.     LString temp;
  259.     if (isInput())
  260.         temp += _("Input");
  261.     else if (isVerb()) {
  262.         temp += _("Verbatim Input");
  263.         if (isVerbVisibleSpace()) temp += '*';
  264.     } else temp += _("Include");
  265.     temp += ": ";
  266.     
  267.     if (contents.empty()) {
  268.         temp+="???";
  269.     } else {
  270.         temp+=contents;
  271.     }
  272.     return temp;
  273. }
  274.  
  275.  
  276. bool InsetInclude::loadIfNeeded() const
  277. {
  278.     if (isNoLoad() || isVerb()) return false;
  279.     if (!IsLyXFilename(getFileName())) return false;
  280.     
  281.     if (bufferlist.exists(getFileName())) return true;
  282.     
  283.     // the readonly flag can/will be wrong, not anymore I think.
  284.     FileInfo finfo(getFileName());
  285.     bool ro = !finfo.writable();
  286.     return ( bufferlist.readFile(getFileName(), ro) != NULL );
  287. }
  288.  
  289.  
  290. int InsetInclude::Latex(FILE *file, signed char /*fragile*/)
  291. {
  292.     LString include_file;
  293.     signed char dummy = 0;
  294.     Latex(include_file, dummy);
  295.     fprintf(file, "%s", include_file.c_str());
  296.     return 0;
  297. }
  298.  
  299.  
  300. int InsetInclude::Latex(LString &file, signed char /*fragile*/)
  301. {
  302.     LString writefile, incfile;
  303.  
  304.     // Do nothing if no file name has been specified
  305.     if (contents.empty())
  306.         return 0;
  307.  
  308.     // Use += to force a copy of contents (JMarc)
  309.     incfile += contents;
  310.  
  311.     if (loadIfNeeded()) {
  312.         Buffer *tmp = bufferlist.getBuffer(getFileName());
  313.         
  314.         // write it to a file (so far the complete file)
  315.         writefile = ChangeExtension(getFileName(), ".tex", false);
  316.         if (!master->tmppath.empty()
  317.             && !master->niceFile) {
  318.             incfile.subst('/','@');
  319.             #ifdef __EMX__
  320.             incfile.subst(':', '$');
  321.             #endif
  322.             writefile = AddName(master->tmppath, incfile);
  323.         } else
  324.             writefile = getFileName();
  325.         writefile = ChangeExtension(writefile,".tex",false);
  326.         lyxerr.debug("incfile:" + incfile, Error::LATEX);
  327.         lyxerr.debug("writefile:" + writefile, Error::LATEX);
  328.         
  329.         tmp->markDepClean(master->tmppath);
  330.         
  331.         tmp->makeLaTeXFile(writefile,
  332.                    OnlyPath(getMasterFilename()), 
  333.                    master->niceFile, true);
  334.     } 
  335.  
  336.     if (isVerb()) {
  337.         file += '\\';
  338.         file += command + '{';
  339.         file += incfile + '}';
  340.     } 
  341.     else if (isInput()) {
  342.         // \input wants file with extension (default is .tex)
  343.         if (!IsLyXFilename(getFileName())) {
  344.             file += '\\';
  345.             file += command + '{';
  346.             file += incfile + '}';
  347.         } else {
  348.             file += '\\';
  349.             file += command + '{';
  350.             file +=    ChangeExtension(incfile, ".tex", false)
  351.                 + '}';
  352.         }
  353.     } else {
  354.         // \include don't want extension and demands that the
  355.         // file really have .tex
  356.         file += '\\';
  357.         file += command + '{';
  358.         file +=    ChangeExtension(incfile, LString(), false)
  359.             + '}';
  360.     }
  361.  
  362.     return 0;
  363. }
  364.  
  365.  
  366. void InsetInclude::Validate(LaTeXFeatures& features) const
  367. {
  368.     if (isVerb())
  369.         features.verbatim = true;
  370.  
  371.     // Here we must do the fun stuff...
  372.     // Load the file in the include if it needs
  373.     // to be loaded:
  374.     if (loadIfNeeded()) {
  375.         // a file got loaded
  376.         Buffer *tmp = bufferlist.getBuffer(getFileName());
  377.         tmp->validate(features);
  378.     }
  379. }
  380.