libc+AxWin3 - RichText rendering, scanf
[tpg/acess2.git] / Usermode / Applications / axwin3_src / WM / renderers / richtext.c
1 /*
2  * Acess2 Window Manager v3
3  * - By John Hodge (thePowersGang)
4  *
5  * render/richtext.c
6  * - Formatted Line Editor
7  */
8 #include <common.h>
9 #include <wm_renderer.h>
10 #include <richtext_messages.h>
11 #include <stdio.h>      // sscanf
12
13 #define LINES_PER_BLOCK 30
14
15 // === TYPES ===
16 typedef struct sRichText_LineBlock
17 {
18         struct sRichText_LineBlock      *Next;
19          int    FirstLine;
20         char    *Lines[LINES_PER_BLOCK];
21 } tRichText_LineBlock;
22 typedef struct sRichText_Window
23 {
24          int    DispLines, DispCols;
25          int    FirstVisRow, FirstVisCol;
26          int    nLines, nCols;
27          int    CursorRow, CursorCol;
28         tRichText_LineBlock     FirstBlock;
29         tColour DefaultFG;
30         tColour DefaultBG;
31         tFont   *Font;
32         
33         short   LineHeight;
34 } tRichText_Window;
35
36 // === PROTOTYPES ===
37  int    Renderer_RichText_Init(void);
38 tWindow *Renderer_RichText_Create(int Flags);
39 void    Renderer_RichText_Redraw(tWindow *Window);
40  int    Renderer_RichText_HandleMessage(tWindow *Target, int Msg, int Len, const void *Data);
41
42 // === GLOBALS ===
43 tWMRenderer     gRenderer_RichText = {
44         .Name = "RichText",
45         .CreateWindow   = Renderer_RichText_Create,
46         .Redraw         = Renderer_RichText_Redraw,
47         .HandleMessage  = Renderer_RichText_HandleMessage
48 };
49
50 // === CODE ===
51 int Renderer_RichText_Init(void)
52 {
53         WM_RegisterRenderer(&gRenderer_RichText);       
54         return 0;
55 }
56
57 tWindow *Renderer_RichText_Create(int Flags)
58 {
59         tRichText_Window        *info;
60         tWindow *ret = WM_CreateWindowStruct( sizeof(*info) );
61         if(!ret)        return NULL;
62         info = ret->RendererInfo;
63         // Everything starts at zero?
64         return ret;
65 }
66
67 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)
68 {
69          int    rwidth;
70         // TODO: Fill only what is needed
71         WM_Render_DrawRect(Window, X, Row*info->LineHeight,
72                 Window->W - X, info->LineHeight,
73                 BG
74                 );
75         rwidth = WM_Render_DrawText(Window,
76                 X, Row*info->LineHeight,
77                 Window->W - X, info->LineHeight,
78                 info->Font, FG,
79                 Text, Bytes
80                 );
81         return rwidth;
82 }
83
84 void Renderer_RichText_RenderText(tWindow *Window, int Line, const char *Text)
85 {
86         tRichText_Window        *info = Window->RendererInfo;
87         tColour fg = info->DefaultFG;
88         tColour bg = info->DefaultBG;
89          int    bBold = 0;
90          int    bULine = 0;
91          int    bItalic = 0;
92          int    bRender = 0;
93          int    curx = 0;
94         const char      *oldtext = Text;
95         
96         for( int i = 0; i < info->FirstVisCol + info->DispCols; i ++ )
97         {
98                 char    ch, flags;
99                  int    len;
100
101                 if( i == info->FirstVisCol )
102                         bRender = 1;
103
104                 ch = *Text++;
105                 if( ch == 0 )   break;
106                 if( ch <=3 && bRender ) {
107                         // Render previous characters
108                         curx += Renderer_RichText_RenderText_Act(Window, info, curx, Line, oldtext, Text - oldtext, fg, bg);
109                         oldtext = Text;
110                 }
111                 switch(ch)
112                 {
113                 case 1: // FG Select (\1 RRGGBB)
114                         len = sscanf(Text, "%6x", &fg);
115                         if( len != 6 ) {
116                                 // Bad client
117                         }
118                         Text += len;
119                         break ;
120                 case 2: // BG Select (\2 RRGGBB)
121                         len = sscanf(Text, "%6x", &bg);
122                         if( len != 6 ) {
123                                 // Bad client
124                         }
125                         Text += len;
126                         break ;
127                 case 3: // Flagset (0,it,uline,bold)
128                         len = sscanf(Text, "%1x", &flags);
129                         if( len != 1 ) {
130                                 // Bad client
131                         }
132                         Text += len;
133                         bItalic = !!(flags & (1 << 2));
134                         bULine = !!(flags & (1 << 1));
135                         bBold = !!(flags & (1 << 0));
136                         break ;
137                 default: // Any char, nop
138                         break;
139                 }
140         }
141         curx += Renderer_RichText_RenderText_Act(Window, info, curx, Line, oldtext, Text - oldtext, fg, bg);
142         WM_Render_DrawRect(Window, curx, Line * info->LineHeight, Window->W - curx, info->LineHeight, info->DefaultBG);
143 }
144
145 void Renderer_RichText_Redraw(tWindow *Window)
146 {
147         tRichText_Window        *info = Window->RendererInfo;
148          int    i;
149         tRichText_LineBlock     *lines = &info->FirstBlock;
150         
151         // Locate the first line block
152         for( i = info->FirstVisRow; i > LINES_PER_BLOCK && lines; i -= LINES_PER_BLOCK )
153                 lines = lines->Next;
154
155         for( i = 0; i < info->DispLines && lines; i ++ )
156         {
157                 if( i >= info->nLines - info->FirstVisRow )
158                         break;
159                 // TODO: Dirty rectangles?
160                 WM_Render_DrawRect(Window,
161                         0, i*info->LineHeight,
162                         Window->W, info->LineHeight,
163                         info->DefaultBG
164                         );
165                 // TODO: Horizontal scrolling?
166                 // TODO: Formatting
167                 //Renderer_RichText_RenderText(Window, i, lines->Lines[i % LINES_PER_BLOCK]);
168                 WM_Render_DrawText(Window,
169                         0, i*info->LineHeight,
170                         Window->W, info->LineHeight,
171                         info->Font, info->DefaultFG,
172                         lines->Lines[i % LINES_PER_BLOCK],
173                         -1);
174         
175                 if( (i + 1) % LINES_PER_BLOCK == 0 )
176                         lines = lines->Next;
177         }
178         // Clear out i -- info->DispLines
179         WM_Render_DrawRect(Window,
180                 0, i*info->LineHeight,
181                 Window->W, (info->DispLines-i)*info->LineHeight,
182                 info->DefaultBG
183                 );
184 }
185
186 int Renderer_RichText_HandleMessage(tWindow *Target, int Msg, int Len, const void *Data)
187 {
188         switch(Msg)
189         {
190         case MSG_RICHTEXT_SETATTR:
191                 break;
192         }
193         return 0;
194 }

UCC git Repository :: git.ucc.asn.au