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

« back to all changes in this revision

Viewing changes to src/DocActionSort.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Vijay(Vijay)
  • Date: 2007-06-14 00:13:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070614001348-z9e2vbgtenb9nhoo
Tags: 1.5.6-0ubuntu1
* New Upstream release 
*  The libgnomevfs2-dev is also added to Build-Depends 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
////////////////////////////////////////////////////////////////////////////
 
2
// NoteCase notes manager project <http://notecase.sf.net>
 
3
//
 
4
// This code is licensed under BSD license.See "license.txt" for more details.
 
5
//
 
6
// File: Implements Node Sort Action
 
7
////////////////////////////////////////////////////////////////////////////
 
8
 
 
9
#include "DocActionSort.h"
 
10
#include "callbacks.h"
 
11
#include "support.h"
 
12
#include "interface.h"
 
13
#include "lib/NoteDocument.h"
 
14
#include "lib/DocumentIterator.h"
 
15
#include "TextView.h"
 
16
#include "TreeView.h"
 
17
#include "lib/debug.h"
 
18
#include <algorithm>
 
19
 
 
20
#ifdef _WIN32
 
21
 #include <io.h>
 
22
 #ifndef __MINGW32__
 
23
  #define strcasecmp stricmp
 
24
 #endif
 
25
#endif
 
26
 
 
27
extern NoteDocument g_doc;
 
28
extern GtkWidget *window1;
 
29
extern TextView g_text;
 
30
extern TreeView g_tree;
 
31
 
 
32
class SortComparator{
 
33
public:
 
34
        bool operator()(const int &a, const int &b)
 
35
        {
 
36
                //operator < (is a<b ?)
 
37
                bool bRes = (strcasecmp(m_pDoc->GetNodeByIdx(a).GetTitle().c_str(), m_pDoc->GetNodeByIdx(b).GetTitle().c_str()) < 0);
 
38
                if(!m_bAscending)
 
39
                        bRes = !bRes;
 
40
                return bRes;
 
41
        };
 
42
 
 
43
        NoteDocument *m_pDoc;
 
44
        bool m_bAscending;
 
45
};
 
46
 
 
47
DocActionSort::DocActionSort()
 
48
{
 
49
        m_nNodeIdx              = -1;
 
50
        m_bAscending    = true;
 
51
        //m_bRecursive  = false;
 
52
}
 
53
 
 
54
DocActionSort::~DocActionSort()
 
55
{
 
56
}
 
57
 
 
58
void DocActionSort::Exec(bool bInteractive)
 
59
{
 
60
        DocumentIterator it(*m_pDoc);
 
61
 
 
62
        UpdateTextFromScreen();
 
63
        
 
64
        int nParentID = -1;
 
65
        if(m_nNodeIdx >= 0)
 
66
                nParentID = m_pDoc->GetNodeByIdx(m_nNodeIdx).m_nID;
 
67
 
 
68
        int nChildCnt = it.GetChildCount(nParentID);
 
69
 
 
70
        //sort a list of indices
 
71
        int i;
 
72
        if(m_lstOldIdxOrder.size() < 1)
 
73
        {
 
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));
 
77
                }
 
78
 
 
79
                m_lstNewIdxOrder = m_lstOldIdxOrder;
 
80
 
 
81
                SortComparator cmp;
 
82
                cmp.m_pDoc = m_pDoc;
 
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]);
 
87
        }
 
88
 
 
89
        GtkWidget *treeview = lookup_widget(window1, "treeview1");
 
90
        GtkTreeModel *model = gtk_tree_view_get_model((GtkTreeView *)treeview);
 
91
 
 
92
        // now use that to sort the document model 
 
93
        for(i=0; i<nChildCnt; i++)
 
94
        {
 
95
                int nOldIdx = it.GetChildIdx(nParentID, i);
 
96
                int nNewIdx = m_lstNewIdxOrder[i];
 
97
                if(nOldIdx != nNewIdx)
 
98
                {
 
99
                        NoteNode &node1 = m_pDoc->GetNodeByIdx(nOldIdx);
 
100
                        NoteNode &node2 = m_pDoc->GetNodeByIdx(nNewIdx);
 
101
 
 
102
                        int nTmp = node1.m_nSiblingIdx;
 
103
                        node1.m_nSiblingIdx = node2.m_nSiblingIdx;
 
104
                        node2.m_nSiblingIdx = nTmp;
 
105
 
 
106
                        //swap two nodes in store
 
107
                        GtkTreeIter iter, iter2;
 
108
                        if( IteratorFromNodeIdx(nOldIdx, iter) &&
 
109
                                IteratorFromNodeIdx(nNewIdx, iter2))
 
110
                        {
 
111
                                gtk_tree_store_swap(GTK_TREE_STORE(model), &iter, &iter2); //BUG crashes!
 
112
                        }
 
113
                }
 
114
        }
 
115
}
 
116
 
 
117
void DocActionSort::Undo()
 
118
{
 
119
        DocumentIterator it(*m_pDoc);
 
120
        
 
121
        int nParentID = -1;
 
122
        if(m_nNodeIdx >= 0)
 
123
                nParentID = m_pDoc->GetNodeByIdx(m_nNodeIdx).m_nID;
 
124
 
 
125
        int nChildCnt = it.GetChildCount(nParentID);
 
126
 
 
127
        GtkWidget *treeview = lookup_widget(window1, "treeview1");
 
128
        GtkTreeModel *model = gtk_tree_view_get_model((GtkTreeView *)treeview);
 
129
 
 
130
        // now use that to sort the document model 
 
131
        for(int i=0; i<nChildCnt; i++)
 
132
        {
 
133
                int nOldIdx = it.GetChildIdx(nParentID, i);
 
134
                int nNewIdx = m_lstOldIdxOrder[i];
 
135
                if(nOldIdx != nNewIdx)
 
136
                {
 
137
                        NoteNode &node1 = m_pDoc->GetNodeByIdx(nOldIdx);
 
138
                        NoteNode &node2 = m_pDoc->GetNodeByIdx(nNewIdx);
 
139
 
 
140
                        int nTmp = node1.m_nSiblingIdx;
 
141
                        node1.m_nSiblingIdx = node2.m_nSiblingIdx;
 
142
                        node2.m_nSiblingIdx = nTmp;
 
143
 
 
144
                        //swap two nodes in store
 
145
                        GtkTreeIter iter, iter2;
 
146
                        if( IteratorFromNodeIdx(nOldIdx, iter) &&
 
147
                                IteratorFromNodeIdx(nNewIdx, iter2))
 
148
                        {
 
149
                                gtk_tree_store_swap(GTK_TREE_STORE(model), &iter, &iter2); //BUG crashes!
 
150
                        }
 
151
                }
 
152
        }
 
153
}
 
154