]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewedInset.h
Cache the BufferView as a weak_ptr; get rid of those horrible current_views.
[lyx.git] / src / graphics / PreviewedInset.h
1 // -*- C++ -*-
2 /**
3  *  \file PreviewedInset.h
4  *  Copyright 2002 the LyX Team
5  *  Read the file COPYING
6  *
7  * \author Angus Leeming <leeming@lyx.org>
8  *
9  *  grfx::PreviewedInset is an abstract base class that can help insets to
10  *  generate previews. The daughter class must instantiate three small
11  *  methods. The Inset would own an instance of this daughter class.
12  */
13
14 #ifndef PREVIEWEDINSET_H
15 #define PREVIEWEDINSET_H
16
17 #ifdef __GNUG__
18 #pragma interface
19 #endif
20
21 #include "LString.h"
22 #include <boost/weak_ptr.hpp>
23 #include <boost/signals/trackable.hpp>
24 #include <boost/signals/connection.hpp>
25
26 class Inset;
27 class BufferView;
28
29 namespace grfx {
30
31 class PreviewImage;
32 class PreviewLoader;
33
34 class PreviewedInset : public boost::signals::trackable {
35 public:
36         /// a wrapper for Previews::activated()
37         static bool activated();
38
39         ///
40         PreviewedInset(Inset & inset) : inset_(inset), pimage_(0) {}
41         ///
42         virtual ~PreviewedInset() {}
43
44         /** Find the PreviewLoader, add a LaTeX snippet to it and
45          *  start the loading process.
46          */
47         void generatePreview();
48
49         /** Add a LaTeX snippet to the PreviewLoader but do not start the
50          *  loading process.
51          */
52         void addPreview(PreviewLoader & ploader);
53
54         /// The preview has been generated and is ready to use.
55         bool previewReady() const;
56
57         /// If !previewReady() returns 0.
58         PreviewImage const * pimage() const { return pimage_; }
59
60         ///
61         void setView(BufferView *);
62
63 protected:
64         /// Allow the daughter classes to cast up to the parent inset.
65         Inset * inset() const { return &inset_; }
66         ///
67         BufferView * view() const { return view_.get(); }
68
69 private:
70         /** This method is connected to the grfx::PreviewLoader::imageReady
71          *  signal.
72          */
73         void imageReady(PreviewImage const &) const;
74
75         /// Does the owning inset want a preview?
76         virtual bool previewWanted() const = 0;
77         /// a wrapper to Inset::latex
78         virtual string const latexString() const = 0;
79
80         ///
81         Inset & inset_;
82         ///
83         string snippet_;
84         ///
85         boost::weak_ptr<BufferView> view_;
86         
87         /// We don't own this. Cached for efficiency reasons.
88         mutable PreviewImage const * pimage_;
89         ///
90         boost::signals::connection connection_;
91 };
92
93 } // namespace grfx
94
95
96 #endif // PREVIEWEDINSET_H