And here the updated patch.

Position and visibility is stored, and
when a toolbar is not visible at start up
it is context sensitive.

When you move a context visible toolbar
the position is also rememberd even when
the toolbar is hidden when exiting.

Peter
Index: session.C
===================================================================
--- session.C   (revision 15652)
+++ session.C   (working copy)
@@ -48,6 +48,7 @@
 string const sec_lastopened = "[last opened files]";
 string const sec_bookmarks = "[bookmarks]";
 string const sec_session = "[session info]";
+string const sec_toolbars = "[toolbars]";
 
 } // anon namespace
 
@@ -251,6 +252,59 @@
 }
 
 
+void ToolbarSection::read(istream & is)
+{
+       string tmp;
+       do {
+               char c = is.peek();
+               if (c == '[')
+                       break;
+               getline(is, tmp);
+
+               // Read session info, saved as key/value pairs
+               // would better yell if pos returns npos
+               string::size_type pos = tmp.find_first_of(" = ");
+               // silently ignore lines without " = "
+               if (pos != string::npos) {
+                       string key = tmp.substr(0, pos);
+                       bool visible;
+                       int posX;
+                       int posY;
+                       int location;
+                       istringstream value(tmp.substr(pos + 3));
+                       value >> visible;
+                       value.ignore(1); // ignore " "
+                       value >> posX;
+                       value.ignore(1); // ignore " "
+                       value >> posY;
+                       value.ignore(1); // ignore " "
+                       value >> location;
+                       toolbars[key] = ToolbarInfo(visible, posX, posY, 
location);
+               }
+       } while (is.good());
+}
+
+
+void ToolbarSection::write(ostream & os) const
+{
+       os << '\n' << sec_toolbars << '\n';
+       for (ToolbarMap::const_iterator tb = toolbars.begin();
+               tb != toolbars.end(); ++tb) {
+               os << tb->first << " = " 
+                  << tb->second.visible << " "
+                  << tb->second.posX << " "
+                  << tb->second.posY << " "
+                  << tb->second.location << '\n';
+       }
+}
+
+
+ToolbarSection::ToolbarInfo & ToolbarSection::load(string const & name)
+{
+       return toolbars[name];
+}
+
+
 void SessionInfoSection::read(istream & is)
 {
        string tmp;
@@ -334,6 +388,8 @@
                        lastFilePos().read(is);
                else if (tmp == sec_bookmarks)
                        bookmarks().read(is);
+               else if (tmp == sec_toolbars)
+                       toolbars().read(is);
                else if (tmp == sec_session)
                        sessionInfo().read(is);
                else
@@ -353,6 +409,7 @@
                lastOpened().write(os);
                lastFilePos().write(os);
                bookmarks().write(os);
+               toolbars().write(os);
                sessionInfo().write(os);
        } else
                lyxerr << "LyX: Warning: unable to save Session: "
Index: frontends/LyXView.h
===================================================================
--- frontends/LyXView.h (revision 15652)
+++ frontends/LyXView.h (working copy)
@@ -136,6 +136,7 @@
 
        /// update the toolbar
        void updateToolbars();
+
        /// update the menubar
        void updateMenubar();
        /// update the status bar
Index: frontends/Toolbars.C
===================================================================
--- frontends/Toolbars.C        (revision 15652)
+++ frontends/Toolbars.C        (working copy)
@@ -24,6 +24,14 @@
 #include "LyXView.h"
 
 
+namespace {
+       static lyx::ToolbarBackend::Flags toTBFlags(int i)
+       {
+               return static_cast<lyx::ToolbarBackend::Flags>(i);
+       }
+}
+
+
 namespace lyx {
 
 using std::endl;
@@ -37,14 +45,48 @@
 {}
 
 
+void Toolbars::initFlags(ToolbarBackend::Toolbar & tbb)
+{
+       ToolbarSection::ToolbarInfo & info = 
LyX::ref().session().toolbars().load(tbb.name);
+       
+       // remove position
+       ToolbarBackend::Flags  clean = toTBFlags( tbb.flags 
+                                                               & 
~ToolbarBackend::TOP & ~ToolbarBackend::BOTTOM 
+                                                               & 
~ToolbarBackend::RIGHT & ~ToolbarBackend::LEFT);
+
+       bool valid_location = true;
+       // init tbb.flags with saved location
+       if (info.location == ToolbarSection::ToolbarInfo::top)
+               tbb.flags = toTBFlags(clean | ToolbarBackend::TOP);
+       else if (info.location == ToolbarSection::ToolbarInfo::bottom)
+               tbb.flags = toTBFlags(clean | ToolbarBackend::BOTTOM);
+       else if (info.location == ToolbarSection::ToolbarInfo::right)
+               tbb.flags = toTBFlags(clean | ToolbarBackend::RIGHT);
+       else if (info.location == ToolbarSection::ToolbarInfo::left)
+               tbb.flags = toTBFlags(clean | ToolbarBackend::LEFT);
+       else
+               valid_location = false;
+
+       // init tbb.flags with saved visibility, 
+       // disable context sensitive flags when visibale
+       if (valid_location && info.visible)
+               tbb.flags = toTBFlags( (tbb.flags 
+                                                               & 
~ToolbarBackend::OFF & ~ToolbarBackend::MATH 
+                                                               & 
~ToolbarBackend::TABLE& ~ToolbarBackend::REVIEW)
+                                                               | 
ToolbarBackend::ON);
+}
+
 void Toolbars::init()
 {
        // extracts the toolbars from the backend
-       ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
-       ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
+       ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
+       ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
 
        for (; cit != end; ++cit)
+       {
+               initFlags(*cit);
                add(*cit);
+       }
 }
 
 
@@ -84,6 +126,19 @@
 }
 
 
+void Toolbars::saveToolbarInfo()
+{
+       ToolbarSection & tb = LyX::ref().session().toolbars();
+       
+       for (ToolbarBackend::Toolbars::const_iterator cit = 
toolbarbackend.begin();
+               cit != toolbarbackend.end(); ++cit) {
+               ToolbarsMap::iterator it = toolbars_.find(cit->name);
+               BOOST_ASSERT(it != toolbars_.end());
+               it->second->saveInfo(tb.load(cit->name));
+       }
+}
+
+
 void Toolbars::setLayout(string const & layout)
 {
        if (layout_)
Index: frontends/qt4/QLToolbar.C
===================================================================
--- frontends/qt4/QLToolbar.C   (revision 15652)
+++ frontends/qt4/QLToolbar.C   (working copy)
@@ -214,6 +214,26 @@
 }
 
 
+void QLToolbar::saveInfo(ToolbarSection::ToolbarInfo & info)
+{
+       // update Toolbar info with current toolbar status
+       // FIXME: there are more information can be saved here
+       info.visible = QLToolbar::isVisible();
+       Qt::ToolBarArea loc = owner_.toolBarArea(this);
+
+       if (loc == Qt::TopToolBarArea)
+               info.location = ToolbarSection::ToolbarInfo::top;
+       else if (loc == Qt::BottomToolBarArea)
+               info.location = ToolbarSection::ToolbarInfo::bottom;
+       else if (loc == Qt::RightToolBarArea)
+               info.location = ToolbarSection::ToolbarInfo::right;
+       else if (loc == Qt::LeftToolBarArea)
+               info.location = ToolbarSection::ToolbarInfo::left;
+       else
+               info.location = ToolbarSection::ToolbarInfo::notset;
+}
+
+
 void QLToolbar::update()
 {
        // This is a speed bottleneck because this is called on every keypress
Index: frontends/qt4/QLToolbar.h
===================================================================
--- frontends/qt4/QLToolbar.h   (revision 15652)
+++ frontends/qt4/QLToolbar.h   (working copy)
@@ -22,6 +22,9 @@
 #include <QToolBar>
 #include <vector>
 
+#include "lyx_main.h"
+#include "session.h"
+
 class QComboBox;
 
 namespace lyx {
@@ -67,6 +70,7 @@
        void add(FuncRequest const & func, lyx::docstring const & tooltip);
        void hide(bool);
        void show(bool);
+       void saveInfo(ToolbarSection::ToolbarInfo & info);
        void update();
        LayoutBox * layout() const { return layout_.get(); }
 
Index: frontends/qt4/GuiView.C
===================================================================
--- frontends/qt4/GuiView.C     (revision 15652)
+++ frontends/qt4/GuiView.C     (working copy)
@@ -182,6 +182,8 @@
                session.sessionInfo().save("WindowPosX", 
convert<string>(geometry.x()));
                session.sessionInfo().save("WindowPosY", 
convert<string>(geometry.y()));
        }
+       // FIXME: put savetoolbar here?
+       getToolbars().saveToolbarInfo();        
 }
                                                  
 void GuiView::setGeometry(unsigned int width,
@@ -465,7 +467,6 @@
        }
 }
 
-
 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarBackend::Toolbar const & tbb)
 {
        QLToolbar * Tb = new QLToolbar(tbb, *this);
Index: frontends/Toolbars.h
===================================================================
--- frontends/Toolbars.h        (revision 15652)
+++ frontends/Toolbars.h        (working copy)
@@ -26,6 +26,8 @@
 #include "ToolbarBackend.h"
 #include <boost/shared_ptr.hpp>
 #include <map>
+#include "lyx_main.h"
+#include "session.h"
 
 
 namespace lyx {
@@ -64,6 +66,10 @@
         *  metrics should be updated.
         */
        virtual void show(bool update_metrics) = 0;
+       /** update toolbar information
+        * ToolbarInfo will then be saved by session
+        */
+       virtual void saveInfo(ToolbarSection::ToolbarInfo & info) = 0;
 
        /// Refresh the contents of the bar.
        virtual void update() = 0;
@@ -86,6 +92,9 @@
        /// Update the state of the toolbars.
        void update(bool in_math, bool in_table, bool review);
 
+       /// save toolbar information
+       void saveToolbarInfo();
+
        /// Select the right layout in the combox.
        void setLayout(std::string const & layout);
 
@@ -128,6 +137,9 @@
 
        /// The last textclass layout list in the layout choice selector
        int last_textclass_;
+
+       // load flags with saved values
+       void initFlags(ToolbarBackend::Toolbar & tbb);
 };
 
 /// Set the layout in the kernel when an entry has been selected
Index: session.h
===================================================================
--- session.h   (revision 15652)
+++ session.h   (working copy)
@@ -202,6 +202,65 @@
 };
 
 
+class ToolbarSection : SessionSection
+{
+public:
+       /// information about a toolbar, not all information can be
+       /// saved/restored by all frontends, but this class provides
+       /// a superset of things that can be managed by session.
+       class ToolbarInfo
+       {
+       public:
+               ///
+               ToolbarInfo() : 
+                       empty(true), visible(false), posX(0), posY(0), 
location(notset) { }
+               ///
+               ToolbarInfo(bool v, int x, int y, int loc) : 
+                       empty(false), visible(v), posX(x), posY(y), 
location(static_cast<Location>(loc)) { }
+
+       public:
+               /// true means no information is available (so ignore visible 
settings)
+               bool empty;
+               /// on/off
+               bool visible;
+               /// position
+               int posX;
+               ///
+               int posY;
+
+               /// location: this can be intepreted differently.
+               enum Location {
+                       top,
+                       bottom,
+                       left,
+                       right,
+                       notset
+               };
+
+               Location location;
+
+               /// potentially, icons
+       };
+
+       /// info for each toolbar
+       typedef std::map<std::string, ToolbarInfo> ToolbarMap;
+
+public:
+       ///
+       void read(std::istream & is);
+
+       ///
+       void write(std::ostream & os) const;
+
+       /// return reference to toolbar info, create a new one if needed
+       ToolbarInfo & load(std::string const & name);
+
+private:
+       /// toolbar information
+       ToolbarMap toolbars;
+};
+
+
 class SessionInfoSection : SessionSection
 {
 public:
@@ -271,6 +330,12 @@
        BookmarksSection const & bookmarks() const { return bookmarks_; }
 
        ///
+       ToolbarSection & toolbars() { return toolbars_; }
+
+       ///
+       ToolbarSection const & toolbars() const { return toolbars_; }
+
+       ///
        SessionInfoSection & sessionInfo() { return session_info; }
 
        ///
@@ -300,6 +365,9 @@
        BookmarksSection bookmarks_;
 
        ///
+       ToolbarSection toolbars_;
+
+       ///
        SessionInfoSection session_info;
 };
 
Index: ToolbarBackend.h
===================================================================
--- ToolbarBackend.h    (revision 15652)
+++ ToolbarBackend.h    (working copy)
@@ -75,9 +75,9 @@
        ToolbarBackend();
 
        /// iterator for all toolbars
-       Toolbars::const_iterator begin() const { return usedtoolbars.begin(); }
+       Toolbars::iterator begin() { return usedtoolbars.begin(); }
 
-       Toolbars::const_iterator end() const { return usedtoolbars.end(); }
+       Toolbars::iterator end() { return usedtoolbars.end(); }
 
        /// read a toolbar from the file
        void read(LyXLex &);

Reply via email to