Updated from its repository.
[cake.git] / workbench / classes / gadgets / texteditor / mcc / StyleOperators.c
blob4ecb004e0dc7c02cbdd2ec20d07c2771873cce50
1 /***************************************************************************
3 TextEditor.mcc - Textediting MUI Custom Class
4 Copyright (C) 1997-2000 Allan Odgaard
5 Copyright (C) 2005-2009 by 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 <proto/intuition.h>
25 #include "private.h"
27 void UpdateStyles (struct InstData *data)
29 UWORD style;
31 ENTER();
33 if(Enabled(data))
35 struct marking newblock;
37 NiceBlock(&data->blockinfo, &newblock);
38 style = GetStyle(data->blockinfo.stopx - ((newblock.startx == data->blockinfo.startx && newblock.startline == data->blockinfo.startline) ? 1 : 0), data->blockinfo.stopline);
40 else
42 style = GetStyle(data->CPos_X, data->actualline);
45 if(style != data->style)
47 UWORD oldstyle = data->style;
49 data->style = style;
51 if(style & BOLD)
53 if(!(oldstyle & BOLD))
54 set(data->object, MUIA_TextEditor_StyleBold, TRUE);
56 else
58 if(oldstyle & BOLD)
59 set(data->object, MUIA_TextEditor_StyleBold, FALSE);
61 if(style & ITALIC)
63 if(!(oldstyle & ITALIC))
64 set(data->object, MUIA_TextEditor_StyleItalic, TRUE);
66 else
68 if(oldstyle & ITALIC)
69 set(data->object, MUIA_TextEditor_StyleItalic, FALSE);
71 if(style & UNDERLINE)
73 if(!(oldstyle & UNDERLINE))
74 set(data->object, MUIA_TextEditor_StyleUnderline, TRUE);
76 else
78 if(oldstyle & UNDERLINE)
79 set(data->object, MUIA_TextEditor_StyleUnderline, FALSE);
83 LEAVE();
86 LONG GetStyle (LONG x, struct line_node *line)
88 LONG style = 0;
90 ENTER();
92 if(line->line.Styles)
94 unsigned short *styles = line->line.Styles;
96 while(*styles <= x+1)
98 if(*++styles > 0xff)
99 style &= *styles++;
100 else style |= *styles++;
104 RETURN(style);
105 return(style);
108 void AddStyleToLine (LONG x, struct line_node *line, LONG length, unsigned short style, struct InstData *data)
110 unsigned short *styles = line->line.Styles;
111 unsigned short *oldstyles = styles;
112 unsigned short *newstyles;
113 unsigned short cur_style = 0;
114 unsigned short end_style = GetStyle(x+length, line);
116 ENTER();
118 x++;
120 if(styles)
121 newstyles = MyAllocPooled(data->mypool, (*((long *)styles-1))+8);
122 else newstyles = MyAllocPooled(data->mypool, 32);
124 if(newstyles)
126 line->line.Styles = newstyles;
127 if(styles)
129 while(*styles != EOS && *styles < x)
131 *newstyles++ = *styles++;
132 if(*styles > 0xff)
133 cur_style &= *styles;
134 else cur_style |= *styles;
135 *newstyles++ = *styles++;
138 if(style > 0xff)
140 if(cur_style & ~style)
142 *newstyles++ = x;
143 *newstyles++ = style;
146 else
148 if(!(cur_style & style))
150 *newstyles++ = x;
151 *newstyles++ = style;
154 if(styles)
156 while(*styles != EOS && *styles <= x+length)
158 if((*(styles+1) != style) && (*(styles+1) != (unsigned short)~style))
160 *newstyles++ = *styles++;
161 *newstyles++ = *styles++;
163 else styles += 2;
166 if(!(((style > 0xff) && (!(end_style & ~style))) ||
167 ((style < 0xff) && ((end_style & style)))))
169 *newstyles++ = x+length;
170 *newstyles++ = ~style;
173 if(styles)
175 while(*styles != EOS)
177 *newstyles++ = *styles++;
178 *newstyles++ = *styles++;
181 *newstyles = EOS;
182 if(oldstyles)
184 MyFreePooled(data->mypool, oldstyles);
188 LEAVE();
191 void AddStyle (struct marking *realblock, unsigned short style, long Set, struct InstData *data)
193 struct marking newblock;
194 LONG startx, stopx;
195 struct line_node *startline, *stopline;
197 ENTER();
199 if(!Set)
201 if(!(data->style & style))
203 LEAVE();
204 return;
207 style = ~style;
209 else
211 if(data->style & style)
213 LEAVE();
214 return;
217 data->HasChanged = TRUE;
219 if(realblock->enabled && (realblock->startx != realblock->stopx || realblock->startline != realblock->stopline))
221 NiceBlock(realblock, &newblock);
222 startx = newblock.startx;
223 stopx = newblock.stopx;
224 startline = newblock.startline;
225 stopline = newblock.stopline;
227 else
229 startx = data->CPos_X;
230 stopx = startx+1;
231 startline = data->actualline;
232 stopline = startline;
235 if(startline == stopline)
237 AddStyleToLine(startx, startline, stopx-startx, style, data);
239 else
241 struct line_node *line = startline->next;
243 AddStyleToLine(startx, startline, startline->line.Length-startx-1, style, data);
244 while(line != stopline)
246 AddStyleToLine(0, line, line->line.Length-1, style, data);
247 line = line->next;
249 AddStyleToLine(0, line, stopx, style, data);
251 RedrawArea(startx, startline, stopx, stopline, data);
253 if(style > 0xff)
254 data->style &= style;
255 else data->style |= style;
257 LEAVE();