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 "MasterPagesSelector.hxx"
25 
26 #include "MasterPageContainer.hxx"
27 #include "DocumentHelper.hxx"
28 #include "SidebarShellManager.hxx"
29 #include "pres.hxx"
30 #include "drawdoc.hxx"
31 #include "DrawDocShell.hxx"
32 #include "sdpage.hxx"
33 #include "glob.hxx"
34 #include "glob.hrc"
35 #include "app.hrc"
36 #include "res_bmp.hrc"
37 #include "strings.hrc"
38 #include "DrawViewShell.hxx"
39 #include "DrawController.hxx"
40 #include "SlideSorterViewShell.hxx"
41 #include "PreviewValueSet.hxx"
42 #include "ViewShellBase.hxx"
43 #include <sfx2/objface.hxx>
44 #include "sdresid.hxx"
45 #include "drawview.hxx"
46 #include <vcl/image.hxx>
47 #include <vcl/floatwin.hxx>
48 #include <svl/languageoptions.hxx>
49 #include <sfx2/app.hxx>
50 #include <sfx2/dispatch.hxx>
51 #include <sfx2/mnumgr.hxx>
52 #include <svl/itemset.hxx>
53 #include <svl/eitem.hxx>
54 #include <svx/dlgutil.hxx>
55 #include <svx/svdpagv.hxx>
56 #include <svx/svxids.hrc>
57 #include "FrameView.hxx"
58 #include "sdpage.hxx"
59 #include "stlpool.hxx"
60 #include "unmovss.hxx"
61 #include <sfx2/request.hxx>
62 #include <svl/itempool.hxx>
63 #include <sfx2/sidebar/Theme.hxx>
64 
65 
66 using namespace ::com::sun::star::text;
67 
68 
69 
70 namespace sd { namespace sidebar {
71 
72 
MasterPagesSelector(::Window * pParent,SdDrawDocument & rDocument,ViewShellBase & rBase,const::boost::shared_ptr<MasterPageContainer> & rpContainer,const cssu::Reference<css::ui::XSidebar> & rxSidebar)73 MasterPagesSelector::MasterPagesSelector (
74     ::Window* pParent,
75     SdDrawDocument& rDocument,
76     ViewShellBase& rBase,
77     const ::boost::shared_ptr<MasterPageContainer>& rpContainer,
78     const cssu::Reference<css::ui::XSidebar>& rxSidebar)
79     : PreviewValueSet(pParent),
80       maMutex(),
81       mpContainer(rpContainer),
82       mrDocument(rDocument),
83       mrBase(rBase),
84       mnDefaultClickAction(SID_TP_APPLY_TO_ALL_SLIDES),
85       maPreviewUpdateQueue(),
86       maCurrentItemList(),
87       maTokenToValueSetIndex(),
88       maLockedMasterPages(),
89       mxSidebar(rxSidebar)
90 {
91     PreviewValueSet::SetSelectHdl (
92         LINK(this, MasterPagesSelector, ClickHandler));
93 	PreviewValueSet::SetRightMouseClickHandler (
94         LINK(this, MasterPagesSelector, RightClickHandler));
95     PreviewValueSet::SetStyle(PreviewValueSet::GetStyle() | WB_NO_DIRECTSELECT);
96     PreviewValueSet::SetPreviewSize(mpContainer->GetPreviewSizePixel());
97     PreviewValueSet::Show();
98 
99     SetBackground(sfx2::sidebar::Theme::GetWallpaper(sfx2::sidebar::Theme::Paint_PanelBackground));
100     SetColor(sfx2::sidebar::Theme::GetColor(sfx2::sidebar::Theme::Paint_PanelBackground));
101 
102     Link aChangeListener (LINK(this,MasterPagesSelector,ContainerChangeListener));
103     mpContainer->AddChangeListener(aChangeListener);
104 }
105 
106 
107 
108 
~MasterPagesSelector(void)109 MasterPagesSelector::~MasterPagesSelector (void)
110 {
111     Clear();
112     UpdateLocks(ItemList());
113 
114     Link aChangeListener (LINK(this,MasterPagesSelector,ContainerChangeListener));
115     mpContainer->RemoveChangeListener(aChangeListener);
116 }
117 
118 
119 
120 
LateInit(void)121 void MasterPagesSelector::LateInit (void)
122 {
123 }
124 
125 
126 
127 
GetPreferredWidth(sal_Int32 nHeight)128 sal_Int32 MasterPagesSelector::GetPreferredWidth (sal_Int32 nHeight)
129 {
130     const ::osl::MutexGuard aGuard (maMutex);
131 
132     return PreviewValueSet::GetPreferredWidth (nHeight);
133 }
134 
135 
136 
137 
GetPreferredHeight(sal_Int32 nWidth)138 sal_Int32 MasterPagesSelector::GetPreferredHeight (sal_Int32 nWidth)
139 {
140     const ::osl::MutexGuard aGuard (maMutex);
141 
142     return PreviewValueSet::GetPreferredHeight (nWidth);
143 }
144 
145 
146 
147 
GetPreferredSize(void)148 Size MasterPagesSelector::GetPreferredSize (void)
149 {
150     int nPreferredWidth = GetPreferredWidth(
151         PreviewValueSet::GetOutputSizePixel().Height());
152     int nPreferredHeight = GetPreferredHeight(nPreferredWidth);
153     return Size (nPreferredWidth, nPreferredHeight);
154 
155 }
156 
157 
158 
159 
UpdateLocks(const ItemList & rItemList)160 void MasterPagesSelector::UpdateLocks (const ItemList& rItemList)
161 {
162     ItemList aNewLockList;
163 
164     // In here we first lock the master pages in the given list and then
165     // release the locks acquired in a previous call to this method.  When
166     // this were done the other way round the lock count of some master
167     // pages might drop temporarily to 0 and would lead to unnecessary
168     // deletion and re-creation of MasterPageDescriptor objects.
169 
170     // Lock the master pages in the given list.
171     ItemList::const_iterator iItem;
172     for (iItem=rItemList.begin(); iItem!=rItemList.end(); ++iItem)
173     {
174         mpContainer->AcquireToken(*iItem);
175         aNewLockList.push_back(*iItem);
176     }
177 
178     // Release the previously locked master pages.
179     ItemList::const_iterator iPage;
180     ItemList::const_iterator iEnd (maLockedMasterPages.end());
181     for (iPage=maLockedMasterPages.begin(); iPage!=iEnd; ++iPage)
182         mpContainer->ReleaseToken(*iPage);
183 
184     maLockedMasterPages.swap(aNewLockList);
185 }
186 
187 
188 
189 
Fill(void)190 void MasterPagesSelector::Fill (void)
191 {
192     ::std::auto_ptr<ItemList> pItemList (new ItemList());
193 
194     Fill(*pItemList);
195 
196     UpdateLocks(*pItemList);
197     UpdateItemList(pItemList);
198 }
199 
200 
201 
202 
GetContextMenuResId(void) const203 ResId MasterPagesSelector::GetContextMenuResId (void) const
204 {
205     return SdResId(RID_TASKPANE_MASTERPAGESSELECTOR_POPUP);
206 }
207 
208 
209 
210 
IMPL_LINK(MasterPagesSelector,ClickHandler,PreviewValueSet *,EMPTYARG)211 IMPL_LINK(MasterPagesSelector, ClickHandler, PreviewValueSet*, EMPTYARG)
212 {
213     // We use the framework to assign the clicked-on master page because we
214     // so use the same mechanism as the context menu does (where we do not
215     // have the option to call the assignment method directly.)
216     ExecuteCommand(mnDefaultClickAction);
217 
218     return 0;
219 }
220 
221 
222 
223 
IMPL_LINK(MasterPagesSelector,RightClickHandler,MouseEvent *,pEvent)224 IMPL_LINK(MasterPagesSelector, RightClickHandler, MouseEvent*, pEvent)
225 {
226     // Here we only prepare the display of the context menu: the item under
227     // the mouse is selected.  The actual display of the context menu is
228     // done in ContextMenuCallback which is called indirectly through
229     // PreviewValueSet::Command().
230     PreviewValueSet::GrabFocus ();
231     PreviewValueSet::ReleaseMouse();
232     SfxViewFrame* pViewFrame = mrBase.GetViewFrame();
233     if (pViewFrame != NULL)
234     {
235         SfxDispatcher* pDispatcher = pViewFrame->GetDispatcher();
236         if (pDispatcher != NULL &&  pEvent != NULL)
237         {
238             sal_uInt16 nIndex = PreviewValueSet::GetItemId (pEvent->GetPosPixel());
239             if (nIndex > 0)
240                 PreviewValueSet::SelectItem (nIndex);
241         }
242     }
243     return 0;
244 }
245 
246 
247 
248 
Command(const CommandEvent & rEvent)249 void MasterPagesSelector::Command (const CommandEvent& rEvent)
250 {
251     switch (rEvent.GetCommand())
252     {
253         case COMMAND_CONTEXTMENU:
254         {
255             // Use the currently selected item and show the popup menu in its
256             // center.
257             const sal_uInt16 nIndex = PreviewValueSet::GetSelectItemId();
258             if (nIndex > 0)
259             {
260                 // The position of the upper left corner of the context menu is
261                 // taken either from the mouse position (when the command was sent
262                 // as reaction to a right click) or in the center of the selected
263                 // item (when the command was sent as reaction to Shift+F10.)
264                 Point aPosition (rEvent.GetMousePosPixel());
265                 if ( ! rEvent.IsMouseEvent())
266                 {
267                     Rectangle aBBox (PreviewValueSet::GetItemRect(nIndex));
268                     aPosition = aBBox.Center();
269                 }
270 
271                 // Setup the menu.
272                 ::boost::scoped_ptr<PopupMenu> pMenu (new PopupMenu(GetContextMenuResId()));
273                 FloatingWindow* pMenuWindow = dynamic_cast<FloatingWindow*>(pMenu->GetWindow());
274                 if (pMenuWindow != NULL)
275                     pMenuWindow->SetPopupModeFlags(
276                         pMenuWindow->GetPopupModeFlags() | FLOATWIN_POPUPMODE_NOMOUSEUPCLOSE);
277                 pMenu->SetSelectHdl(LINK(this, MasterPagesSelector, OnMenuItemSelected));
278 
279                 ProcessPopupMenu(*pMenu);
280 
281                 // Show the menu.
282                 pMenu->Execute(this, Rectangle(aPosition,Size(1,1)), POPUPMENU_EXECUTE_DOWN);
283             }
284             break;
285         }
286     }
287 }
288 
289 
290 
291 
ProcessPopupMenu(Menu & rMenu)292 void MasterPagesSelector::ProcessPopupMenu (Menu& rMenu)
293 {
294     // Disable some entries.
295     if (mpContainer->GetPreviewSize() == MasterPageContainer::SMALL)
296         rMenu.EnableItem(SID_TP_SHOW_SMALL_PREVIEW, sal_False);
297     else
298         rMenu.EnableItem(SID_TP_SHOW_LARGE_PREVIEW, sal_False);
299 }
300 
301 
302 
303 
IMPL_LINK(MasterPagesSelector,OnMenuItemSelected,Menu *,pMenu)304 IMPL_LINK(MasterPagesSelector, OnMenuItemSelected, Menu*, pMenu)
305 {
306     if (pMenu == NULL)
307     {
308         OSL_ENSURE(pMenu!=NULL, "MasterPagesSelector::OnMenuItemSelected: illegal menu!");
309         return 0;
310     }
311 
312     pMenu->Deactivate();
313     ExecuteCommand(pMenu->GetCurItemId());
314     return 0;
315 }
316 
317 
318 
319 
ExecuteCommand(const sal_Int32 nCommandId)320 void MasterPagesSelector::ExecuteCommand (const sal_Int32 nCommandId)
321 {
322 	switch (nCommandId)
323     {
324         case SID_TP_APPLY_TO_ALL_SLIDES:
325             mrBase.SetBusyState (true);
326             AssignMasterPageToAllSlides (GetSelectedMasterPage());
327             mrBase.SetBusyState (false);
328             break;
329 
330         case SID_TP_APPLY_TO_SELECTED_SLIDES:
331             mrBase.SetBusyState (true);
332             AssignMasterPageToSelectedSlides (GetSelectedMasterPage());
333             mrBase.SetBusyState (false);
334             break;
335 
336         case SID_TP_USE_FOR_NEW_PRESENTATIONS:
337             DBG_ASSERT (false,
338                 "Using slides as default for new presentations"
339                 " is not yet implemented");
340             break;
341 
342         case SID_TP_SHOW_SMALL_PREVIEW:
343         case SID_TP_SHOW_LARGE_PREVIEW:
344         {
345             mrBase.SetBusyState (true);
346             mpContainer->SetPreviewSize(
347                 nCommandId==SID_TP_SHOW_SMALL_PREVIEW
348                 ? MasterPageContainer::SMALL
349                 : MasterPageContainer::LARGE);
350             mrBase.SetBusyState (false);
351             if (mxSidebar.is())
352                 mxSidebar->requestLayout();
353             break;
354         }
355 
356         case SID_TP_EDIT_MASTER:
357         {
358             using namespace ::com::sun::star;
359             uno::Reference<drawing::XDrawPage> xSelectedMaster (
360                 GetSelectedMasterPage()->getUnoPage(), uno::UNO_QUERY);
361             SfxViewFrame* pViewFrame = mrBase.GetViewFrame();
362             if (pViewFrame != NULL && xSelectedMaster.is())
363             {
364                 SfxDispatcher* pDispatcher = pViewFrame->GetDispatcher();
365                 if (pDispatcher != NULL)
366                 {
367                     sal_uInt16 nIndex = PreviewValueSet::GetSelectItemId();
368                     pDispatcher->Execute(SID_MASTERPAGE, SFX_CALLMODE_SYNCHRON);
369                     PreviewValueSet::SelectItem (nIndex);
370                     mrBase.GetDrawController().setCurrentPage(xSelectedMaster);
371                 }
372             }
373             break;
374         }
375 
376         case SID_CUT:
377         case SID_COPY:
378         case SID_PASTE:
379             // Cut, copy, and paste are not supported and thus are ignored.
380             break;
381     }
382 }
383 
384 
385 
386 
IMPL_LINK(MasterPagesSelector,ContainerChangeListener,MasterPageContainerChangeEvent *,pEvent)387 IMPL_LINK(MasterPagesSelector, ContainerChangeListener, MasterPageContainerChangeEvent*, pEvent)
388 {
389     if (pEvent)
390         NotifyContainerChangeEvent(*pEvent);
391     return 0;
392 }
393 
394 
395 
396 
GetSelectedMasterPage(void)397 SdPage* MasterPagesSelector::GetSelectedMasterPage (void)
398 {
399     const ::osl::MutexGuard aGuard (maMutex);
400 
401     SdPage* pMasterPage = NULL;
402     sal_uInt16 nIndex = PreviewValueSet::GetSelectItemId();
403     UserData* pData = GetUserData(nIndex);
404     if (pData != NULL)
405     {
406         pMasterPage = mpContainer->GetPageObjectForToken(pData->second);
407     }
408     return pMasterPage;
409 }
410 
411 
412 
413 
414 /** Assemble a list of all slides of the document and pass it to
415     AssignMasterPageToPageList().
416 */
AssignMasterPageToAllSlides(SdPage * pMasterPage)417 void MasterPagesSelector::AssignMasterPageToAllSlides (SdPage* pMasterPage)
418 {
419     do
420     {
421         if (pMasterPage == NULL)
422             break;
423 
424         sal_uInt16 nPageCount = mrDocument.GetSdPageCount(PK_STANDARD);
425         if (nPageCount == 0)
426             break;
427 
428         // Get a list of all pages.  As a little optimization we only
429         // include pages that do not already have the given master page
430         // assigned.
431         String sFullLayoutName (pMasterPage->GetLayoutName());
432         ::sd::slidesorter::SharedPageSelection pPageList (
433             new ::sd::slidesorter::SlideSorterViewShell::PageSelection());
434         for (sal_uInt16 nPageIndex=0; nPageIndex<nPageCount; nPageIndex++)
435         {
436             SdPage* pPage = mrDocument.GetSdPage (nPageIndex, PK_STANDARD);
437             if (pPage != NULL
438                 && pPage->GetLayoutName().CompareTo(sFullLayoutName)!=0)
439             {
440                 pPageList->push_back (pPage);
441             }
442         }
443 
444         AssignMasterPageToPageList(pMasterPage, pPageList);
445     }
446     while (false);
447 }
448 
449 
450 
451 
452 /** Assemble a list of the currently selected slides (selected in a visible
453     slide sorter) and pass it to AssignMasterPageToPageList().
454 */
AssignMasterPageToSelectedSlides(SdPage * pMasterPage)455 void MasterPagesSelector::AssignMasterPageToSelectedSlides (
456     SdPage* pMasterPage)
457 {
458     do
459     {
460         using namespace ::std;
461         using namespace ::sd::slidesorter;
462         using namespace ::sd::slidesorter::controller;
463 
464         if (pMasterPage == NULL)
465             break;
466 
467         // Find a visible slide sorter.
468         SlideSorterViewShell* pSlideSorter = SlideSorterViewShell::GetSlideSorter(mrBase);
469         if (pSlideSorter == NULL)
470             break;
471 
472         // Get a list of selected pages.
473         ::sd::slidesorter::SharedPageSelection pPageSelection = pSlideSorter->GetPageSelection();
474         if (pPageSelection->empty())
475             break;
476 
477         AssignMasterPageToPageList(pMasterPage, pPageSelection);
478 
479         // Restore the previous selection.
480         pSlideSorter->SetPageSelection(pPageSelection);
481     }
482     while (false);
483 }
484 
485 
486 
487 
AssignMasterPageToPageList(SdPage * pMasterPage,const::sd::slidesorter::SharedPageSelection & rPageList)488 void MasterPagesSelector::AssignMasterPageToPageList (
489     SdPage* pMasterPage,
490     const ::sd::slidesorter::SharedPageSelection& rPageList)
491 {
492     DocumentHelper::AssignMasterPageToPageList(mrDocument, pMasterPage, rPageList);
493 }
494 
495 
496 
497 
NotifyContainerChangeEvent(const MasterPageContainerChangeEvent & rEvent)498 void MasterPagesSelector::NotifyContainerChangeEvent (const MasterPageContainerChangeEvent& rEvent)
499 {
500     const ::osl::MutexGuard aGuard (maMutex);
501 
502     switch (rEvent.meEventType)
503     {
504         case MasterPageContainerChangeEvent::SIZE_CHANGED:
505             PreviewValueSet::SetPreviewSize(mpContainer->GetPreviewSizePixel());
506             UpdateAllPreviews();
507             break;
508 
509         case MasterPageContainerChangeEvent::PREVIEW_CHANGED:
510         {
511             int nIndex (GetIndexForToken(rEvent.maChildToken));
512             if (nIndex >= 0)
513             {
514                 PreviewValueSet::SetItemImage (
515                     (sal_uInt16)nIndex,
516                     mpContainer->GetPreviewForToken(rEvent.maChildToken));
517                 PreviewValueSet::Invalidate(PreviewValueSet::GetItemRect((sal_uInt16)nIndex));
518             }
519         }
520         break;
521 
522         case MasterPageContainerChangeEvent::DATA_CHANGED:
523         {
524             InvalidateItem(rEvent.maChildToken);
525             Fill();
526         }
527         break;
528 
529 		default:
530 			break;
531    }
532 }
533 
534 
535 
536 
CreateUserData(int nIndex,MasterPageContainer::Token aToken) const537 MasterPagesSelector::UserData* MasterPagesSelector::CreateUserData (
538     int nIndex,
539     MasterPageContainer::Token aToken) const
540 {
541     return new UserData(nIndex,aToken);
542 }
543 
544 
545 
546 
GetUserData(int nIndex) const547 MasterPagesSelector::UserData* MasterPagesSelector::GetUserData (int nIndex) const
548 {
549     const ::osl::MutexGuard aGuard (maMutex);
550 
551     if (nIndex>0 && nIndex<=PreviewValueSet::GetItemCount())
552         return reinterpret_cast<UserData*>(PreviewValueSet::GetItemData((sal_uInt16)nIndex));
553     else
554         return NULL;
555 }
556 
557 
558 
559 
SetUserData(int nIndex,UserData * pData)560 void MasterPagesSelector::SetUserData (int nIndex, UserData* pData)
561 {
562     const ::osl::MutexGuard aGuard (maMutex);
563 
564     if (nIndex>0 && nIndex<=PreviewValueSet::GetItemCount())
565     {
566         UserData* pOldData = GetUserData(nIndex);
567         if (pOldData!=NULL && pOldData!=pData)
568             delete pOldData;
569         PreviewValueSet::SetItemData((sal_uInt16)nIndex, pData);
570     }
571 }
572 
573 
574 
575 
IsResizable(void)576 bool MasterPagesSelector::IsResizable (void)
577 {
578     return false;
579 }
580 
581 
582 
583 
GetWindow(void)584 ::Window* MasterPagesSelector::GetWindow (void)
585 {
586     return this;
587 }
588 
589 
590 
591 
GetMinimumWidth(void)592 sal_Int32 MasterPagesSelector::GetMinimumWidth (void)
593 {
594     return mpContainer->GetPreviewSizePixel().Width() + 2*3;
595 }
596 
597 
598 
599 
UpdateSelection(void)600 void MasterPagesSelector::UpdateSelection (void)
601 {
602 }
603 
604 
605 
606 
SetItem(sal_uInt16 nIndex,MasterPageContainer::Token aToken)607 void MasterPagesSelector::SetItem (
608     sal_uInt16 nIndex,
609     MasterPageContainer::Token aToken)
610 {
611     const ::osl::MutexGuard aGuard (maMutex);
612 
613     RemoveTokenToIndexEntry(nIndex,aToken);
614 
615     if (nIndex > 0)
616     {
617         if (aToken != MasterPageContainer::NIL_TOKEN)
618         {
619             Image aPreview (mpContainer->GetPreviewForToken(aToken));
620             MasterPageContainer::PreviewState eState (mpContainer->GetPreviewState(aToken));
621 
622             if (aPreview.GetSizePixel().Width()>0)
623             {
624                 if (PreviewValueSet::GetItemPos(nIndex) != VALUESET_ITEM_NOTFOUND)
625                 {
626                     PreviewValueSet::SetItemImage(nIndex,aPreview);
627                     PreviewValueSet::SetItemText(nIndex, mpContainer->GetPageNameForToken(aToken));
628                 }
629                 else
630                 {
631                     PreviewValueSet::InsertItem (
632                         nIndex,
633                         aPreview,
634                         mpContainer->GetPageNameForToken(aToken),
635                         nIndex);
636                 }
637                 SetUserData(nIndex, CreateUserData(nIndex,aToken));
638 
639                 AddTokenToIndexEntry(nIndex,aToken);
640             }
641 
642             if (eState == MasterPageContainer::PS_CREATABLE)
643                 mpContainer->RequestPreview(aToken);
644         }
645         else
646         {
647             PreviewValueSet::RemoveItem(nIndex);
648         }
649     }
650 
651 }
652 
653 
654 
655 
AddTokenToIndexEntry(sal_uInt16 nIndex,MasterPageContainer::Token aToken)656 void MasterPagesSelector::AddTokenToIndexEntry (
657     sal_uInt16 nIndex,
658     MasterPageContainer::Token aToken)
659 {
660     const ::osl::MutexGuard aGuard (maMutex);
661 
662     maTokenToValueSetIndex[aToken] = nIndex;
663 }
664 
665 
666 
667 
RemoveTokenToIndexEntry(sal_uInt16 nIndex,MasterPageContainer::Token aNewToken)668 void MasterPagesSelector::RemoveTokenToIndexEntry (
669     sal_uInt16 nIndex,
670     MasterPageContainer::Token aNewToken)
671 {
672     const ::osl::MutexGuard aGuard (maMutex);
673 
674     UserData* pData = GetUserData(nIndex);
675     if (pData != NULL)
676     {
677         // Get the token that the index pointed to previously.
678         MasterPageContainer::Token aOldToken (pData->second);
679 
680         if (aNewToken != aOldToken
681             && nIndex == GetIndexForToken(aOldToken))
682         {
683             maTokenToValueSetIndex[aOldToken] = 0;
684         }
685     }
686 }
687 
688 
689 
690 
InvalidatePreview(const SdPage * pPage)691 void MasterPagesSelector::InvalidatePreview (const SdPage* pPage)
692 {
693     const ::osl::MutexGuard aGuard (maMutex);
694 
695     for (sal_uInt16 nIndex=1; nIndex<=PreviewValueSet::GetItemCount(); nIndex++)
696     {
697         UserData* pData = GetUserData(nIndex);
698         if (pData != NULL)
699         {
700             MasterPageContainer::Token aToken (pData->second);
701             if (pPage == mpContainer->GetPageObjectForToken(aToken,false))
702             {
703                 mpContainer->InvalidatePreview(aToken);
704                 mpContainer->RequestPreview(aToken);
705                 break;
706             }
707         }
708     }
709 }
710 
UpdateAllPreviews(void)711 void MasterPagesSelector::UpdateAllPreviews (void)
712 {
713     const ::osl::MutexGuard aGuard (maMutex);
714 
715     for (sal_uInt16 nIndex=1; nIndex<=PreviewValueSet::GetItemCount(); nIndex++)
716     {
717         UserData* pData = GetUserData(nIndex);
718         if (pData != NULL)
719         {
720             MasterPageContainer::Token aToken (pData->second);
721             PreviewValueSet::SetItemImage(
722                 nIndex,
723                 mpContainer->GetPreviewForToken(aToken));
724             if (mpContainer->GetPreviewState(aToken) == MasterPageContainer::PS_CREATABLE)
725                 mpContainer->RequestPreview(aToken);
726         }
727     }
728     PreviewValueSet::Rearrange(true);
729 }
730 
731 
732 
733 
ClearPageSet(void)734 void MasterPagesSelector::ClearPageSet (void)
735 {
736     const ::osl::MutexGuard aGuard (maMutex);
737 
738     for (sal_uInt16 nIndex=1; nIndex<=PreviewValueSet::GetItemCount(); nIndex++)
739     {
740         UserData* pData = GetUserData(nIndex);
741         if (pData != NULL)
742             delete pData;
743     }
744     PreviewValueSet::Clear();
745 }
746 
747 
748 
749 
SetHelpId(const rtl::OString & aId)750 void MasterPagesSelector::SetHelpId( const rtl::OString& aId )
751 {
752     const ::osl::MutexGuard aGuard (maMutex);
753 
754 	PreviewValueSet::SetHelpId( aId );
755 }
756 
757 
758 
759 
GetIndexForToken(MasterPageContainer::Token aToken) const760 sal_Int32 MasterPagesSelector::GetIndexForToken (MasterPageContainer::Token aToken) const
761 {
762     const ::osl::MutexGuard aGuard (maMutex);
763 
764     TokenToValueSetIndex::const_iterator iIndex (maTokenToValueSetIndex.find(aToken));
765     if (iIndex != maTokenToValueSetIndex.end())
766         return iIndex->second;
767     else
768         return -1;
769 }
770 
771 
772 
773 
Clear(void)774 void MasterPagesSelector::Clear (void)
775 {
776     const ::osl::MutexGuard aGuard (maMutex);
777 
778     ClearPageSet();
779 }
780 
781 
782 
783 
InvalidateItem(MasterPageContainer::Token aToken)784 void MasterPagesSelector::InvalidateItem (MasterPageContainer::Token aToken)
785 {
786     const ::osl::MutexGuard aGuard (maMutex);
787 
788     ItemList::iterator iItem;
789     for (iItem=maCurrentItemList.begin(); iItem!=maCurrentItemList.end(); ++iItem)
790     {
791         if (*iItem == aToken)
792         {
793             *iItem = MasterPageContainer::NIL_TOKEN;
794             break;
795         }
796     }
797 }
798 
799 
800 
801 
UpdateItemList(::std::auto_ptr<ItemList> pNewItemList)802 void MasterPagesSelector::UpdateItemList (::std::auto_ptr<ItemList> pNewItemList)
803 {
804     const ::osl::MutexGuard aGuard (maMutex);
805 
806     ItemList::const_iterator iNewItem (pNewItemList->begin());
807     ItemList::const_iterator iCurrentItem (maCurrentItemList.begin());
808     ItemList::const_iterator iNewEnd (pNewItemList->end());
809     ItemList::const_iterator iCurrentEnd (maCurrentItemList.end());
810     sal_uInt16 nIndex (1);
811 
812     // Update existing items.
813     for ( ; iNewItem!=iNewEnd && iCurrentItem!=iCurrentEnd; ++iNewItem, ++iCurrentItem,++nIndex)
814     {
815         if (*iNewItem != *iCurrentItem)
816         {
817             SetItem(nIndex,*iNewItem);
818         }
819     }
820 
821     // Append new items.
822     for ( ; iNewItem!=iNewEnd; ++iNewItem,++nIndex)
823     {
824         SetItem(nIndex,*iNewItem);
825     }
826 
827     // Remove trailing items.
828     for ( ; iCurrentItem!=iCurrentEnd; ++iCurrentItem,++nIndex)
829     {
830         SetItem(nIndex,MasterPageContainer::NIL_TOKEN);
831     }
832 
833     maCurrentItemList.swap(*pNewItemList);
834 
835     PreviewValueSet::Rearrange();
836     if (mxSidebar.is())
837         mxSidebar->requestLayout();
838 }
839 
840 
841 
842 
GetHeightForWidth(const sal_Int32 nWidth)843 css::ui::LayoutSize MasterPagesSelector::GetHeightForWidth (const sal_Int32 nWidth)
844 {
845     const sal_Int32 nHeight (GetPreferredHeight(nWidth));
846     return css::ui::LayoutSize(nHeight,nHeight,nHeight);
847 }
848 
849 } } // end of namespace sd::sidebar
850