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 / LyXView.C < prev    next >
C/C++ Source or Header  |  1998-04-23  |  8KB  |  359 lines

  1. // -*- C++ -*-
  2. /* This file is part of
  3. * ======================================================
  4. *           LyX, The Document Processor
  5. *        
  6. *           Copyright (C) 1995 Matthias Ettrich
  7. *           Copyright (C) 1995-1998 The LyX Team.
  8. *
  9. *======================================================*/
  10.  
  11. #include <config.h>
  12.  
  13. #ifdef __GNUG__
  14. #pragma implementation
  15. #endif
  16.  
  17. #include "LyXView.h"
  18. #include "lyx_main.h"
  19. #include "lyxlookup.h"
  20. #include "toolbar.h"
  21. #include "minibuffer.h"
  22. #include "lyxfunc.h"
  23. #include "lyx.xpm"
  24. #include "error.h"
  25. #include "layout_forms.h"
  26. #include "intl.h"
  27. #include "lyxrc.h"
  28. #include "lyxscreen.h"
  29. #include "filetools.h"        // OnlyFilename()
  30. #include "layout.h"
  31. #include "lyxtext.h"
  32.  
  33. //     $Id: LyXView.C,v 1.1.1.1 1998/04/23 16:02:47 larsbj Exp $    
  34.  
  35. #if !defined(lint) && !defined(WITH_WARNINGS)
  36. static char vcid[] = "$Id: LyXView.C,v 1.1.1.1 1998/04/23 16:02:47 larsbj Exp $";
  37. #endif /* lint */
  38.  
  39. extern FD_form_document *fd_form_document;
  40. FD_form_main *fd_form_main; /* a pointer to the one in LyXView
  41.                    should be removed as soon as possible */
  42.  
  43. MiniBuffer *minibuffer;/* a pointer to the one in LyXView
  44.               should be removed as soon as possible */
  45.  
  46. extern void AutoSave();
  47. extern char updatetimer;
  48. extern void QuitLyX();
  49. int current_layout = 0;
  50.  
  51. // This is very temporary
  52. BufferView *current_view;
  53.  
  54.  
  55. LyXView::LyXView(int width, int height)
  56. {
  57.     fd_form_main = create_form_form_main(width,height);
  58.     fl_set_form_atclose(_form, atCloseMainFormCB, NULL);
  59.     lyxerr.debug("Initializing LyXFunc");
  60.     lyxfunc = new LyXFunc(this);
  61.     intl = new Intl;
  62. }
  63.  
  64.  
  65. LyXView::~LyXView()
  66. {
  67.     delete menus;
  68.     delete toolbar;
  69.     delete bufferview;
  70.     delete minibuffer;
  71.     delete lyxfunc;
  72.     delete intl;
  73. }
  74.  
  75. /// Redraw the main form.
  76. void LyXView::redraw() {
  77.     lyxerr.debug("LyXView::redraw()");
  78.     fl_redraw_form(_form);
  79. }
  80.  
  81.  
  82. // Callback for update timer
  83. void LyXView::UpdateTimerCB(FL_OBJECT *ob, long)
  84. {
  85.     LyXView *view = (LyXView*) ob->u_vdata;
  86.     if (!view->currentView()->available()) 
  87.         return;
  88.     if (!view->currentView()->getScreen() || !updatetimer)
  89.         return;
  90.  
  91.     view->currentView()->getScreen()->HideCursor();
  92.     view->currentBuffer()->update(-2);
  93.     /* this update can happen, even when the work area has lost
  94.      * the focus. So suppress the cursor in that case */
  95.     updatetimer = 0;
  96. }
  97.  
  98.  
  99. // Callback for autosave timer
  100. void LyXView::AutosaveTimerCB(FL_OBJECT *, long)
  101. {
  102.     lyxerr.debug("Running AutoSave()");
  103.     AutoSave();
  104. }
  105.  
  106.  
  107. /// Reset autosave timer
  108. void LyXView::resetAutosaveTimer()
  109. {
  110.     if (lyxrc->autosave)
  111.         fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
  112. }
  113.  
  114.  
  115. // Callback for close main form from window manager
  116. int LyXView::atCloseMainFormCB(FL_FORM *, void *)
  117. {
  118.     QuitLyX();
  119.     return FL_IGNORE;
  120. }
  121.  
  122.  
  123. void LyXView::setPosition(int x, int y)
  124. {
  125.     fl_set_form_position(_form, x, y);
  126. }
  127.  
  128.  
  129. void LyXView::show(int place, int border, char const* title)
  130. {
  131.     fl_show_form(_form, place, border, title);
  132.     InitLyXLookup(fl_display, _form->window);
  133. }
  134.  
  135.  
  136. FD_form_main *LyXView::create_form_form_main(int width, int height)
  137.     /* to make this work as it should, .lyxrc should have been
  138.      * read first; OR maybe this one should be made dynamic.
  139.      * Hmmmm. Lgb. 
  140.      * We will probably not have lyxrc before the main form is
  141.      * initialized, because error messages from lyxrc parsing 
  142.      * are presented (and rightly so) in GUI popups. Asger. 
  143.      */
  144. {
  145.     FL_OBJECT *obj;
  146.     
  147.     FD_form_main *fdui = (FD_form_main *)
  148.         fl_calloc(1, sizeof(FD_form_main));
  149.  
  150.     _form_main = fdui;
  151.  
  152.     // the main form
  153.     _form = fdui->form_main = fl_bgn_form(FL_NO_BOX, width, height);
  154.     fdui->form_main->u_vdata = (void*) this;
  155.     obj = fl_add_box(FL_FLAT_BOX,0,0,width,height,"");
  156.     fl_set_object_color(obj,FL_MCOL,FL_MCOL);
  157.  
  158.     // Parameters for the appearance of the main form
  159.     const int air = 2;
  160.     const int bw = abs(fl_get_border_width());
  161.     
  162.     //
  163.     // THE MENUBAR
  164.     //
  165.  
  166.     menus = new Menus(this, air);
  167.  
  168.     //
  169.     // TOOLBAR
  170.     //
  171.  
  172.     toolbar = new Toolbar(lyxrc->toolbarBackend, this, air, 30 + air + bw);
  173.  
  174.     // Setup the toolbar
  175.     toolbar->set(true);
  176.  
  177.     //
  178.     // WORKAREA
  179.     //
  180.  
  181.     const int ywork = 60 + 2*air + bw;
  182.     const int workheight = height - ywork - (25 + 2*air);
  183.  
  184.     ::current_view = bufferview = new BufferView(this, air, ywork,
  185.                              width - 3*air,
  186.                              workheight);
  187.  
  188.     //
  189.     // MINIBUFFER
  190.     //
  191.  
  192.     minibuffer = new MiniBuffer(this, air, height-(25+air), 
  193.                       width-(2*air), 25);
  194.     ::minibuffer = minibuffer; // to be removed later
  195.  
  196.  
  197.     //
  198.     // TIMERS
  199.     //
  200.     
  201.     // timer_autosave
  202.     fdui->timer_autosave = obj = fl_add_timer(FL_HIDDEN_TIMER,
  203.                           0,0,0,0,"Timer");
  204.     fl_set_object_callback(obj,AutosaveTimerCB,0);
  205.     
  206.     // timer_update
  207.     fdui->timer_update = obj = fl_add_timer(FL_HIDDEN_TIMER,
  208.                         0,0,0,0,"Timer");
  209.     fl_set_object_callback(obj,UpdateTimerCB,0);
  210.     obj->u_vdata = (void*) this;
  211.  
  212.     //
  213.     // Misc
  214.     //
  215.  
  216.         //  assign an icon to main form
  217.         unsigned int w, h;
  218.         Pixmap lyx_p, lyx_mask;
  219.         lyx_p = fl_create_from_pixmapdata(fl_root,
  220.                       lyx_xpm,
  221.                       &w,
  222.                       &h,
  223.                       &lyx_mask,
  224.                       0,
  225.                       0,
  226.                       0); // this leaks
  227.         fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
  228.  
  229.     // set min size
  230.     fl_set_form_minsize(fdui->form_main, 50, 50);
  231.     
  232.     fl_end_form();
  233.  
  234.     return fdui;
  235. }
  236.  
  237.  
  238. void LyXView::init()
  239. {
  240.     // Set the textclass choice
  241.     invalidateLayoutChoice();
  242.     updateLayoutChoice();
  243.     UpdateDocumentClassChoice();
  244.     
  245.     // Start autosave timer
  246.     if (lyxrc->autosave)
  247.         fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
  248.     
  249.     
  250.     // Install the raw callback for keyboard events 
  251.     fl_register_raw_callback(_form,
  252.                  KeyPressMask,
  253.                  KeyPressMask_raw_callback);
  254.         intl->InitKeyMapper(lyxrc->use_kbmap);
  255. }
  256.  
  257.  
  258. void LyXView::invalidateLayoutChoice()
  259. {
  260.     last_textclass = -1;
  261. }
  262.  
  263.  
  264. void LyXView::updateLayoutChoice()
  265. {
  266.     /* update the layout display */
  267.     if (!toolbar->combox) return;
  268.  
  269.     // this has a bi-effect that the layouts are not showed when no
  270.     // document is loaded.
  271.     if (bufferview==0 || bufferview->currentBuffer()==0) {
  272.         toolbar->combox->clear();
  273.         return;    
  274.     }
  275.  
  276.     // If textclass is different, we need to update the list
  277.     if (toolbar->combox->empty() ||
  278.         (last_textclass != currentBuffer()->params.textclass)) {
  279.         toolbar->combox->clear();
  280.         for (int i = 0;
  281.              lyxstyle.NameOfLayout(currentBuffer()->
  282.                        params.textclass, i) !="@@end@@";
  283.              i++) {
  284.             LyXLayout *layout = lyxstyle.
  285.                 Style(currentBuffer()->params.textclass, i);
  286.             if (layout->obsoleted_by.empty())
  287.                 toolbar->combox->addline(layout->name.c_str());
  288.             else
  289.                 toolbar->combox->addline(("@N"+layout->name).c_str());
  290.         }
  291.         last_textclass = currentBuffer()->params.textclass;
  292.         current_layout = 0;
  293.     }
  294.     
  295.     char layout = currentBuffer()->text->cursor.par->GetLayout();
  296.  
  297.     if (layout != current_layout){
  298.         toolbar->combox->select(layout + 1);
  299.         current_layout = layout;
  300.     }
  301. }
  302.  
  303.  
  304. void LyXView::UpdateDocumentClassChoice()
  305. {
  306.     /* update the document class display in the document form */
  307.     int i;
  308.     if (fd_form_document) {
  309.         fl_clear_choice(fd_form_document->choice_class);
  310.         for (i = 0; lyxstyle.DescOfClass (i)!="@@end@@"; i++) {
  311.             fl_addto_choice(fd_form_document->choice_class,
  312.                     lyxstyle.DescOfClass(i).c_str());
  313.         }
  314.     }
  315. }
  316.  
  317.  
  318. // This is necessary, since FL_FREE-Objects doesn't get all keypress events
  319. // as FL_KEYBOARD events :-(   Matthias 280596
  320. int LyXView::KeyPressMask_raw_callback(FL_FORM *fl, void *xev)
  321. {
  322.     LyXView *view = (LyXView*) fl->u_vdata;
  323.     int retval = 0;  // 0 means XForms should have a look at this event
  324.  
  325.     // funny. Even though the raw_callback is registered with KeyPressMask,
  326.     // also KeyRelease-events are passed through:-(
  327.     // [It seems that xforms puts them in pairs... (JMarc)]
  328.     if (((XEvent*)xev)->type == KeyPress
  329.         && view->bufferview->getWorkArea()->focus
  330.         && view->bufferview->getWorkArea()->active)
  331.         retval = view->getLyXFunc()->processKeyEvent((XEvent*)xev);
  332.  
  333.     return retval;
  334. }
  335.  
  336.  
  337. // Updates the title of the window with the filename of the current document
  338. void LyXView::updateWindowTitle() {
  339.     static LString last_title="LyX";
  340.     LString title = "LyX";
  341.  
  342.     if (currentView()->available()) {
  343.         LString cur_title = currentBuffer()->getFileName();
  344.         if (!cur_title.empty()){
  345.             title += ": " + OnlyFilename(cur_title);
  346.             if (!currentBuffer()->isLyxClean())
  347.                 title += _(" (Changed)");
  348.             if (currentBuffer()->isReadonly())
  349.                 title += _(" (read only)");
  350.         }
  351.     }
  352.     // Don't update title if it's the same as last time
  353.     if (title != last_title) {
  354.         fl_set_form_title(_form, title.c_str());
  355.         last_title = title;
  356.     }
  357. }
  358.