From 05a4c4d79fff606a98da8acdc6a135b9b984f547 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Thu, 27 Feb 2003 16:24:14 +0000 Subject: [PATCH] A real, LyX label dialog. Part 1. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6301 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BufferView_pimpl.C | 12 +++- src/ChangeLog | 10 +++ src/factory.C | 6 ++ src/frontends/controllers/ChangeLog | 6 ++ src/frontends/controllers/ControlLabel.C | 19 ++++++ src/frontends/controllers/ControlLabel.h | 26 ++++++++ src/frontends/controllers/Makefile.am | 2 + src/insets/insetbibitem.h | 2 +- src/insets/insetlabel.C | 78 +++++++++++++++--------- src/insets/insetlabel.h | 4 ++ src/lyx_cb.C | 72 ++++++++++------------ src/lyx_cb.h | 2 +- 12 files changed, 165 insertions(+), 74 deletions(-) create mode 100644 src/frontends/controllers/ControlLabel.C create mode 100644 src/frontends/controllers/ControlLabel.h diff --git a/src/BufferView_pimpl.C b/src/BufferView_pimpl.C index f29895c2ab..6bfa31a502 100644 --- a/src/BufferView_pimpl.C +++ b/src/BufferView_pimpl.C @@ -1090,9 +1090,15 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in) owner_->getLyXFunc().setMessage(currentState(bv_)); break; - case LFUN_INSERT_LABEL: - MenuInsertLabel(bv_, ev.argument); - break; + case LFUN_INSERT_LABEL: { + // Try and generate a valid label + string const contents = ev.argument.empty() ? + getPossibleLabel(*bv_) : ev.argument; + InsetCommandParams icp("label", contents); + string data = InsetCommandMailer::params2string("label", icp); + owner_->getDialogs().show("label", data, 0); + } + break; case LFUN_BOOKMARK_SAVE: savePosition(strToUnsignedInt(ev.argument)); diff --git a/src/ChangeLog b/src/ChangeLog index 82df282882..348358d1d5 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,13 @@ +2003-02-27 Angus Leeming + + * BufferView_pimpl.C (dispatch): call a real, LyX dialog to insert + a label. + + * factory.C (createInset): add "label" to the factory. + + * lyx_cb.[Ch] (getPossibleLabel): rewrite MenuInsertLabel to return the + string and do no more. + 2003-02-27 Angus Leeming * commandtags.h: diff --git a/src/factory.C b/src/factory.C index 288aa0e150..31ccbb68b3 100644 --- a/src/factory.C +++ b/src/factory.C @@ -27,6 +27,7 @@ #include "insets/insetfloat.h" #include "insets/insetfoot.h" #include "insets/insetindex.h" +#include "insets/insetlabel.h" #include "insets/insetmarginal.h" #include "insets/insetminipage.h" #include "insets/insetnote.h" @@ -198,6 +199,11 @@ Inset * createInset(FuncRequest const & cmd) InsetCommandMailer::string2params(cmd.argument, icp); return new InsetIndex(icp); + } else if (name == "label") { + InsetCommandParams icp; + InsetCommandMailer::string2params(cmd.argument, icp); + return new InsetLabel(icp); + } else if (name == "ref") { InsetCommandParams icp; InsetCommandMailer::string2params(cmd.argument, icp); diff --git a/src/frontends/controllers/ChangeLog b/src/frontends/controllers/ChangeLog index eeaa7ff1c5..d0f18f792b 100644 --- a/src/frontends/controllers/ChangeLog +++ b/src/frontends/controllers/ChangeLog @@ -1,3 +1,9 @@ +2003-02-27 Angus Leeming + + * ControlLabel.[Ch]: new files. A controller for the Label dialog. + + * Makefile.am: add the new files. + 2003-02-27 Angus Leeming * ControlBibitem.C (c-tor): diff --git a/src/frontends/controllers/ControlLabel.C b/src/frontends/controllers/ControlLabel.C new file mode 100644 index 0000000000..ba842e17b4 --- /dev/null +++ b/src/frontends/controllers/ControlLabel.C @@ -0,0 +1,19 @@ +/** + * \file ControlLabel.C + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Angus Leeming + * + * Full author contact details are available in file CREDITS + */ + +#include + + +#include "ControlLabel.h" + + +ControlLabel::ControlLabel(Dialog & d) + : ControlCommand(d, "label") +{} diff --git a/src/frontends/controllers/ControlLabel.h b/src/frontends/controllers/ControlLabel.h new file mode 100644 index 0000000000..bd0657804c --- /dev/null +++ b/src/frontends/controllers/ControlLabel.h @@ -0,0 +1,26 @@ +// -*- C++ -*- +/** + * \file ControlLabel.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Angus Leeming + * + * Full author contact details are available in file CREDITS + */ + +#ifndef CONTROLLABEL_H +#define CONTROLLABEL_H + + +#include "ControlCommand.h" + +/** A controller for Label dialogs. + */ +class ControlLabel : public ControlCommand { +public: + /// + ControlLabel(Dialog &); +}; + +#endif // CONTROLLABEL_H diff --git a/src/frontends/controllers/Makefile.am b/src/frontends/controllers/Makefile.am index de72b27498..073eb54a63 100644 --- a/src/frontends/controllers/Makefile.am +++ b/src/frontends/controllers/Makefile.am @@ -67,6 +67,8 @@ libcontrollers_la_SOURCES= \ ControlIndex.C \ ControlIndex.h \ ControlInset.h \ + ControlLabel.C \ + ControlLabel.h \ ControlLog.C \ ControlLog.h \ ControlMath.C \ diff --git a/src/insets/insetbibitem.h b/src/insets/insetbibitem.h index 1d0444057c..8156b1ca7e 100644 --- a/src/insets/insetbibitem.h +++ b/src/insets/insetbibitem.h @@ -30,7 +30,7 @@ public: ~InsetBibitem(); /// Inset * clone(Buffer const &, bool same_id = false) const; - /// small wrapper for the time being + /// virtual dispatch_result localDispatch(FuncRequest const & cmd); /** Currently \bibitem is used as a LyX2.x command, so we need this method. diff --git a/src/insets/insetlabel.C b/src/insets/insetlabel.C index 537b85b312..459699b895 100644 --- a/src/insets/insetlabel.C +++ b/src/insets/insetlabel.C @@ -12,56 +12,78 @@ #include "insetlabel.h" -#include "support/LOstream.h" -#include "frontends/Alert.h" -#include "support/lstrings.h" //frontStrip, strip -#include "lyxtext.h" #include "buffer.h" -#include "gettext.h" #include "BufferView.h" +#include "funcrequest.h" +#include "gettext.h" +#include "lyxtext.h" + #include "support/lstrings.h" +#include "support/LOstream.h" +#include "support/lstrings.h" //frontStrip, strip using std::ostream; using std::vector; using std::pair; -/* Label. Used to insert a label automatically */ - InsetLabel::InsetLabel(InsetCommandParams const & p, bool) : InsetCommand(p) {} +InsetLabel::~InsetLabel() +{ + InsetCommandMailer mailer("label", *this); + mailer.hideDialog(); +} + + vector const InsetLabel::getLabelList() const { return vector(1, getContents()); } -void InsetLabel::edit(BufferView * bv, int, int, mouse_button::state) +void InsetLabel::edit(BufferView *, int, int, mouse_button::state) { - pair result = Alert::askForText(_("Enter label:"), getContents()); - if (result.first) { - string new_contents = trim(result.second); - if (!new_contents.empty() && - getContents() != new_contents) { - bv->buffer()->markDirty(); - bool flag = bv->ChangeRefsIfUnique(getContents(), - new_contents); - setContents(new_contents); -#if 0 - bv->text->redoParagraph(bv); - if (flag) { - bv->redraw(); - bv->fitCursor(); - } else - bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE); -#else - bv->updateInset(this, !flag); -#endif - } + InsetCommandMailer mailer("label", *this); + mailer.showDialog(); +} + + +dispatch_result InsetLabel::localDispatch(FuncRequest const & cmd) +{ + if (cmd.action != LFUN_INSET_APPLY) + return UNDISPATCHED; + + InsetCommandParams p; + InsetCommandMailer::string2params(cmd.argument, p); + if (p.getCmdName().empty()) + return UNDISPATCHED; + + bool clean = true; + if (view() && p.getContents() != params().getContents()) { + clean = view()->ChangeCitationsIfUnique(params().getContents(), + p.getContents()); } + + setParams(p); + if (view()) + view()->updateInset(this, !clean); + + return DISPATCHED; +// if (result.first) { +// string new_contents = trim(result.second); +// if (!new_contents.empty() && +// getContents() != new_contents) { +// bv->buffer()->markDirty(); +// bool flag = bv->ChangeRefsIfUnique(getContents(), +// new_contents); +// setContents(new_contents); +// bv->updateInset(this, !flag); +// } +// } } diff --git a/src/insets/insetlabel.h b/src/insets/insetlabel.h index 4cb3b2657d..b4478c3d82 100644 --- a/src/insets/insetlabel.h +++ b/src/insets/insetlabel.h @@ -20,10 +20,14 @@ public: /// InsetLabel(InsetCommandParams const &, bool same_id = false); /// + ~InsetLabel(); + /// virtual Inset * clone(Buffer const &, bool same_id = false) const { return new InsetLabel(params(), same_id); } /// + virtual dispatch_result localDispatch(FuncRequest const & cmd); + /// string const getScreenLabel(Buffer const *) const { return getContents(); } /// EDITABLE editable() const { return IS_EDITABLE; } diff --git a/src/lyx_cb.C b/src/lyx_cb.C index e357831411..c1ea6d4fdd 100644 --- a/src/lyx_cb.C +++ b/src/lyx_cb.C @@ -469,53 +469,43 @@ string getContentsOfAsciiFile(BufferView * bv, string const & f, bool asParagrap } -void MenuInsertLabel(BufferView * bv, string const & arg) +string const getPossibleLabel(BufferView const & bv) { - string label = arg; - if (label.empty()) { - Paragraph * par = bv->getLyXText()->cursor.par(); - LyXLayout_ptr layout = par->layout(); - if (layout->latextype == LATEX_PARAGRAPH && par->previous()) { - Paragraph * par2 = par->previous(); + Paragraph * par = bv.getLyXText()->cursor.par(); + LyXLayout_ptr layout = par->layout(); + if (layout->latextype == LATEX_PARAGRAPH && par->previous()) { + Paragraph * par2 = par->previous(); - LyXLayout_ptr const & layout2 = par2->layout(); + LyXLayout_ptr const & layout2 = par2->layout(); - if (layout2->latextype != LATEX_PARAGRAPH) { - par = par2; - layout = layout2; - } - } - string text = layout->latexname().substr(0, 3); - if (layout->latexname() == "theorem") - text = "thm"; // Create a correct prefix for prettyref - - text += ':'; - if (layout->latextype == LATEX_PARAGRAPH || - lyxrc.label_init_length < 0) - text.erase(); - string par_text = par->asString(bv->buffer(), false); - for (int i = 0; i < lyxrc.label_init_length; ++i) { - if (par_text.empty()) - break; - string head; - par_text = split(par_text, head, ' '); - if (i > 0) - text += '-'; // Is it legal to use spaces in - // labels ? - text += head; - } - - pair result = - Alert::askForText(_("Enter new label to insert:"), text); - if (result.first) { - label = trim(result.second); + if (layout2->latextype != LATEX_PARAGRAPH) { + par = par2; + layout = layout2; } } - if (!label.empty()) { - InsetCommandParams p("label", label); - InsetLabel * inset = new InsetLabel(p); - bv->insertInset(inset); + + string text = layout->latexname().substr(0, 3); + if (layout->latexname() == "theorem") + text = "thm"; // Create a correct prefix for prettyref + + text += ':'; + if (layout->latextype == LATEX_PARAGRAPH || + lyxrc.label_init_length < 0) + text.erase(); + + string par_text = par->asString(bv.buffer(), false); + for (int i = 0; i < lyxrc.label_init_length; ++i) { + if (par_text.empty()) + break; + string head; + par_text = split(par_text, head, ' '); + if (i > 0) + text += '-'; // Is it legal to use spaces in + // labels ? + text += head; } + + return text; } diff --git a/src/lyx_cb.h b/src/lyx_cb.h index 5db5524967..ce4220728b 100644 --- a/src/lyx_cb.h +++ b/src/lyx_cb.h @@ -33,7 +33,7 @@ void InsertAsciiFile(BufferView * bv, string const & f, bool asParagraph); /// string getContentsOfAsciiFile(BufferView * bv, string const & f, bool asParagraph); /// -void MenuInsertLabel(BufferView * bv, string const & arg); +string const getPossibleLabel(BufferView const & bv); /// void Reconfigure(BufferView * bv); #endif