~ubuntu-branches/ubuntu/saucy/lazarus/saucy

« back to all changes in this revision

Viewing changes to lcl/interfaces/cocoa/cocoawinapi.inc

  • Committer: Package Import Robot
  • Author(s): Paul Gevers, Abou Al Montacir, Bart Martens, Paul Gevers
  • Date: 2013-06-08 14:12:17 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20130608141217-7k0cy9id8ifcnutc
Tags: 1.0.8+dfsg-1
[ Abou Al Montacir ]
* New upstream major release and multiple maintenace release offering many
  fixes and new features marking a new milestone for the Lazarus development
  and its stability level.
  - The detailed list of changes can be found here:
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_release_notes
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_fixes_branch
* LCL changes:
  - LCL is now a normal package.
      + Platform independent parts of the LCL are now in the package LCLBase
      + LCL is automatically recompiled when switching the target platform,
        unless pre-compiled binaries for this target are already installed.
      + No impact on existing projects.
      + Linker options needed by LCL are no more added to projects that do
        not use the LCL package.
  - Minor changes in LCL basic classes behaviour
      + TCustomForm.Create raises an exception if a form resource is not
        found.
      + TNotebook and TPage: a new implementation of these classes was added.
      + TDBNavigator: It is now possible to have focusable buttons by setting
        Options = [navFocusableButtons] and TabStop = True, useful for
        accessibility and for devices with neither mouse nor touch screen.
      + Names of TControlBorderSpacing.GetSideSpace and GetSpace were swapped
        and are now consistent. GetSideSpace = Around + GetSpace.
      + TForm.WindowState=wsFullscreen was added
      + TCanvas.TextFitInfo was added to calculate how many characters will
        fit into a specified Width. Useful for word-wrapping calculations.
      + TControl.GetColorResolvingParent and
        TControl.GetRGBColorResolvingParent were added, simplifying the work
        to obtain the final color of the control while resolving clDefault
        and the ParentColor.
      + LCLIntf.GetTextExtentExPoint now has a good default implementation
        which works in any platform not providing a specific implementation.
        However, Widgetset specific implementation is better, when available.
      + TTabControl was reorganized. Now it has the correct class hierarchy
        and inherits from TCustomTabControl as it should.
  - New unit in the LCL:
      + lazdialogs.pas: adds non-native versions of various native dialogs,
        for example TLazOpenDialog, TLazSaveDialog, TLazSelectDirectoryDialog.
        It is used by widgetsets which either do not have a native dialog, or
        do not wish to use it because it is limited. These dialogs can also be
        used by user applications directly.
      + lazdeviceapis.pas: offers an interface to more hardware devices such
        as the accelerometer, GPS, etc. See LazDeviceAPIs
      + lazcanvas.pas: provides a TFPImageCanvas descendent implementing
        drawing in a LCL-compatible way, but 100% in Pascal.
      + lazregions.pas. LazRegions is a wholly Pascal implementation of
        regions for canvas clipping, event clipping, finding in which control
        of a region tree one an event should reach, for drawing polygons, etc.
      + customdrawncontrols.pas, customdrawndrawers.pas,
        customdrawn_common.pas, customdrawn_android.pas and
        customdrawn_winxp.pas: are the Lazarus Custom Drawn Controls -controls
        which imitate the standard LCL ones, but with the difference that they
        are non-native and support skinning.
  - New APIs added to the LCL to improve support of accessibility software
    such as screen readers.
* IDE changes:
  - Many improvments.
  - The detailed list of changes can be found here:
    http://wiki.lazarus.freepascal.org/New_IDE_features_since#v1.0_.282012-08-29.29
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_release_notes#IDE_Changes
* Debugger / Editor changes:
  - Added pascal sources and breakpoints to the disassembler
  - Added threads dialog.
* Components changes:
  - TAChart: many fixes and new features
  - CodeTool: support Delphi style generics and new syntax extensions.
  - AggPas: removed to honor free licencing. (Closes: Bug#708695)
[Bart Martens]
* New debian/watch file fixing issues with upstream RC release.
[Abou Al Montacir]
* Avoid changing files in .pc hidden directory, these are used by quilt for
  internal purpose and could lead to surprises during build.
[Paul Gevers]
* Updated get-orig-source target and it compinion script orig-tar.sh so that they
  repack the source file, allowing bug 708695 to be fixed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
//##apiwiz##sps##   // Do not remove, no wizard declaration before this line
28
28
 
 
29
function CocoaCombineMode(ACombineMode: Integer): TCocoaCombine;
 
30
begin
 
31
  case ACombineMode of
 
32
    RGN_AND:  Result:=cc_And;
 
33
    RGN_OR:   Result:=cc_Or;
 
34
    RGN_XOR:  Result:=cc_Xor;
 
35
    RGN_DIFF: Result:=cc_Diff;
 
36
  else
 
37
    Result:=cc_Copy;
 
38
  end;
 
39
end;
 
40
 
 
41
const
 
42
  CocoaRegionTypeToWin32Map: array[TCocoaRegionType] of Integer = (
 
43
 { crt_Error     } ERROR,
 
44
 { crt_Empty     } NULLREGION,
 
45
 { crt_Rectangle } SIMPLEREGION,
 
46
 { crt_Complex   } COMPLEXREGION
 
47
  );
 
48
 
 
49
function TCocoaWidgetSet.Arc(DC: HDC; Left, Top, Right, Bottom, angle1,
 
50
  angle2: Integer): Boolean;
 
51
begin
 
52
  Result:=inherited Arc(DC, Left, Top, Right, Bottom, angle1, angle2);
 
53
end;
 
54
 
 
55
function TCocoaWidgetSet.AngleChord(DC: HDC; x1, y1, x2, y2, angle1,
 
56
  angle2: Integer): Boolean;
 
57
begin
 
58
  Result:=inherited AngleChord(DC, x1, y1, x2, y2, angle1, angle2);
 
59
end;
 
60
 
 
61
function TCocoaWidgetSet.BeginPaint(Handle: hWnd; var PS: TPaintStruct): hdc;
 
62
begin
 
63
  Result := inherited BeginPaint(Handle, PS);
 
64
  PS.hdc := Result;
 
65
end;
 
66
 
 
67
function TCocoaWidgetSet.BitBlt(DestDC: HDC; X, Y, Width, Height: Integer;
 
68
  SrcDC: HDC; XSrc, YSrc: Integer; Rop: DWORD): Boolean;
 
69
begin
 
70
  Result := StretchMaskBlt(DestDC, X, Y, Width, Height, SrcDC, XSrc, YSrc,
 
71
    Width, Height, 0, 0, 0, Rop);
 
72
end;
 
73
 
 
74
function TCocoaWidgetSet.ClientToScreen(Handle: HWND; var P: TPoint): Boolean;
 
75
begin
 
76
  Result := Handle <> 0;
 
77
 
 
78
  if Result then
 
79
  begin
 
80
    // 1. convert client to window
 
81
    with NSObject(Handle).lclClientFrame do
 
82
    begin
 
83
      inc(P.X, Left);
 
84
      inc(P.Y, Top);
 
85
    end;
 
86
    // 2. convert window to screen
 
87
    NSObject(Handle).lclLocalToScreen(P.X, P.Y);
 
88
  end;
 
89
end;
 
90
 
 
91
function TCocoaWidgetSet.CombineRgn(Dest, Src1, Src2: HRGN; fnCombineMode: Longint): Longint;
 
92
begin
 
93
  Result := LCLType.Error;
 
94
  if (Dest = 0) or (Src1 = 0) or (fnCombineMode<RGN_AND) or (fnCombineMode>RGN_COPY) then Exit;
 
95
  if (fnCombineMode <> RGN_COPY) and (Src2 = 0) then Exit;
 
96
 
 
97
  TCocoaRegion(Dest).CombineWith(TCocoaRegion(Src1), cc_Copy);
 
98
 
 
99
  if fnCombineMode <> RGN_COPY then
 
100
    TCocoaRegion(Dest). CombineWith(TCocoaRegion(Src2), CocoaCombineMode(fnCombineMode));
 
101
end;
 
102
 
 
103
{------------------------------------------------------------------------------
 
104
  Method:  CreateBitmap
 
105
  Params:  Width      - Bitmap width, in pixels
 
106
           Height     - Bitmap height, in pixels
 
107
           Planes     - Number of color planes
 
108
           BitCount   - Number of bits required to identify a color (TODO)
 
109
           BitmapBits - Pointer to array containing color data (TODO)
 
110
  Returns: A handle to a bitmap
 
111
 
 
112
  Creates a bitmap with the specified width, height and color format
 
113
 ------------------------------------------------------------------------------}
 
114
function TCocoaWidgetSet.CreateBitmap(Width, Height: Integer;
 
115
  Planes, BitCount: Longint; BitmapBits: Pointer): HBITMAP;
 
116
var
 
117
  bmpType: TCocoaBitmapType;
 
118
begin
 
119
  // WORKAROUND: force context supported depths
 
120
  if BitmapBits = nil then
 
121
  begin
 
122
    if BitCount = 24 then BitCount := 32;
 
123
    // if BitCount = 1 then BitCount := 8;
 
124
  end;
 
125
 
 
126
  case BitCount of
 
127
    1:  bmpType := cbtMono;
 
128
    8:  bmpType := cbtGray;
 
129
    32: bmpType := cbtARGB;
 
130
  else
 
131
    bmpType := cbtRGB;
 
132
  end;
 
133
 
 
134
  // winapi Bitmaps are on a word boundary
 
135
  Result := HBITMAP(TCocoaBitmap.Create(Width, Height, BitCount, BitCount, cbaWord, bmpType, BitmapBits));
 
136
end;
 
137
 
 
138
function TCocoaWidgetSet.CreateBrushIndirect(const LogBrush: TLogBrush): HBRUSH;
 
139
begin
 
140
  Result := HBrush(TCocoaBrush.Create(LogBrush));
 
141
end;
 
142
 
 
143
function TCocoaWidgetSet.CreateCaret(Handle: HWND; Bitmap: hBitmap; Width,
 
144
  Height: Integer): Boolean;
 
145
var
 
146
  Obj: NSObject;
 
147
begin
 
148
  Result := (Handle <> 0);
 
149
  if Result then
 
150
  begin
 
151
    Obj := NSObject(Handle);
 
152
    if Obj.isKindOfClass(NSView) then
 
153
      Result := CocoaCaret.CreateCaret(NSView(Handle), Bitmap, Width, Height)
 
154
    else
 
155
    if Obj.isKindOfClass(NSWindow) then
 
156
      Result := CocoaCaret.CreateCaret(NSWindow(Handle).contentView, Bitmap, Width, Height)
 
157
    else
 
158
      Result := False;
 
159
  end;
 
160
end;
 
161
 
 
162
function TCocoaWidgetSet.CreateCompatibleBitmap(DC: HDC; Width, Height: Integer): HBITMAP;
 
163
begin
 
164
  Result := HBITMAP(TCocoaBitmap.Create(Width, Height, 32, 32, cbaDQWord, cbtARGB, nil));
 
165
end;
 
166
 
 
167
{------------------------------------------------------------------------------
 
168
  Method:  CreateCompatibleDC
 
169
  Params:  DC - Handle to memory device context
 
170
  Returns: Handle to a memory device context
 
171
 
 
172
  Creates a memory device context (DC) compatible with the specified device
 
173
 ------------------------------------------------------------------------------}
 
174
function TCocoaWidgetSet.CreateCompatibleDC(DC: HDC): HDC;
 
175
begin
 
176
  Result := HDC(TCocoaContext.Create);
 
177
end;
 
178
 
 
179
//todo:
 
180
//function TCocoaWidgetSet.CreateEllipticRgn(p1, p2, p3, p4: Integer): HRGN;
 
181
//begin
 
182
//end;
 
183
 
 
184
function TCocoaWidgetSet.CreateFontIndirect(const LogFont: TLogFont): HFONT;
 
185
begin
 
186
  Result := HFont(TCocoaFont.Create(LogFont, LogFont.lfFaceName));
 
187
end;
 
188
 
 
189
function TCocoaWidgetSet.CreateFontIndirectEx(const LogFont: TLogFont;
 
190
  const LongFontName: string): HFONT;
 
191
begin
 
192
  Result := HFont(TCocoaFont.Create(LogFont, LongFontName));
 
193
end;
 
194
 
 
195
function Create32BitAlphaBitmap(ABitmap, AMask: TCocoaBitmap): TCocoaBitmap;
 
196
var
 
197
  ARawImage: TRawImage;
 
198
  Desc: TRawImageDescription absolute ARawimage.Description;
 
199
 
 
200
  ImgHandle, ImgMaskHandle: HBitmap;
 
201
  ImagePtr: PRawImage;
 
202
  DevImage: TRawImage;
 
203
  DevDesc: TRawImageDescription;
 
204
  SrcImage, DstImage: TLazIntfImage;
 
205
  W, H: Integer;
 
206
begin
 
207
  Result := nil;
 
208
 
 
209
  if not RawImage_FromBitmap(ARawImage, HBITMAP(ABitmap), HBITMAP(AMask)) then
 
210
    Exit;
 
211
 
 
212
  ImgMaskHandle := 0;
 
213
 
 
214
  W := Desc.Width;
 
215
  if W < 1 then W := 1;
 
216
  H := Desc.Height;
 
217
  if H < 1 then H := 1;
 
218
 
 
219
  QueryDescription(DevDesc, [riqfRGB, riqfAlpha], W, H);
 
220
 
 
221
  if DevDesc.IsEqual(Desc)
 
222
  then begin
 
223
    // image is compatible, so use it
 
224
    DstImage := nil;
 
225
    ImagePtr := @ARawImage;
 
226
  end
 
227
  else begin
 
228
    // create compatible copy
 
229
    SrcImage := TLazIntfImage.Create(ARawImage, False);
 
230
    DstImage := TLazIntfImage.Create(0,0,[]);
 
231
    DstImage.DataDescription := DevDesc;
 
232
    DstImage.CopyPixels(SrcImage);
 
233
    SrcImage.Free;
 
234
    DstImage.GetRawImage(DevImage);
 
235
    ImagePtr := @DevImage;
 
236
  end;
 
237
 
 
238
  try
 
239
    if not RawImage_CreateBitmaps(ImagePtr^, ImgHandle, ImgMaskHandle, True) then Exit;
 
240
 
 
241
    Result := TCocoaBitmap(ImgHandle);
 
242
  finally
 
243
    ARawImage.FreeData;
 
244
    DstImage.Free;
 
245
  end;
 
246
end;
 
247
 
 
248
function TCocoaWidgetSet.CreateIconIndirect(IconInfo: PIconInfo): HICON;
 
249
var
 
250
  ABitmap: TCocoaBitmap;
 
251
begin
 
252
  Result := 0;
 
253
  if IconInfo^.hbmColor = 0 then Exit;
 
254
 
 
255
  ABitmap := Create32BitAlphaBitmap(TCocoaBitmap(IconInfo^.hbmColor), TCocoaBitmap(IconInfo^.hbmMask));
 
256
 
 
257
  if IconInfo^.fIcon then
 
258
    Result := HICON(ABitmap)
 
259
  else
 
260
    Result := HICON(TCocoaCursor.CreateFromBitmap(ABitmap, GetNSPoint(IconInfo^.xHotSpot, IconInfo^.yHotSpot)));
 
261
end;
 
262
 
 
263
function TCocoaWidgetSet.CreatePenIndirect(const LogPen: TLogPen): HPEN;
 
264
begin
 
265
  Result := HPen(TCocoaPen.Create(LogPen));
 
266
end;
 
267
 
 
268
{------------------------------------------------------------------------------
 
269
  Method:  CreatePolygonRgn
 
270
  Params:  Points   - Pointer to array of polygon points
 
271
           NumPts   - Number of points passed
 
272
           FillMode - Filling mode
 
273
  Returns: The new polygonal region
 
274
 
 
275
  Creates a new polygonal region from the specified points
 
276
 ------------------------------------------------------------------------------}
 
277
function TCocoaWidgetSet.CreatePolygonRgn(Points: PPoint; NumPts: Integer;
 
278
  FillMode: integer): HRGN;
 
279
begin
 
280
  {$IFDEF VerboseWinAPI}
 
281
    DebugLn('TCocoaWidgetSet.CreatePolygonRgn NumPts: ' + DbgS(NumPts) +
 
282
      ' FillMode: ' + DbgS(FillMode));
 
283
  {$ENDIF}
 
284
 
 
285
  Result := HRGN(TCocoaRegion.Create(Points, NumPts, FillMode=ALTERNATE));
 
286
end;
 
287
 
 
288
function TCocoaWidgetSet.CreateRectRgn(X1, Y1, X2, Y2: Integer): HRGN;
 
289
begin
 
290
  {$IFDEF VerboseWinAPI}
 
291
    DebugLn('TCocoaWidgetSet.CreateRectRgn R: ' + DbgS(Classes.Rect(X1, Y1, X2, Y2)));
 
292
  {$ENDIF}
 
293
 
 
294
  Result := HRGN(TCocoaRegion.Create(X1, Y1, X2, Y2));
 
295
end;
 
296
 
 
297
function TCocoaWidgetSet.DeleteObject(GDIObject: HGDIOBJ): Boolean;
 
298
var
 
299
  gdi: TCocoaGDIObject;
 
300
begin
 
301
  Result := False;
 
302
  if GDIObject = 0 then
 
303
    Exit(True);
 
304
 
 
305
  gdi := CheckGDIOBJ(GdiObject);
 
306
 
 
307
  if not Assigned(gdi) then
 
308
    Exit;
 
309
 
 
310
  if gdi.Global then
 
311
    Exit;
 
312
 
 
313
  if gdi.RefCount = 0 then gdi.Destroy;
 
314
end;
 
315
 
 
316
function TCocoaWidgetSet.DestroyCaret(Handle: HWND): Boolean;
 
317
begin
 
318
  Result := CocoaCaret.DestroyCaret;
 
319
end;
 
320
 
 
321
function TCocoaWidgetSet.DestroyIcon(Handle: HICON): Boolean;
 
322
var
 
323
  Ico: TObject;
 
324
begin
 
325
  Result := Handle <> 0;
 
326
  if not Result then
 
327
    Exit;
 
328
  Ico := TObject(Handle);
 
329
  Result := (Ico is TCocoaBitmap) or (Ico is TCocoaCursor);
 
330
  if Result then
 
331
    Ico.Destroy;
 
332
end;
 
333
 
 
334
function TCocoaWidgetSet.DrawFocusRect(DC: HDC; const Rect: TRect): boolean;
 
335
var
 
336
  ctx: TCocoaContext;
 
337
begin
 
338
  ctx := CheckDC(DC);
 
339
  Result := Assigned(ctx);
 
340
  if Result then
 
341
    ctx.DrawFocusRect(Rect);
 
342
end;
 
343
 
 
344
function TCocoaWidgetSet.DrawEdge(DC: HDC; var Rect: TRect; edge: Cardinal;
 
345
  grfFlags: Cardinal): Boolean;
 
346
begin
 
347
  Result := inherited DrawEdge(DC, Rect, edge, grfFlags);
 
348
end;
 
349
 
 
350
function TCocoaWidgetSet.Ellipse(DC: HDC; x1, y1, x2, y2: Integer): Boolean;
 
351
var
 
352
  ctx: TCocoaContext;
 
353
begin
 
354
  ctx := CheckDC(DC);
 
355
  Result := Assigned(ctx);
 
356
  if Result then
 
357
    ctx.Ellipse(x1, y1, x2, y2);
 
358
end;
 
359
 
 
360
function TCocoaWidgetSet.EnableWindow(hWnd: HWND; bEnable: Boolean): Boolean;
 
361
begin
 
362
  Result := hWnd <> 0;
 
363
  if Result then
 
364
    NSObject(hWnd).lclSetEnabled(bEnable)
 
365
end;
 
366
 
 
367
function TCocoaWidgetSet.EndPaint(Handle: hwnd; var PS: TPaintStruct): Integer;
 
368
begin
 
369
  Result:=inherited EndPaint(Handle, PS);
 
370
end;
 
371
 
 
372
function TCocoaWidgetSet.EnumFontFamiliesEx(DC: HDC; lpLogFont: PLogFont;
 
373
  Callback: FontEnumExProc; Lparam: LParam; Flags: dword): longint;
 
374
var
 
375
  fname: NSString;
 
376
  ELogFont: TEnumLogFontEx;
 
377
  Metric: TNewTextMetricEx;
 
378
  FontName: AnsiString;
 
379
begin
 
380
  Result := 0;
 
381
  if not Assigned(Callback) then Exit;
 
382
  for fname in NSFontManager.sharedFontManager.availableFontFamilies do
 
383
  begin
 
384
    try
 
385
      FontName := NSStringToString(fname);
 
386
      FillChar(ELogFont, SizeOf(ELogFont), #0);
 
387
      FillChar(Metric, SizeOf(Metric), #0);
 
388
      ELogFont.elfLogFont.lfFaceName := FontName;
 
389
      ELogFont.elfFullName := FontName;
 
390
      //todo: read the data from all fonts of the fontfamily
 
391
      Result := CallBack(ELogFont, Metric, TRUETYPE_FONTTYPE, lparam);
 
392
      if Result = 0 then Break;
 
393
    except
 
394
      Break;
 
395
    end;
 
396
  end;
 
397
end;
 
398
 
 
399
function TCocoaWidgetSet.EnumDisplayMonitors(hdc: HDC; lprcClip: PRect;
 
400
  lpfnEnum: MonitorEnumProc; dwData: LPARAM): LongBool;
 
401
var
 
402
  i: integer;
 
403
begin
 
404
  Result := True;
 
405
  for i := 0 to NSScreen.screens.count - 1 do
 
406
  begin
 
407
    Result := Result and lpfnEnum(HMONITOR(NSScreen.screens.objectAtIndex(i)), 0, nil, dwData);
 
408
    if not Result then break;
 
409
  end;
 
410
end;
 
411
 
 
412
function TCocoaWidgetSet.ExtSelectClipRGN(dc: hdc; rgn: hrgn; Mode: Longint): Integer;
 
413
var
 
414
  ctx: TCocoaContext;
 
415
begin
 
416
  ctx := CheckDC(DC);
 
417
  if Assigned(ctx) then
 
418
    Result := CocoaRegionTypeToWin32Map[ctx.SetClipRegion(TCocoaRegion(rgn), CocoaCombineMode(Mode))]
 
419
  else
 
420
    Result := ERROR;
 
421
end;
 
422
 
 
423
function TCocoaWidgetSet.ExtCreatePen(dwPenStyle, dwWidth: DWord;
 
424
  const lplb: TLogBrush; dwStyleCount: DWord; lpStyle: PDWord): HPEN;
 
425
begin
 
426
  Result := HPEN(TCocoaPen.Create(dwPenStyle, dwWidth, lplb, dwStyleCount, lpStyle));
 
427
end;
 
428
 
 
429
function TCocoaWidgetSet.ExtTextOut(DC: HDC; X, Y: Integer; Options: Longint;
 
430
  Rect: PRect; Str: PChar; Count: Longint; Dx: PInteger): Boolean;
 
431
var
 
432
  ctx: TCocoaContext;
 
433
begin
 
434
  ctx := CheckDC(DC);
 
435
  Result := Assigned(ctx);
 
436
  if Assigned(ctx) then
 
437
    ctx.TextOut(X, Y, Options, Rect, Str, Count, Dx);
 
438
end;
 
439
 
 
440
function TCocoaWidgetSet.FillRect(DC: HDC; const Rect: TRect; Brush: HBRUSH): Boolean;
 
441
var
 
442
  ctx: TCocoaContext;
 
443
  br: TCocoaGDIObject;
 
444
begin
 
445
  ctx := CheckDC(DC);
 
446
  br := CheckGDIOBJ(Brush);
 
447
  Result := Assigned(ctx) and (not Assigned(br) or (br is TCocoaBrush));
 
448
  if not Result then Exit;
 
449
 
 
450
  with Rect do
 
451
    ctx.Rectangle(Left, Top, Right, Bottom, True, TCocoaBrush(br));
 
452
end;
 
453
 
 
454
function TCocoaWidgetSet.FillRgn(DC: HDC; RegionHnd: HRGN; hbr: HBRUSH): Bool;
 
455
var
 
456
  OldRgn: TCocoaRegion;
 
457
  R: TRect;
 
458
  Clipped: Boolean;
 
459
  ctx: TCocoaContext;
 
460
  br: TCocoaGDIObject;
 
461
  I: Integer;
 
462
begin
 
463
  ctx := CheckDC(DC);
 
464
  br := CheckGDIOBJ(hbr);
 
465
  Result := Assigned(ctx) and (not Assigned(br) or (br is TCocoaBrush));
 
466
  if not Result then Exit;
 
467
 
 
468
  Clipped := ctx.Clipped;
 
469
  I := ctx.SaveDC;
 
470
  if Clipped then
 
471
    OldRgn := TCocoaRegion.CreateDefault;
 
472
  try
 
473
    if Clipped then
 
474
      ctx.CopyClipRegion(OldRgn);
 
475
    if SelectClipRgn(DC, RegionHnd) <> ERROR then
 
476
    begin
 
477
      R := TCocoaRegion(RegionHnd).GetBounds;
 
478
      with R do
 
479
        ctx.Rectangle(Left, Top, Right, Bottom, True, TCocoaBrush(br));
 
480
      if Clipped then
 
481
        SelectClipRgn(DC, HRGN(OldRgn));
 
482
      Result := True;
 
483
    end;
 
484
  finally
 
485
    if Clipped then
 
486
      OldRgn.Free;
 
487
    ctx.RestoreDC(I);
 
488
  end;
 
489
end;
 
490
 
 
491
function TCocoaWidgetSet.Frame3d(DC: HDC; var ARect: TRect;
 
492
  const FrameWidth: integer; const Style: TBevelCut): Boolean;
 
493
var
 
494
  ctx: TCocoaContext;
 
495
begin
 
496
  ctx := CheckDC(DC);
 
497
  Result := Assigned(ctx) and (FrameWidth > 0);
 
498
  if Result then
 
499
    ctx.Frame3d(ARect, FrameWidth, Style);
 
500
end;
 
501
 
 
502
function TCocoaWidgetSet.FrameRect(DC: HDC; const ARect: TRect; hBr: HBRUSH): Integer;
 
503
var
 
504
  ctx: TCocoaContext;
 
505
begin
 
506
  ctx := CheckDC(DC);
 
507
  if Assigned(ctx) then
 
508
  begin
 
509
    ctx.FrameRect(ARect, TCocoaBrush(hBr));
 
510
    Result := -1;
 
511
  end
 
512
  else
 
513
    Result := 0;
 
514
end;
 
515
 
 
516
function TCocoaWidgetSet.GetActiveWindow: HWND;
 
517
begin
 
518
  // return the currect application active window
 
519
  Result := HWND(NSApp.keyWindow);
 
520
end;
 
521
 
 
522
function TCocoaWidgetSet.GetBkColor(DC: HDC): TColorRef;
 
523
var
 
524
  ctx: TCocoaContext;
 
525
begin
 
526
  ctx := CheckDC(DC);
 
527
  if Assigned(ctx) then
 
528
    Result := ctx.BkColor
 
529
  else
 
530
    Result := CLR_INVALID;
 
531
end;
 
532
 
 
533
function TCocoaWidgetSet.GetCaretPos(var lpPoint: TPoint): Boolean;
 
534
begin
 
535
  Result := CocoaCaret.GetCaretPos(lpPoint);
 
536
end;
 
537
 
 
538
function TCocoaWidgetSet.GetCaretRespondToFocus(handle: HWND;
 
539
  var ShowHideOnFocus: boolean): Boolean;
 
540
begin
 
541
  Result := inherited GetCaretRespondToFocus(handle, ShowHideOnFocus);
 
542
end;
 
543
 
29
544
{------------------------------------------------------------------------------
30
545
  function ShowWindow(hWnd: HWND; nCmdShow: Integer): Boolean;
31
546
 
32
547
  nCmdShow:
33
548
    SW_SHOWNORMAL, SW_MINIMIZE, SW_SHOWMAXIMIZED
34
549
------------------------------------------------------------------------------}
35
 
 
36
 
procedure ColorToRGBFloat(cl: TColorRef; var r,g,b: Single); inline;
37
 
begin
38
 
  R:=(cl and $FF) / $FF;
39
 
  G:=((cl shr 8) and $FF) / $FF;
40
 
  B:=((cl shr 16) and $FF) / $FF;
41
 
end;
42
 
 
43
 
function RGBToColorFloat(r,g,b: Single): TColorRef; inline;
44
 
begin
45
 
  Result:=(Round(b*$FF) shl 16) or (Round(g*$FF) shl 8) or Round(r*$FF);
46
 
end;
47
 
 
48
 
{--------------------------------- WINDOWS ------------------------------------}
49
 
 
50
550
function TCocoaWidgetSet.ShowWindow(hWnd: HWND; nCmdShow: Integer): Boolean;
51
551
begin
52
552
  {$ifdef VerboseCocoaWinAPI}
60
560
      NSWindow(hwnd).orderOut(nil);
61
561
    SW_MINIMIZE:
62
562
      NSWindow(hwnd).miniaturize(nil);
 
563
    SW_MAXIMIZE:
 
564
      NSWindow(hwnd).zoom(nil);
63
565
  end;
64
566
  Result:=true;
65
567
end;
66
568
 
 
569
function TCocoaWidgetSet.StretchBlt(DestDC: HDC; X, Y, Width, Height: Integer;
 
570
  SrcDC: HDC; XSrc, YSrc, SrcWidth, SrcHeight: Integer; ROp: Cardinal
 
571
  ): Boolean;
 
572
begin
 
573
  Result := StretchMaskBlt(DestDC, X, Y, Width, Height, SrcDC, XSrc, YSrc,
 
574
    SrcWidth, SrcHeight, 0, 0, 0, Rop);
 
575
end;
 
576
 
 
577
function TCocoaWidgetSet.StretchMaskBlt(DestDC: HDC; X, Y, Width,
 
578
  Height: Integer; SrcDC: HDC; XSrc, YSrc, SrcWidth, SrcHeight: Integer;
 
579
  Mask: HBITMAP; XMask, YMask: Integer; Rop: DWORD): Boolean;
 
580
var
 
581
  SrcCtx, DestCtx: TCocoaContext;
 
582
begin
 
583
  DestCtx := CheckDC(DestDC);
 
584
  SrcCtx := CheckDC(SrcDC);
 
585
 
 
586
  Result := Assigned(DestCtx) and Assigned(SrcCtx);
 
587
 
 
588
  if not Result then
 
589
    Exit;
 
590
 
 
591
  Result := DestCtx.StretchDraw(X, Y, Width, Height,
 
592
    SrcCtx, XSrc, YSrc, SrcWidth, SrcHeight,
 
593
    TCocoaBitmap(Mask), XMask, YMask, Rop);
 
594
end;
 
595
 
 
596
function TCocoaWidgetSet.SystemParametersInfo(uiAction: DWord; uiParam: DWord;
 
597
  pvParam: Pointer; fWinIni: DWord): LongBool;
 
598
begin
 
599
  Result := True;
 
600
  case uiAction of
 
601
    SPI_GETWHEELSCROLLLINES: PDword(pvPAram)^ := 3;
 
602
    SPI_GETWORKAREA:
 
603
      TRect(pvParam^) := Bounds(GetSystemMetrics(SM_XVIRTUALSCREEN),
 
604
                                GetSystemMetrics(SM_YVIRTUALSCREEN),
 
605
                                GetSystemMetrics(SM_CXVIRTUALSCREEN),
 
606
                                GetSystemMetrics(SM_CYVIRTUALSCREEN));
 
607
  else
 
608
    Result := False;
 
609
  end
 
610
end;
 
611
 
67
612
{------------------------------------------------------------------------------
68
613
  Method:  GetWindowRect
69
614
  Params:  Handle - Handle of window
77
622
var
78
623
  dx, dy: Integer;
79
624
begin
80
 
  if Handle<>0 then begin
81
 
    ARect:=NSObject(Handle).lclFrame;
82
 
    if not NSObject(Handle).isKindOfClass_(NSWindow) then begin
83
 
      dx:=0; dy:=0;
 
625
  if Handle <> 0 then
 
626
  begin
 
627
    ARect := NSObject(Handle).lclFrame;
 
628
    if not NSObject(Handle).isKindOfClass_(NSWindow) then
 
629
    begin
 
630
      dx := 0;
 
631
      dy := 0;
84
632
      NSObject(Handle).lclLocalToScreen(dx, dx);
85
633
      MoveRect(ARect, dx, dy);
86
634
    end;
87
 
    Result:=1;
 
635
    Result := 1;
88
636
  end else
89
 
    Result:=0;
 
637
    Result := 0;
90
638
end;
91
639
 
92
640
function TCocoaWidgetSet.IsWindowEnabled(Handle: HWND): boolean;
96
644
    else Result:=False;
97
645
end;
98
646
 
99
 
function TCocoaWidgetSet.EnableWindow(hWnd: HWND; bEnable: Boolean): Boolean;
100
 
begin
101
 
  if hWnd<>0
102
 
    then NSObject(hWnd).lclSetEnabled(bEnable)
103
 
    else Result:=False;
104
 
end;
105
 
 
106
647
function TCocoaWidgetSet.IsWindowVisible(Handle: HWND): boolean;
107
648
begin
108
649
  if Handle<>0
112
653
 
113
654
function TCocoaWidgetSet.GetClientBounds(handle : HWND; var ARect : TRect) : Boolean;
114
655
begin
115
 
  if Handle<>0 then begin
116
 
    Result:=True;
117
 
    ARect:=NSObject(handle).lclClientFrame;
118
 
  end else
119
 
    Result:=False;
 
656
  Result := Handle <> 0;
 
657
  if Result then
 
658
    ARect := NSObject(handle).lclClientFrame;
120
659
end;
121
660
 
122
661
function TCocoaWidgetSet.GetClientRect(handle : HWND; var ARect : TRect) : Boolean;
123
 
var
124
 
  dx, dy: Integer;
125
 
begin
126
 
  if Handle<>0 then begin
127
 
    Result:=True;
128
 
    ARect:=NSObject(handle).lclClientFrame;
129
 
    dx:=0; dy:=0;
130
 
    NSObject(Handle).lclLocalToScreen(dx, dy);
131
 
    MoveRect(ARect, dx, dy);
132
 
  end else
133
 
    Result:=False;
 
662
begin
 
663
  Result := Handle <> 0;
 
664
  if Result then
 
665
  begin
 
666
    ARect := NSObject(handle).lclClientFrame;
 
667
    OffsetRect(ARect, -ARect.Left, -ARect.Top);
 
668
  end;
 
669
end;
 
670
 
 
671
function TCocoaWidgetSet.GetClipBox(DC: hDC; lpRect: PRect): Longint;
 
672
var
 
673
  ctx: TCocoaContext;
 
674
begin
 
675
  ctx := CheckDC(DC);
 
676
  if Assigned(ctx) and Assigned(lpRect) then
 
677
  begin
 
678
    lpRect^ := ctx.GetClipRect;
 
679
    Result := COMPLEXREGION;
 
680
  end
 
681
  else
 
682
    Result := ERROR;
 
683
end;
 
684
 
 
685
function TCocoaWidgetSet.GetClipRGN(DC: hDC; RGN: hRGN): Longint;
 
686
var
 
687
  ctx: TCocoaContext;
 
688
begin
 
689
  ctx := CheckDC(DC);
 
690
  if Assigned(ctx) and (RGN <> 0) then
 
691
    Result := CocoaRegionTypeToWin32Map[ctx.CopyClipRegion(TCocoaRegion(RGN))]
 
692
  else
 
693
    Result := ERROR;
 
694
end;
 
695
 
 
696
function TCocoaWidgetSet.GetCursorPos(var lpPoint: TPoint ): Boolean;
 
697
begin
 
698
  with NSEvent.mouseLocation do
 
699
  begin
 
700
    lpPoint.x := Round(x);
 
701
    // cocoa returns cursor with inverted y coordinate
 
702
    lpPoint.y := Round(NSScreen.mainScreen.frame.size.height-y);
 
703
  end;
 
704
  Result := True;
 
705
end;
 
706
 
 
707
function TCocoaWidgetSet.GetMonitorInfo(hMonitor: HMONITOR; lpmi: PMonitorInfo): Boolean;
 
708
var
 
709
  ScreenID: NSScreen absolute hMonitor;
 
710
begin
 
711
  Result := (lpmi <> nil) and (lpmi^.cbSize >= SizeOf(TMonitorInfo));
 
712
  if not Result then Exit;
 
713
  NSToLCLRect(ScreenID.frame, lpmi^.rcMonitor);
 
714
  NSToLCLRect(ScreenID.visibleFrame, lpmi^.rcWork);
 
715
  if ScreenID = NSScreen.mainScreen then
 
716
    lpmi^.dwFlags := MONITORINFOF_PRIMARY
 
717
  else
 
718
    lpmi^.dwFlags := 0;
 
719
end;
 
720
 
 
721
function TCocoaWidgetSet.GetObject(GDIObj: HGDIOBJ; BufSize: Integer; Buf: Pointer): Integer;
 
722
var
 
723
  AObject: TCocoaGDIObject;
 
724
  DIB: TDIBSection;
 
725
  Width, Height, RequiredSize, i: Integer;
 
726
 
 
727
  APen: TCocoaPen absolute AObject;
 
728
  ALogPen: PLogPen absolute Buf;
 
729
  AExtLogPen: PExtLogPen absolute Buf;
 
730
begin
 
731
  Result := 0;
 
732
 
 
733
  AObject := CheckGDIObj(GDIObj);
 
734
 
 
735
  if AObject is TCocoaBitmap then
 
736
  begin
 
737
    if Buf = nil then
 
738
    begin
 
739
      Result := SizeOf(TDIBSection);
 
740
      Exit;
 
741
    end;
 
742
 
 
743
    Width := TCocoaBitmap(AObject).Width;
 
744
    Height := TCocoaBitmap(AObject).Height;
 
745
 
 
746
    FillChar(DIB, SizeOf(TDIBSection), 0);
 
747
 
 
748
    {dsBM - BITMAP}
 
749
    DIB.dsBm.bmType := $4D42;
 
750
    DIB.dsBm.bmWidth := Width;
 
751
    DIB.dsBm.bmHeight := Height;
 
752
    DIB.dsBm.bmWidthBytes := 0;
 
753
    DIB.dsBm.bmPlanes := 1;
 
754
    DIB.dsBm.bmBitsPixel := 32;
 
755
    DIB.dsBm.bmBits := nil;
 
756
 
 
757
    {dsBmih - BITMAPINFOHEADER}
 
758
    DIB.dsBmih.biSize := 40;
 
759
    DIB.dsBmih.biWidth := Width;
 
760
    DIB.dsBmih.biHeight := Height;
 
761
    DIB.dsBmih.biPlanes := DIB.dsBm.bmPlanes;
 
762
    DIB.dsBmih.biCompression := 0;
 
763
    DIB.dsBmih.biSizeImage := 0;
 
764
    DIB.dsBmih.biXPelsPerMeter := 0;
 
765
    DIB.dsBmih.biYPelsPerMeter := 0;
 
766
    DIB.dsBmih.biClrUsed   := 0;
 
767
    DIB.dsBmih.biClrImportant := 0;
 
768
    DIB.dsBmih.biBitCount := 32;
 
769
 
 
770
    if BufSize >= SizeOf(TDIBSection) then
 
771
    begin
 
772
      PDIBSection(Buf)^ := DIB;
 
773
      Result := SizeOf(TDIBSection);
 
774
    end
 
775
    else
 
776
      if BufSize > 0 then
 
777
      begin
 
778
        System.Move(DIB, Buf^, BufSize);
 
779
        Result := BufSize;
 
780
      end;
 
781
  end
 
782
  else
 
783
  if AObject is TCocoaPen then
 
784
  begin
 
785
    if APen.IsExtPen then
 
786
    begin
 
787
      RequiredSize := SizeOf(TExtLogPen);
 
788
      if Length(APen.Dashes) > 1 then
 
789
        inc(RequiredSize, (Length(APen.Dashes) - 1) * SizeOf(DWord));
 
790
      if Buf = nil then
 
791
        Result := RequiredSize
 
792
      else
 
793
      if BufSize >= RequiredSize then
 
794
      begin
 
795
        Result := RequiredSize;
 
796
        AExtLogPen^.elpPenStyle := APen.Style;
 
797
        if APen.IsGeometric then
 
798
        begin
 
799
          case APen.JoinStyle of
 
800
            kCGLineJoinRound:
 
801
              AExtLogPen^.elpPenStyle := AExtLogPen^.elpPenStyle or PS_JOIN_ROUND;
 
802
            kCGLineJoinBevel:
 
803
              AExtLogPen^.elpPenStyle := AExtLogPen^.elpPenStyle or PS_JOIN_BEVEL;
 
804
            kCGLineJoinMiter:
 
805
              AExtLogPen^.elpPenStyle := AExtLogPen^.elpPenStyle or PS_JOIN_MITER;
 
806
          end;
 
807
 
 
808
          case APen.CapStyle of
 
809
            kCGLineCapRound:
 
810
              AExtLogPen^.elpPenStyle := AExtLogPen^.elpPenStyle or PS_ENDCAP_ROUND;
 
811
            kCGLineCapSquare:
 
812
              AExtLogPen^.elpPenStyle := AExtLogPen^.elpPenStyle or PS_ENDCAP_SQUARE;
 
813
            kCGLineCapButt:
 
814
              AExtLogPen^.elpPenStyle := AExtLogPen^.elpPenStyle or PS_ENDCAP_FLAT;
 
815
          end;
 
816
          AExtLogPen^.elpWidth := APen.Width;
 
817
        end
 
818
        else
 
819
          AExtLogPen^.elpWidth := 1;
 
820
 
 
821
        AExtLogPen^.elpBrushStyle := BS_SOLID;
 
822
        AExtLogPen^.elpColor := APen.ColorRef;
 
823
        AExtLogPen^.elpHatch := 0;
 
824
 
 
825
        AExtLogPen^.elpNumEntries := Length(APen.Dashes);
 
826
        if AExtLogPen^.elpNumEntries > 0 then
 
827
        begin
 
828
          for i := 0 to AExtLogPen^.elpNumEntries - 1 do
 
829
            PDword(@AExtLogPen^.elpStyleEntry)[i] := Trunc(APen.Dashes[i]);
 
830
        end
 
831
        else
 
832
          AExtLogPen^.elpStyleEntry[0] := 0;
 
833
      end;
 
834
    end
 
835
    else
 
836
    begin
 
837
      if Buf = nil then
 
838
        Result := SizeOf(TLogPen)
 
839
      else
 
840
      if BufSize >= SizeOf(TLogPen) then
 
841
      begin
 
842
        Result := SizeOf(TLogPen);
 
843
        ALogPen^.lopnStyle := APen.Style;
 
844
        ALogPen^.lopnWidth := Types.Point(APen.Width, 0);
 
845
        ALogPen^.lopnColor := APen.ColorRef;
 
846
      end;
 
847
    end;
 
848
  end
134
849
end;
135
850
 
136
851
function TCocoaWidgetSet.GetParent(Handle : HWND): HWND;
143
858
 
144
859
function TCocoaWidgetSet.GetWindowRelativePosition(Handle: hwnd; var Left, Top: Integer): boolean;
145
860
begin
146
 
  if Handle<>0 then begin
147
 
    Result:=True;
 
861
  Result := Handle <> 0;
 
862
  if Result then
148
863
    NSObject(handle).lclRelativePos(Left, Top);
149
 
  end else
150
 
    Result:=False;
151
864
end;
152
865
 
153
866
function TCocoaWidgetSet.GetWindowSize(Handle: hwnd; var Width, Height: Integer): boolean;
154
867
var
155
 
  r   : TRect;
156
 
begin
157
 
  if Handle<>0 then begin
158
 
    Result:=True;
159
 
    r:=NSObject(Handle).lclFrame;
160
 
    Width:=R.Right-R.Left;
161
 
    Height:=R.Bottom-R.Top;
162
 
  end else
163
 
    Result:=False;
 
868
  r: TRect;
 
869
begin
 
870
  Result := Handle <> 0;
 
871
  if Result then
 
872
  begin
 
873
    r := NSObject(Handle).lclFrame;
 
874
    Width := R.Right - R.Left;
 
875
    Height := R.Bottom - R.Top;
 
876
  end;
 
877
end;
 
878
 
 
879
function TCocoaWidgetSet.HideCaret(Handle: HWND): Boolean;
 
880
var
 
881
  Obj: NSObject;
 
882
begin
 
883
  Result := (Handle <> 0);
 
884
  if Result then
 
885
  begin
 
886
    Obj := NSObject(Handle);
 
887
    if Obj.isKindOfClass(NSView) then
 
888
      Result := CocoaCaret.HideCaret(NSView(Handle))
 
889
    else
 
890
    if Obj.isKindOfClass(NSWindow) then
 
891
      Result := CocoaCaret.HideCaret(NSWindow(Handle).contentView)
 
892
    else
 
893
      Result := False;
 
894
  end;
164
895
end;
165
896
 
166
897
function TCocoaWidgetSet.InvalidateRect(aHandle : HWND; Rect : pRect; bErase : Boolean): Boolean;
167
898
begin
168
 
  if aHandle<>0 then begin
169
 
    Result:=True;
170
 
    if Assigned(Rect)
171
 
      then NSObject(aHandle).lclInvalidateRect(Rect^)
172
 
      else NSObject(aHandle).lclInvalidate;
173
 
  end else
174
 
    Result:=False;
 
899
  Result := aHandle <> 0;
 
900
  if Result then
 
901
  begin
 
902
    if Assigned(Rect) then
 
903
      NSObject(aHandle).lclInvalidateRect(Rect^)
 
904
    else
 
905
      NSObject(aHandle).lclInvalidate;
 
906
  end;
175
907
end;
176
908
 
177
909
function TCocoaWidgetSet.UpdateWindow(Handle: HWND): Boolean;
178
910
begin
179
 
  Result:=InvalidateRect(Handle, nil, false);
180
 
end;
181
 
 
182
 
function TCocoaWidgetSet.GetProp(Handle : hwnd; Str : PChar): Pointer;
183
 
begin
184
 
  Result:=nil;
185
 
end;
186
 
 
187
 
function TCocoaWidgetSet.SetProp(Handle: hwnd; Str : PChar; Data : Pointer) : Boolean;
188
 
begin
189
 
  Result:=False;
 
911
  Result := Handle <> 0;
 
912
  if Result then
 
913
    NSObject(Handle).lclUpdate;
 
914
end;
 
915
 
 
916
function TCocoaWidgetSet.GetProp(Handle: hwnd; Str: PChar): Pointer;
 
917
var
 
918
  PropStorage: TStringList;
 
919
  I: Integer;
 
920
begin
 
921
  if Handle <> 0 then
 
922
  begin
 
923
    PropStorage := NSObject(Handle).lclGetPropStorage;
 
924
    if Assigned(PropStorage) then
 
925
    begin
 
926
      I := PropStorage.IndexOf(Str);
 
927
      if I <> -1 then
 
928
        Result := PropStorage.Objects[I]
 
929
      else
 
930
        Result := nil;
 
931
    end
 
932
    else
 
933
      Result := nil;
 
934
  end;
 
935
end;
 
936
 
 
937
function TCocoaWidgetSet.GetRgnBox(RGN: HRGN; lpRect: PRect): Longint;
 
938
begin
 
939
  Result := ERROR;
 
940
  if Assigned(lpRect) then
 
941
    lpRect^ := Types.Rect(0, 0, 0, 0);
 
942
 
 
943
  if not (TObject(RGN) is TCocoaRegion) then
 
944
    Exit;
 
945
 
 
946
  if Assigned(lpRect) then
 
947
  begin
 
948
    lpRect^ := TCocoaRegion(RGN).GetBounds;
 
949
    Result := CocoaRegionTypeToWin32Map[TCocoaRegion(RGN).GetType];
 
950
  end;
 
951
end;
 
952
 
 
953
function TCocoaWidgetSet.GetROP2(DC: HDC): Integer;
 
954
var
 
955
  ctx: TCocoaContext;
 
956
begin
 
957
  ctx := CheckDC(DC);
 
958
  if Assigned(ctx) then
 
959
    Result := ctx.ROP2
 
960
  else
 
961
    Result := 0;
 
962
end;
 
963
 
 
964
function TCocoaWidgetSet.SetProp(Handle: hwnd; Str: PChar; Data: Pointer): Boolean;
 
965
var
 
966
  PropStorage: TStringList;
 
967
begin
 
968
  Result := Handle <> 0;
 
969
  if Result then
 
970
  begin
 
971
    PropStorage := NSObject(Handle).lclGetPropStorage;
 
972
    Result := Assigned(PropStorage);
 
973
    if Result then
 
974
      PropStorage.AddObject(Str, TObject(Data));
 
975
  end;
 
976
end;
 
977
 
 
978
function TCocoaWidgetSet.SetROP2(DC: HDC; Mode: Integer): Integer;
 
979
var
 
980
  ctx: TCocoaContext;
 
981
begin
 
982
  ctx := CheckDC(DC);
 
983
  if Assigned(ctx) then
 
984
  begin
 
985
    Result := ctx.ROP2;
 
986
    ctx.ROP2 := Mode;
 
987
  end
 
988
  else
 
989
    Result := 0;
190
990
end;
191
991
 
192
992
{----------------------------- WINDOWS SCROLLING ------------------------------}
206
1006
  Result:=False;
207
1007
end;
208
1008
 
 
1009
function TCocoaWidgetSet.GetStockObject(Value: Integer): THandle;
 
1010
begin
 
1011
  Result := 0;
 
1012
 
 
1013
  case Value of
 
1014
    BLACK_BRUSH:         // Black brush.
 
1015
      Result := FStockBlackBrush;
 
1016
    DKGRAY_BRUSH:        // Dark gray brush.
 
1017
      Result := FStockDKGrayBrush;
 
1018
    GRAY_BRUSH:          // Gray brush.
 
1019
      Result := FStockGrayBrush;
 
1020
    LTGRAY_BRUSH:        // Light gray brush.
 
1021
      Result := FStockLtGrayBrush;
 
1022
    NULL_BRUSH:          // Null brush (equivalent to HOLLOW_BRUSH).
 
1023
      Result := FStockNullBrush;
 
1024
    WHITE_BRUSH:         // White brush.
 
1025
      Result := FStockWhiteBrush;
 
1026
 
 
1027
    BLACK_PEN:           // Black pen.
 
1028
      Result := FStockBlackPen;
 
1029
    NULL_PEN:            // Null pen.
 
1030
      Result := FStockNullPen;
 
1031
    WHITE_PEN:           // White pen.
 
1032
      Result := FStockWhitePen;
 
1033
 
 
1034
    DEFAULT_GUI_FONT, SYSTEM_FONT:
 
1035
      Result := FStockSystemFont;
 
1036
    SYSTEM_FIXED_FONT:
 
1037
      Result := FStockFixedFont;
 
1038
  end;
 
1039
end;
 
1040
 
 
1041
function SysColorToNSColor(nIndex: Integer): NSColor;
 
1042
begin
 
1043
  case NIndex of
 
1044
    COLOR_GRADIENTACTIVECAPTION, COLOR_ACTIVECAPTION,
 
1045
    COLOR_WINDOWFRAME, COLOR_ACTIVEBORDER:
 
1046
      Result := NSColor.windowFrameColor;
 
1047
    COLOR_GRADIENTINACTIVECAPTION, COLOR_INACTIVECAPTION, COLOR_INACTIVEBORDER:
 
1048
      Result := NSColor.windowBackgroundColor;
 
1049
    COLOR_CAPTIONTEXT,
 
1050
    COLOR_INACTIVECAPTIONTEXT:
 
1051
      Result := NSColor.windowFrameTextColor;
 
1052
    COLOR_BACKGROUND,
 
1053
    COLOR_WINDOW, COLOR_FORM:
 
1054
      Result := NSColor.windowBackgroundColor;
 
1055
    COLOR_MENU:
 
1056
      Result := NSColor.controlBackgroundColor;
 
1057
    COLOR_MENUTEXT:
 
1058
      Result := NSColor.controlTextColor;
 
1059
    COLOR_MENUBAR:
 
1060
      Result := NSColor.selectedTextBackgroundColor;
 
1061
    COLOR_MENUHILIGHT:
 
1062
      Result := NSColor.selectedMenuItemColor;
 
1063
    COLOR_INFOTEXT, COLOR_WINDOWTEXT:
 
1064
      Result := NSColor.controlTextColor;
 
1065
    COLOR_APPWORKSPACE:
 
1066
      Result := NSColor.windowBackgroundColor;
 
1067
    COLOR_HIGHLIGHT:
 
1068
      Result := NSColor.selectedControlColor;
 
1069
    COLOR_HOTLIGHT:
 
1070
      Result := NSColor.alternateSelectedControlColor;
 
1071
    COLOR_HIGHLIGHTTEXT:
 
1072
      Result := NSColor.selectedControlTextColor;
 
1073
    COLOR_SCROLLBAR:
 
1074
      Result := NSColor.scrollBarColor;
 
1075
    COLOR_BTNFACE:
 
1076
      Result := NSColor.controlColor;
 
1077
    COLOR_BTNSHADOW:
 
1078
      Result := NSColor.controlShadowColor;
 
1079
    COLOR_BTNHIGHLIGHT:
 
1080
      Result := NSColor.controlHighlightColor;
 
1081
    COLOR_BTNTEXT:
 
1082
      Result := NSColor.controlTextColor;
 
1083
    COLOR_GRAYTEXT:
 
1084
      Result := NSColor.disabledControlTextColor;
 
1085
    COLOR_3DDKSHADOW:
 
1086
      Result := NSColor.controlDarkShadowColor;
 
1087
    COLOR_3DLIGHT:
 
1088
      Result := NSColor.controlLightHighlightColor;
 
1089
    COLOR_INFOBK:
 
1090
      Result := NSColor.colorWithCalibratedRed_green_blue_alpha(249 / $FF, 252 / $FF, 201 / $FF, 1);
 
1091
  else
 
1092
    Result := nil;
 
1093
  end;
 
1094
end;
 
1095
 
 
1096
function TCocoaWidgetSet.GetSysColor(nIndex: Integer): DWORD;
 
1097
var
 
1098
  Color: NSColor;
 
1099
  SysBrush: HBrush;
 
1100
begin
 
1101
  // 1. get the system brush - it has a NSColor reference
 
1102
  SysBrush := GetSysColorBrush(nIndex);
 
1103
  if SysBrush = 0 then
 
1104
  begin
 
1105
    Result := 0;
 
1106
    Exit;
 
1107
  end;
 
1108
 
 
1109
  Color := TCocoaBrush(SysBrush).Color;
 
1110
 
 
1111
  if Assigned(Color) then
 
1112
    Result := NSColorToColorRef(Color)
 
1113
  else
 
1114
    Result := 0;
 
1115
end;
 
1116
 
 
1117
function TCocoaWidgetSet.GetSysColorBrush(nIndex: Integer): HBRUSH;
 
1118
begin
 
1119
  if (nIndex < 0) or (nIndex > MAX_SYS_COLORS) then
 
1120
  begin
 
1121
    Result := 0;
 
1122
    Exit;
 
1123
  end;
 
1124
 
 
1125
  if (FSysColorBrushes[nIndex] = 0) then
 
1126
    FSysColorBrushes[nIndex] := HBrush(TCocoaBrush.Create(SysColorToNSColor(nIndex), True));
 
1127
  Result := FSysColorBrushes[nIndex]
 
1128
end;
 
1129
 
209
1130
function TCocoaWidgetSet.SetScrollInfo(Handle : HWND; SBStyle : Integer; ScrollInfo: TScrollInfo; bRedraw : Boolean): Integer;
210
1131
begin
211
1132
  Result:=0;
225
1146
 
226
1147
function TCocoaWidgetSet.LineTo(DC: HDC; X, Y: Integer): Boolean;
227
1148
var
228
 
  ctx : TCocoaContext;
 
1149
  ctx: TCocoaContext;
229
1150
begin
230
 
  ctx:=CheckDC(DC);
231
 
  Result:=Assigned(ctx);
232
 
  if not Result then Exit;
233
 
 
234
 
  ctx.LineTo(x,y);
 
1151
  ctx := CheckDC(DC);
 
1152
  Result := Assigned(ctx);
 
1153
  if Result then
 
1154
    ctx.LineTo(X, Y);
235
1155
end;
236
1156
 
237
1157
function TCocoaWidgetSet.MoveToEx(DC: HDC; X, Y: Integer; OldPoint: PPoint): Boolean;
238
1158
var
239
 
  ctx : TCocoaContext;
 
1159
  ctx: TCocoaContext;
240
1160
begin
241
 
  ctx:=CheckDC(DC);
242
 
  Result:=Assigned(ctx);
243
 
  if not Result then Exit;
244
 
 
245
 
  if Assigned(OldPoint) then OldPoint^:=ctx.PenPos;
246
 
  ctx.MoveTo(x,y);
 
1161
  ctx := CheckDC(DC);
 
1162
  Result := Assigned(ctx);
 
1163
  if Result then
 
1164
  begin
 
1165
    if Assigned(OldPoint) then
 
1166
      OldPoint^ := ctx.PenPos;
 
1167
    ctx.MoveTo(X, Y);
 
1168
  end;
247
1169
end;
248
1170
 
249
1171
function TCocoaWidgetSet.Polygon(DC: HDC; Points: PPoint; NumPts: Integer; Winding: boolean): boolean;
250
1172
var
251
 
  ctx : TCocoaContext;
 
1173
  ctx: TCocoaContext;
252
1174
begin
253
 
  ctx:=CheckDC(DC);
254
 
  Result:=Assigned(ctx) and Assigned(Points) and (NumPts>=2);
255
 
  if not Result then Exit;
256
 
 
257
 
  ctx.Polygon(PPointArray(Points)^, NumPts, Winding);
258
 
  Result:=True;
 
1175
  ctx := CheckDC(DC);
 
1176
  Result := Assigned(ctx) and Assigned(Points) and (NumPts >= 2);
 
1177
  if Result then
 
1178
    ctx.Polygon(PPointArray(Points)^, NumPts, Winding);
259
1179
end;
260
1180
 
261
1181
function TCocoaWidgetSet.Polyline(DC: HDC; Points: PPoint; NumPts: Integer): boolean;
262
1182
var
263
 
  ctx : TCocoaContext;
 
1183
  ctx: TCocoaContext;
264
1184
begin
265
 
  ctx:=CheckDC(DC);
266
 
  Result:=Assigned(ctx) and Assigned(Points) and (NumPts>0);
267
 
  if not Result then Exit;
 
1185
  ctx := CheckDC(DC);
 
1186
  Result := Assigned(ctx) and Assigned(Points) and (NumPts > 0);
 
1187
  if Result then
 
1188
    ctx.Polyline(PPointArray(Points)^, NumPts);
 
1189
end;
268
1190
 
269
 
  ctx.Polyline(PPointArray(Points)^, NumPts);
270
 
  Result:=True;
 
1191
function TCocoaWidgetSet.PostMessage(Handle: HWND; Msg: Cardinal;
 
1192
  wParam: WParam; lParam: LParam): Boolean;
 
1193
var
 
1194
  Info: NSDictionary;
 
1195
  Event: NSEvent;
 
1196
begin
 
1197
  Result := Handle <> 0;
 
1198
  if Result then
 
1199
  begin
 
1200
    Info := PrepareUserEventInfo(Handle, Msg, WParam, LParam);
 
1201
    // if we will want a postmessage using notification center
 
1202
    // NSDistributedNotificationCenter.defaultCenter.postNotificationName_object_userInfo_deliverImmediately(NSMessageNotification, nil, Info, False);
 
1203
    Event := PrepareUserEvent(Handle, Info);
 
1204
    NSApp.postEvent_atStart(Event, False);
 
1205
  end;
271
1206
end;
272
1207
 
273
1208
function TCocoaWidgetSet.Rectangle(DC: HDC; X1, Y1, X2, Y2: Integer): Boolean;
274
1209
var
275
 
  ctx : TCocoaContext;
276
 
begin
277
 
  ctx:=CheckDC(DC);
278
 
  Result:=Assigned(ctx);
279
 
  if not Result then Exit;
280
 
 
281
 
  ctx.Rectangle(X1, Y1, X2, Y2, False, nil);
282
 
  Result:=True;
283
 
end;
284
 
 
285
 
function TCocoaWidgetSet.FillRect(DC: HDC; const Rect: TRect; Brush: HBRUSH): Boolean;
286
 
var
287
 
  ctx : TCocoaContext;
288
 
  br  : TCocoaGDIObject;
289
 
begin
290
 
  ctx:=CheckDC(DC);
291
 
  br:=CheckGDIOBJ(Brush);
292
 
  Result:=Assigned(ctx) and (not Assigned(br) or (br is TCocoaBrush));
293
 
  if not Result then Exit;
294
 
 
295
 
  with Rect do
296
 
    ctx.Rectangle(Left, Top, Right, Bottom, True, TCocoaBrush(br));
297
 
end;
298
 
 
299
 
function TCocoaWidgetSet.Ellipse(DC: HDC; x1, y1, x2, y2: Integer): Boolean;
300
 
var
301
 
  ctx : TCocoaContext;
302
 
begin
303
 
  ctx:=CheckDC(DC);
304
 
  Result:=Assigned(ctx);
305
 
  if not Result then Exit;
306
 
 
307
 
  ctx.Ellipse(x1, y1, x2, y2);
308
 
end;
309
 
 
310
 
{----------------------------------- REGION -----------------------------------}
311
 
 
312
 
{------------------------------------------------------------------------------
313
 
  Method:  CreatePolygonRgn
314
 
  Params:  Points   - Pointer to array of polygon points
315
 
           NumPts   - Number of points passed
316
 
           FillMode - Filling mode
317
 
  Returns: The new polygonal region
318
 
 
319
 
  Creates a new polygonal region from the specified points
320
 
 ------------------------------------------------------------------------------}
321
 
function TCocoaWidgetSet.CreatePolygonRgn(Points: PPoint; NumPts: Integer;
322
 
  FillMode: integer): HRGN;
323
 
begin
324
 
  {$IFDEF VerboseWinAPI}
325
 
    DebugLn('TCocoaWidgetSet.CreatePolygonRgn NumPts: ' + DbgS(NumPts) +
326
 
      ' FillMode: ' + DbgS(FillMode));
327
 
  {$ENDIF}
328
 
 
329
 
  Result := HRGN(TCocoaRegion.Create(Points, NumPts, FillMode=ALTERNATE));
330
 
end;
331
 
 
332
 
function CocoaCombineMode(fnCombineMode: Integer): TCocoaCombine;
333
 
begin
334
 
  case fnCombineMode of
335
 
    RGN_AND:  Result:=cc_And;
336
 
    RGN_OR:   Result:=cc_Or;
337
 
    RGN_XOR:  Result:=cc_Xor;
338
 
    RGN_DIFF: Result:=cc_Diff;
339
 
  else
340
 
    Result:=cc_Copy;
341
 
  end;
342
 
end;
343
 
 
344
 
function TCocoaWidgetSet.CombineRgn(Dest, Src1, Src2: HRGN;
345
 
  fnCombineMode: Longint): Longint;
346
 
begin
347
 
  Result := LCLType.Error;
348
 
  if (Dest = 0) or (Src1 = 0) or (fnCombineMode<RGN_AND) or (fnCombineMode>RGN_COPY) then Exit;
349
 
  if (fnCombineMode <> RGN_COPY) and (Src2 = 0) then Exit;
350
 
 
351
 
  TCocoaRegion(Dest).CombineWith(TCocoaRegion(Src1), cc_Copy);
352
 
 
353
 
  if fnCombineMode <> RGN_COPY then
354
 
    TCocoaRegion(Dest). CombineWith(TCocoaRegion(Src2), CocoaCombineMode(fnCombineMode));
355
 
end;
356
 
 
357
 
function TCocoaWidgetSet.CreateRectRgn(X1, Y1, X2, Y2: Integer): HRGN;
358
 
begin
359
 
  {$IFDEF VerboseWinAPI}
360
 
    DebugLn('TCocoaWidgetSet.CreateRectRgn R: ' + DbgS(Classes.Rect(X1, Y1, X2, Y2)));
361
 
  {$ENDIF}
362
 
 
363
 
  Result := HRGN(TCocoaRegion.Create(X1, Y1, X2, Y2));
364
 
end;
365
 
 
366
 
//todo:
367
 
//function TCocoaWidgetSet.CreateEllipticRgn(p1, p2, p3, p4: Integer): HRGN;
368
 
//begin
369
 
//end;
370
 
 
371
 
{---------------------------- PEN AND BRUSH -----------------------------------}
372
 
 
373
 
function TCocoaWidgetSet.CreatePenIndirect(const LogPen: TLogPen): HPEN;
374
 
var
375
 
  p   : TCocoaPen;
376
 
  cl  : DWORD;
377
 
begin
378
 
  {$IFDEF VerboseWinAPI}
379
 
    DebugLn('TCocoaWidgetSet.CreatePenIndirect');
380
 
  {$ENDIF}
381
 
  p:=TCocoaPen.Create;
382
 
  if LogPen.lopnWidth.x>0 then p.Width:=LogPen.lopnWidth.x;
383
 
  p.Style:=LogPen.lopnStyle;
384
 
  if LogPen.lopnColor and $8000000 > 0 then cl:=GetSysColor(LogPen.lopnColor)
385
 
  else cl:=LogPen.lopnColor;
386
 
  //todo:!
387
 
  ColorToRGBFloat(cl, p.R, p.G, p.B);
388
 
  Result := HPEN(p);//TCocoaPen.Create(LogPen));
389
 
end;
390
 
 
391
 
function TCocoaWidgetSet.CreateBrushIndirect(const LogBrush: TLogBrush): HBRUSH;
392
 
var
393
 
  b     : TCocoaBrush;
394
 
begin
395
 
  b:=TCocoaBrush.Create;
396
 
  with b do ColorToRGBFloat(LogBrush.lbColor, R, G, B);
397
 
  Result:=HBRUSH(b);
 
1210
  ctx: TCocoaContext;
 
1211
begin
 
1212
  ctx := CheckDC(DC);
 
1213
  Result := Assigned(ctx);
 
1214
  if Result then
 
1215
    ctx.Rectangle(X1, Y1, X2, Y2, False, nil);
398
1216
end;
399
1217
 
400
1218
{------------------------------- SYNC OBJECTS ---------------------------------}
411
1229
  CritSection:=0;
412
1230
end;
413
1231
 
414
 
type
415
 
  //workaround for CocoaInt bug
416
 
  NSLockExtension = objccategory(NSRecursiveLock)
417
 
    procedure lock; message 'lock';
418
 
    procedure unlock; message 'unlock';
419
 
  end; external;
 
1232
function TCocoaWidgetSet.DeleteDC(hDC: HDC): Boolean;
 
1233
begin
 
1234
  Result := hDC <> 0;
 
1235
  if Result then
 
1236
    TCocoaContext(hDC).Free;
 
1237
end;
420
1238
 
421
1239
procedure TCocoaWidgetSet.EnterCriticalSection(var CritSection: TCriticalSection);
422
1240
begin
433
1251
{------------------------------- DEVICE CONTEXT -------------------------------}
434
1252
 
435
1253
function TCocoaWidgetSet.GetDC(hWnd: HWND): HDC;
 
1254
var
 
1255
  ctx: TCocoaContext;
436
1256
begin
437
 
  if hWnd=0 then begin
 
1257
  if hWnd=0 then
 
1258
  begin
438
1259
    if not Assigned(ScreenContext) then ScreenContext:=TCocoaContext.Create;
439
1260
    Result:=HDC(ScreenContext);
440
 
  end else
441
 
    //todo:!
442
 
    Result:=0;
443
 
end;
444
 
 
445
 
function TCocoaWidgetSet.DeleteObject(GDIObject: HGDIOBJ): Boolean;
446
 
var
447
 
  gdi: TCocoaGDIObject;
448
 
begin
449
 
  Result:=True;
450
 
  gdi:=CheckGDIOBJ(GdiObject);
451
 
  if Assigned(gdi) then gdi.Release;
 
1261
  end
 
1262
  else
 
1263
  begin
 
1264
    // ToDo: Not finished yet
 
1265
    Result := 0;
 
1266
 
 
1267
{    if NSObject(hWnd) is TCocoaGroupBox then ctx := TCocoaGroupBox(hWnd).Context;
 
1268
 
 
1269
    if ctx <> nil then
 
1270
      Result := HDC(ctx)
 
1271
    // use dummy context if we are outside paint event
 
1272
    else
 
1273
    begin}
 
1274
      ctx := TCocoaContext.Create({NSObject(HWnd)});
 
1275
      Result := HDC(ctx);
 
1276
{    end;}
 
1277
  end;
 
1278
 
 
1279
  {$IFDEF VerboseWinAPI}
 
1280
    DebugLn('[TCocoaWidgetSet.GetDC] hWnd: %x Result: %x', [hWnd, Result]);
 
1281
  {$ENDIF}
 
1282
end;
 
1283
 
 
1284
function TCocoaWidgetSet.GetDeviceCaps(DC: HDC; Index: Integer): Integer;
 
1285
var
 
1286
  ctx: TCocoaContext;
 
1287
begin
 
1288
  ctx := CheckDC(DC);
 
1289
  if not Assigned(ctx) then
 
1290
    Exit(0);
 
1291
 
 
1292
  // todo: change implementation for printers
 
1293
  case Index of
 
1294
    HORZSIZE:
 
1295
      Result := Round(NSScreen.mainScreen.frame.size.width / 72 * 25.4);
 
1296
    VERTSIZE:
 
1297
      Result := Round(NSScreen.mainScreen.frame.size.height / 72 * 25.4);
 
1298
    HORZRES:
 
1299
      Result := Round(NSScreen.mainScreen.frame.size.width);
 
1300
    BITSPIXEL:
 
1301
      Result := CGDisplayBitsPerPixel(CGMainDisplayID);
 
1302
    PLANES:
 
1303
      Result := 1;
 
1304
    SIZEPALETTE:
 
1305
      Result := 0;
 
1306
    LOGPIXELSX:
 
1307
      Result := 72;
 
1308
    LOGPIXELSY:
 
1309
      Result := 72;
 
1310
    VERTRES:
 
1311
      Result := Round(NSScreen.mainScreen.frame.size.height);
 
1312
    NUMRESERVED:
 
1313
      Result := 0;
 
1314
  else
 
1315
    DebugLn('TCocoaWidgetSet.GetDeviceCaps TODO Index: ' + DbgS(Index));
 
1316
  end;
 
1317
end;
 
1318
 
 
1319
function TCocoaWidgetSet.GetDeviceSize(DC: HDC; var P: TPoint): Boolean;
 
1320
var
 
1321
  ctx: TCocoaContext;
 
1322
begin
 
1323
  ctx := CheckDC(DC);
 
1324
  Result := Assigned(ctx);
 
1325
  if Result then
 
1326
    with ctx.Size do
 
1327
    begin
 
1328
      P.X := cx;
 
1329
      P.Y := cy;
 
1330
    end;
 
1331
end;
 
1332
 
 
1333
function TCocoaWidgetSet.GetFocus: HWND;
 
1334
var
 
1335
  Obj: NSObject;
 
1336
begin
 
1337
  Result := HWND(NSApp.keyWindow);
 
1338
  if Result <> 0 then
 
1339
  begin
 
1340
    Obj := NSWindow(Result).firstResponder;
 
1341
    if Assigned(Obj) and Obj.isKindOfClass(NSView) then
 
1342
      Result := HWND(Obj);
 
1343
  end;
 
1344
end;
 
1345
 
 
1346
function TCocoaWidgetSet.GetForegroundWindow: HWND;
 
1347
//var
 
1348
//  App: NSRunningApplication;
 
1349
begin
 
1350
  // return the currect active window in the system
 
1351
{ this is not possible because we can't access another application NSApplication
 
1352
  for App in NSWorkSpace.sharedWorkspace.runningApplications do
 
1353
    if App.isActive then
 
1354
    begin
 
1355
      Result := HWND(App.keyWindow);
 
1356
      Exit;
 
1357
    end;
 
1358
}
 
1359
  if NSApp.isActive then
 
1360
    Result := HWND(NSApp.keyWindow)
 
1361
  else
 
1362
    Result := 0;
 
1363
end;
 
1364
 
 
1365
function TCocoaWidgetSet.GetKeyState(nVirtKey: Integer): Smallint;
 
1366
const
 
1367
  StateDown    = SmallInt($FF80);
 
1368
  StateToggled = SmallInt($0001);
 
1369
  DownMap: array[Boolean] of SmallInt = (0, StateDown);
 
1370
  ToggleMap: array[Boolean] of SmallInt = (0, StateToggled);
 
1371
var
 
1372
  Modifiers: NSUInteger;
 
1373
begin
 
1374
  Modifiers := NSApp.currentEvent.modifierFlags;
 
1375
  case nVirtKey of
 
1376
    VK_MENU,
 
1377
    VK_LMENU:
 
1378
      // the ssAlt/VK_MENU is mapped to optionKey under MacOS
 
1379
      Result := DownMap[(Modifiers and NSAlternateKeyMask) <> 0];
 
1380
    VK_SHIFT,
 
1381
    VK_LSHIFT:
 
1382
      Result := DownMap[(Modifiers and NSShiftKeyMask) <> 0];
 
1383
    VK_CONTROL,
 
1384
    VK_LCONTROL:
 
1385
      Result := DownMap[(Modifiers and NSControlKeyMask) <> 0];
 
1386
    VK_LWIN, VK_RWIN:
 
1387
      Result := DownMap[(Modifiers and NSCommandKeyMask) <> 0];
 
1388
    VK_CAPITAL:
 
1389
      Result := ToggleMap[(Modifiers and NSAlphaShiftKeyMask) <> 0];
 
1390
    VK_LBUTTON:
 
1391
      Result := DownMap[(GetCurrentEventButtonState and $01) <> 0];
 
1392
    VK_RBUTTON:
 
1393
      Result := DownMap[(GetCurrentEventButtonState and $02) <> 0];
 
1394
    VK_MBUTTON:
 
1395
      Result := DownMap[(GetCurrentEventButtonState and $03) <> 0];
 
1396
    VK_XBUTTON1:
 
1397
      Result := DownMap[(GetCurrentEventButtonState and $04) <> 0];
 
1398
    VK_XBUTTON2:
 
1399
      Result := DownMap[(GetCurrentEventButtonState and $05) <> 0];
 
1400
    else
 
1401
      Result := 0;
 
1402
  end;
452
1403
end;
453
1404
 
454
1405
function TCocoaWidgetSet.SelectObject(ADC: HDC; GDIObj: HGDIOBJ): HGDIOBJ;
459
1410
  SName = 'TCarbonWidgetSet.SelectObject';
460
1411
begin
461
1412
  {$IFDEF VerboseWinAPI}
462
 
    DebugLn('TCocoaWidgetSet.SelectObject DC: ' + DbgS(DC) + ' GDIObj: ' +  DbgS(GDIObj));
 
1413
    DebugLn(Format('TCocoaWidgetSet.SelectObject DC: %x GDIObj: %x', [ADC, GDIObj]));
463
1414
  {$ENDIF}
464
1415
  Result := 0;
465
1416
 
467
1418
  gdi:=CheckGDIOBJ(GDIObj);
468
1419
  if not Assigned(dc) then Exit;
469
1420
 
470
 
  if gdi is TCocoaBrush then begin // select brush
 
1421
  if gdi is TCocoaBrush then
 
1422
  begin // select brush
471
1423
    Result := HBRUSH(dc.Brush);
472
1424
    dc.Brush := TCocoaBrush(gdi);
473
 
  end else if gdi is TCocoaPen then begin // select pen
 
1425
  end else if gdi is TCocoaPen then
 
1426
  begin // select pen
474
1427
    Result := HPEN(dc.Pen);
475
1428
    dc.Pen := TCocoaPen(gdi);
476
 
  end else if gdi is TCocoaFont then begin // select font
 
1429
  end else if gdi is TCocoaFont then
 
1430
  begin // select font
477
1431
    Result := HFONT(dc.Font);
478
1432
    dc.Font := TCocoaFont(gdi);
479
 
  end else if gdi is TCocoaRegion then begin // select region
480
 
    Result := HBRUSH(dc.Region);
 
1433
  end else if gdi is TCocoaRegion then
 
1434
  begin // select region
 
1435
    Result := HRGN(dc.Region);
481
1436
    dc.Region := TCocoaRegion(gdi);
482
 
  end else if gdi is TCocoaBitmap then begin // select bitmap
 
1437
  end else if gdi is TCocoaBitmap then
 
1438
  begin // select bitmap
483
1439
    {if not (ADC is TCarbonBitmapContext) then
484
1440
    begin
485
1441
      DebugLn(SName + ' Error - The specified device context is not bitmap context!');
494
1450
  if Assigned(gdi) then gdi.AddRef;
495
1451
 
496
1452
  {$IFDEF VerboseWinAPI}
497
 
    DebugLn('TCocoaWidgetSet.SelectObject Result: ' + DbgS(Result));
 
1453
    DebugLn(Format('TCocoaWidgetSet.SelectObject Result: %x', [Result]));
498
1454
  {$ENDIF}
499
1455
end;
500
1456
 
 
1457
function TCocoaWidgetSet.SendMessage(Handle: HWND; Msg: Cardinal;
 
1458
  WParam: WParam; LParam: LParam): LResult;
 
1459
var
 
1460
  Info: NSDictionary;
 
1461
  Event: NSEvent;
 
1462
begin
 
1463
  if Handle <> 0 then
 
1464
  begin
 
1465
    Info := PrepareUserEventInfo(Handle, Msg, WParam, LParam);
 
1466
    Event := PrepareUserEvent(Handle, Info);
 
1467
    NSApp.sendEvent(Event);
 
1468
    Result := NSNumber(Info.objectForKey(NSMessageResult)).integerValue;
 
1469
  end;
 
1470
end;
 
1471
 
 
1472
function TCocoaWidgetSet.SetActiveWindow(Handle: HWND): HWND;
 
1473
var
 
1474
  Obj: NSObject;
 
1475
begin
 
1476
  Obj := NSObject(Handle);
 
1477
  if Assigned(Obj) and NSApp.isActive then
 
1478
  begin
 
1479
    Result := HWND(NSApp.keyWindow);
 
1480
    if Obj.isKindOfClass(NSWindow) then
 
1481
      NSwindow(Obj).makeKeyWindow
 
1482
    else
 
1483
    if Obj.isKindOfClass(NSView) then
 
1484
      NSView(Obj).window.makeKeyWindow
 
1485
    else
 
1486
      Result := 0;
 
1487
  end;
 
1488
end;
 
1489
 
 
1490
function TCocoaWidgetSet.SetBKColor(DC: HDC; Color: TColorRef): TColorRef;
 
1491
var
 
1492
  ctx: TCocoaContext;
 
1493
begin
 
1494
  ctx := CheckDC(DC);
 
1495
  if Assigned(ctx) then
 
1496
  begin
 
1497
    Result := ctx.BkColor;
 
1498
    ctx.BkColor := Color;
 
1499
  end
 
1500
  else
 
1501
    Result := CLR_INVALID;
 
1502
end;
 
1503
 
 
1504
function TCocoaWidgetSet.SetBkMode(DC: HDC; bkMode: Integer): Integer;
 
1505
var
 
1506
  ctx: TCocoaContext;
 
1507
begin
 
1508
  ctx := CheckDC(DC);
 
1509
  if Assigned(ctx) then
 
1510
  begin
 
1511
    Result := ctx.BkMode;
 
1512
    ctx.BkMode := bkMode;
 
1513
  end
 
1514
  else
 
1515
    Result := 0;
 
1516
end;
 
1517
 
 
1518
function TCocoaWidgetSet.SetCaretPos(X, Y: Integer): Boolean;
 
1519
begin
 
1520
  Result := CocoaCaret.SetCaretPos(X, Y);
 
1521
end;
 
1522
 
 
1523
function TCocoaWidgetSet.SetCaretPosEx(Handle: HWnd; X, Y: Integer): Boolean;
 
1524
begin
 
1525
  Result := CocoaCaret.SetCaretPos(X, Y);
 
1526
end;
 
1527
 
 
1528
function TCocoaWidgetSet.SetCaretRespondToFocus(handle: HWND;
 
1529
  ShowHideOnFocus: boolean): Boolean;
 
1530
begin
 
1531
  Result:=inherited SetCaretRespondToFocus(handle, ShowHideOnFocus);
 
1532
end;
 
1533
 
501
1534
function TCocoaWidgetSet.RectVisible(DC: HDC; const ARect: TRect): Boolean;
502
1535
var
503
1536
  ClipBox: CGRect;
504
1537
  ctx : TCocoaContext;
505
1538
  R: TRect;
506
1539
begin
507
 
  Result := False;
508
 
 
509
 
  {$IFDEF VerboseWinAPI}
510
 
    DebugLn('TCarbonWidgetSet.RectVisible DC: ' + DbgS(DC) + ' R: ' + DbgS(ARect));
511
 
  {$ENDIF}
512
 
 
513
 
  ctx:=CheckDC(DC);
514
 
  if not Assigned(ctx) or (ARect.Right <= ARect.Left) or (ARect.Bottom <= ARect.Top) then Exit;
 
1540
  ctx := CheckDC(DC);
 
1541
  Result := Assigned(ctx) and (ARect.Right > ARect.Left) and (ARect.Bottom > ARect.Top);
 
1542
 
 
1543
  if not Result then Exit;
515
1544
 
516
1545
  // In Quartz 2D there is no direct access to clipping path of CGContext,
517
1546
  // therefore we can only test bounding box of the clipping path.
518
1547
 
519
1548
  ClipBox := CGContextGetClipBoundingBox(ctx.CGContext);
520
1549
  Result := IntersectRect(R, ARect, CGRectToRect(ClipBox));
521
 
 
522
 
  {$IFDEF VerboseWinAPI}
523
 
    DebugLn('TCarbonWidgetSet.RectVisible Result: ' + DbgS(Result) + ' Clip: ' + DbgS(CGRectToRect(ClipBox)));
524
 
  {$ENDIF}
525
1550
end;
526
1551
 
527
1552
function TCocoaWidgetSet.MoveWindowOrgEx(DC: HDC; dX, dY: Integer): Boolean;
552
1577
  end;
553
1578
end;
554
1579
 
 
1580
function TCocoaWidgetSet.SetCursor(ACursor: HCURSOR): HCURSOR;
 
1581
begin
 
1582
  Result := HCURSOR(TCocoaCursor(ACursor).Install);
 
1583
end;
 
1584
 
 
1585
function TCocoaWidgetSet.SetCursorPos(X, Y: Integer): Boolean;
 
1586
var
 
1587
  CursorPos: CGPoint;
 
1588
begin
 
1589
  Result := False;
 
1590
 
 
1591
  CursorPos.X := X;
 
1592
  CursorPos.Y := Y;
 
1593
  if CGWarpMouseCursorPosition(CursorPos) <> noErr then Exit;
 
1594
  Result := True;
 
1595
end;
 
1596
 
 
1597
function TCocoaWidgetSet.SetFocus(Handle: HWND): HWND;
 
1598
var
 
1599
  Obj: NSObject;
 
1600
begin
 
1601
  if Handle <> 0 then
 
1602
  begin
 
1603
    if Result = Handle then
 
1604
      Exit;
 
1605
    Obj := NSObject(Handle);
 
1606
    if Obj.isKindOfClass(NSWindow) then
 
1607
    begin
 
1608
      NSWindow(Obj).makeKeyWindow;
 
1609
      NSWindow(Obj).makeFirstResponder(nil);
 
1610
    end
 
1611
    else
 
1612
    if Obj.isKindOfClass(NSView) then
 
1613
    begin
 
1614
      NSView(Obj).window.makeKeyWindow;
 
1615
      NSView(Obj).window.makeFirstResponder(NSView(Obj));
 
1616
    end;
 
1617
  end;
 
1618
end;
 
1619
 
 
1620
function TCocoaWidgetSet.SetForegroundWindow(HWnd: HWND): boolean;
 
1621
var
 
1622
  Obj: NSObject;
 
1623
begin
 
1624
  Result := HWnd <> 0;
 
1625
  if Result then
 
1626
  begin
 
1627
    NSApp.activateIgnoringOtherApps(True);
 
1628
    Obj := NSObject(HWnd);
 
1629
    if Obj.isKindOfClass(NSWindow) then
 
1630
      NSwindow(Obj).makeKeyAndOrderFront(NSApp)
 
1631
    else
 
1632
    if Obj.isKindOfClass(NSView) then
 
1633
      NSView(Obj).window.makeKeyAndOrderFront(NSApp)
 
1634
    else
 
1635
      Result := False;
 
1636
  end;
 
1637
end;
 
1638
 
555
1639
{------------------------------- FONT AND TEXT --------------------------------}
556
1640
 
557
 
function TCocoaWidgetSet.CreateFontIndirect(const LogFont: TLogFont): HFONT;
558
 
begin
559
 
  Result:=CreateFontIndirectEx(LogFont, LogFont.lfFaceName);
560
 
end;
561
 
 
562
 
function TCocoaWidgetSet.CreateFontIndirectEx(const LogFont: TLogFont;
563
 
  const LongFontName: string): HFONT;
564
 
var
565
 
  cf  : TCocoaFont;
566
 
begin
567
 
  cf:=TCocoaFont.Create;
568
 
  cf.Size:=LogFont.lfHeight;
569
 
  cf.Name:=LongFontName;
570
 
  if LogFont.lfWeight>FW_NORMAL then Include(cf.Style, cfs_Bold);
571
 
  if LogFont.lfItalic>0 then Include(cf.Style, cfs_Italic);
572
 
  if LogFont.lfUnderline>0 then Include(cf.Style, cfs_Underline);
573
 
  if LogFont.lfStrikeOut>0 then Include(cf.Style, cfs_Strikeout);
574
 
  cf.Antialiased:=logFont.lfQuality>=ANTIALIASED_QUALITY;
575
 
  Result:=HFONT(cf);
576
 
end;
577
 
 
578
 
function TCocoaWidgetSet.ExtTextOut(DC: HDC; X, Y: Integer; Options: Longint;
579
 
  Rect: PRect; Str: PChar; Count: Longint; Dx: PInteger): Boolean;
580
 
var
581
 
  ctx : TCocoaContext;
582
 
begin
583
 
  ctx:=CheckDC(DC);
584
 
  Result:=Assigned(ctx);
585
 
  if not Assigned(ctx) then Exit;
586
 
  ctx.TextOut(X,Y, Str, Count, Dx);
587
 
end;
588
 
 
589
1641
function TCocoaWidgetSet.SetTextColor(DC: HDC; Color: TColorRef): TColorRef;
590
1642
var
591
 
  ctx : TCocoaContext;
592
 
begin
593
 
  ctx:=CheckDC(DC);
594
 
  if not Assigned(ctx) then begin
595
 
    Result:=0;
596
 
    Exit;
597
 
  end;
598
 
  with ctx do begin
599
 
    Result:=RGBToColorFloat(TR, TG, TB);
600
 
    ColorToRGBFloat(Color, TR, TG, TB);
601
 
  end;
 
1643
  ctx: TCocoaContext;
 
1644
begin
 
1645
  ctx := CheckDC(DC);
 
1646
  if Assigned(ctx) then
 
1647
  begin
 
1648
    Result := ctx.TextColor;
 
1649
    ctx.TextColor := Color
 
1650
  end
 
1651
  else
 
1652
    Result := CLR_INVALID;
 
1653
end;
 
1654
 
 
1655
function TCocoaWidgetSet.ShowCaret(Handle: HWND): Boolean;
 
1656
var
 
1657
  Obj: NSObject;
 
1658
begin
 
1659
  Result := (Handle <> 0);
 
1660
  if Result then
 
1661
  begin
 
1662
    Obj := NSObject(Handle);
 
1663
    if Obj.isKindOfClass(NSView) then
 
1664
      Result := CocoaCaret.ShowCaret(NSView(Handle))
 
1665
    else
 
1666
    if Obj.isKindOfClass(NSWindow) then
 
1667
      Result := CocoaCaret.ShowCaret(NSWindow(Handle).contentView)
 
1668
    else
 
1669
      Result := False;
 
1670
  end;
 
1671
end;
 
1672
 
 
1673
{------------------------------------------------------------------------------
 
1674
  Method:  GetSystemMetrics
 
1675
  Params:  NIndex - System metric to retrieve
 
1676
  Returns: The requested system metric value
 
1677
 
 
1678
  Retrieves various system metrics.
 
1679
 ------------------------------------------------------------------------------}
 
1680
function TCocoaWidgetSet.GetSystemMetrics(nIndex: Integer): Integer;
 
1681
begin
 
1682
  Result := 0;
 
1683
 
 
1684
  {$IFDEF VerboseWinAPI}
 
1685
    DebugLn('TCocoaWidgetSet.GetSystemMetrics NIndex: ' + DbgS(NIndex));
 
1686
  {$ENDIF}
 
1687
 
 
1688
  case NIndex of
 
1689
{    SM_CXHSCROLL,
 
1690
    SM_CYHSCROLL,
 
1691
    SM_CXVSCROLL,
 
1692
    SM_CYVSCROLL:
 
1693
      Result := 10;//GetCarbonThemeMetric(kThemeMetricScrollBarWidth);}
 
1694
    SM_CXSCREEN,
 
1695
    SM_CXVIRTUALSCREEN: Result := Round(NSScreen.mainScreen.frame.size.width);
 
1696
    SM_CYSCREEN,
 
1697
    SM_CYVIRTUALSCREEN: Result := Round(NSScreen.mainScreen.frame.size.height);
 
1698
    SM_XVIRTUALSCREEN: Result := Round(NSScreen.mainScreen.frame.origin.x);
 
1699
    SM_YVIRTUALSCREEN: Result := Round(NSScreen.mainScreen.frame.origin.y);
 
1700
    SM_CXSMICON,
 
1701
    SM_CYSMICON:
 
1702
      Result := 16;
 
1703
    SM_CXICON,
 
1704
    SM_CYICON:
 
1705
      Result := 128;
 
1706
    SM_CXCURSOR,
 
1707
    SM_CYCURSOR:
 
1708
      begin
 
1709
{        if TCarbonCursor.HardwareCursorsSupported then
 
1710
          Result := 64 else}
 
1711
          Result := 16;
 
1712
      end;
 
1713
{    SM_CXHTHUMB:
 
1714
      Result := 16;//GetCarbonThemeMetric(kThemeMetricScrollBarMinThumbWidth);
 
1715
    SM_CYVTHUMB:
 
1716
      Result := 16;//GetCarbonThemeMetric(kThemeMetricScrollBarMinThumbHeight);}
 
1717
    SM_SWSCROLLBARSPACING:
 
1718
      Result:=0;
 
1719
  else
 
1720
    DebugLn('TCocoaWidgetSet.GetSystemMetrics TODO ', DbgS(NIndex));;
 
1721
  end;
 
1722
 
 
1723
  {$IFDEF VerboseWinAPI}
 
1724
    DebugLn('TCocoaWidgetSet.GetSystemMetrics Result: ' + DbgS(Result));
 
1725
  {$ENDIF}
602
1726
end;
603
1727
 
604
1728
function TCocoaWidgetSet.GetTextColor(DC: HDC) : TColorRef;
605
1729
var
 
1730
  ctx: TCocoaContext;
 
1731
begin
 
1732
  ctx := CheckDC(DC);
 
1733
  if Assigned(ctx) then
 
1734
    Result := ctx.TextColor
 
1735
  else
 
1736
    Result := CLR_INVALID;
 
1737
end;
 
1738
 
 
1739
{------------------------------------------------------------------------------
 
1740
  Method:  GetTextExtentPoint
 
1741
  Params:  DC    - Handle of device context
 
1742
           Str   - Text string
 
1743
           Count - Number of characters in string
 
1744
           Size  - The record for the dimensions of the string
 
1745
  Returns: If the function succeeds
 
1746
 
 
1747
  Computes the width and height of the specified string of text
 
1748
 ------------------------------------------------------------------------------}
 
1749
function TCocoaWidgetSet.GetTextExtentPoint(DC: HDC; Str: PChar; Count: Integer; var Size: TSize): Boolean;
 
1750
var
606
1751
  ctx : TCocoaContext;
607
1752
begin
 
1753
  {$IFDEF VerboseWinAPI}
 
1754
    DebugLn('[TCocoaWidgetSet.GetTextExtentPoint] DC: %x Str: %s Count: %d', [DC, Str, Count]);
 
1755
  {$ENDIF}
608
1756
  ctx:=CheckDC(DC);
609
 
  if not Assigned(ctx) then begin
610
 
    Result:=0;
611
 
    Exit;
612
 
  end;
613
 
  with ctx do Result:=RGBToColorFloat(TR, TG, TB);
614
 
end;
615
 
 
616
 
function TCocoaWidgetSet.TextOut(DC: HDC; X,Y : Integer; Str : Pchar; Count: Integer) : Boolean;
617
 
begin
618
 
  Result:=ExtTextOut(DC, X, Y, 0, nil, Str, Count, nil);
619
 
end;
620
 
 
621
 
function TCocoaWidgetSet.EnumFontFamiliesEx(DC: HDC; lpLogFont: PLogFont;
622
 
  Callback: FontEnumExProc; Lparam: LParam; Flags: dword): longint;
 
1757
  Result:=Assigned(ctx);
 
1758
  if not Assigned(ctx) then Exit(False);
 
1759
  Result := ctx.GetTextExtentPoint(Str, Count, Size);
 
1760
  {$IFDEF VerboseWinAPI}
 
1761
    DebugLn('[TCocoaWidgetSet.GetTextExtentPoint] Size: %d,%d', [Size.cx, Size.cy]);
 
1762
  {$ENDIF}
 
1763
end;
 
1764
 
 
1765
{------------------------------------------------------------------------------
 
1766
  Method:  GetTextMetrics
 
1767
  Params:  DC - Handle of device context
 
1768
           TM - The Record for the text metrics
 
1769
  Returns: If the function succeeds
 
1770
 
 
1771
  Fills the specified buffer with the metrics for the currently selected font
 
1772
  TODO: get exact max. and av. char width, pitch and charset
 
1773
 ------------------------------------------------------------------------------}
 
1774
function TCocoaWidgetSet.GetTextMetrics(DC: HDC; var TM: TTextMetric): Boolean;
623
1775
var
624
 
  fontManager : NSFontManager;
625
 
  arr         : NSArray;
626
 
  fname       : NSString;
627
 
  i           : Integer;
 
1776
  ctx: TCocoaContext;
 
1777
begin
 
1778
  ctx := CheckDC(DC);
 
1779
  Result := Assigned(ctx) and ctx.GetTextMetrics(TM);
 
1780
end;
628
1781
 
629
 
  ELogFont    : TEnumLogFontEx;
630
 
  Metric      : TNewTextMetricEx;
631
 
  FontName    : AnsiString;
 
1782
function TCocoaWidgetSet.TextOut(DC: HDC; X,Y: Integer; Str: Pchar; Count: Integer) : Boolean;
632
1783
begin
633
 
  Result:=0;
634
 
  if not Assigned(Callback) then Exit;
635
 
  fontManager:=NSFontManager.sharedFontManager;
636
 
  arr:=fontManager.availableFontFamilies;
637
 
  for i:=0 to arr.count-1 do begin
638
 
    fname:=NSString(arr.objectAtIndex(i));
639
 
    try
640
 
      FontName:=NSStringToString(fname);
641
 
      FillChar(ELogFont, SizeOf(ELogFont), #0);
642
 
      FillChar(Metric, SizeOf(Metric), #0);
643
 
      ELogFont.elfLogFont.lfFaceName := FontName;
644
 
      ELogFont.elfFullName := FontName;
645
 
      //todo: read the data from all fonts of the fontfamily
646
 
      Result:=CallBack(ELogFont, Metric, TRUETYPE_FONTTYPE, lparam);
647
 
      if Result=0 then Break;
648
 
    except
649
 
      Break;
650
 
    end;
651
 
  end;
652
 
  arr.release;
 
1784
  Result := ExtTextOut(DC, X, Y, 0, nil, Str, Count, nil);
653
1785
end;
654
1786
 
655
1787
function TCocoaWidgetSet.SaveDC(DC: HDC): Integer;
656
1788
var
657
 
  ctx : TCocoaContext;
658
 
  cg  : CGContextRef;
 
1789
  ctx: TCocoaContext;
659
1790
begin
660
1791
  ctx := CheckDC(DC);
661
 
  if not Assigned(ctx) then begin
662
 
    Result:=0;
663
 
    Exit;
664
 
  end;
665
 
  cg:=ctx.CGContext;
666
 
  if Assigned(cg) then begin
667
 
    CGContextSaveGState(cg);
668
 
    inc(ctx.Stack);
669
 
    Result:=ctx.Stack;
670
 
  end else
671
 
    Result:=0;
 
1792
  if Assigned(ctx) then
 
1793
    Result := ctx.SaveDC
 
1794
  else
 
1795
    Result:=0;
 
1796
end;
 
1797
 
 
1798
function TCocoaWidgetSet.ScreenToClient(Handle: HWND; var P: TPoint): Integer;
 
1799
begin
 
1800
  Result := Ord(Handle <> 0);
 
1801
 
 
1802
  if Result = 1 then
 
1803
  begin
 
1804
    // 1. convert screen to window
 
1805
    NSObject(Handle).lclScreenToLocal(P.X, P.Y);
 
1806
    // 2. convert window to client
 
1807
    with NSObject(Handle).lclClientFrame do
 
1808
    begin
 
1809
      dec(P.X, Left);
 
1810
      dec(P.Y, Top);
 
1811
    end;
 
1812
  end;
 
1813
end;
 
1814
 
 
1815
function TCocoaWidgetSet.SelectClipRGN(DC: hDC; RGN: HRGN): Longint;
 
1816
begin
 
1817
  Result := ExtSelectClipRgn(DC, RGN, RGN_COPY);
 
1818
end;
 
1819
 
 
1820
function TCocoaWidgetSet.SetSysColors(cElements: Integer; const lpaElements; const lpaRgbValues): Boolean;
 
1821
var
 
1822
  n: Integer;
 
1823
  Element: LongInt;
 
1824
  Color: NSColor;
 
1825
begin
 
1826
  Result := False;
 
1827
  if cElements > MAX_SYS_COLORS then Exit;
 
1828
 
 
1829
  for n := 0 to cElements - 1 do
 
1830
  begin
 
1831
    Element := PInteger(@lpaElements)[n];
 
1832
    if (Element > MAX_SYS_COLORS) or (Element < 0) then 
 
1833
      Exit;
 
1834
    Color := ColorToNSColor(PDWord(@lpaRgbValues)[n]);
 
1835
    if (FSysColorBrushes[Element] <> 0) then
 
1836
      TCocoaBrush(FSysColorBrushes[Element]).Color := Color
 
1837
    else
 
1838
      FSysColorBrushes[Element] := HBrush(TCocoaBrush.Create(Color, True));
 
1839
  end;
 
1840
 
 
1841
  Result := True;
672
1842
end;
673
1843
 
674
1844
function TCocoaWidgetSet.RestoreDC(DC: HDC; SavedDC: Integer): Boolean;
675
1845
var
676
 
  ctx : TCocoaContext;
677
 
  cg  : CGContextRef;
678
 
  cnt : Integer;
679
 
  i   : Integer;
 
1846
  ctx: TCocoaContext;
680
1847
begin
681
 
  Result:=False;
682
1848
  ctx := CheckDC(DC);
683
 
  cg:=ctx.CGContext;
684
 
  if not Assigned(ctx) or not Assigned(cg) then Exit;
685
 
 
686
 
  if SavedDC<0 then cnt:=1
687
 
  else cnt:=ctx.Stack-SavedDC+1;
688
 
  Result:=cnt>0;
689
 
 
690
 
  if Result then begin
691
 
    for i:=1 to cnt do CGContextRestoreGState(cg);
692
 
    dec(ctx.Stack, cnt);
693
 
  end;
 
1849
  if Assigned(ctx) then
 
1850
    Result := ctx.RestoreDC(SavedDC)
 
1851
  else
 
1852
    Result := False;
 
1853
end;
 
1854
 
 
1855
function TCocoaWidgetSet.RoundRect(DC: HDC; X1, Y1, X2, Y2: Integer; RX,
 
1856
  RY: Integer): Boolean;
 
1857
begin
 
1858
  Result:=inherited RoundRect(DC, X1, Y1, X2, Y2, RX, RY);
694
1859
end;
695
1860
 
696
1861
//##apiwiz##eps##   // Do not remove, no wizard declaration after this line