This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathiosbutton.lcb
More file actions
224 lines (168 loc) · 8.14 KB
/
iosbutton.lcb
File metadata and controls
224 lines (168 loc) · 8.14 KB
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
/*
Copyright (C) 2017 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
/**
This widget is a native push button on iOS.
Name: enabled
Type: property
Summary: Whether the field is active and responds to user action
Syntax:
set the enabled of <widget> to {true | false}
get the enabled of <widget>
Description:
Use the <enabled> property to enable or disable the native button. When
disabled, the button has a greyed out appearance and does not accept
clicks or touches.
*/
widget com.livecode.widget.native.ios.button
use com.livecode.foreign
use com.livecode.objc
use com.livecode.widget
use com.livecode.canvas
use com.livecode.engine
use com.livecode.library.widgetutils
metadata version is "1.0.0"
metadata author is "LiveCode"
metadata title is "iOS Native Button"
metadata os is "ios"
metadata svgicon is "M 535.66797 241.55273 C 491.1077 241.55273 455.23438 277.42605 455.23438 321.98633 L 455.23438 913.65039 C 455.23438 958.21066 491.1077 994.08398 535.66797 994.08398 L 2014.5762 994.08398 C 2059.1364 994.08398 2095.0098 958.21066 2095.0098 913.65039 L 2095.0098 321.98633 C 2095.0098 277.42605 2059.1364 241.55273 2014.5762 241.55273 L 535.66797 241.55273 z M 1155.1543 304.32422 C 1293.7311 304.30837 1399.8223 413.6262 1399.8223 613.28906 C 1399.8223 831.92443 1278.4507 931.3125 1145.5957 931.3125 C 1009.8798 931.3125 899.97656 823.80972 899.97656 622.33203 C 899.97656 415.4574 1013.7086 304.32422 1155.1543 304.32422 z M 1637.668 305.20508 C 1689.1647 305.20508 1728.013 319.66027 1746.082 332.30859 L 1738.9023 347.9043 C 1723.5436 337.0708 1682.0317 323.26562 1636.875 323.26562 C 1535.6646 323.26562 1491.5781 399.7417 1491.5781 456.04102 C 1491.5781 533.80129 1551.8559 559.80417 1629.5527 596.8457 C 1719.89 641.10692 1767.7656 680.13793 1767.7656 765.07031 C 1767.7656 856.31896 1702.7239 930.38477 1587.082 930.38477 C 1539.1912 930.38477 1484.0899 914.13108 1456.9863 893.34375 L 1465.0605 875.2832 C 1494.8824 895.15914 1546.3559 914.35352 1590.625 914.35352 C 1674.646 914.35352 1749.3711 855.57405 1749.3711 769.05664 C 1749.3711 688.7683 1699.5944 646.87616 1615.082 610.88867 C 1539.4457 578.68139 1471.4414 543.72442 1471.4414 457.89648 C 1471.4414 368.46267 1541.9021 305.20508 1637.668 305.20508 z M 1156.1758 325.58008 C 999.78351 325.58008 917.99023 456.01743 917.99023 617.80664 C 917.99023 783.58214 998.33285 912.36328 1146.4355 912.36328 C 1295.5685 912.36328 1381.1035 779.61907 1381.1035 615.91992 C 1381.1035 464.11624 1312.5681 325.58008 1156.1758 325.58008 z M 799.93164 336.48438 C 810.31342 336.48438 816.91406 344.97176 816.91406 355.35352 C 816.91406 366.19495 810.31315 374.21484 798.98828 374.21484 C 789.55751 374.21484 782.48047 366.19495 782.48047 355.35352 C 782.48047 344.97176 790.02537 336.48438 799.93164 336.48438 z M 790.80859 488.61328 L 808.58398 488.61328 L 808.58398 922.26953 L 790.80859 922.26953 L 790.80859 488.61328 z"
/**
Syntax:
set the label of <widget> to <pLabel>
get the label of <widget>
Summary: The label displayed by the button.
Value (string): The string to use as the button label
Example:
set the label of widget "iOS Button" to "Click me!"
Description:
The <label> property is the label displayed by the button.
*/
property label get mLabel set SetLabel
metadata label.editor is "com.livecode.pi.string"
metadata label.default is ""
private variable mLabel as String
private variable mButtonView as optional ObjcObject
private variable mButtonProxy as optional ObjcObject
private handler IsIOS() returns Boolean
return the operating system is "ios"
end handler
/**/
public handler OnCreate()
put "" into mLabel
end handler
public handler OnDestroy()
put nothing into mButtonView
end handler
/**/
public handler OnOpen()
if IsIOS() then
unsafe
CreateButtonView()
end unsafe
end if
end handler
public handler OnClose()
if IsIOS() then
unsafe
DestroyButtonView()
end unsafe
end if
end handler
/**/
public handler OnPaint()
if IsIOS() then
return
end if
variable tLabel as String
if mLabel is empty then
put my name into tLabel
else
put mLabel into tLabel
end if
paintPlaceholderImage(this canvas, my bounds, placeholderIcon("ios"), \
tLabel, my font, nothing)
end handler
/**/
public handler OnSave(out rProperties as Array)
put mLabel into rProperties["label"]
end handler
public handler OnLoad(in pProperties as Array)
put pProperties["label"] into mLabel
end handler
/**/
public handler OnParentPropertyChanged()
unsafe
UpdateButtonView()
end unsafe
end handler
/**/
private handler SetLabel(in pLabel as String) returns nothing
put pLabel into mLabel
unsafe
UpdateButtonView()
end unsafe
redraw all
end handler
/****/
private handler ButtonActionCallback(in pSender as ObjcObject, in pContext as optional any) returns nothing
post "mouseUp"
end handler
/****/
private type NSUInteger is CULong
private foreign handler ObjC_UIButtonButtonWithType(in pType as NSUInteger) returns ObjcId binds to "objc:UIButton.+buttonWithType:?ui"
private foreign handler ObjC_UIButtonSetTitleForState(in pObj as ObjcId, in pTitle as ObjcId, in pState as NSUInteger) returns nothing binds to "objc:UIButton.-setTitle:forState:?ui"
private foreign handler ObjC_UIButtonSetEnabled(in pObj as ObjcId, in pEnabled as CBool) returns nothing binds to "objc:UIButton.-setEnabled:?ui"
private foreign handler ObjC_UIButtonGetTitleLabel(in pObj as ObjcId) returns ObjcId binds to "objc:UIButton.-titleLabel?ui"
private foreign handler ObjC_UILabelSetFont(in pObj as ObjcId, in pFont as ObjcId) returns nothing binds to "objc:UILabel.-setFont:?ui"
private foreign handler ObjC_UIButtonAddTargetActionForControlEvents(in pObj as ObjcId, in pTarget as ObjcId, in pAction as UIntPtr, in pControlEvents as NSUInteger) returns nothing binds to "objc:UIButton.-addTarget:action:forControlEvents:?ui"
private foreign handler MCCanvasFontGetHandle(in pFont as Font, out rHandle as ObjcId) returns nothing binds to "<builtin>"
private unsafe handler CreateButtonView() returns nothing
variable tButtonView as ObjcObject
/* For a standard push button we need:
* type to be UIButtonTypeSystem = 1 */
put ObjC_UIButtonButtonWithType(1) into tButtonView
set my native layer to PointerFromObjcObject(tButtonView)
put tButtonView into mButtonView
put ObjcProxyGetTarget(ButtonActionCallback, nothing) into mButtonProxy
/* For a push button action we need:
* controlEvents to be UIControlEventTouchUpInside = 1 << 6 */
ObjC_UIButtonAddTargetActionForControlEvents(mButtonView, mButtonProxy, ObjcProxyGetAction(), 1 shifted left by 6 bitwise)
UpdateButtonView()
end handler
private unsafe handler DestroyButtonView() returns nothing
set my native layer to nothing
put nothing into mButtonView
put nothing into mButtonProxy
end handler
private unsafe handler UpdateButtonView() returns nothing
if mButtonView is nothing then
return
end if
/* Set the enabled state of the button to the host property. */
ObjC_UIButtonSetEnabled(mButtonView, my enabled)
/* Set the font of the button to the host property. */
variable tFontToUse as ObjcObject
MCCanvasFontGetHandle(my font, tFontToUse)
ObjC_UILabelSetFont(ObjC_UIButtonGetTitleLabel(mButtonView), tFontToUse)
/* Set the label of the button to mLabel, if not empty; otherwise to the
* name of the host. */
variable tLabelToUse as String
if mLabel is the empty string then
put my name into tLabelToUse
else
put mLabel into tLabelToUse
end if
/* We use UIControlStateNormal = 0 */
ObjC_UIButtonSetTitleForState(mButtonView, StringToNSString(tLabelToUse), 0)
end handler
/**/
end widget