1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sd.hxx"
#include "MasterPageContainer.hxx"
#include "MasterPageDescriptor.hxx"
#include "MasterPageContainerFiller.hxx"
#include "MasterPageContainerQueue.hxx"
#include "TemplateScanner.hxx"
#include "tools/AsynchronousTask.hxx"
#include "strings.hrc"
#include <algorithm>
#include <list>
#include <set>
#include "unomodel.hxx"
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/io/XStream.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/uno/Reference.hxx>
#include <com/sun/star/uno/Any.hxx>
#include <com/sun/star/uno/Sequence.hxx>
#include <com/sun/star/util/XCloseable.hpp>
#include <comphelper/processfactory.hxx>
#include <tools/urlobj.hxx>
#include <sfx2/app.hxx>
#include <svx/svdpage.hxx>
#include "DrawDocShell.hxx"
#include "drawdoc.hxx"
#include "sdpage.hxx"
#include <svl/itemset.hxx>
#include <svl/eitem.hxx>
#include "sdresid.hxx"
#include "tools/TimerBasedTaskExecution.hxx"
#include "pres.hxx"
#include <osl/mutex.hxx>
#include <boost/weak_ptr.hpp>
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::sd::toolpanel::controls;
namespace {
typedef ::std::vector<SharedMasterPageDescriptor> MasterPageContainerType;
} // end of anonymous namespace
namespace sd { namespace toolpanel { namespace controls {
/** Inner implementation class of the MasterPageContainer.
*/
class MasterPageContainer::Implementation
: public SdGlobalResource,
public MasterPageContainerFiller::ContainerAdapter,
public MasterPageContainerQueue::ContainerAdapter
{
public:
mutable ::osl::Mutex maMutex;
static ::boost::weak_ptr<Implementation> mpInstance;
MasterPageContainerType maContainer;
static ::boost::shared_ptr<Implementation> Instance (void);
void LateInit (void);
void AddChangeListener (const Link& rLink);
void RemoveChangeListener (const Link& rLink);
void UpdatePreviewSizePixel (void);
Size GetPreviewSizePixel (PreviewSize eSize) const;
bool HasToken (Token aToken) const;
const SharedMasterPageDescriptor GetDescriptor (MasterPageContainer::Token aToken) const;
SharedMasterPageDescriptor GetDescriptor (MasterPageContainer::Token aToken);
virtual Token PutMasterPage (const SharedMasterPageDescriptor& rDescriptor);
void InvalidatePreview (Token aToken);
Image GetPreviewForToken (
Token aToken,
PreviewSize ePreviewSize);
PreviewState GetPreviewState (Token aToken) const;
bool RequestPreview (Token aToken);
Reference<frame::XModel> GetModel (void);
SdDrawDocument* GetDocument (void);
void FireContainerChange (
MasterPageContainerChangeEvent::EventType eType,
Token aToken,
bool bNotifyAsynchronously = false);
virtual bool UpdateDescriptor (
const SharedMasterPageDescriptor& rpDescriptor,
bool bForcePageObject,
bool bForcePreview,
bool bSendEvents);
void ReleaseDescriptor (Token aToken);
/** Called by the MasterPageContainerFiller to notify that all master
pages from template documents have been added.
*/
virtual void FillingDone (void);
private:
Implementation (void);
virtual ~Implementation (void);
class Deleter { public:
void operator() (Implementation* pObject) { delete pObject; }
};
friend class Deleter;
enum InitializationState { NOT_INITIALIZED, INITIALIZING, INITIALIZED } meInitializationState;
::boost::scoped_ptr<MasterPageContainerQueue> mpRequestQueue;
::com::sun::star::uno::Reference<com::sun::star::frame::XModel> mxModel;
SdDrawDocument* mpDocument;
PreviewRenderer maPreviewRenderer;
/** Remember whether the first page object has already been used to
determine the correct size ratio.
*/
bool mbFirstPageObjectSeen;
// The widths for the previews contain two pixels for the border that is
// painted arround the preview.
static const int SMALL_PREVIEW_WIDTH = 72 + 2;
static const int LARGE_PREVIEW_WIDTH = 2*72 + 2;
/** This substition of page preview shows "Preparing preview" and is
shown as long as the actual previews are not being present.
*/
Image maLargePreviewBeingCreated;
Image maSmallPreviewBeingCreated;
/** This substition of page preview is shown when a preview can not be
created and thus is not available.
*/
Image maLargePreviewNotAvailable;
Image maSmallPreviewNotAvailable;
::std::vector<Link> maChangeListeners;
// We have to remember the tasks for initialization and filling in case
// a MasterPageContainer object is destroyed before these tasks have
// been completed.
::boost::weak_ptr<sd::tools::TimerBasedTaskExecution> mpFillerTask;
Size maSmallPreviewSizePixel;
Size maLargePreviewSizePixel;
bool mbPageRatioKnown;
bool mbContainerCleaningPending;
typedef ::std::pair<MasterPageContainerChangeEvent::EventType,Token> EventData;
DECL_LINK(AsynchronousNotifyCallback, EventData*);
::sd::DrawDocShell* LoadDocument (
const String& sFileName,
SfxObjectShellLock& rxDocumentShell);
Image GetPreviewSubstitution (sal_uInt16 nId, PreviewSize ePreviewSize);
void CleanContainer (void);
};
//===== MasterPageContainer ===================================================
::boost::weak_ptr<MasterPageContainer::Implementation>
MasterPageContainer::Implementation::mpInstance;
static const MasterPageContainer::Token NIL_TOKEN (-1);
::boost::shared_ptr<MasterPageContainer::Implementation>
MasterPageContainer::Implementation::Instance (void)
{
::boost::shared_ptr<MasterPageContainer::Implementation> pInstance;
if (Implementation::mpInstance.expired())
{
::osl::GetGlobalMutex aMutexFunctor;
::osl::MutexGuard aGuard (aMutexFunctor());
if (Implementation::mpInstance.expired())
{
OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
pInstance = ::boost::shared_ptr<MasterPageContainer::Implementation>(
new MasterPageContainer::Implementation(),
MasterPageContainer::Implementation::Deleter());
SdGlobalResourceContainer::Instance().AddResource(pInstance);
Implementation::mpInstance = pInstance;
}
else
pInstance = ::boost::shared_ptr<MasterPageContainer::Implementation>(
Implementation::mpInstance);
}
else
{
OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
pInstance = ::boost::shared_ptr<MasterPageContainer::Implementation>(
Implementation::mpInstance);
}
DBG_ASSERT (pInstance.get()!=NULL,
"MasterPageContainer::Implementation::Instance(): instance is NULL");
return pInstance;
}
MasterPageContainer::MasterPageContainer (void)
: mpImpl(Implementation::Instance()),
mePreviewSize(SMALL)
{
mpImpl->LateInit();
}
MasterPageContainer::~MasterPageContainer (void)
{
}
void MasterPageContainer::AddChangeListener (const Link& rLink)
{
mpImpl->AddChangeListener(rLink);
}
void MasterPageContainer::RemoveChangeListener (const Link& rLink)
{
mpImpl->RemoveChangeListener(rLink);
}
void MasterPageContainer::SetPreviewSize (PreviewSize eSize)
{
mePreviewSize = eSize;
mpImpl->FireContainerChange(
MasterPageContainerChangeEvent::SIZE_CHANGED,
NIL_TOKEN);
}
MasterPageContainer::PreviewSize MasterPageContainer::GetPreviewSize (void) const
{
return mePreviewSize;
}
Size MasterPageContainer::GetPreviewSizePixel (void) const
{
return mpImpl->GetPreviewSizePixel(mePreviewSize);
}
MasterPageContainer::Token MasterPageContainer::PutMasterPage (
const SharedMasterPageDescriptor& rDescriptor)
{
return mpImpl->PutMasterPage(rDescriptor);
}
void MasterPageContainer::AcquireToken (Token aToken)
{
SharedMasterPageDescriptor pDescriptor = mpImpl->GetDescriptor(aToken);
if (pDescriptor.get() != NULL)
{
++pDescriptor->mnUseCount;
}
}
void MasterPageContainer::ReleaseToken (Token aToken)
{
SharedMasterPageDescriptor pDescriptor = mpImpl->GetDescriptor(aToken);
if (pDescriptor.get() != NULL)
{
OSL_ASSERT(pDescriptor->mnUseCount>0);
--pDescriptor->mnUseCount;
if (pDescriptor->mnUseCount <= 0)
{
switch (pDescriptor->meOrigin)
{
case DEFAULT:
case TEMPLATE:
default:
break;
case MASTERPAGE:
mpImpl->ReleaseDescriptor(aToken);
break;
}
}
}
}
int MasterPageContainer::GetTokenCount (void) const
{
const ::osl::MutexGuard aGuard (mpImpl->maMutex);
return mpImpl->maContainer.size();
}
bool MasterPageContainer::HasToken (Token aToken) const
{
const ::osl::MutexGuard aGuard (mpImpl->maMutex);
return mpImpl->HasToken(aToken);
}
MasterPageContainer::Token MasterPageContainer::GetTokenForIndex (int nIndex)
{
const ::osl::MutexGuard aGuard (mpImpl->maMutex);
Token aResult (NIL_TOKEN);
if (HasToken(nIndex))
aResult = mpImpl->maContainer[nIndex]->maToken;
return aResult;
}
MasterPageContainer::Token MasterPageContainer::GetTokenForURL (
const String& sURL)
{
const ::osl::MutexGuard aGuard (mpImpl->maMutex);
Token aResult (NIL_TOKEN);
if (sURL.Len() > 0)
{
MasterPageContainerType::iterator iEntry (
::std::find_if (
mpImpl->maContainer.begin(),
mpImpl->maContainer.end(),
MasterPageDescriptor::URLComparator(sURL)));
if (iEntry != mpImpl->maContainer.end())
aResult = (*iEntry)->maToken;
}
return aResult;
}
MasterPageContainer::Token MasterPageContainer::GetTokenForStyleName (const String& sStyleName)
{
const ::osl::MutexGuard aGuard (mpImpl->maMutex);
Token aResult (NIL_TOKEN);
if (sStyleName.Len() > 0)
{
MasterPageContainerType::iterator iEntry (
::std::find_if (
mpImpl->maContainer.begin(),
mpImpl->maContainer.end(),
MasterPageDescriptor::StyleNameComparator(sStyleName)));
if (iEntry != mpImpl->maContainer.end())
aResult = (*iEntry)->maToken;
}
return aResult;
}
MasterPageContainer::Token MasterPageContainer::GetTokenForPageObject (
const SdPage* pPage)
{
const ::osl::MutexGuard aGuard (mpImpl->maMutex);
Token aResult (NIL_TOKEN);
if (pPage != NULL)
{
MasterPageContainerType::iterator iEntry (
::std::find_if (
mpImpl->maContainer.begin(),
mpImpl->maContainer.end(),
MasterPageDescriptor::PageObjectComparator(pPage)));
if (iEntry != mpImpl->maContainer.end())
aResult = (*iEntry)->maToken;
}
return aResult;
}
String MasterPageContainer::GetURLForToken (
MasterPageContainer::Token aToken)
{
const ::osl::MutexGuard aGuard (mpImpl->maMutex);
SharedMasterPageDescriptor pDescriptor = mpImpl->GetDescriptor(aToken);
if (pDescriptor.get() != NULL)
return pDescriptor->msURL;
else
return String();
}
String MasterPageContainer::GetPageNameForToken (
MasterPageContainer::Token aToken)
{
const ::osl::MutexGuard aGuard (mpImpl->maMutex);
SharedMasterPageDescriptor pDescriptor = mpImpl->GetDescriptor(aToken);
if (pDescriptor.get() != NULL)
return pDescriptor->msPageName;
else
return String();
}
String MasterPageContainer::GetStyleNameForToken (
MasterPageContainer::Token aToken)
{
const ::osl::MutexGuard aGuard (mpImpl->maMutex);
SharedMasterPageDescriptor pDescriptor = mpImpl->GetDescriptor(aToken);
if (pDescriptor.get() != NULL)
return pDescriptor->msStyleName;
else
return String();
}
SdPage* MasterPageContainer::GetPageObjectForToken (
MasterPageContainer::Token aToken,
bool bLoad)
{
const ::osl::MutexGuard aGuard (mpImpl->maMutex);
SdPage* pPageObject = NULL;
SharedMasterPageDescriptor pDescriptor = mpImpl->GetDescriptor(aToken);
if (pDescriptor.get() != NULL)
{
pPageObject = pDescriptor->mpMasterPage;
if (pPageObject == NULL)
{
// The page object is not (yet) present. Call
// UpdateDescriptor() to trigger the PageObjectProvider() to
// provide it.
if (bLoad)
mpImpl->GetModel();
if (mpImpl->UpdateDescriptor(pDescriptor,bLoad,false, true))
pPageObject = pDescriptor->mpMasterPage;
}
}
return pPageObject;
}
MasterPageContainer::Origin MasterPageContainer::GetOriginForToken (Token aToken)
{
const ::osl::MutexGuard aGuard (mpImpl->maMutex);
SharedMasterPageDescriptor pDescriptor = mpImpl->GetDescriptor(aToken);
if (pDescriptor.get() != NULL)
return pDescriptor->meOrigin;
else
return UNKNOWN;
}
sal_Int32 MasterPageContainer::GetTemplateIndexForToken (Token aToken)
{
const ::osl::MutexGuard aGuard (mpImpl->maMutex);
SharedMasterPageDescriptor pDescriptor = mpImpl->GetDescriptor(aToken);
if (pDescriptor.get() != NULL)
return pDescriptor->mnTemplateIndex;
else
return -1;
}
SharedMasterPageDescriptor MasterPageContainer::GetDescriptorForToken (
MasterPageContainer::Token aToken)
{
const ::osl::MutexGuard aGuard (mpImpl->maMutex);
return mpImpl->GetDescriptor(aToken);
}
void MasterPageContainer::InvalidatePreview (MasterPageContainer::Token aToken)
{
mpImpl->InvalidatePreview(aToken);
}
Image MasterPageContainer::GetPreviewForToken (MasterPageContainer::Token aToken)
{
return mpImpl->GetPreviewForToken(aToken,mePreviewSize);
}
MasterPageContainer::PreviewState MasterPageContainer::GetPreviewState (Token aToken)
{
return mpImpl->GetPreviewState(aToken);
}
bool MasterPageContainer::RequestPreview (Token aToken)
{
return mpImpl->RequestPreview(aToken);
}
//==== Implementation ================================================
MasterPageContainer::Implementation::Implementation (void)
: maMutex(),
maContainer(),
meInitializationState(NOT_INITIALIZED),
mpRequestQueue(NULL),
mxModel(NULL),
mpDocument(NULL),
maPreviewRenderer(),
mbFirstPageObjectSeen(false),
maLargePreviewBeingCreated(),
maSmallPreviewBeingCreated(),
maLargePreviewNotAvailable(),
maSmallPreviewNotAvailable(),
maChangeListeners(),
maSmallPreviewSizePixel(),
maLargePreviewSizePixel(),
mbPageRatioKnown(false),
mbContainerCleaningPending(true)
{
UpdatePreviewSizePixel();
}
MasterPageContainer::Implementation::~Implementation (void)
{
// When the initializer or filler tasks are still running then we have
// to stop them now in order to prevent them from calling us back.
tools::TimerBasedTaskExecution::ReleaseTask(mpFillerTask);
mpRequestQueue.reset();
uno::Reference<util::XCloseable> xCloseable (mxModel, uno::UNO_QUERY);
if (xCloseable.is())
{
try
{
xCloseable->close(true);
}
catch (::com::sun::star::util::CloseVetoException aException)
{
}
}
mxModel = NULL;
}
void MasterPageContainer::Implementation::LateInit (void)
{
const ::osl::MutexGuard aGuard (maMutex);
if (meInitializationState == NOT_INITIALIZED)
{
meInitializationState = INITIALIZING;
OSL_ASSERT(Instance().get()==this);
mpRequestQueue.reset(MasterPageContainerQueue::Create(
::boost::shared_ptr<MasterPageContainerQueue::ContainerAdapter>(Instance())));
mpFillerTask = ::sd::tools::TimerBasedTaskExecution::Create(
::boost::shared_ptr<tools::AsynchronousTask>(new MasterPageContainerFiller(*this)),
5,
50);
meInitializationState = INITIALIZED;
}
}
void MasterPageContainer::Implementation::AddChangeListener (const Link& rLink)
{
const ::osl::MutexGuard aGuard (maMutex);
::std::vector<Link>::iterator iListener (
::std::find(maChangeListeners.begin(),maChangeListeners.end(),rLink));
if (iListener == maChangeListeners.end())
maChangeListeners.push_back(rLink);
}
void MasterPageContainer::Implementation::RemoveChangeListener (const Link& rLink)
{
const ::osl::MutexGuard aGuard (maMutex);
::std::vector<Link>::iterator iListener (
::std::find(maChangeListeners.begin(),maChangeListeners.end(),rLink));
if (iListener != maChangeListeners.end())
maChangeListeners.erase(iListener);
}
void MasterPageContainer::Implementation::UpdatePreviewSizePixel (void)
{
const ::osl::MutexGuard aGuard (maMutex);
// The default aspect ratio is 4:3
int nWidth (4);
int nHeight (3);
// Search for the first entry with an existing master page.
MasterPageContainerType::const_iterator iDescriptor;
MasterPageContainerType::const_iterator iContainerEnd(maContainer.end());
for (iDescriptor=maContainer.begin(); iDescriptor!=iContainerEnd; ++iDescriptor)
if (*iDescriptor!=NULL && (*iDescriptor)->mpMasterPage != NULL)
{
Size aPageSize ((*iDescriptor)->mpMasterPage->GetSize());
OSL_ASSERT(aPageSize.Width() > 0 && aPageSize.Height() > 0);
if (aPageSize.Width() > 0)
nWidth = aPageSize.Width();
if (aPageSize.Height() > 0)
nHeight = aPageSize.Height();
mbFirstPageObjectSeen = true;
break;
}
maSmallPreviewSizePixel.Width() = SMALL_PREVIEW_WIDTH;
maLargePreviewSizePixel.Width() = LARGE_PREVIEW_WIDTH;
int nNewSmallHeight ((maSmallPreviewSizePixel.Width()-2) * nHeight / nWidth + 2);
int nNewLargeHeight ((maLargePreviewSizePixel.Width()-2) * nHeight / nWidth + 2);
if (nNewSmallHeight!=maSmallPreviewSizePixel.Height()
|| nNewLargeHeight!=maLargePreviewSizePixel.Height())
{
maSmallPreviewSizePixel.Height() = nNewSmallHeight;
maLargePreviewSizePixel.Height() = nNewLargeHeight;
FireContainerChange(
MasterPageContainerChangeEvent::SIZE_CHANGED,
NIL_TOKEN);
}
}
Size MasterPageContainer::Implementation::GetPreviewSizePixel (PreviewSize eSize) const
{
if (eSize == SMALL)
return maSmallPreviewSizePixel;
else
return maLargePreviewSizePixel;
}
IMPL_LINK(MasterPageContainer::Implementation,AsynchronousNotifyCallback, EventData*, pData)
{
const ::osl::MutexGuard aGuard (maMutex);
if (pData != NULL)
{
FireContainerChange(pData->first, pData->second, false);
delete pData;
}
return 0;
}
MasterPageContainer::Token MasterPageContainer::Implementation::PutMasterPage (
const SharedMasterPageDescriptor& rpDescriptor)
{
const ::osl::MutexGuard aGuard (maMutex);
Token aResult (NIL_TOKEN);
// Get page object and preview when that is inexpensive.
UpdateDescriptor(rpDescriptor,false,false, false);
// Look up the new MasterPageDescriptor and either insert it or update
// an already existing one.
MasterPageContainerType::iterator aEntry (
::std::find_if (
maContainer.begin(),
maContainer.end(),
MasterPageDescriptor::AllComparator(rpDescriptor)));
if (aEntry == maContainer.end())
{
// Insert a new MasterPageDescriptor.
bool bIgnore (rpDescriptor->mpPageObjectProvider.get()==NULL
&& rpDescriptor->msURL.getLength()==0);
if ( ! bIgnore)
{
if (mbContainerCleaningPending)
CleanContainer();
aResult = maContainer.size();
rpDescriptor->SetToken(aResult);
// Templates are precious, i.e. we lock them so that they will
// not be destroyed when (temporarily) no one references them.
// They will only be deleted when the container is destroyed.
switch (rpDescriptor->meOrigin)
{
case TEMPLATE:
case DEFAULT:
++rpDescriptor->mnUseCount;
break;
default:
break;
}
maContainer.push_back(rpDescriptor);
aEntry = maContainer.end()-1;
FireContainerChange(MasterPageContainerChangeEvent::CHILD_ADDED,aResult);
}
}
else
{
// Update an existing MasterPageDescriptor.
aResult = (*aEntry)->maToken;
::std::auto_ptr<std::vector<MasterPageContainerChangeEvent::EventType> > pEventTypes(
(*aEntry)->Update(*rpDescriptor));
if (pEventTypes.get()!=NULL && pEventTypes->size()>0)
{
// One or more aspects of the descriptor have changed. Send
// appropriate events to the listeners.
UpdateDescriptor(*aEntry,false,false, true);
std::vector<MasterPageContainerChangeEvent::EventType>::const_iterator iEventType;
for (iEventType=pEventTypes->begin(); iEventType!=pEventTypes->end(); ++iEventType)
{
FireContainerChange(
*iEventType,
(*aEntry)->maToken,
false);
}
}
}
return aResult;
}
bool MasterPageContainer::Implementation::HasToken (Token aToken) const
{
return aToken>=0
&& (unsigned)aToken<maContainer.size()
&& maContainer[aToken].get()!=NULL;
}
const SharedMasterPageDescriptor MasterPageContainer::Implementation::GetDescriptor (
Token aToken) const
{
if (aToken>=0 && (unsigned)aToken<maContainer.size())
return maContainer[aToken];
else
return SharedMasterPageDescriptor();
}
SharedMasterPageDescriptor MasterPageContainer::Implementation::GetDescriptor (Token aToken)
{
if (aToken>=0 && (unsigned)aToken<maContainer.size())
return maContainer[aToken];
else
return SharedMasterPageDescriptor();
}
void MasterPageContainer::Implementation::InvalidatePreview (Token aToken)
{
const ::osl::MutexGuard aGuard (maMutex);
SharedMasterPageDescriptor pDescriptor (GetDescriptor(aToken));
if (pDescriptor.get() != NULL)
{
pDescriptor->maSmallPreview = Image();
pDescriptor->maLargePreview = Image();
RequestPreview(aToken);
}
}
Image MasterPageContainer::Implementation::GetPreviewForToken (
MasterPageContainer::Token aToken,
PreviewSize ePreviewSize)
{
const ::osl::MutexGuard aGuard (maMutex);
Image aPreview;
PreviewState ePreviewState (GetPreviewState(aToken));
SharedMasterPageDescriptor pDescriptor = GetDescriptor(aToken);
// When the preview is missing but inexpensively creatable then do that
// now.
if (pDescriptor.get()!=NULL)
{
if (ePreviewState == PS_CREATABLE)
if (UpdateDescriptor(pDescriptor, false,false, true))
if (pDescriptor->maLargePreview.GetSizePixel().Width() != 0)
ePreviewState = PS_AVAILABLE;
switch (ePreviewState)
{
case PS_AVAILABLE:
aPreview = pDescriptor->GetPreview(ePreviewSize);
break;
case PS_PREPARING:
aPreview = GetPreviewSubstitution(
STR_TASKPANEL_PREPARING_PREVIEW_SUBSTITUTION,
ePreviewSize);
break;
case PS_CREATABLE:
aPreview = GetPreviewSubstitution(
STR_TASKPANEL_PREPARING_PREVIEW_SUBSTITUTION,
ePreviewSize);
break;
case PS_NOT_AVAILABLE:
aPreview = GetPreviewSubstitution(
STR_TASKPANEL_NOT_AVAILABLE_SUBSTITUTION,
ePreviewSize);
if (ePreviewSize == SMALL)
pDescriptor->maSmallPreview = aPreview;
else
pDescriptor->maLargePreview = aPreview;
break;
}
}
return aPreview;
}
MasterPageContainer::PreviewState MasterPageContainer::Implementation::GetPreviewState (
Token aToken) const
{
const ::osl::MutexGuard aGuard (maMutex);
PreviewState eState (PS_NOT_AVAILABLE);
SharedMasterPageDescriptor pDescriptor = GetDescriptor(aToken);
if (pDescriptor.get() != NULL)
{
if (pDescriptor->maLargePreview.GetSizePixel().Width() != 0)
eState = PS_AVAILABLE;
else if (pDescriptor->mpPreviewProvider.get() != NULL)
{
// The preview does not exist but can be created. When that is
// not expensive then do it at once.
if (mpRequestQueue->HasRequest(aToken))
eState = PS_PREPARING;
else
eState = PS_CREATABLE;
}
else
eState = PS_NOT_AVAILABLE;
}
return eState;
}
bool MasterPageContainer::Implementation::RequestPreview (Token aToken)
{
SharedMasterPageDescriptor pDescriptor = GetDescriptor(aToken);
if (pDescriptor.get() != NULL)
return mpRequestQueue->RequestPreview(pDescriptor);
else
return false;
}
Reference<frame::XModel> MasterPageContainer::Implementation::GetModel (void)
{
const ::osl::MutexGuard aGuard (maMutex);
if ( ! mxModel.is())
{
// Get the desktop a s service factory.
::rtl::OUString sDesktopServiceName (
RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop"));
uno::Reference<frame::XComponentLoader> xDesktop (
::comphelper::getProcessServiceFactory()->createInstance(
sDesktopServiceName),
uno::UNO_QUERY);
// Create a new model.
::rtl::OUString sModelServiceName (
RTL_CONSTASCII_USTRINGPARAM(
"com.sun.star.presentation.PresentationDocument"));
mxModel = uno::Reference<frame::XModel>(
::comphelper::getProcessServiceFactory()->createInstance(
sModelServiceName),
uno::UNO_QUERY);
// Initialize the model.
uno::Reference<frame::XLoadable> xLoadable (mxModel,uno::UNO_QUERY);
if (xLoadable.is())
xLoadable->initNew();
// Use its tunnel to get a pointer to its core implementation.
uno::Reference<lang::XUnoTunnel> xUnoTunnel (mxModel, uno::UNO_QUERY);
if (xUnoTunnel.is())
{
mpDocument = reinterpret_cast<SdXImpressDocument*>(
xUnoTunnel->getSomething(
SdXImpressDocument::getUnoTunnelId()))->GetDoc();
}
// Create a default page.
uno::Reference<drawing::XDrawPagesSupplier> xSlideSupplier (mxModel, uno::UNO_QUERY);
if (xSlideSupplier.is())
{
uno::Reference<drawing::XDrawPages> xSlides (
xSlideSupplier->getDrawPages(), uno::UNO_QUERY);
if (xSlides.is())
{
sal_Int32 nIndex (0);
uno::Reference<drawing::XDrawPage> xNewPage (xSlides->insertNewByIndex(nIndex));
uno::Reference<beans::XPropertySet> xProperties(xNewPage, uno::UNO_QUERY);
if (xProperties.is())
xProperties->setPropertyValue(
rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Layout")),
makeAny((sal_Int16)AUTOLAYOUT_TITLE));
}
}
}
return mxModel;
}
SdDrawDocument* MasterPageContainer::Implementation::GetDocument (void)
{
GetModel();
return mpDocument;
}
Image MasterPageContainer::Implementation::GetPreviewSubstitution (
sal_uInt16 nId,
PreviewSize ePreviewSize)
{
const ::osl::MutexGuard aGuard (maMutex);
Image aPreview;
switch (nId)
{
case STR_TASKPANEL_PREPARING_PREVIEW_SUBSTITUTION:
{
Image& rPreview (ePreviewSize==SMALL
? maSmallPreviewBeingCreated
: maLargePreviewBeingCreated);
if (rPreview.GetSizePixel().Width() == 0)
{
rPreview = maPreviewRenderer.RenderSubstitution(
ePreviewSize==SMALL ? maSmallPreviewSizePixel : maLargePreviewSizePixel,
SdResId(STR_TASKPANEL_PREPARING_PREVIEW_SUBSTITUTION));
}
aPreview = rPreview;
}
break;
case STR_TASKPANEL_NOT_AVAILABLE_SUBSTITUTION:
{
Image& rPreview (ePreviewSize==SMALL
? maSmallPreviewNotAvailable
: maLargePreviewNotAvailable);
if (rPreview.GetSizePixel().Width() == 0)
{
rPreview = maPreviewRenderer.RenderSubstitution(
ePreviewSize==SMALL ? maSmallPreviewSizePixel : maLargePreviewSizePixel,
SdResId(STR_TASKPANEL_NOT_AVAILABLE_SUBSTITUTION));
}
aPreview = rPreview;
}
break;
}
return aPreview;
}
void MasterPageContainer::Implementation::CleanContainer (void)
{
// Remove the empty elements at the end of the container. The empty
// elements in the middle can not be removed because that would
// invalidate the references still held by others.
int nIndex (maContainer.size()-1);
while (nIndex>=0 && maContainer[nIndex].get()==NULL)
--nIndex;
maContainer.resize(++nIndex);
}
void MasterPageContainer::Implementation::FireContainerChange (
MasterPageContainerChangeEvent::EventType eType,
Token aToken,
bool bNotifyAsynchronously)
{
if (bNotifyAsynchronously)
{
Application::PostUserEvent(
LINK(this,Implementation,AsynchronousNotifyCallback),
new EventData(eType,aToken));
}
else
{
::std::vector<Link> aCopy(maChangeListeners.begin(),maChangeListeners.end());
::std::vector<Link>::iterator iListener;
MasterPageContainerChangeEvent aEvent;
aEvent.meEventType = eType;
aEvent.maChildToken = aToken;
for (iListener=aCopy.begin(); iListener!=aCopy.end(); ++iListener)
iListener->Call(&aEvent);
}
}
bool MasterPageContainer::Implementation::UpdateDescriptor (
const SharedMasterPageDescriptor& rpDescriptor,
bool bForcePageObject,
bool bForcePreview,
bool bSendEvents)
{
const ::osl::MutexGuard aGuard (maMutex);
// We have to create the page object when the preview provider needs it
// and the caller needs the preview.
bForcePageObject |= (bForcePreview
&& rpDescriptor->mpPreviewProvider->NeedsPageObject()
&& rpDescriptor->mpMasterPage==NULL);
// Define a cost threshold so that an update or page object or preview
// that is at least this cost are made at once. Updates with higher cost
// are scheduled for later.
sal_Int32 nCostThreshold (mpRequestQueue->IsEmpty() ? 5 : 0);
// Update the page object (which may be used for the preview update).
if (bForcePageObject)
GetDocument();
bool bPageObjectModified (rpDescriptor->UpdatePageObject(
(bForcePageObject ? -1 : nCostThreshold),
mpDocument));
if (bPageObjectModified && bSendEvents)
FireContainerChange(
MasterPageContainerChangeEvent::DATA_CHANGED,
rpDescriptor->maToken);
if (bPageObjectModified && ! mbFirstPageObjectSeen)
UpdatePreviewSizePixel();
// Update the preview.
bool bPreviewModified (rpDescriptor->UpdatePreview(
(bForcePreview ? -1 : nCostThreshold),
maSmallPreviewSizePixel,
maLargePreviewSizePixel,
maPreviewRenderer));
if (bPreviewModified && bSendEvents)
FireContainerChange(
MasterPageContainerChangeEvent::PREVIEW_CHANGED,
rpDescriptor->maToken);
return bPageObjectModified || bPreviewModified;
}
void MasterPageContainer::Implementation::ReleaseDescriptor (Token aToken)
{
if (aToken>=0 && (unsigned)aToken<maContainer.size())
{
maContainer[aToken].reset();
mbContainerCleaningPending = true;
}
}
void MasterPageContainer::Implementation::FillingDone (void)
{
mpRequestQueue->ProcessAllRequests();
}
} } } // end of namespace ::sd::toolpanel::controls
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|