LCOV - code coverage report
Current view: top level - libreoffice/sw/source/ui/docvw - AnnotationWin.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 61 156 39.1 %
Date: 2012-12-27 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             : 
      21             : #include <AnnotationWin.hxx>
      22             : 
      23             : #include <AnnotationMenuButton.hxx>
      24             : #include <PostItMgr.hxx>
      25             : 
      26             : #include <annotation.hrc>
      27             : #include <popup.hrc>
      28             : #include <cmdid.h>
      29             : 
      30             : #include <vcl/menu.hxx>
      31             : 
      32             : #include <svl/undo.hxx>
      33             : #include <unotools/syslocale.hxx>
      34             : #include <svl/languageoptions.hxx>
      35             : 
      36             : #include <editeng/postitem.hxx>
      37             : #include <editeng/fhgtitem.hxx>
      38             : #include <editeng/langitem.hxx>
      39             : 
      40             : #include <editeng/editview.hxx>
      41             : #include <editeng/outliner.hxx>
      42             : #include <editeng/editeng.hxx>
      43             : #include <editeng/editobj.hxx>
      44             : 
      45             : #include <docufld.hxx> // SwPostItField
      46             : #include <txtfld.hxx>
      47             : #include <ndtxt.hxx>
      48             : #include <view.hxx>
      49             : #include <wrtsh.hxx>
      50             : #include <docsh.hxx>
      51             : #include <doc.hxx>
      52             : #include <IDocumentUndoRedo.hxx>
      53             : #include <SwUndoField.hxx>
      54             : 
      55             : #include <boost/scoped_ptr.hpp>
      56             : 
      57             : 
      58             : namespace sw { namespace annotation {
      59             : 
      60           6 : SwAnnotationWin::SwAnnotationWin( SwEditWin& rEditWin,
      61             :                                   WinBits nBits,
      62             :                                   SwPostItMgr& aMgr,
      63             :                                   SwPostItBits aBits,
      64             :                                   SwSidebarItem& rSidebarItem,
      65             :                                   SwFmtFld* aField )
      66             :     : SwSidebarWin( rEditWin, nBits, aMgr, aBits, rSidebarItem )
      67             :     , mpFmtFld(aField)
      68           6 :     , mpFld( static_cast<SwPostItField*>(aField->GetFld()))
      69          12 :     , mpButtonPopup(0)
      70             : {
      71           6 : }
      72             : 
      73           9 : SwAnnotationWin::~SwAnnotationWin()
      74             : {
      75           3 :     delete mpButtonPopup;
      76           6 : }
      77             : 
      78           6 : void SwAnnotationWin::SetPostItText()
      79             : {
      80             :     //If the cursor was visible, then make it visible again after
      81             :     //changing text, e.g. fdo#33599
      82           6 :     Cursor *pCursor = GetOutlinerView()->GetEditView().GetCursor();
      83           6 :     bool bCursorVisible = pCursor ? pCursor->IsVisible() : false;
      84             : 
      85             :     //If the new text is the same as the old text, keep the same insertion
      86             :     //point .e.g. fdo#33599
      87           6 :     mpFld = static_cast<SwPostItField*>(mpFmtFld->GetFld());
      88           6 :     rtl::OUString sNewText = mpFld->GetPar2();
      89           6 :     bool bTextUnchanged = sNewText.equals(Engine()->GetEditEngine().GetText());
      90           6 :     ESelection aOrigSelection(GetOutlinerView()->GetEditView().GetSelection());
      91             : 
      92             :     // get text from SwPostItField and insert into our textview
      93           6 :     Engine()->SetModifyHdl( Link() );
      94           6 :     Engine()->EnableUndo( sal_False );
      95           6 :     if( mpFld->GetTextObject() )
      96           4 :         Engine()->SetText( *mpFld->GetTextObject() );
      97             :     else
      98             :     {
      99           2 :         Engine()->Clear();
     100           2 :         GetOutlinerView()->SetAttribs(DefaultItem());
     101           2 :         GetOutlinerView()->InsertText(sNewText,false);
     102             :     }
     103             : 
     104           6 :     Engine()->ClearModifyFlag();
     105           6 :     Engine()->GetUndoManager().Clear();
     106           6 :     Engine()->EnableUndo( sal_True );
     107           6 :     Engine()->SetModifyHdl( LINK( this, SwAnnotationWin, ModifyHdl ) );
     108           6 :     if (bTextUnchanged)
     109           0 :         GetOutlinerView()->GetEditView().SetSelection(aOrigSelection);
     110           6 :     if (bCursorVisible)
     111           6 :         GetOutlinerView()->ShowCursor();
     112           6 :     Invalidate();
     113           6 : }
     114             : 
     115           0 : void SwAnnotationWin::UpdateData()
     116             : {
     117           0 :     if ( Engine()->IsModified() )
     118             :     {
     119             :         IDocumentUndoRedo & rUndoRedo(
     120           0 :             DocView().GetDocShell()->GetDoc()->GetIDocumentUndoRedo());
     121           0 :         boost::scoped_ptr<SwField> pOldField;
     122           0 :         if (rUndoRedo.DoesUndo())
     123             :         {
     124           0 :             pOldField.reset(mpFld->Copy());
     125             :         }
     126           0 :         mpFld->SetPar2(Engine()->GetEditEngine().GetText());
     127           0 :         mpFld->SetTextObject(Engine()->CreateParaObject());
     128           0 :         if (rUndoRedo.DoesUndo())
     129             :         {
     130           0 :             SwTxtFld *const pTxtFld = mpFmtFld->GetTxtFld();
     131           0 :             SwPosition aPosition( pTxtFld->GetTxtNode() );
     132           0 :             aPosition.nContent = *pTxtFld->GetStart();
     133             :             rUndoRedo.AppendUndo(
     134           0 :                 new SwUndoFieldFromDoc(aPosition, *pOldField, *mpFld, 0, true));
     135             :         }
     136             :         // so we get a new layout of notes (anchor position is still the same and we would otherwise not get one)
     137           0 :         Mgr().SetLayout();
     138             :         // #i98686# if we have several views, all notes should update their text
     139           0 :         mpFmtFld->Broadcast(SwFmtFldHint( 0, SWFMTFLD_CHANGED));
     140           0 :         DocView().GetDocShell()->SetModified();
     141             :     }
     142           0 :     Engine()->ClearModifyFlag();
     143           0 :     Engine()->GetUndoManager().Clear();
     144           0 : }
     145             : 
     146           0 : void SwAnnotationWin::Delete()
     147             : {
     148           0 :     SwSidebarWin::Delete();
     149             :     // we delete the field directly, the Mgr cleans up the PostIt by listening
     150           0 :     DocView().GetWrtShellPtr()->GotoField(*mpFmtFld);
     151           0 :     GrabFocusToDocument();
     152           0 :     DocView().GetWrtShellPtr()->DelRight();
     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          26 : bool SwAnnotationWin::CalcFollow()
     171             : {
     172          26 :     SwTxtFld* pTxtFld = mpFmtFld->GetTxtFld();
     173          26 :     SwPosition aPosition( pTxtFld->GetTxtNode() );
     174          26 :     aPosition.nContent = *pTxtFld->GetStart();
     175          26 :     SwTxtAttr * const pTxtAttr = pTxtFld->GetTxtNode().GetTxtAttrForCharAt(
     176          52 :                     aPosition.nContent.GetIndex() - 1, RES_TXTATR_FIELD );
     177          26 :     const SwField* pFld = pTxtAttr ? pTxtAttr->GetFld().GetFld() : 0;
     178          26 :     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_FIELD );
     192             :     SwField* pFld = pTxtAttr
     193           0 :                     ? const_cast<SwField*>(pTxtAttr->GetFld().GetFld())
     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_FIELD );
     201             :         pFld = pTxtAttr
     202           0 :                ? const_cast<SwField*>(pTxtAttr->GetFld().GetFld())
     203           0 :                : 0;
     204             :     }
     205           0 :     return aCount - 1;
     206             : }
     207             : 
     208           6 : MenuButton* SwAnnotationWin::CreateMenuButton()
     209             : {
     210           6 :     mpButtonPopup = new PopupMenu(SW_RES(MN_ANNOTATION_BUTTON));
     211           6 :     XubString aText = mpButtonPopup->GetItemText( FN_DELETE_NOTE_AUTHOR );
     212           6 :     SwRewriter aRewriter;
     213           6 :     aRewriter.AddRule(UndoArg1,GetAuthor());
     214           6 :     aText = aRewriter.Apply(aText);
     215           6 :     mpButtonPopup->SetItemText(FN_DELETE_NOTE_AUTHOR,aText);
     216           6 :     MenuButton* pMenuButton = new AnnotationMenuButton( *this );
     217           6 :     pMenuButton->SetPopupMenu( mpButtonPopup );
     218           6 :     pMenuButton->Show();
     219           6 :     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 :     String aText = String(SW_RES(STR_REPLY));
     229           0 :         SwRewriter aRewriter;
     230           0 :         aRewriter.AddRule(UndoArg1, pWin->GetAuthor());
     231           0 :         aText = aRewriter.Apply(aText);
     232             :         aText.Append(String(rtl::OUString(" (") +
     233           0 :         String(rLocalData.getDate( pWin->GetDate())) + rtl::OUString(", ") +
     234           0 :         String(rLocalData.getTime( pWin->GetTime(),false)) + rtl::OUString("): \"")));
     235           0 :     GetOutlinerView()->InsertText(aText,false);
     236             : 
     237             :     // insert old, selected text or "..."
     238             :     // TOOD: iterate over all paragraphs, not only first one to find out if it is empty
     239           0 :     if (pText->GetTextObject().GetText(0).Len())
     240           0 :         GetOutlinerView()->GetEditView().InsertText(pText->GetTextObject());
     241             :     else
     242           0 :         GetOutlinerView()->InsertText(rtl::OUString("..."),false);
     243           0 :     GetOutlinerView()->InsertText(rtl::OUString("\"\n"),false);
     244             : 
     245           0 :     GetOutlinerView()->SetSelection(ESelection(0x0,0x0,0xFFFF,0xFFFF));
     246           0 :     SfxItemSet aAnswerSet( DocView().GetDocShell()->GetPool() );
     247           0 :     aAnswerSet.Put(SvxFontHeightItem(200,80,EE_CHAR_FONTHEIGHT));
     248           0 :     aAnswerSet.Put(SvxPostureItem(ITALIC_NORMAL,EE_CHAR_ITALIC));
     249           0 :     GetOutlinerView()->SetAttribs(aAnswerSet);
     250           0 :     GetOutlinerView()->SetSelection(ESelection(0xFFFF,0xFFFF,0xFFFF,0xFFFF));
     251             : 
     252             :     //remove all attributes and reset our standard ones
     253           0 :     GetOutlinerView()->GetEditView().RemoveAttribsKeepLanguages(true);
     254           0 :     GetOutlinerView()->SetAttribs(DefaultItem());
     255             :     // lets insert an undo step so the initial text can be easily deleted
     256             :     // but do not use UpdateData() directly, would set modified state again and reentrance into Mgr
     257           0 :     Engine()->SetModifyHdl( Link() );
     258             :     IDocumentUndoRedo & rUndoRedo(
     259           0 :         DocView().GetDocShell()->GetDoc()->GetIDocumentUndoRedo());
     260           0 :     boost::scoped_ptr<SwField> pOldField;
     261           0 :     if (rUndoRedo.DoesUndo())
     262             :     {
     263           0 :         pOldField.reset(mpFld->Copy());
     264             :     }
     265           0 :     mpFld->SetPar2(Engine()->GetEditEngine().GetText());
     266           0 :     mpFld->SetTextObject(Engine()->CreateParaObject());
     267           0 :     if (rUndoRedo.DoesUndo())
     268             :     {
     269           0 :         SwTxtFld *const pTxtFld = mpFmtFld->GetTxtFld();
     270           0 :         SwPosition aPosition( pTxtFld->GetTxtNode() );
     271           0 :         aPosition.nContent = *pTxtFld->GetStart();
     272             :         rUndoRedo.AppendUndo(
     273           0 :             new SwUndoFieldFromDoc(aPosition, *pOldField, *mpFld, 0, true));
     274             :     }
     275           0 :     Engine()->SetModifyHdl( LINK( this, SwAnnotationWin, ModifyHdl ) );
     276           0 :     Engine()->ClearModifyFlag();
     277           0 :     Engine()->GetUndoManager().Clear();
     278           0 : }
     279             : 
     280           6 : SvxLanguageItem SwAnnotationWin::GetLanguage(void)
     281             : {
     282             :     // set initial language for outliner
     283           6 :     sal_uInt16 nScriptType = SvtLanguageOptions::GetScriptTypeOfLanguage( mpFld->GetLanguage() );
     284           6 :     sal_uInt16 nLangWhichId = 0;
     285           6 :     switch (nScriptType)
     286             :     {
     287           6 :         case SCRIPTTYPE_LATIN :    nLangWhichId = EE_CHAR_LANGUAGE ; break;
     288           0 :         case SCRIPTTYPE_ASIAN :    nLangWhichId = EE_CHAR_LANGUAGE_CJK; break;
     289           0 :         case SCRIPTTYPE_COMPLEX :  nLangWhichId = EE_CHAR_LANGUAGE_CTL; break;
     290             :         default: OSL_FAIL("GetLanguage: wrong script type");
     291             :     }
     292           6 :     return SvxLanguageItem(mpFld->GetLanguage(),nLangWhichId);
     293             : }
     294             : 
     295           0 : bool SwAnnotationWin::IsProtected()
     296             : {
     297           0 :     return SwSidebarWin::IsProtected() ||
     298           0 :            GetLayoutStatus() == SwPostItHelper::DELETED ||
     299           0 :            ( mpFmtFld ? mpFmtFld->IsProtect() : false );
     300             : }
     301             : 
     302          18 : String SwAnnotationWin::GetAuthor()
     303             : {
     304          18 :     return mpFld->GetPar1();
     305             : }
     306             : 
     307           6 : Date SwAnnotationWin::GetDate()
     308             : {
     309           6 :     return mpFld->GetDate();
     310             : }
     311             : 
     312          10 : Time SwAnnotationWin::GetTime()
     313             : {
     314          10 :     return mpFld->GetTime();
     315             : }
     316             : 
     317             : } } // end of namespace sw::annotation
     318             : 
     319             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10