FILE COMPARISON
Produced: 2/18/2011 4:20:52 PM
   
Mode:  All Lines  
   
Left file: C:\Documents and Settings\krollins\My Documents\Admin\OpenSource\eRCP\eSWT\current\source\modified\original\Button.c  
Right file: C:\Documents and Settings\krollins\My Documents\Admin\OpenSource\eRCP\eSWT\current\source\modified\Button.c  
1 /******************************************************************************* = 1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 IBM Corporation and others.   2 * Copyright (c) 2000, 2005 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials   3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0   4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at   5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html   6 * http://www.eclipse.org/legal/epl-v10.html
7 *   7 *
8 * Contributors:   8 * Contributors:
9 *     IBM Corporation - initial API and implementation   9 *     IBM Corporation - initial API and implementation
10 *******************************************************************************/   10 *******************************************************************************/
    <> 11 /*******************************************************************************
      12 * Additions/modifications to this source file by Oracle America, Inc. 2011
      13 *******************************************************************************/
11   = 14  
12 #include "ugl_win32.h"   15 #include "ugl_win32.h"
13 #include "UGL_Win32_Widget.h"   16 #include "UGL_Win32_Widget.h"
14 #include "UGL_win32_display.h"   17 #include "UGL_win32_display.h"
15 #include "UGL_Win32_DC.h"   18 #include "UGL_Win32_DC.h"
16     19  
17 #include "Widget.h"   20 #include "Widget.h"
18 #include "Control.h"   21 #include "Control.h"
19 #include "Image.h"   22 #include "Image.h"
20 #include "Button.h"   23 #include "Button.h"
21 #include "Graphics.h"   24 #include "Graphics.h"
22     25  
23 #ifndef _WIN32_WCE +-    
24 // Defining this flag will include code to custom draw the push button.  Owner = 26 // Defining this flag will include code to custom draw the push button.  Owner
25 // drawn push buttons are required to support background colors on buttons   27 // drawn push buttons are required to support background colors on buttons
26  #define UGL_CUSTOM_DRAWN_PUSHBUTTON     <> 28  #define UGL_CUSTOM_DRAWN_PUSHBUTTON
27 #endif      
28   = 29  
29 //========================================================================================   30 //========================================================================================
30 #define UI_CREATE_PUSHBUTTON    1   31 #define UI_CREATE_PUSHBUTTON    1
31 #define UI_CREATE_RADIOBUTTON   2   32 #define UI_CREATE_RADIOBUTTON   2
32 #define UI_CREATE_CHECKBUTTON   3   33 #define UI_CREATE_CHECKBUTTON   3
33 #define UI_CREATE_TOGGLEBUTTON  4   34 #define UI_CREATE_TOGGLEBUTTON  4
34     35  
35 static UGL_Int _Button_New(UGL_Int type, UGL_Int parentHandle, UGL_Int ugl_style, UGL_Error uglError);   36 static UGL_Int _Button_New(UGL_Int type, UGL_Int parentHandle, UGL_Int ugl_style, UGL_Error uglError);
36 static UGL_Int _Button_ExStyle(UGL_Int ugl_style, UGL_Int type);   37 static UGL_Int _Button_ExStyle(UGL_Int ugl_style, UGL_Int type);
37 static UGL_Int _Button_Style(UGL_Int ugl_style, UGL_Int type);   38 static UGL_Int _Button_Style(UGL_Int ugl_style, UGL_Int type);
38 LRESULT _Button_CommandChild(PUGL_Control ugl_control, WPARAM wParam, HWND hWnd);   39 LRESULT _Button_CommandChild(PUGL_Control ugl_control, WPARAM wParam, HWND hWnd);
39 LRESULT CALLBACK _Button_Proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);   40 LRESULT CALLBACK _Button_Proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
40 void Button_DrawImage(PUGL_Control);   41 void Button_DrawImage(PUGL_Control);
41 static void _Button_GetMinimumSize(UGL_Int handle, POINT *point, UGL_Error uglError);   42 static void _Button_GetMinimumSize(UGL_Int handle, POINT *point, UGL_Error uglError);
42 void _Button_DrawFocusRect(HWND hWnd, UINT message);   43 void _Button_DrawFocusRect(HWND hWnd, UINT message);
43 //========================================================================================   44 //========================================================================================
44     45  
45 UGL_Int Checkbox_New(UGL_Int parentHandle, UGL_Int style, UGL_Error uglError) {   46 UGL_Int Checkbox_New(UGL_Int parentHandle, UGL_Int style, UGL_Error uglError) {
46     return _Button_New(UI_CREATE_CHECKBUTTON, parentHandle, style, uglError);   47     return _Button_New(UI_CREATE_CHECKBUTTON, parentHandle, style, uglError);
47 }   48 }
48 //========================================================================================   49 //========================================================================================
49     50  
50 void Button_SetAlignment(UGL_Int handle, UGL_Int alignment, UGL_Error uglError) {   51 void Button_SetAlignment(UGL_Int handle, UGL_Int alignment, UGL_Error uglError) {
51     PUGL_Control ugl_button = UGL_CONTROL(handle);   52     PUGL_Control ugl_button = UGL_CONTROL(handle);
52     53  
53     int bits = GetWindowLong(ugl_button->hWnd, GWL_STYLE);   54     int bits = GetWindowLong(ugl_button->hWnd, GWL_STYLE);
54     int style = bits;   55     int style = bits;
55       56    
56     style &= ~(BS_LEFT | BS_CENTER | BS_RIGHT);   57     style &= ~(BS_LEFT | BS_CENTER | BS_RIGHT);
57       58    
58     if (IS_FLAG_SET(alignment, UGL_STYLE_LEAD)) {   59     if (IS_FLAG_SET(alignment, UGL_STYLE_LEAD)) {
59         style |= BS_LEFT;   60         style |= BS_LEFT;
60     } else if (IS_FLAG_SET(alignment, UGL_STYLE_TRAIL)) {   61     } else if (IS_FLAG_SET(alignment, UGL_STYLE_TRAIL)) {
61         style |= BS_RIGHT;   62         style |= BS_RIGHT;
62     } else if (IS_FLAG_SET(alignment, UGL_STYLE_CENTER)) {   63     } else if (IS_FLAG_SET(alignment, UGL_STYLE_CENTER)) {
63         style |= BS_CENTER;   64         style |= BS_CENTER;
64     }   65     }
65     66  
66     SetWindowLong(ugl_button->hWnd, GWL_STYLE, style);   67     SetWindowLong(ugl_button->hWnd, GWL_STYLE, style);
67     _Control_Redraw(ugl_button);   68     _Control_Redraw(ugl_button);
68 }   69 }
69 //========================================================================================   70 //========================================================================================
70     71  
71 void Button_SetImage(UGL_Int handle, UGL_Int imageHandle, UGL_Error uglError) {   72 void Button_SetImage(UGL_Int handle, UGL_Int imageHandle, UGL_Error uglError) {
72 #ifdef _WIN32_WCE   73 #ifdef _WIN32_WCE
73     PUGL_Control ugl_control = UGL_CONTROL(handle);   74     PUGL_Control ugl_control = UGL_CONTROL(handle);
74     PUGL_Button ugl_button = UGL_BUTTON(handle);   75     PUGL_Button ugl_button = UGL_BUTTON(handle);
75     PUGL_Image ugl_image = UGL_IMAGE(imageHandle);   76     PUGL_Image ugl_image = UGL_IMAGE(imageHandle);
76     ugl_button->ugl_image = ugl_image;   77     ugl_button->ugl_image = ugl_image;
77     if (ugl_image != NULL) {   78     if (ugl_image != NULL) {
78         SetWindowText(ugl_control->hWnd, NULL);   79         SetWindowText(ugl_control->hWnd, NULL);
79     }   80     }
80 #else   81 #else
81     82  
82     PUGL_Control ugl_control = UGL_CONTROL(handle);   83     PUGL_Control ugl_control = UGL_CONTROL(handle);
83     PUGL_Image ugl_image = UGL_IMAGE(imageHandle);   84     PUGL_Image ugl_image = UGL_IMAGE(imageHandle);
84     PUGL_Button ugl_button = UGL_BUTTON(handle);   85     PUGL_Button ugl_button = UGL_BUTTON(handle);
85     int newBits, oldBits;   86     int newBits, oldBits;
86     HANDLE hBitmap;   87     HANDLE hBitmap;
87     88  
88     newBits = GetWindowLong(ugl_control->hWnd, GWL_STYLE);   89     newBits = GetWindowLong(ugl_control->hWnd, GWL_STYLE);
89     oldBits = newBits;   90     oldBits = newBits;
90     91  
91     newBits &= ~(BS_BITMAP | BS_ICON);   92     newBits &= ~(BS_BITMAP | BS_ICON);
92     if (ugl_image != NULL) {   93     if (ugl_image != NULL) {
93         newBits |= BS_BITMAP;   94         newBits |= BS_BITMAP;
94     }   95     }
95     96  
96     if (newBits != oldBits) {   97     if (newBits != oldBits) {
97         SetWindowLong(ugl_control->hWnd, GWL_STYLE, newBits);   98         SetWindowLong(ugl_control->hWnd, GWL_STYLE, newBits);
98     }   99     }
99     100  
100     hBitmap = ugl_image != NULL ? ugl_image->image_handle : NULL;   101     hBitmap = ugl_image != NULL ? ugl_image->image_handle : NULL;
101     SendMessage(ugl_control->hWnd, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);   102     SendMessage(ugl_control->hWnd, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);
102     103  
103     ugl_button->ugl_image = ugl_image;   104     ugl_button->ugl_image = ugl_image;
104     105  
105 #endif   106 #endif
106 }   107 }
107     108  
108 //========================================================================================   109 //========================================================================================
109     110  
110 void Button_SetLabel(UGL_Int handle, UGL_String string, UGL_Error uglError) {   111 void Button_SetLabel(UGL_Int handle, UGL_String string, UGL_Error uglError) {
111     PUGL_Control ugl_button = UGL_CONTROL(handle);   112     PUGL_Control ugl_button = UGL_CONTROL(handle);
112     113  
113 #ifndef _WIN32_WCE   114 #ifndef _WIN32_WCE
114     int newBits = GetWindowLong(ugl_button->hWnd, GWL_STYLE);   115     int newBits = GetWindowLong(ugl_button->hWnd, GWL_STYLE);
115     int oldBits = newBits;   116     int oldBits = newBits;
116     117  
117     newBits &= ~(BS_BITMAP | BS_ICON);   118     newBits &= ~(BS_BITMAP | BS_ICON);
118     119  
119     if (newBits != oldBits) {   120     if (newBits != oldBits) {
120         SetWindowLong(ugl_button->hWnd, GWL_STYLE, newBits);   121         SetWindowLong(ugl_button->hWnd, GWL_STYLE, newBits);
121     }   122     }
122 #endif   123 #endif
123     124  
124     SetWindowText(ugl_button->hWnd, string);   125     SetWindowText(ugl_button->hWnd, string);
125 }   126 }
126 //========================================================================================   127 //========================================================================================
127     128  
128 UGL_Int RadioButton_New(UGL_Int parentHandle, UGL_Int style, UGL_Error uglError) {   129 UGL_Int RadioButton_New(UGL_Int parentHandle, UGL_Int style, UGL_Error uglError) {
129     return _Button_New(UI_CREATE_RADIOBUTTON, parentHandle, style, uglError);   130     return _Button_New(UI_CREATE_RADIOBUTTON, parentHandle, style, uglError);
130 }   131 }
131 //========================================================================================   132 //========================================================================================
132     133  
133 void Button_SetSelection(UGL_Int handle, UGL_Boolean selected, UGL_Error uglError) {   134 void Button_SetSelection(UGL_Int handle, UGL_Boolean selected, UGL_Error uglError) {
134     PUGL_Control ugl_button = UGL_CONTROL(handle);   135     PUGL_Control ugl_button = UGL_CONTROL(handle);
135     int flags = selected ? BST_CHECKED : BST_UNCHECKED;   136     int flags = selected ? BST_CHECKED : BST_UNCHECKED;
136       137    
137     /*   138     /*
138     * Feature in Windows. When BM_SETCHECK is used   139     * Feature in Windows. When BM_SETCHECK is used
139     * to set the checked state of a radio or check   140     * to set the checked state of a radio or check
140     * button, it sets the WM_TABSTOP style.  This   141     * button, it sets the WM_TABSTOP style.  This
141     * is undocumented and unwanted.  The fix is   142     * is undocumented and unwanted.  The fix is
142     * to save and restore the window style bits.   143     * to save and restore the window style bits.
143     */   144     */
144     int bits = GetWindowLong(ugl_button->hWnd, GWL_STYLE);   145     int bits = GetWindowLong(ugl_button->hWnd, GWL_STYLE);
145     SendMessage(ugl_button->hWnd, BM_SETCHECK, flags, 0);   146     SendMessage(ugl_button->hWnd, BM_SETCHECK, flags, 0);
146     SetWindowLong(ugl_button->hWnd, GWL_STYLE, bits);      147     SetWindowLong(ugl_button->hWnd, GWL_STYLE, bits);   
147 }   148 }
148 //========================================================================================   149 //========================================================================================
149     150  
    <> 151 UGL_Boolean Button_GetSelection(UGL_Int handle, UGL_Error uglError)
      152 {
      153     PUGL_Control ugl_button = UGL_CONTROL(handle);
      154     LRESULT state = SendMessage(ugl_button->hWnd, BM_GETSTATE, 0, 0);
      155     if ((state & BST_PUSHED) == BST_PUSHED) {
      156         return UGL_TRUE;
      157     } else {
      158         return UGL_FALSE;
      159     }
      160 }
      161 //========================================================================================
      162  
150 UGL_Int PushButton_New(UGL_Int parentHandle, UGL_Int ugl_style, UGL_Error uglError) { = 163 UGL_Int PushButton_New(UGL_Int parentHandle, UGL_Int ugl_style, UGL_Error uglError) {
151     return _Button_New(UI_CREATE_PUSHBUTTON, parentHandle, ugl_style, uglError);   164     return _Button_New(UI_CREATE_PUSHBUTTON, parentHandle, ugl_style, uglError);
152 }   165 }
153 //========================================================================================   166 //========================================================================================
154     167  
155 UGL_Int ToggleButton_New(UGL_Int parentHandle, UGL_Int ugl_style, UGL_Error uglError) {   168 UGL_Int ToggleButton_New(UGL_Int parentHandle, UGL_Int ugl_style, UGL_Error uglError) {
156     return _Button_New(UI_CREATE_TOGGLEBUTTON, parentHandle, ugl_style, uglError);   169     return _Button_New(UI_CREATE_TOGGLEBUTTON, parentHandle, ugl_style, uglError);
157 }   170 }
158 //========================================================================================   171 //========================================================================================
159     172  
160     173  
161 //========================================================================================   174 //========================================================================================
162 UGL_Int _Button_New(UGL_Int type, UGL_Int parentHandle, UGL_Int ugl_style, UGL_Error uglError) {   175 UGL_Int _Button_New(UGL_Int type, UGL_Int parentHandle, UGL_Int ugl_style, UGL_Error uglError) {
163     int style = _Button_Style(ugl_style, type) | BS_MULTILINE;   176     int style = _Button_Style(ugl_style, type) | BS_MULTILINE;
164     int extended_style = _Button_ExStyle(ugl_style, type);   177     int extended_style = _Button_ExStyle(ugl_style, type);
165     178  
166     PUGL_Control ugl_button = _Control_New(_T("BUTTON"), sizeof(UGL_Button), _Button_Proc,   179     PUGL_Control ugl_button = _Control_New(_T("BUTTON"), sizeof(UGL_Button), _Button_Proc,
167                         parentHandle, ugl_style, style, extended_style, uglError);   180                         parentHandle, ugl_style, style, extended_style, uglError);
168     181  
169     switch (type) {   182     switch (type) {
170         case UI_CREATE_RADIOBUTTON: {   183         case UI_CREATE_RADIOBUTTON: {
171             ((PUGL_Button)ugl_button)->ignoreWMCommand = FALSE;   184             ((PUGL_Button)ugl_button)->ignoreWMCommand = FALSE;
172             ugl_button->inheritsBackground = TRUE;   185             ugl_button->inheritsBackground = TRUE;
173         } break;   186         } break;
174         case UI_CREATE_CHECKBUTTON: {   187         case UI_CREATE_CHECKBUTTON: {
175             ugl_button->inheritsBackground = TRUE;   188             ugl_button->inheritsBackground = TRUE;
176         } break;   189         } break;
177     }   190     }
178       191    
179     return (UGL_Int)ugl_button;   192     return (UGL_Int)ugl_button;
180 }   193 }
181 //========================================================================================   194 //========================================================================================
182     195  
183     196  
184     197  
185 //========================================================================================   198 //========================================================================================
186 UGL_Int _Button_Style(UGL_Int ugl_style, UGL_Int type) {   199 UGL_Int _Button_Style(UGL_Int ugl_style, UGL_Int type) {
187     int bits = _Control_Style(ugl_style);   200     int bits = _Control_Style(ugl_style);
188     201  
189 #ifndef _WIN32_WCE   202 #ifndef _WIN32_WCE
190     if (IS_FLAG_SET(ugl_style, UGL_STYLE_FLAT))         bits |= BS_FLAT;   203     if (IS_FLAG_SET(ugl_style, UGL_STYLE_FLAT))         bits |= BS_FLAT;
191 #endif   204 #endif
192       205    
193     if (IS_FLAG_SET(ugl_style, UGL_STYLE_LEAD))         bits |= BS_LEFT;   206     if (IS_FLAG_SET(ugl_style, UGL_STYLE_LEAD))         bits |= BS_LEFT;
194     else if (IS_FLAG_SET(ugl_style, UGL_STYLE_CENTER))  bits |= BS_CENTER;   207     else if (IS_FLAG_SET(ugl_style, UGL_STYLE_CENTER))  bits |= BS_CENTER;
195     else if (IS_FLAG_SET(ugl_style, UGL_STYLE_TRAIL))   bits |= BS_RIGHT;   208     else if (IS_FLAG_SET(ugl_style, UGL_STYLE_TRAIL))   bits |= BS_RIGHT;
196     209  
197     210  
198     //if ((style & SWT.ARROW) != 0) return bits | OS.BS_OWNERDRAW;   211     //if ((style & SWT.ARROW) != 0) return bits | OS.BS_OWNERDRAW;
199     switch (type) {   212     switch (type) {
200 #ifdef UGL_CUSTOM_DRAWN_PUSHBUTTON   213 #ifdef UGL_CUSTOM_DRAWN_PUSHBUTTON
201         case UI_CREATE_PUSHBUTTON:      bits |= (BS_OWNERDRAW | WS_TABSTOP); break;   214         case UI_CREATE_PUSHBUTTON:      bits |= (BS_OWNERDRAW | WS_TABSTOP); break;
202 #else   215 #else
203         case UI_CREATE_PUSHBUTTON:      bits |= (BS_PUSHBUTTON | WS_TABSTOP); break;   216         case UI_CREATE_PUSHBUTTON:      bits |= (BS_PUSHBUTTON | WS_TABSTOP); break;
204 #endif   217 #endif
205         case UI_CREATE_CHECKBUTTON:     bits |= (BS_CHECKBOX | WS_TABSTOP); break;   218         case UI_CREATE_CHECKBUTTON:     bits |= (BS_CHECKBOX | WS_TABSTOP); break;
206         case UI_CREATE_RADIOBUTTON:     bits |= BS_RADIOBUTTON; break;   219         case UI_CREATE_RADIOBUTTON:     bits |= BS_RADIOBUTTON; break;
207         case UI_CREATE_TOGGLEBUTTON:    bits |= (BS_PUSHLIKE | BS_CHECKBOX); break;   220         case UI_CREATE_TOGGLEBUTTON:    bits |= (BS_PUSHLIKE | BS_CHECKBOX); break;
208         default:                        bits |= (BS_PUSHBUTTON | WS_TABSTOP); break;   221         default:                        bits |= (BS_PUSHBUTTON | WS_TABSTOP); break;
209     }   222     }
210     223  
211     return bits;   224     return bits;
212 }   225 }
213     226  
214 //========================================================================================   227 //========================================================================================
215 #ifdef UGL_CUSTOM_DRAWN_PUSHBUTTON   228 #ifdef UGL_CUSTOM_DRAWN_PUSHBUTTON
216 void _Button_OwnerDraw(PUGL_Control button, LPDRAWITEMSTRUCT drawItemStruct) {   229 void _Button_OwnerDraw(PUGL_Control button, LPDRAWITEMSTRUCT drawItemStruct) {
217     HDC hdc = drawItemStruct->hDC;   230     HDC hdc = drawItemStruct->hDC;
218     LPRECT rect = &drawItemStruct->rcItem;   231     LPRECT rect = &drawItemStruct->rcItem;
219     int textLength;   232     int textLength;
220     int oldBkMode;   233     int oldBkMode;
221     LPTSTR text;   234     LPTSTR text;
222     HBRUSH backgroundBrush;   235     HBRUSH backgroundBrush;
    -+ 236     COLORREF foreground, oldForeground;
223     HWND hWnd = button->hWnd; = 237     HWND hWnd = button->hWnd;
224     BOOL disabled = (drawItemStruct->itemState & ODS_DISABLED) == ODS_DISABLED;   238     BOOL disabled = (drawItemStruct->itemState & ODS_DISABLED) == ODS_DISABLED;
    -+ 239     BOOL pushed = (drawItemStruct->itemState & ODS_SELECTED) == ODS_SELECTED;
      240     BOOL focused = (drawItemStruct->itemState & ODS_FOCUS) == ODS_FOCUS;
225     PUGL_Button ugl_button = (PUGL_Button) button; = 241     PUGL_Button ugl_button = (PUGL_Button) button;
226     PUGL_Image image = ugl_button->ugl_image;   242     PUGL_Image image = ugl_button->ugl_image;
227     BITMAP bitmap;   243     BITMAP bitmap;
228     <> 244  
229     UINT flags = ((drawItemStruct->itemState & ODS_SELECTED) == ODS_SELECTED ? DFCS_PUSHED : 0);   245  
      246     UINT flags = (pushed ? DFCS_PUSHED : 0);
230     int bits = GetWindowLong(hWnd, GWL_STYLE); = 247     int bits = GetWindowLong(hWnd, GWL_STYLE);
231     int align; //the horizontal alignment of the text to be drawn   248     int align; //the horizontal alignment of the text to be drawn
232     /*   249     /*
233      * Check the button style and set the drawing style accordingly.   250      * Check the button style and set the drawing style accordingly.
234      * The logic here is a little strange, but it is because BS_CENTER   251      * The logic here is a little strange, but it is because BS_CENTER
235      * also seems to set all the bits of BS_RIGHT, so if we just make   252      * also seems to set all the bits of BS_RIGHT, so if we just make
236      * BS_CENTER the default, without checking it first, we end up with   253      * BS_CENTER the default, without checking it first, we end up with
237      * right-justified text.   254      * right-justified text.
238      * This flag is also used for manually determining the position of the   255      * This flag is also used for manually determining the position of the
239      * button text in the case of a disabled button.   256      * button text in the case of a disabled button.
240      */   257      */
241     if (IS_FLAG_SET(bits, BS_CENTER)) align = DT_CENTER;   258     if (IS_FLAG_SET(bits, BS_CENTER)) align = DT_CENTER;
242     else if (IS_FLAG_SET(bits, BS_RIGHT)) align = DT_RIGHT;   259     else if (IS_FLAG_SET(bits, BS_RIGHT)) align = DT_RIGHT;
243     else if (IS_FLAG_SET(bits, BS_LEFT)) align = DT_LEFT;   260     else if (IS_FLAG_SET(bits, BS_LEFT)) align = DT_LEFT;
244     else align = DT_CENTER;   261     else align = DT_CENTER;
245     262  
246     if (disabled) flags |= DFCS_INACTIVE;   263     if (disabled) flags |= DFCS_INACTIVE;
    <> 264  
      265     // LRN - To match the original control behavior, don't draw frame on image buttons
      266     if (image == NULL) {
      267 #ifndef _WIN32_WCE
247     if (IS_FLAG_SET(bits, BS_FLAT)) flags |= DFCS_FLAT;   268         if (IS_FLAG_SET(bits, BS_FLAT)) flags |= DFCS_FLAT;
248     DrawFrameControl(hdc, rect, DFC_BUTTON, flags | DFCS_BUTTONPUSH | DFCS_ADJUSTRECT);   269         DrawFrameControl(hdc, rect, DFC_BUTTON, flags | DFCS_BUTTONPUSH | DFCS_ADJUSTRECT);
      270 #else
      271         DrawFrameControl(hdc, rect, DFC_BUTTON, flags | DFCS_BUTTONPUSH);
      272         rect->top += 1;
      273         rect->left += 1;
      274         rect->bottom -= 1;
      275         rect->right -= 1;
      276 #endif
      277     }
249   = 278  
250     // Update the background color of the button   279     // Update the background color of the button
    -+ 280 #ifdef _WIN32_WCE
      281     if (pushed) {
      282         backgroundBrush = GetStockObject(BLACK_BRUSH);
      283     } else
      284 #endif     
251     if (button->background_color != NULL) { = 285     if (button->background_color != NULL) {
252         backgroundBrush = Color_GetBrush(button->background_color);   286         backgroundBrush = Color_GetBrush(button->background_color);
253     } else {   287     } else {
254         backgroundBrush = GetSysColorBrush(COLOR_BTNFACE);   288         backgroundBrush = GetSysColorBrush(COLOR_BTNFACE);
255     }   289     }
256     FillRect(hdc, rect, backgroundBrush);   290     FillRect(hdc, rect, backgroundBrush);
257     291  
    <> 292     // Update the foreground color of the button
      293 #ifdef _WIN32_WCE
      294     if (pushed) {
      295         foreground = RGB(255, 255, 255);
      296     } else
      297 #endif
      298     if (button->foreground_color != NULL) {
      299         foreground = button->foreground_color->color;
      300     } else {
      301         foreground = GetSysColor(COLOR_BTNTEXT);
      302     }
      303  
258     // Draw the focus rectangle if the button has focus. = 304     // Draw the focus rectangle if the button has focus.
259     // NOTE:  Drawing the focus rectangle after the fillbackground   305     // NOTE:  Drawing the focus rectangle after the fillbackground
260     // happens is important so that the focus rectangle will still   306     // happens is important so that the focus rectangle will still
261     // be visible with a non-default background color   307     // be visible with a non-default background color
262     if ((drawItemStruct->itemState & ODS_FOCUS) == ODS_FOCUS) { <> 308     if (focused && !pushed) {
263         DrawFocusRect(hdc, rect); = 309         DrawFocusRect(hdc, rect);
264     }   310     }
265     311  
266     textLength = GetWindowTextLength(hWnd)+1;   312     textLength = GetWindowTextLength(hWnd)+1;
267     text = calloc(sizeof(TCHAR), textLength);   313     text = calloc(sizeof(TCHAR), textLength);
268     GetWindowText(hWnd, text, textLength);   314     GetWindowText(hWnd, text, textLength);
269     315  
270     oldBkMode = SetBkMode(hdc, TRANSPARENT);   316     oldBkMode = SetBkMode(hdc, TRANSPARENT);
    <> 317  
      318 #ifndef _WIN32_WCE
271     if (!disabled) { = 319     if (!disabled) {
    <> 320 #endif
272         if(image != NULL){   321         if (image != NULL) {
273             PUGL_DC ugl_dc; = 322             PUGL_DC ugl_dc;
274             int imageWidth, imageHeight;   323             int imageWidth, imageHeight;
275             GetObject(ugl_button->ugl_image->image_handle, sizeof(BITMAP), &bitmap);   324             GetObject(ugl_button->ugl_image->image_handle, sizeof(BITMAP), &bitmap);
276             imageWidth = bitmap.bmWidth;   325             imageWidth = bitmap.bmWidth;
277             imageHeight = bitmap.bmHeight;   326             imageHeight = bitmap.bmHeight;
278             ugl_dc = _internal_new_DC(hdc, NULL,rect, NULL);   327             ugl_dc = _internal_new_DC(hdc, NULL,rect, NULL);
279         if (align == DT_LEFT) { <> 328             if (align == DT_LEFT) {
280             Graphics_DrawImage((UGL_Int)ugl_dc, (UGL_Int)ugl_button->ugl_image, 2, ((rect->bottom-rect->top)- imageHeight)/2, NULL);   329                 Graphics_DrawImage((UGL_Int)ugl_dc, (UGL_Int)ugl_button->ugl_image, 2, ((rect->bottom-rect->top)- imageHeight)/2, NULL);
281         } else if (align == DT_RIGHT) {   330             } else if (align == DT_RIGHT) {
282             Graphics_DrawImage((UGL_Int)ugl_dc, (UGL_Int)ugl_button->ugl_image, ((rect->right-rect->left)-imageWidth)-2, ((rect->bottom-rect->top)- imageHeight)/2, NULL);   331                 Graphics_DrawImage((UGL_Int)ugl_dc, (UGL_Int)ugl_button->ugl_image, ((rect->right-rect->left)-imageWidth)-2, ((rect->bottom-rect->top)- imageHeight)/2, NULL);
283         } else if (align == DT_CENTER) {   332             } else if (align == DT_CENTER) {
284             Graphics_DrawImage((UGL_Int)ugl_dc, (UGL_Int)ugl_button->ugl_image, ((rect->right-rect->left)-imageWidth)/2, ((rect->bottom-rect->top)- imageHeight)/2, NULL);   333                 Graphics_DrawImage((UGL_Int)ugl_dc, (UGL_Int)ugl_button->ugl_image, ((rect->right-rect->left)-imageWidth)/2, ((rect->bottom-rect->top)- imageHeight)/2, NULL);
285         } else {   334             } else {
286             Graphics_DrawImage((UGL_Int)ugl_dc, (UGL_Int)ugl_button->ugl_image, ((rect->right-rect->left)-imageWidth)/2, ((rect->bottom-rect->top)- imageHeight)/2, NULL);   335                 Graphics_DrawImage((UGL_Int)ugl_dc, (UGL_Int)ugl_button->ugl_image, ((rect->right-rect->left)-imageWidth)/2, ((rect->bottom-rect->top)- imageHeight)/2, NULL);
287         }   336             }
288     337         } else {
      338 #ifdef _WIN32_WCE
      339             if (!disabled) {
      340 #endif
      341                 oldForeground = SetTextColor(hdc, foreground);
      342 #ifdef _WIN32_WCE
289         } else{   343             } else {
      344                 oldForeground = SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
      345             }
      346 #endif
290             DrawText(hdc, text, -1, rect, align | DT_VCENTER | DT_SINGLELINE); = 347             DrawText(hdc, text, -1, rect, align | DT_VCENTER | DT_SINGLELINE);
    -+ 348             SetTextColor(hdc, oldForeground);
291         } = 349         }
292   <> 350 #ifndef _WIN32_WCE
293     } else { = 351     } else {
294         int buttonWidth, buttonHeight;   352         int buttonWidth, buttonHeight;
295         int x,y,width,height;   353         int x,y,width,height;
296     354  
297         // Cache to size of the button because the next call to DrawText will modify the   355         // Cache to size of the button because the next call to DrawText will modify the
298         // values in the rect structure   356         // values in the rect structure
299         buttonWidth = rect->right - rect->left;   357         buttonWidth = rect->right - rect->left;
300         buttonHeight = rect->bottom - rect->top;   358         buttonHeight = rect->bottom - rect->top;
301     359  
302         // Caclulate the size required to draw the string.    360         // Caclulate the size required to draw the string. 
303         DrawText(hdc, text, -1, rect, DT_CALCRECT);   361         DrawText(hdc, text, -1, rect, DT_CALCRECT);
304     362  
305         // Caclulate the correct position of the string to center it in the button   363         // Caclulate the correct position of the string to center it in the button
306         width = rect->right - rect->left;   364         width = rect->right - rect->left;
307         height = rect->bottom - rect->top;   365         height = rect->bottom - rect->top;
308     366  
309         x = rect->left;   367         x = rect->left;
310         y = rect->top + (buttonHeight-height)/2;   368         y = rect->top + (buttonHeight-height)/2;
311     369  
312         if (align == DT_RIGHT){   370         if (align == DT_RIGHT){
313             x = x + (buttonWidth - width) - 1;   371             x = x + (buttonWidth - width) - 1;
314         }   372         }
315         else if (align == DT_LEFT){   373         else if (align == DT_LEFT){
316             x = x + 1; //plus 1 and minus 1 (above) just to keep things spacious   374             x = x + 1; //plus 1 and minus 1 (above) just to keep things spacious
317         }   375         }
318         else { //center is the default and all that PPro uses   376         else { //center is the default and all that PPro uses
319             x = x + (buttonWidth-width)/2;   377             x = x + (buttonWidth-width)/2;
320         }   378         }
321     379  
322         DrawState(hdc, NULL, NULL, (LPARAM)text, 0, x,y, width, height, DST_TEXT | DSS_DISABLED);   380         DrawState(hdc, NULL, NULL, (LPARAM)text, 0, x,y, width, height, DST_TEXT | DSS_DISABLED);
323   <>    
324     } = 381     }
    -+ 382 #endif
325   = 383  
326     free(text);   384     free(text);
327     385  
328     SetBkMode(hdc, oldBkMode);   386     SetBkMode(hdc, oldBkMode);
329 }   387 }
330 #endif   388 #endif
331     389  
332 //========================================================================================   390 //========================================================================================
333 LRESULT CALLBACK _Button_Proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {   391 LRESULT CALLBACK _Button_Proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
334     392  
335     switch (message) {   393     switch (message) {
336         case UI_COMMAND_CHILD: {                      394         case UI_COMMAND_CHILD: {                   
337             PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);   395             PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);
338             return _Button_CommandChild(ugl_control, wParam, hWnd);   396             return _Button_CommandChild(ugl_control, wParam, hWnd);
339             } break;   397             } break;
340     398  
341         case UI_GET_MINIMUM_SIZE: {   399         case UI_GET_MINIMUM_SIZE: {
342             PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);   400             PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);
343             UGL_Int handle = (UGL_Int)ugl_control;   401             UGL_Int handle = (UGL_Int)ugl_control;
344             POINT* point = (POINT*)wParam;   402             POINT* point = (POINT*)wParam;
345             _Button_GetMinimumSize(handle, point, (UGL_Error)lParam);   403             _Button_GetMinimumSize(handle, point, (UGL_Error)lParam);
346             return 0;   404             return 0;
347             } break;   405             } break;
348         case UI_GETBACKGROUNDRGB: {   406         case UI_GETBACKGROUNDRGB: {
349             LONG style = GetWindowLong(hWnd, GWL_STYLE);   407             LONG style = GetWindowLong(hWnd, GWL_STYLE);
350               408            
351             // BS_OWNERDRAW style is combination of both   409             // BS_OWNERDRAW style is combination of both
352             // BS_CHECKBOX and BS_RADIOBUTTON so we must   410             // BS_CHECKBOX and BS_RADIOBUTTON so we must
353             // check that the button is not owner drawn before   411             // check that the button is not owner drawn before
354             // checking if it is a checkbox or radio button   412             // checking if it is a checkbox or radio button
355             if ((style & BS_OWNERDRAW) != BS_OWNERDRAW) {   413             if ((style & BS_OWNERDRAW) != BS_OWNERDRAW) {
356                 if (((style & BS_CHECKBOX) == BS_CHECKBOX) ||   414                 if (((style & BS_CHECKBOX) == BS_CHECKBOX) ||
357                     ((style & BS_RADIOBUTTON) == BS_RADIOBUTTON)) {   415                     ((style & BS_RADIOBUTTON) == BS_RADIOBUTTON)) {
358                         // The background for checkboxes and radio buttons is the same as   416                         // The background for checkboxes and radio buttons is the same as
359                         // the parents background   417                         // the parents background
360                         return SendMessage(GetParent(hWnd), UI_GETBACKGROUNDRGB, 0, 0);   418                         return SendMessage(GetParent(hWnd), UI_GETBACKGROUNDRGB, 0, 0);
361                 }   419                 }
362             }   420             }
363     421  
364             return (UGL_Int)GetSysColor(COLOR_BTNFACE);   422             return (UGL_Int)GetSysColor(COLOR_BTNFACE);
365         } break;   423         } break;
366         case UI_SET_FOCUS:{   424         case UI_SET_FOCUS:{
367             HWND result;   425             HWND result;
368               426            
369            /*   427            /*
370             * This extra logic exists because of a bug (or at least undesireable behavior) in Windows.   428             * This extra logic exists because of a bug (or at least undesireable behavior) in Windows.
371             * Ultimately, ForceFocus() results in a WM_Command message. Mouse clicks also result   429             * Ultimately, ForceFocus() results in a WM_Command message. Mouse clicks also result
372             * in this message. We are supposed to be able to differentiate where the message   430             * in this message. We are supposed to be able to differentiate where the message
373             * comes from based on the HIWORD of the WPARAM, but that value is the same for   431             * comes from based on the HIWORD of the WPARAM, but that value is the same for
374             * 'tabbing' and for mouse clicks.   432             * 'tabbing' and for mouse clicks.
375             * That's a problem because it means that by tabbing between RadioButtons, we also   433             * That's a problem because it means that by tabbing between RadioButtons, we also
376             * toggle which one is selected. The fix is this workaround, which basically   434             * toggle which one is selected. The fix is this workaround, which basically
377             * amounts to setting a flag on the PUGL_Button struct.   435             * amounts to setting a flag on the PUGL_Button struct.
378             */   436             */
379             PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);   437             PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);
380             PUGL_Button ugl_button = (PUGL_Button)ugl_control;   438             PUGL_Button ugl_button = (PUGL_Button)ugl_control;
381             int style = GetWindowLong(ugl_control->hWnd, GWL_STYLE);   439             int style = GetWindowLong(ugl_control->hWnd, GWL_STYLE);
382             if (IS_FLAG_SET(style, BS_RADIOBUTTON)) {   440             if (IS_FLAG_SET(style, BS_RADIOBUTTON)) {
383                 ugl_button->ignoreWMCommand = TRUE;   441                 ugl_button->ignoreWMCommand = TRUE;
384             }   442             }
385             result = SetFocus((HWND)wParam);   443             result = SetFocus((HWND)wParam);
386             if (IS_FLAG_SET(style, BS_RADIOBUTTON)) {   444             if (IS_FLAG_SET(style, BS_RADIOBUTTON)) {
387                 ugl_button->ignoreWMCommand = FALSE;   445                 ugl_button->ignoreWMCommand = FALSE;
388             }   446             }
389             return (result != NULL);   447             return (result != NULL);
390         } break;   448         } break;
391     449  
392 #ifdef UGL_CUSTOM_DRAWN_PUSHBUTTON   450 #ifdef UGL_CUSTOM_DRAWN_PUSHBUTTON
393         case UI_DRAWITEM: {   451         case UI_DRAWITEM: {
394             PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);   452             PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);
395             LPDRAWITEMSTRUCT drawItemStruct = (LPDRAWITEMSTRUCT)lParam;   453             LPDRAWITEMSTRUCT drawItemStruct = (LPDRAWITEMSTRUCT)lParam;
    -+ 454             LPRECT rect = &drawItemStruct->rcItem;
396             _Button_OwnerDraw(ugl_control, drawItemStruct); = 455             _Button_OwnerDraw(ugl_control, drawItemStruct);
    -+ 456             PaintCallback(UGL_WIDGET(ugl_control)->callback_target, rect->left, rect->top, (rect->right - rect->left), (rect->bottom - rect->top));
397             return TRUE; = 457             return TRUE;
398                           } break; <> 458         } break;
      459  
399 //fix for bug 158718 space doesn't generate button "click" = 460 //fix for bug 158718 space doesn't generate button "click"
400         case WM_KEYDOWN:{   461         case WM_KEYDOWN:{
401           462        
402             if(wParam == VK_SPACE){   463             if(wParam == VK_SPACE){
403                 int state;   464                 int state;
404                 BOOL selected;   465                 BOOL selected;
405                 int style = GetWindowLong(hWnd, GWL_STYLE);   466                 int style = GetWindowLong(hWnd, GWL_STYLE);
406               467            
407                 PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);   468                 PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);
408                 UGL_Int handle = (UGL_Int)ugl_control;   469                 UGL_Int handle = (UGL_Int)ugl_control;
409                 PUGL_Control ugl_button = UGL_CONTROL(handle);   470                 PUGL_Control ugl_button = UGL_CONTROL(handle);
410     471  
411                 if (IS_FLAG_SET(style, BS_RADIOBUTTON)) {   472                 if (IS_FLAG_SET(style, BS_RADIOBUTTON)) {
412                     /*   473                     /*
413                     * The explanation for this is given in detail   474                     * The explanation for this is given in detail
414                     * in the case statement for UI_SET_FOCUS in _Button_Proc   475                     * in the case statement for UI_SET_FOCUS in _Button_Proc
415                     */   476                     */
416                     if (((PUGL_Button)ugl_button)->ignoreWMCommand) {   477                     if (((PUGL_Button)ugl_button)->ignoreWMCommand) {
417                         return 0;   478                         return 0;
418                     }   479                     }
419                 }   480                 }
420                 state = SendMessage(hWnd, BM_GETCHECK, 0, 0);   481                 state = SendMessage(hWnd, BM_GETCHECK, 0, 0);
421                 selected = (state & BST_CHECKED) != 0;   482                 selected = (state & BST_CHECKED) != 0;
422                   483                
423                 Button_SetSelection(handle, !selected, NULL);   484                 Button_SetSelection(handle, !selected, NULL);
424                 ButtonCallback(UGL_WIDGET(ugl_button)->callback_target);   485                 ButtonCallback(UGL_WIDGET(ugl_button)->callback_target);
425                 }   486                 }
426         } break;   487         } break;
427         case WM_LBUTTONUP:{   488         case WM_LBUTTONUP:{
428     489  
429         int state;   490         int state;
430         BOOL selected;   491         BOOL selected;
431         int style = GetWindowLong(hWnd, GWL_STYLE);   492         int style = GetWindowLong(hWnd, GWL_STYLE);
432       493    
433         PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);   494         PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);
434         UGL_Int handle = (UGL_Int)ugl_control;   495         UGL_Int handle = (UGL_Int)ugl_control;
435         PUGL_Control ugl_button = UGL_CONTROL(handle);   496         PUGL_Control ugl_button = UGL_CONTROL(handle);
436     497  
437         if (IS_FLAG_SET(style, BS_RADIOBUTTON)) {   498         if (IS_FLAG_SET(style, BS_RADIOBUTTON)) {
438             /*   499             /*
439              * The explanation for this is given in detail   500              * The explanation for this is given in detail
440              * in the case statement for UI_SET_FOCUS in _Button_Proc   501              * in the case statement for UI_SET_FOCUS in _Button_Proc
441              */   502              */
442             if (((PUGL_Button)ugl_button)->ignoreWMCommand) {   503             if (((PUGL_Button)ugl_button)->ignoreWMCommand) {
443                 return 0;   504                 return 0;
444             }   505             }
445         }   506         }
446         state = SendMessage(hWnd, BM_GETCHECK, 0, 0);   507         state = SendMessage(hWnd, BM_GETCHECK, 0, 0);
447         selected = (state & BST_CHECKED) != 0;   508         selected = (state & BST_CHECKED) != 0;
448           509        
449         Button_SetSelection(handle, !selected, NULL);   510         Button_SetSelection(handle, !selected, NULL);
450         ButtonCallback(UGL_WIDGET(ugl_button)->callback_target);   511         ButtonCallback(UGL_WIDGET(ugl_button)->callback_target);
451         } break;   512         } break;
452 #else   513 #else
453     514  
454         case WM_KEYDOWN:{   515         case WM_KEYDOWN:{
455         //fix for bug 158718 space doesn't generate button "click"   516         //fix for bug 158718 space doesn't generate button "click"
456 #if defined (WIN32_PLATFORM_WFSP)   517 #if defined (WIN32_PLATFORM_WFSP)
457             if(wParam == VK_RETURN) {   518             if(wParam == VK_RETURN) {
458 #else   519 #else
459             if(wParam == VK_SPACE || wParam == VK_RETURN) {   520             if(wParam == VK_SPACE || wParam == VK_RETURN) {
460 #endif   521 #endif
461                 int state;   522                 int state;
462                 BOOL selected;   523                 BOOL selected;
463                 int style = GetWindowLong(hWnd, GWL_STYLE);   524                 int style = GetWindowLong(hWnd, GWL_STYLE);
464               525            
465                 PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);   526                 PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);
466                 UGL_Int handle = (UGL_Int)ugl_control;   527                 UGL_Int handle = (UGL_Int)ugl_control;
467                 PUGL_Button ugl_button = (PUGL_Button) ugl_control;   528                 PUGL_Button ugl_button = (PUGL_Button) ugl_control;
468     529  
469                 if (IS_FLAG_SET(style, BS_RADIOBUTTON)) {   530                 if (IS_FLAG_SET(style, BS_RADIOBUTTON)) {
470                     /*   531                     /*
471                     * The explanation for this is given in detail   532                     * The explanation for this is given in detail
472                     * in the case statement for UI_SET_FOCUS in _Button_Proc   533                     * in the case statement for UI_SET_FOCUS in _Button_Proc
473                     */   534                     */
474                     if (ugl_button->ignoreWMCommand) {   535                     if (ugl_button->ignoreWMCommand) {
475                         return 0;   536                         return 0;
476                     }   537                     }
477                 }   538                 }
478                 state = SendMessage(hWnd, BM_GETCHECK, 0, 0);   539                 state = SendMessage(hWnd, BM_GETCHECK, 0, 0);
479                 selected = (state & BST_CHECKED) != 0;   540                 selected = (state & BST_CHECKED) != 0;
480                   541                
481                 Button_SetSelection(handle, !selected, NULL);   542                 Button_SetSelection(handle, !selected, NULL);
482                   543                
483                 ButtonCallback(UGL_WIDGET(ugl_button)->callback_target);   544                 ButtonCallback(UGL_WIDGET(ugl_button)->callback_target);
484                     _Control_Proc(hWnd, message, wParam, lParam);   545                     _Control_Proc(hWnd, message, wParam, lParam);
485                     if(ugl_button->ugl_image != NULL){   546                     if(ugl_button->ugl_image != NULL){
486                         Button_DrawImage(ugl_control);   547                         Button_DrawImage(ugl_control);
487                     }   548                     }
488                 return TRUE;   549                 return TRUE;
489             }   550             }
490         } break;   551         } break;
491     552  
492         case WM_SYSKEYUP:   553         case WM_SYSKEYUP:
493         case WM_KEYUP:{   554         case WM_KEYUP:{
494 #if defined (WIN32_PLATFORM_WFSP)   555 #if defined (WIN32_PLATFORM_WFSP)
495             if(wParam == VK_RETURN) {   556             if(wParam == VK_RETURN) {
496 #else   557 #else
497             if(wParam == VK_SPACE || wParam == VK_RETURN) {   558             if(wParam == VK_SPACE || wParam == VK_RETURN) {
498 #endif   559 #endif
499                 PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);   560                 PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);
500                 UGL_Int handle = (UGL_Int)ugl_control;   561                 UGL_Int handle = (UGL_Int)ugl_control;
501     562  
502                 LRESULT result = _Control_Proc(hWnd, message, wParam, lParam);   563                 LRESULT result = _Control_Proc(hWnd, message, wParam, lParam);
503     564  
504                 _Button_DrawFocusRect(hWnd, message);   565                 _Button_DrawFocusRect(hWnd, message);
505                 return result;   566                 return result;
506             }   567             }
507         } break;   568         } break;
508     569  
509         case WM_LBUTTONUP:{   570         case WM_LBUTTONUP:{
510         int state;   571         int state;
511         BOOL selected;   572         BOOL selected;
512         int style = GetWindowLong(hWnd, GWL_STYLE);   573         int style = GetWindowLong(hWnd, GWL_STYLE);
513       574    
514         PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);   575         PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);
515         UGL_Int handle = (UGL_Int)ugl_control;   576         UGL_Int handle = (UGL_Int)ugl_control;
516         PUGL_Button ugl_button = (PUGL_Button) ugl_control;   577         PUGL_Button ugl_button = (PUGL_Button) ugl_control;
517     578  
518         if (IS_FLAG_SET(style, BS_RADIOBUTTON)) {   579         if (IS_FLAG_SET(style, BS_RADIOBUTTON)) {
519             /*   580             /*
520              * The explanation for this is given in detail   581              * The explanation for this is given in detail
521              * in the case statement for UI_SET_FOCUS in _Button_Proc   582              * in the case statement for UI_SET_FOCUS in _Button_Proc
522              */   583              */
523             if (ugl_button->ignoreWMCommand) {   584             if (ugl_button->ignoreWMCommand) {
524                 return 0;   585                 return 0;
525             }   586             }
526         }   587         }
527         state = SendMessage(hWnd, BM_GETCHECK, 0, 0);   588         state = SendMessage(hWnd, BM_GETCHECK, 0, 0);
528         selected = (state & BST_CHECKED) != 0;   589         selected = (state & BST_CHECKED) != 0;
529           590        
530         Button_SetSelection(handle, !selected, NULL);   591         Button_SetSelection(handle, !selected, NULL);
531           592        
532         ButtonCallback(UGL_WIDGET(ugl_button)->callback_target);   593         ButtonCallback(UGL_WIDGET(ugl_button)->callback_target);
533             _Control_Proc(hWnd, message, wParam, lParam);   594             _Control_Proc(hWnd, message, wParam, lParam);
534             if(ugl_button->ugl_image != NULL){   595             if(ugl_button->ugl_image != NULL){
535                 Button_DrawImage(ugl_control);   596                 Button_DrawImage(ugl_control);
536             }   597             }
537             _Button_DrawFocusRect(hWnd, message);   598             _Button_DrawFocusRect(hWnd, message);
538             return TRUE;   599             return TRUE;
539         }   600         }
540     601  
541         case WM_PAINT: {   602         case WM_PAINT: {
542               603            
543             PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);   604             PUGL_Control ugl_control = (PUGL_Control)GetWindowLong(hWnd, GWL_USERDATA);
544             PUGL_Button ugl_button = (PUGL_Button) ugl_control;   605             PUGL_Button ugl_button = (PUGL_Button) ugl_control;
545             _Control_Proc(hWnd, message, wParam, lParam);   606             _Control_Proc(hWnd, message, wParam, lParam);
546             if(ugl_button->ugl_image != NULL){   607             if(ugl_button->ugl_image != NULL){
547                 Button_DrawImage(ugl_control);   608                 Button_DrawImage(ugl_control);
548             }   609             }
549             if(ugl_control->hWnd == GetFocus()) {   610             if(ugl_control->hWnd == GetFocus()) {
550                 _Button_DrawFocusRect(hWnd, message);   611                 _Button_DrawFocusRect(hWnd, message);
551             }   612             }
552                 return TRUE;   613                 return TRUE;
553         }   614         }
554         case WM_KILLFOCUS: {   615         case WM_KILLFOCUS: {
555             _Button_DrawFocusRect(hWnd, message);   616             _Button_DrawFocusRect(hWnd, message);
556         } break;   617         } break;
557         case WM_SETFOCUS: {   618         case WM_SETFOCUS: {
558             _Button_DrawFocusRect(hWnd, message);   619             _Button_DrawFocusRect(hWnd, message);
559         } break;   620         } break;
560 #endif   621 #endif
561     }   622     }
562     return _Control_Proc(hWnd, message, wParam, lParam);   623     return _Control_Proc(hWnd, message, wParam, lParam);
563 }   624 }
564 //========================================================================================   625 //========================================================================================
565     626  
566 LRESULT _Button_CommandChild(PUGL_Control ugl_control, WPARAM wParam, HWND hWnd){   627 LRESULT _Button_CommandChild(PUGL_Control ugl_control, WPARAM wParam, HWND hWnd){
567     UGL_Int handle = (UGL_Int)ugl_control;   628     UGL_Int handle = (UGL_Int)ugl_control;
568     PUGL_Control ugl_button = UGL_CONTROL(handle);   629     PUGL_Control ugl_button = UGL_CONTROL(handle);
569     LRESULT result = 0;   630     LRESULT result = 0;
570     int code = wParam >> 16;   631     int code = wParam >> 16;
571     632  
572     switch (code) {   633     switch (code) {
573 #ifndef _WIN32_WCE   634 #ifndef _WIN32_WCE
574         case BN_DOUBLECLICKED:   635         case BN_DOUBLECLICKED:
575 #endif   636 #endif
576         case BN_CLICKED: {   637         case BN_CLICKED: {
577     638  
578 /*   639 /*
579     640  
580     // this is being marked out and moved to the button proc's WM_LBUTTONUP event, this is for fixing the bug JYJC6QK9ZP where   641     // this is being marked out and moved to the button proc's WM_LBUTTONUP event, this is for fixing the bug JYJC6QK9ZP where
581     // the button does not generate a clicked event when the button is re-layoutted to other location while being clicked   642     // the button does not generate a clicked event when the button is re-layoutted to other location while being clicked
582     // the button now uses the WM_LBUTTONUP to detect the button click.   643     // the button now uses the WM_LBUTTONUP to detect the button click.
583     644  
584             //if ((style & (SWT.CHECK | SWT.RADIO | SWT.TOGGLE)) == 0) return false;   645             //if ((style & (SWT.CHECK | SWT.RADIO | SWT.TOGGLE)) == 0) return false;
585             int state;   646             int state;
586             BOOL selected;   647             BOOL selected;
587             int style = GetWindowLong(ugl_button->hWnd, GWL_STYLE);   648             int style = GetWindowLong(ugl_button->hWnd, GWL_STYLE);
588             fprintf(pFile, "bn clicked 00\n");   649             fprintf(pFile, "bn clicked 00\n");
589             if (IS_FLAG_SET(style, BS_RADIOBUTTON)) {   650             if (IS_FLAG_SET(style, BS_RADIOBUTTON)) {
590             */   651             */
591                 /*   652                 /*
592                  * The explanation for this is given in detail   653                  * The explanation for this is given in detail
593                  * in the case statement for UI_SET_FOCUS in _Button_Proc   654                  * in the case statement for UI_SET_FOCUS in _Button_Proc
594                  */   655                  */
595             /*   656             /*
596                 if (((PUGL_Button)ugl_button)->ignoreWMCommand) {   657                 if (((PUGL_Button)ugl_button)->ignoreWMCommand) {
597                     return 0;   658                     return 0;
598                 }   659                 }
599             }   660             }
600             state = SendMessage(hWnd, BM_GETCHECK, 0, 0);   661             state = SendMessage(hWnd, BM_GETCHECK, 0, 0);
601             selected = (state & BST_CHECKED) != 0;   662             selected = (state & BST_CHECKED) != 0;
602               663            
603             Button_SetSelection(handle, !selected, NULL);   664             Button_SetSelection(handle, !selected, NULL);
604             ButtonCallback(UGL_WIDGET(ugl_button)->callback_target);   665             ButtonCallback(UGL_WIDGET(ugl_button)->callback_target);
605 */   666 */
606         } break;   667         } break;
607         default: break;   668         default: break;
608     }   669     }
609     return result;   670     return result;
610 }   671 }
611     672  
612 //========================================================================================   673 //========================================================================================
613     674  
614 UGL_Int _Button_ExStyle(UGL_Int ugl_style, UGL_Int type) {   675 UGL_Int _Button_ExStyle(UGL_Int ugl_style, UGL_Int type) {
615     int bits = 0;   676     int bits = 0;
616     677  
617 #ifndef _WIN32_WCE    678 #ifndef _WIN32_WCE 
618     bits |= WS_EX_NOINHERITLAYOUT;   679     bits |= WS_EX_NOINHERITLAYOUT;
619 #endif   680 #endif
620     if (IS_FLAG_SET(ugl_style, UGL_STYLE_BORDER)) bits |= WS_EX_CLIENTEDGE;   681     if (IS_FLAG_SET(ugl_style, UGL_STYLE_BORDER)) bits |= WS_EX_CLIENTEDGE;
621     //else bits &= ~WS_EX_CLIENTEDGE;   682     //else bits &= ~WS_EX_CLIENTEDGE;
622     683  
623     return bits;   684     return bits;
624 }   685 }
625 //========================================================================================   686 //========================================================================================
626     687  
627 void _Button_GetMinimumSize(UGL_Int handle, POINT *point, UGL_Error uglError) {   688 void _Button_GetMinimumSize(UGL_Int handle, POINT *point, UGL_Error uglError) {
628     PUGL_Control ugl_control = UGL_CONTROL(handle);   689     PUGL_Control ugl_control = UGL_CONTROL(handle);
629     HWND hWnd = ugl_control->hWnd;   690     HWND hWnd = ugl_control->hWnd;
630     PUGL_Button ugl_button = UGL_BUTTON(handle);   691     PUGL_Button ugl_button = UGL_BUTTON(handle);
631     int border = _Control_GetBorderWidth(hWnd);   692     int border = _Control_GetBorderWidth(hWnd);
632     int width = 0, height = 0;   693     int width = 0, height = 0;
633     int extra = 0;   694     int extra = 0;
634     int bits = GetWindowLong(hWnd, GWL_STYLE);   695     int bits = GetWindowLong(hWnd, GWL_STYLE);
635     696  
636     if (ugl_button->ugl_image == NULL) {   697     if (ugl_button->ugl_image == NULL) {
637         HGDIOBJ oldFont = 0;   698         HGDIOBJ oldFont = 0;
638         HDC hDC = GetDC(hWnd);   699         HDC hDC = GetDC(hWnd);
639         HGDIOBJ newFont = (HGDIOBJ)SendMessage (hWnd, WM_GETFONT, 0, 0);   700         HGDIOBJ newFont = (HGDIOBJ)SendMessage (hWnd, WM_GETFONT, 0, 0);
640         TEXTMETRIC lptm;   701         TEXTMETRIC lptm;
641         int length = 0;   702         int length = 0;
642         LPTSTR window_text = NULL;   703         LPTSTR window_text = NULL;
643     704  
644         if (newFont != 0) oldFont = SelectObject(hDC, newFont);   705         if (newFont != 0) oldFont = SelectObject(hDC, newFont);
645         GetTextMetrics(hDC, &lptm);   706         GetTextMetrics(hDC, &lptm);
646         length = GetWindowTextLength(hWnd);   707         length = GetWindowTextLength(hWnd);
647         if (length == 0) {   708         if (length == 0) {
648             height += lptm.tmHeight;   709             height += lptm.tmHeight;
649         } else {   710         } else {
650             RECT rect;   711             RECT rect;
651             int flags = DT_CALCRECT | DT_SINGLELINE;   712             int flags = DT_CALCRECT | DT_SINGLELINE;
652     713  
653             SetRect((LPRECT)&rect, 0, 0, 1, 1);   714             SetRect((LPRECT)&rect, 0, 0, 1, 1);
654             extra = MAX(8, lptm.tmAveCharWidth);   715             extra = MAX(8, lptm.tmAveCharWidth);
655             window_text = (LPTSTR)calloc(sizeof(TCHAR), length + 2);   716             window_text = (LPTSTR)calloc(sizeof(TCHAR), length + 2);
656             GetWindowText(hWnd, window_text, length + 1);   717             GetWindowText(hWnd, window_text, length + 1);
657             DrawText(hDC, window_text, length, &rect, flags);   718             DrawText(hDC, window_text, length, &rect, flags);
658             free(window_text);   719             free(window_text);
659             width += rect.right - rect.left;   720             width += rect.right - rect.left;
660             height += rect.bottom - rect.top;   721             height += rect.bottom - rect.top;
661         }   722         }
662         if (newFont != 0) SelectObject (hDC, oldFont);   723         if (newFont != 0) SelectObject (hDC, oldFont);
663         ReleaseDC (hWnd, hDC);   724         ReleaseDC (hWnd, hDC);
664     } else {   725     } else {
665         int x, y;   726         int x, y;
666         Image_GetBounds((int)ugl_button->ugl_image, &x, &y, &width, &height, uglError);   727         Image_GetBounds((int)ugl_button->ugl_image, &x, &y, &width, &height, uglError);
667         extra = 8;   728         extra = 8;
668     }   729     }
669     730  
670     731  
671     if ((bits & (BS_CHECKBOX | BS_RADIOBUTTON)) != 0) {   732     if ((bits & (BS_CHECKBOX | BS_RADIOBUTTON)) != 0) {
672         /*   733         /*
673          * TODO, should not recalculate checkWidth and checkHeight   734          * TODO, should not recalculate checkWidth and checkHeight
674          * repeatedly, but should cache the value somewhere.   735          * repeatedly, but should cache the value somewhere.
675          */   736          */
676         int checkWidth = 0, checkHeight = 0;   737         int checkWidth = 0, checkHeight = 0;
677 //      HBITMAP hBitmap = LoadBitmap(NULL, OBM_CHECKBOXES);   738 //      HBITMAP hBitmap = LoadBitmap(NULL, OBM_CHECKBOXES);
678         //if (hBitmap == 0) {   739         //if (hBitmap == 0) {
679 #ifndef _WIN32_WCE   740 #ifndef _WIN32_WCE
680             checkWidth = GetSystemMetrics(SM_CXVSCROLL);   741             checkWidth = GetSystemMetrics(SM_CXVSCROLL);
681             checkHeight = GetSystemMetrics (SM_CYVSCROLL);   742             checkHeight = GetSystemMetrics (SM_CYVSCROLL);
682 #else   743 #else
683             checkWidth = GetSystemMetrics(SM_CXSMICON);   744             checkWidth = GetSystemMetrics(SM_CXSMICON);
684             checkHeight = GetSystemMetrics(SM_CYSMICON);   745             checkHeight = GetSystemMetrics(SM_CYSMICON);
685 #endif // if defined _WIN32_WCE   746 #endif // if defined _WIN32_WCE
686         //} else {   747         //} else {
687         //  BITMAP bitmap;   748         //  BITMAP bitmap;
688         //  GetObject(hBitmap, sizeof(BITMAP), &bitmap);   749         //  GetObject(hBitmap, sizeof(BITMAP), &bitmap);
689         //  DeleteObject(hBitmap);   750         //  DeleteObject(hBitmap);
690         //  checkWidth = bitmap.bmWidth / 4;   751         //  checkWidth = bitmap.bmWidth / 4;
691         //  checkHeight =  bitmap.bmHeight / 3;   752         //  checkHeight =  bitmap.bmHeight / 3;
692         //}   753         //}
693     754  
694         width += checkWidth + extra;   755         width += checkWidth + extra;
695         height = MAX(height+4, (checkHeight + 3));   756         height = MAX(height+4, (checkHeight + 3));
696     } else {   757     } else {
697         /**   758         /**
698          * BS_PUSHBUTTON's value is 0x00, thus, bit   759          * BS_PUSHBUTTON's value is 0x00, thus, bit
699          * checking for the flag always fails.   760          * checking for the flag always fails.
700          * However, an else clause is sufficient   761          * However, an else clause is sufficient
701          * because there are only 4 styles, and this   762          * because there are only 4 styles, and this
702          * case covers the other two.   763          * case covers the other two.
703          */   764          */
704       765    
705         width += 12;  height += 10;   766         width += 12;  height += 10;
706     }   767     }
707       768    
708     width += border * 2; height += border * 2;   769     width += border * 2; height += border * 2;
709       770    
710       771    
711     point->x = width;   772     point->x = width;
712     point->y = height;   773     point->y = height;
713       774    
714 }   775 }
715     776  
716 //========================================================================================   777 //========================================================================================
717 // fix for Label.setAlignment, paint to image ourselves to adjust the position depending on the image size   778 // fix for Label.setAlignment, paint to image ourselves to adjust the position depending on the image size
718 void Button_DrawImage(PUGL_Control handle) {   779 void Button_DrawImage(PUGL_Control handle) {
719       780    
720 #ifdef _WIN32_WCE   781 #ifdef _WIN32_WCE
721     PUGL_Control ugl_control = (PUGL_Control)handle;   782     PUGL_Control ugl_control = (PUGL_Control)handle;
722     PUGL_Button ugl_button = (PUGL_Button)ugl_control;   783     PUGL_Button ugl_button = (PUGL_Button)ugl_control;
723     PUGL_Image ugl_image = ugl_button->ugl_image;   784     PUGL_Image ugl_image = ugl_button->ugl_image;
724     HWND hWnd = ugl_control->hWnd;   785     HWND hWnd = ugl_control->hWnd;
725     HDC hdc = GetDC(hWnd);   786     HDC hdc = GetDC(hWnd);
726     HDC srcHDC = CreateCompatibleDC(hdc);   787     HDC srcHDC = CreateCompatibleDC(hdc);
727     HBRUSH newBrush;   788     HBRUSH newBrush;
728     BITMAP bitmap;   789     BITMAP bitmap;
729     int bits = GetWindowLong(hWnd, GWL_STYLE);   790     int bits = GetWindowLong(hWnd, GWL_STYLE);
730     HBRUSH backgroundBrush;   791     HBRUSH backgroundBrush;
731     792  
732     if(ugl_image != NULL){   793     if(ugl_image != NULL){
733         HGDIOBJ oldBrush;   794         HGDIOBJ oldBrush;
734         RECT rect;   795         RECT rect;
735     796  
736         int rop2 = 0;   797         int rop2 = 0;
737         int dwRop;   798         int dwRop;
738         newBrush = CreatePatternBrush(ugl_button->ugl_image->image_handle);   799         newBrush = CreatePatternBrush(ugl_button->ugl_image->image_handle);
739         oldBrush = SelectObject(hdc, newBrush);   800         oldBrush = SelectObject(hdc, newBrush);
740         rop2 = SetROP2(hdc, SRCCOPY);   801         rop2 = SetROP2(hdc, SRCCOPY);
741         SetROP2(hdc, rop2);   802         SetROP2(hdc, rop2);
742         dwRop = SRCCOPY;      803         dwRop = SRCCOPY;   
743         GetClipBox(hdc, &rect);   804         GetClipBox(hdc, &rect);
744     805  
745         // Paint the background for the label   806         // Paint the background for the label
746             if (ugl_control->background_color != NULL) {   807             if (ugl_control->background_color != NULL) {
747                 backgroundBrush = Color_GetBrush(ugl_control->background_color);   808                 backgroundBrush = Color_GetBrush(ugl_control->background_color);
748             } else {   809             } else {
749                 backgroundBrush = GetSysColorBrush(COLOR_BTNFACE);   810                 backgroundBrush = GetSysColorBrush(COLOR_BTNFACE);
750             }   811             }
751         if (((bits & BS_CHECKBOX) == BS_CHECKBOX) ||   812         if (((bits & BS_CHECKBOX) == BS_CHECKBOX) ||
752         ((bits & BS_RADIOBUTTON) == BS_RADIOBUTTON)) {   813         ((bits & BS_RADIOBUTTON) == BS_RADIOBUTTON)) {
753   814
754         } else {   815         } else {
755             FillRect(hdc, &rect, backgroundBrush);   816             FillRect(hdc, &rect, backgroundBrush);
756         }   817         }
757     818  
758         // check alignment and paint the image in accordance to the alignment   819         // check alignment and paint the image in accordance to the alignment
759         SelectObject(srcHDC, ugl_image->image_handle);   820         SelectObject(srcHDC, ugl_image->image_handle);
760         GetObject(ugl_button->ugl_image->image_handle, sizeof(BITMAP), &bitmap);              821         GetObject(ugl_button->ugl_image->image_handle, sizeof(BITMAP), &bitmap);           
761               822            
762         if (IS_FLAG_SET(bits, BS_CENTER)) {   823         if (IS_FLAG_SET(bits, BS_CENTER)) {
763             BitBlt(hdc, ((rect.right-rect.left)-bitmap.bmWidth)/2, ((rect.bottom-rect.top)-bitmap.bmHeight)/2, bitmap.bmWidth, bitmap.bmHeight, srcHDC, 0, 0, dwRop);   824             BitBlt(hdc, ((rect.right-rect.left)-bitmap.bmWidth)/2, ((rect.bottom-rect.top)-bitmap.bmHeight)/2, bitmap.bmWidth, bitmap.bmHeight, srcHDC, 0, 0, dwRop);
764         } else if (IS_FLAG_SET(bits, BS_RIGHT)) {   825         } else if (IS_FLAG_SET(bits, BS_RIGHT)) {
765             BitBlt(hdc, rect.right-bitmap.bmWidth-2, ((rect.bottom-rect.top)-bitmap.bmHeight)/2, bitmap.bmWidth, bitmap.bmHeight, srcHDC, 0, 0, dwRop);   826             BitBlt(hdc, rect.right-bitmap.bmWidth-2, ((rect.bottom-rect.top)-bitmap.bmHeight)/2, bitmap.bmWidth, bitmap.bmHeight, srcHDC, 0, 0, dwRop);
766         } else {   827         } else {
767             if (((bits & BS_CHECKBOX) == BS_CHECKBOX) || ((bits & BS_RADIOBUTTON) == BS_RADIOBUTTON))   828             if (((bits & BS_CHECKBOX) == BS_CHECKBOX) || ((bits & BS_RADIOBUTTON) == BS_RADIOBUTTON))
768             {   829             {
769                 BitBlt(hdc, rect.left+20, ((rect.bottom-rect.top)-bitmap.bmHeight)/2, bitmap.bmWidth, bitmap.bmHeight, srcHDC, 0, 0, dwRop);   830                 BitBlt(hdc, rect.left+20, ((rect.bottom-rect.top)-bitmap.bmHeight)/2, bitmap.bmWidth, bitmap.bmHeight, srcHDC, 0, 0, dwRop);
770             }   831             }
771             else   832             else
772             {   833             {
773                 BitBlt(hdc, rect.left+2, ((rect.bottom-rect.top)-bitmap.bmHeight)/2, bitmap.bmWidth, bitmap.bmHeight, srcHDC, 0, 0, dwRop);   834                 BitBlt(hdc, rect.left+2, ((rect.bottom-rect.top)-bitmap.bmHeight)/2, bitmap.bmWidth, bitmap.bmHeight, srcHDC, 0, 0, dwRop);
774             }   835             }
775     836  
776         }   837         }
777             SelectObject(hdc, oldBrush);   838             SelectObject(hdc, oldBrush);
778             DeleteDC(srcHDC);   839             DeleteDC(srcHDC);
779     }   840     }
780     ReleaseDC(hWnd, hdc);   841     ReleaseDC(hWnd, hdc);
781 #endif   842 #endif
782 }   843 }
783     844  
784 //========================================================================================   845 //========================================================================================
785     846  
786 void _Button_DrawFocusRect(HWND hWnd, UINT message) {   847 void _Button_DrawFocusRect(HWND hWnd, UINT message) {
787 #ifdef _WIN32_WCE   848 #ifdef _WIN32_WCE
788     RECT rect;   849     RECT rect;
789     HDC hdc = GetDC(hWnd);   850     HDC hdc = GetDC(hWnd);
790 //  HWND parent = GetParent(hWnd);   851 //  HWND parent = GetParent(hWnd);
791     852  
792 //  if(parent == NULL) // Shell   853 //  if(parent == NULL) // Shell
793 //      return;   854 //      return;
794     855  
795     if (GetClipBox(hdc, &rect) != NULLREGION) {   856     if (GetClipBox(hdc, &rect) != NULLREGION) {
796         DrawFocusRect(hdc, &rect);   857         DrawFocusRect(hdc, &rect);
797     }   858     }
798     859  
799 #endif   860 #endif
800 }   861 }