LCOV - code coverage report
Current view: top level - sw/source/core/uibase/docvw - AnnotationWin.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 62 156 39.7 %
Date: 2014-04-11 Functions: 10 17 58.8 %
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          48 : 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          48 :     , mpFld( static_cast<SwPostItField*>(aField->GetField()))
      67          96 :     , mpButtonPopup(0)
      68             : {
      69          48 : }
      70             : 
      71         144 : SwAnnotationWin::~SwAnnotationWin()
      72             : {
      73          48 :     delete mpButtonPopup;
      74          96 : }
      75             : 
      76          48 : void SwAnnotationWin::SetPostItText()
      77             : {
      78             :     //If the cursor was visible, then make it visible again after
      79             :     //changing text, e.g. fdo#33599
      80          48 :     Cursor *pCursor = GetOutlinerView()->GetEditView().GetCursor();
      81          48 :     bool bCursorVisible = pCursor ? pCursor->IsVisible() : false;
      82             : 
      83             :     //If the new text is the same as the old text, keep the same insertion
      84             :     //point .e.g. fdo#33599
      85          48 :     mpFld = static_cast<SwPostItField*>(mpFmtFld->GetField());
      86          48 :     OUString sNewText = mpFld->GetPar2();
      87          48 :     bool bTextUnchanged = sNewText.equals(Engine()->GetEditEngine().GetText());
      88          48 :     ESelection aOrigSelection(GetOutlinerView()->GetEditView().GetSelection());
      89             : 
      90             :     // get text from SwPostItField and insert into our textview
      91          48 :     Engine()->SetModifyHdl( Link() );
      92          48 :     Engine()->EnableUndo( false );
      93          48 :     if( mpFld->GetTextObject() )
      94          47 :         Engine()->SetText( *mpFld->GetTextObject() );
      95             :     else
      96             :     {
      97           1 :         Engine()->Clear();
      98           1 :         GetOutlinerView()->SetAttribs(DefaultItem());
      99           1 :         GetOutlinerView()->InsertText(sNewText,false);
     100             :     }
     101             : 
     102          48 :     Engine()->ClearModifyFlag();
     103          48 :     Engine()->GetUndoManager().Clear();
     104          48 :     Engine()->EnableUndo( true );
     105          48 :     Engine()->SetModifyHdl( LINK( this, SwAnnotationWin, ModifyHdl ) );
     106          48 :     if (bTextUnchanged)
     107           0 :         GetOutlinerView()->GetEditView().SetSelection(aOrigSelection);
     108          48 :     if (bCursorVisible)
     109          48 :         GetOutlinerView()->ShowCursor();
     110          48 :     Invalidate();
     111          48 : }
     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, SWFMTFLD_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 :     SwSidebarWin::Delete();
     147             :     // we delete the field directly, the Mgr cleans up the PostIt by listening
     148           0 :     DocView().GetWrtShellPtr()->GotoField(*mpFmtFld);
     149           0 :     GrabFocusToDocument();
     150           0 :     DocView().GetWrtShellPtr()->DelRight();
     151           0 : }
     152             : 
     153           0 : void SwAnnotationWin::GotoPos()
     154             : {
     155           0 :     DocView().GetDocShell()->GetWrtShell()->GotoField(*mpFmtFld);
     156           0 : }
     157             : 
     158           0 : sal_uInt32 SwAnnotationWin::MoveCaret()
     159             : {
     160             :     // if this is an answer, do not skip over all following ones, but insert directly behind the current one
     161             :     // but when just leaving a note, skip all following ones as well to continue typing
     162           0 :     return Mgr().IsAnswer()
     163             :            ? 1
     164           0 :            : 1 + CountFollowing();
     165             : }
     166             : 
     167             : //returns true, if there is another note right before this note
     168         322 : bool SwAnnotationWin::CalcFollow()
     169             : {
     170         322 :     SwTxtFld* pTxtFld = mpFmtFld->GetTxtFld();
     171         322 :     SwPosition aPosition( pTxtFld->GetTxtNode() );
     172         322 :     aPosition.nContent = *pTxtFld->GetStart();
     173             :     SwTxtAttr * const pTxtAttr =
     174         322 :         pTxtFld->GetTxtNode().GetTxtAttrForCharAt(
     175         322 :             aPosition.nContent.GetIndex() - 1,
     176         644 :             RES_TXTATR_ANNOTATION );
     177         322 :     const SwField* pFld = pTxtAttr ? pTxtAttr->GetFmtFld().GetField() : 0;
     178         322 :     return pFld && (pFld->Which()== RES_POSTITFLD);
     179             : }
     180             : 
     181             : // counts how many SwPostItField we have right after the current one
     182           0 : sal_uInt32 SwAnnotationWin::CountFollowing()
     183             : {
     184           0 :     sal_uInt32 aCount = 1;  // we start with 1, so we have to subtract one at the end again
     185           0 :     SwTxtFld* pTxtFld = mpFmtFld->GetTxtFld();
     186           0 :     SwPosition aPosition( pTxtFld->GetTxtNode() );
     187           0 :     aPosition.nContent = *pTxtFld->GetStart();
     188             : 
     189           0 :     SwTxtAttr * pTxtAttr = pTxtFld->GetTxtNode().GetTxtAttrForCharAt(
     190           0 :                                         aPosition.nContent.GetIndex() + 1,
     191           0 :                                         RES_TXTATR_ANNOTATION );
     192             :     SwField* pFld = pTxtAttr
     193           0 :                     ? const_cast<SwField*>(pTxtAttr->GetFmtFld().GetField())
     194           0 :                     : 0;
     195           0 :     while ( pFld && ( pFld->Which()== RES_POSTITFLD ) )
     196             :     {
     197           0 :         aCount++;
     198           0 :         pTxtAttr = pTxtFld->GetTxtNode().GetTxtAttrForCharAt(
     199           0 :                                         aPosition.nContent.GetIndex() + aCount,
     200           0 :                                         RES_TXTATR_ANNOTATION );
     201             :         pFld = pTxtAttr
     202           0 :                ? const_cast<SwField*>(pTxtAttr->GetFmtFld().GetField())
     203           0 :                : 0;
     204             :     }
     205           0 :     return aCount - 1;
     206             : }
     207             : 
     208          48 : MenuButton* SwAnnotationWin::CreateMenuButton()
     209             : {
     210          48 :     mpButtonPopup = new PopupMenu(SW_RES(MN_ANNOTATION_BUTTON));
     211          48 :     OUString aText = mpButtonPopup->GetItemText( FN_DELETE_NOTE_AUTHOR );
     212          96 :     SwRewriter aRewriter;
     213          48 :     aRewriter.AddRule(UndoArg1,GetAuthor());
     214          48 :     aText = aRewriter.Apply(aText);
     215          48 :     mpButtonPopup->SetItemText(FN_DELETE_NOTE_AUTHOR,aText);
     216          48 :     MenuButton* pMenuButton = new AnnotationMenuButton( *this );
     217          48 :     pMenuButton->SetPopupMenu( mpButtonPopup );
     218          48 :     pMenuButton->Show();
     219          96 :     return pMenuButton;
     220             : }
     221             : 
     222           0 : void SwAnnotationWin::InitAnswer(OutlinerParaObject* pText)
     223             : {
     224             :     //collect our old meta data
     225           0 :     SwSidebarWin* pWin = Mgr().GetNextPostIt(KEY_PAGEUP, this);
     226           0 :     const SvtSysLocale aSysLocale;
     227           0 :     const LocaleDataWrapper& rLocalData = aSysLocale.GetLocaleData();
     228           0 :     SwRewriter aRewriter;
     229           0 :     aRewriter.AddRule(UndoArg1, pWin->GetAuthor());
     230             :     const OUString aText = aRewriter.Apply(SW_RESSTR(STR_REPLY))
     231           0 :             + " (" + rLocalData.getDate( pWin->GetDate())
     232           0 :             + ", " + rLocalData.getTime( pWin->GetTime(), false)
     233           0 :             + "): \"";
     234           0 :     GetOutlinerView()->InsertText(aText,false);
     235             : 
     236             :     // insert old, selected text or "..."
     237             :     // TODO: iterate over all paragraphs, not only first one to find out if it is empty
     238           0 :     if (!pText->GetTextObject().GetText(0).isEmpty())
     239           0 :         GetOutlinerView()->GetEditView().InsertText(pText->GetTextObject());
     240             :     else
     241           0 :         GetOutlinerView()->InsertText(OUString("..."),false);
     242           0 :     GetOutlinerView()->InsertText(OUString("\"\n"),false);
     243             : 
     244           0 :     GetOutlinerView()->SetSelection(ESelection(0,0,EE_PARA_ALL,EE_TEXTPOS_ALL));
     245           0 :     SfxItemSet aAnswerSet( DocView().GetDocShell()->GetPool() );
     246           0 :     aAnswerSet.Put(SvxFontHeightItem(200,80,EE_CHAR_FONTHEIGHT));
     247           0 :     aAnswerSet.Put(SvxPostureItem(ITALIC_NORMAL,EE_CHAR_ITALIC));
     248           0 :     GetOutlinerView()->SetAttribs(aAnswerSet);
     249           0 :     GetOutlinerView()->SetSelection(ESelection(EE_PARA_MAX_COUNT,EE_TEXTPOS_MAX_COUNT,EE_PARA_MAX_COUNT,EE_TEXTPOS_MAX_COUNT));
     250             : 
     251             :     //remove all attributes and reset our standard ones
     252           0 :     GetOutlinerView()->GetEditView().RemoveAttribsKeepLanguages(true);
     253           0 :     GetOutlinerView()->SetAttribs(DefaultItem());
     254             :     // lets insert an undo step so the initial text can be easily deleted
     255             :     // but do not use UpdateData() directly, would set modified state again and reentrance into Mgr
     256           0 :     Engine()->SetModifyHdl( Link() );
     257             :     IDocumentUndoRedo & rUndoRedo(
     258           0 :         DocView().GetDocShell()->GetDoc()->GetIDocumentUndoRedo());
     259           0 :     boost::scoped_ptr<SwField> pOldField;
     260           0 :     if (rUndoRedo.DoesUndo())
     261             :     {
     262           0 :         pOldField.reset(mpFld->Copy());
     263             :     }
     264           0 :     mpFld->SetPar2(Engine()->GetEditEngine().GetText());
     265           0 :     mpFld->SetTextObject(Engine()->CreateParaObject());
     266           0 :     if (rUndoRedo.DoesUndo())
     267             :     {
     268           0 :         SwTxtFld *const pTxtFld = mpFmtFld->GetTxtFld();
     269           0 :         SwPosition aPosition( pTxtFld->GetTxtNode() );
     270           0 :         aPosition.nContent = *pTxtFld->GetStart();
     271             :         rUndoRedo.AppendUndo(
     272           0 :             new SwUndoFieldFromDoc(aPosition, *pOldField, *mpFld, 0, true));
     273             :     }
     274           0 :     Engine()->SetModifyHdl( LINK( this, SwAnnotationWin, ModifyHdl ) );
     275           0 :     Engine()->ClearModifyFlag();
     276           0 :     Engine()->GetUndoManager().Clear();
     277           0 : }
     278             : 
     279          48 : SvxLanguageItem SwAnnotationWin::GetLanguage(void)
     280             : {
     281             :     // set initial language for outliner
     282          48 :     sal_uInt16 nScriptType = SvtLanguageOptions::GetScriptTypeOfLanguage( mpFld->GetLanguage() );
     283          48 :     sal_uInt16 nLangWhichId = 0;
     284          48 :     switch (nScriptType)
     285             :     {
     286          48 :         case SCRIPTTYPE_LATIN :    nLangWhichId = EE_CHAR_LANGUAGE ; break;
     287           0 :         case SCRIPTTYPE_ASIAN :    nLangWhichId = EE_CHAR_LANGUAGE_CJK; break;
     288           0 :         case SCRIPTTYPE_COMPLEX :  nLangWhichId = EE_CHAR_LANGUAGE_CTL; break;
     289             :         default: OSL_FAIL("GetLanguage: wrong script type");
     290             :     }
     291          48 :     return SvxLanguageItem(mpFld->GetLanguage(),nLangWhichId);
     292             : }
     293             : 
     294           0 : bool SwAnnotationWin::IsProtected()
     295             : {
     296           0 :     return SwSidebarWin::IsProtected() ||
     297           0 :            GetLayoutStatus() == SwPostItHelper::DELETED ||
     298           0 :            ( mpFmtFld ? mpFmtFld->IsProtect() : false );
     299             : }
     300             : 
     301         144 : OUString SwAnnotationWin::GetAuthor()
     302             : {
     303         144 :     return mpFld->GetPar1();
     304             : }
     305             : 
     306          48 : Date SwAnnotationWin::GetDate()
     307             : {
     308          48 :     return mpFld->GetDate();
     309             : }
     310             : 
     311          89 : Time SwAnnotationWin::GetTime()
     312             : {
     313          89 :     return mpFld->GetTime();
     314             : }
     315             : 
     316             : } } // end of namespace sw::annotation
     317             : 
     318             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10