2 * Acess2 Window Manager v3
3 * - By John Hodge (thePowersGang)
6 * - Formatted Line Editor
9 #include <wm_renderer.h>
10 #include <wm_messages.h>
11 #include <richtext_messages.h>
12 #include <stdio.h> // sscanf
13 #include <string.h> // memcpy
15 #define LINES_PER_BLOCK 30
18 typedef struct sRichText_Line
20 struct sRichText_Line *Next;
21 struct sRichText_Line *Prev;
23 // TODO: Pre-rendered cache?
28 typedef struct sRichText_Window
30 int DispLines, DispCols;
31 int FirstVisRow, FirstVisCol;
33 int CursorRow, CursorCol;
34 tRichText_Line *FirstLine;
35 tRichText_Line *FirstVisLine;
44 int Renderer_RichText_Init(void);
45 tWindow *Renderer_RichText_Create(int Flags);
46 void Renderer_RichText_Redraw(tWindow *Window);
47 int Renderer_RichText_HandleMessage(tWindow *Target, int Msg, int Len, const void *Data);
50 tWMRenderer gRenderer_RichText = {
52 .CreateWindow = Renderer_RichText_Create,
53 .Redraw = Renderer_RichText_Redraw,
54 .HandleMessage = Renderer_RichText_HandleMessage
58 int Renderer_RichText_Init(void)
60 WM_RegisterRenderer(&gRenderer_RichText);
64 tWindow *Renderer_RichText_Create(int Flags)
66 tRichText_Window *info;
67 tWindow *ret = WM_CreateWindowStruct( sizeof(*info) );
69 info = ret->RendererInfo;
71 // Initialise font (get an idea of dimensions)
73 WM_Render_GetTextDims(NULL, "yY!", 3, NULL, &h);
79 static inline int Renderer_RichText_RenderText_Act(tWindow *Window, tRichText_Window *info, int X, int Row, const char *Text, int Bytes, tColour FG, tColour BG, int Flags)
82 // TODO: Fill only what is needed? What about the rest of the line?
83 WM_Render_DrawRect(Window, X, Row*info->LineHeight,
84 Window->W - X, info->LineHeight,
87 // TODO: Bold, Italic, Underline
88 rwidth = WM_Render_DrawText(Window,
89 X, Row*info->LineHeight,
90 Window->W - X, info->LineHeight,
97 void Renderer_RichText_RenderText(tWindow *Window, int Line, const char *Text)
99 tRichText_Window *info = Window->RendererInfo;
100 tColour fg = info->DefaultFG;
101 tColour bg = info->DefaultBG;
105 const char *oldtext = Text;
107 for( int i = 0; curx < Window->W; i ++ )
112 if( i == info->FirstVisCol )
118 // Not an escape - move along
123 // Render previous characters
124 curx += Renderer_RichText_RenderText_Act(Window, info, curx, Line,
125 oldtext, Text - oldtext - 1, fg, bg, flagset);
126 if( curx >= Window->W )
132 case 1: // FG Select (\1 RRGGBB)
133 if( sscanf(Text, "%6x%n", &fg, &len) != 1 || len != 6 ) {
135 _SysDebug("foreground scanf failed - len=%i", len);
140 _SysDebug("FG update to %x", fg);
142 case 2: // BG Select (\2 RRGGBB)
143 if( sscanf(Text, "%6x%n", &bg, &len) != 1 || len != 6 ) {
145 _SysDebug("background scanf failed - len=%i", len);
150 _SysDebug("BG update to %x", bg);
152 case 3: // Flagset (0,it,uline,bold)
153 if( sscanf(Text, "%1hhx%n", &flags, &len) != 1 || len != 1 ) {
155 _SysDebug("Flagset scanf failed - len=%i", len);
161 case 4: // Escape (do nothing)
163 // NOTE: No update to oldtext
169 curx += Renderer_RichText_RenderText_Act(Window, info, curx,
170 Line, oldtext, Text - oldtext + 1, fg, bg, flagset);
171 WM_Render_DrawRect(Window, curx, Line * info->LineHeight,
172 Window->W - curx, info->LineHeight, info->DefaultBG);
175 void Renderer_RichText_Redraw(tWindow *Window)
177 tRichText_Window *info = Window->RendererInfo;
179 tRichText_Line *line = info->FirstVisLine;
182 line = info->FirstLine;
183 while(line && line->Num < info->FirstVisRow )
185 info->FirstVisLine = line;
187 while( line && line->Prev && line->Prev->Num > info->FirstVisRow )
190 for( i = 0; i < info->DispLines && line; i ++ )
192 if( i >= info->nLines - info->FirstVisRow )
194 // TODO: Dirty rectangles?
195 WM_Render_FillRect(Window,
196 0, i*info->LineHeight,
197 Window->W, info->LineHeight,
200 if( line->Num > info->FirstVisRow + i )
202 // TODO: Horizontal scrolling?
205 // Formatted text out
206 Renderer_RichText_RenderText(Window, i, line->Data);
207 _SysDebug("RichText: %p - Render %i '%.*s'", Window,
208 line->Num, line->ByteLength, line->Data);
212 // Clear out i -- info->DispLines
213 _SysDebug("RichText: %p - Clear %i px lines with %06x starting at %i",
214 Window, (info->DispLines-i)*info->LineHeight, info->DefaultBG, i*info->LineHeight);
215 WM_Render_FillRect(Window,
216 0, i*info->LineHeight,
217 Window->W, (info->DispLines-i)*info->LineHeight,
222 int Renderer_RichText_HandleMessage(tWindow *Target, int Msg, int Len, const void *Data)
224 tRichText_Window *info = Target->RendererInfo;
227 case WNDMSG_RESIZE: {
228 const struct sWndMsg_Resize *msg = Data;
229 if(Len < sizeof(*msg)) return -1;
230 info->DispLines = msg->H / info->LineHeight;
232 case MSG_RICHTEXT_SETATTR: {
233 const struct sRichTextMsg_SetAttr *msg = Data;
234 if(Len < sizeof(*msg)) return -1;
235 _SysDebug("RichText Attr %i set to %x", msg->Attr, msg->Value);
239 info->DefaultBG = msg->Value;
242 info->DefaultFG = msg->Value;
245 // TODO: Set scroll flag
247 case _ATTR_LINECOUNT:
248 info->nLines = msg->Value;
253 case MSG_RICHTEXT_SENDLINE: {
254 const struct sRichTextMsg_SendLine *msg = Data;
255 if(Len < sizeof(*msg)) return -1;
256 _SysDebug("RichText Line %i = '%.*s'", msg->Line, Len - sizeof(*msg), msg->LineData);
257 if( msg->Line >= info->nLines ) return 1; // Bad count
259 tRichText_Line *line = info->FirstLine;
260 tRichText_Line *prev = NULL;
261 while(line && line->Num < msg->Line)
262 prev = line, line = line->Next;
263 if( !line || line->Num > msg->Line )
267 int space = ((Len - sizeof(*msg)) + 32-1) & ~(32-1);
268 tRichText_Line *new = malloc(sizeof(*line) + space);
269 // TODO: Bookkeeping on how much memory each window uses
272 new->Num = msg->Line;
274 if(new->Prev) new->Prev->Next = new;
275 else info->FirstLine = new;
276 if(new->Next) new->Next->Prev = new;
279 else if( line->Space < Len - sizeof(*msg) )
281 // Need to allocate more space
282 int space = ((Len - sizeof(*msg)) + 32-1) & ~(32-1);
283 tRichText_Line *new = realloc(line, space);
284 // TODO: Bookkeeping on how much memory each window uses
287 if(new->Prev) new->Prev->Next = new;
288 else info->FirstLine = new;
289 if(new->Next) new->Next->Prev = new;
296 line->ByteLength = Len - sizeof(*msg);
297 memcpy(line->Data, msg->LineData, Len - sizeof(*msg));