LCOV - code coverage report
Current view: top level - sw/source/uibase/docvw - AnnotationWin.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 63 157 40.1 %
Date: 2014-11-03 Functions: 12 19 63.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10