~ubuntu-branches/ubuntu/karmic/notecase/karmic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
////////////////////////////////////////////////////////////////////////////
// NoteCase notes manager project <http://notecase.sf.net>
//
// This code is licensed under BSD license.See "license.txt" for more details.
//
// File: Implements Node Sort Action
////////////////////////////////////////////////////////////////////////////

#include "DocActionSort.h"
#include "callbacks.h"
#include "support.h"
#include "interface.h"
#include "lib/NoteDocument.h"
#include "lib/DocumentIterator.h"
#include "TextView.h"
#include "TreeView.h"
#include "lib/debug.h"
#include <algorithm>

#ifdef _WIN32
 #include <io.h>
 #ifndef __MINGW32__
  #define strcasecmp stricmp
 #endif
#endif

extern NoteDocument g_doc;
extern GtkWidget *window1;
extern TextView g_text;
extern TreeView g_tree;

class SortComparator{
public:
	bool operator()(const int &a, const int &b)
	{
		//operator < (is a<b ?)
		bool bRes = (strcasecmp(m_pDoc->GetNodeByIdx(a).GetTitle().c_str(), m_pDoc->GetNodeByIdx(b).GetTitle().c_str()) < 0);
		if(!m_bAscending)
			bRes = !bRes;
		return bRes;
	};

	NoteDocument *m_pDoc;
	bool m_bAscending;
};

DocActionSort::DocActionSort()
{
	m_nNodeIdx		= -1;
	m_bAscending	= true;
	//m_bRecursive	= false;
}

DocActionSort::~DocActionSort()
{
}

void DocActionSort::Exec(bool bInteractive)
{
	DocumentIterator it(*m_pDoc);

	UpdateTextFromScreen();
	
	int nParentID = -1;
	if(m_nNodeIdx >= 0)
		nParentID = m_pDoc->GetNodeByIdx(m_nNodeIdx).m_nID;

	int nChildCnt = it.GetChildCount(nParentID);

	//sort a list of indices
	int i;
	if(m_lstOldIdxOrder.size() < 1)
	{
		for(i=0; i<nChildCnt; i++){
			//TRACE("Before %d: %d\n", i, it.GetChildIdx(nParentID, i));
			m_lstOldIdxOrder.push_back(it.GetChildIdx(nParentID, i));
		}

		m_lstNewIdxOrder = m_lstOldIdxOrder;

		SortComparator cmp;
		cmp.m_pDoc = m_pDoc;
		cmp.m_bAscending = m_bAscending;
		std::sort(m_lstNewIdxOrder.begin(), m_lstNewIdxOrder.end(), cmp);
		//for(i=0; i<nChildCnt; i++)
		//	TRACE("After %d: %d\n", i, lstIdxOrder[i]);
	}

	GtkWidget *treeview = lookup_widget(window1, "treeview1");
	GtkTreeModel *model = gtk_tree_view_get_model((GtkTreeView *)treeview);

	// now use that to sort the document model 
	for(i=0; i<nChildCnt; i++)
	{
		int nOldIdx = it.GetChildIdx(nParentID, i);
		int nNewIdx = m_lstNewIdxOrder[i];
		if(nOldIdx != nNewIdx)
		{
			NoteNode &node1 = m_pDoc->GetNodeByIdx(nOldIdx);
			NoteNode &node2 = m_pDoc->GetNodeByIdx(nNewIdx);

			int nTmp = node1.m_nSiblingIdx;
			node1.m_nSiblingIdx = node2.m_nSiblingIdx;
			node2.m_nSiblingIdx = nTmp;

			//swap two nodes in store
			GtkTreeIter iter, iter2;
			if( IteratorFromNodeIdx(nOldIdx, iter) &&
				IteratorFromNodeIdx(nNewIdx, iter2))
			{
				gtk_tree_store_swap(GTK_TREE_STORE(model), &iter, &iter2); //BUG crashes!
			}
		}
	}
}

void DocActionSort::Undo()
{
	DocumentIterator it(*m_pDoc);
	
	int nParentID = -1;
	if(m_nNodeIdx >= 0)
		nParentID = m_pDoc->GetNodeByIdx(m_nNodeIdx).m_nID;

	int nChildCnt = it.GetChildCount(nParentID);

	GtkWidget *treeview = lookup_widget(window1, "treeview1");
	GtkTreeModel *model = gtk_tree_view_get_model((GtkTreeView *)treeview);

	// now use that to sort the document model 
	for(int i=0; i<nChildCnt; i++)
	{
		int nOldIdx = it.GetChildIdx(nParentID, i);
		int nNewIdx = m_lstOldIdxOrder[i];
		if(nOldIdx != nNewIdx)
		{
			NoteNode &node1 = m_pDoc->GetNodeByIdx(nOldIdx);
			NoteNode &node2 = m_pDoc->GetNodeByIdx(nNewIdx);

			int nTmp = node1.m_nSiblingIdx;
			node1.m_nSiblingIdx = node2.m_nSiblingIdx;
			node2.m_nSiblingIdx = nTmp;

			//swap two nodes in store
			GtkTreeIter iter, iter2;
			if( IteratorFromNodeIdx(nOldIdx, iter) &&
				IteratorFromNodeIdx(nNewIdx, iter2))
			{
				gtk_tree_store_swap(GTK_TREE_STORE(model), &iter, &iter2); //BUG crashes!
			}
		}
	}
}