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
21 ***************************************************************************/
24 #include <proto/utility.h>
25 #include <proto/exec.h>
31 /***********************************************************************
32 Import the given 0 terminated text by invoking the given import Hook
34 ***********************************************************************/
35 BOOL
ImportText(struct InstData
*data
, const char *contents
, struct Hook
*importHook
, LONG wraplength
, struct MinList
*lines
)
37 struct line_node
*line
;
41 // make sure we start with an empty list of lines
44 if((line
= AllocVecPooled(data
->mypool
, sizeof(struct line_node
))) != NULL
)
46 struct ImportMessage im
;
48 memset(line
, 0, sizeof(*line
));
51 im
.ImportWrap
= wraplength
;
52 im
.PoolHandle
= data
->mypool
;
53 im
.ConvertTabs
= data
->ConvertTabs
;
54 im
.TabSize
= data
->TabSize
;
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
64 im
.Data
= (char*)CallHookPkt(importHook
, NULL
, &im
);
68 if(line
->line
.Contents
!= NULL
)
70 // add the last imported line to the list
75 // free the line node if it didn't contain any contents
76 if(ContainsLines(lines
) == FALSE
)
78 FreeVecPooled(data
->mypool
, line
);
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
85 if(Init_LineNode(data
, line
, "\n") == TRUE
)
88 FreeVecPooled(data
->mypool
, line
);
96 // add the imported line to the list
99 if((new_line
= AllocVecPooled(data
->mypool
, sizeof(struct line_node
))) == NULL
)
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
;
111 RETURN(ContainsLines(lines
));
112 return ContainsLines(lines
);
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
);
122 struct Hook
*ExportHookCopy
;
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
);
140 ResetUndoBuffer(data
);
147 // restore the former export hook
148 data
->ExportHook
= ExportHookCopy
;