revert between 56095 -> 55830 in arch
[AROS.git] / workbench / classes / zune / texteditor / mcc / ImportText.c
blob9c4b1f73238605774dc09ae373b21cbdfeb361f8
1 /***************************************************************************
3 TextEditor.mcc - Textediting MUI Custom Class
4 Copyright (C) 1997-2000 Allan Odgaard
5 Copyright (C) 2005-2014 TextEditor.mcc Open Source Team
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 TextEditor class Support Site: http://www.sf.net/projects/texteditor-mcc
19 $Id$
21 ***************************************************************************/
23 #include <string.h>
24 #include <proto/utility.h>
25 #include <proto/exec.h>
27 #include "private.h"
28 #include "Debug.h"
30 /// ImportText()
31 /***********************************************************************
32 Import the given 0 terminated text by invoking the given import Hook
33 for every line
34 ***********************************************************************/
35 BOOL ImportText(struct InstData *data, const char *contents, struct Hook *importHook, LONG wraplength, struct MinList *lines)
37 struct line_node *line;
39 ENTER();
41 // make sure we start with an empty list of lines
42 InitLines(lines);
44 if((line = AllocVecPooled(data->mypool, sizeof(struct line_node))) != NULL)
46 struct ImportMessage im;
48 memset(line, 0, sizeof(*line));
50 im.Data = contents;
51 im.ImportWrap = wraplength;
52 im.PoolHandle = data->mypool;
53 im.ConvertTabs = data->ConvertTabs;
54 im.TabSize = data->TabSize;
56 while(TRUE)
58 struct line_node *new_line;
60 im.linenode = &line->line;
62 // invoke the hook, it will return NULL in case it is finished or
63 // an error occured
64 im.Data = (char*)CallHookPkt(importHook, NULL, &im);
66 if(im.Data == NULL)
68 if(line->line.Contents != NULL)
70 // add the last imported line to the list
71 AddLine(lines, line);
73 else
75 // free the line node if it didn't contain any contents
76 if(ContainsLines(lines) == FALSE)
78 FreeVecPooled(data->mypool, line);
80 else
82 // if the line has nor predecessor it was obviously the first line
83 // so we prepare a "fake" line_node to let the textEditor clear our
84 // text
85 if(Init_LineNode(data, line, "\n") == TRUE)
86 AddLine(lines, line);
87 else
88 FreeVecPooled(data->mypool, line);
92 // bail out
93 break;
96 // add the imported line to the list
97 AddLine(lines, line);
99 if((new_line = AllocVecPooled(data->mypool, sizeof(struct line_node))) == NULL)
100 break;
102 // inherit the flow from the current line for the next line,
103 // but only if the clearFlow variable is not set
104 if(line->line.clearFlow == FALSE)
105 new_line->line.Flow = line->line.Flow;
107 line = new_line;
111 RETURN(ContainsLines(lines));
112 return ContainsLines(lines);
116 /// ReimportText
117 // export and reimport the current text with modified TAB size or TAB conversion
118 BOOL ReimportText(struct IClass *cl, Object *obj)
120 struct InstData *data = INST_DATA(cl, obj);
121 BOOL result = FALSE;
122 struct Hook *ExportHookCopy;
123 char *buff;
125 ENTER();
127 // use the plain export hook
128 ExportHookCopy = data->ExportHook;
129 data->ExportHook = &ExportHookPlain;
131 if((buff = (char *)mExportText(cl, obj, NULL)) != NULL)
133 struct MinList newlines;
135 if(ImportText(data, buff, data->ImportHook, data->ImportWrap, &newlines) == TRUE)
137 FreeTextMem(data, &data->linelist);
138 MoveLines(&data->linelist, &newlines);
139 ResetDisplay(data);
140 ResetUndoBuffer(data);
141 result = TRUE;
144 FreeVec(buff);
147 // restore the former export hook
148 data->ExportHook = ExportHookCopy;
150 RETURN(result);
151 return result;