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

« back to all changes in this revision

Viewing changes to lcl/interfaces/customdrawn/cocoautils.pas

  • 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:
 
1
unit CocoaUtils;
 
2
 
 
3
{$mode objfpc}{$H+}
 
4
{$modeswitch objectivec1}
 
5
 
 
6
interface
 
7
 
 
8
uses
 
9
  MacOSAll, CocoaAll,
 
10
  Types, LCLType;
 
11
 
 
12
type
 
13
  { NSLCLDebugExtension }
 
14
 
 
15
  NSLCLDebugExtension = objccategory(NSObject)
 
16
    function lclClassName: shortstring; message 'lclClassName';
 
17
  end;
 
18
 
 
19
const
 
20
  NSNullRect : NSRect = (origin:(x:0; y:0); size:(width:0; height:0));
 
21
 
 
22
function GetNSPoint(x,y: single): NSPoint; inline;
 
23
 
 
24
function GetCGRect(x1, y1, x2, y2: Integer): CGRect;
 
25
function CGRectToRect(const c: CGRect): TRect;
 
26
 
 
27
function GetNSRect(x, y, width, height: Integer): NSRect; inline;
 
28
function RectToNSRect(const r: TRect): NSRect;
 
29
 
 
30
procedure NSToLCLRect(const ns: NSRect; var lcl: TRect); overload;
 
31
procedure NSToLCLRect(const ns: NSRect; ParentHeight: Single; var lcl: TRect); overload;
 
32
 
 
33
procedure LCLToNSRect(const lcl: TRect; var ns: NSRect); overload;
 
34
procedure LCLToNSRect(const lcl: TRect; ParentHeight: Single; var ns: NSRect); overload;
 
35
 
 
36
function CreateParamsToNSRect(const params: TCreateParams): NSRect;
 
37
 
 
38
function NSStringUtf8(s: PChar): NSString;
 
39
function NSStringUtf8(const s: String): NSString;
 
40
function NSStringToString(ns: NSString): String;
 
41
 
 
42
function GetNSObjectView(obj: NSObject): NSView;
 
43
procedure AddViewToNSObject(ctrl: NSView; obj: NSObject);
 
44
procedure AddViewToNSObject(ctrl: NSView; obj: NSObject; X,Y: integer);
 
45
 
 
46
procedure SetNSText(text: NSText; const s: String); inline;
 
47
function GetNSText(text: NSText): string; inline;
 
48
 
 
49
procedure SetNSControlValue(c: NSControl; const S: String); inline;
 
50
function GetNSControlValue(c: NSControl): String; inline;
 
51
 
 
52
implementation
 
53
 
 
54
const
 
55
  DEFAULT_CFSTRING_ENCODING = kCFStringEncodingUTF8;
 
56
 
 
57
function CFStringToStr(AString: CFStringRef; Encoding: CFStringEncoding = DEFAULT_CFSTRING_ENCODING): String;
 
58
var
 
59
  Str: Pointer;
 
60
  StrSize: CFIndex;
 
61
  StrRange: CFRange;
 
62
begin
 
63
  if AString = nil then
 
64
  begin
 
65
    Result := '';
 
66
    Exit;
 
67
  end;
 
68
 
 
69
  // Try the quick way first
 
70
  Str := CFStringGetCStringPtr(AString, Encoding);
 
71
  if Str <> nil then
 
72
    Result := PChar(Str)
 
73
  else
 
74
  begin
 
75
    // if that doesn't work this will
 
76
    StrRange.location := 0;
 
77
    StrRange.length := CFStringGetLength(AString);
 
78
 
 
79
    CFStringGetBytes(AString, StrRange, Encoding,
 
80
      Ord('?'), False, nil, 0, StrSize);
 
81
    SetLength(Result, StrSize);
 
82
 
 
83
    if StrSize > 0 then
 
84
      CFStringGetBytes(AString, StrRange, Encoding,
 
85
        Ord('?'), False, @Result[1], StrSize, StrSize);
 
86
  end;
 
87
end;
 
88
 
 
89
function GetNSObjectView(obj: NSObject): NSView;
 
90
begin
 
91
  Result:=nil;
 
92
  if not Assigned(obj) then Exit;
 
93
  if obj.isKindOfClass_(NSView) then Result:=NSView(obj)
 
94
  else if obj.isKindOfClass_(NSWindow) then Result:=NSWindow(obj).contentView;
 
95
end;
 
96
 
 
97
procedure AddViewToNSObject(ctrl: NSView; obj: NSObject);
 
98
var
 
99
  view : NSView;
 
100
begin
 
101
  view:=GetNSObjectView(obj);
 
102
  if not Assigned(view) then Exit;
 
103
  view.addSubView(ctrl);
 
104
end;
 
105
 
 
106
procedure AddViewToNSObject(ctrl: NSView; obj: NSObject; X,Y: integer);
 
107
begin
 
108
  AddViewToNSObject(ctrl, obj);
 
109
  //SetViewFramePos(ctrl, x,y);
 
110
end;
 
111
 
 
112
function GetNSPoint(x, y: single): NSPoint;
 
113
begin
 
114
  Result.x:=x;
 
115
  Result.y:=y;
 
116
end;
 
117
 
 
118
function GetNSRect(x, y, width, height: Integer): NSRect;
 
119
begin
 
120
  Result.origin.x:=x;
 
121
  Result.origin.y:=y;
 
122
  Result.size.width:=width;
 
123
  Result.size.height:=height;
 
124
end;
 
125
 
 
126
function GetCGRect(x1, y1, x2, y2: Integer): CGRect;
 
127
begin
 
128
  Result.origin.x:=x1;
 
129
  Result.origin.y:=y1;
 
130
  Result.size.width:=x2-x1;
 
131
  Result.size.height:=y2-y1;
 
132
end;
 
133
 
 
134
function CGRectToRect(const c:CGRect):TRect;
 
135
begin
 
136
  with Result do begin
 
137
    Left:=round(c.origin.x);
 
138
    Top:=round(c.origin.y);
 
139
    Right:=round(c.origin.x+c.size.width);
 
140
    Bottom:=round(c.origin.y+c.size.height);
 
141
  end;
 
142
end;
 
143
 
 
144
function RectToNSRect(const r: TRect): NSRect;
 
145
begin
 
146
  Result:=GetNSRect(r.Left,r.Top,r.Right-r.Left,r.Bottom-r.Top);
 
147
end;
 
148
 
 
149
procedure NSToLCLRect(const ns: NSRect; var lcl: TRect);
 
150
begin
 
151
  lcl.Left:=round(ns.origin.x);
 
152
  lcl.Top:=round(ns.origin.y);
 
153
  lcl.Right:=round(ns.origin.x+ns.size.width);
 
154
  lcl.Bottom:=round(ns.origin.y+ns.size.height);
 
155
end;
 
156
 
 
157
procedure NSToLCLRect(const ns: NSRect; ParentHeight: Single; var lcl: TRect);
 
158
begin
 
159
  lcl.Left:=Round(ns.origin.x);
 
160
  lcl.Top:=Round(ParentHeight-ns.size.height-ns.origin.y);
 
161
  lcl.Right:=Round(ns.origin.x+ns.size.width);
 
162
  lcl.Bottom:=Round(lcl.Top+ns.size.height);
 
163
end;
 
164
 
 
165
procedure LCLToNSRect(const lcl: TRect; var ns: NSRect); overload;
 
166
begin
 
167
  ns.origin.x:=lcl.Left;
 
168
  ns.origin.y:=lcl.Top;
 
169
  ns.size.width:=lcl.Right-lcl.Left;
 
170
  ns.size.height:=lcl.Bottom-lcl.Top;
 
171
end;
 
172
 
 
173
procedure LCLToNSRect(const lcl: TRect; ParentHeight: Single; var ns: NSRect); overload;
 
174
begin
 
175
  ns.origin.x:=lcl.left;
 
176
  ns.origin.y:=ParentHeight-(lcl.bottom-lcl.Top)-lcl.Top;
 
177
  ns.size.width:=lcl.Right-lcl.Left;
 
178
  ns.size.height:=lcl.Bottom-lcl.Top;
 
179
end;
 
180
 
 
181
 
 
182
function CreateParamsToNSRect(const params: TCreateParams): NSRect;
 
183
begin
 
184
  with params do Result:=GetNSRect(X,Y,Width,Height);
 
185
end;
 
186
 
 
187
function NSStringUtf8(s: PChar): NSString;
 
188
var
 
189
  cf : CFStringRef;
 
190
begin
 
191
  {NSString and CFStringRef are interchangable}
 
192
  cf:=CFStringCreateWithCString(nil, S, kCFStringEncodingUTF8);
 
193
  Result:=NSString(cf);
 
194
end;
 
195
 
 
196
function NSStringUtf8(const s: String): NSString;
 
197
var
 
198
  cf : CFStringRef;
 
199
begin
 
200
  {NSString and CFStringRef are interchangable}
 
201
  cf:=CFStringCreateWithCString(nil, Pointer(PChar(S)), kCFStringEncodingUTF8);
 
202
  Result:=NSString(cf);
 
203
end;
 
204
 
 
205
function NSStringToString(ns: NSString): String;
 
206
begin
 
207
  Result:=CFStringToStr(CFStringRef(ns));
 
208
end;
 
209
 
 
210
procedure SetNSText(text: NSText; const s: String); inline;
 
211
var
 
212
  ns : NSString;
 
213
begin
 
214
  if Assigned(text) then
 
215
  begin
 
216
    ns:=NSStringUTF8(s);
 
217
    text.setString(ns);
 
218
    ns.release;
 
219
  end;
 
220
end;
 
221
 
 
222
function GetNSText(text: NSText): string; inline;
 
223
begin
 
224
  if Assigned(text) then
 
225
    Result := NSStringToString(text.string_)
 
226
  else
 
227
    Result:='';
 
228
end;
 
229
 
 
230
procedure SetNSControlValue(c: NSControl; const S: String); inline;
 
231
var
 
232
  ns : NSString;
 
233
begin
 
234
  if Assigned(c) then
 
235
  begin
 
236
    ns:=NSStringUtf8(S);
 
237
    c.setStringValue(ns);
 
238
    ns.release;
 
239
  end;
 
240
end;
 
241
 
 
242
function GetNSControlValue(c: NSControl): String; inline;
 
243
begin
 
244
  if Assigned(c) then
 
245
    Result:=NSStringToString(c.stringValue)
 
246
  else
 
247
    Result:='';
 
248
end;
 
249
 
 
250
 
 
251
{ NSLCLDebugExtension }
 
252
 
 
253
function NSLCLDebugExtension.lclClassName: shortstring;
 
254
begin
 
255
  Result:=NSStringToString(self.className);
 
256
end;
 
257
 
 
258
initialization
 
259
 
 
260
end.
 
261