xref: /AOO41X/main/sw/source/ui/docvw/AnnotationWin.cxx (revision efeef26f81c84063fb0a91bde3856d4a51172d90)
1*efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*efeef26fSAndrew Rist  * distributed with this work for additional information
6*efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9*efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15*efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*efeef26fSAndrew Rist  * specific language governing permissions and limitations
18*efeef26fSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*efeef26fSAndrew Rist  *************************************************************/
21*efeef26fSAndrew Rist 
22*efeef26fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_sw.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <AnnotationWin.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <AnnotationMenuButton.hxx>
29cdf0e10cSrcweir #include <PostItMgr.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <annotation.hrc>
32cdf0e10cSrcweir #include <popup.hrc>
33cdf0e10cSrcweir #include <cmdid.h>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <vcl/menu.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <svl/undo.hxx>
38cdf0e10cSrcweir #include <unotools/syslocale.hxx>
39cdf0e10cSrcweir #include <svl/languageoptions.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <editeng/postitem.hxx>
42cdf0e10cSrcweir #include <editeng/fhgtitem.hxx>
43cdf0e10cSrcweir #include <editeng/langitem.hxx>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #include <editeng/editview.hxx>
46cdf0e10cSrcweir #include <editeng/outliner.hxx>
47cdf0e10cSrcweir #include <editeng/editeng.hxx>
48cdf0e10cSrcweir #include <editeng/editobj.hxx>
49cdf0e10cSrcweir 
50cdf0e10cSrcweir #include <docufld.hxx> // SwPostItField
51cdf0e10cSrcweir #include <txtfld.hxx>
52cdf0e10cSrcweir #include <ndtxt.hxx>
53cdf0e10cSrcweir #include <view.hxx>
54cdf0e10cSrcweir #include <wrtsh.hxx>
55cdf0e10cSrcweir #include <docsh.hxx>
56cdf0e10cSrcweir #include <doc.hxx>
57cdf0e10cSrcweir #include <IDocumentUndoRedo.hxx>
58cdf0e10cSrcweir #include <SwUndoField.hxx>
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 
61cdf0e10cSrcweir namespace sw { namespace annotation {
62cdf0e10cSrcweir 
63cdf0e10cSrcweir SwAnnotationWin::SwAnnotationWin( SwEditWin& rEditWin,
64cdf0e10cSrcweir                                   WinBits nBits,
65cdf0e10cSrcweir                                   SwPostItMgr& aMgr,
66cdf0e10cSrcweir                                   SwPostItBits aBits,
67cdf0e10cSrcweir                                   SwSidebarItem& rSidebarItem,
68cdf0e10cSrcweir                                   SwFmtFld* aField )
69cdf0e10cSrcweir     : SwSidebarWin( rEditWin, nBits, aMgr, aBits, rSidebarItem )
70cdf0e10cSrcweir     , mpFmtFld(aField)
71cdf0e10cSrcweir     , mpFld( static_cast<SwPostItField*>(aField->GetFld()))
72cdf0e10cSrcweir     , mpButtonPopup(0)
73cdf0e10cSrcweir {
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir SwAnnotationWin::~SwAnnotationWin()
77cdf0e10cSrcweir {
78cdf0e10cSrcweir     delete mpButtonPopup;
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir void SwAnnotationWin::SetPostItText()
82cdf0e10cSrcweir {
83cdf0e10cSrcweir     // get text from SwPostItField and insert into our textview
84cdf0e10cSrcweir     Engine()->SetModifyHdl( Link() );
85cdf0e10cSrcweir     Engine()->EnableUndo( sal_False );
86cdf0e10cSrcweir     mpFld = static_cast<SwPostItField*>(mpFmtFld->GetFld());
87cdf0e10cSrcweir     if( mpFld->GetTextObject() )
88cdf0e10cSrcweir         Engine()->SetText( *mpFld->GetTextObject() );
89cdf0e10cSrcweir     else
90cdf0e10cSrcweir     {
91cdf0e10cSrcweir         Engine()->Clear();
92cdf0e10cSrcweir         GetOutlinerView()->SetAttribs(DefaultItem());
93cdf0e10cSrcweir         GetOutlinerView()->InsertText(mpFld->GetPar2(),false);
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     Engine()->ClearModifyFlag();
97cdf0e10cSrcweir     Engine()->GetUndoManager().Clear();
98cdf0e10cSrcweir     Engine()->EnableUndo( sal_True );
99cdf0e10cSrcweir     Engine()->SetModifyHdl( LINK( this, SwAnnotationWin, ModifyHdl ) );
100cdf0e10cSrcweir     Invalidate();
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir void SwAnnotationWin::UpdateData()
104cdf0e10cSrcweir {
105cdf0e10cSrcweir     if ( Engine()->IsModified() )
106cdf0e10cSrcweir     {
107cdf0e10cSrcweir         IDocumentUndoRedo & rUndoRedo(
108cdf0e10cSrcweir             DocView().GetDocShell()->GetDoc()->GetIDocumentUndoRedo());
109cdf0e10cSrcweir         ::std::auto_ptr<SwField> pOldField;
110cdf0e10cSrcweir         if (rUndoRedo.DoesUndo())
111cdf0e10cSrcweir         {
112cdf0e10cSrcweir             pOldField.reset(mpFld->Copy());
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir         mpFld->SetPar2(Engine()->GetEditEngine().GetText());
115cdf0e10cSrcweir         mpFld->SetTextObject(Engine()->CreateParaObject());
116cdf0e10cSrcweir         if (rUndoRedo.DoesUndo())
117cdf0e10cSrcweir         {
118cdf0e10cSrcweir             SwTxtFld *const pTxtFld = mpFmtFld->GetTxtFld();
119cdf0e10cSrcweir             SwPosition aPosition( pTxtFld->GetTxtNode() );
120cdf0e10cSrcweir             aPosition.nContent = *pTxtFld->GetStart();
121cdf0e10cSrcweir             rUndoRedo.AppendUndo(
122cdf0e10cSrcweir                 new SwUndoFieldFromDoc(aPosition, *pOldField, *mpFld, 0, true));
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir         // so we get a new layout of notes (anchor position is still the same and we would otherwise not get one)
125cdf0e10cSrcweir         Mgr().SetLayout();
126cdf0e10cSrcweir         // #i98686# if we have several views, all notes should update their text
127cdf0e10cSrcweir         mpFmtFld->Broadcast(SwFmtFldHint( 0, SWFMTFLD_CHANGED));
128cdf0e10cSrcweir         DocView().GetDocShell()->SetModified();
129cdf0e10cSrcweir     }
130cdf0e10cSrcweir     Engine()->ClearModifyFlag();
131cdf0e10cSrcweir     Engine()->GetUndoManager().Clear();
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir void SwAnnotationWin::Delete()
135cdf0e10cSrcweir {
136cdf0e10cSrcweir     SwSidebarWin::Delete();
137cdf0e10cSrcweir     // we delete the field directly, the Mgr cleans up the PostIt by listening
138cdf0e10cSrcweir     DocView().GetWrtShellPtr()->GotoField(*mpFmtFld);
139cdf0e10cSrcweir     GrabFocusToDocument();
140cdf0e10cSrcweir     DocView().GetWrtShellPtr()->DelRight();
141cdf0e10cSrcweir }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir void SwAnnotationWin::GotoPos()
144cdf0e10cSrcweir {
145cdf0e10cSrcweir     DocView().GetDocShell()->GetWrtShell()->GotoField(*mpFmtFld);
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir sal_uInt32 SwAnnotationWin::MoveCaret()
149cdf0e10cSrcweir {
150cdf0e10cSrcweir     // if this is an answer, do not skip over all following ones, but insert directly behind the current one
151cdf0e10cSrcweir     // but when just leaving a note, skip all following ones as well to continue typing
152cdf0e10cSrcweir     return Mgr().IsAnswer()
153cdf0e10cSrcweir            ? 1
154cdf0e10cSrcweir            : 1 + CountFollowing();
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir //returns true, if there is another note right before this note
158cdf0e10cSrcweir bool SwAnnotationWin::CalcFollow()
159cdf0e10cSrcweir {
160cdf0e10cSrcweir     SwTxtFld* pTxtFld = mpFmtFld->GetTxtFld();
161cdf0e10cSrcweir     SwPosition aPosition( pTxtFld->GetTxtNode() );
162cdf0e10cSrcweir     aPosition.nContent = *pTxtFld->GetStart();
163cdf0e10cSrcweir     SwTxtAttr * const pTxtAttr = pTxtFld->GetTxtNode().GetTxtAttrForCharAt(
164cdf0e10cSrcweir                     aPosition.nContent.GetIndex() - 1, RES_TXTATR_FIELD );
165cdf0e10cSrcweir     const SwField* pFld = pTxtAttr ? pTxtAttr->GetFld().GetFld() : 0;
166cdf0e10cSrcweir     return pFld && (pFld->Which()== RES_POSTITFLD);
167cdf0e10cSrcweir }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir // counts how many SwPostItField we have right after the current one
170cdf0e10cSrcweir sal_uInt32 SwAnnotationWin::CountFollowing()
171cdf0e10cSrcweir {
172cdf0e10cSrcweir     sal_uInt32 aCount = 1;  // we start with 1, so we have to subtract one at the end again
173cdf0e10cSrcweir     SwTxtFld* pTxtFld = mpFmtFld->GetTxtFld();
174cdf0e10cSrcweir     SwPosition aPosition( pTxtFld->GetTxtNode() );
175cdf0e10cSrcweir     aPosition.nContent = *pTxtFld->GetStart();
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     SwTxtAttr * pTxtAttr = pTxtFld->GetTxtNode().GetTxtAttrForCharAt(
178cdf0e10cSrcweir                                         aPosition.nContent.GetIndex() + 1,
179cdf0e10cSrcweir                                         RES_TXTATR_FIELD );
180cdf0e10cSrcweir     SwField* pFld = pTxtAttr
181cdf0e10cSrcweir                     ? const_cast<SwField*>(pTxtAttr->GetFld().GetFld())
182cdf0e10cSrcweir                     : 0;
183cdf0e10cSrcweir     while ( pFld && ( pFld->Which()== RES_POSTITFLD ) )
184cdf0e10cSrcweir     {
185cdf0e10cSrcweir         aCount++;
186cdf0e10cSrcweir         pTxtAttr = pTxtFld->GetTxtNode().GetTxtAttrForCharAt(
187cdf0e10cSrcweir                                         aPosition.nContent.GetIndex() + aCount,
188cdf0e10cSrcweir                                         RES_TXTATR_FIELD );
189cdf0e10cSrcweir         pFld = pTxtAttr
190cdf0e10cSrcweir                ? const_cast<SwField*>(pTxtAttr->GetFld().GetFld())
191cdf0e10cSrcweir                : 0;
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir     return aCount - 1;
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir MenuButton* SwAnnotationWin::CreateMenuButton()
197cdf0e10cSrcweir {
198cdf0e10cSrcweir     mpButtonPopup = new PopupMenu(SW_RES(MN_ANNOTATION_BUTTON));
199cdf0e10cSrcweir     XubString aText = mpButtonPopup->GetItemText( FN_DELETE_NOTE_AUTHOR );
200cdf0e10cSrcweir     SwRewriter aRewriter;
201cdf0e10cSrcweir     aRewriter.AddRule(UNDO_ARG1,GetAuthor());
202cdf0e10cSrcweir     aText = aRewriter.Apply(aText);
203cdf0e10cSrcweir     mpButtonPopup->SetItemText(FN_DELETE_NOTE_AUTHOR,aText);
204cdf0e10cSrcweir     MenuButton* pMenuButton = new AnnotationMenuButton( *this );
205cdf0e10cSrcweir     pMenuButton->SetPopupMenu( mpButtonPopup );
206cdf0e10cSrcweir     pMenuButton->Show();
207cdf0e10cSrcweir     return pMenuButton;
208cdf0e10cSrcweir }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir void SwAnnotationWin::InitAnswer(OutlinerParaObject* pText)
211cdf0e10cSrcweir {
212cdf0e10cSrcweir     //collect our old meta data
213cdf0e10cSrcweir     SwSidebarWin* pWin = Mgr().GetNextPostIt(KEY_PAGEUP, this);
214cdf0e10cSrcweir     const SvtSysLocale aSysLocale;
215cdf0e10cSrcweir     const LocaleDataWrapper& rLocalData = aSysLocale.GetLocaleData();
216cdf0e10cSrcweir     String aText = String(SW_RES(STR_REPLY));
217cdf0e10cSrcweir         SwRewriter aRewriter;
218cdf0e10cSrcweir         aRewriter.AddRule(UNDO_ARG1, pWin->GetAuthor());
219cdf0e10cSrcweir         aText = aRewriter.Apply(aText);
220cdf0e10cSrcweir         aText.Append(String(rtl::OUString::createFromAscii(" (") +
221cdf0e10cSrcweir         String(rLocalData.getDate( pWin->GetDate())) + rtl::OUString::createFromAscii(", ") +
222cdf0e10cSrcweir         String(rLocalData.getTime( pWin->GetTime(),false)) + rtl::OUString::createFromAscii("): \"")));
223cdf0e10cSrcweir     GetOutlinerView()->InsertText(aText,false);
224cdf0e10cSrcweir 
225cdf0e10cSrcweir     // insert old, selected text or "..."
226cdf0e10cSrcweir     // TOOD: iterate over all paragraphs, not only first one to find out if it is empty
227cdf0e10cSrcweir     if (pText->GetTextObject().GetText(0) != String(rtl::OUString::createFromAscii("")))
228cdf0e10cSrcweir         GetOutlinerView()->GetEditView().InsertText(pText->GetTextObject());
229cdf0e10cSrcweir     else
230cdf0e10cSrcweir         GetOutlinerView()->InsertText(rtl::OUString::createFromAscii("..."),false);
231cdf0e10cSrcweir     GetOutlinerView()->InsertText(rtl::OUString::createFromAscii("\"\n"),false);
232cdf0e10cSrcweir 
233cdf0e10cSrcweir     GetOutlinerView()->SetSelection(ESelection(0x0,0x0,0xFFFF,0xFFFF));
234cdf0e10cSrcweir     SfxItemSet aAnswerSet( DocView().GetDocShell()->GetPool() );
235cdf0e10cSrcweir     aAnswerSet.Put(SvxFontHeightItem(200,80,EE_CHAR_FONTHEIGHT));
236cdf0e10cSrcweir     aAnswerSet.Put(SvxPostureItem(ITALIC_NORMAL,EE_CHAR_ITALIC));
237cdf0e10cSrcweir     GetOutlinerView()->SetAttribs(aAnswerSet);
238cdf0e10cSrcweir     GetOutlinerView()->SetSelection(ESelection(0xFFFF,0xFFFF,0xFFFF,0xFFFF));
239cdf0e10cSrcweir 
240cdf0e10cSrcweir     //remove all attributes and reset our standard ones
241cdf0e10cSrcweir     GetOutlinerView()->GetEditView().RemoveAttribsKeepLanguages(true);
242cdf0e10cSrcweir     GetOutlinerView()->SetAttribs(DefaultItem());
243cdf0e10cSrcweir     // lets insert an undo step so the initial text can be easily deleted
244cdf0e10cSrcweir     // but do not use UpdateData() directly, would set modified state again and reentrance into Mgr
245cdf0e10cSrcweir     Engine()->SetModifyHdl( Link() );
246cdf0e10cSrcweir     IDocumentUndoRedo & rUndoRedo(
247cdf0e10cSrcweir         DocView().GetDocShell()->GetDoc()->GetIDocumentUndoRedo());
248cdf0e10cSrcweir     ::std::auto_ptr<SwField> pOldField;
249cdf0e10cSrcweir     if (rUndoRedo.DoesUndo())
250cdf0e10cSrcweir     {
251cdf0e10cSrcweir         pOldField.reset(mpFld->Copy());
252cdf0e10cSrcweir     }
253cdf0e10cSrcweir     mpFld->SetPar2(Engine()->GetEditEngine().GetText());
254cdf0e10cSrcweir     mpFld->SetTextObject(Engine()->CreateParaObject());
255cdf0e10cSrcweir     if (rUndoRedo.DoesUndo())
256cdf0e10cSrcweir     {
257cdf0e10cSrcweir         SwTxtFld *const pTxtFld = mpFmtFld->GetTxtFld();
258cdf0e10cSrcweir         SwPosition aPosition( pTxtFld->GetTxtNode() );
259cdf0e10cSrcweir         aPosition.nContent = *pTxtFld->GetStart();
260cdf0e10cSrcweir         rUndoRedo.AppendUndo(
261cdf0e10cSrcweir             new SwUndoFieldFromDoc(aPosition, *pOldField, *mpFld, 0, true));
262cdf0e10cSrcweir     }
263cdf0e10cSrcweir     Engine()->SetModifyHdl( LINK( this, SwAnnotationWin, ModifyHdl ) );
264cdf0e10cSrcweir     Engine()->ClearModifyFlag();
265cdf0e10cSrcweir     Engine()->GetUndoManager().Clear();
266cdf0e10cSrcweir }
267cdf0e10cSrcweir 
268cdf0e10cSrcweir SvxLanguageItem SwAnnotationWin::GetLanguage(void)
269cdf0e10cSrcweir {
270cdf0e10cSrcweir     // set initial language for outliner
271cdf0e10cSrcweir     sal_uInt16 nScriptType = SvtLanguageOptions::GetScriptTypeOfLanguage( mpFld->GetLanguage() );
272cdf0e10cSrcweir     sal_uInt16 nLangWhichId = 0;
273cdf0e10cSrcweir     switch (nScriptType)
274cdf0e10cSrcweir     {
275cdf0e10cSrcweir         case SCRIPTTYPE_LATIN :    nLangWhichId = EE_CHAR_LANGUAGE ; break;
276cdf0e10cSrcweir         case SCRIPTTYPE_ASIAN :    nLangWhichId = EE_CHAR_LANGUAGE_CJK; break;
277cdf0e10cSrcweir         case SCRIPTTYPE_COMPLEX :  nLangWhichId = EE_CHAR_LANGUAGE_CTL; break;
278cdf0e10cSrcweir         default: DBG_ERROR("GetLanguage: wrong script tye");
279cdf0e10cSrcweir     }
280cdf0e10cSrcweir     return SvxLanguageItem(mpFld->GetLanguage(),nLangWhichId);
281cdf0e10cSrcweir }
282cdf0e10cSrcweir 
283cdf0e10cSrcweir bool SwAnnotationWin::IsProtected()
284cdf0e10cSrcweir {
285cdf0e10cSrcweir     return SwSidebarWin::IsProtected() ||
286cdf0e10cSrcweir            GetLayoutStatus() == SwPostItHelper::DELETED ||
287cdf0e10cSrcweir            ( mpFmtFld ? mpFmtFld->IsProtect() : false );
288cdf0e10cSrcweir }
289cdf0e10cSrcweir 
290cdf0e10cSrcweir String SwAnnotationWin::GetAuthor()
291cdf0e10cSrcweir {
292cdf0e10cSrcweir     return mpFld->GetPar1();
293cdf0e10cSrcweir }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir Date SwAnnotationWin::GetDate()
296cdf0e10cSrcweir {
297cdf0e10cSrcweir     return mpFld->GetDate();
298cdf0e10cSrcweir }
299cdf0e10cSrcweir 
300cdf0e10cSrcweir Time SwAnnotationWin::GetTime()
301cdf0e10cSrcweir {
302cdf0e10cSrcweir     return mpFld->GetTime();
303cdf0e10cSrcweir }
304cdf0e10cSrcweir 
305cdf0e10cSrcweir } } // end of namespace sw::annotation
306