~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/plugins/contrib/BrowseTracker/BrowseTrackerLayout.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "BrowseTrackerLayout.h"
 
2
/*
 
3
        This file is part of Browse Tracker, a plugin for Code::Blocks
 
4
        Copyright (C) 2007 Pecan Heber
 
5
 
 
6
        This program is free software; you can redistribute it and/or
 
7
        modify it under the terms of the GNU General Public License
 
8
        as published by the Free Software Foundation; either version 2
 
9
        of the License, or (at your option) any later version.
 
10
 
 
11
        This program is distributed in the hope that it will be useful,
 
12
        but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
        GNU General Public License for more details.
 
15
 
 
16
        You should have received a copy of the GNU General Public License
 
17
        along with this program; if not, write to the Free Software
 
18
        Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
*/
 
20
// RCS-ID: $Id: BrowseTrackerLayout.cpp 24 2008-01-02 15:19:04Z Pecan $
 
21
 
 
22
/*
 
23
* This file is part of Code::Bocks, an open-source cross-platform IDE
 
24
* Copyright (C) 2003  Yiannis An. Mandravellos
 
25
*
 
26
* This program is distributed under the terms of the GNU General Public License as published by
 
27
* the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 
28
*
 
29
* $Revision: 4606 $
 
30
* $Id: BrowseTrackerLayout.cpp 24 2008-01-02 15:19:04Z Pecan $
 
31
* $HeadURL: https://svn.berlios.de/svnroot/repos/codeblocks/trunk/src/sdk/BrowseTrackerLayout.cpp $
 
32
*/
 
33
 
 
34
#include "sdk_precomp.h"
 
35
 
 
36
#ifndef CB_PRECOMP
 
37
    #include <wx/confbase.h>
 
38
    #include <wx/fileconf.h>
 
39
    #include <wx/intl.h>
 
40
    #include "manager.h"
 
41
    #include "projectmanager.h"
 
42
    #include "logmanager.h"
 
43
    #include "editormanager.h"
 
44
    #include "cbeditor.h"
 
45
    #include "cbproject.h"
 
46
#endif
 
47
 
 
48
#include <wx/tokenzr.h>
 
49
#include "tinyxml/tinyxml.h"
 
50
#include "tinyxml/tinywxuni.h"
 
51
 
 
52
#include "BrowseTrackerLayout.h"
 
53
#include "BrowseMarks.h"
 
54
#include "Version.h"
 
55
 
 
56
// ----------------------------------------------------------------------------
 
57
BrowseTrackerLayout::BrowseTrackerLayout(cbProject* project)
 
58
// ----------------------------------------------------------------------------
 
59
    : m_pProject(project)
 
60
{
 
61
        //ctor
 
62
}
 
63
 
 
64
// ----------------------------------------------------------------------------
 
65
BrowseTrackerLayout::~BrowseTrackerLayout()
 
66
// ----------------------------------------------------------------------------
 
67
{
 
68
        //dtor
 
69
}
 
70
 
 
71
// IMPORTANT! We have to be careful of what to unicode and what not to.
 
72
// TinyXML must use NON-unicode strings!
 
73
// ----------------------------------------------------------------------------
 
74
bool BrowseTrackerLayout::Open(const wxString& filename, FileBrowse_MarksHash& m_FileBrowse_MarksArchive , FileBrowse_MarksHash& m_EdBook_MarksArchive )
 
75
// ----------------------------------------------------------------------------
 
76
{
 
77
    TiXmlDocument doc;
 
78
    if (!TinyXML::LoadDocument(filename, &doc))
 
79
        return false;
 
80
 
 
81
    ProjectManager* pMan = Manager::Get()->GetProjectManager();
 
82
    LogManager* pMsg = Manager::Get()->GetLogManager();
 
83
    if (!pMan || !pMsg)
 
84
        return false;
 
85
 
 
86
    TiXmlElement* root;
 
87
    TiXmlElement* elem;
 
88
    wxString fname;
 
89
    ProjectFile* pf;
 
90
 
 
91
 
 
92
    root = doc.FirstChildElement("BrowseTracker_layout_file");
 
93
    if (!root)
 
94
    {
 
95
        // old tag
 
96
        root = doc.FirstChildElement("BrowseTracker_layout_file");
 
97
        if (!root)
 
98
        {
 
99
            pMsg->DebugLog(_T("Not a valid BrowseTracker layout file..."));
 
100
            return false;
 
101
        }
 
102
    }
 
103
 
 
104
    elem = root->FirstChildElement("ActiveTarget");
 
105
    if (elem)
 
106
    {
 
107
        if (elem->Attribute("name"))
 
108
            ;//m_pProject->SetActiveBuildTarget(cbC2U(elem->Attribute("name")));
 
109
    }
 
110
 
 
111
    elem = root->FirstChildElement("File");
 
112
    if (!elem)
 
113
    {
 
114
        //pMsg->DebugLog(_T("No 'File' element in file..."));
 
115
        return false;
 
116
    }
 
117
 
 
118
    while (elem)
 
119
    {
 
120
        //pMsg->DebugLog(elem->Value());
 
121
        fname = cbC2U(elem->Attribute("name"));
 
122
        if (fname.IsEmpty())
 
123
        {
 
124
            //pMsg->DebugLog(_T("'File' node exists, but no filename?!?"));
 
125
            pf = 0L;
 
126
        }
 
127
        else
 
128
            pf = m_pProject->GetFileByFilename(fname);
 
129
 
 
130
        if (pf)
 
131
        {
 
132
            //pf->editorOpen = false;
 
133
            //pf->editorPos = 0;
 
134
            //pf->editorTopLine = 0;
 
135
            int open = 0;
 
136
            int top = 0;
 
137
            int tabpos = 0;
 
138
            if (elem->QueryIntAttribute("open", &open) == TIXML_SUCCESS)
 
139
                ;//pf->editorOpen = open != 0;
 
140
            if (elem->QueryIntAttribute("top", &top) == TIXML_SUCCESS)
 
141
            {
 
142
                if(top)
 
143
                    m_TopProjectFile = pf;
 
144
            }
 
145
            if (elem->QueryIntAttribute("tabpos", &tabpos) == TIXML_SUCCESS)
 
146
                                ;//pf->editorTabPos = tabpos;
 
147
 
 
148
            TiXmlElement* cursor = elem->FirstChildElement();
 
149
            if (cursor)
 
150
            {
 
151
                int pos = 0;
 
152
                int topline = 0;
 
153
                if (cursor->QueryIntAttribute("position", &pos) == TIXML_SUCCESS)
 
154
                    ;//pf->editorPos = pos;
 
155
                if (cursor->QueryIntAttribute("topLine", &topline) == TIXML_SUCCESS)
 
156
                    ;//pf->editorTopLine = topline;
 
157
            }
 
158
 
 
159
            #if defined(LOGGING)
 
160
            ///LOGIT( _T("Open Layout processing for[%s]"),fname.c_str() );
 
161
            #endif
 
162
 
 
163
            TiXmlElement* browsemarks = cursor->NextSiblingElement("BrowseMarks");
 
164
            ///if (not browsemarks)
 
165
            ///    LOGIT( _T("OPEN LAYOUT failed for BrowseMarks") );
 
166
            if (browsemarks)
 
167
            {
 
168
                wxString marksString = cbC2U(browsemarks->Attribute("positions"));
 
169
                #if defined(LOGGING)
 
170
                ////LOGIT( _T("OPEN_LAYOUT BROWSEMarksStrng[%s][%s]"), fname.c_str(), marksString.c_str() );
 
171
                #endif
 
172
                ParseBrowse_MarksString( fname, marksString, m_FileBrowse_MarksArchive );
 
173
            }
 
174
 
 
175
            TiXmlElement* bookmarks = cursor->NextSiblingElement("Book_Marks");
 
176
            ///if (not bookmarks)
 
177
            ///    LOGIT( _T("OPEN LAYOUT failed for Book_Marks") );
 
178
            if (bookmarks)
 
179
            {
 
180
                wxString marksString = cbC2U(bookmarks->Attribute("positions"));
 
181
                #if defined(LOGGING)
 
182
                ////LOGIT( _T("OPEN_LAYOUT BOOKMarksStrng[%s][%s]"), fname.c_str(), marksString.c_str() );
 
183
                #endif
 
184
                ParseBrowse_MarksString( fname, marksString, m_EdBook_MarksArchive );
 
185
            }
 
186
        }
 
187
 
 
188
        elem = elem->NextSiblingElement();
 
189
    }
 
190
 
 
191
    return true;
 
192
}//Open
 
193
// ----------------------------------------------------------------------------
 
194
bool BrowseTrackerLayout::ParseBrowse_MarksString(const wxString& filename, wxString BrowseMarksString, FileBrowse_MarksHash& m_EdMarksArchive)
 
195
// ----------------------------------------------------------------------------
 
196
{
 
197
    if (filename.IsEmpty()) return false;
 
198
    if ( BrowseMarksString.IsEmpty() ) return false;
 
199
    ProjectFile* pf = m_pProject->GetFileByFilename( filename, true, true);
 
200
    if ( not pf ) return false;
 
201
    wxString filenamePath = pf->file.GetFullPath();
 
202
 
 
203
   // parse the comma delimited string
 
204
    BrowseMarks*  pEdPosnArchive = new BrowseMarks(filenamePath );
 
205
    wxStringTokenizer tkz(BrowseMarksString, wxT(","));
 
206
    while ( tkz.HasMoreTokens() )
 
207
    {   long longnum;
 
208
        tkz.GetNextToken().ToLong(&longnum);
 
209
        pEdPosnArchive->RecordMark(longnum);
 
210
    }//while
 
211
    m_EdMarksArchive[filenamePath] = pEdPosnArchive;
 
212
    return true;
 
213
}//ParseBrowse_MarksString
 
214
// ----------------------------------------------------------------------------
 
215
bool BrowseTrackerLayout::Save(const wxString& filename, FileBrowse_MarksHash& m_FileBrowse_MarksArchive, FileBrowse_MarksHash& m_EdBook_MarksArchive)
 
216
// ----------------------------------------------------------------------------
 
217
{
 
218
    ////DumpBrowse_Marks(wxT("BookMarks"), m_FileBrowse_MarksArchive, m_EdBook_MarksArchive);
 
219
 
 
220
    const char* ROOT_TAG = "BrowseTracker_layout_file";
 
221
 
 
222
    TiXmlDocument doc;
 
223
    doc.SetCondenseWhiteSpace(false);
 
224
    doc.InsertEndChild(TiXmlDeclaration("1.0", "UTF-8", "yes"));
 
225
    TiXmlElement* rootnode = static_cast<TiXmlElement*>(doc.InsertEndChild(TiXmlElement(ROOT_TAG)));
 
226
    if (!rootnode)
 
227
        return false;
 
228
 
 
229
    TiXmlElement* tgtidx = static_cast<TiXmlElement*>(rootnode->InsertEndChild(TiXmlElement("ActiveTarget")));
 
230
    tgtidx->SetAttribute("name", cbU2C(m_pProject->GetActiveBuildTarget()));
 
231
 
 
232
        ProjectFile* active = 0L;
 
233
    cbEditor* ed = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
 
234
        if (ed)
 
235
                active = ed->GetProjectFile();
 
236
 
 
237
        int count = m_pProject->GetFilesCount();
 
238
        for (int i = 0; i < count; ++i)
 
239
        {
 
240
                ProjectFile* f = m_pProject->GetFile(i);
 
241
 
 
242
                if (f->editorOpen || f->editorPos || f->editorTopLine || f->editorTabPos)
 
243
                {
 
244
            TiXmlElement* node = static_cast<TiXmlElement*>(rootnode->InsertEndChild(TiXmlElement("File")));
 
245
            node->SetAttribute("name", cbU2C(f->relativeFilename));
 
246
            node->SetAttribute("open", f->editorOpen);
 
247
            node->SetAttribute("top", (f == active));
 
248
            node->SetAttribute("tabpos", f->editorTabPos);
 
249
 
 
250
            TiXmlElement* cursor = static_cast<TiXmlElement*>(node->InsertEndChild(TiXmlElement("Cursor")));
 
251
            cursor->SetAttribute("position", f->editorPos);
 
252
            cursor->SetAttribute("topLine", f->editorTopLine);
 
253
 
 
254
            EditorBase* eb = 0;
 
255
            // write out a string of browse mark positions
 
256
            #if defined(LOGGING)
 
257
            ////LOGIT( _T("ProjectFilename[%s]"),f->file.GetFullPath().c_str() );
 
258
            #endif
 
259
 
 
260
            eb = Manager::Get()->GetEditorManager()->GetEditor(f->file.GetFullPath());
 
261
            #if defined(LOGGING)
 
262
            ////if (eb) LOGIT( _T("EditorBase Filename[%d][%s]"), i, eb->GetFilename().c_str() );
 
263
            #endif
 
264
            ////if(eb) if (f->file.GetFullPath() != eb->GetFilename())
 
265
            ////{
 
266
            ////    #if defined(LOGGING)
 
267
            ////    LOGIT( _T("NAME MISSMATCH ProjectFile[%s]EditorBase[%s]"), f->file.GetFullPath().c_str(), eb->GetFilename().c_str() );
 
268
            ////    #endif
 
269
            ////}
 
270
 
 
271
////            #if defined(LOGGING)
 
272
////            if (m_FileBrowse_MarksArchive.find(eb) != m_FileBrowse_MarksArchive.end() )
 
273
////                LOGIT( _T("Found eb[%p][%s]"), eb, eb->GetShortName().c_str() );
 
274
////            else{
 
275
////                int i = 0;
 
276
////                for (EbBrowse_MarksHash::iterator it = m_FileBrowse_MarksArchive.begin();
 
277
////                        it != m_FileBrowse_MarksArchive.end(); ++it)
 
278
////                {
 
279
////                    #if defined(LOGGING)
 
280
////                    LOGIT( _T("m_FileBrowse_MarksArchive[i][%d][%p]"), i, it->first );
 
281
////                    #endif
 
282
////                    ++i;
 
283
////                }
 
284
////            }
 
285
////            #endif
 
286
            #if defined(LOGGING)
 
287
            ////LOGIT( _T("Layout processing for[%s]"),/*f->relativeFilename.c_str(),*/ f->file.GetFullPath().c_str() );
 
288
            #endif
 
289
            // Save the BrowseMarks
 
290
            FileBrowse_MarksHash::iterator it = m_FileBrowse_MarksArchive.find(f->file.GetFullPath());
 
291
            if (it != m_FileBrowse_MarksArchive.end() ) do
 
292
            {
 
293
                BrowseMarks* pBrowse_Marks = it->second;
 
294
                if (not pBrowse_Marks) break;
 
295
                wxString browseMarks = pBrowse_Marks->GetStringOfBrowse_Marks();
 
296
                #if defined(LOGGING)
 
297
                ////LOGIT( _T("Layout writing BROWSEMarkString [%p]is[%s]"), pBrowse_Marks, browseMarks.c_str());
 
298
                #endif
 
299
                TiXmlElement* btMarks = static_cast<TiXmlElement*>(node->InsertEndChild(TiXmlElement("BrowseMarks")));
 
300
                btMarks->SetAttribute("positions", cbU2C(browseMarks));
 
301
            }while(0);
 
302
            ////else{
 
303
            ////    #if defined(LOGGING)
 
304
            ////    LOGIT( _T("Browse_Marks failed find for[%s]"), f->file.GetFullPath().c_str() );
 
305
            ////    #endif
 
306
            ////}
 
307
            // Save the Book_Marks
 
308
            it = m_EdBook_MarksArchive.find(f->file.GetFullPath());
 
309
            if (it != m_EdBook_MarksArchive.end() ) do
 
310
            {
 
311
                BrowseMarks* pBook_Marks = it->second;
 
312
                if (not pBook_Marks) break;
 
313
                wxString bookMarks = pBook_Marks->GetStringOfBrowse_Marks();
 
314
                #if defined(LOGGING)
 
315
                ////LOGIT( _T("Layout writing BOOKMarkString [%p]is[%s]"), pBook_Marks, bookMarks.c_str());
 
316
                #endif
 
317
                TiXmlElement* btMarks = static_cast<TiXmlElement*>(node->InsertEndChild(TiXmlElement("Book_Marks")));
 
318
                btMarks->SetAttribute("positions", cbU2C(bookMarks));
 
319
            }while(0);
 
320
            ////else{
 
321
            ////    #if defined(LOGGING)
 
322
            ////    LOGIT( _T("Book_Marks failed find for[%s]"), f->file.GetFullPath().c_str() );
 
323
            ////    #endif
 
324
            ////}
 
325
 
 
326
                }
 
327
        }//for
 
328
 
 
329
        const wxArrayString& en = m_pProject->ExpandedNodes();
 
330
        for (unsigned int i = 0; i < en.GetCount(); ++i)
 
331
        {
 
332
                if (!en[i].IsEmpty())
 
333
                {
 
334
            TiXmlElement* node = static_cast<TiXmlElement*>(rootnode->InsertEndChild(TiXmlElement("Expand")));
 
335
            node->SetAttribute("folder", cbU2C(en[i]));
 
336
                }
 
337
        }
 
338
    return cbSaveTinyXMLDocument(&doc, filename);
 
339
}
 
340
// ----------------------------------------------------------------------------
 
341
void BrowseTrackerLayout::DumpBrowse_Marks( const wxString hashType, FileBrowse_MarksHash& m_FileBrowse_MarksArchive, FileBrowse_MarksHash& m_FileBook_MarksArchive )
 
342
// ----------------------------------------------------------------------------
 
343
{
 
344
    #if defined(LOGGING)
 
345
    LOGIT( _T("--- DumpBrowseData ---[%s]"), hashType.c_str()  );
 
346
 
 
347
    FileBrowse_MarksHash* phash = &m_FileBrowse_MarksArchive;
 
348
    if ( hashType == wxT("BookMarks") )
 
349
        phash = &m_FileBook_MarksArchive;
 
350
    FileBrowse_MarksHash& hash = *phash;
 
351
    #if defined(LOGGING)
 
352
    LOGIT( _T("Dump_%s Size[%d]"), hashType.c_str(), hash.size() );
 
353
    #endif
 
354
    for (FileBrowse_MarksHash::iterator it = hash.begin(); it != hash.end(); ++it)
 
355
    {
 
356
        wxString filename = it->first;
 
357
        BrowseMarks* p = it->second;
 
358
        LOGIT( _T("Filename[%s]%s*[%p]name[%s]"), filename.c_str(), hashType.c_str(), p, p->GetFilePath().c_str() );
 
359
        if (p)
 
360
        {   //dump the browse marks
 
361
            p->Dump();
 
362
        }
 
363
    }
 
364
 
 
365
    #endif
 
366
}
 
367
// ----------------------------------------------------------------------------