summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi Abe <tabe@fixedpoint.jp>2016-05-25 20:00:35 +0900
committerTakeshi Abe <tabe@fixedpoint.jp>2016-05-26 02:22:54 +0000
commitf5ea7c09c8a6924b72c9b756a8e435ff40dad6c6 (patch)
treec85d1a5b4fc49f2cc055b961848a911e610c7e46
parenta042951ad4db2b84021e1d43361511dec998ce82 (diff)
starmath: nodes in the clipboard are owened by SmClipboard
This also omits the bIsOnlyIfSelected parameter for former CloneLineToList(), which was true at its sole call site. Change-Id: Idb71323f68f13ecc90d430ec8e18e0eef766ae4b Reviewed-on: https://gerrit.libreoffice.org/25444 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Takeshi Abe <tabe@fixedpoint.jp>
-rw-r--r--starmath/inc/cursor.hxx26
-rw-r--r--starmath/source/cursor.cxx52
2 files changed, 27 insertions, 51 deletions
diff --git a/starmath/inc/cursor.hxx b/starmath/inc/cursor.hxx
index 3f5c3df35fb0..9554fc7ea5c8 100644
--- a/starmath/inc/cursor.hxx
+++ b/starmath/inc/cursor.hxx
@@ -69,6 +69,8 @@ enum SmBracketType {
/** A list of nodes */
typedef std::list<SmNode*> SmNodeList;
+typedef std::list<std::unique_ptr<SmNode>> SmClipboard;
+
class SmDocShell;
/** Formula cursor
@@ -84,7 +86,6 @@ public:
, mpPosition(nullptr)
, mpTree(tree)
, mpDocShell(pShell)
- , mpClipboard(nullptr)
, mnEditSections(0)
, mbIsEnabledSetModifiedSmDocShell(false)
{
@@ -94,7 +95,6 @@ public:
~SmCursor()
{
- SetClipboard();
}
/** Get position */
@@ -231,7 +231,7 @@ private:
/** Graph over caret position in the current tree */
std::unique_ptr<SmCaretPosGraph> mpGraph;
/** Clipboard holder */
- SmNodeList* mpClipboard;
+ SmClipboard maClipboard;
/** Returns a node that is selected, if any could be found */
SmNode* FindSelectedNode(SmNode* pNode);
@@ -281,13 +281,12 @@ private:
return pList;
}
- /** Clone a visual line to a list
+ /** Clone a visual line to a clipboard
*
- * Doesn't clone SmErrorNode's these are ignored, as they are context dependent metadata.
+ * ... but the selected part only.
+ * Doesn't clone SmErrorNodes, which are ignored as they are context dependent metadata.
*/
- static SmNodeList* CloneLineToList(SmStructureNode* pLine,
- bool bOnlyIfSelected = false,
- SmNodeList* pList = new SmNodeList());
+ static void CloneLineToClipboard(SmStructureNode* pLine, SmClipboard* pClipboard);
/** Build pGraph over caret positions */
void BuildGraph();
@@ -304,15 +303,8 @@ private:
/** Set selected on nodes of the tree */
void AnnotateSelection();
- /** Set the clipboard, and release current clipboard
- *
- * Call this method with NULL to reset the clipboard
- * @remarks: This method takes ownership of pList.
- */
- void SetClipboard(SmNodeList* pList = nullptr);
-
- /** Clone list of nodes (creates a deep clone) */
- static SmNodeList* CloneList(SmNodeList* pList);
+ /** Clone list of nodes in a clipboard (creates a deep clone) */
+ static SmNodeList* CloneList(SmClipboard &rClipboard);
/** Find an iterator pointing to the node in pLineList following aCaretPos
*
diff --git a/starmath/source/cursor.cxx b/starmath/source/cursor.cxx
index b70b8edd0791..36bc43d62497 100644
--- a/starmath/source/cursor.cxx
+++ b/starmath/source/cursor.cxx
@@ -1168,67 +1168,52 @@ void SmCursor::Copy(){
assert(pLine);
//Clone selected nodes
- SmNodeList* pList;
+ SmClipboard aClipboard;
if(IsLineCompositionNode(pLine))
- pList = CloneLineToList(static_cast<SmStructureNode*>(pLine), true);
+ CloneLineToClipboard(static_cast<SmStructureNode*>(pLine), &aClipboard);
else{
- pList = new SmNodeList();
//Special care to only clone selected text
if(pLine->GetType() == NTEXT) {
SmTextNode *pText = static_cast<SmTextNode*>(pLine);
- SmTextNode *pClone = new SmTextNode( pText->GetToken(), pText->GetFontDesc() );
+ std::unique_ptr<SmTextNode> pClone(new SmTextNode( pText->GetToken(), pText->GetFontDesc() ));
int start = pText->GetSelectionStart(),
length = pText->GetSelectionEnd() - pText->GetSelectionStart();
pClone->ChangeText(pText->GetText().copy(start, length));
pClone->SetScaleMode(pText->GetScaleMode());
- pList->push_front(pClone);
+ aClipboard.push_front(std::move(pClone));
} else {
SmCloningVisitor aCloneFactory;
- pList->push_front(aCloneFactory.Clone(pLine));
+ aClipboard.push_front(std::unique_ptr<SmNode>(aCloneFactory.Clone(pLine)));
}
}
//Set clipboard
- if (pList->size() > 0)
- SetClipboard(pList);
- else
- delete pList;
+ if (aClipboard.size() > 0)
+ maClipboard = std::move(aClipboard);
}
void SmCursor::Paste() {
BeginEdit();
Delete();
- if(mpClipboard && mpClipboard->size() > 0)
- InsertNodes(CloneList(mpClipboard));
+ if(maClipboard.size() > 0)
+ InsertNodes(CloneList(maClipboard));
EndEdit();
}
-SmNodeList* SmCursor::CloneList(SmNodeList* pList){
+SmNodeList* SmCursor::CloneList(SmClipboard &rClipboard){
SmCloningVisitor aCloneFactory;
SmNodeList* pClones = new SmNodeList();
- SmNodeList::iterator it;
- for(it = pList->begin(); it != pList->end(); ++it){
- SmNode *pClone = aCloneFactory.Clone(*it);
+ for(auto &xNode : rClipboard){
+ SmNode *pClone = aCloneFactory.Clone(xNode.get());
pClones->push_back(pClone);
}
return pClones;
}
-void SmCursor::SetClipboard(SmNodeList* pList){
- if(mpClipboard){
- //Delete all nodes on the clipboard
- SmNodeList::iterator it;
- for(it = mpClipboard->begin(); it != mpClipboard->end(); ++it)
- delete (*it);
- delete mpClipboard;
- }
- mpClipboard = pList;
-}
-
SmNode* SmCursor::FindTopMostNodeInLine(SmNode* pSNode, bool MoveUpIfSelected){
//If we haven't got a subnode
if(!pSNode)
@@ -1288,27 +1273,26 @@ SmNodeList* SmCursor::LineToList(SmStructureNode* pLine, SmNodeList* list){
return list;
}
-SmNodeList* SmCursor::CloneLineToList(SmStructureNode* pLine, bool bOnlyIfSelected, SmNodeList* pList){
+void SmCursor::CloneLineToClipboard(SmStructureNode* pLine, SmClipboard* pClipboard){
SmCloningVisitor aCloneFactory;
SmNodeIterator it(pLine);
while(it.Next()){
if( IsLineCompositionNode( it.Current() ) )
- CloneLineToList( static_cast<SmStructureNode*>(it.Current()), bOnlyIfSelected, pList );
- else if( (!bOnlyIfSelected || it->IsSelected()) && it->GetType() != NERROR ) {
+ CloneLineToClipboard( static_cast<SmStructureNode*>(it.Current()), pClipboard );
+ else if( it->IsSelected() && it->GetType() != NERROR ) {
//Only clone selected text from SmTextNode
if(it->GetType() == NTEXT) {
SmTextNode *pText = static_cast<SmTextNode*>(it.Current());
- SmTextNode *pClone = new SmTextNode( it->GetToken(), pText->GetFontDesc() );
+ std::unique_ptr<SmTextNode> pClone(new SmTextNode( it->GetToken(), pText->GetFontDesc() ));
int start = pText->GetSelectionStart(),
length = pText->GetSelectionEnd() - pText->GetSelectionStart();
pClone->ChangeText(pText->GetText().copy(start, length));
pClone->SetScaleMode(pText->GetScaleMode());
- pList->push_back(pClone);
+ pClipboard->push_back(std::move(pClone));
} else
- pList->push_back(aCloneFactory.Clone(it.Current()));
+ pClipboard->push_back(std::unique_ptr<SmNode>(aCloneFactory.Clone(it.Current())));
}
}
- return pList;
}
bool SmCursor::IsLineCompositionNode(SmNode* pNode){