lyx_mirror/src/insets/insetinfo.C
Lars Gullik Bjønnes 85a5ea7bfc change a lot of methods to begin with small char
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2150 a592a061-630c-0410-9148-cb99ea01b6c8
2001-06-28 10:25:20 +00:00

256 lines
5.2 KiB
C

/* This file is part of
* ======================================================
*
* LyX, The Document Processor
*
* Copyright 1995 Matthias Ettrich
* Copyright 1995-2001 The LyX Team.
*
* ====================================================== */
#include <config.h>
#include <cctype>
#ifdef __GNUG__
#pragma implementation
#endif
#include "insetinfo.h"
#include "paragraph.h"
#include "debug.h"
#include "gettext.h"
#include "lyx_gui_misc.h" // CancelCloseBoxCB
#include "buffer.h"
#include "support/lstrings.h"
#include "Painter.h"
#include "font.h"
#include "BufferView.h"
using std::ostream;
using std::endl;
/* Info, used for the Info boxes */
extern BufferView * current_view;
InsetInfo::InsetInfo()
: form(0), labelfont(LyXFont::ALL_SANE)
{
labelfont.decSize().decSize()
.setColor(LColor::note)
#ifndef NO_LATEX
.setLatex(LyXFont::OFF)
#endif
;
}
InsetInfo::InsetInfo(string const & str)
: contents(str), form(0), labelfont(LyXFont::ALL_SANE)
{
labelfont.decSize().decSize()
.setColor(LColor::note)
#ifndef NO_LATEX
.setLatex(LyXFont::OFF)
#endif
;
}
InsetInfo::~InsetInfo()
{
if (form) {
fl_hide_form(form);
fl_free_form(form);
form = 0;
}
}
int InsetInfo::ascent(BufferView *, LyXFont const &) const
{
return lyxfont::maxAscent(labelfont) + 1;
}
int InsetInfo::descent(BufferView *, LyXFont const &) const
{
return lyxfont::maxDescent(labelfont) + 1;
}
int InsetInfo::width(BufferView *, LyXFont const &) const
{
return 6 + lyxfont::width(_("Note"), labelfont);
}
void InsetInfo::draw(BufferView * bv, LyXFont const &,
int baseline, float & x, bool) const
{
Painter & pain = bv->painter();
// Draw as "Note" in a yellow box
x += 1;
pain.fillRectangle(int(x), baseline - ascent(bv, labelfont),
width(bv, labelfont) - 2,
ascent(bv, labelfont) + descent(bv, labelfont) - 2,
LColor::notebg);
pain.rectangle(int(x), baseline - ascent(bv, labelfont),
width(bv, labelfont) - 2,
ascent(bv, labelfont) + descent(bv, labelfont) - 2,
LColor::noteframe);
pain.text(int(x + 2), baseline, _("Note"), labelfont);
x += width(bv, labelfont) - 1;
}
void InsetInfo::write(Buffer const *, ostream & os) const
{
os << "Info\n" << contents;
}
void InsetInfo::read(Buffer const *, LyXLex & lex)
{
string tmp = lex.GetString(); // should be "Info"
if (tmp != "Info")
lyxerr << "ERROR (InsetInfo::Read): "
"consistency check 1 failed." << endl;
while (lex.IsOK()) {
if (!lex.EatLine())
// blank line in the file being read
// should we skip blank lines?
continue;
string const token = strip(lex.GetString());
lyxerr[Debug::PARSER] << "Note: " << token << endl;
if (token != "\\end_inset") {
contents += token + '\n';
}
else // token == "\\end_inset"
break;
}
// now remove the last '\n's
contents = strip(contents, '\n');
}
int InsetInfo::latex(Buffer const *, ostream &,
bool /*fragile*/, bool /*free_spc*/) const
{
return 0;
}
int InsetInfo::ascii(Buffer const *, ostream &, int) const
{
return 0;
}
int InsetInfo::linuxdoc(Buffer const *, ostream &) const
{
return 0;
}
int InsetInfo::docBook(Buffer const *, ostream &) const
{
return 0;
}
Inset::EDITABLE InsetInfo::editable() const
{
return IS_EDITABLE;
}
void InsetInfo::closeInfoCB(FL_OBJECT * ob, long)
{
InsetInfo * inset = static_cast<InsetInfo*>(ob->u_vdata);
string tmp = fl_get_input(inset->strobj);
Buffer * buffer = current_view->buffer();
if (tmp != inset->contents && !(buffer->isReadonly())) {
buffer->markDirty();
inset->contents = tmp;
}
if (inset->form) {
fl_hide_form(inset->form);
fl_free_form(inset->form);
inset->form = 0;
}
}
// This is just a wrapper.
extern "C"
void C_InsetInfo_CloseInfoCB(FL_OBJECT * ob, long data)
{
InsetInfo::closeInfoCB(ob, data);
}
string const InsetInfo::editMessage() const
{
return _("Opened note");
}
void InsetInfo::edit(BufferView *bv, int, int, unsigned int)
{
static int ow = -1;
static int oh;
if (bv->buffer()->isReadonly())
WarnReadonly(bv->buffer()->fileName());
if (!form) {
FL_OBJECT *obj;
form = fl_bgn_form(FL_UP_BOX, 400, 180);
strobj = obj = fl_add_input(FL_MULTILINE_INPUT, 10, 10, 380, 120, "");
fl_set_object_color(obj, FL_MCOL, FL_MCOL);
fl_set_object_resize(obj, FL_RESIZE_ALL);
fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
obj = fl_add_button(FL_NORMAL_BUTTON, 130, 140, 120, 30, idex(_("Close|#C^[")));
fl_set_object_resize(obj, FL_RESIZE_NONE);
fl_set_object_gravity(obj, SouthEastGravity, SouthEastGravity);
fl_set_object_callback(obj, C_InsetInfo_CloseInfoCB, 0);
obj->u_vdata = this;
fl_set_object_shortcut(obj, scex(_("Close|#C^[")), 1);
fl_end_form();
fl_set_form_atclose(form, CancelCloseBoxCB, 0);
}
fl_set_input(strobj, contents.c_str());
if (form->visible) {
fl_raise_form(form);
} else {
fl_show_form(form,
FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
_("Note"));
if (ow < 0) {
ow = form->w;
oh = form->h;
}
fl_set_form_minsize(form, ow, oh);
}
}
Inset * InsetInfo::clone(Buffer const &) const
{
return new InsetInfo(contents);
}
Inset::Code InsetInfo::lyxCode() const
{
return Inset::IGNORE_CODE;
}