]> git.lyx.org Git - features.git/commitdiff
Big shake up of all Alert::askQuestion/Confirmation into something
authorJohn Levon <levon@movementarian.org>
Sat, 29 Mar 2003 07:09:13 +0000 (07:09 +0000)
committerJohn Levon <levon@movementarian.org>
Sat, 29 Mar 2003 07:09:13 +0000 (07:09 +0000)
hopefully a lot better.

Warning: this may negatively affect your muscle memory :(

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6622 a592a061-630c-0410-9148-cb99ea01b6c8

21 files changed:
src/BufferView.C
src/ChangeLog
src/bufferlist.C
src/bufferlist.h
src/frontends/Alert.C
src/frontends/Alert.h
src/frontends/Alert_pimpl.h
src/frontends/ChangeLog
src/frontends/controllers/ChangeLog
src/frontends/controllers/ControlDocument.C
src/frontends/qt2/Alert_pimpl.C
src/frontends/qt2/ChangeLog
src/frontends/qt2/qt_helpers.C
src/frontends/qt2/qt_helpers.h
src/frontends/xforms/Alert_pimpl.C
src/frontends/xforms/ChangeLog
src/lyx_cb.C
src/lyx_main.C
src/lyxfunc.C
src/lyxvc.C
src/lyxvc.h

index a041a3d7164bbef0d799625c54e6f39731e64636..f78eab403d1509b7c216660dabc49344ac53e5f4 100644 (file)
@@ -106,7 +106,7 @@ void BufferView::buffer(Buffer * b)
 void BufferView::reload()
 {
        string const fn = buffer()->fileName();
-       if (bufferlist.close(buffer()))
+       if (bufferlist.close(buffer(), false))
                buffer(bufferlist.loadLyXFile(fn));
 }
 
index 9392f6b5fea9401d4a671586dfefd9debd7a8af9..c59c5477ab638c218b24b4304c89a0fc8fab5f74 100644 (file)
@@ -1,3 +1,19 @@
+2003-03-29  John Levon  <levon@movementarian.org>
+
+       * bufferlist.h:
+       * bufferlist.C: several UI fixes using Alert::prompt.
+       Fix the pointless looping quit code. Fix stupid revert
+       behaviour (bug 938)
+
+       * lyxvc.h:
+       * lyxvc.C:
+       * lyx_cb.C: use Alert::prompt
+
+       * lyx_main.C: remove a silly question
+
+       * lyxfunc.C: remove a couple of silly questions,
+       use Alert::prompt
+       
 2003-03-28  John Levon  <levon@movementarian.org>
 
        * text2.C: fix bug 974 (End on empty par)
index 0739d30c398ca488caef1fef277395c55f2f2b9c..89d9dec90bf7da706278707fdb07fbcda2af5dec 100644 (file)
@@ -1,15 +1,11 @@
-/* This file is part of
- * ======================================================
+/**
+ * \file bufferlist.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *           LyX, The Document Word Processor
+ * \author Lars Gullik Bjønnes
  *
- *           Copyright 1995 Matthias Ettrich
- *           Copyright 1995-2001 The LyX Team.
- *
- *           This file is Copyright 1996-2001
- *           Lars Gullik Bjønnes
- *
- * ======================================================
+ * Full author contact details are available in file CREDITS
  */
 
 #include <config.h>
@@ -65,55 +61,63 @@ bool BufferList::empty() const
 }
 
 
-bool BufferList::qwriteOne(Buffer * buf, string const & fname,
-                          string & unsaved_list)
+bool BufferList::quitWriteBuffer(Buffer * buf)
 {
-       bool reask = true;
-       while (reask) {
-               switch (Alert::askConfirmation(_("Changes in document:"),
-                                      fname,
-                                      _("Save document?"))) {
-               case 1: // Yes
-                       // FIXME: WriteAs can be asynch !
-                       if (buf->isUnnamed())
-                               reask = !WriteAs(current_view, buf);
-                       else {
-                               reask = !MenuWrite(current_view, buf);
-                       }
-                       break;
-               case 2: // No
-                       // if we crash after this we could
-                       // have no autosave file but I guess
-                       // this is really inprobable (Jug)
-                       if (buf->isUnnamed()) {
-                               removeAutosaveFile(buf->fileName());
-                       }
+       string file;
+       if (buf->isUnnamed())
+               file = OnlyFilename(buf->fileName());
+       else
+               file = MakeDisplayPath(buf->fileName(), 30);
+
+#if USE_BOOST_FORMAT
+       boost::format fmt(_("The document %1$s has unsaved changes.\n\nDo you want to save the document?"));
+       fmt % file;
+       string text = fmt.str();
+#else
+       string text = _("The document ");
+       text += file + _(" has unsaved changes.\n\nDo you want to save the document?");
+#endif
+       int const ret = Alert::prompt(_("Save changed document?"),
+               text, 0, _("&Save"), _("&Discard"), _("&Cancel quit"));
 
-                       unsaved_list += MakeDisplayPath(fname, 50) + "\n";
-                       return true;
-               case 3: // Cancel
+       if (ret == 0) {
+               // FIXME: WriteAs can be asynch !
+               // but not right now...maybe we should remove that
+
+               bool succeeded;
+
+               if (buf->isUnnamed())
+                       succeeded = !WriteAs(current_view, buf);
+               else
+                       succeeded = !MenuWrite(current_view, buf);
+
+               if (!succeeded)
                        return false;
-               }
+       } else if (ret == 1) {
+               // if we crash after this we could
+               // have no autosave file but I guess
+               // this is really inprobable (Jug)
+               if (buf->isUnnamed())
+                       removeAutosaveFile(buf->fileName());
+
+       } else {
+               return false;
        }
+
        return true;
 }
 
 
-bool BufferList::qwriteAll()
+bool BufferList::quitWriteAll()
 {
-       string unsaved;
        BufferStorage::iterator it = bstore.begin();
        BufferStorage::iterator end = bstore.end();
        for (; it != end; ++it) {
-               if (!(*it)->isClean()) {
-                       string fname;
-                       if ((*it)->isUnnamed())
-                               fname = OnlyFilename((*it)->fileName());
-                       else
-                               fname = MakeDisplayPath((*it)->fileName(), 50);
-                       if (!qwriteOne(*it, fname, unsaved)) // cancel the request!
-                               return false;
-               }
+               if ((*it)->isClean())
+                       continue;
+
+               if (!quitWriteBuffer(*it))
+                       return false;
        }
 
        return true;
@@ -154,47 +158,50 @@ void BufferList::closeAll()
        textcache.clear();
 
        while (!bstore.empty()) {
-               close(bstore.front());
+               close(bstore.front(), true);
        }
 }
 
 
-bool BufferList::close(Buffer * buf)
+bool BufferList::close(Buffer * buf, bool ask)
 {
        lyx::Assert(buf);
 
-       if (!buf->paragraphs.empty() && !buf->isClean() && !quitting) {
-               string fname;
-               if (buf->isUnnamed())
-                       fname = OnlyFilename(buf->fileName());
-               else
-                       fname = MakeDisplayPath(buf->fileName(), 50);
-               bool reask = true;
-               while (reask) {
-                       switch (Alert::askConfirmation(_("Changes in document:"),
-                                              fname,
-                                              _("Save document?"))) {
-                       case 1: // Yes
-                               if (buf->isUnnamed())
-                                       reask = !WriteAs(current_view, buf);
-                               else if (buf->save()) {
-                                       lastfiles->newFile(buf->fileName());
-                                       reask = false;
-                               } else {
-                                       return false;
-                               }
-                               break;
-                       case 2:
-                               if (buf->isUnnamed()) {
-                                       removeAutosaveFile(buf->fileName());
-                               }
-                               reask = false;
-                               break;
-                       case 3: // Cancel
+       if (!ask || buf->isClean() || quitting || buf->paragraphs.empty()) {
+               release(buf);
+               return true;
+       }
+
+       string fname;
+       if (buf->isUnnamed())
+               fname = OnlyFilename(buf->fileName());
+       else
+               fname = MakeDisplayPath(buf->fileName(), 30);
+#if USE_BOOST_FORMAT
+       boost::format fmt(_("The document %1$s has unsaved changes.\n\nDo you want to save the document?"));
+       fmt % fname;
+       string text = fmt.str();
+#else
+       string text = _("The document ");
+       text += fname + _(" has unsaved changes.\n\nDo you want to save the document?");
+#endif
+       int const ret = Alert::prompt(_("Save changed document?"),
+               text, 0, _("&Save"), _("&Discard"));
+
+       if (ret == 0) {
+               if (buf->isUnnamed()) {
+                       if (!WriteAs(current_view, buf))
                                return false;
-                       }
+               } else if (buf->save()) {
+                       lastfiles->newFile(buf->fileName());
+               } else {
+                       return false;
                }
        }
+       
+       if (buf->isUnnamed()) {
+               removeAutosaveFile(buf->fileName());
+       }
 
        release(buf);
        return true;
@@ -340,9 +347,19 @@ Buffer * BufferList::readFile(string const & s, bool ronly)
        if (fileInfoE.exist() && fileInfo2.exist()) {
                if (fileInfoE.getModificationTime()
                    > fileInfo2.getModificationTime()) {
-                       if (Alert::askQuestion(_("An emergency save of this document exists!"),
-                                       MakeDisplayPath(s, 50),
-                                       _("Try to load that instead?"))) {
+                       string const file = MakeDisplayPath(s, 20);
+#if USE_BOOST_FORMAT
+                       boost::format fmt(_("An emergency save of the document %1$s exists.\n\nRecover emergency save?"));
+                       fmt % file;
+                       string text = fmt.str();
+#else
+                       string text = _("An emergency save of the document ");
+                       text += file + _(" exists.\n\nRecover emergency save?");
+#endif
+                       int const ret = Alert::prompt(_("Load emergency save?"),
+                               text, 0, _("&Recover"), _("&Load original"));
+
+                       if (ret == 0) {
                                ts = e;
                                // the file is not saved if we load the
                                // emergency file.
@@ -364,9 +381,19 @@ Buffer * BufferList::readFile(string const & s, bool ronly)
                if (fileInfoA.exist() && fileInfo2.exist()) {
                        if (fileInfoA.getModificationTime()
                            > fileInfo2.getModificationTime()) {
-                               if (Alert::askQuestion(_("Autosave file is newer."),
-                                               MakeDisplayPath(s, 50),
-                                               _("Load that one instead?"))) {
+                               string const file = MakeDisplayPath(s, 20);
+#if USE_BOOST_FORMAT
+                               boost::format fmt(_("The backup of the document %1$s is newer.\n\nLoad the backup instead?"));
+                               fmt % file;
+                               string text = fmt.str();
+#else
+                               string text = _("The backup of the document ");
+                               text += file + _(" is newer.\n\nLoad the backup instead?");
+#endif
+                               int const ret = Alert::prompt(_("Load backup?"),
+                                       text, 0, _("&Load backup"), _("Load &original"));
+
+                               if (ret == 0) {
                                        ts = a;
                                        // the file is not saved if we load the
                                        // autosave file.
@@ -469,11 +496,21 @@ Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
 
        // file already open?
        if (exists(s)) {
-               if (Alert::askQuestion(_("Document is already open:"),
-                               MakeDisplayPath(s, 50),
-                               _("Do you want to reload that document?"))) {
-                       // Reload is accomplished by closing and then loading
-                       if (!close(getBuffer(s))) {
+               string const file = MakeDisplayPath(s, 20);
+#if USE_BOOST_FORMAT
+               boost::format fmt(_("The document %1$s is already loaded.\n\nDo you want to revert to the saved version?"));
+               fmt % file;
+               string text = fmt.str();
+#else
+               string text = _("The document ");
+               text += file + _(" is already loaded.\n\nDo you want to revert to the saved version?");
+#endif
+               int const ret = Alert::prompt(_("Revert to saved document?"),
+                       text, 1, _("&Revert"), _("&Switch to document"));
+
+               if (ret == 0) {
+                       // FIXME: should be LFUN_REVERT
+                       if (!close(getBuffer(s), false)) {
                                return 0;
                        }
                        // Fall through to new load. (Asger)
@@ -483,6 +520,7 @@ Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
                        return getBuffer(s);
                }
        }
+
        Buffer * b = 0;
        bool ro = false;
        switch (IsFileWriteable(s)) {
@@ -495,12 +533,22 @@ Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
                        b->lyxvc.file_found_hook(s);
                }
                break; //fine- it's r/w
-       case -1:
+       case -1: {
+               string const file = MakeDisplayPath(s, 20);
                // Here we probably should run
                if (LyXVC::file_not_found_hook(s)) {
-                       // Ask if the file should be checked out for
-                       // viewing/editing, if so: load it.
-                       if (Alert::askQuestion(_("Do you want to retrieve file under version control?"))) {
+#if USE_BOOST_FORMAT
+                       boost::format fmt(_("Do you want to retrieve the document %1$s from version control?"));
+                       fmt % file;
+                       string text = fmt.str();
+#else
+                       string text = _("Do you want to retrieve the document ");
+                       text += file + _(" from version control?");
+#endif
+                       int const ret = Alert::prompt(_("Retrieve from version control?"),
+                               text, 0, _("&Retrieve"), _("&Cancel"));
+
+                       if (ret == 0) {
                                // How can we know _how_ to do the checkout?
                                // With the current VC support it has to be,
                                // a RCS file since CVS do not have special ,v files.
@@ -508,14 +556,23 @@ Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
                                return loadLyXFile(filename, tolastfiles);
                        }
                }
-               if (Alert::askQuestion(_("Cannot open specified file:"),
-                               MakeDisplayPath(s, 50),
-                               _("Create new document with this name?")))
-                       {
-                               // Find a free buffer
-                               b = newFile(s, string(), true);
-                       }
+
+#if USE_BOOST_FORMAT
+               boost::format fmt(_("The document %1$s does not yet exist. Do you want to create a new document?"));
+               fmt % file;
+               string text = fmt.str();
+#else
+               string text = _("The document ");
+               text += file + _(" does not yet exist. Do you want to create a new document?");
+#endif
+               int const ret = Alert::prompt(_("Create new document?"),
+                       text, 0, _("&Create"), _("Cancel"));
+
+               if (ret == 0)
+                       b = newFile(s, string(), true);
+
                break;
+               }
        }
 
        if (b && tolastfiles)
index 573f08b5c215fe92cf26eff8a1d1dc88c5cc633c..0dbd2434b380af635077c51efb3d9d5dd9f8732b 100644 (file)
@@ -1,10 +1,13 @@
 // -*- C++ -*-
-/** \file
- *  Copyright 2002 the LyX Team
- *  Read the file COPYING
+/**
+ * \file bufferlist.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Lars Gullik Bjønnes
  *
- *  \author Lars Gullik Bjønnes
-*/
+ * Full author contact details are available in file CREDITS
+ */
 
 #ifndef BUFFER_LIST_H
 #define BUFFER_LIST_H
@@ -36,8 +39,8 @@ public:
        Buffer * loadLyXFile(string const & filename,
                             bool tolastfiles = true);
 
-       /// write all buffers, asking the user
-       bool qwriteAll();
+       /// write all buffers, asking the user, returns false if cancelled
+       bool quitWriteAll();
 
        /// create a new buffer
        Buffer * newBuffer(string const & s, bool ronly = false);
@@ -63,7 +66,7 @@ public:
        void emergencyWriteAll();
 
        /// close buffer. Returns false if cancelled by user
-       bool close(Buffer * buf);
+       bool close(Buffer * buf, bool ask);
 
        /// return true if no buffers loaded
        bool empty() const;
@@ -86,9 +89,8 @@ public:
        void setCurrentAuthor(string const & name, string const & email);
 
 private:
-       /// ask to save a buffer on quit
-       bool qwriteOne(Buffer * buf, string const & fname,
-                      string & unsaved_list);
+       /// ask to save a buffer on quit, returns false if should cancel
+       bool quitWriteBuffer(Buffer * buf);
 
        typedef std::vector<Buffer *> BufferStorage;
 
index dba128cabb4446102c0e9d05bb6dc9f26711dd52..334e8d103ba84879a4de915378e71e2e0b3db763 100644 (file)
@@ -46,50 +46,23 @@ void Alert::err_alert(string const & s1, string const & s2)
 }
 
 
-bool Alert::askQuestion(string const & s1, string const & s2,
-                       string const & s3, bool default_value)
+int Alert::prompt(string const & title, string const & question,
+           int default_button,
+          string const & b1, string const & b2, string const & b3)
 {
-       if (!lyxrc.use_gui) {
-               lyxerr << "----------------------------------------" << endl
-                      << s1 << endl;
-               if (!s2.empty())
-                       lyxerr << s2 << endl;
-               if (!s3.empty())
-                       lyxerr << s3 << endl;
-               lyxerr << "Assuming answer is "
-                      << (default_value ? "yes" : "no")
-                      << endl
-                      << "----------------------------------------" << endl;
-               return default_value;
-       } else {
-               return askQuestion_pimpl(s1, s2, s3);
-       }
-}
-
-
-int Alert::askConfirmation(string const & s1, string const & s2,
-                          string const & s3, int default_value)
-{
-       if (!lyxrc.use_gui) {
-               lyxerr << "----------------------------------------" << endl
-                      << s1 << endl;
-               if (!s2.empty())
-                       lyxerr << s2 << endl;
-               if (!s3.empty())
-                       lyxerr << s3 << endl;
-               lyxerr << "Assuming answer is ";
-               if (default_value == 1)
-                       lyxerr << "yes";
-               else if (default_value == 2)
-                       lyxerr << "no";
-               else
-                       lyxerr << "cancel";
-               lyxerr << endl
-                      << "----------------------------------------" << endl;
-               return default_value;
-       } else {
-               return askConfirmation_pimpl(s1, s2, s3);
+       if (lyxrc.use_gui)
+               return prompt_pimpl(title, question, default_button, b1, b2, b3);
+
+       lyxerr << title << endl;
+       lyxerr << "----------------------------------------" << endl;
+       lyxerr << question << endl;
+       lyxerr << "Assuming answer is ";
+       switch (default_button) {
+               case 0: lyxerr << b1 << endl;
+               case 1: lyxerr << b2 << endl;
+               case 2: lyxerr << b3 << endl;
        }
+       return default_button;
 }
 
 
index bd56b1a76e77c862162315678e6c791c2493581c..0e235546a4a63feb80be761785f3ef8d2f1e1d5d 100644 (file)
 
 namespace Alert {
 
+/**
+ * Prompt for a question. Returns 0-2 for the chosen button.
+ * Set default_button to a reasonable value. b1-b3 should have
+ * accelerators marked with an '&'. title should be a short summary.
+ * Strings should be gettextised. Please think about the poor user.
+ *
+ * Remember to use boost::format. If you make any of these buttons
+ * "Yes" or "No", I will personally come around to your house and
+ * slap you with fish, and not in an enjoyable way either.
+ */
+int prompt(string const & title, string const & question,
+           int default_button,
+          string const & b1, string const & b2, string const & b3 = string());
+
 /// show an alert message
 void alert(string const & title, string const & s1 = string(),
           string const & s2 = string());
@@ -25,14 +39,6 @@ void alert(string const & title, string const & s1 = string(),
 /// show an alert message and strerror(errno)
 void err_alert(string const & s1, string const & s2 = string());
 
-/// ask a question
-bool askQuestion(string const & s1, string const & s2 = string(),
-                string const & s3 = string(), bool default_value = true);
-
-/// Returns 1 for yes, 2 for no, 3 for cancel.
-int askConfirmation(string const & s1, string const & s2 = string(),
-                   string const & s3 = string(), int default_value = 1);
-
 /// Asks for a text
 std::pair<bool, string> const
 askForText(string const & msg,
index 24e3e6e44528178c3f1ff2a91943e5b2b9f6fabc..b29d1fd9e014bd335137c2d5df3725fa45e44107 100644 (file)
 #include "debug.h"
 
 // GUI-specific implementations
+
 void alert_pimpl(string const & s1, string const & s2, string const & s3);
-bool askQuestion_pimpl(string const & s1, string const & s2, string const & s3);
-int askConfirmation_pimpl(string const & s1, string const & s2, string const & s3);
+
+int prompt_pimpl(string const & title, string const & question,
+           int default_button,
+          string const & b1, string const & b2, string const & b3);
+
 std::pair<bool, string> const askForText_pimpl(string const & msg, string const & dflt);
index 1c96937f315585746566fa2bed93d12bd524a080..bc3802240eadc76e7d818b32a45b88b8d5114709 100644 (file)
@@ -1,3 +1,10 @@
+2003-03-29  John Levon  <levon@movementarian.org>
+
+       * Alert.h:
+       * Alert.C:
+       * Alert_pimpl.h: fix up askQuestion/askConfirmation
+       into prompt, to discourage Yes/No questions.
+
 2003-03-26  Angus Leeming  <leeming@lyx.org>
 
        * Dialogs.h: remove showThesaurus.
index 6124fefb86e3f09b9d83a3553780b670017ebd9d..617b0fc361d3a8d4473323c0060767c9b50c700a 100644 (file)
@@ -1,3 +1,7 @@
+2003-03-29  John Levon  <levon@movementarian.org>
+
+       * ControlDocument.C: comment out seemingly silly question
+
 2003-03-27  Lars Gullik Bjønnes  <larsbj@gullik.net>
 
        * ControlDialog.tmpl (show): qualify emergency_exit_ with this->
index d4fe5a52718a68abb89740a0443f308bce7bd322..78e2c0b51bad4dfe097e161aeb16587f1b1376ae 100644 (file)
@@ -157,10 +157,13 @@ bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
 
 void ControlDocument::saveAsDefault()
 {
+// Can somebody justify this ? I think it should be removed - jbl
+#if 0
        if (!Alert::askQuestion(_("Do you want to save the current settings"),
                                _("for the document layout as default?"),
                                _("(they will be valid for any new document)")))
                return;
+#endif
 
        lv_.buffer()->params.preamble = bp_->preamble;
 
index 6a306c644290378aa132f68bc20f009f60396bcf..b63b5805854d6126476a947afeabac64831679a9 100644 (file)
@@ -37,17 +37,21 @@ void alert_pimpl(string const & s1, string const & s2, string const & s3)
 }
 
 
-bool askQuestion_pimpl(string const & s1, string const & s2, string const & s3)
+int prompt_pimpl(string const & tit, string const & question,
+           int default_button,
+          string const & b1, string const & b2, string const & b3)
 {
-       return !(QMessageBox::information(0, "LyX", toqstr(s1 + '\n' + s2 + '\n' + s3),
-               qt_("&Yes"), qt_("&No"), 0, 1));
-}
-
+#if USE_BOOST_FORMAT
+       boost::format fmt(_("LyX: %1$s"));
+       fmt % tit;
+       string const title = fmt.str();
+#else
+       string const title = _("LyX: ") + tit;
+#endif
 
-int askConfirmation_pimpl(string const & s1, string const & s2, string const & s3)
-{
-       return (QMessageBox::information(0, "LyX", toqstr(s1 + '\n' + s2 + '\n' + s3),
-               qt_("&Yes"), qt_("&No"), qt_("&Cancel"), 0, 2)) + 1;
+       return QMessageBox::information(0, toqstr(title), toqstr(formatted(question)),
+               toqstr(b1), toqstr(b2), b3.empty() ? QString::null : toqstr(b3),
+               default_button);
 }
 
 
index 72dafec25d4c95db6efd5f2c2388253e4700dce9..5038e8191c7faaf209bea9c7f4242077c5946a61 100644 (file)
@@ -1,3 +1,7 @@
+2003-03-29  John Levon  <levon@movementarian.org>
+
+       * Alert_pimpl.C: implement prompt()
+
 2003-03-29  John Levon  <levon@movementarian.org>
 
        * QDocument.C:
index ebd844a7dee1e81c54ac91a646667e61fe6c153d..e9fa12f85c98a2dcfc958bc23eb274de5c054cad 100644 (file)
@@ -23,6 +23,7 @@
 
 using std::pair;
 using std::make_pair;
+using std::min;
 
 string makeFontName(string const & family, string const & foundry)
 {
@@ -117,3 +118,60 @@ string const fromqstr(QString const & str)
        char const * tmpcstr = tmpstr;
        return tmpcstr;
 }
+
+
+string const formatted(string const & text, int w)
+{
+       string sout;
+
+       if (text.empty())
+               return sout;
+
+       string::size_type curpos = 0;
+       string line;
+
+       for (;;) {
+               string::size_type const nxtpos1 = text.find(' ',  curpos);
+               string::size_type const nxtpos2 = text.find('\n', curpos);
+               string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
+
+               string const word = nxtpos == string::npos ?
+                       text.substr(curpos) : text.substr(curpos, nxtpos-curpos);
+
+               bool const newline = (nxtpos2 != string::npos &&
+                                     nxtpos2 < nxtpos1);
+
+               string const line_plus_word =
+                       line.empty() ? word : line + ' ' + word;
+
+               // FIXME: make w be size_t
+               if (line_plus_word.length() >= w) {
+                       sout += line + '\n';
+                       if (newline) {
+                               sout += word + '\n';
+                               line.erase();
+                       } else {
+                               line = word;
+                       }
+
+               } else if (newline) {
+                       sout += line_plus_word + '\n';
+                       line.erase();
+
+               } else {
+                       if (!line.empty())
+                               line += ' ';
+                       line += word;
+               }
+
+               if (nxtpos == string::npos) {
+                       if (!line.empty())
+                               sout += line;
+                       break;
+               }
+
+               curpos = nxtpos + 1;
+       }
+
+       return sout;
+}
index 39e791a3a03869b8ba19432419e709d5b8c5c518..a9eb174b08e3e7dedfb828efc247612c46d9866c 100644 (file)
@@ -34,6 +34,9 @@ string widgetsToLength(QLineEdit const * input, LengthCombo const * combo);
 void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
        string const & len, LyXLength::UNIT default_unit);
 
+/// format a string to the given width
+string const formatted(string const & text, int w = 80);
+
 /**
  * toqstr - convert char * into unicode
  *
index 5e68365ad4dc0bfe91ff4e898544a59ba58ab857..816c55df29bac08c669b4e4a27cdde3d11f82d1b 100644 (file)
@@ -29,25 +29,21 @@ void alert_pimpl(string const & s1, string const & s2, string const & s3)
 }
 
 
-bool askQuestion_pimpl(string const & s1, string const & s2, string const & s3)
-{
-       fl_set_resource("flQuestion.yes.label", idex(_("Yes|#y")).c_str());
-       fl_set_resource("flQuestion.no.label", idex(_("No|#n")).c_str());
-       return fl_show_question((s1 + "\n" + s2 + "\n" + s3).c_str(), 0);
-}
-
+#warning error needs fixing up
+#warning a "&Save" -> "Save|#S" is whats needed
+#warning and a format maybe, and fixup default_button ?
 
-int askConfirmation_pimpl(string const & s1, string const & s2, string const & s3)
+int prompt_pimpl(string const & title, string const & question,
+           int default_button,
+          string const & b1, string const & b2, string const & b3);
 {
-       string const yes    = _("Yes|#Y");
-       string const no     = _("No|#N");
-       string const cancel = _("Cancel|^[");
-       
-       fl_set_choices_shortcut(scex(yes).c_str(), scex(no).c_str(),
-                               scex(cancel).c_str());
-       return fl_show_choice(s1.c_str(), s2.c_str(), s3.c_str(),
-                             3, idex(yes).c_str(), idex(no).c_str(),
-                             idex(cancel).c_str(), 3);
+       if (b3.empty()) {
+               return fl_show_choice(title.c_str(), question.c_str(), "",
+                       2, b1.c_str(), b2.c_str(), 2);
+       } else {
+               return fl_show_choice(title.c_str(), question.c_str(), "",
+                       3, b1.c_str(), b2.c_str(), b3.c_str(), 3);
+       }
 }
 
 
index 1d19d06604abdd7afbdb30298ca24af3136ca39c..b81ba40a26f2be4b8c270ddef19a4227bc924187 100644 (file)
@@ -1,3 +1,7 @@
+2003-03-29  John Levon  <levon@movementarian.org>
+
+       * Alert_pimpl.C: implement prompt()
+
 2003-03-26  Angus Leeming  <leeming@lyx.org>
 
        * combox.C (show):
index df1a9530ef5dac1fc49c8979a8dc18703f8f44e8..f47700b01b843f6eb162ad2e6d7457eb2596829d 100644 (file)
@@ -75,28 +75,35 @@ void ShowMessage(Buffer const * buf,
 // Menu callbacks
 //
 
-//
-// File menu
-//
-// should be moved to lyxfunc.C
 bool MenuWrite(BufferView * bv, Buffer * buffer)
 {
-       if (!buffer->save()) {
-               if (Alert::askQuestion(_("Save failed. Rename and try again?"),
-                               MakeDisplayPath(buffer->fileName(), 50),
-                               _("(If not, document is not saved.)"))) {
-                       return WriteAs(bv, buffer);
-               }
-               return false;
-       } else
+       if (buffer->save()) {
                lastfiles->newFile(buffer->fileName());
-       return true;
+               return true;
+       }
+
+       // FIXME: we don't tell the user *WHY* the save failed !!
+
+       string const file = MakeDisplayPath(buffer->fileName(), 30);
+
+#if USE_BOOST_FORMAT
+       boost::format fmt(_("The document %1$s could not be saved.\n\nDo you want to rename the document and try again?"));
+       fmt % file;
+       string text = fmt.str();
+#else
+       string text = _("The document ");
+       text += file + _(" could not be saved.\n\nDo you want to rename the document and try again?");
+#endif
+       int const ret = Alert::prompt(_("Rename and save?"),
+               text, 0, _("&Rename"), _("&Cancel"));
+
+       if (ret == 0)
+               return WriteAs(bv, buffer);
+       return false;
 }
 
 
 
-// should be moved to BufferView.C
-// Half of this func should be in LyXView, the rest in BufferView.
 bool WriteAs(BufferView * bv, Buffer * buffer, string const & filename)
 {
        string fname = buffer->fileName();
@@ -134,36 +141,22 @@ bool WriteAs(BufferView * bv, Buffer * buffer, string const & filename)
        } else
                fname = filename;
 
-       // Same name as we have already?
-       if (!buffer->isUnnamed() && fname == oldname) {
-               if (!Alert::askQuestion(_("Same name as document already has:"),
-                                MakeDisplayPath(fname, 50),
-                                _("Save anyway?")))
-                       return false;
-               // Falls through to name change and save
-       }
-       // No, but do we have another file with this name open?
-       else if (!buffer->isUnnamed() && bufferlist.exists(fname)) {
-               if (Alert::askQuestion(_("Another document with same name open!"),
-                               MakeDisplayPath(fname, 50),
-                               _("Replace with current document?")))
-                       {
-                               bufferlist.close(bufferlist.getBuffer(fname));
-
-                               // Ok, change the name of the buffer, but don't save!
-                               buffer->setFileName(fname);
-                               buffer->markDirty();
-
-                               ShowMessage(buffer, _("Document renamed to '"),
-                                               MakeDisplayPath(fname), _("', but not saved..."));
-               }
-               return false;
-       } // Check whether the file exists
-       else {
-               FileInfo const myfile(fname);
-               if (myfile.isOK() && !Alert::askQuestion(_("Document already exists:"),
-                                                 MakeDisplayPath(fname, 50),
-                                                 _("Replace file?")))
+       FileInfo const myfile(fname);
+       if (myfile.isOK()) {
+               string const file = MakeDisplayPath(fname, 30);
+
+#if USE_BOOST_FORMAT
+               boost::format fmt(_("The document %1$s already exists.\n\nDo you want to over-write that document?"));
+               fmt % file;
+               string text = fmt.str();
+#else
+               string text = _("The document ");
+               text += file + _(" already exists.\n\nDo you want to over-write that document?");
+#endif
+               int const ret = Alert::prompt(_("Over-write document?"),
+                       text, 1, _("&Over-write"), _("&Cancel"));
+
+               if (ret == 1)
                        return false;
        }
 
@@ -174,13 +167,11 @@ bool WriteAs(BufferView * bv, Buffer * buffer, string const & filename)
        buffer->setUnnamed(false);
 
        if (!MenuWrite(bv, buffer)) {
-           buffer->setFileName(oldname);
-           buffer->setUnnamed(unnamed);
-           ShowMessage(buffer, _("Document could not be saved!"),
-                       _("Holding the old name."), MakeDisplayPath(oldname));
-           return false;
+               buffer->setFileName(oldname);
+               buffer->setUnnamed(unnamed);
+               return false;
        }
-       // now remove the oldname autosave file if existant!
+
        removeAutosaveFile(oldname);
        return true;
 }
@@ -222,7 +213,7 @@ void QuitLyX()
        lyxerr[Debug::INFO] << "Running QuitLyX." << endl;
 
        if (lyxrc.use_gui) {
-               if (!bufferlist.qwriteAll())
+               if (!bufferlist.quitWriteAll())
                        return;
 
                lastfiles->writeFile(lyxrc.lastfiles);
index 6e3699fdaba9b08f7fc33d696b0c774ec87e6109..e55212f0388ea4df967ec2801a5dbec6e71988cb 100644 (file)
@@ -598,18 +598,6 @@ void LyX::queryUserLyXDir(bool explicit_userdir)
 
        first_start = !explicit_userdir;
 
-       // If the user specified explicitly a directory, ask whether
-       // to create it (otherwise, always create it)
-       if (explicit_userdir &&
-           !Alert::askQuestion(_("You have specified an invalid LyX directory."),
-                        _("It is needed to keep your own configuration."),
-                        _("Should I try to set it up for you (recommended)?"))) {
-               lyxerr << _("Running without personal LyX directory.") << endl;
-               // No, let's use $HOME instead.
-               user_lyxdir = GetEnvPath("HOME");
-               return;
-       }
-
 #if USE_BOOST_FORMAT
        lyxerr << boost::format(_("LyX: Creating directory %1$s"
                                  " and running configure..."))
index 34e54fc295dc63b376f16064f76878b8a90aad97..ba063131392a66096c13be1e169285236c8df060 100644 (file)
@@ -1758,11 +1758,6 @@ void LyXFunc::open(string const & fname)
        // if the file doesn't exist, let the user create one
        FileInfo const f(filename, true);
        if (!f.exist()) {
-               if (!Alert::askQuestion(_("No such file"), disp_fn,
-                       _("Start a new document with this filename ?"))) {
-                       owner->message(_("Canceled."));
-                       return;
-               }
                // the user specifically chose this name. Believe them.
                Buffer * buffer =  bufferlist.newFile(filename, "", true);
                view()->buffer(buffer);
@@ -1861,30 +1856,44 @@ void LyXFunc::doImport(string const & argument)
 
        // Check if the document already is open
        if (lyxrc.use_gui && bufferlist.exists(lyxfile)) {
-               switch (Alert::askConfirmation(_("Document is already open:"),
-                                       MakeDisplayPath(lyxfile, 50),
-                                       _("Do you want to close that document now?\n"
-                                         "('No' will just switch to the open version)")))
-                       {
-                       case 1:
-                               // If close is canceled, we cancel here too.
-                               if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
-                                       return;
-                               break;
-                       case 2:
-                               view()->buffer(bufferlist.getBuffer(lyxfile));
-                               return;
-                       case 3:
-                               owner->message(_("Canceled."));
-                               return;
-                       }
+               string const file = MakeDisplayPath(lyxfile, 30);
+
+               // FIXME: sucky UI !
+
+#if USE_BOOST_FORMAT
+               boost::format fmt(_("The document %1$s is already open.\n\nDo you want to close that document?"));
+               fmt % file;
+               string text = fmt.str();
+#else
+               string text = _("The document ");
+               text += file + _(" is already open.\n\nDo you want to close that document?");
+#endif
+               int const ret = Alert::prompt(_("Close open document?"),
+                       text, 1, _("&Close"), _("&Cancel"));
+
+               if (ret == 0 || !bufferlist.close(bufferlist.getBuffer(lyxfile), true)) {
+                       owner->message(_("Canceled."));
+                       return;
+               }
        }
 
        // if the file exists already, and we didn't do
        // -i lyx thefile.lyx, warn
        if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
-               if (!Alert::askQuestion(_("A document by the name"),
-                       MakeDisplayPath(lyxfile), _("already exists. Overwrite?"))) {
+               string const file = MakeDisplayPath(lyxfile, 30);
+
+#if USE_BOOST_FORMAT
+               boost::format fmt(_("The document %1$s already exists.\n\nDo you want to over-write that document?"));
+               fmt % file;
+               string text = fmt.str();
+#else
+               string text = _("The document ");
+               text += file + _(" already exists.\n\nDo you want to over-write that document?");
+#endif
+               int const ret = Alert::prompt(_("Over-write document?"),
+                       text, 1, _("&Over-write"), _("&Cancel"));
+
+               if (ret == 1) {
                        owner->message(_("Canceled."));
                        return;
                }
@@ -1896,7 +1905,7 @@ void LyXFunc::doImport(string const & argument)
 
 void LyXFunc::closeBuffer()
 {
-       if (bufferlist.close(owner->buffer()) && !quitting) {
+       if (bufferlist.close(owner->buffer(), true) && !quitting) {
                if (bufferlist.empty()) {
                        // need this otherwise SEGV may occur while trying to
                        // set variables that don't exist
index ddf087580a90c681921398b4338cb6f5076be719..5dff933476ccba05a8e623ef84f80be54f5ea890 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "support/filetools.h"
 #include "support/lyxlib.h"
+#include "BoostFormat.h"
 
 #include <unistd.h>
 
@@ -73,6 +74,31 @@ void LyXVC::buffer(Buffer * buf)
 }
 
 
+bool LyXVC::ensureClean()
+{
+       if (owner_->isClean())
+               return true;
+
+       string const file = MakeDisplayPath(owner_->fileName(), 30);
+#if USE_BOOST_FORMAT
+       boost::format fmt(_("The document %1$s has unsaved changes.\n\nDo you want to save the document?"));
+       fmt % file;
+       string text = fmt.str();
+#else
+       string text = _("The document ");
+       text += file + _(" has unsaved changes.\n\nDo you want to save the document?");
+#endif
+       int const ret = Alert::prompt(_("Save changed document?"),
+               text, 0, _("&Save"), _("&Cancel"));
+
+       if (ret == 0) {
+               vcs->owner()->getUser()->owner()->dispatch(FuncRequest(LFUN_MENUWRITE));
+       }
+
+       return owner_->isClean();
+}
+
+
 void LyXVC::registrer()
 {
        string const filename = owner_->fileName();
@@ -107,18 +133,9 @@ void LyXVC::registrer()
                vcs->owner(owner_);
        }
 
-       // If the document is changed, we might want to save it
-       if (!vcs->owner()->isClean() &&
-           Alert::askQuestion(_("Changes in document:"),
-                       MakeDisplayPath(filename, 50),
-                       _("Save document and proceed?"))) {
-               vcs->owner()->getUser()->owner()
-                       ->dispatch(FuncRequest(LFUN_MENUWRITE));
-       }
-
        // Maybe the save fails, or we answered "no". In both cases,
        // the document will be dirty, and we abort.
-       if (!vcs->owner()->isClean())
+       if (!ensureClean())
                return;
 
        lyxerr[Debug::LYXVC] << "LyXVC: registrer" << endl;
@@ -140,10 +157,7 @@ void LyXVC::registrer()
 void LyXVC::checkIn()
 {
        // If the document is changed, we might want to save it
-       if (!vcs->owner()->isClean() &&
-           Alert::askQuestion(_("Changes in document:"),
-                       MakeDisplayPath(vcs->owner()->fileName(), 50),
-                       _("Save document and proceed?"))) {
+       if (!vcs->owner()->isClean()) {
                vcs->owner()->getUser()->owner()
                        ->dispatch(FuncRequest(LFUN_MENUWRITE));
        }
@@ -169,12 +183,8 @@ void LyXVC::checkIn()
 void LyXVC::checkOut()
 {
        lyxerr[Debug::LYXVC] << "LyXVC: checkOut" << endl;
-       if (!vcs->owner()->isClean()
-           && !Alert::askQuestion(_("Changes in document:"),
-                          MakeDisplayPath(vcs->owner()->fileName(), 50),
-                          _("Ignore changes and proceed with check out?"))) {
+       if (!ensureClean())
                return;
-       }
 
        vcs->checkOut();
 }
@@ -183,15 +193,22 @@ void LyXVC::checkOut()
 void LyXVC::revert()
 {
        lyxerr[Debug::LYXVC] << "LyXVC: revert" << endl;
-       // Here we should check if the buffer is dirty. And if it is
-       // we should warn the user that reverting will discard all
-       // changes made since the last check in.
-       if (Alert::askQuestion(_("When you revert, you will loose all changes made"),
-                       _("to the document since the last check in."),
-                       _("Do you still want to do it?"))) {
 
+       string const file = MakeDisplayPath(owner_->fileName(), 20);
+#if USE_BOOST_FORMAT
+       boost::format fmt(_("Reverting to the stored version of the document %1$s will "
+                       "lose all current changes.\n\nDo you want to revert to the saved version?"));
+       fmt % file;
+       string text = fmt.str();
+#else
+       string text = _("Reverting to the stored version of the document ");
+       text += file + _(" will lose all current changes.\n\nDo you want to revert to the saved version?");
+#endif
+       int const ret = Alert::prompt(_("Revert to stored version of document?"),
+               text, 1, _("&Revert"), _("&Cancel"));
+
+       if (ret == 0)
                vcs->revert();
-       }
 }
 
 
index d282f395534edf2d5f1d812481e28f26cfbff07f..efb9c40c3acb702beea9d6716c155fd78ebbc65a 100644 (file)
@@ -86,6 +86,9 @@ public:
        string const & locker() const;
 
 private:
+       /// returns false if still not clean
+       bool ensureClean();
+       
        ///
        Buffer * owner_;