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