xref: /aoo41x/main/sw/source/ui/docvw/AnnotationWin.cxx (revision 22ea211e)
1efeef26fSAndrew Rist /**************************************************************
2efeef26fSAndrew Rist  *
3efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5efeef26fSAndrew Rist  * distributed with this work for additional information
6efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10efeef26fSAndrew Rist  *
11efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12efeef26fSAndrew Rist  *
13efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17efeef26fSAndrew Rist  * specific language governing permissions and limitations
18efeef26fSAndrew Rist  * under the License.
19efeef26fSAndrew Rist  *
20efeef26fSAndrew Rist  *************************************************************/
21efeef26fSAndrew Rist 
22efeef26fSAndrew 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 
SwAnnotationWin(SwEditWin & rEditWin,WinBits nBits,SwPostItMgr & aMgr,SwPostItBits aBits,SwSidebarItem & rSidebarItem,SwFmtFld * aField)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)
71c0286415SOliver-Rainer Wittmann     , mpFld( static_cast<SwPostItField*>(aField->GetField()))
72cdf0e10cSrcweir     , mpButtonPopup(0)
73cdf0e10cSrcweir {
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
~SwAnnotationWin()76cdf0e10cSrcweir SwAnnotationWin::~SwAnnotationWin()
77cdf0e10cSrcweir {
78cdf0e10cSrcweir     delete mpButtonPopup;
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
SetPostItText()81cdf0e10cSrcweir void SwAnnotationWin::SetPostItText()
82cdf0e10cSrcweir {
83cdf0e10cSrcweir     // get text from SwPostItField and insert into our textview
84cdf0e10cSrcweir     Engine()->SetModifyHdl( Link() );
85cdf0e10cSrcweir     Engine()->EnableUndo( sal_False );
86c0286415SOliver-Rainer Wittmann     mpFld = static_cast<SwPostItField*>(mpFmtFld->GetField());
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 
UpdateData()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 
Delete()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 
GotoPos()143cdf0e10cSrcweir void SwAnnotationWin::GotoPos()
144cdf0e10cSrcweir {
145cdf0e10cSrcweir     DocView().GetDocShell()->GetWrtShell()->GotoField(*mpFmtFld);
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
MoveCaret()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
CalcFollow()158cdf0e10cSrcweir bool SwAnnotationWin::CalcFollow()
159cdf0e10cSrcweir {
160cdf0e10cSrcweir     SwTxtFld* pTxtFld = mpFmtFld->GetTxtFld();
161cdf0e10cSrcweir     SwPosition aPosition( pTxtFld->GetTxtNode() );
162cdf0e10cSrcweir     aPosition.nContent = *pTxtFld->GetStart();
163dec99bbdSOliver-Rainer Wittmann     SwTxtAttr * const pTxtAttr =
164dec99bbdSOliver-Rainer Wittmann         pTxtFld->GetTxtNode().GetTxtAttrForCharAt(
165dec99bbdSOliver-Rainer Wittmann             aPosition.nContent.GetIndex() - 1,
166dec99bbdSOliver-Rainer Wittmann             RES_TXTATR_ANNOTATION );
167c0286415SOliver-Rainer Wittmann     const SwField* pFld = pTxtAttr ? pTxtAttr->GetFmtFld().GetField() : 0;
168cdf0e10cSrcweir     return pFld && (pFld->Which()== RES_POSTITFLD);
169cdf0e10cSrcweir }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir // counts how many SwPostItField we have right after the current one
CountFollowing()172cdf0e10cSrcweir sal_uInt32 SwAnnotationWin::CountFollowing()
173cdf0e10cSrcweir {
174cdf0e10cSrcweir     sal_uInt32 aCount = 1;  // we start with 1, so we have to subtract one at the end again
175cdf0e10cSrcweir     SwTxtFld* pTxtFld = mpFmtFld->GetTxtFld();
176cdf0e10cSrcweir     SwPosition aPosition( pTxtFld->GetTxtNode() );
177cdf0e10cSrcweir     aPosition.nContent = *pTxtFld->GetStart();
178cdf0e10cSrcweir 
179cdf0e10cSrcweir     SwTxtAttr * pTxtAttr = pTxtFld->GetTxtNode().GetTxtAttrForCharAt(
180cdf0e10cSrcweir                                         aPosition.nContent.GetIndex() + 1,
181dec99bbdSOliver-Rainer Wittmann                                         RES_TXTATR_ANNOTATION );
182cdf0e10cSrcweir     SwField* pFld = pTxtAttr
183c0286415SOliver-Rainer Wittmann                     ? const_cast<SwField*>(pTxtAttr->GetFmtFld().GetField())
184cdf0e10cSrcweir                     : 0;
185cdf0e10cSrcweir     while ( pFld && ( pFld->Which()== RES_POSTITFLD ) )
186cdf0e10cSrcweir     {
187cdf0e10cSrcweir         aCount++;
188cdf0e10cSrcweir         pTxtAttr = pTxtFld->GetTxtNode().GetTxtAttrForCharAt(
189cdf0e10cSrcweir                                         aPosition.nContent.GetIndex() + aCount,
190dec99bbdSOliver-Rainer Wittmann                                         RES_TXTATR_ANNOTATION );
191cdf0e10cSrcweir         pFld = pTxtAttr
192c0286415SOliver-Rainer Wittmann                ? const_cast<SwField*>(pTxtAttr->GetFmtFld().GetField())
193cdf0e10cSrcweir                : 0;
194cdf0e10cSrcweir     }
195cdf0e10cSrcweir     return aCount - 1;
196cdf0e10cSrcweir }
197cdf0e10cSrcweir 
CreateMenuButton()198cdf0e10cSrcweir MenuButton* SwAnnotationWin::CreateMenuButton()
199cdf0e10cSrcweir {
200cdf0e10cSrcweir     mpButtonPopup = new PopupMenu(SW_RES(MN_ANNOTATION_BUTTON));
201cdf0e10cSrcweir     XubString aText = mpButtonPopup->GetItemText( FN_DELETE_NOTE_AUTHOR );
202cdf0e10cSrcweir     SwRewriter aRewriter;
203cdf0e10cSrcweir     aRewriter.AddRule(UNDO_ARG1,GetAuthor());
204cdf0e10cSrcweir     aText = aRewriter.Apply(aText);
205cdf0e10cSrcweir     mpButtonPopup->SetItemText(FN_DELETE_NOTE_AUTHOR,aText);
206cdf0e10cSrcweir     MenuButton* pMenuButton = new AnnotationMenuButton( *this );
207cdf0e10cSrcweir     pMenuButton->SetPopupMenu( mpButtonPopup );
208cdf0e10cSrcweir     pMenuButton->Show();
209cdf0e10cSrcweir     return pMenuButton;
210cdf0e10cSrcweir }
211cdf0e10cSrcweir 
InitAnswer(OutlinerParaObject * pText)212cdf0e10cSrcweir void SwAnnotationWin::InitAnswer(OutlinerParaObject* pText)
213cdf0e10cSrcweir {
214cdf0e10cSrcweir     //collect our old meta data
215cdf0e10cSrcweir     SwSidebarWin* pWin = Mgr().GetNextPostIt(KEY_PAGEUP, this);
216cdf0e10cSrcweir     const SvtSysLocale aSysLocale;
217cdf0e10cSrcweir     const LocaleDataWrapper& rLocalData = aSysLocale.GetLocaleData();
218cdf0e10cSrcweir     String aText = String(SW_RES(STR_REPLY));
219cdf0e10cSrcweir         SwRewriter aRewriter;
220cdf0e10cSrcweir         aRewriter.AddRule(UNDO_ARG1, pWin->GetAuthor());
221cdf0e10cSrcweir         aText = aRewriter.Apply(aText);
222cdf0e10cSrcweir         aText.Append(String(rtl::OUString::createFromAscii(" (") +
223cdf0e10cSrcweir         String(rLocalData.getDate( pWin->GetDate())) + rtl::OUString::createFromAscii(", ") +
224cdf0e10cSrcweir         String(rLocalData.getTime( pWin->GetTime(),false)) + rtl::OUString::createFromAscii("): \"")));
225cdf0e10cSrcweir     GetOutlinerView()->InsertText(aText,false);
226cdf0e10cSrcweir 
227cdf0e10cSrcweir     // insert old, selected text or "..."
228*22ea211eSmseidel     // TODO: iterate over all paragraphs, not only first one to find out if it is empty
229cdf0e10cSrcweir     if (pText->GetTextObject().GetText(0) != String(rtl::OUString::createFromAscii("")))
230cdf0e10cSrcweir         GetOutlinerView()->GetEditView().InsertText(pText->GetTextObject());
231cdf0e10cSrcweir     else
232cdf0e10cSrcweir         GetOutlinerView()->InsertText(rtl::OUString::createFromAscii("..."),false);
233cdf0e10cSrcweir     GetOutlinerView()->InsertText(rtl::OUString::createFromAscii("\"\n"),false);
234cdf0e10cSrcweir 
235cdf0e10cSrcweir     GetOutlinerView()->SetSelection(ESelection(0x0,0x0,0xFFFF,0xFFFF));
236cdf0e10cSrcweir     SfxItemSet aAnswerSet( DocView().GetDocShell()->GetPool() );
237cdf0e10cSrcweir     aAnswerSet.Put(SvxFontHeightItem(200,80,EE_CHAR_FONTHEIGHT));
238cdf0e10cSrcweir     aAnswerSet.Put(SvxPostureItem(ITALIC_NORMAL,EE_CHAR_ITALIC));
239cdf0e10cSrcweir     GetOutlinerView()->SetAttribs(aAnswerSet);
240cdf0e10cSrcweir     GetOutlinerView()->SetSelection(ESelection(0xFFFF,0xFFFF,0xFFFF,0xFFFF));
241cdf0e10cSrcweir 
242cdf0e10cSrcweir     //remove all attributes and reset our standard ones
243cdf0e10cSrcweir     GetOutlinerView()->GetEditView().RemoveAttribsKeepLanguages(true);
244cdf0e10cSrcweir     GetOutlinerView()->SetAttribs(DefaultItem());
245cdf0e10cSrcweir     // lets insert an undo step so the initial text can be easily deleted
246cdf0e10cSrcweir     // but do not use UpdateData() directly, would set modified state again and reentrance into Mgr
247cdf0e10cSrcweir     Engine()->SetModifyHdl( Link() );
248cdf0e10cSrcweir     IDocumentUndoRedo & rUndoRedo(
249cdf0e10cSrcweir         DocView().GetDocShell()->GetDoc()->GetIDocumentUndoRedo());
250cdf0e10cSrcweir     ::std::auto_ptr<SwField> pOldField;
251cdf0e10cSrcweir     if (rUndoRedo.DoesUndo())
252cdf0e10cSrcweir     {
253cdf0e10cSrcweir         pOldField.reset(mpFld->Copy());
254cdf0e10cSrcweir     }
255cdf0e10cSrcweir     mpFld->SetPar2(Engine()->GetEditEngine().GetText());
256cdf0e10cSrcweir     mpFld->SetTextObject(Engine()->CreateParaObject());
257cdf0e10cSrcweir     if (rUndoRedo.DoesUndo())
258cdf0e10cSrcweir     {
259cdf0e10cSrcweir         SwTxtFld *const pTxtFld = mpFmtFld->GetTxtFld();
260cdf0e10cSrcweir         SwPosition aPosition( pTxtFld->GetTxtNode() );
261cdf0e10cSrcweir         aPosition.nContent = *pTxtFld->GetStart();
262cdf0e10cSrcweir         rUndoRedo.AppendUndo(
263cdf0e10cSrcweir             new SwUndoFieldFromDoc(aPosition, *pOldField, *mpFld, 0, true));
264cdf0e10cSrcweir     }
265cdf0e10cSrcweir     Engine()->SetModifyHdl( LINK( this, SwAnnotationWin, ModifyHdl ) );
266cdf0e10cSrcweir     Engine()->ClearModifyFlag();
267cdf0e10cSrcweir     Engine()->GetUndoManager().Clear();
268cdf0e10cSrcweir }
269cdf0e10cSrcweir 
GetLanguage(void)270cdf0e10cSrcweir SvxLanguageItem SwAnnotationWin::GetLanguage(void)
271cdf0e10cSrcweir {
272cdf0e10cSrcweir     // set initial language for outliner
273cdf0e10cSrcweir     sal_uInt16 nScriptType = SvtLanguageOptions::GetScriptTypeOfLanguage( mpFld->GetLanguage() );
274cdf0e10cSrcweir     sal_uInt16 nLangWhichId = 0;
275cdf0e10cSrcweir     switch (nScriptType)
276cdf0e10cSrcweir     {
277cdf0e10cSrcweir         case SCRIPTTYPE_LATIN :    nLangWhichId = EE_CHAR_LANGUAGE ; break;
278cdf0e10cSrcweir         case SCRIPTTYPE_ASIAN :    nLangWhichId = EE_CHAR_LANGUAGE_CJK; break;
279cdf0e10cSrcweir         case SCRIPTTYPE_COMPLEX :  nLangWhichId = EE_CHAR_LANGUAGE_CTL; break;
280cdf0e10cSrcweir         default: DBG_ERROR("GetLanguage: wrong script tye");
281cdf0e10cSrcweir     }
282cdf0e10cSrcweir     return SvxLanguageItem(mpFld->GetLanguage(),nLangWhichId);
283cdf0e10cSrcweir }
284cdf0e10cSrcweir 
IsProtected()285cdf0e10cSrcweir bool SwAnnotationWin::IsProtected()
286cdf0e10cSrcweir {
287cdf0e10cSrcweir     return SwSidebarWin::IsProtected() ||
288cdf0e10cSrcweir            GetLayoutStatus() == SwPostItHelper::DELETED ||
289cdf0e10cSrcweir            ( mpFmtFld ? mpFmtFld->IsProtect() : false );
290cdf0e10cSrcweir }
291cdf0e10cSrcweir 
GetAuthor()292cdf0e10cSrcweir String SwAnnotationWin::GetAuthor()
293cdf0e10cSrcweir {
294cdf0e10cSrcweir     return mpFld->GetPar1();
295cdf0e10cSrcweir }
296cdf0e10cSrcweir 
GetDate()297cdf0e10cSrcweir Date SwAnnotationWin::GetDate()
298cdf0e10cSrcweir {
299cdf0e10cSrcweir     return mpFld->GetDate();
300cdf0e10cSrcweir }
301cdf0e10cSrcweir 
GetTime()302cdf0e10cSrcweir Time SwAnnotationWin::GetTime()
303cdf0e10cSrcweir {
304cdf0e10cSrcweir     return mpFld->GetTime();
305cdf0e10cSrcweir }
306cdf0e10cSrcweir 
307cdf0e10cSrcweir } } // end of namespace sw::annotation
308