summaryrefslogtreecommitdiff
path: root/chart2/source/controller/main/ChartController.cxx
blob: c21b4983e565dc827838490d80a038996a033f41 (plain)
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
/*************************************************************************
 *
 *  $RCSfile: ChartController.cxx,v $
 *
 *  $Revision: 1.5 $
 *
 *  last change: $Author: iha $ $Date: 2003-10-30 12:15:45 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library 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 for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2003 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/
#include "ChartController.hxx"
#include "servicenames.hxx"
#include "SchItemPool.hxx"

#include "InlineContainer.hxx"
#include "Chart.hrc"
#include "ResId.hxx"
#include "SchSlotIds.hxx"

#include "chartview/ChartView.hxx"
#include "ChartWindow.hxx"
#include "DrawModelWrapper.hxx"
#include "DrawViewWrapper.hxx"

#include "macros.hxx"

#include "chartview/NumberFormatterWrapper.hxx"

#include "dlg_ChartType.hxx"
#include "ChartTypeItemConverter.hxx"

//for SID_CHARMAP:
#ifndef _SVX_SVXIDS_HRC
#include <svx/svxids.hrc>
#endif

#ifndef _DRAFTS_COM_SUN_STAR_CHART2_XCHARTDOCUMENT_HPP_
#include <drafts/com/sun/star/chart2/XChartDocument.hpp>
#endif
#ifndef _DRAFTS_COM_SUN_STAR_CHART2_XSTACKABLESCALEGROUP_HPP_
#include <drafts/com/sun/star/chart2/XStackableScaleGroup.hpp>
#endif

//-------
// header for define RET_OK
#ifndef _SV_MSGBOX_HXX
#include <vcl/msgbox.hxx>
#endif
//-------

//-------
#ifndef _TOOLKIT_AWT_VCLXWINDOW_HXX_
#include <toolkit/awt/vclxwindow.hxx>
#endif
#ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
#include <toolkit/helper/vclunohelper.hxx>
#endif
#ifndef _VOS_MUTEX_HXX_
#include <vos/mutex.hxx>
#endif
//-------
#ifndef _COMPHELPER_PROCESSFACTORY_HXX_
#include <comphelper/processfactory.hxx>
#endif
#ifndef _COM_SUN_STAR_UTIL_XURLTRANSFORMER_HPP_
#include <com/sun/star/util/XURLTransformer.hpp>
#endif

//.............................................................................
namespace chart
{
//.............................................................................

using namespace ::com::sun::star;
using namespace ::drafts::com::sun::star::chart2;

//-----------------------------------------------------------------
// ChartController Constructor and Destructor
//-----------------------------------------------------------------

ChartController::ChartController(uno::Reference<uno::XComponentContext> const & xContext)
    : m_aLifeTimeManager( NULL )
    , m_bSuspended( sal_False )
    , m_bCanClose( sal_True )
    , m_xCC(xContext) //@todo is it allowed to hold this context??
    , m_xFrame( NULL )
    , m_aModelMutex()
    , m_aModel( NULL, m_aModelMutex )
    , m_pChartWindow( NULL )
    , m_xViewWindow( NULL )
    , m_pChartView( NULL )
    , m_pDrawModelWrapper( NULL )
    , m_pDrawViewWrapper(NULL)
    , m_bViewDirty( false )
    , m_pNumberFormatterWrapper(NULL)
{

    //@todo
    m_pNumberFormatterWrapper = new NumberFormatterWrapper();
}

ChartController::~ChartController()
{
    impl_deleteView();
    //m_pChartWindow is deleted via UNO by Window hierarchy or Framework
    if(m_pNumberFormatterWrapper)
        delete m_pNumberFormatterWrapper;

    //@todo ?
}

//-----------------------------------------------------------------

ChartController::RefCountable::RefCountable() : m_nRefCount(0)
{
}

ChartController::RefCountable::~RefCountable()
{
}
void ChartController::RefCountable::acquire()
{
    m_nRefCount++;
}
void ChartController::RefCountable::release()
{
    m_nRefCount--;
    if(!m_nRefCount)
        delete this;
}

//-----------------------------------------------------------------

ChartController::TheModel::TheModel( const uno::Reference< frame::XModel > & xModel )
    : m_xModel( xModel )
    , m_xCloseable( NULL )
    , m_bOwnership( sal_True )
    , m_bOwnershipIsWellKnown( sal_False )
{
    m_xCloseable =
        uno::Reference< util::XCloseable >( xModel, uno::UNO_QUERY );
}

ChartController::TheModel::~TheModel()
{
}

void ChartController::TheModel::SetOwnerShip( sal_Bool bGetsOwnership )
{
    m_bOwnership                = bGetsOwnership;
    m_bOwnershipIsWellKnown = sal_True;
}

void ChartController::TheModel::addListener( ChartController* pController )
{
    if(m_xCloseable.is())
    {
        //if you need to be able to veto against the destruction of the model
        // you must add as a close listener

        //otherwise you 'can' add as closelistener or 'must' add as dispose event listener

        m_xCloseable->addCloseListener(
            static_cast<util::XCloseListener*>(pController) );
    }
    else if( m_xModel.is() )
    {
        //we need to add as dispose event listener
        m_xModel->addEventListener(
            static_cast<lang::XEventListener*>(pController) );
    }

}

void ChartController::TheModel::removeListener(  ChartController* pController )
{
    if(m_xCloseable.is())
        m_xCloseable->removeCloseListener(
            static_cast<util::XCloseListener*>(pController) );

    else if( m_xModel.is() )
        m_xModel->removeEventListener(
            static_cast<lang::XEventListener*>(pController) );
}

void ChartController::TheModel::tryTermination()
{
    if(!m_bOwnership)
        return;

    try
    {
        if(m_xCloseable.is())
        {
            try
            {
                //@todo ? are we allowed to use sal_True here if we have the explicit ownership?
                //I think yes, because there might be other closelistners later in the list which might be interested still
                //but make sure that we do not throw the CloseVetoException here ourselfs
                //so stop listening before trying to terminate or check the source of queryclosing event
                m_xCloseable->close(sal_True);

                m_bOwnership                = false;
                m_bOwnershipIsWellKnown = sal_True;
            }
            catch( util::CloseVetoException& )
            {
                //since we have indicated to give up the ownership with paramter true in close call
                //the one who has thrown the CloseVetoException is the new owner

                OSL_ENSURE( !m_bOwnership,
                    "INFO: a well known owner has catched a CloseVetoException after calling close(true)" );

                m_bOwnership                = false;
                m_bOwnershipIsWellKnown = sal_True;
                return;
            }

        }
        else if( m_xModel.is() )
        {
            //@todo correct??
            m_xModel->dispose();
            return;
        }
    }
    catch( uno::Exception& ex)
    {
        OSL_ENSURE( sal_False, ( rtl::OString("Termination of model failed: ")
            + rtl::OUStringToOString( ex.Message, RTL_TEXTENCODING_ASCII_US ) ).getStr() );
    }
}

//-----------------------------------------------------------------

ChartController::TheModelRef::TheModelRef( TheModel* pTheModel, ::osl::Mutex& rMutex )
        : m_pTheModel(pTheModel), m_rModelMutex(rMutex)
{
    ::osl::Guard< ::osl::Mutex > aGuard( m_rModelMutex );
    if(m_pTheModel)
        m_pTheModel->acquire();
}
ChartController::TheModelRef::TheModelRef( const TheModelRef& rTheModel, ::osl::Mutex& rMutex )
        : m_rModelMutex(rMutex)
{
    ::osl::Guard< ::osl::Mutex > aGuard( m_rModelMutex );
    m_pTheModel=rTheModel.operator->();
    if(m_pTheModel)
        m_pTheModel->acquire();
}
ChartController::TheModelRef& ChartController::TheModelRef::operator=(TheModel* pTheModel)
{
    ::osl::Guard< ::osl::Mutex > aGuard( m_rModelMutex );
    if(m_pTheModel==pTheModel)
        return *this;
    if(m_pTheModel)
        m_pTheModel->release();
    m_pTheModel=pTheModel;
    if(m_pTheModel)
        m_pTheModel->acquire();
    return *this;
}
ChartController::TheModelRef& ChartController::TheModelRef::operator=(const TheModelRef& rTheModel)
{
    ::osl::Guard< ::osl::Mutex > aGuard( m_rModelMutex );
    TheModel* pNew=rTheModel.operator->();
    if(m_pTheModel==pNew)
        return *this;
    if(m_pTheModel)
        m_pTheModel->release();
    m_pTheModel=pNew;
    if(m_pTheModel)
        m_pTheModel->acquire();
    return *this;
}
ChartController::TheModelRef::~TheModelRef()
{
    ::osl::Guard< ::osl::Mutex > aGuard( m_rModelMutex );
    if(m_pTheModel)
        m_pTheModel->release();
}
sal_Bool ChartController::TheModelRef::is() const
{
    return (m_pTheModel != 0);
}


//-----------------------------------------------------------------
// private methods
//-----------------------------------------------------------------

    sal_Bool ChartController
::impl_isDisposedOrSuspended()
{
    if( m_aLifeTimeManager.impl_isDisposed() )
        return sal_True;

    if( m_bSuspended )
    {
        OSL_ENSURE( sal_False, "This Controller is suspended" );
        return sal_True;
    }
    return sal_False;
}

//-----------------------------------------------------------------
// lang::XServiceInfo
//-----------------------------------------------------------------

APPHELPER_XSERVICEINFO_IMPL(ChartController,CHART_CONTROLLER_SERVICE_IMPLEMENTATION_NAME)

    uno::Sequence< rtl::OUString > ChartController
::getSupportedServiceNames_Static()
{
    uno::Sequence< rtl::OUString > aSNS( 2 );
    aSNS.getArray()[ 0 ] = CHART_CONTROLLER_SERVICE_NAME;
    aSNS.getArray()[ 1 ] = ::rtl::OUString::createFromAscii("com.sun.star.frame.Controller");
    //// @todo : add additional services if you support any further
    return aSNS;
}

//-----------------------------------------------------------------
// XController
//-----------------------------------------------------------------

        void SAL_CALL ChartController
::attachFrame( const uno::Reference<frame::XFrame>& xFrame )
        throw(uno::RuntimeException)
{
    osl::Guard< osl::Mutex > aGuard( m_aControllerMutex );

    if( impl_isDisposedOrSuspended() ) //@todo? allow attaching the frame while suspended?
        return; //behave passive if already disposed or suspended

    if(m_xFrame.is()) //what happens, if we do have a Frame already??
    {
        //@todo? throw exception?
        OSL_ENSURE( sal_False, "there is already a frame attached to the controller" );
        return;
    }

    //--attach frame
    m_xFrame = xFrame; //the frameloader is responsible to call xFrame->setComponent

    //add as disposelistener to the frame (due to persistent reference) ??...:

    //the frame is considered to be owner of this controller and will live longer than we do
    //the frame or the disposer of the frame has the duty to call suspend and dispose on this object
    //so we do not need to add as lang::XEventListener for DisposingEvents right?

    //@todo nothing right???



    //--------------------------------------------------
    //create view @todo is this the correct place here??

    Window* pParent = NULL;
    //get the window parent from the frame to use as parent for our new window
    if(xFrame.is())
    {
        uno::Reference< awt::XWindow > xContainerWindow = xFrame->getContainerWindow();
        VCLXWindow* pParentComponent = VCLXWindow::GetImplementation(xContainerWindow);
        pParentComponent->setVisible(sal_True);

        pParent = VCLUnoHelper::GetWindow( xContainerWindow );
    }

    if(m_pChartWindow)
    {
        //@todo delete ...
    }
    {
        // calls to VCL
        ::vos::OGuard aSolarGuard( Application::GetSolarMutex());
        m_pChartWindow = new ChartWindow(this,pParent,pParent?pParent->GetStyle():0);
        m_xViewWindow = uno::Reference< awt::XWindow >( m_pChartWindow->GetComponentInterface(), uno::UNO_QUERY );
        m_pChartWindow->Show();
    }

    impl_tryInitializeView();

    {//create the menu
        util::URL aURL( SchResId(RID_MENU).getURL() );

        uno::Reference< lang::XMultiServiceFactory >  xMgr = comphelper::getProcessServiceFactory();
        uno::Reference< util::XURLTransformer > xTrans(
                    xMgr->createInstance( ::rtl::OUString::createFromAscii(
                    "com.sun.star.util.URLTransformer") ), uno::UNO_QUERY );
        if( xTrans.is() )
        {
            // Datei laden
            xTrans->parseStrict( aURL );

            uno::Reference< frame::XDispatchProvider > xProv( xFrame, uno::UNO_QUERY );
            if ( xProv.is() )
            {
                uno::Reference< frame::XDispatch > aDisp =
                    xProv->queryDispatch( aURL,
                    ::rtl::OUString::createFromAscii("_menubar"), 12 );
                if ( aDisp.is() )
                    aDisp->dispatch( aURL, uno::Sequence<beans::PropertyValue>() );
            }
        }
    }
}

void SAL_CALL ChartController
::impl_rebuildView()
    throw(uno::RuntimeException)
{
    m_bViewDirty = false;
    impl_deleteView();
    impl_tryInitializeView();
    if( m_aSelectedObjectCID.getLength() )
    {
        //@todo reselect object
        m_aSelectedObjectCID = C2U("");
    }
}

void SAL_CALL ChartController
::impl_deleteView()
        throw(uno::RuntimeException)
{
    if( m_pDrawViewWrapper->IsTextEdit() )
        this->EndTextEdit();

    delete m_pChartView; m_pChartView = NULL;
    delete m_pDrawViewWrapper; m_pDrawViewWrapper = NULL;
    delete m_pDrawModelWrapper; m_pDrawModelWrapper = NULL;
}

        sal_Bool SAL_CALL ChartController
::impl_tryInitializeView()
        throw(uno::RuntimeException)
{
    osl::ClearableGuard< osl::Mutex > aGuard( m_aControllerMutex );
    if(!m_pChartWindow || !m_aModel.is() )
        return sal_False;

    m_pDrawModelWrapper = new DrawModelWrapper(m_xCC
        ,wrapper::SchItemPool::CreateSchItemPool());

    uno::Reference< frame::XModel > xDrawModel = m_pDrawModelWrapper->getUnoModel();
    if( xDrawModel.is())
        xDrawModel->lockControllers();

    m_pChartView = ChartView::createView( m_xCC, m_aModel->getModel()
                                , uno::Reference<drawing::XDrawPagesSupplier>::query( xDrawModel )
                                , m_pNumberFormatterWrapper );
    //OutputDevice* pOutDev = m_pDrawViewWrapper->GetWin(0);
    Size aPageSize = m_pChartWindow->GetOutputSize();
    m_pChartView->create( awt::Size( aPageSize.Width(), aPageSize.Height() ) );
//  m_pChartWindow->SetChartView(m_pChartView);//enable event flow from window to View (Window knows View)

    //create draw view:
    m_pDrawViewWrapper = new DrawViewWrapper(&m_pDrawModelWrapper->getSdrModel(),m_pChartWindow);
    //test:
    //Rectangle aTest = m_pDrawViewWrapper->GetWorkArea();
    //m_pDrawViewWrapper->SetWorkArea(pOutDev->PixelToLogic(Rectangle(rOfs, rSize)));

    if( xDrawModel.is())
    {
        xDrawModel->unlockControllers();
        m_pChartWindow->Invalidate();
    }
    return sal_True;
}

        sal_Bool SAL_CALL ChartController
::attachModel( const uno::Reference< frame::XModel > & xModel )
        throw(uno::RuntimeException)
{
    //is called to attach the controller to a new model.
    //return true if attach was successfully, false otherwise (e.g. if you do not work with a model)

    osl::ClearableGuard< osl::Mutex > aGuard( m_aControllerMutex );
    if( impl_isDisposedOrSuspended() ) //@todo? allow attaching a new model while suspended?
        return sal_False; //behave passive if already disposed or suspended
    aGuard.clear();


    TheModelRef aNewModelRef( new TheModel( xModel), m_aModelMutex);
    TheModelRef aOldModelRef(m_aModel,m_aModelMutex);
    m_aModel = aNewModelRef;

    //--handle relations to the old model if any
    if( aOldModelRef.is() )
    {
        aOldModelRef->removeListener( this );
        //@todo?? termination correct?
        aOldModelRef->tryTermination();
    }

    //--handle relations to the new model
    aNewModelRef->addListener( this );

    //the frameloader is responsible to call xModel->connectController

    {
        // Indirect calls to VCL
        ::vos::OGuard aSolarGuard( Application::GetSolarMutex());
        impl_tryInitializeView();
    }

    return sal_True;
}

        uno::Reference< frame::XFrame > SAL_CALL ChartController
::getFrame()    throw(uno::RuntimeException)
{
    //provides access to owner frame of this controller
    //return the frame containing this controller

    return m_xFrame;
}

        uno::Reference< frame::XModel > SAL_CALL ChartController
::getModel()    throw(uno::RuntimeException)
{
    //provides access to currently attached model
    //returns the currently attached model

    //return nothing, if you do not have a model
    TheModelRef aModelRef( m_aModel, m_aModelMutex);
    if(aModelRef.is())
        return aModelRef->getModel();

    return uno::Reference< frame::XModel > ();
}

        uno::Any SAL_CALL ChartController
::getViewData() throw(uno::RuntimeException)
{
    //provides access to current view status
    //set of data that can be used to restore the current view status at later time
    //  by using XController::restoreViewData()

    osl::ResettableGuard< osl::Mutex > aGuard( m_aControllerMutex );
    if( impl_isDisposedOrSuspended() )
        return uno::Any(); //behave passive if already disposed or suspended //@todo? or throw an exception??

    //-- collect current view state
    uno::Any aRet;
    //// @todo integrate specialized implementation

    return aRet;
}

        void SAL_CALL ChartController
::restoreViewData( const uno::Any& Value )
        throw(uno::RuntimeException)
{
    //restores the view status using the data gotten from a previous call to XController::getViewData()

    osl::ResettableGuard< osl::Mutex > aGuard( m_aControllerMutex );
    if( impl_isDisposedOrSuspended() )
        return; //behave passive if already disposed or suspended //@todo? or throw an exception??

    //// @todo integrate specialized implementation
}

        sal_Bool SAL_CALL ChartController
::suspend( sal_Bool bSuspend )
        throw(uno::RuntimeException)
{
    //is called to prepare the controller for closing the view
    //bSuspend==true: force the controller to suspend his work
    //bSuspend==false try to reactivate the controller
    //returns true if request was accepted and of course successfully finished, false otherwise

    //we may show dialogs here to ask the user for saving changes ... @todo?

    osl::ResettableGuard< osl::Mutex > aGuard( m_aControllerMutex );
    if( m_aLifeTimeManager.impl_isDisposed() )
        return sal_False; //behave passive if already disposed, return false because request was not accepted //@todo? correct

    if(bSuspend==m_bSuspended)
    {
        OSL_ENSURE( sal_False, "new suspend mode equals old suspend mode" );
        //@todo ??? or return true in this case?
        return sal_False;
    }

    //change suspend mode
    if(bSuspend)
    {
        //aGuard.clear();
        //@todo ???  try to stop all what may prevent me from becoming disposed
        //aGuard.reset();

        m_bSuspended = bSuspend;
        return sal_True;
    }
    else
    {
        //aGuard.clear();
        //@todo ??? redo what was made in section bSuspend==true
        //aGuard.reset();

        m_bSuspended = bSuspend;
        return sal_True;
    }


    /*
    if ( bSuspend )
        getFrame()->removeFrameActionListener( pImp );
    else
        getFrame()->addFrameActionListener( pImp );
        */
    return sal_True;
}

//-----------------------------------------------------------------
// XComponent (base of XController)
//-----------------------------------------------------------------

        void SAL_CALL ChartController
::dispose() throw(uno::RuntimeException)
{
    //This object should release all resources and references in the
    //easiest possible manner
    //This object must notify all registered listeners using the method
    //<member>XEventListener::disposing</member>

    //hold no mutex
    if( !m_aLifeTimeManager.dispose() )
        return;

    OSL_ENSURE( m_bSuspended, "dispose was called but controller is not suspended" );

    //--release all resources and references

    impl_deleteView();
    m_pChartWindow = NULL;//m_pChartWindow is deleted via UNO by Window hierarchy or Framework

    m_xFrame = NULL;
    TheModelRef aModelRef( m_aModel, m_aModelMutex);
    m_aModel = NULL;

    if(aModelRef.is())
    {
        aModelRef->removeListener( this );
        aModelRef->tryTermination();
    }

    //// @todo integrate specialized implementation
    //e.g. release further resources and references
 }

        void SAL_CALL ChartController
::addEventListener( const uno::Reference<lang::XEventListener>& xListener )
        throw(uno::RuntimeException)
{
    osl::Guard< osl::Mutex > aGuard( m_aControllerMutex );
    if( impl_isDisposedOrSuspended() )//@todo? allow adding of listeners in suspend mode?
        return; //behave passive if already disposed or suspended

    //--add listener
    m_aLifeTimeManager.m_aListenerContainer.addInterface( ::getCppuType((const uno::Reference< lang::XEventListener >*)0), xListener );
}

        void SAL_CALL ChartController
::removeEventListener( const uno::Reference<
        lang::XEventListener>& xListener )
        throw(uno::RuntimeException)
{
    osl::Guard< osl::Mutex > aGuard( m_aControllerMutex );
    if( impl_isDisposedOrSuspended() ) //@todo? allow removing of listeners in suspend mode?
        return; //behave passive if already disposed or suspended

    //--remove listener
    m_aLifeTimeManager.m_aListenerContainer.removeInterface( ::getCppuType((const uno::Reference< lang::XEventListener >*)0), xListener );
}


//-----------------------------------------------------------------
// util::XCloseListener
//-----------------------------------------------------------------
        void SAL_CALL ChartController
::queryClosing( const lang::EventObject& rSource, sal_Bool bGetsOwnership )
        throw(util::CloseVetoException, uno::RuntimeException)
{
    //do not use the m_aControllerMutex here because this call is not allowed to block

    TheModelRef aModelRef( m_aModel, m_aModelMutex);

    if( !aModelRef.is() )
        return;

    if( !(aModelRef->getModel() == rSource.Source) )
    {
        OSL_ENSURE( sal_False, "queryClosing was called on a controller from an unknown source" );
        return;
    }

    if( !m_bCanClose )//@todo tryaqcuire mutex
    {
        if( bGetsOwnership )
        {
            aModelRef->SetOwnerShip( bGetsOwnership );
        }

        throw util::CloseVetoException();
    }
    else
    {
        //@ todo prepare to to closing model -> don't start any further hindering actions
    }
}

        void SAL_CALL ChartController
::notifyClosing( const lang::EventObject& rSource )
        throw(uno::RuntimeException)
{
    //Listener should deregister himself and relaese all references to the closing object.

    TheModelRef aModelRef( 0, m_aModelMutex);
    bool bReleaseModel = sal_False;
    {
        ::osl::Guard< ::osl::Mutex > aGuard( m_aModelMutex );
        aModelRef = m_aModel;
        if( aModelRef.is() && aModelRef->getModel() == rSource.Source )
        {
            m_aModel = NULL;
            bReleaseModel = sal_True;
        }
    }
    //@todo ?? why remove the listener??? this could be handled by the closing object
    if( bReleaseModel )
    {
        //--stop listening to the closing model
        aModelRef->removeListener( this );
    }
}

//-----------------------------------------------------------------
// util::XEventListener (base of XCloseListener)
//-----------------------------------------------------------------
        void SAL_CALL ChartController
::disposing( const lang::EventObject& rSource )
        throw(uno::RuntimeException)
{
    notifyClosing(rSource);
}

//-----------------------------------------------------------------
// XDispatchProvider (required interface)
//-----------------------------------------------------------------

bool isFormatObjectSID( sal_Int32 nSlotID )
{
    if((sal_Int32)SID_DIAGRAM_TITLE_MAIN == nSlotID
    || (sal_Int32)SID_DIAGRAM_TITLE_SUB == nSlotID
    || (sal_Int32)SID_DIAGRAM_TITLE_X == nSlotID
    || (sal_Int32)SID_DIAGRAM_TITLE_Y == nSlotID
    || (sal_Int32)SID_DIAGRAM_TITLE_Z == nSlotID
    || (sal_Int32)SID_DIAGRAM_TITLE_ALL == nSlotID
    || (sal_Int32)SID_LEGEND == nSlotID
    || (sal_Int32)SID_DIAGRAM_AXIS_X == nSlotID
    || (sal_Int32)SID_DIAGRAM_AXIS_Y == nSlotID
    || (sal_Int32)SID_DIAGRAM_AXIS_Z == nSlotID
    || (sal_Int32)SID_DIAGRAM_AXIS_A == nSlotID //secondary x axis
    || (sal_Int32)SID_DIAGRAM_AXIS_B == nSlotID
    || (sal_Int32)SID_DIAGRAM_AXIS_ALL == nSlotID
    || (sal_Int32)SID_DIAGRAM_GRID_X_MAIN == nSlotID
    || (sal_Int32)SID_DIAGRAM_GRID_Y_MAIN == nSlotID
    || (sal_Int32)SID_DIAGRAM_GRID_Z_MAIN == nSlotID
    || (sal_Int32)SID_DIAGRAM_GRID_X_HELP == nSlotID
    || (sal_Int32)SID_DIAGRAM_GRID_Y_HELP == nSlotID
    || (sal_Int32)SID_DIAGRAM_GRID_Z_HELP == nSlotID
    || (sal_Int32)SID_DIAGRAM_GRID_ALL == nSlotID
    || (sal_Int32)SID_DIAGRAM_WALL == nSlotID
    || (sal_Int32)SID_DIAGRAM_FLOOR == nSlotID
    || (sal_Int32)SID_DIAGRAM_AREA == nSlotID
    )
        return true;
    return false;
}

        uno::Reference<frame::XDispatch> SAL_CALL ChartController
::queryDispatch( const util::URL& rURL
        , const rtl::OUString& rTargetFrameName
        , sal_Int32 nSearchFlags)
        throw(uno::RuntimeException)
{
    //// @todo integrate specialized implementation
    //decide which commands you can handle

    if ( !m_aLifeTimeManager.impl_isDisposed() )
    {
        //@todo avoid OString (see Mathias mail on bug #104387#)
        rtl::OString aCommand( rtl::OUStringToOString( rURL.Path, RTL_TEXTENCODING_ASCII_US ) );
        if( aCommand.equals("Save")
            || aCommand.equals("SaveAs")
            || aCommand.equals("SaveAll")

            || aCommand.equals("Undo")
            || aCommand.equals("Cut")
            || aCommand.equals("Copy")
            || aCommand.equals("Paste")
            || aCommand.equals("SelectAll")
            || aCommand.equals("Close")
            || aCommand.equals("TESTTEST")
            || aCommand.equals("slot:23")
            //|| aCommand.copy(0,4).EqualsAscii("Bib/")
            //|| aURL.Complete.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("slot:5503"))
            )
        {
            //@todo create a seperate dispatcher object or implement XDispatch interface by yourself
            //to return it here
            return static_cast< frame::XDispatch* >( this );
        }
        else if(rURL.Protocol.equalsIgnoreAsciiCase( C2U("slot:") ) )
        {
            sal_Int32 nSlotID = rURL.Path.toInt32();
            if( (sal_Int32)SID_DIAGRAM_OBJECTS == nSlotID
                || (sal_Int32)SID_DIAGRAM_TYPE == nSlotID
                || (sal_Int32)SID_INSERT_TITLE == nSlotID
                || (sal_Int32)SID_INSERT_CHART_LEGEND == nSlotID
                || (sal_Int32)SID_INSERT_DESCRIPTION == nSlotID
                || (sal_Int32)SID_INSERT_AXIS == nSlotID
                || (sal_Int32)SID_INSERT_GRIDS == nSlotID
                || (sal_Int32)SID_INSERT_STATISTICS == nSlotID
                || (sal_Int32)SID_CHARMAP == nSlotID
                || (sal_Int32)SID_TEXTEDIT == nSlotID
                || isFormatObjectSID(nSlotID)
                )
            {
                return static_cast< frame::XDispatch* >( this );
            }
        }
    }
    return uno::Reference< frame::XDispatch > ();
}
/*
typedef ::std::map< sal_Int32, ::rtl::OUString >    tSlotIdCommandMap;
typedef ::comphelper::MakeMap< sal_Int32, ::rtl::OUString > tMakeSlotIdCommandMap;
tMakeSlotIdCommandMap m_aSlotIdCommandMap =
    tMakeSlotIdCommandMap
    ( (sal_Int32)SID_DIAGRAM_TITLE_MAIN, C2U( "slot:" )+=rtl::OUString::valueOf( (sal_Int32)SID_DIAGRAM_TITLE_MAIN ) )
    ;
*/
        uno::Sequence<uno::Reference<frame::XDispatch > >   ChartController
::queryDispatches( const uno::Sequence<
        frame::DispatchDescriptor>& xDescripts)
        throw(uno::RuntimeException)
{
    sal_Int32 nCount = xDescripts.getLength();

    if( !nCount )
        return uno::Sequence<uno::Reference<frame::XDispatch > > ();

    uno::Sequence<uno::Reference<frame::XDispatch > > aRet( nCount );
    for( sal_Int32 nPos = 0; nPos<nCount; ++nPos )
    {
        aRet[ nPos ] = queryDispatch(
                              xDescripts[nPos].FeatureURL
                            , xDescripts[nPos].FrameName
                            , xDescripts[nPos].SearchFlags );
    }
    return aRet;
}

//-----------------------------------------------------------------
// frame::XDispatch
//-----------------------------------------------------------------

    void SAL_CALL ChartController
::dispatch( const util::URL& rURL
            , const uno::Sequence< beans::PropertyValue >& rArgs )
            throw (uno::RuntimeException)
{
    //@todo avoid OString (see Mathias mail on bug #104387#)
    rtl::OString aCommand( rtl::OUStringToOString( rURL.Path, RTL_TEXTENCODING_ASCII_US ) );
    //test only
    if(aCommand.equals("TESTTEST")
        || aCommand.equals("Undo") )
    {
        Window* pWindow = m_pChartWindow;
        Rectangle aRect( Point(0,0), pWindow->GetOutputSize() );
        Region aRegion( aRect );
        m_pDrawViewWrapper->InitRedraw(pWindow, aRegion );

        /*
        INVALIDATE_CHILDREN
        INVALIDATE_NOCHILDREN
        INVALIDATE_NOERASE
        INVALIDATE_UPDATE
        INVALIDATE_TRANSPARENT
        INVALIDATE_NOTRANSPARENT
        INVALIDATE_NOCLIPCHILDREN
        */
        //Invalidate(INVALIDATE_UPDATE);
    }

    else if(aCommand.equals("Save"))
    {
        //only test:
        impl_rebuildView();
    }
    else if(aCommand.equals("SaveAs"))
    {
        //only test:
        this->executeDispatch_ObjectToDefault();
    }
    //----------------------------------
    else if(rURL.Protocol.equalsIgnoreAsciiCase( C2U("slot:") ) )
    {
        sal_Int32 nSlotID = rURL.Path.toInt32();
        if((sal_Int32)SID_DIAGRAM_OBJECTS == nSlotID)
        {
            this->executeDispatch_ObjectProperties();
        }
        else if((sal_Int32)SID_DIAGRAM_TYPE == nSlotID)
        {
            this->executeDispatch_ChartType();
        }
        else if((sal_Int32)SID_INSERT_TITLE == nSlotID)
        {
            this->executeDispatch_InsertTitle();
        }
        else if((sal_Int32)SID_INSERT_CHART_LEGEND == nSlotID)
        {
            this->executeDispatch_InsertLegend();
        }
        else if((sal_Int32)SID_INSERT_DESCRIPTION == nSlotID)
        {
            this->executeDispatch_InsertDataLabel();
        }
        else if((sal_Int32)SID_INSERT_AXIS == nSlotID)
        {
            this->executeDispatch_InsertAxis();
        }
        else if((sal_Int32)SID_INSERT_GRIDS == nSlotID)
        {
            this->executeDispatch_InsertGrid();
        }
        else if((sal_Int32)SID_INSERT_STATISTICS == nSlotID)
        {
            this->executeDispatch_InsertStatistic();
        }
        else if((sal_Int32)SID_CHARMAP == nSlotID)
        {
            this->executeDispatch_InsertSpecialCharacter();
        }
        else if((sal_Int32)SID_TEXTEDIT == nSlotID)
        {
            this->executeDispatch_EditText();
        }
        else if( isFormatObjectSID(nSlotID) )
        {
            this->executeDispatch_FormatObject(nSlotID);
        }
    }
    else if(aCommand.equals("SaveAll"))
    {
        if( m_aModel.is())
        {
            uno::Reference< ::drafts::com::sun::star::chart2::XChartDocument > xDoc(
                m_aModel->getModel(), uno::UNO_QUERY );
            OSL_ASSERT( xDoc.is());

            uno::Reference< ::drafts::com::sun::star::chart2::XDataProvider > xDataProvider(
                m_xCC->getServiceManager()->createInstanceWithContext(
                    C2U( "com.sun.star.comp.chart.FileDataProvider" ),
                    m_xCC ), uno::UNO_QUERY );
            OSL_ASSERT( xDataProvider.is());

            ::rtl::OUString aFileName(
#if defined WNT
                RTL_CONSTASCII_USTRINGPARAM( "file:///D:/files/data.chd" )
#else
                RTL_CONSTASCII_USTRINGPARAM( "file:///work/data/data.chd" )
#endif
                );

            xDoc->attachDataProvider( xDataProvider );
            xDoc->setRangeRepresentation( aFileName );
            impl_rebuildView();
        }
    }
}

    void SAL_CALL ChartController
::addStatusListener( const uno::Reference<frame::XStatusListener >& xControl
            , const util::URL& aURL )
            throw (uno::RuntimeException)
{
}

    void SAL_CALL ChartController
::removeStatusListener( const uno::Reference<frame::XStatusListener >& xControl
            , const util::URL& aURL )
            throw (uno::RuntimeException)
{
}

//-----------------------------------------------------------------
// XContextMenuInterception (optional interface)
//-----------------------------------------------------------------
        void SAL_CALL ChartController
::registerContextMenuInterceptor( const uno::Reference<
        ui::XContextMenuInterceptor > & xInterceptor)
        throw(uno::RuntimeException)
{
    //@todo
}

        void SAL_CALL ChartController
::releaseContextMenuInterceptor( const uno::Reference<
        ui::XContextMenuInterceptor > & xInterceptor)
        throw(uno::RuntimeException)
{
    //@todo
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

void SAL_CALL ChartController::executeDispatch_ChartType()
{
    bool bChanged = false;

    //-------------------------------------------------------------
    //convert properties to ItemSet
    uno::Reference< beans::XPropertySet > xProp;
    uno::Reference< XChartDocument > xChartDoc( m_aModel->getModel(), uno::UNO_QUERY );
    if( xChartDoc.is())
        xProp.set( xChartDoc->getChartTypeManager(), uno::UNO_QUERY );
    DBG_ASSERT( xProp.is(), "Invalid ChartTypeManager" );

    {
        wrapper::ChartTypeItemConverter aItemConverter( xProp, m_pDrawModelWrapper->GetItemPool() );
        SfxItemSet aItemSet = aItemConverter.CreateEmptyItemSet();//creates only an empty itemset
        aItemConverter.FillItemSet( aItemSet );
        //-------------------------------------------------------------
        //prepare and open dialog
        Window* pParent( NULL );
        SchDiagramTypeDlg aDlg( pParent, aItemSet );
        if( aDlg.Execute() == RET_OK )
        {
            SfxItemSet aOutItemSet = aItemConverter.CreateEmptyItemSet();
            aDlg.GetAttr( aOutItemSet );

            bChanged = aItemConverter.ApplyItemSet( aOutItemSet );//model should be changed now
        }
    }

    try
    {
        //make sure that all objects using  m_pDrawModelWrapper or m_pChartView are already deleted
        if(bChanged &&
           xChartDoc.is())
        {
            uno::Reference< lang::XMultiServiceFactory > xFact( xProp, uno::UNO_QUERY );
            if( xFact.is() )
            {
                ::rtl::OUString aServiceName;
                if( xProp->getPropertyValue( C2U( "ChartStyleTemplateServiceName" )) >>= aServiceName )
                {
                    uno::Reference< XChartTypeTemplate > xTemplate(
                        xFact->createInstance( aServiceName ), uno::UNO_QUERY );
                    xChartDoc->setChartTypeTemplate( xTemplate );
                    if( xTemplate.is())
                        xTemplate->changeDiagram( xChartDoc->getDiagram() );
                    impl_rebuildView();
                }
            }
        }
    }
    catch( uno::RuntimeException& e)
    {
        ASSERT_EXCEPTION( e );
    }
}

//.............................................................................
} //namespace chart
//.............................................................................