b21d6b60d5915f0bd5da5c10f3b45a544487fda0
[sdk] / ecere / src / gui / skins / WindowsSkin.ec
1 #if defined(WIN32)
2 #define WIN32_LEAN_AND_MEAN
3 #define String _String
4 #define Method _Method
5 #define strlen _strlen
6 #include <windows.h>
7 #undef Method
8 #undef String
9 #undef strlen
10 #endif
11
12 import "Window"
13
14 #if !defined(WIN32)
15 bool gui::drivers::XGetBorderWidths(Window window, Box box);
16 #endif
17
18 namespace gui::skins;
19
20 #define BORDER       4
21 #define TOP          4
22 #define BOTTOM       4
23 #define CORNER       (BORDER * 2)
24 #if defined(HIGH_DPI)
25 #define BUTTON_SIZE  45
26 #define CAPTION      60
27 #else
28 #define BUTTON_SIZE  15
29 #define CAPTION      20
30 #endif
31 #define DEAD_BORDER  3
32 #define MIN_WIDTH    60
33 #define MIN_HEIGHT   3
34 #define BUTTON_OFFSET   2
35 #if defined(HIGH_DPI)
36 #define NAME_OFFSET   12
37 #else
38 #define NAME_OFFSET   2
39 #endif
40 #define NAME_OFFSETX  4
41
42 #define SB_WIDTH  16
43 #define SB_HEIGHT 16
44
45
46 #define GRADIENT_SMOOTHNESS 1.0f
47
48 #define TEXT_COLOR   black
49 /*
50
51 #define GRADIENT_DIRECTION horizontal
52
53 static ColorKey gradient[] =
54 {
55    { Color { 128, 128, 255}, 0.00f },
56    { Color { 254, 254, 254}, 0.60f },
57    { Color {   0,   0, 255}, 1.00f }
58 };
59 */
60
61 #define GRADIENT_DIRECTION vertical
62 static ColorKey gradient[] =
63 {
64    //{ ColorAlpha { 255, { 180, 200, 220} }, 0.00f },
65    { ColorAlpha { 255, menuBarColor }, 0.00f },
66    { ColorAlpha { 255, { 255, 255, 255} }, 0.60f },
67    { ColorAlpha { 255, { 158, 158, 160} }, 1.00f }
68 };
69
70 static ColorKey gradientInactive[] =
71 {
72    //{ ColorAlpha { 255, { 160, 180, 200} },  0.00f },
73    { ColorAlpha { 255, popupMenuColor }, 0.00f },
74    { ColorAlpha { 255, { 220, 220, 220} }, 0.60f },
75    { ColorAlpha { 255, { 120, 120, 120} }, 1.00f }
76 };
77
78 /*
79 #define GRADIENT_DIRECTION horizontal
80 #define TEXT_COLOR         white
81 */
82 //#define TEXT_INACTIVE      Color { 212,208,200 }
83 #define TEXT_INACTIVE      Color { 40, 50, 60 }
84 /*
85 static ColorKey gradient[] =
86 {
87    { ColorAlpha { 255, Color {  10,   36, 106 } }, 0.00f },
88    { ColorAlpha { 255, Color { 166,  202, 240 } }, 1.00f }
89 };
90 static ColorKey gradientInactive[] =
91 {
92    { ColorAlpha { 255, Color { 128, 128, 128 } }, 0.00f },
93    { ColorAlpha { 255, Color { 192, 192, 192 } }, 1.00f }
94 };
95 */
96 char * cursorsBitmaps[] =
97 {
98    "<:ecere>cursors/arrow.png",
99    "<:ecere>cursors/iBeam.png",
100    "<:ecere>cursors/cross.png",
101    "<:ecere>cursors/move.png",
102    "<:ecere>cursors/sizeNorthEastSouthWest.png",
103    "<:ecere>cursors/sizeNorthSouth.png",
104    "<:ecere>cursors/sizeNorthWestSouthEast.png",
105    "<:ecere>cursors/sizeWestEast.png",
106    "<:ecere>cursors/move.png"
107 };
108
109 static Point cursorsHotSpots[] =
110 {
111    { 0, 0 },
112    { 0, 0 },
113    { 8, 8 },
114    { 10, 10 },
115    { 8, 8 },
116    { 4, 10 },
117    { 7, 7 },
118    { 5, 0 }
119 };
120
121 static char * skinBitmaps[SkinBitmap] =
122 {
123    "<:ecere>elements/areaMinimize.png",
124    "<:ecere>elements/areaMaximize.png",
125    "<:ecere>elements/areaRestore.png",
126    "<:ecere>elements/areaClose.png"
127 };
128
129 class WindowsSkin : Skin
130 {
131    class_property(name) = "Windows";
132    class_property(selectionColor) = Color { 10, 36, 106 };
133    class_property(selectionText)  = (Color)white;
134    class_property(disabledFrontColor) = Color { 128,128,128 };
135    class_property(disabledBackColor) = (Color)white;
136
137    FontResource ::SystemFont()
138    {
139 #if defined(HIGH_DPI)
140       return FontResource { faceName = $"Tahoma", size = 18.25f };
141 #else
142       return FontResource { faceName = $"Tahoma", size = 8.25f };
143 #endif
144    }
145
146    FontResource ::CaptionFont()
147    {
148 #if defined(HIGH_DPI)
149       return FontResource { faceName = $"Tahoma", size = 18.25f, bold = true };
150 #else
151       return FontResource { faceName = $"Tahoma", size = 8.25f, bold = true };
152 #endif
153    }
154
155    char * ::CursorsBitmaps(uint id, int * hotSpotX, int *hotSpotY, byte ** paletteShades)
156    {
157       *hotSpotX = cursorsHotSpots[id].x;
158       *hotSpotY = cursorsHotSpots[id].y;
159       *paletteShades = null;
160       return cursorsBitmaps[id];
161    }
162
163    BitmapResource ::GetBitmap(SkinBitmap id)
164    {
165       return BitmapResource { fileName = skinBitmaps[id] };
166    }
167
168    int ::VerticalSBW() { return SB_WIDTH; }
169    int ::HorizontalSBH() { return SB_HEIGHT; }
170 };
171
172
173 public class WindowsSkin_Window : Window
174 {
175    void GetDecorationsSize(MinMaxValue * w, MinMaxValue * h)
176    {
177       *w = *h = 0;
178
179       if(hasMenuBar && state != minimized)
180       {
181          *h += skinMenuHeight;
182       }
183       if(statusBar && state != minimized)
184       {
185          *h += statusBarHeight;
186       }
187
188       if(nativeDecorations && rootWindow == this && windowHandle)
189       {
190 #if defined(WIN32)
191          RECT rcClient = { 0 }, rcWindow = { 0 };
192          if(GetClientRect(windowHandle, &rcClient) && GetWindowRect(windowHandle, &rcWindow))
193          {
194             *w += (rcWindow.right - rcWindow.left) - rcClient.right;
195             *h += (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
196          }
197
198          // PrintLn(_class.name, " is at l = ", rcWindow.left, ", r = ", rcWindow.right);
199 #else
200          Box widths = { 0 };
201 #if !defined(__ANDROID__)
202          XGetBorderWidths(this, widths);
203 #endif
204          *w += widths.left + widths.right;
205          *h += widths.top + widths.bottom;
206 #endif
207          return;
208       }
209       if((((BorderBits)borderStyle).deep || ((BorderBits)borderStyle).bevel) && state != minimized)
210       {
211          *w += 4;
212          *h += 4;
213       }
214       if(((BorderBits)borderStyle).sizable && (state == normal))
215       {
216          *w += 2 * BORDER;
217          *h += TOP + BOTTOM;
218       }
219       if(((BorderBits)borderStyle).fixed && (state != maximized || !GetParentMenuBar()))
220       {
221          *h += CAPTION;
222          if(!((BorderBits)borderStyle).sizable || state == minimized)
223          {
224             *h += 2*DEAD_BORDER;
225             *w += 2*DEAD_BORDER;
226          }
227       }
228       if(((BorderBits)borderStyle).contour && !((BorderBits)borderStyle).fixed)
229       {
230          *w += 2;
231          *h += 2;
232       }
233    }
234
235    void SetWindowMinimum(MinMaxValue * mw, MinMaxValue * mh)
236    {
237       bool isNormal = (state == normal);
238       if(nativeDecorations && rootWindow == this && windowHandle) return;
239       if(((BorderBits)borderStyle).fixed && (state != maximized || !GetParentMenuBar()))
240       {
241          *mw = MIN_WIDTH;
242          *mh = MIN_HEIGHT;
243       }
244       else
245          *mw = *mh = 0;
246       if(((BorderBits)borderStyle).sizable && isNormal)
247          *mw += 2*CORNER;
248       // GetDecorationsSize(window, mw, mh);
249
250       if(hasVertScroll)
251          *mw += SB_WIDTH;
252       if(hasHorzScroll)
253          *mh += SB_HEIGHT;
254       if(hasVertScroll && hasHorzScroll)
255       {
256          *mw += 2 * SB_WIDTH + SB_WIDTH;
257          *mh += 2 * SB_HEIGHT + SB_HEIGHT;
258          if(((BorderBits)borderStyle).sizable && isNormal)
259             *mw -= 2*CORNER;
260       }
261    }
262
263    void SetWindowArea(int * x, int * y, MinMaxValue * w, MinMaxValue * h, MinMaxValue * cw, MinMaxValue * ch)
264    {
265       bool isNormal = (state == normal);
266       MinMaxValue aw = 0, ah = 0;
267
268       *x = *y = 0;
269
270       if(hasMenuBar)
271       {
272          *y += skinMenuHeight;
273       }
274
275       GetDecorationsSize(&aw, &ah);
276
277       if(nativeDecorations && rootWindow == this && windowHandle)
278       {
279 #if defined(WIN32)
280          RECT rcWindow;
281          POINT client00 = { 0, 0 };
282          ClientToScreen(windowHandle, &client00);
283          GetWindowRect(windowHandle, &rcWindow);
284          *x += client00.x - rcWindow.left;
285          *y += client00.y - rcWindow.top;
286 #else
287          Box widths = { 0 };
288 #if !defined(__ANDROID__)
289          XGetBorderWidths(this, widths);
290 #endif
291          *x += widths.left;
292          *y += widths.top;
293 #endif
294       }
295       else
296       {
297          // Compute client area start
298          if(((BorderBits)borderStyle).deep || ((BorderBits)borderStyle).bevel)
299          {
300             *x += 2;
301             *y += 2;
302          }
303
304          if(((BorderBits)borderStyle).sizable && isNormal)
305          {
306             *x += BORDER;
307             *y += TOP;
308          }
309
310          if(((BorderBits)borderStyle).fixed && (state != maximized || !GetParentMenuBar()))
311          {
312             *y += CAPTION;
313             if(!((BorderBits)borderStyle).sizable || state == minimized)
314             {
315                *y += DEAD_BORDER;
316                *x += DEAD_BORDER;
317             }
318          }
319
320          if(((BorderBits)borderStyle).contour && !((BorderBits)borderStyle).fixed)
321          {
322             *x += 1;
323             *y += 1;
324          }
325       }
326
327       // Reduce client area
328       *cw = *w - aw;
329       *ch = *h - ah;
330
331       *cw = Max(*cw, 0);
332       *ch = Max(*ch, 0);
333    }
334
335    void ShowDecorations(Font captionFont, Surface surface, char * name, bool active, bool moving)
336    {
337       bool isNormal = (state == normal);
338       int top = 0, border = 0, bottom = 0;
339       Window parentMenuBar = GetParentMenuBar();
340
341       if(nativeDecorations && rootWindow == this && windowHandle) return;
342
343       if(state == minimized)
344          top = border = bottom = DEAD_BORDER;
345       else if(((BorderBits)borderStyle).sizable)
346       {
347          top = isNormal ? TOP : 0;
348          border = isNormal ? BORDER : 0;
349          bottom = BOTTOM;
350       }
351       else if(((BorderBits)borderStyle).fixed)
352       {
353          top = DEAD_BORDER;
354          border = DEAD_BORDER;
355          bottom = DEAD_BORDER;
356       }
357       else if(((BorderBits)borderStyle).contour)
358       {
359          top = 1;
360          border = 1;
361          bottom = 1;
362       }
363
364       if(((BorderBits)borderStyle).deep || ((BorderBits)borderStyle).bevel)
365       {
366          int deepTop = 0, deepBottom = 0, deepBorder = 0;
367          if(((BorderBits)borderStyle).contour)
368          {
369             deepBorder = border;
370             deepTop = (((BorderBits)borderStyle).fixed && (state != maximized || !parentMenuBar)) ? (top + CAPTION) : top;
371             deepBottom = (((BorderBits)borderStyle).sizable && isNormal) ? bottom : border;
372          }
373
374          surface.Bevel(((BorderBits)borderStyle).bevel ? false : true, deepBorder, deepTop,
375             size.w - deepBorder - deepBorder, size.h - deepBottom - deepTop);
376       }
377
378       if(((BorderBits)borderStyle).fixed && (state != maximized || !parentMenuBar))
379       {
380          if(state != maximized || !((BorderBits)borderStyle).sizable)
381          {
382             // Frame for ES_CAPTION windows
383             surface.Bevel(false, 0, 0, size.w, size.h);
384             surface.SetForeground(formColor);
385             surface.Rectangle(2, 2, size.w-3, size.h-3);
386
387             // Resizeable frame is 1 pixel thicker
388             if(((BorderBits)borderStyle).sizable && isNormal)
389                surface.Rectangle(3, 3, size.w - 4, size.h - 4);
390          }
391
392          // Caption
393          if(active)
394             surface.Gradient(gradient, sizeof(gradient) / sizeof(ColorKey), GRADIENT_SMOOTHNESS, GRADIENT_DIRECTION,
395                border, top, size.w - border - 1, top + CAPTION - 2);
396          else
397             surface.Gradient(gradientInactive, sizeof(gradientInactive) / sizeof(ColorKey),
398             GRADIENT_SMOOTHNESS, GRADIENT_DIRECTION,
399                border, top, size.w - border - 1, top + CAPTION - 2);
400
401          surface.SetForeground(formColor);
402          if(state != minimized)
403             surface.HLine(border, size.w-border-1, top + CAPTION-1);
404
405          surface.SetForeground((active ? TEXT_COLOR : TEXT_INACTIVE));
406          surface.TextOpacity(false);
407          surface.TextFont(captionFont);
408          if(name)
409          {
410             int buttonsSize = border +
411                ((hasMaximize || hasMinimize) ? (BUTTON_SIZE*3)+7 : (BUTTON_SIZE+3));
412             surface.WriteTextDots(left, border + NAME_OFFSETX, top + NAME_OFFSET,
413                size.w - (buttonsSize + border + 4), name, strlen(name));
414          }
415       }
416       if(((BorderBits)borderStyle).contour && !((BorderBits)borderStyle).fixed)
417       {
418          surface.SetForeground(black);
419          surface.Rectangle(0, 0, size.w - 1, size.h - 1);
420       }
421
422       if(state != minimized && hasHorzScroll && hasVertScroll)
423       {
424          if(sbh && sbh.visible && sbv && sbv.visible)
425          {
426             surface.SetBackground(formColor);
427             surface.Area(
428                clientStart.x + clientSize.w,
429                clientStart.y + clientSize.h,
430                clientStart.x + clientSize.w + SB_WIDTH - 1,
431                clientStart.y + clientSize.h + SB_HEIGHT - 1);
432          }
433       }
434    }
435
436    bool IsMouseMoving(int x, int y, int w, int h)
437    {
438       bool isNormal = (state == normal);
439       bool result = false;
440       if(nativeDecorations && rootWindow == this && windowHandle) return false;
441
442       if(((BorderBits)borderStyle).fixed && (state != maximized || !GetParentMenuBar()))
443       {
444          int corner = 0, border = 0, top = 0;
445          if(((BorderBits)borderStyle).sizable && isNormal)
446          {
447             corner = CORNER;
448             border = BORDER;
449             top    = TOP;
450          }
451          // Special case for having caption on resize bar
452          if(!CAPTION)
453             result = Box { corner, 0, w-corner-1, TOP-1 }.IsPointInside({x,y});
454          else
455             result = Box { border, top, w-border-1, top+CAPTION-1 }.IsPointInside({x, y});
456       }
457       return result;
458    }
459
460    bool IsMouseResizing(int x, int y, int w, int h, bool *resizeX, bool *resizeY, bool *resizeEndX, bool *resizeEndY)
461    {
462       bool result = false;
463
464       *resizeX = *resizeY = *resizeEndX = *resizeEndY = false;
465       if(nativeDecorations && rootWindow == this && windowHandle) return false;
466
467       if(((BorderBits)borderStyle).sizable && (state == normal))
468       {
469          // TopLeft Corner
470          if(Box { 0, 0,CORNER-1, TOP-1 }.IsPointInside({x, y}))
471             result = *resizeX = *resizeY = true;
472          // TopRight Corner
473          if(Box { w-CORNER-1, 0, w-1, TOP-1 }.IsPointInside({x, y}))
474             result = *resizeEndX = *resizeY = true;
475          // BottomLeft Corner
476          if(Box { 0, h-BOTTOM-1, CORNER-1, h-1 }.IsPointInside({x, y}))
477             result = *resizeX = *resizeEndY = true;
478          // BottomRight Corner
479          if(Box { w-CORNER-1, h-BOTTOM-1, w-1, h-1 }.IsPointInside({x, y}))
480             result = *resizeEndX = *resizeEndY = true;
481          // Left Border
482          if(Box { 0,TOP, BORDER, h-BOTTOM-1 }.IsPointInside({x, y}))
483             result = *resizeX = true;
484          // Right Border
485          if(Box { w-BORDER-1, TOP, w-1, h-BOTTOM-1 }.IsPointInside({x, y}))
486             result = *resizeEndX = true;
487          // Top Border
488          if(Box { CORNER, 0, w-CORNER-1, TOP-1 }.IsPointInside({x, y}))
489             result = *resizeY = true;
490          // Bottom Border
491          if(Box { CORNER, h-BOTTOM-1, w-CORNER-1, h-1 }.IsPointInside({x, y}))
492             result = *resizeEndY = true;
493       }
494       return result;
495    }
496
497    void UpdateNonClient()
498    {
499       bool isNormal = (state == normal);
500       int top = 0, border = 0;
501       int insideBorder = 0;
502
503       if(!nativeDecorations || rootWindow != this || !windowHandle)
504       {
505          if(state == minimized)
506             top = border = DEAD_BORDER;
507          else if(((BorderBits)borderStyle).sizable)
508          {
509             if(state == maximized && GetParentMenuBar())
510             {
511                top = 2;
512                border = 2;
513             }
514             else
515             {
516                top = isNormal ? TOP : 0;
517                border = isNormal ? BORDER : 0;
518             }
519          }
520          else if(((BorderBits)borderStyle).fixed)
521          {
522             top = DEAD_BORDER;
523             border = DEAD_BORDER;
524          }
525          else if(((BorderBits)borderStyle).contour)
526          {
527             top = 1;
528             border = 1;
529          }
530          insideBorder = border;
531          if(((BorderBits)borderStyle).deep)
532             insideBorder += 2;
533       }
534       else
535       {
536          border = clientStart.x;
537          insideBorder = border;
538       }
539
540       if(menuBar)
541       {
542          if(state == minimized)
543             menuBar.visible = false;
544          else
545             menuBar.visible = true;
546          menuBar.Move(clientStart.x, clientStart.y - skinMenuHeight, size.w - insideBorder * 2, skinMenuHeight);
547       }
548       if(statusBar)
549       {
550          if(state == minimized)
551             statusBar.visible = false;
552          else
553          {
554             statusBar.visible = true;
555             if(nativeDecorations && rootWindow == this && windowHandle)
556             {
557                statusBar.anchor = { left = clientStart.x, bottom = (int)(size.h - clientSize.h - clientStart.y - statusBarHeight ) };
558                statusBar.size.w = size.w - insideBorder * 2;
559             }
560             else
561             {
562                statusBar.anchor = { left = clientStart.x, bottom = border };
563                statusBar.size.w = size.w - insideBorder * 2;
564             }
565          }
566       }
567       if(!nativeDecorations || rootWindow != this || !windowHandle)
568       {
569          if(sysButtons[0])
570          {
571             sysButtons[0].anchor = { right = 2+(2*BUTTON_SIZE+3) + border, top = top + BUTTON_OFFSET };
572             sysButtons[0].size = { BUTTON_SIZE, BUTTON_SIZE };
573             sysButtons[0].bevel = true;
574             sysButtons[0].bitmap = { skinBitmaps[(state == minimized) ? restore : minimize] };
575             sysButtons[0].visible = true;
576          }
577          if(sysButtons[1])
578          {
579             sysButtons[1].anchor = { right = 2+(BUTTON_SIZE+3) + border, top = top + BUTTON_OFFSET };
580             sysButtons[1].size = { BUTTON_SIZE, BUTTON_SIZE };
581             sysButtons[1].bevel = true;
582             sysButtons[1].bitmap = { skinBitmaps[(state == maximized) ? restore : maximize] };
583             sysButtons[1].visible = true;
584          }
585          if(sysButtons[2])
586          {
587             sysButtons[2].anchor = { right = 2 + border, top = top + BUTTON_OFFSET };
588             sysButtons[2].size = { BUTTON_SIZE, BUTTON_SIZE };
589             sysButtons[2].bevel = true;
590             sysButtons[2].bitmap = { skinBitmaps[close] };
591             sysButtons[2].visible = true;
592          }
593       }
594       else
595       {
596          if(sysButtons[0])
597             sysButtons[0].visible = false;
598          if(sysButtons[1])
599             sysButtons[1].visible = false;
600          if(sysButtons[2])
601             sysButtons[2].visible = false;
602       }
603    }
604 }
605
606
607 #define PUREVTBL(c)     (*(void ***)((byte *)class(c).data + sizeof(uintptr)))
608 #define CAPTION_DISTANCE   18
609
610 default:
611 static void Dummy()
612 {
613    Window a;
614    a.OnApplyGraphics();
615    a.OnRedraw(null);
616 }
617
618 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnApplyGraphics;
619 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRedraw;
620 private:
621
622 public class WindowsSkin_Button : Button
623 {
624    void OnRedraw(Surface surface)
625    {
626       if(isRadio)
627       {
628          ((void (*)(Window, Surface))PUREVTBL(Button)[__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRedraw])(this, surface);
629          return;
630       }
631       // if(bevel)
632       {
633          ButtonState state = this.buttonState;
634          Bitmap buttonBitmap = bitmap ? bitmap.bitmap : null;
635          char * text = this.text;
636          int offset = (state == down && this.offset) ? 1 : 0;
637          Color backColor = background;
638          int isDefault = this.isDefault;
639          Font font;
640          int tw = 0, th = 0;
641          int bw = 0, bh = 0;
642
643          font = fontObject;
644          surface.TextFont(font);
645          if(text)
646             surface.TextExtent(text, strlen(text),&tw, &th);
647
648          if(bevelOver && checked)
649             offset = 1;
650
651          if(!isEnabled)
652          {
653             if(bitmaps[disabled]) buttonBitmap = bitmaps[disabled].bitmap;
654             state = disabled;
655          }
656
657          if(buttonBitmap && buttonStyle.bevelOver && !buttonStyle.checkBox && !buttonStyle.radio && text)
658             isDefault = 0;
659
660          // Background
661          if((bevel /*|| bevelOver*/) && opacity && backColor)
662          {
663             if(!scaleBitmap || !buttonBitmap)
664             {
665                ColorKey keys[2] = { { silver, 0.0f }, { white, 1.0f } };
666                surface.Gradient(keys, sizeof(keys) / sizeof(ColorKey), 1, vertical, 0, 0, clientSize.w-1, clientSize.h-1);
667             }
668          }
669
670          // Checkbox
671          if(isCheckbox && !buttonBitmap)
672          {
673             int height = 16;
674             int start = (clientSize.h - height) / 2;
675
676             if(!isEnabled)
677                // surface.SetBackground(formColor);
678                surface.SetBackground(gainsboro);
679             else if(active && !text)
680                surface.SetBackground((offset ? formColor : Color { 0,0,170 }));
681             else
682                surface.SetBackground((offset ? formColor : white));
683             surface.Area(2, start+2,height-3,start+height-3);
684
685             surface.SetForeground(Color { 85, 85, 85 });
686             surface.HLine(0, height - 2, start + 0);
687             surface.VLine(start+1, start+height - 2, 0);
688
689             surface.SetForeground(Color { 64,64,64 });
690             surface.HLine(1, height - 3, start + 1);
691             surface.VLine(start+2, start+height - 3, 1);
692
693             surface.SetForeground(Color { 212,208,200 });
694             surface.HLine(1, height - 2, start + height-2);
695             surface.VLine(start+1, start+height - 3, height-2);
696
697             surface.SetForeground(white);
698             surface.HLine(0, height - 1, start + height-1);
699             surface.VLine(start+0, start+height - 2, height-1);
700
701             if(checked)
702             {
703                if(active && !text)
704                   surface.SetForeground(white);
705                else if(!isEnabled)
706                   surface.SetForeground(Color { 85, 85, 85 });
707                else
708                   surface.SetForeground(black);
709                surface.DrawLine(4, start+8, 7,start+11);
710                surface.DrawLine(4, start+9, 7,start+12);
711                surface.DrawLine(7, start+11, 11,start+3);
712                surface.DrawLine(7, start+12, 11,start+4);
713             }
714          }
715
716          // Bitmaps
717          if(buttonBitmap)
718          {
719             surface.SetForeground(white);
720             if(isRadio || isCheckbox)
721             {
722                int x = 0, y = (clientSize.h-buttonBitmap.height)/2;
723                if(bevelOver && text)
724                {
725                   x = (CAPTION_DISTANCE-buttonBitmap.width)/2 + offset;
726                   y = (clientSize.h-buttonBitmap.height)/2 + offset;
727
728
729                }
730
731                // Radio Buttons and Checkboxes
732                surface.Blit(buttonBitmap,
733                   x, y,
734                   0,0,buttonBitmap.width,buttonBitmap.height);
735             }
736             else
737             {
738                // Push Buttons
739                if(scaleBitmap)
740                {
741                   if(bevel || offset)
742                      surface.Stretch(buttonBitmap,
743                         1 + offset, 1 + offset,0,0,
744                         clientSize.w-3,clientSize.h-3,buttonBitmap.width,buttonBitmap.height);
745                   else
746                      surface.Stretch(buttonBitmap, 0,0, 0,0,
747                         clientSize.w,clientSize.h,buttonBitmap.width,buttonBitmap.height);
748                }
749                else
750                {
751                   int x, y;
752                   bw = buttonBitmap.width;
753                   bh = buttonBitmap.height;
754
755                   if(bitmapAlignment == left || bitmapAlignment == right)
756                   {
757                      if(bitmapAlignment == left)
758                         x = 2;
759                      else
760                         x = clientSize.w-bw-2;
761                      y = (clientSize.h-bh)/2;
762                   }
763                   else if(bitmapAlignment == top || bitmapAlignment == bottom)
764                   {
765                      x = (clientSize.w-bw)/2;
766                      if(bitmapAlignment == top)
767                         y = 2;
768                      else
769                         y = clientSize.h-bh-2;
770                   }
771                   else
772                   {
773                      x = (clientSize.w-bw)/2;
774                      y = (clientSize.h-bh - (int)(buttonStyle.bevelOver && text) * th)/2;
775                   }
776                   if(buttonStyle.bevel || buttonStyle.offset)
777                   {
778                      x += offset;
779                      y += offset;
780                   }
781                   surface.Blit(buttonBitmap, x,y, 0,0, bw,bh);
782                }
783             }
784          }
785
786          // Shadows
787          if(bevel || (bevelOver && (state == down || state == over || checked)))
788          {
789             if(state == down || checked)
790             {
791                surface.SetForeground(Color { 85, 85, 85 });
792                surface.HLine(isDefault + 0, clientSize.w-2-isDefault, 0);
793                surface.VLine(isDefault + 1, clientSize.h-2-isDefault, 0);
794                surface.SetForeground(white);
795                surface.HLine(isDefault + 0, clientSize.w-1-isDefault, clientSize.h-1-isDefault);
796                surface.VLine(isDefault + 0, clientSize.h-2-isDefault, clientSize.w-1-isDefault);
797             }
798             else
799             {
800                surface.SetForeground(white);
801                surface.HLine(0 + isDefault, clientSize.w-2 - isDefault,  isDefault);
802                surface.VLine(1 + isDefault, clientSize.h-2 - isDefault,  isDefault);
803                surface.SetForeground(Color { 85, 85, 85 });
804                surface.HLine(1 + isDefault, clientSize.w-2 - isDefault, clientSize.h-2 - isDefault);
805                surface.VLine(1 + isDefault, clientSize.h-3 - isDefault, clientSize.w-2 - isDefault);
806
807                if(bevel)
808                {
809                   surface.SetForeground(black);
810                   surface.HLine( isDefault, clientSize.w-1 - isDefault, clientSize.h-1 - isDefault);
811                   surface.VLine( isDefault, clientSize.h-2 - isDefault, clientSize.w-1 - isDefault);
812                }
813             }
814          }
815
816          // Text
817          surface.TextOpacity(false);
818          surface.SetForeground(foreground);
819          if(text)
820          {
821             int tw, th;
822             surface.TextExtent(text, strlen(text),&tw, &th);
823
824             if((isRadio || isCheckbox) && !bevelOver)
825                WriteCaption(surface, CAPTION_DISTANCE + 3, // + clientSize.h,
826                   (clientSize.h - th - 4)/2);
827             else
828             {
829                int x, y = (clientSize.h - th - 1)/2 + offset;
830
831                if(buttonStyle.bevelOver && buttonBitmap && !buttonStyle.checkBox && !buttonStyle.radio)
832                {
833                   if(bitmapAlignment == top)
834                      y = (clientSize.h - bh - 4 - th - 5)/2 + offset + bh + 4;
835                   else if(bitmapAlignment == bottom)
836                      y = (clientSize.h - bh - 4 - th - 5)/2 + offset;
837                   else//if(bitmapAlignment == left || bitmapAlignment == right)
838                      y = clientSize.h - th - 5 + offset;
839                }
840                else
841                   y = (clientSize.h - th - 1)/2 + offset;
842
843                if(ellipsis)
844                {
845                   int width = clientSize.w - 2*6;
846                   int x = 6 + offset;
847
848                   surface.WriteTextDots(alignment, x, y, width, text, strlen(text));
849                }
850                else
851                {
852                   int width = clientSize.w - 2 * 6;
853                   x = 6 + offset;
854                   if(bitmapAlignment == left || bitmapAlignment == right)
855                   {
856                      if(bitmapAlignment == left)
857                         x += bw + 4;
858                      width -= bw + 4;
859                   }
860                   if(isCheckbox || ((isRadio /*|| bevelOver*/) && buttonBitmap))
861                   {
862                      x += CAPTION_DISTANCE + 3;
863                   }
864
865                   if(tw < width)
866                   {
867                      if(alignment == right)
868                         x += width - tw - 1;
869                      else if(alignment == center)
870                         x += (width - tw) / 2;
871                   }
872                   WriteCaption(surface, x, y);
873                }
874             }
875          }
876
877          // Activation Highlight
878          if(isDefault)
879          {
880             surface.SetForeground(black);
881             surface.Rectangle(0,0,clientSize.w-1,clientSize.h-1);
882          }
883          if(!bevelOver && !isRemote)
884          {
885             if(active) // && (text || !(buttonStyle.radio || buttonStyle.checkBox)))
886             {
887                int x1,y1,x2,y2;
888                surface.SetForeground(black);
889                surface.LineStipple(0x5555);
890
891                if((isRadio || isCheckbox) && text)
892                {
893                   x1 = CAPTION_DISTANCE;  // + clientSize.h;
894                   y1 = 0;
895                   x2 = clientSize.w-4;
896                   y2 = clientSize.h-4;
897                }
898                else
899                {
900                   x1 = 3+offset;
901                   y1 = 3+offset;
902                   x2 = clientSize.w - 5+offset;
903                   y2 = clientSize.h - 5+offset;
904
905                   if(isRadio || isCheckbox)
906                   {
907                      x1-=3;
908                      y1-=3;
909                      x2+=1;
910                      y2+=1;
911                   }
912                }
913                if((x2 - x1) & 1) x2++;
914                if((y2 - y1) & 1) y2++;
915
916                surface.Rectangle(x1, y1, x2, y2);
917                surface.LineStipple(0);
918             }
919          }
920       }
921    }
922 }