2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2000 The LyX Team.
10 * ====================================================== */
22 #include "support/lstrings.h"
25 #pragma implementation
28 #include "commandtags.h"
29 #include "BufferView.h"
30 #include "bufferlist.h"
33 #include "insets/lyxinset.h"
34 #include "insets/insetbib.h"
35 #include "minibuffer.h"
36 #include "lyxscreen.h"
40 #include "lyx_gui_misc.h"
41 #include "BackStack.h"
46 #include "TextCache.h"
52 #include "BufferView_pimpl.h"
59 extern BufferList bufferlist;
61 void sigchldhandler(pid_t pid, int * status);
63 extern void SetXtermCursor(Window win);
64 extern bool input_prohibited;
65 extern bool selection_possible;
66 extern char ascii_type;
67 extern void MenuPasteSelection(char at);
68 extern void FreeUpdateTimer();
70 BufferView::BufferView(LyXView * o, int xpos, int ypos,
71 int width, int height)
72 : pimpl_(new Pimpl(this, o, xpos, ypos, width, height))
75 the_locking_inset = 0;
80 BufferView::~BufferView()
87 Buffer * BufferView::buffer() const
89 return pimpl_->buffer_;
93 LyXView * BufferView::owner() const
95 return pimpl_->owner_;
99 void BufferView::pushIntoUpdateList(Inset * i)
101 pimpl_->updatelist.push(i);
105 Painter & BufferView::painter()
107 return pimpl_->workarea->getPainter();
111 void BufferView::buffer(Buffer * b)
113 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
115 if (pimpl_->buffer_) {
117 pimpl_->buffer_->delUser(this);
119 // Put the old text into the TextCache, but
120 // only if the buffer is still loaded.
121 // Also set the owner of the test to 0
124 if (lyxerr.debugging())
125 textcache.show(lyxerr, "BufferView::buffer");
130 // Set current buffer
133 if (bufferlist.getState() == BufferList::CLOSING) return;
136 // screen is always deleted when the buffer is changed.
137 delete pimpl_->screen;
140 // If we are closing the buffer, use the first buffer as current
141 if (!pimpl_->buffer_) {
142 pimpl_->buffer_ = bufferlist.first();
145 if (pimpl_->buffer_) {
146 lyxerr[Debug::INFO] << "Buffer addr: " << pimpl_->buffer_ << endl;
147 pimpl_->buffer_->addUser(this);
148 pimpl_->owner_->getMenus()->showMenus();
149 // If we don't have a text object for this, we make one
151 resizeCurrentBuffer();
153 pimpl_->updateScreen();
156 pimpl_->screen->first = pimpl_->screen->TopCursorVisible();
158 updateAllVisibleBufferRelatedPopups();
161 lyxerr[Debug::INFO] << " No Buffer!" << endl;
162 pimpl_->owner_->getMenus()->hideMenus();
164 pimpl_->workarea->redraw();
166 // Also remove all remaining text's from the testcache.
167 // (there should not be any!) (if there is any it is a
169 if (lyxerr.debugging())
170 textcache.show(lyxerr, "buffer delete all");
173 // should update layoutchoice even if we don't have a buffer.
174 pimpl_->owner_->updateLayoutChoice();
175 pimpl_->owner_->getMiniBuffer()->Init();
176 pimpl_->owner_->updateWindowTitle();
180 void BufferView::resize(int xpos, int ypos, int width, int height)
182 pimpl_->workarea->resize(xpos, ypos, width, height);
188 void BufferView::resize()
190 // This will resize the buffer. (Asger)
192 resizeCurrentBuffer();
196 void BufferView::redraw()
198 lyxerr[Debug::INFO] << "BufferView::redraw()" << endl;
199 pimpl_->workarea->redraw();
203 void BufferView::fitCursor()
205 if (pimpl_->screen) pimpl_->screen->FitCursor();
210 void BufferView::update()
212 if (pimpl_->screen) pimpl_->screen->Update();
216 void BufferView::updateScrollbar()
218 /* If the text is smaller than the working area, the scrollbar
219 * maximum must be the working area height. No scrolling will
222 if (!pimpl_->buffer_) {
223 pimpl_->workarea->setScrollbar(0, 1.0);
227 static long max2 = 0;
228 static long height2 = 0;
236 cbsf = pimpl_->screen->first;
238 // check if anything has changed.
240 height2 == pimpl_->workarea->height() &&
241 pimpl_->current_scrollbar_value == cbsf)
244 height2 = pimpl_->workarea->height();
245 pimpl_->current_scrollbar_value = cbsf;
247 if (cbth <= height2) { // text is smaller than screen
248 pimpl_->workarea->setScrollbar(0, 1.0); // right?
252 long maximum_height = pimpl_->workarea->height() * 3 / 4 + cbth;
256 double hfloat = pimpl_->workarea->height();
257 double maxfloat = maximum_height;
259 float slider_size = 0.0;
260 int slider_value = value;
262 pimpl_->workarea->setScrollbarBounds(0, text->height - pimpl_->workarea->height());
263 double lineh = text->DefaultHeight();
264 pimpl_->workarea->setScrollbarIncrements(lineh);
265 if (maxfloat > 0.0) {
266 if ((hfloat / maxfloat) * float(height2) < 3)
267 slider_size = 3.0/float(height2);
269 slider_size = hfloat / maxfloat;
271 slider_size = hfloat;
273 pimpl_->workarea->setScrollbar(slider_value, slider_size / pimpl_->workarea->height());
277 void BufferView::redoCurrentBuffer()
279 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
280 if (pimpl_->buffer_ && text) {
282 pimpl_->owner_->updateLayoutChoice();
287 int BufferView::resizeCurrentBuffer()
289 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
291 LyXParagraph * par = 0;
292 LyXParagraph * selstartpar = 0;
293 LyXParagraph * selendpar = 0;
302 pimpl_->owner_->getMiniBuffer()->Set(_("Formatting document..."));
305 par = text->cursor.par;
306 pos = text->cursor.pos;
307 selstartpar = text->sel_start_cursor.par;
308 selstartpos = text->sel_start_cursor.pos;
309 selendpar = text->sel_end_cursor.par;
310 selendpos = text->sel_end_cursor.pos;
311 selection = text->selection;
312 mark_set = text->mark_set;
314 text = new LyXText(this, pimpl_->workarea->workWidth(), pimpl_->buffer_);
316 // See if we have a text in TextCache that fits
317 // the new buffer_ with the correct width.
318 text = textcache.findFit(pimpl_->buffer_, pimpl_->workarea->workWidth());
320 if (lyxerr.debugging()) {
321 lyxerr << "Found a LyXText that fits:\n";
322 textcache.show(lyxerr, text);
324 // Set the owner of the newly found text
326 if (lyxerr.debugging())
327 textcache.show(lyxerr, "resizeCurrentBuffer");
329 text = new LyXText(this, pimpl_->workarea->workWidth(), pimpl_->buffer_);
332 pimpl_->updateScreen();
335 text->selection = true;
336 /* at this point just to avoid the Delete-Empty-Paragraph
337 * Mechanism when setting the cursor */
338 text->mark_set = mark_set;
340 text->SetCursor(selstartpar, selstartpos);
341 text->sel_cursor = text->cursor;
342 text->SetCursor(selendpar, selendpos);
343 text->SetSelection();
344 text->SetCursor(par, pos);
346 text->SetCursor(par, pos);
347 text->sel_cursor = text->cursor;
348 text->selection = false;
351 pimpl_->screen->first = pimpl_->screen->TopCursorVisible(); /* this will scroll the
352 * screen such that the
357 pimpl_->owner_->getMiniBuffer()->Init();
361 // Now if the title form still exist kill it
368 void BufferView::gotoError()
373 pimpl_->screen->HideCursor();
378 if (!text->GotoNextError()) {
380 || text->cursor.par != text->FirstParagraph()) {
382 text->cursor.par = text->FirstParagraph();
383 text->cursor.pos = 0;
384 if (!text->GotoNextError()) {
386 pimpl_->owner_->getMiniBuffer()
387 ->Set(_("No more errors"));
391 pimpl_->owner_->getMiniBuffer()->Set(_("No more errors"));
396 text->sel_cursor = text->cursor;
401 void C_BufferView_CursorToggleCB(FL_OBJECT * ob, long buf)
403 BufferView::cursorToggleCB(ob, buf);
408 // Callback for scrollbar up button
409 void BufferView::upCB(long time, int button)
411 if (pimpl_->buffer_ == 0) return;
415 pimpl_->scrollUpOnePage();
418 pimpl_->scrollDownOnePage();
421 pimpl_->scrollUp(time);
430 XSync(fl_get_display(), 0);
434 // Callback for scrollbar slider
435 void BufferView::scrollCB(double value)
437 extern bool cursor_follows_scrollbar;
439 if (pimpl_->buffer_ == 0) return;
441 pimpl_->current_scrollbar_value = long(value);
442 if (pimpl_->current_scrollbar_value < 0)
443 pimpl_->current_scrollbar_value = 0;
448 pimpl_->screen->Draw(pimpl_->current_scrollbar_value);
450 if (cursor_follows_scrollbar) {
451 LyXText * vbt = text;
452 int height = vbt->DefaultHeight();
454 if (vbt->cursor.y < pimpl_->screen->first + height) {
455 vbt->SetCursorFromCoordinates(0,
456 pimpl_->screen->first +
458 } else if (vbt->cursor.y >
459 pimpl_->screen->first + pimpl_->workarea->height() - height) {
460 vbt->SetCursorFromCoordinates(0,
461 pimpl_->screen->first +
462 pimpl_->workarea->height() -
470 // Callback for scrollbar down button
471 void BufferView::downCB(long time, int button)
473 if (pimpl_->buffer_ == 0) return;
477 pimpl_->scrollUpOnePage();
480 pimpl_->scrollDownOnePage();
483 pimpl_->scrollDown(time);
489 void BufferView::workAreaMotionNotify(int x, int y, unsigned int state)
491 pimpl_->workAreaMotionNotify(x, y, state);
495 extern int bibitemMaxWidth(Painter &, LyXFont const &);
498 /// Single-click on work area
499 void BufferView::workAreaButtonPress(int xpos, int ypos, unsigned int button)
501 pimpl_->workAreaButtonPress(xpos, ypos, button);
505 void BufferView::doubleClick(int x, int y, unsigned int button)
507 pimpl_->doubleClick(x, y, button);
511 void BufferView::tripleClick(int x, int y, unsigned int button)
513 pimpl_->tripleClick(x, y, button);
517 void BufferView::workAreaButtonRelease(int x, int y, unsigned int button)
519 pimpl_->workAreaButtonRelease(x, y, button);
523 void BufferView::workAreaExpose()
525 pimpl_->workAreaExpose();
529 // Callback for cursor timer
530 void BufferView::cursorToggleCB(FL_OBJECT * ob, long)
532 BufferView * view = static_cast<BufferView*>(ob->u_vdata);
534 // Quite a nice place for asyncron Inset updating, isn't it?
535 // Actually no! This is run even if no buffer exist... so (Lgb)
536 if (view && !view->pimpl_->buffer_) {
537 goto set_timer_and_return;
541 // On my quest to solve the gs render hangups I am now
542 // disabling the SIGHUP completely, and will do a wait
543 // now and then instead. If the guess that xforms somehow
544 // destroys something is true, this is likely (hopefully)
545 // to solve the problem...at least I hope so. Lgb
547 // ...Ok this seems to work...at least it does not make things
548 // worse so far. However I still see gs processes that hangs.
549 // I would really like to know _why_ they are hanging. Anyway
550 // the solution without the SIGCHLD handler seems to be easier
553 // When attaching gdb to a a running gs that hangs it shows
554 // that it is waiting for input(?) Is it possible for us to
555 // provide that input somehow? Or figure what it is expecing
558 // One solution is to, after some time, look if there are some
559 // old gs processes still running and if there are: kill them
562 // Another solution is to provide the user an option to rerender
563 // a picture. This would, for the picture in question, check if
564 // there is a gs running for it, if so kill it, and start a new
565 // rendering process.
567 // these comments posted to lyx@via
570 int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
571 if (pid == -1) // error find out what is wrong
572 ; // ignore it for now.
574 sigchldhandler(pid, &status);
577 view->pimpl_->updatelist.update(view);
579 if (view && !view->pimpl_->screen) {
580 goto set_timer_and_return;
583 if (view->pimpl_->lyx_focus && view->pimpl_->work_area_focus) {
584 if (!view->the_locking_inset) {
585 view->pimpl_->screen->CursorToggle();
587 view->the_locking_inset->
588 ToggleInsetCursor(view);
590 goto set_timer_and_return;
592 // Make sure that the cursor is visible.
593 if (!view->the_locking_inset) {
594 view->pimpl_->screen->ShowCursor();
596 if (!view->the_locking_inset->isCursorVisible())
597 view->the_locking_inset->
598 ToggleInsetCursor(view);
600 // This is only run when work_area_focus or lyx_focus is false.
603 XGetInputFocus(fl_display, &tmpwin, &tmp);
604 // Commenting this out, we have not had problems with this
605 // for a long time. We will probably work on this code later
606 // and we can reenable this debug code then. Now it only
607 // anoying when debugging. (Lgb)
608 //if (lyxerr.debugging(Debug::INFO)) {
609 // lyxerr << "tmpwin: " << tmpwin
610 // << "\nwindow: " << view->owner_->getForm()->window
611 // << "\nwork_area_focus: " << view->work_area_focus
612 // << "\nlyx_focus : " << view->lyx_focus
615 if (tmpwin != view->pimpl_->owner_->getForm()->window) {
616 view->pimpl_->lyx_focus = false;
619 view->pimpl_->lyx_focus = true;
620 if (!view->pimpl_->work_area_focus)
623 goto set_timer_and_return;
627 set_timer_and_return:
628 fl_set_timer(ob, 0.4);
634 void BufferView::workAreaSelectionNotify(Window win, XEvent * event)
636 pimpl_->workAreaSelectionNotify(win, event);
640 void BufferView::cursorPrevious()
642 if (!text->cursor.row->previous) return;
644 long y = pimpl_->screen->first;
645 Row * cursorrow = text->cursor.row;
646 text->SetCursorFromCoordinates(text->cursor.x_fix, y);
648 // This is to allow jumping over large insets
649 if ((cursorrow == text->cursor.row))
652 if (text->cursor.row->height < pimpl_->workarea->height())
653 pimpl_->screen->Draw(text->cursor.y
654 - text->cursor.row->baseline
655 + text->cursor.row->height
656 - pimpl_->workarea->height() + 1 );
660 void BufferView::cursorNext()
662 if (!text->cursor.row->next) return;
664 long y = pimpl_->screen->first;
665 text->GetRowNearY(y);
666 Row * cursorrow = text->cursor.row;
667 text->SetCursorFromCoordinates(text->cursor.x_fix, y
668 + pimpl_->workarea->height());
670 // This is to allow jumping over large insets
671 if ((cursorrow == text->cursor.row))
674 if (text->cursor.row->height < pimpl_->workarea->height())
675 pimpl_->screen->Draw(text->cursor.y
676 - text->cursor.row->baseline);
680 bool BufferView::available() const
682 if (pimpl_->buffer_ && text) return true;
687 void BufferView::beforeChange()
690 text->ClearSelection();
695 void BufferView::savePosition()
697 pimpl_->backstack.push(buffer()->fileName(),
703 void BufferView::restorePosition()
705 if (pimpl_->backstack.empty()) return;
708 string fname = pimpl_->backstack.pop(&x, &y);
711 Buffer * b = bufferlist.exists(fname) ?
712 bufferlist.getBuffer(fname) :
713 bufferlist.loadLyXFile(fname); // don't ask, just load it
715 text->SetCursorFromCoordinates(x, y);
720 void BufferView::update(signed char f)
722 owner()->updateLayoutChoice();
724 if (!text->selection && f > -3)
725 text->sel_cursor = text->cursor;
732 if (f != 3 && f != -3) {
737 if (f == 1 || f == -1) {
738 if (buffer()->isLyxClean()) {
739 buffer()->markDirty();
740 owner()->getMiniBuffer()->setTimer(4);
742 buffer()->markDirty();
748 void BufferView::smallUpdate(signed char f)
750 pimpl_->screen->SmallUpdate();
751 if (pimpl_->screen->TopCursorVisible()
752 != pimpl_->screen->first) {
760 if (!text->selection)
761 text->sel_cursor = text->cursor;
763 if (f == 1 || f == -1) {
764 if (buffer()->isLyxClean()) {
765 buffer()->markDirty();
766 owner()->getMiniBuffer()->setTimer(4);
768 buffer()->markDirty();
774 void BufferView::setState()
776 if (!lyxrc.rtl_support)
779 if (text->real_current_font.isVisibleRightToLeft()) {
780 if (pimpl_->owner_->getIntl()->primarykeymap)
781 pimpl_->owner_->getIntl()->KeyMapSec();
783 if (!pimpl_->owner_->getIntl()->primarykeymap)
784 pimpl_->owner_->getIntl()->KeyMapPrim();
789 void BufferView::insetSleep()
791 if (the_locking_inset && !inset_slept) {
792 the_locking_inset->GetCursorPos(slx, sly);
793 the_locking_inset->InsetUnlock(this);
799 void BufferView::insetWakeup()
801 if (the_locking_inset && inset_slept) {
802 the_locking_inset->Edit(this, slx, sly, 0);
808 void BufferView::insetUnlock()
810 if (the_locking_inset) {
811 if (!inset_slept) the_locking_inset->InsetUnlock(this);
812 the_locking_inset = 0;
819 bool BufferView::focus() const
821 return pimpl_->workarea->hasFocus();
825 void BufferView::focus(bool f)
827 if (f) pimpl_->workarea->setFocus();
831 bool BufferView::active() const
833 return pimpl_->workarea->active();
837 Painter & BufferView::getPainter() const
839 return pimpl_->workarea->getPainter();
843 unsigned short BufferView::paperWidth() const
845 return text->paperWidth();
849 bool BufferView::belowMouse() const
851 return pimpl_->workarea->belowMouse();
855 void BufferView::showCursor()
858 pimpl_->screen->ShowCursor();
862 void BufferView::hideCursor()
865 pimpl_->screen->HideCursor();
869 void BufferView::toggleSelection(bool b)
872 pimpl_->screen->ToggleSelection(b);
876 void BufferView::toggleToggle()
879 pimpl_->screen->ToggleToggle();
883 void BufferView::center()
886 if (text->cursor.y > pimpl_->workarea->height() / 2) {
887 pimpl_->screen->Draw(text->cursor.y - pimpl_->workarea->height() / 2);
889 pimpl_->screen->Draw(0);