ecere;samples/blokus: Grouped High DPI tweaks separately with HIGH_DPI preprocessor...
[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;
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    }
595 }
596
597
598 #define PUREVTBL(c)     (*(void ***)((byte *)class(c).data + sizeof(uintptr)))
599 #define CAPTION_DISTANCE   18
600
601 default:
602 static void Dummy()
603 {
604    Window a;
605    a.OnApplyGraphics();
606    a.OnRedraw(null);
607 }
608
609 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnApplyGraphics;
610 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRedraw;
611 private:
612
613 public class WindowsSkin_Button : Button
614 {
615    void OnRedraw(Surface surface)
616    {
617       if(isRadio)
618       {
619          ((void (*)(Window, Surface))PUREVTBL(Button)[__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRedraw])(this, surface);
620          return;
621       }
622       // if(bevel)
623       {
624          ButtonState state = this.buttonState;
625          Bitmap buttonBitmap = bitmap ? bitmap.bitmap : null;
626          char * text = this.text;
627          int offset = (state == down && this.offset) ? 1 : 0;
628          Color backColor = background;
629          int isDefault = this.isDefault;
630          Font font;
631          int tw = 0, th = 0;
632          int bw = 0, bh = 0;
633
634          font = fontObject;
635          surface.TextFont(font);
636          if(text)
637             surface.TextExtent(text, strlen(text),&tw, &th);
638
639          if(bevelOver && checked)
640             offset = 1;
641
642          if(!isEnabled)
643          {
644             if(bitmaps[disabled]) buttonBitmap = bitmaps[disabled].bitmap;
645             state = disabled;
646          }
647
648          if(buttonBitmap && buttonStyle.bevelOver && !buttonStyle.checkBox && !buttonStyle.radio && text)
649             isDefault = 0;
650
651          // Background
652          if((bevel /*|| bevelOver*/) && opacity && backColor)
653          {
654             if(!scaleBitmap || !buttonBitmap)
655             {
656                ColorKey keys[2] = { { silver, 0.0f }, { white, 1.0f } };
657                surface.Gradient(keys, sizeof(keys) / sizeof(ColorKey), 1, vertical, 0, 0, clientSize.w-1, clientSize.h-1);
658             }
659          }
660
661          // Checkbox
662          if(isCheckbox && !buttonBitmap)
663          {
664             int height = 16;
665             int start = (clientSize.h - height) / 2;
666
667             if(!isEnabled)
668                // surface.SetBackground(formColor);
669                surface.SetBackground(gainsboro);
670             else if(active && !text)
671                surface.SetBackground((offset ? formColor : Color { 0,0,170 }));
672             else
673                surface.SetBackground((offset ? formColor : white));
674             surface.Area(2, start+2,height-3,start+height-3);
675
676             surface.SetForeground(Color { 85, 85, 85 });
677             surface.HLine(0, height - 2, start + 0);
678             surface.VLine(start+1, start+height - 2, 0);
679
680             surface.SetForeground(Color { 64,64,64 });
681             surface.HLine(1, height - 3, start + 1);
682             surface.VLine(start+2, start+height - 3, 1);
683
684             surface.SetForeground(Color { 212,208,200 });
685             surface.HLine(1, height - 2, start + height-2);
686             surface.VLine(start+1, start+height - 3, height-2);
687
688             surface.SetForeground(white);
689             surface.HLine(0, height - 1, start + height-1);
690             surface.VLine(start+0, start+height - 2, height-1);
691
692             if(checked)
693             {
694                if(active && !text)
695                   surface.SetForeground(white);
696                else if(!isEnabled)
697                   surface.SetForeground(Color { 85, 85, 85 });
698                else
699                   surface.SetForeground(black);
700                surface.DrawLine(4, start+8, 7,start+11);
701                surface.DrawLine(4, start+9, 7,start+12);
702                surface.DrawLine(7, start+11, 11,start+3);
703                surface.DrawLine(7, start+12, 11,start+4);
704             }
705          }
706
707          // Bitmaps
708          if(buttonBitmap)
709          {
710             surface.SetForeground(white);
711             if(isRadio || isCheckbox)
712             {
713                int x = 0, y = (clientSize.h-buttonBitmap.height)/2;
714                if(bevelOver && text)
715                {
716                   x = (CAPTION_DISTANCE-buttonBitmap.width)/2 + offset;
717                   y = (clientSize.h-buttonBitmap.height)/2 + offset;
718
719
720                }
721
722                // Radio Buttons and Checkboxes
723                surface.Blit(buttonBitmap,
724                   x, y,
725                   0,0,buttonBitmap.width,buttonBitmap.height);
726             }
727             else 
728             {
729                // Push Buttons
730                if(scaleBitmap)
731                {
732                   if(bevel || offset)
733                      surface.Stretch(buttonBitmap, 
734                         1 + offset, 1 + offset,0,0,
735                         clientSize.w-3,clientSize.h-3,buttonBitmap.width,buttonBitmap.height);
736                   else
737                      surface.Stretch(buttonBitmap, 0,0, 0,0,
738                         clientSize.w,clientSize.h,buttonBitmap.width,buttonBitmap.height);
739                }
740                else
741                {
742                   int x, y;
743                   bw = buttonBitmap.width;
744                   bh = buttonBitmap.height;
745
746                   if(bitmapAlignment == left || bitmapAlignment == right)
747                   {
748                      if(bitmapAlignment == left)
749                         x = 2;
750                      else
751                         x = clientSize.w-bw-2;
752                      y = (clientSize.h-bh)/2;
753                   }
754                   else if(bitmapAlignment == top || bitmapAlignment == bottom)
755                   {
756                      x = (clientSize.w-bw)/2;
757                      if(bitmapAlignment == top)
758                         y = 2;
759                      else
760                         y = clientSize.h-bh-2;
761                   }
762                   else
763                   {
764                      x = (clientSize.w-bw)/2;
765                      y = (clientSize.h-bh - (int)(buttonStyle.bevelOver && text) * th)/2;
766                   }
767                   if(buttonStyle.bevel || buttonStyle.offset)
768                   {
769                      x += offset;
770                      y += offset;
771                   }
772                   surface.Blit(buttonBitmap, x,y, 0,0, bw,bh);
773                }
774             }
775          }
776
777          // Shadows
778          if(bevel || (bevelOver && (state == down || state == over || checked)))
779          {
780             if(state == down || checked)
781             {
782                surface.SetForeground(Color { 85, 85, 85 });
783                surface.HLine(isDefault + 0, clientSize.w-2-isDefault, 0);
784                surface.VLine(isDefault + 1, clientSize.h-2-isDefault, 0);
785                surface.SetForeground(white);
786                surface.HLine(isDefault + 0, clientSize.w-1-isDefault, clientSize.h-1-isDefault);
787                surface.VLine(isDefault + 0, clientSize.h-2-isDefault, clientSize.w-1-isDefault);
788             }
789             else
790             {
791                surface.SetForeground(white);
792                surface.HLine(0 + isDefault, clientSize.w-2 - isDefault,  isDefault);
793                surface.VLine(1 + isDefault, clientSize.h-2 - isDefault,  isDefault);
794                surface.SetForeground(Color { 85, 85, 85 });
795                surface.HLine(1 + isDefault, clientSize.w-2 - isDefault, clientSize.h-2 - isDefault);
796                surface.VLine(1 + isDefault, clientSize.h-3 - isDefault, clientSize.w-2 - isDefault);
797
798                if(bevel)
799                {
800                   surface.SetForeground(black);
801                   surface.HLine( isDefault, clientSize.w-1 - isDefault, clientSize.h-1 - isDefault);
802                   surface.VLine( isDefault, clientSize.h-2 - isDefault, clientSize.w-1 - isDefault);
803                }
804             }
805          }
806
807          // Text
808          surface.TextOpacity(false);
809          surface.SetForeground(foreground);
810          if(text)
811          {
812             int tw, th;
813             surface.TextExtent(text, strlen(text),&tw, &th);
814
815             if((isRadio || isCheckbox) && !bevelOver)
816                WriteCaption(surface, CAPTION_DISTANCE + 3, // + clientSize.h,
817                   (clientSize.h - th - 4)/2);
818             else 
819             {
820                int x, y = (clientSize.h - th - 1)/2 + offset;
821                
822                if(buttonStyle.bevelOver && buttonBitmap && !buttonStyle.checkBox && !buttonStyle.radio)
823                {
824                   if(bitmapAlignment == top)
825                      y = (clientSize.h - bh - 4 - th - 5)/2 + offset + bh + 4;
826                   else if(bitmapAlignment == bottom)
827                      y = (clientSize.h - bh - 4 - th - 5)/2 + offset;
828                   else//if(bitmapAlignment == left || bitmapAlignment == right)
829                      y = clientSize.h - th - 5 + offset;
830                }
831                else
832                   y = (clientSize.h - th - 1)/2 + offset;
833
834                if(ellipsis)
835                {
836                   int width = clientSize.w - 2*6;
837                   int x = 6 + offset;
838
839                   surface.WriteTextDots(alignment, x, y, width, text, strlen(text));
840                }
841                else
842                {
843                   int width = clientSize.w - 2 * 6;
844                   x = 6 + offset;
845                   if(bitmapAlignment == left || bitmapAlignment == right)
846                   {
847                      if(bitmapAlignment == left)
848                         x += bw + 4;
849                      width -= bw + 4;
850                   }
851                   if(isCheckbox || ((isRadio /*|| bevelOver*/) && buttonBitmap))
852                   {
853                      x += CAPTION_DISTANCE + 3;
854                   }
855
856                   if(tw < width)
857                   {
858                      if(alignment == right)
859                         x += width - tw - 1;
860                      else if(alignment == center)
861                         x += (width - tw) / 2;
862                   }
863                   WriteCaption(surface, x, y);
864                }
865             }
866          }
867
868          // Activation Highlight
869          if(isDefault)
870          {
871             surface.SetForeground(black);
872             surface.Rectangle(0,0,clientSize.w-1,clientSize.h-1);
873          }
874          if(!bevelOver && !isRemote)
875          {
876             if(active) // && (text || !(buttonStyle.radio || buttonStyle.checkBox)))
877             {
878                int x1,y1,x2,y2;
879                surface.SetForeground(black);
880                surface.LineStipple(0x5555);
881
882                if((isRadio || isCheckbox) && text)
883                {
884                   x1 = CAPTION_DISTANCE;  // + clientSize.h;
885                   y1 = 0;
886                   x2 = clientSize.w-4;
887                   y2 = clientSize.h-4;
888                }
889                else
890                {
891                   x1 = 3+offset;
892                   y1 = 3+offset;
893                   x2 = clientSize.w - 5+offset;
894                   y2 = clientSize.h - 5+offset;
895
896                   if(isRadio || isCheckbox)
897                   {
898                      x1-=3;
899                      y1-=3;
900                      x2+=1;
901                      y2+=1;
902                   }
903                }
904                if((x2 - x1) & 1) x2++;
905                if((y2 - y1) & 1) y2++;
906
907                surface.Rectangle(x1, y1, x2, y2);
908                surface.LineStipple(0);
909             }
910          }
911       }
912    }
913 }