1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tpusrlst.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
34 #undef SC_DLLIMPLEMENTATION
38 //------------------------------------------------------------------
40 #include <vcl/msgbox.hxx>
43 #include "document.hxx"
44 #include "tabvwsh.hxx"
45 #include "viewdata.hxx"
46 #include "uiitems.hxx"
47 #include "userlist.hxx"
48 #include "rangeutl.hxx"
50 #include "scresid.hxx"
51 #include "sc.hrc" // -> Slot-IDs
53 #include "globstr.hrc"
56 #include "tpusrlst.hxx"
59 //CHINA001 #include "scui_def.hxx" //CHINA001
60 //CHINA001 #include "scresid.hxx" //add by CHINA001
61 //CHINA001 #include "miscdlgs.hrc" //add by CHINA001
63 //CHINA001 #include "scabstdlg.hxx" //CHINA001
64 // STATIC DATA -----------------------------------------------------------
66 #define CR (sal_Unicode)13
67 #define LF (sal_Unicode)10
69 static USHORT pUserListsRanges
[] =
76 static const sal_Unicode cDelimiter
= ',';
79 //========================================================================
80 // Benutzerdefinierte Listen:
83 ScTpUserLists::ScTpUserLists( Window
* pParent
,
84 const SfxItemSet
& rCoreAttrs
)
86 : SfxTabPage ( pParent
,
87 ScResId( RID_SCPAGE_USERLISTS
),
89 aFtLists ( this, ScResId( FT_LISTS
) ),
90 aLbLists ( this, ScResId( LB_LISTS
) ),
91 aFtEntries ( this, ScResId( FT_ENTRIES
) ),
92 aEdEntries ( this, ScResId( ED_ENTRIES
) ),
93 aFtCopyFrom ( this, ScResId( FT_COPYFROM
) ),
94 aEdCopyFrom ( this, ScResId( ED_COPYFROM
) ),
95 aBtnNew ( this, ScResId( BTN_NEW
) ),
96 aBtnAdd ( this, ScResId( BTN_ADD
) ),
97 aBtnRemove ( this, ScResId( BTN_REMOVE
) ),
98 aBtnCopy ( this, ScResId( BTN_COPY
) ),
99 aStrQueryRemove ( ScResId( STR_QUERYREMOVE
) ),
100 aStrNew ( aBtnNew
.GetText() ),
101 aStrCancel ( ScResId( STR_DISMISS
) ),
102 aStrAdd ( ScResId( SCSTR_ADD
) ),
103 aStrModify ( ScResId( SCSTR_MODIFY
) ),
104 aStrCopyList ( ScResId( STR_COPYLIST
) ),
105 aStrCopyFrom ( ScResId( STR_COPYFROM
) ),
106 aStrCopyErr ( ScResId( STR_COPYERR
) ),
108 nWhichUserLists ( GetWhich( SID_SCUSERLISTS
) ),
112 pRangeUtil ( new ScRangeUtil
),
113 bModifyMode ( FALSE
),
114 bCancelMode ( FALSE
),
118 SetExchangeSupport();
123 // -----------------------------------------------------------------------
125 __EXPORT
ScTpUserLists::~ScTpUserLists()
131 // -----------------------------------------------------------------------
133 void ScTpUserLists::Init()
135 SfxViewShell
* pSh
= SfxViewShell::Current();
136 ScTabViewShell
* pViewSh
= PTR_CAST(ScTabViewShell
, pSh
);
138 aLbLists
.SetSelectHdl ( LINK( this, ScTpUserLists
, LbSelectHdl
) );
139 aBtnNew
.SetClickHdl ( LINK( this, ScTpUserLists
, BtnClickHdl
) );
140 aBtnNew
.SetClickHdl ( LINK( this, ScTpUserLists
, BtnClickHdl
) );
141 aBtnAdd
.SetClickHdl ( LINK( this, ScTpUserLists
, BtnClickHdl
) );
142 aBtnRemove
.SetClickHdl ( LINK( this, ScTpUserLists
, BtnClickHdl
) );
143 aEdEntries
.SetModifyHdl ( LINK( this, ScTpUserLists
, EdEntriesModHdl
) );
155 pViewData
= pViewSh
->GetViewData();
156 pDoc
= pViewData
->GetDocument();
158 pViewData
->GetSimpleArea( nStartCol
, nStartRow
, nStartTab
,
159 nEndCol
, nEndRow
, nEndTab
);
161 PutInOrder( nStartCol
, nEndCol
);
162 PutInOrder( nStartRow
, nEndRow
);
163 PutInOrder( nStartTab
, nEndTab
);
165 ScRange( nStartCol
, nStartRow
, nStartTab
, nEndCol
, nEndRow
, nEndTab
166 ).Format( aStrSelectedArea
, SCR_ABS_3D
, pDoc
);
168 aBtnCopy
.SetClickHdl ( LINK( this, ScTpUserLists
, BtnClickHdl
) );
174 aFtCopyFrom
.Disable();
175 aEdCopyFrom
.Disable();
178 // aLbLists.GrabFocus();
181 // -----------------------------------------------------------------------
183 USHORT
* __EXPORT
ScTpUserLists::GetRanges()
185 return pUserListsRanges
;
188 // -----------------------------------------------------------------------
190 SfxTabPage
* __EXPORT
ScTpUserLists::Create( Window
* pParent
, const SfxItemSet
& rAttrSet
)
192 return ( new ScTpUserLists( pParent
, rAttrSet
) );
195 // -----------------------------------------------------------------------
197 void __EXPORT
ScTpUserLists::Reset( const SfxItemSet
& rCoreAttrs
)
199 const ScUserListItem
& rUserListItem
= (const ScUserListItem
&)
200 rCoreAttrs
.Get( nWhichUserLists
);
201 const ScUserList
* pCoreList
= rUserListItem
.GetUserList();
203 DBG_ASSERT( pCoreList
, "UserList not found :-/" );
208 pUserLists
= new ScUserList( *pCoreList
);
210 *pUserLists
= *pCoreList
;
212 if ( UpdateUserListBox() > 0 )
214 aLbLists
.SelectEntryPos( 0 );
218 else if ( !pUserLists
)
219 pUserLists
= new ScUserList
;
221 aEdCopyFrom
.SetText( aStrSelectedArea
);
223 if ( aLbLists
.GetEntryCount() == 0 )
227 aFtEntries
.Disable();
228 aEdEntries
.Disable();
229 aBtnRemove
.Disable();
232 aBtnNew
.SetText( aStrNew
);
233 aBtnAdd
.SetText( aStrAdd
);
236 if ( !bCopyDone
&& pViewData
)
238 aFtCopyFrom
.Enable();
239 aEdCopyFrom
.Enable();
243 // aLbLists.GrabFocus();
246 // -----------------------------------------------------------------------
248 BOOL __EXPORT
ScTpUserLists::FillItemSet( SfxItemSet
& rCoreAttrs
)
250 // Modifikationen noch nicht uebernommen?
251 // -> Click auf Add-Button simulieren
253 if ( bModifyMode
|| bCancelMode
)
254 BtnClickHdl( &aBtnAdd
);
256 const ScUserListItem
& rUserListItem
= (const ScUserListItem
&)
257 GetItemSet().Get( nWhichUserLists
);
259 ScUserList
* pCoreList
= rUserListItem
.GetUserList();
260 BOOL bDataModified
= FALSE
;
262 if ( (pUserLists
== NULL
) && (pCoreList
== NULL
) )
264 bDataModified
= FALSE
;
266 else if ( pUserLists
!= NULL
)
268 if ( pCoreList
!= NULL
)
269 bDataModified
= (*pUserLists
!= *pCoreList
);
271 bDataModified
= TRUE
;
276 ScUserListItem
aULItem( nWhichUserLists
);
279 aULItem
.SetUserList( *pUserLists
);
281 rCoreAttrs
.Put( aULItem
);
284 return bDataModified
;
287 // -----------------------------------------------------------------------
289 int __EXPORT
ScTpUserLists::DeactivatePage( SfxItemSet
* pSetP
)
292 FillItemSet( *pSetP
);
297 // -----------------------------------------------------------------------
299 USHORT
ScTpUserLists::UpdateUserListBox()
303 if ( !pUserLists
) return 0;
305 //----------------------------------------------------------
307 USHORT nCount
= pUserLists
->GetCount();
312 for ( USHORT i
=0; i
<nCount
; i
++ )
314 aEntry
= (*pUserLists
)[i
]->GetString();
315 DBG_ASSERT( aEntry
.Len() > 0, "Empty UserList-entry :-/" );
316 aLbLists
.InsertEntry( aEntry
);
323 // -----------------------------------------------------------------------
325 void ScTpUserLists::UpdateEntries( USHORT nList
)
327 if ( !pUserLists
) return;
329 //----------------------------------------------------------
331 if ( nList
< pUserLists
->GetCount() )
333 ScUserListData
* pList
= (*pUserLists
)[nList
];
334 USHORT nSubCount
= pList
->GetSubCount();
335 String aEntryListStr
;
337 for ( USHORT i
=0; i
<nSubCount
; i
++ )
341 aEntryListStr
+= pList
->GetSubStr( i
);
344 aEntryListStr
.ConvertLineEnd();
345 aEdEntries
.SetText( aEntryListStr
);
349 DBG_ERROR( "Invalid ListIndex :-/" );
353 // -----------------------------------------------------------------------
355 void ScTpUserLists::MakeListStr( String
& rListStr
)
357 String
aInputStr(rListStr
);
360 xub_StrLen nLen
= aStr
.Len();
363 aInputStr
.ConvertLineEnd( LINEEND_LF
);
364 //aStr.EraseAllChars( ' ' );
366 xub_StrLen nToken
=rListStr
.GetTokenCount(LF
);
368 for(xub_StrLen i
=0;i
<nToken
;i
++)
370 String aString
=rListStr
.GetToken(i
,LF
);
371 aString
.EraseLeadingChars(' ');
372 aString
.EraseTrailingChars(' ');
378 // '\n' durch cDelimiter ersetzen:
380 (c < nLen) && (nFound != STRING_NOTFOUND);
383 nFound = aStr.Search( LF, nFound );
384 if ( nFound != STRING_NOTFOUND )
385 aStr[nFound] = cDelimiter;
389 aStr
.EraseLeadingChars( cDelimiter
);
390 aStr
.EraseTrailingChars( cDelimiter
);
395 // Alle Doppelten cDelimiter entfernen:
399 rListStr
+= aStr
.GetChar(c
);
402 if ( aStr
.GetChar(c
) == cDelimiter
)
404 rListStr
+= aStr
.GetChar(c
);
406 while ( (aStr
.GetChar(c
) == cDelimiter
) && (c
< nLen
) )
413 // -----------------------------------------------------------------------
415 void ScTpUserLists::AddNewList( const String
& rEntriesStr
)
417 String
theEntriesStr( rEntriesStr
);
420 pUserLists
= new ScUserList
;
422 MakeListStr( theEntriesStr
);
424 if ( !pUserLists
->Insert( new ScUserListData( theEntriesStr
) ) )
426 DBG_ERROR( "Entry could not be inserted :-/" );
430 // -----------------------------------------------------------------------
432 void ScTpUserLists::CopyListFromArea( const ScRefAddress
& rStartPos
,
433 const ScRefAddress
& rEndPos
)
435 if ( bCopyDone
) return;
437 //----------------------------------------------------------
439 SCTAB nTab
= rStartPos
.Tab();
440 SCCOL nStartCol
= rStartPos
.Col();
441 SCROW nStartRow
= rStartPos
.Row();
442 SCCOL nEndCol
= rEndPos
.Col();
443 SCROW nEndRow
= rEndPos
.Row();
444 USHORT nCellDir
= SCRET_COLS
;
445 BOOL bValueIgnored
= FALSE
;
447 if ( (nStartCol
!= nEndCol
) && (nStartRow
!= nEndRow
) )
449 nCellDir
= ScColOrRowDlg( this, aStrCopyList
, aStrCopyFrom
).Execute();
450 //CHINA001 ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
451 //CHINA001 DBG_ASSERT(pFact, "ScAbstractFactory create fail!");//CHINA001
453 //CHINA001 VclAbstractDialog* pDlg = pFact->CreateScColOrRowDlg( this, aStrCopyList, aStrCopyFrom,ResId(RID_SCDLG_COLORROW) );
454 //CHINA001 DBG_ASSERT(pDlg, "Dialog create fail!");//CHINA001
455 //CHINA001 nCellDir = pDlg->Execute();
456 //CHINA001 delete pDlg; //CHINA001
458 else if ( nStartCol
!= nEndCol
)
459 nCellDir
= SCRET_ROWS
;
461 nCellDir
= SCRET_COLS
;
463 if ( nCellDir
!= RET_CANCEL
)
468 if ( nCellDir
== SCRET_COLS
)
470 for ( SCCOL col
=nStartCol
; col
<=nEndCol
; col
++ )
472 for ( SCROW row
=nStartRow
; row
<=nEndRow
; row
++ )
474 if ( pDoc
->HasStringData( col
, row
, nTab
) )
476 pDoc
->GetString( col
, row
, nTab
, aStrField
);
478 if ( aStrField
.Len() > 0 )
480 aStrList
+= aStrField
;
485 bValueIgnored
= TRUE
;
487 if ( aStrList
.Len() > 0 )
488 AddNewList( aStrList
);
494 for ( SCROW row
=nStartRow
; row
<=nEndRow
; row
++ )
496 for ( SCCOL col
=nStartCol
; col
<=nEndCol
; col
++ )
498 if ( pDoc
->HasStringData( col
, row
, nTab
) )
500 pDoc
->GetString( col
, row
, nTab
, aStrField
);
502 if ( aStrField
.Len() > 0 )
504 aStrList
+= aStrField
;
509 bValueIgnored
= TRUE
;
511 if ( aStrList
.Len() > 0 )
512 AddNewList( aStrList
);
519 InfoBox( this, aStrCopyErr
).Execute();
523 //----------------------------------------------------------
529 // -----------------------------------------------------------------------
531 void ScTpUserLists::ModifyList( USHORT nSelList
,
532 const String
& rEntriesStr
)
534 if ( !pUserLists
) return;
536 //----------------------------------------------------------
538 String
theEntriesStr( rEntriesStr
);
540 MakeListStr( theEntriesStr
);
542 (*pUserLists
)[nSelList
]->SetString( theEntriesStr
);
545 // -----------------------------------------------------------------------
547 void ScTpUserLists::RemoveList( USHORT nList
)
549 if ( pUserLists
) pUserLists
->AtFree( nList
);
552 //-----------------------------------------------------------------------
556 IMPL_LINK( ScTpUserLists
, LbSelectHdl
, ListBox
*, pLb
)
558 if ( pLb
== &aLbLists
)
560 USHORT nSelPos
= aLbLists
.GetSelectEntryPos();
561 if ( nSelPos
!= LISTBOX_ENTRY_NOTFOUND
)
563 if ( !aFtEntries
.IsEnabled() ) aFtEntries
.Enable();
564 if ( !aEdEntries
.IsEnabled() ) aEdEntries
.Enable();
565 if ( !aBtnRemove
.IsEnabled() ) aBtnRemove
.Enable();
566 if ( aBtnAdd
.IsEnabled() ) aBtnAdd
.Disable();
568 UpdateEntries( nSelPos
);
575 // -----------------------------------------------------------------------
577 IMPL_LINK( ScTpUserLists
, BtnClickHdl
, PushButton
*, pBtn
)
579 if ( pBtn
== &aBtnNew
)
583 nCancelPos
= ( aLbLists
.GetEntryCount() > 0 )
584 ? aLbLists
.GetSelectEntryPos()
586 aLbLists
.SetNoSelection();
591 aEdEntries
.SetText( EMPTY_STRING
);
592 aEdEntries
.GrabFocus();
594 aBtnRemove
.Disable();
595 //-----------------------------
596 if ( aBtnCopy
.IsEnabled() )
599 aFtCopyFrom
.Disable();
600 aEdCopyFrom
.Disable();
602 aBtnNew
.SetText( aStrCancel
);
605 else // if ( bCancelMode )
607 if ( aLbLists
.GetEntryCount() > 0 )
609 aLbLists
.SelectEntryPos( nCancelPos
);
610 LbSelectHdl( &aLbLists
);
616 aFtEntries
.Disable();
617 aEdEntries
.Disable();
618 aEdEntries
.SetText( EMPTY_STRING
);
619 aBtnRemove
.Disable();
622 //-----------------------------
623 if ( pViewData
&& !bCopyDone
)
626 aFtCopyFrom
.Enable();
627 aEdCopyFrom
.Enable();
629 aBtnNew
.SetText( aStrNew
);
634 else if ( pBtn
== &aBtnAdd
)
636 String
theEntriesStr( aEdEntries
.GetText() );
640 if ( theEntriesStr
.Len() > 0 )
642 AddNewList( theEntriesStr
);
644 aLbLists
.SelectEntryPos( aLbLists
.GetEntryCount()-1 );
645 LbSelectHdl( &aLbLists
);
651 if ( aLbLists
.GetEntryCount() > 0 )
653 aLbLists
.SelectEntryPos( nCancelPos
);
654 LbSelectHdl( &aLbLists
);
662 aBtnNew
.SetText( aStrNew
);
665 else // if ( bModifyMode )
667 USHORT nSelList
= aLbLists
.GetSelectEntryPos();
669 DBG_ASSERT( nSelList
!= LISTBOX_ENTRY_NOTFOUND
, "Modify without List :-/" );
671 if ( theEntriesStr
.Len() > 0 )
673 ModifyList( nSelList
, theEntriesStr
);
675 aLbLists
.SelectEntryPos( nSelList
);
679 aLbLists
.SelectEntryPos( 0 );
680 LbSelectHdl( &aLbLists
);
683 aBtnNew
.SetText( aStrNew
); bCancelMode
= FALSE
;
684 aBtnAdd
.SetText( aStrAdd
); bModifyMode
= FALSE
;
691 if ( pViewData
&& !bCopyDone
)
694 aFtCopyFrom
.Enable();
695 aEdCopyFrom
.Enable();
698 else if ( pBtn
== &aBtnRemove
)
700 if ( aLbLists
.GetEntryCount() > 0 )
702 USHORT nRemovePos
= aLbLists
.GetSelectEntryPos();
703 String
aMsg ( aStrQueryRemove
.GetToken( 0, '#' ) );
705 aMsg
+= aLbLists
.GetEntry( nRemovePos
);
706 aMsg
+= aStrQueryRemove
.GetToken( 1, '#' );
709 if ( RET_YES
== QueryBox( this,
710 WinBits( WB_YES_NO
| WB_DEF_YES
),
714 RemoveList( nRemovePos
);
717 if ( aLbLists
.GetEntryCount() > 0 )
719 aLbLists
.SelectEntryPos(
720 ( nRemovePos
>= aLbLists
.GetEntryCount() )
721 ? aLbLists
.GetEntryCount()-1
723 LbSelectHdl( &aLbLists
);
729 aFtEntries
.Disable();
730 aEdEntries
.Disable();
731 aEdEntries
.SetText( EMPTY_STRING
);
732 aBtnRemove
.Disable();
736 if ( pViewData
&& !bCopyDone
&& !aBtnCopy
.IsEnabled() )
739 aFtCopyFrom
.Enable();
740 aEdCopyFrom
.Enable();
744 else if ( pViewData
&& (pBtn
== &aBtnCopy
) )
749 //-----------------------------------------------------------
751 ScRefAddress theStartPos
;
752 ScRefAddress theEndPos
;
753 String
theAreaStr( aEdCopyFrom
.GetText() );
754 BOOL bAreaOk
= FALSE
;
756 if ( theAreaStr
.Len() > 0 )
758 bAreaOk
= pRangeUtil
->IsAbsArea( theAreaStr
,
760 pViewData
->GetTabNo(),
764 pDoc
->GetAddressConvention() );
767 bAreaOk
= pRangeUtil
->IsAbsPos( theAreaStr
,
769 pViewData
->GetTabNo(),
772 pDoc
->GetAddressConvention() );
773 theEndPos
= theStartPos
;
779 CopyListFromArea( theStartPos
, theEndPos
);
781 aLbLists
.SelectEntryPos( aLbLists
.GetEntryCount()-1 );
782 LbSelectHdl( &aLbLists
);
783 aEdCopyFrom
.SetText( theAreaStr
);
784 aEdCopyFrom
.Disable();
786 aFtCopyFrom
.Disable();
790 ErrorBox( this, WinBits( WB_OK
| WB_DEF_OK
),
791 ScGlobal::GetRscString( STR_INVALID_TABREF
)
793 aEdCopyFrom
.GrabFocus();
794 aEdCopyFrom
.SetSelection( Selection( 0, SELECTION_MAX
) );
801 // -----------------------------------------------------------------------
803 IMPL_LINK( ScTpUserLists
, EdEntriesModHdl
, MultiLineEdit
*, pEd
)
805 if ( pEd
!= &aEdEntries
)
808 //-----------------------------------------------------------
810 if ( aBtnCopy
.IsEnabled() )
813 aFtCopyFrom
.Disable();
814 aEdCopyFrom
.Disable();
817 if ( aEdEntries
.GetText().Len() > 0 )
819 if ( !bCancelMode
&& !bModifyMode
)
821 aBtnNew
.SetText( aStrCancel
); bCancelMode
= TRUE
;
822 aBtnAdd
.SetText( aStrModify
); bModifyMode
= TRUE
;
824 aBtnRemove
.Disable();
828 else // if ( bCancelMode || bModifyMode )
830 if ( !aBtnAdd
.IsEnabled() ) aBtnAdd
.Enable();
835 if ( aBtnAdd
.IsEnabled() ) aBtnAdd
.Disable();