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 ***************************************************************************/
23 #include <proto/exec.h>
24 #include <proto/intuition.h>
30 void UpdateStyles(struct InstData
*data
)
38 struct marking newblock
;
40 NiceBlock(&data
->blockinfo
, &newblock
);
41 if(newblock
.startx
== data
->blockinfo
.startx
&& newblock
.startline
== data
->blockinfo
.startline
)
42 newStyle
= GetStyle(data
->blockinfo
.stopx
-1, data
->blockinfo
.stopline
);
44 newStyle
= GetStyle(data
->blockinfo
.stopx
, data
->blockinfo
.stopline
);
48 newStyle
= GetStyle(data
->CPos_X
, data
->actualline
);
51 if(newStyle
!= data
->style
)
53 UWORD oldStyle
= data
->style
;
55 data
->style
= newStyle
;
57 if(isFlagSet(newStyle
, BOLD
) && isFlagClear(oldStyle
, BOLD
))
58 set(data
->object
, MUIA_TextEditor_StyleBold
, TRUE
);
59 else if(isFlagClear(newStyle
, BOLD
) && isFlagSet(oldStyle
, BOLD
))
60 set(data
->object
, MUIA_TextEditor_StyleBold
, FALSE
);
62 if(isFlagSet(newStyle
, ITALIC
) && isFlagClear(oldStyle
, ITALIC
))
63 set(data
->object
, MUIA_TextEditor_StyleItalic
, TRUE
);
64 else if(isFlagClear(newStyle
, ITALIC
) && isFlagSet(oldStyle
, ITALIC
))
65 set(data
->object
, MUIA_TextEditor_StyleItalic
, FALSE
);
67 if(isFlagSet(newStyle
, UNDERLINE
) && isFlagClear(oldStyle
, UNDERLINE
))
68 set(data
->object
, MUIA_TextEditor_StyleUnderline
, TRUE
);
69 else if(isFlagClear(newStyle
, UNDERLINE
) && isFlagSet(oldStyle
, UNDERLINE
))
70 set(data
->object
, MUIA_TextEditor_StyleUnderline
, FALSE
);
78 UWORD
GetStyle(LONG x
, struct line_node
*line
)
84 if(line
->line
.Styles
!= NULL
&& x
>= 0)
86 struct LineStyle
*styles
= line
->line
.Styles
;
88 while(styles
->column
!= EOS
&& styles
->column
<= x
+1)
90 if(styles
->style
> 0xff)
91 style
&= styles
->style
;
93 style
|= styles
->style
;
105 void AddStyleToLine(struct InstData
*data
, LONG x
, struct line_node
*line
, LONG length
, UWORD style
)
107 struct Grow styleGrow
;
108 struct LineStyle
*styles
;
110 UWORD end_style
= GetStyle(x
+length
, line
);
116 InitGrow(&styleGrow
, data
->mypool
, sizeof(struct LineStyle
));
118 if((styles
= line
->line
.Styles
) != NULL
)
120 while(styles
->column
!= EOS
&& styles
->column
< x
)
122 AddToGrow(&styleGrow
, styles
);
124 if(styles
->style
> 0xff)
125 cur_style
&= styles
->style
;
127 cur_style
|= styles
->style
;
134 if(cur_style
& ~style
)
136 struct LineStyle newStyle
;
139 newStyle
.style
= style
;
140 AddToGrow(&styleGrow
, &newStyle
);
145 if(!(cur_style
& style
))
147 struct LineStyle newStyle
;
150 newStyle
.style
= style
;
151 AddToGrow(&styleGrow
, &newStyle
);
157 while(styles
->column
!= EOS
&& styles
->column
<= x
+length
)
159 UWORD invstyle
= ~style
;
161 if(styles
->style
!= style
&& styles
->style
!= invstyle
)
163 AddToGrow(&styleGrow
, styles
);
169 if(!(((style
> 0xff) && (!(end_style
& ~style
))) ||
170 ((style
< 0xff) && ((end_style
& style
)))))
172 struct LineStyle newStyle
;
174 newStyle
.column
= x
+length
;
175 newStyle
.style
= ~style
;
176 AddToGrow(&styleGrow
, &newStyle
);
181 while(styles
->column
!= EOS
)
183 AddToGrow(&styleGrow
, styles
);
189 // the old styles are not needed anymore
190 if(line
->line
.Styles
!= NULL
)
191 FreeVecPooled(data
->mypool
, line
->line
.Styles
);
193 if(styleGrow
.itemCount
> 0)
195 struct LineStyle newStyle
;
197 newStyle
.column
= EOS
;
199 AddToGrow(&styleGrow
, &newStyle
);
202 line
->line
.Styles
= (struct LineStyle
*)styleGrow
.array
;
209 void AddStyle(struct InstData
*data
, struct marking
*realblock
, UWORD style
, BOOL set
)
211 struct marking newblock
;
214 struct line_node
*startline
;
215 struct line_node
*stopline
;
221 if((data
->style
& style
) == 0)
231 if((data
->style
& style
) != 0)
237 data
->HasChanged
= TRUE
;
239 if(realblock
->enabled
== TRUE
&& (realblock
->startx
!= realblock
->stopx
|| realblock
->startline
!= realblock
->stopline
))
241 NiceBlock(realblock
, &newblock
);
242 startx
= newblock
.startx
;
243 stopx
= newblock
.stopx
;
244 startline
= newblock
.startline
;
245 stopline
= newblock
.stopline
;
249 startx
= data
->CPos_X
;
251 startline
= data
->actualline
;
252 stopline
= startline
;
255 if(startline
== stopline
)
257 AddStyleToLine(data
, startx
, startline
, stopx
-startx
, style
);
261 struct line_node
*line
= GetNextLine(startline
);
263 AddStyleToLine(data
, startx
, startline
, startline
->line
.Length
-startx
-1, style
);
264 while(line
!= stopline
)
266 AddStyleToLine(data
, 0, line
, line
->line
.Length
-1, style
);
267 line
= GetNextLine(line
);
269 AddStyleToLine(data
, 0, line
, stopx
, style
);
271 RedrawArea(data
, startx
, startline
, stopx
, stopline
);
274 data
->style
&= style
;
276 data
->style
|= style
;