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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sd.hxx"
26 
27 #include "RecentMasterPagesSelector.hxx"
28 #include "PreviewValueSet.hxx"
29 
30 #include "RecentlyUsedMasterPages.hxx"
31 #include "MasterPageContainerProviders.hxx"
32 #include "MasterPageObserver.hxx"
33 #include "sdpage.hxx"
34 #include "drawdoc.hxx"
35 #include "app.hrc"
36 #include <vcl/bitmap.hxx>
37 #include <tools/color.hxx>
38 
39 namespace sd { namespace toolpanel { namespace controls {
40 
41 
42 RecentMasterPagesSelector::RecentMasterPagesSelector (
43     TreeNode* pParent,
44     SdDrawDocument& rDocument,
45     ViewShellBase& rBase,
46     const ::boost::shared_ptr<MasterPageContainer>& rpContainer)
47     : MasterPagesSelector (pParent, rDocument, rBase, rpContainer)
48 {
49 	SetName (String(RTL_CONSTASCII_USTRINGPARAM("RecentMasterPagesSelector")));
50 }
51 
52 
53 
54 
55 RecentMasterPagesSelector::~RecentMasterPagesSelector (void)
56 {
57     RecentlyUsedMasterPages::Instance().RemoveEventListener (
58         LINK(this,RecentMasterPagesSelector,MasterPageListListener));
59 }
60 
61 
62 
63 
64 void RecentMasterPagesSelector::LateInit (void)
65 {
66     MasterPagesSelector::LateInit();
67 
68     MasterPagesSelector::Fill();
69     RecentlyUsedMasterPages::Instance().AddEventListener (
70         LINK(this,RecentMasterPagesSelector,MasterPageListListener));
71 }
72 
73 
74 
75 
76 IMPL_LINK(RecentMasterPagesSelector,MasterPageListListener, void*, EMPTYARG)
77 {
78     MasterPagesSelector::Fill();
79     return 0;
80 }
81 
82 
83 
84 
85 void RecentMasterPagesSelector::Fill (ItemList& rItemList)
86 {
87     // Create a set of names of the master pages used by the document.
88     MasterPageObserver::MasterPageNameSet aCurrentNames;
89     sal_uInt16 nMasterPageCount = mrDocument.GetMasterSdPageCount(PK_STANDARD);
90     sal_uInt16 nIndex;
91     for (nIndex=0; nIndex<nMasterPageCount; nIndex++)
92     {
93         SdPage* pMasterPage = mrDocument.GetMasterSdPage (nIndex, PK_STANDARD);
94         if (pMasterPage != NULL)
95             aCurrentNames.insert (pMasterPage->GetName());
96     }
97     MasterPageObserver::MasterPageNameSet::iterator aI;
98 
99     // Insert the recently used master pages that are currently not used.
100     RecentlyUsedMasterPages& rInstance (RecentlyUsedMasterPages::Instance());
101     int nPageCount = rInstance.GetMasterPageCount();
102     for (nIndex=0; nIndex<nPageCount; nIndex++)
103     {
104         // Add an entry when a) the page is already known to the
105         // MasterPageContainer, b) the style name is empty, i.e. it has not yet
106         // been loaded (and thus can not be in use) or otherwise c) the
107         // style name is not currently in use.
108         MasterPageContainer::Token aToken (rInstance.GetTokenForIndex(nIndex));
109         if (aToken != MasterPageContainer::NIL_TOKEN)
110         {
111             String sStyleName (mpContainer->GetStyleNameForToken(aToken));
112             if (sStyleName.Len()==0
113                 || aCurrentNames.find(sStyleName) == aCurrentNames.end())
114             {
115                 rItemList.push_back(aToken);
116             }
117         }
118     }
119 }
120 
121 
122 
123 
124 void RecentMasterPagesSelector::AssignMasterPageToPageList (
125     SdPage* pMasterPage,
126     const ::boost::shared_ptr<std::vector<SdPage*> >& rpPageList)
127 {
128 	sal_uInt16 nSelectedItemId = mpPageSet->GetSelectItemId();
129 
130     MasterPagesSelector::AssignMasterPageToPageList(pMasterPage, rpPageList);
131 
132     // Restore the selection.
133     if (mpPageSet->GetItemCount() > 0)
134     {
135         if (mpPageSet->GetItemCount() >= nSelectedItemId)
136             mpPageSet->SelectItem(nSelectedItemId);
137         else
138             mpPageSet->SelectItem(mpPageSet->GetItemCount());
139     }
140 }
141 
142 
143 
144 
145 void RecentMasterPagesSelector::GetState (SfxItemSet& rItemSet)
146 {
147     MasterPagesSelector::GetState (rItemSet);
148     if (rItemSet.GetItemState(SID_TP_EDIT_MASTER) == SFX_ITEM_AVAILABLE)
149         rItemSet.DisableItem (SID_TP_EDIT_MASTER);
150 }
151 
152 
153 
154 
155 } } } // end of namespace ::sd::toolpanel::controls
156