File: wincewsbuttons.pp

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (319 lines) | stat: -rw-r--r-- 10,183 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
{ $Id: WinCEwsbuttons.pp 8815 2006-02-24 13:31:16Z mattias $}
{
 *****************************************************************************
 *                              WinCEWSButtons.pp                            *
 *                              --------------                               * 
 *                                                                           *
 *                                                                           *
 *****************************************************************************

 *****************************************************************************
  This file is part of the Lazarus Component Library (LCL)

  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************
}
unit WinCEWSButtons;

{$mode delphi}{$H+}

interface

uses
  // Libs
  Windows,
  // RTL
  SysUtils, Classes,
  // LCL
  Controls, Buttons, Graphics, GraphType, LCLType, LCLProc, LazUTF8,
  // Widgetset
  WSButtons, WSLCLClasses, WinCEWSControls, WinCEWSImgList, WinCEProc;

type
  { TWinCEWSBitBtn }

  TWinCEWSBitBtn = class(TWSBitBtn)
  published
    class function  CreateHandle(const AWinControl: TWinControl;
          const AParams: TCreateParams): HWND; override;
    class procedure GetPreferredSize(const AWinControl: TWinControl;
          var PreferredWidth, PreferredHeight: integer;
          WithThemeSpace: Boolean); override;
    class procedure SetGlyph(const ABitBtn: TCustomBitBtn; const AValue: TButtonGlyph); override;
    class procedure SetLayout(const ABitBtn: TCustomBitBtn; const AValue: TButtonLayout); override;
    class procedure SetMargin(const ABitBtn: TCustomBitBtn; const AValue: Integer); override;
    class procedure SetSpacing(const ABitBtn: TCustomBitBtn; const AValue: Integer); override;
    class procedure SetText(const AWinControl: TWinControl; const AText: string); override;
  end;

  { TWinCEWSSpeedButton }

  TWinCEWSSpeedButton = class(TWSSpeedButton)
  private
  protected
  public
  end;

procedure DrawBitBtnImage(BitBtn: TCustomBitBtn; DrawStruct: PDrawItemStruct);

implementation

uses ImgList, WinCEInt, WinCEExtra;

type
  TBitBtnAceess = class(TCustomBitBtn)
  end;

const
  ButtonStateToFrameState: array[TButtonState] of UInt =
  (
{ bsUp        } DFCS_BUTTONPUSH,
{ bsDisabled  } DFCS_BUTTONPUSH or DFCS_INACTIVE,
{ bsDown      } DFCS_BUTTONPUSH or DFCS_PUSHED,
{ bsExclusive } DFCS_BUTTONPUSH,
{ bsHot       } DFCS_BUTTONPUSH
  );

{ TWinCEWSBitBtn }

{------------------------------------------------------------------------------
  Method: DrawBitBtnImage
  Params:  BitBtn: The TCustomBitBtn to update the image of
           ButtonCaption: new button caption
  Returns: Nothing

  Updates the button image combining the glyph and caption
 ------------------------------------------------------------------------------}
procedure DrawBitBtnImage(BitBtn: TCustomBitBtn; DrawStruct: PDrawItemStruct);
var
  BitBtnLayout: TButtonLayout; // Layout of button and glyph
  OldFontHandle: HFONT; // Handle of previous font in hdcNewBitmap
  TextSize: Windows.SIZE; // For computing the length of button caption in pixels
  XDestBitmap, YDestBitmap: integer; // X,Y coordinate of destination rectangle for bitmap
  XDestText, YDestText: integer; // X,Y coordinates of destination rectangle for caption
  newWidth, newHeight: integer; // dimensions of new combined bitmap
  srcWidth, srcHeight: integer; // width of glyph to use, bitmap may have multiple glyphs
  DrawRect: TRect;
  ButtonCaption: PWideChar;
  ButtonState: TButtonState;
  AIndex: Integer;
  AImageRes: TScaledImageListResolution;
  AEffect: TGraphicsDrawEffect;

  procedure DrawBitmap;
  var
    TextFlags: integer; // flags for caption (enabled or disabled)
  begin
    TextFlags := DST_PREFIXTEXT;
    if ButtonState = bsDisabled then
      TextFlags := TextFlags or DSS_DISABLED;

    // fill with background color

    if (srcWidth <> 0) and (srcHeight <> 0) then
    begin
      TBitBtnAceess(BitBtn).FButtonGlyph.GetImageIndexAndEffect(ButtonState, BitBtn.Font.PixelsPerInch, 1,
        AImageRes, AIndex, AEffect);

      TWinCEWSCustomImageListResolution.DrawToDC(AImageRes.Resolution, AIndex,
        DrawStruct^._hDC, Rect(XDestBitmap, YDestBitmap, srcWidth, srcHeight),
        AImageRes.Resolution.ImageList.BkColor,
        AImageRes.Resolution.ImageList.BlendColor, AEffect,
        AImageRes.Resolution.ImageList.DrawingStyle,
        AImageRes.Resolution.ImageList.ImageType);
    end;
    SetBkMode(DrawStruct^._hDC, TRANSPARENT);
    if ButtonState = bsDown then
      SetTextColor(DrawStruct^._hDC, $FFFFFF)
    else
      SetTextColor(DrawStruct^._hDC, 0);
    DrawState(DrawStruct^._hDC, 0, nil, LPARAM(ButtonCaption), 0, XDestText, YDestText, 0, 0, TextFlags);
  end;

begin
  DrawRect := DrawStruct^.rcItem;

  if DrawStruct^.itemState and ODS_DISABLED <> 0 then
    ButtonState := bsDisabled
  else
  if DrawStruct^.itemState and (ODS_FOCUS or ODS_SELECTED) = (ODS_FOCUS or ODS_SELECTED) then
    ButtonState := bsDown
  else
  if DrawStruct^.itemState and ODS_FOCUS = ODS_FOCUS then
    ButtonState := bsHot
  else
    ButtonState := bsUp;

  DrawFrameControl(DrawStruct^._hDC, DrawRect, DFC_BUTTON, ButtonStateToFrameState[ButtonState]);

  // DFCS_ADJUSTRECT doesnot work
  InflateRect(DrawRect, -4, -4);

  ButtonCaption := PWideChar(UTF8Decode(BitBtn.Caption));

  // gather info about bitbtn
  if BitBtn.CanShowGlyph(True) then
  begin
    TBitBtnAceess(BitBtn).FButtonGlyph.GetImageIndexAndEffect(Low(TButtonState), BitBtn.Font.PixelsPerInch, 1,
      AImageRes, AIndex, AEffect);
    srcWidth := AImageRes.Width;
    srcHeight := AImageRes.Height;
  end else
  begin
    srcWidth := 0;
    srcHeight := 0;
  end;
  BitBtnLayout := BitBtn.Layout;

  OldFontHandle := SelectObject(DrawStruct^._hDC, BitBtn.Font.Reference.Handle);
  GetTextExtentPoint32W(DrawStruct^._hDC, ButtonCaption, Length(BitBtn.Caption), TextSize);
  // calculate size of new bitmap
  case BitBtnLayout of
    blGlyphLeft, blGlyphRight:
    begin
      YDestBitmap := (DrawRect.Bottom + DrawRect.Top - srcHeight) div 2;
      YDestText := (DrawRect.Bottom + DrawRect.Top - TextSize.cy) div 2;

      newWidth := TextSize.cx + srcWidth;
      
      if BitBtn.Spacing <> -1 then
        newWidth := newWidth + BitBtn.Spacing;
        
      if srcWidth <> 0 then
        inc(newWidth, 2);
        
      case BitBtnLayout of
        blGlyphLeft:
        begin
          XDestBitmap := (DrawRect.Right + DrawRect.Left - newWidth) div 2;
          XDestText := XDestBitmap + srcWidth;
        end;
        blGlyphRight:
        begin
          XDestText := (DrawRect.Right + DrawRect.Left - newWidth) div 2;
          XDestBitmap := XDestText + TextSize.cx;
        end;
      end;
    end;
    blGlyphTop, blGlyphBottom:
    begin
      XDestBitmap := (DrawRect.Right + DrawRect.Left - srcWidth) shr 1;
      XDestText := (DrawRect.Right + DrawRect.Left - TextSize.cx) shr 1;

      newHeight := TextSize.cy + srcHeight;

      if BitBtn.Spacing <> -1 then
        newHeight := newHeight + BitBtn.Spacing;
        
      if srcHeight <> 0 then
        inc(newHeight, 2);
        
      case BitBtnLayout of
        blGlyphTop:
        begin
          YDestBitmap := (DrawRect.Top + DrawRect.Bottom - newHeight) div 2;
          YDestText := YDestBitmap + srcHeight;
        end;
        blGlyphBottom:
        begin
          YDestText := (DrawRect.Top + DrawRect.Bottom - newHeight) div 2;
          YDestBitmap := YDestText + TextSize.cy;
        end;
      end;
    end;
  end;

  DrawBitmap;

  SelectObject(DrawStruct^._hDC, OldFontHandle);
end;


class function TWinCEWSBitBtn.CreateHandle(const AWinControl: TWinControl;
  const AParams: TCreateParams): HWND;
var
  Params: TCreateWindowExParams;
begin
  // general initialization of Params
  PrepareCreateWindow(AWinControl, AParams, Params);
  // customization of Params
  with Params do
  begin
    pClassName := @ButtonClsName;
    Flags := Flags or BS_OWNERDRAW; // Draw bitmap on WM_DRAWITEM
    WindowTitle := '';
  end;
  // create window
  FinishCreateWindow(AWinControl, Params, false);
  Result := Params.Window;
end;


class procedure TWinCEWSBitBtn.GetPreferredSize(const AWinControl: TWinControl;
  var PreferredWidth, PreferredHeight: integer; WithThemeSpace: Boolean);
var
  BitmapInfo: BITMAP; // Buffer for bitmap
  BitBtn: TBitBtn absolute AWinControl;
  Glyph: TBitmap;
  spacing, srcWidth: integer;
begin
  if MeasureText(AWinControl, AWinControl.Caption, PreferredWidth, PreferredHeight) then
  begin
    Glyph := BitBtn.Glyph;
    if not Glyph.Empty then
    begin
      Windows.GetObject(Glyph.Handle, sizeof(BitmapInfo), @BitmapInfo);
      srcWidth := BitmapInfo.bmWidth;
      if BitBtn.NumGlyphs > 1 then
        srcWidth := srcWidth div BitBtn.NumGlyphs;
      if BitBtn.Spacing = -1 then
        spacing := 8
      else
        spacing := BitBtn.Spacing;
      if BitBtn.Layout in [blGlyphLeft, blGlyphRight] then
      begin
        Inc(PreferredWidth, spacing + srcWidth);
        if BitmapInfo.bmHeight > PreferredHeight then
          PreferredHeight := BitmapInfo.bmHeight;
      end else begin
        Inc(PreferredHeight, spacing + BitmapInfo.bmHeight);
        if srcWidth > PreferredWidth then
          PreferredWidth := srcWidth;
      end;
    end;
    Inc(PreferredWidth, 20);
    Inc(PreferredHeight, 12);
  end;
end;

class procedure TWinCEWSBitBtn.SetGlyph(const ABitBtn: TCustomBitBtn;
  const AValue: TButtonGlyph);
begin
  ABitBtn.invalidate;
end;

class procedure TWinCEWSBitBtn.SetLayout(const ABitBtn: TCustomBitBtn;
  const AValue: TButtonLayout);
begin
  ABitBtn.invalidate;
end;

class procedure TWinCEWSBitBtn.SetMargin(const ABitBtn: TCustomBitBtn;
  const AValue: Integer);
begin
  ABitBtn.invalidate;
end;

class procedure TWinCEWSBitBtn.SetSpacing(const ABitBtn: TCustomBitBtn;
  const AValue: Integer);
begin
  ABitBtn.invalidate;
end;

class procedure TWinCEWSBitBtn.SetText(const AWinControl: TWinControl; const AText: string);
begin
  AWinControl.invalidate;
end;

end.