1
////////////////////////////////////////////////////////////////////////////
2
// NoteCase notes manager project <http://notecase.sf.net>
4
// This code is licensed under BSD license.See "license.txt" for more details.
6
// File: Implements Node Sort Action
7
////////////////////////////////////////////////////////////////////////////
9
#include "DocActionSort.h"
10
#include "callbacks.h"
12
#include "interface.h"
13
#include "lib/NoteDocument.h"
14
#include "lib/DocumentIterator.h"
17
#include "lib/debug.h"
23
#define strcasecmp stricmp
27
extern NoteDocument g_doc;
28
extern GtkWidget *window1;
29
extern TextView g_text;
30
extern TreeView g_tree;
34
bool operator()(const int &a, const int &b)
36
//operator < (is a<b ?)
37
bool bRes = (strcasecmp(m_pDoc->GetNodeByIdx(a).GetTitle().c_str(), m_pDoc->GetNodeByIdx(b).GetTitle().c_str()) < 0);
47
DocActionSort::DocActionSort()
51
//m_bRecursive = false;
54
DocActionSort::~DocActionSort()
58
void DocActionSort::Exec(bool bInteractive)
60
DocumentIterator it(*m_pDoc);
62
UpdateTextFromScreen();
66
nParentID = m_pDoc->GetNodeByIdx(m_nNodeIdx).m_nID;
68
int nChildCnt = it.GetChildCount(nParentID);
70
//sort a list of indices
72
if(m_lstOldIdxOrder.size() < 1)
74
for(i=0; i<nChildCnt; i++){
75
//TRACE("Before %d: %d\n", i, it.GetChildIdx(nParentID, i));
76
m_lstOldIdxOrder.push_back(it.GetChildIdx(nParentID, i));
79
m_lstNewIdxOrder = m_lstOldIdxOrder;
83
cmp.m_bAscending = m_bAscending;
84
std::sort(m_lstNewIdxOrder.begin(), m_lstNewIdxOrder.end(), cmp);
85
//for(i=0; i<nChildCnt; i++)
86
// TRACE("After %d: %d\n", i, lstIdxOrder[i]);
89
GtkWidget *treeview = lookup_widget(window1, "treeview1");
90
GtkTreeModel *model = gtk_tree_view_get_model((GtkTreeView *)treeview);
92
// now use that to sort the document model
93
for(i=0; i<nChildCnt; i++)
95
int nOldIdx = it.GetChildIdx(nParentID, i);
96
int nNewIdx = m_lstNewIdxOrder[i];
97
if(nOldIdx != nNewIdx)
99
NoteNode &node1 = m_pDoc->GetNodeByIdx(nOldIdx);
100
NoteNode &node2 = m_pDoc->GetNodeByIdx(nNewIdx);
102
int nTmp = node1.m_nSiblingIdx;
103
node1.m_nSiblingIdx = node2.m_nSiblingIdx;
104
node2.m_nSiblingIdx = nTmp;
106
//swap two nodes in store
107
GtkTreeIter iter, iter2;
108
if( IteratorFromNodeIdx(nOldIdx, iter) &&
109
IteratorFromNodeIdx(nNewIdx, iter2))
111
gtk_tree_store_swap(GTK_TREE_STORE(model), &iter, &iter2); //BUG crashes!
117
void DocActionSort::Undo()
119
DocumentIterator it(*m_pDoc);
123
nParentID = m_pDoc->GetNodeByIdx(m_nNodeIdx).m_nID;
125
int nChildCnt = it.GetChildCount(nParentID);
127
GtkWidget *treeview = lookup_widget(window1, "treeview1");
128
GtkTreeModel *model = gtk_tree_view_get_model((GtkTreeView *)treeview);
130
// now use that to sort the document model
131
for(int i=0; i<nChildCnt; i++)
133
int nOldIdx = it.GetChildIdx(nParentID, i);
134
int nNewIdx = m_lstOldIdxOrder[i];
135
if(nOldIdx != nNewIdx)
137
NoteNode &node1 = m_pDoc->GetNodeByIdx(nOldIdx);
138
NoteNode &node2 = m_pDoc->GetNodeByIdx(nNewIdx);
140
int nTmp = node1.m_nSiblingIdx;
141
node1.m_nSiblingIdx = node2.m_nSiblingIdx;
142
node2.m_nSiblingIdx = nTmp;
144
//swap two nodes in store
145
GtkTreeIter iter, iter2;
146
if( IteratorFromNodeIdx(nOldIdx, iter) &&
147
IteratorFromNodeIdx(nNewIdx, iter2))
149
gtk_tree_store_swap(GTK_TREE_STORE(model), &iter, &iter2); //BUG crashes!