root/trunk/ElephantDoc.cpp

Revision 5, 8.1 kB (checked in by qbert, 6 years ago)

Initial ( and last :( ) commit

Line 
1 // ElephantDoc.cpp : implementation of the CElephantDoc class
2 //
3
4 #include "stdafx.h"
5 #include "Elephant.h"
6
7 #include "ElephantDoc.h"
8 #include "ElephantView.h"
9
10 #ifdef _DEBUG
11 #define new DEBUG_NEW
12 #undef THIS_FILE
13 static char THIS_FILE[] = __FILE__;
14 #endif
15
16 /////////////////////////////////////////////////////////////////////////////
17 // CElephantDoc
18
19 IMPLEMENT_DYNCREATE(CElephantDoc, CScintillaDoc)
20
21 BEGIN_MESSAGE_MAP(CElephantDoc, CScintillaDoc)
22 //{{AFX_MSG_MAP(CElephantDoc)
23 ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
24 ON_COMMAND(ID_FILE_SAVE_SAVEFILE, OnFileSaveSavefile)
25     ON_COMMAND(ID_FILE_CLOSE, OnFileClose)
26     //}}AFX_MSG_MAP
27 END_MESSAGE_MAP()
28
29 /////////////////////////////////////////////////////////////////////////////
30 // CElephantDoc construction/destruction
31
32 CElephantDoc::CElephantDoc()
33 {
34 }
35
36
37 CElephantDoc::~CElephantDoc()
38 {
39 }
40 //
41 //BOOL CElephantDoc::OnNewDocument()
42 //{
43 //  if (!CDocument::OnNewDocument())
44 //      return FALSE;
45 //
46 //  return TRUE;
47 //}
48
49
50
51 /////////////////////////////////////////////////////////////////////////////
52 // CElephantDoc serialization
53
54 void CElephantDoc::Serialize(CArchive& ar)
55 {
56     if (ar.IsStoring())
57     {
58     }
59     else
60     {
61     }
62 }
63
64 /////////////////////////////////////////////////////////////////////////////
65 // CElephantDoc diagnostics
66
67 #ifdef _DEBUG
68 void CElephantDoc::AssertValid() const
69 {
70     CDocument::AssertValid();
71 }
72
73 void CElephantDoc::Dump(CDumpContext& dc) const
74 {
75     CDocument::Dump(dc);
76 }
77 #endif //_DEBUG
78
79 /////////////////////////////////////////////////////////////////////////////
80 // CElephantDoc commands
81
82 BOOL CElephantDoc::OnOpenDocument(LPCTSTR lpszPathName)
83 {
84    
85    
86     if (!CScintillaDoc::OnOpenDocument(lpszPathName))
87         return FALSE;
88     if ( !ScintillaOpen(lpszPathName) )
89     {
90         string err = "Error opening document with path :";
91         err += lpszPathName;
92         ElephantMessageBox(err.c_str());
93     }
94    
95     return TRUE;
96 }
97
98
99 bool CElephantDoc::ScintillaOpen(LPCTSTR filename)
100 {
101    
102    
103     CScintillaCtrl& rCtrl  = GetCtrl();
104     rCtrl.SetUndoCollection(false);
105     rCtrl.ClearAll();
106    
107     CElephantFile file;
108     if ( file.Open(filename, CElephantFile::modeRead | CElephantFile::modeBinary) )
109     {
110         // Disable UNDO
111        
112         char data[blockSize];
113         int lenFile = file.Read(data, sizeof(data));
114        
115         EElephantSaveFormat endings = determineLineEndings(data, lenFile);
116        
117         ///@todo otherwise set user's code page... (int bomLength =)
118         determineEncoding(reinterpret_cast<unsigned char*>(data), lenFile, rCtrl.m_encType);
119         if(rCtrl.m_encType != eUnknown)
120         {
121             // We do a Unicode-friendly read for unicode files...
122             rCtrl.SetCodePage(SC_CP_UTF8);
123             Utf8_16_Read converter;
124            
125             while (lenFile > 0)
126             {
127                 lenFile = converter.convert(data, lenFile);
128                 rCtrl.AddText( lenFile, converter.getNewBuf());
129                 lenFile = file.Read(data, sizeof(data));
130             }
131         }
132         else
133         {
134             // Otherwise we do a simple read.
135             while (lenFile > 0)
136             {
137                 rCtrl.AddText(lenFile, data);
138                 lenFile = file.Read(data, sizeof(data));
139             }
140         }
141         file.Close();
142         rCtrl.SetSel( 0, 0);
143        
144         rCtrl.SetEOLMode(endings);
145        
146         // Re-Enable UNDO
147         rCtrl.SetUndoCollection(1);
148         rCtrl.SetSavePoint();
149        
150        
151        
152         return true;
153     }
154     else
155         return false;
156    
157    
158    
159    
160     return false;
161 }
162
163 BOOL CElephantDoc::OnSaveDocument(LPCTSTR lpszPathName)
164 {
165     Globals::theApp.LogEvent(SAVE_FILE, lpszPathName );
166     return CDocument::OnSaveDocument(lpszPathName);
167 } 
168
169 void CElephantDoc::OnCloseDocument()
170 {
171    
172     CString buf  = GetTitle();
173     Globals::theApp.RemoveFileHistory(this);
174
175     Globals::theApp.LogEvent(CLOSE_FILE, buf.GetBuffer(0) );
176    
177     CDocument::OnCloseDocument();
178 }
179
180 void CElephantDoc::OnFileSaveSavefile()
181 {
182    
183    
184    
185     CString path = GetPathName();
186
187     Globals::theApp.LogEvent(SAVE_FILE,path .GetBuffer(0) );
188    
189     if ( path != "") {
190         ScintillaSave(path.GetBuffer(0));
191         SetTitle(StripFileName(path.GetBuffer(0) ).c_str() );
192         Globals::theApp.UpdateFileParser();
193     }
194     else {
195        
196         OnFileSaveAs();
197        
198     }
199    
200    
201    
202 }
203
204
205 bool CElephantDoc::ScintillaSave(LPCTSTR filename)
206 {
207    
208    
209     CScintillaCtrl& rCtrl  = GetCtrl();
210    
211     char data[blockSize + 1];
212     int lengthDoc = rCtrl.GetLength();
213    
214     if(rCtrl.m_encType == eUnknown)
215     {
216         // Standard 8-bit ascii...
217         CElephantFile file;
218         if( file.Open(filename, CElephantFile::modeWrite | CElephantFile::modeBinary) )
219         {
220             for (int i = 0; i < lengthDoc; i += blockSize)
221             {
222                 int grabSize = lengthDoc - i;
223                 if (grabSize > blockSize)
224                     grabSize = blockSize;
225                 rCtrl.GetRange(i, i + grabSize, data);
226                
227                 /*if (props.GetInt("strip.trailing.spaces"))
228                 grabSize = StripTrailingSpaces(
229                 data, grabSize, grabSize != blockSize);*/
230                
231                 file.Write(data, grabSize);
232             }
233             file.Close();
234         }
235         else
236             return false;
237     }
238     else
239     {
240         // Deal with writing unicode formats here...
241         Utf8_16_Write converter;
242         converter.setEncoding( static_cast<Utf8_16::encodingType>(rCtrl.m_encType) );
243        
244         FILE* fp = converter.fopen(filename, _T("wb"));
245         if(fp != NULL)
246         {
247             for(int i = 0; i < lengthDoc; i += blockSize)
248             {
249                 int grabSize = lengthDoc - i;
250                 if( grabSize > blockSize )
251                     grabSize = blockSize;
252                 rCtrl.GetRange(i, i + grabSize, data);
253                
254                 converter.fwrite(data, grabSize);
255             }
256             converter.fclose();
257         }
258         else
259             return false;
260     }
261    
262     rCtrl.SetSavePoint();
263     return true;
264 }
265
266
267 void CElephantDoc::FileModified() {
268    
269     CScintillaCtrl& rCtrl = GetCtrl();
270    
271     if ( !rCtrl.GetModify()) return;
272    
273     CString x = GetTitle();
274     if ( x.GetLength() ) {
275         if (x[x.GetLength() - 1] == '*') return;
276         x += "*";
277         SetTitle((LPCTSTR)x);
278     }
279 }
280
281 CScintillaCtrl& CElephantDoc::GetCtrl()
282 {
283    
284     POSITION x = GetFirstViewPosition();
285     return static_cast<CElephantView*>(GetNextView(x))->GetCtrl();
286    
287 }
288
289
290 void RecentViews::Add ( LastView& lv ) {
291    
292     if ( lv.me == lastAdded ) {
293         //          ElephantMessageBox ("skipping last added");
294         return;
295     }
296    
297     Globals::theApp.AddFileHistory(lv.me);
298    
299     backViews.push_front(lv);
300     lastAdded = lv.me;
301    
302 }
303
304 void RecentViews::Remove(LastView& lv)
305 {
306    
307     lastAdded = lv.me;
308     //ElephantMessageBox("Removing");
309     int i = 0;
310     for (i = 0;i < backViews.size();i++) {
311         if ( backViews[i].me == lv.me ) {
312             backViews.erase(backViews.begin() + i );
313             break;
314         }
315     }
316    
317     for ( i = 0;i < forwardViews.size();i++) {
318         if ( forwardViews[i].me == lv.me ) {
319             forwardViews.erase(forwardViews.begin() + i );
320             break;
321         }
322     }
323    
324    
325    
326     Globals::theApp.RemoveFileHistory(lv.me);
327    
328 }
329
330 void CElephantDoc::Reload()
331 {
332     CString file_ = GetPathName();
333     LPCTSTR file = file_.GetBuffer(0);
334     if ( file )
335         ScintillaOpen(file);
336 }
337
338 void CElephantDoc::OnFileSaveAs()
339
340 {
341
342     string file_path;
343         if ( SaveFileFromCFileDialog(Globals::lastDir, file_path ) )
344         {
345             if ( FileExists(file_path))
346             {
347                
348                 if ( ElephantMessageBoxYesNo("A file with this name already exists, do you want to overwrite it ?") )
349                 {
350                    
351                     SetPathName(file_path.c_str() );
352                     SetTitle(StripFileName(file_path).c_str() );
353                     ScintillaSave(file_path.c_str());
354                    
355                 }
356                 else Globals::theApp.SetStatusBarText("Bailing out off save file as.");
357                
358             }
359
360             else
361                 {
362
363                     SetPathName(file_path.c_str() );
364                     SetTitle(StripFileName(file_path).c_str() );
365                     ScintillaSave(file_path.c_str());
366
367                 }
368
369             Globals::theApp.LogEvent(SAVE_FILE_AS ,file_path);
370            
371         }
372
373    
374    
375    
376 }
377
378 // ghetto shit
379
380 BOOL CElephantDoc::PromptSave()
381 {
382       CString path = GetPathName();
383   CString buf  = GetTitle();
384    
385   Globals::theApp.LogEvent(SAVE_FILE,path .GetBuffer(0) );
386    
387   if ( path != "") {
388     ScintillaSave(path.GetBuffer(0));
389     SetTitle(StripFileName(path.GetBuffer(0) ).c_str() );
390     Globals::theApp.UpdateFileParser();
391    
392   }
393
394   else
395     {
396       /*
397       CString prompt = "Would you like to save the file titled " + buf + " ?  ( To auto save/discard these files, see Settings->Behavior ) ";
398       if ( ElephantMessageBoxYesNo(prompt.GetBuffer(0) ) )
399       {
400           OnFileSaveAs();
401       }
402
403   */
404     }
405    
406
407    
408   return true;
409
410 }
411
412 BOOL CElephantDoc::SaveModified()
413 {
414
415     return PromptSave();
416 }
Note: See TracBrowser for help on using the browser.