cleaned all trailing white space from source files.
[sdk] / extras / gui / RoundedSquare.ec
1 import "ecere"
2
3 static define borderSize = (radius * 8 / 20);
4
5 struct RoundMatch
6 {
7    DisplaySystem displaySystem;
8    int radius;
9 };
10
11 static Map<RoundMatch, RoundedBMPs> roundedBMPs { };
12
13 static byte borderData[] =
14 {
15    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x4B, 0x8D, 0xC9, 0xCC, 0xFC, 0xFF, 0xFF,
16    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0xAE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
17    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3C, 0xCC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
18    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7A, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC0, 0x78, 0x36, 0x33, 0x03, 0x00, 0x00,
19    0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xB3, 0xFF, 0xFF, 0xFF, 0xF6, 0x8D, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20    0x00, 0x00, 0x00, 0x00, 0x03, 0xD4, 0xFF, 0xFF, 0xFF, 0xB1, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21    0x00, 0x00, 0x00, 0x00, 0xB0, 0xFF, 0xFF, 0xFF, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
22    0x00, 0x00, 0x00, 0x7D, 0xFF, 0xFF, 0xFF, 0x57, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23    0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0x7B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24    0x00, 0x00, 0xC9, 0xFF, 0xFF, 0xAE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25    0x00, 0x45, 0xFF, 0xFF, 0xF9, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
26    0x00, 0xC6, 0xFF, 0xFF, 0x8A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
27    0x0F, 0xFF, 0xFF, 0xFC, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
28    0x57, 0xFF, 0xFF, 0xB4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29    0x93, 0xFF, 0xFF, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30    0xC6, 0xFF, 0xFF, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
31    0xEA, 0xFF, 0xFF, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32    0xF9, 0xFF, 0xFF, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
33    0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34    0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
35 };
36
37 static byte insideData[] =
38 {
39    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x87, 0xC9, 0xCC, 0xFC, 0xFF, 0xFF,
43    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x72, 0xEA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
44    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4E, 0xE1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
45    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF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, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
55    0x00, 0x00, 0x00, 0xEA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
56    0x00, 0x00, 0x00, 0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
57    0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
58    0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
59 };
60
61 class RoundedBMPs
62 {
63    int count;
64    DisplaySystem displaySystem;
65    int radius;
66
67    Bitmap tl { alphaBlend = true };
68    Bitmap bl { alphaBlend = true };
69    Bitmap tr { alphaBlend = true };
70    Bitmap br { alphaBlend = true };
71    Bitmap vert { alphaBlend = true };
72    Bitmap horz { alphaBlend = true };
73    Bitmap eVert { alphaBlend = true };
74    Bitmap eHorz { alphaBlend = true };
75    Bitmap itl { alphaBlend = true };
76    Bitmap ibl { alphaBlend = true };
77    Bitmap itr { alphaBlend = true };
78    Bitmap ibr { alphaBlend = true };
79    Bitmap ivert { alphaBlend = true };
80    Bitmap ihorz { alphaBlend = true };
81    Bitmap ieVert { alphaBlend = true };
82    Bitmap ieHorz { alphaBlend = true };
83
84    bool OnLoadGraphics()
85    {
86       /*tl.Load(":roundedTL.png", null, null);
87       itl.Load(":insideTL.png", null, null);
88
89       tl.Convert(null, pixelFormatAlpha, null);
90       itl.Convert(null, pixelFormatAlpha, null);*/
91
92       tl.Allocate(null, radius, radius, 0, pixelFormatAlpha, false);
93       itl.Allocate(null, radius, radius, 0, pixelFormatAlpha, false);
94
95       if(radius == 20)
96       {
97          memcpy(tl.picture, borderData, sizeof(borderData));
98          memcpy(itl.picture, insideData, sizeof(insideData));
99       }
100       else
101       {
102          Bitmap temp { alphaBlend = true };
103          Surface surface;
104
105          temp.Allocate(null, 20, 20, 0, pixelFormatAlpha, false);
106
107          memcpy(temp.picture, borderData, sizeof(borderData));
108          surface = tl.GetSurface(0,0, null);
109          surface.blend = false;
110          surface.Filter(temp, 0,0,0,0, radius, radius, 20,20);
111          delete surface;
112
113          memcpy(temp.picture, insideData, sizeof(insideData));
114          surface = itl.GetSurface(0,0, null);
115          surface.blend = false;
116          surface.Filter(temp, 0,0,0,0, radius, radius, 20,20);
117          delete surface;
118
119          delete temp;
120       }
121
122       bl.Allocate(null, tl.width, tl.height, 0, pixelFormatAlpha, false);
123       tr.Allocate(null, tl.width, tl.height, 0, pixelFormatAlpha, false);
124       br.Allocate(null, tl.width, tl.height, 0, pixelFormatAlpha, false);
125       ibl.Allocate(null, itl.width, itl.height, 0, pixelFormatAlpha, false);
126       itr.Allocate(null, itl.width, itl.height, 0, pixelFormatAlpha, false);
127       ibr.Allocate(null, itl.width, itl.height, 0, pixelFormatAlpha, false);
128       vert.Allocate(null, tl.width, 1, 0, pixelFormatAlpha, false);
129       horz.Allocate(null, 1, tl.height, 0, pixelFormatAlpha, false);
130       eVert.Allocate(null, borderSize /*tl.width*/, 1, 0, pixelFormatAlpha, false);
131       eHorz.Allocate(null, 1, borderSize /*tl.height*/, 0, pixelFormatAlpha, false);
132       ivert.Allocate(null, tl.width, 1, 0, pixelFormatAlpha, false);
133       ihorz.Allocate(null, 1, tl.height, 0, pixelFormatAlpha, false);
134       ieVert.Allocate(null, borderSize /*tl.width*/, 1, 0, pixelFormatAlpha, false);
135       ieHorz.Allocate(null, 1, borderSize /*tl.height*/, 0, pixelFormatAlpha, false);
136
137       {
138          int x, y;
139          int w = tl.width, h = tl.height, s = tl.stride;
140          byte * picture = tl.picture, * ipicture = itl.picture;
141          for(y = 0; y < tl.height; y++)
142          {
143             for(x = 0; x < tl.width; x++)
144             {
145                byte b = picture[y * s + x];
146                byte ib = ipicture[y * s + x];
147
148                ((byte *)bl.picture)[(h-1-y) * s + x]        = b;
149                ((byte *)br.picture)[(h-1-y) * s + (w-1-x)]  = b;
150                ((byte *)tr.picture)[y * s + (w-1-x)]        = b;
151
152                ((byte *)ibl.picture)[(h-1-y) * s + x]        = ib;
153                ((byte *)ibr.picture)[(h-1-y) * s + (w-1-x)]  = ib;
154                ((byte *)itr.picture)[y * s + (w-1-x)]        = ib;
155
156                /*if(x) Print(", ");
157                printf("0x%02X", b);*/
158             }
159             /*if(y < tl.height-1) Print(",");
160             PrintLn("");*/
161          }
162          /*PrintLn("");
163          PrintLn("");
164          for(y = 0; y < tl.height; y++)
165          {
166             for(x = 0; x < tl.width; x++)
167             {
168                byte ib = ipicture[y * s + x];
169
170                if(x) Print(", ");
171                printf("0x%02X", ib);
172             }
173             if(y < tl.height-1) Print(",");
174             PrintLn("");
175          }*/
176       }
177       vert.Grab(tl, 0, tl.height - vert.height);
178       horz.Grab(tl, tl.width - horz.width, 0);
179       eVert.Grab(br, tl.width - borderSize, 0);
180       eHorz.Grab(br, 0, tl.height - borderSize);
181
182       ivert.Grab(itl, 0, tl.height - vert.height);
183       ihorz.Grab(itl, tl.width - horz.width, 0);
184       ieVert.Grab(ibr, tl.width - borderSize, 0);
185       ieHorz.Grab(ibr, 0, tl.height - borderSize);
186
187       tl.MakeDD(displaySystem);
188       bl.MakeDD(displaySystem);
189       tr.MakeDD(displaySystem);
190       br.MakeDD(displaySystem);
191       vert.MakeDD(displaySystem);
192       horz.MakeDD(displaySystem);
193       eVert.MakeDD(displaySystem);
194       eHorz.MakeDD(displaySystem);
195       itl.MakeDD(displaySystem);
196       ibl.MakeDD(displaySystem);
197       itr.MakeDD(displaySystem);
198       ibr.MakeDD(displaySystem);
199       ivert.MakeDD(displaySystem);
200       ihorz.MakeDD(displaySystem);
201       ieVert.MakeDD(displaySystem);
202       ieHorz.MakeDD(displaySystem);
203       return true;
204    }
205
206    void RenderBack(Surface surface, Size size, ColorAlpha background)
207    {
208       if(background)
209       {
210          surface.blitTint = background;
211
212          surface.Tile(ihorz, tl.width,0, size.w - 2*tl.width, borderSize);
213          surface.Tile(ivert, 0, tl.height, borderSize, size.h - 2*tl.height);
214          surface.Tile(ieHorz, br.width, size.h - ieHorz.height, size.w - 2*br.width, ieHorz.height);
215          surface.Tile(ieVert, size.w - ieVert.width, br.height, ieVert.width, size.h - 2*br.height);
216
217          surface.Blit(itl, 0,0,0,0, borderSize, tl.height);
218          surface.Blit(itl, 0,0,0,0, tl.width, borderSize);
219
220          surface.Blit(itr, size.w - borderSize, 0, itr.width - borderSize, 0, borderSize, tr.height);
221          surface.Blit(itr, size.w - tr.width, 0, 0, 0, tr.width, borderSize);
222
223          surface.Blit(ibl, 0,size.h - borderSize, 0,bl.height - borderSize, bl.width, bl.height - borderSize);
224          surface.Blit(ibl, 0,size.h - bl.height,0,0, borderSize, bl.height);
225
226          surface.Blit(ibr, size.w - borderSize, size.h - tr.height, bl.width - borderSize, 0, borderSize, br.height);
227          surface.Blit(ibr, size.w - br.width,   size.h - borderSize, 0, bl.height - borderSize, br.width, borderSize);
228       }
229    }
230
231    void RenderBorder(Surface surface, Size size, ColorAlpha borderColor)
232    {
233       if(borderColor)
234       {
235          surface.blitTint = borderColor;
236          /*
237          surface.Tile(horz, tl.width,0, size.w - 2*tl.width, horz.height);
238          surface.Tile(vert, 0, tl.height, vert.width, size.h - 2*tl.height);
239          surface.Tile(eHorz, br.width, size.h - eHorz.height, size.w - 2*br.width, eHorz.height);
240          surface.Tile(eVert, size.w - eVert.width, br.height, eVert.width, size.h - 2*br.height);
241
242          surface.Blit(tl, 0,0,0,0, tl.width, tl.height);
243          surface.Blit(tr, size.w - tr.width,0,0,0, tr.width, tr.height);
244          surface.Blit(bl, 0,size.h - bl.height,0,0, bl.width, bl.height);
245          surface.Blit(br, size.w - br.width, size.h - tr.height,0,0, br.width, br.height);*/
246
247          surface.Tile(horz, tl.width,0, size.w - 2*tl.width, borderSize);
248          surface.Tile(vert, 0, tl.height, borderSize, size.h - 2*tl.height);
249          surface.Tile(eHorz, br.width, size.h - ieHorz.height, size.w - 2*br.width, ieHorz.height);
250          surface.Tile(eVert, size.w - ieVert.width, br.height, ieVert.width, size.h - 2*br.height);
251
252          surface.Blit(tl, 0,0,0,0, borderSize, tl.height);
253          surface.Blit(tl, 0,0,0,0, tl.width, borderSize);
254
255          surface.Blit(tr, size.w - borderSize, 0, itr.width - borderSize, 0, borderSize, tr.height);
256          surface.Blit(tr, size.w - tr.width, 0, 0, 0, tr.width, borderSize);
257
258          surface.Blit(bl, 0,size.h - borderSize, 0,bl.height - borderSize, bl.width, bl.height - borderSize);
259          surface.Blit(bl, 0,size.h - bl.height,0,0, borderSize, bl.height);
260
261          surface.Blit(br, size.w - borderSize, size.h - tr.height, bl.width - borderSize, 0, borderSize, br.height);
262          surface.Blit(br, size.w - br.width,   size.h - borderSize, 0, bl.height - borderSize, br.width, borderSize);
263       }
264    }
265 }
266
267 class RoundedSquare : Window
268 {
269    ColorAlpha borderColor;
270
271    moveable = true;
272    alphaBlend = true;
273
274    RoundedBMPs bmps;
275
276    int radius;
277
278    radius = 20;
279
280    bool OnLoadGraphics()
281    {
282       bmps = roundedBMPs[RoundMatch { displaySystem, radius }];
283       if(!bmps)
284       {
285          bmps = RoundedBMPs { displaySystem = displaySystem, radius = radius };
286          roundedBMPs[RoundMatch { displaySystem, radius }] = bmps;
287          bmps.OnLoadGraphics();
288       }
289       bmps.count++;
290       return true;
291    }
292
293    void OnUnloadGraphics()
294    {
295       RoundedBMPs bmps = roundedBMPs[RoundMatch { displaySystem, radius }];
296       bmps.count--;
297       if(!bmps.count)
298       {
299          //Iterator<RoundedBMPs> it { }; it.Find(bmps); it.Remove();
300          roundedBMPs[RoundMatch { displaySystem, radius }] = null;
301          delete bmps;
302       }
303    }
304
305    void GetDecorationsSize(MinMaxValue * w, MinMaxValue * h)
306    {
307       *w = borderSize * 2;
308       *h = borderSize * 2;
309    }
310
311    void SetWindowArea(int * x, int * y, MinMaxValue * w, MinMaxValue * h, MinMaxValue * cw, MinMaxValue * ch)
312    {
313       *x = borderSize;
314       *y = borderSize;
315       *cw = *w - borderSize * 2;
316       *ch = *h - borderSize * 2;
317
318       *cw = Max(*cw, 0);
319       *ch = Max(*ch, 0);
320    }
321
322    void PreShowDecorations(Font captionFont, Surface surface, char * name, bool active, bool moving)
323    {
324       bmps.RenderBack(surface, size, { (byte)(opacity * 255), background });
325    }
326
327    void ShowDecorations(Font captionFont, Surface surface, char * name, bool active, bool moving)
328    {
329       //if(borderStyle == contour)
330          bmps.RenderBorder(surface, size, borderColor);
331    }
332
333    bool IsOpaque()
334    {
335       return false;//(!style.drawBehind && opacity == 1.0);
336    }
337 }
338 /*
339 class MyRoundedSquare : RoundedSquare
340 {
341    background = lightBlue;
342    //opacity = 0;
343    borderColor = teal;
344    radius = 15;
345
346    size = { 100, 100 };
347    Label test { this, position = { 0, 0 }, size = { 100, 20 }, text = "Bubble Window" };
348 }
349 */
350 /*MyRoundedSquare testRounded
351 {
352 };*/
353