1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef SD_TOOLPANEL_CONTROLS_MASTER_PAGE_CONTAINER_HXX
25 #define SD_TOOLPANEL_CONTROLS_MASTER_PAGE_CONTAINER_HXX
26 
27 #include "MasterPageContainerProviders.hxx"
28 
29 #include <osl/mutex.hxx>
30 #include <tools/string.hxx>
31 #include <vcl/image.hxx>
32 #include <memory>
33 #include "PreviewRenderer.hxx"
34 #include <com/sun/star/frame/XModel.hpp>
35 #include <vcl/timer.hxx>
36 #include "tools/SdGlobalResourceContainer.hxx"
37 
38 #include <boost/shared_ptr.hpp>
39 
40 class SdPage;
41 class SdDrawDocument;
42 class SfxObjectShellLock;
43 
44 namespace sd {
45 class DrawDocShell;
46 }
47 
48 namespace sd { namespace toolpanel { namespace controls {
49 
50 class MasterPageDescriptor;
51 
52 /** This container manages the master pages used by the MasterPagesSelector
53     controls.  It uses internally a singleton implementation object.
54     Therefore, all MasterPageContainer object operator on the same set of
55     master pages.  Each MasterPageContainer, however, has its own
56     PreviewSize value and thus can independantly switch between large and
57     small previews.
58 
59     The container maintains its own document to store master page objects.
60 
61     For each master page container stores its URL, preview bitmap, page
62     name, and, if available, the page object.
63 
64     Entries are accessed via a Token, which is mostly a numerical index but
65     whose values do not neccessarily have to be consecutive.
66 */
67 class MasterPageContainer
68 {
69 public:
70     typedef int Token;
71     static const Token NIL_TOKEN = -1;
72 
73     MasterPageContainer (void);
74     virtual ~MasterPageContainer (void);
75 
76     void AddChangeListener (const Link& rLink);
77     void RemoveChangeListener (const Link& rLink);
78 
79     enum PreviewSize { SMALL, LARGE };
80     /** There are two different preview sizes, a small one and a large one.
81         Which one is used by the called container can be changed with this
82         method.
83         When the preview size is changed then all change listeners are
84         notified of this.
85     */
86     void SetPreviewSize (PreviewSize eSize);
87 
88     /** Returns the preview size.
89     */
90     PreviewSize GetPreviewSize (void) const;
91 
92     /** Return the preview size in pixels.
93     */
94     Size GetPreviewSizePixel (void) const;
95 
96     enum PreviewState { PS_AVAILABLE, PS_CREATABLE, PS_PREPARING, PS_NOT_AVAILABLE };
97     PreviewState GetPreviewState (Token aToken);
98 
99     /** This method is typically called for entries in the container for
100         which GetPreviewState() returns OS_CREATABLE.  The creation of the
101         preview is then scheduled to be executed asynchronously at a later
102         point in time.  When the preview is available the change listeners
103         will be notified.
104     */
105     bool RequestPreview (Token aToken);
106 
107     /** Each entry of the container is either the first page of a template
108         document or is a master page of an Impress document.
109     */
110     enum Origin {
111         MASTERPAGE,  // Master page of a document.
112         TEMPLATE,    // First page of a template file.
113         DEFAULT,     // Empty master page with default style.
114         UNKNOWN
115     };
116 
117     /** Put the master page identified and described by the given parameters
118         into the container.  When there already is a master page with the
119         given URL, page name, or object pointer (when that is not NULL) then
120         the existing entry is replaced/updated by the given one.  Otherwise
121         a new entry is inserted.
122     */
123     Token PutMasterPage (const ::boost::shared_ptr<MasterPageDescriptor>& rDescriptor);
124     void AcquireToken (Token aToken);
125     void ReleaseToken (Token aToken);
126 
127     /** This and the GetTokenForIndex() methods can be used to iterate over
128         all members of the container.
129     */
130     int GetTokenCount (void) const;
131 
132     /** Determine whether the container has a member for the given token.
133     */
134     bool HasToken (Token aToken) const;
135 
136     /** Return a token for an index in the range
137         0 <= index < GetTokenCount().
138     */
139     Token GetTokenForIndex (int nIndex);
140 
141     Token GetTokenForURL (const String& sURL);
142     Token GetTokenForStyleName (const String& sStyleName);
143     Token GetTokenForPageObject (const SdPage* pPage);
144 
145     String GetURLForToken (Token aToken);
146     String GetPageNameForToken (Token aToken);
147     String GetStyleNameForToken (Token aToken);
148     SdPage* GetPageObjectForToken (Token aToken, bool bLoad=true);
149     Origin GetOriginForToken (Token aToken);
150     sal_Int32 GetTemplateIndexForToken (Token aToken);
151     ::boost::shared_ptr<MasterPageDescriptor> GetDescriptorForToken (Token aToken);
152 
153     void InvalidatePreview (Token aToken);
154 
155     /** Return a preview for the specified token.  When the preview is not
156         present then the PreviewProvider associated with the token is
157         executed only when that is not expensive.  It is the responsibility
158         of the caller to call RequestPreview() to do the same
159         (asynchronously) for expensive PreviewProviders.
160         Call GetPreviewState() to find out if that is necessary.
161         @param aToken
162             This token specifies for which master page to return the prview.
163             Tokens are returned for example by the GetTokenFor...() methods.
164         @return
165             The returned image is the requested preview or a substitution.
166     */
167     Image GetPreviewForToken (Token aToken);
168 
169 private:
170     class Implementation;
171     ::boost::shared_ptr<Implementation> mpImpl;
172     PreviewSize mePreviewSize;
173 
174     /** Retrieve the preview of the document specified by the given URL.
175     */
176     static BitmapEx LoadPreviewFromURL (const ::rtl::OUString& aURL);
177 };
178 
179 
180 
181 
182 /** For some changes to the set of master pages in a MasterPageContainer or
183     to the data stored for each master page one or more events are sent to
184     registered listeners.
185     Each event has an event type and a token that tells the listener where
186     the change took place.
187 */
188 class MasterPageContainerChangeEvent
189 {
190 public:
191     enum EventType {
192         // A master page was added to the container.
193         CHILD_ADDED,
194         // A master page was removed from the container.
195         CHILD_REMOVED,
196         // The preview of a master page has changed.
197         PREVIEW_CHANGED,
198         // The size of a preview has changed.
199         SIZE_CHANGED,
200         // Some of the data stored for a master page has changed.
201         DATA_CHANGED,
202         // The TemplateIndex of a master page has changed.
203         INDEX_CHANGED,
204         // More than one entries changed their TemplateIndex
205         INDEXES_CHANGED
206     } meEventType;
207 
208     // Token of the container entry whose data changed or which was added or
209     // removed.
210     MasterPageContainer::Token maChildToken;
211 };
212 
213 
214 } } } // end of namespace ::sd::toolpanel::controls
215 
216 #endif
217