Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 02270bd

Browse files
committed
[[ BrowserWidget ]] Add get / set native layer syntax to widget
1 parent 5702c9e commit 02270bd

File tree

10 files changed

+206
-45
lines changed

10 files changed

+206
-45
lines changed

engine/src/native-layer-mac.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class MCNativeLayerMac : public MCNativeLayer
3737
virtual void OnToolChanged(Tool p_new_tool);
3838
virtual void OnLayerChanged();
3939

40+
virtual bool GetNativeView(void *&r_view);
41+
4042
MCNativeLayerMac(MCWidget*, NSView *p_view);
4143
~MCNativeLayerMac();
4244

engine/src/native-layer-mac.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@
231231
return ((MCMacPlatformWindow*)(m_widget->getstack()->getwindow()))->GetHandle();
232232
}
233233

234+
bool MCNativeLayerMac::GetNativeView(void *&r_view)
235+
{
236+
r_view = m_view;
237+
return true;
238+
}
239+
234240
////////////////////////////////////////////////////////////////////////////////
235241

236242
MCNativeLayer* MCNativeLayer::CreateNativeLayer(MCWidget *p_widget, void *p_view)

engine/src/native-layer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ class MCNativeLayer
3737

3838
// Returns true if the layer would be attached if its card were visible
3939
bool isAttached() const;
40-
40+
41+
virtual bool GetNativeView(void *&r_view) = 0;
42+
4143
// Implemented by the platform-specific native layers: creates a new layer
4244
static MCNativeLayer* CreateNativeLayer(MCWidget *p_widget, void *p_native_view);
4345
protected:

engine/src/widget.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,14 @@ void MCWidget::SetDisabled(MCExecContext& ctxt, uint32_t p_part_id, bool p_flag)
845845
recompute();
846846
}
847847

848+
bool MCWidget::GetNativeView(void *&r_view)
849+
{
850+
if (m_native_layer == nil)
851+
return false;
852+
853+
return m_native_layer->GetNativeView(r_view);
854+
}
855+
848856
bool MCWidget::SetNativeView(void *p_view)
849857
{
850858
bool t_success;
@@ -986,10 +994,10 @@ MCNativeLayer* MCWidget::getNativeLayer() const
986994

987995
void MCWidget::OnOpen()
988996
{
989-
if (m_native_layer)
990-
m_native_layer->OnOpen();
991-
992997
CallHandler(MCNAME("OnOpen"), nil, 0);
998+
999+
if (m_native_layer)
1000+
m_native_layer->OnOpen();
9931001
}
9941002

9951003
void MCWidget::OnClose()
@@ -2405,15 +2413,26 @@ extern "C" MC_DLLEXPORT void MCWidgetExecClosePopup(MCValueRef p_result)
24052413

24062414
////////////////////////////////////////////////////////////////////////////////
24072415

2408-
extern "C" MC_DLLEXPORT void MCWidgetExecSetNativeView(void *p_native_view)
2416+
extern "C" MC_DLLEXPORT void MCWidgetExecGetNativeLayer(void *&r_native_layer)
2417+
{
2418+
if (MCwidgetobject == nil)
2419+
{
2420+
MCWidgetThrowNoCurrentWidgetError();
2421+
return;
2422+
}
2423+
2424+
MCwidgetobject->GetNativeView(r_native_layer);
2425+
}
2426+
2427+
extern "C" MC_DLLEXPORT void MCWidgetExecSetNativeLayer(void *p_native_layer)
24092428
{
24102429
if (MCwidgetobject == nil)
24112430
{
24122431
MCWidgetThrowNoCurrentWidgetError();
24132432
return;
24142433
}
24152434

2416-
MCwidgetobject->SetNativeView(p_native_view);
2435+
MCwidgetobject->SetNativeView(p_native_layer);
24172436
}
24182437

24192438
extern "C" MC_DLLEXPORT void MCWidgetEvalStackNativeView(void *&r_native_view)

engine/src/widget.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ class MCWidget: public MCControl
8080
virtual void toolchanged(Tool p_new_tool);
8181

8282
virtual void layerchanged();
83-
83+
84+
bool GetNativeView(void *&r_view);
8485
bool SetNativeView(void *p_view);
8586

8687
virtual void SetDisabled(MCExecContext& ctxt, uint32_t part, bool flag);

engine/src/widget.mlc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,4 +975,17 @@ begin
975975
MCWidgetExecClosePopupWithResult(Result)
976976
end syntax
977977

978+
979+
public foreign handler MCWidgetExecGetNativeLayer(out rNativeLayer as Pointer) returns nothing binds to "<builtin>"
980+
public foreign handler MCWidgetExecSetNativeLayer(in pNativeLayer as Pointer) returns nothing binds to "<builtin>"
981+
/*
982+
TODO - docs
983+
*/
984+
syntax MyNativeLayer is expression
985+
"my" "native" "layer"
986+
begin
987+
MCWidgetExecGetNativeLayer(output)
988+
MCWidgetExecSetNativeLayer(input)
989+
end syntax
990+
978991
end module

extensions/widgets/browser/browser.lcb

Lines changed: 147 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ widget com.livecode.extensions.livecode.browser
2828
--
2929

3030
-- dependancy declarations
31+
use com.livecode.foreign
3132
use com.livecode.widget
3233
use com.livecode.engine
3334
--
@@ -38,8 +39,8 @@ metadata author is "Ian Macphail"
3839
metadata version is "1.0.0"
3940
--
4041

41-
property url get getUrl set setUrl
42-
metadata url.editor is "com.livecode.pi.text"
42+
property uri get getUrl set setUrl
43+
metadata uri.editor is "com.livecode.pi.text"
4344

4445
property htmltext get getHtmlText set setHtmlText
4546
metadata htmltext.editor is "com.livecode.pi.text"
@@ -58,38 +59,63 @@ metadata javascripthandlers.editor is "com.livecode.pi.text"
5859
type MCBrowserFactoryRef is Pointer
5960
type MCBrowserRef is Pointer
6061

61-
foreign handler MCBrowserFactoryGet(in pFactoryId as NativeCString, out rFactory as MCBrowserFactoryRef) as CBool binds to "<builtin>"
62-
foreign handler MCBrowserFactoryCreateBrowser(in pFactory as MCBrowserFactoryRef, out rBrowser as MCBrowserRef) as CBool binds to "<builtin>"
62+
foreign handler MCBrowserFactoryGet(in pFactoryId as NativeCString, out rFactory as MCBrowserFactoryRef) returns CBool binds to "<builtin>"
63+
foreign handler MCBrowserFactoryCreateBrowser(in pFactory as MCBrowserFactoryRef, out rBrowser as MCBrowserRef) returns CBool binds to "<builtin>"
6364

64-
foreign handler MCBrowserRetain(in pBrowser as MCBrowserRef) as MCBrowserRef binds to "<builtin>"
65-
foreign handler MCBrowserRelease(in pBrowser as MCBrowserRef) as undefined binds to "<builtin>"
65+
foreign handler MCBrowserRetain(in pBrowser as MCBrowserRef) returns MCBrowserRef binds to "<builtin>"
66+
foreign handler MCBrowserRelease(in pBrowser as MCBrowserRef) returns nothing binds to "<builtin>"
6667

67-
foreign handler MCBrowserGetNativeLayer(in pBrowser as MCBrowserRef) as Pointer binds to "<builtin>"
68+
foreign handler MCBrowserGetNativeLayer(in pBrowser as MCBrowserRef) returns Pointer binds to "<builtin>"
6869

69-
foreign handler MCBrowserGetBoolProperty(in pBrowser as MCBrowserRef, in pProperty as CInt, out rValue as CBool) as CBool binds to "<builtin>"
70-
foreign handler MCBrowserSetBoolProperty(in pBrowser as MCBrowserRef, in pProperty as CInt, in pValue as CBool) as CBool binds to "<builtin>"
70+
foreign handler MCBrowserGetBoolProperty(in pBrowser as MCBrowserRef, in pProperty as CInt, out rValue as CBool) returns CBool binds to "<builtin>"
71+
foreign handler MCBrowserSetBoolProperty(in pBrowser as MCBrowserRef, in pProperty as CInt, in pValue as CBool) returns CBool binds to "<builtin>"
7172

72-
foreign handler MCBrowserGetStringProperty(in pBrowser as MCBrowserRef, in pProperty as CInt, out rValue as NativeCString) as CBool binds to "<builtin>"
73-
foreign handler MCBrowserSetStringProperty(in pBrowser as MCBrowserRef, in pProperty as CInt, in pValue as NativeCString) as CBool binds to "<builtin>"
73+
foreign handler MCBrowserGetStringProperty(in pBrowser as MCBrowserRef, in pProperty as CInt, out rValue as NativeCString) returns CBool binds to "<builtin>"
74+
foreign handler MCBrowserSetStringProperty(in pBrowser as MCBrowserRef, in pProperty as CInt, in pValue as NativeCString) returns CBool binds to "<builtin>"
7475

75-
foreign handler MCBrowserGoBack(in pBrowser as MCBrowserRef) as CBool binds to "<builtin>"
76-
foreign handler MCBrowserGoForward(in pBrowser as MCBrowserRef) as CBool binds to "<builtin>"
77-
foreign handler MCBrowserGoToURL(in pBrowser as MCBrowserRef) as CBool binds to "<builtin>"
78-
foreign handler MCBrowserEvaluateJavaScript(in pBrowser as MCBrowserRef, in pScript as NativeCString, out rResult as NativeCString) as CBool binds to "<builtin>"
76+
foreign handler MCBrowserGoBack(in pBrowser as MCBrowserRef) returns CBool binds to "<builtin>"
77+
foreign handler MCBrowserGoForward(in pBrowser as MCBrowserRef) returns CBool binds to "<builtin>"
78+
foreign handler MCBrowserGoToURL(in pBrowser as MCBrowserRef) returns CBool binds to "<builtin>"
79+
foreign handler MCBrowserEvaluateJavaScript(in pBrowser as MCBrowserRef, in pScript as NativeCString, out rResult as NativeCString) returns CBool binds to "<builtin>"
7980

8081
// TODO - declare functions that accept callbacks (not sure of the format here)
8182

8283
--------------------------------------------------------------------------------
8384

84-
variable mBrowser as MCBrowserRef
85-
variable mBrowserType as optional String
85+
variable mBrowser as optional MCBrowserRef
86+
variable mBrowserType as String
87+
variable mProperties as optional Array
88+
89+
--
90+
91+
variable sPropertyMap as Array
92+
variable sStringProps as List
93+
variable sBoolProps as List
8694

8795
--------------------------------------------------------------------------------
8896

97+
public handler OnLoad()
98+
put the empty array into sPropertyMap
99+
put 0 into sPropertyMap["scrollbars"]
100+
put 1 into sPropertyMap["allowNewWindows"]
101+
put 2 into sPropertyMap["enableContextMenu"]
102+
put 3 into sPropertyMap["url"]
103+
put 4 into sPropertyMap["htmlText"]
104+
put 5 into sPropertyMap["userAgent"]
105+
put 6 into sPropertyMap["javaScriptHandlers"]
106+
107+
put ["url", "htmlText", "userAgent", "javaScriptHandlers"] into sStringProps
108+
put ["scrollbars", "allowNewWindows", "enableContextMenu"] into sBoolProps
109+
end handler
110+
89111
----------
90112

91113
-- called when the widget is created
92114
public handler OnCreate()
115+
OnLoad() -- TODO - placed this here temporarily - need proper module init
116+
117+
put the empty string into mBrowserType
118+
put the empty array into mProperties
93119
end handler
94120

95121
--
@@ -101,69 +127,156 @@ public handler OnOpen()
101127

102128
variable tFactory as MCBrowserFactoryRef
103129
/* UNCHECKED */
104-
MCBrowserFactoryGet("default", tFactory)
130+
MCBrowserFactoryGet(mBrowserType, tFactory)
105131

106132
/* UNCHECKED */
107-
MCBrowserFactoryCreate(tFactory, mBrowser)
133+
MCBrowserFactoryCreateBrowser(tFactory, mBrowser)
108134

109135
variable tBrowserView as Pointer
110136
/* UNCHECKED */
111-
MCBrowserGetNativeLayer(mBrowser, tBrowserView)
137+
put MCBrowserGetNativeLayer(mBrowser) into tBrowserView
112138

113139
// TODO - add widget code to set native layer from view
114-
// set my native layer to tBrowserView
140+
-- set my native layer to tBrowserView
141+
put tBrowserView into my native layer
142+
143+
setBrowserProperties(mProperties)
115144
end handler
116145

117146
--
118147

119148
public handler OnClose()
149+
// Save browser properties
150+
put getBrowserProperties() into mProperties
151+
120152
// TODO - add widget code to set native layer from view
121-
// set my native layer to nothing
153+
set my native layer to nothing
122154

123155
MCBrowserRelease(mBrowser)
124156
put nothing into mBrowser -- TODO - check if this is equivalent to mBrowser = nil
125157
end handler
126158

127159
----------
128160

129-
public handler getUrl() as String
130-
// TODO - implement
161+
private handler getProperty(in pProperty as String) returns any
162+
if mBrowser is nothing then
163+
return mProperties[pProperty]
164+
else
165+
return getBrowserProperty(pProperty)
166+
end if
167+
end handler
168+
169+
private handler setProperty(in pProperty as String, in pValue as any)
170+
if mBrowser is nothing then
171+
put pValue into mProperties[pProperty]
172+
else
173+
setBrowserProperty(pProperty, pValue)
174+
end if
175+
end handler
176+
177+
--
178+
179+
public handler getUrl() returns String
180+
return getProperty("url")
131181
end handler
132182

133183
public handler setUrl(in pUrl as String)
134-
// TODO - implement
184+
setProperty("url", pUrl)
135185
end handler
136186

137187
--
138188

139-
public handler getHtmlText() as String
140-
// TODO - implement
189+
public handler getHtmlText() returns String
190+
return getProperty("htmlText")
141191
end handler
142192

143193
public handler setHtmlText(in pHtmlText as String)
144-
// TODO - implement
194+
setProperty("htmlText", pHtmlText)
145195
end handler
146196

147197
--
148198

149-
public handler getUserAgent() as String
150-
// TODO - implement
199+
public handler getUserAgent() returns String
200+
return getProperty("userAgent")
151201
end handler
152202

153203
public handler setUserAgent(in pUserAgent as String)
154-
// TODO - implement
204+
setProperty("userAgent", pUserAgent)
155205
end handler
156206

157207
--
158208

159-
public handler getScrollbars() as Boolean
160-
// TODO - implement
209+
public handler getJavaScriptHandlers() returns String
210+
return getProperty("javaScriptHandlers")
211+
end handler
212+
213+
public handler setJavaScriptHandlers(in pJavaScriptHandlers as String)
214+
setProperty("javaScriptHandlers", pJavaScriptHandlers)
215+
end handler
216+
217+
--
218+
219+
public handler getScrollbars() returns Boolean
220+
return getProperty("scrollbars")
161221
end handler
162222

163223
public handler setScrollbars(in pScrollbars as Boolean)
164-
// TODO - implement
224+
setProperty("scrollbars", pScrollbars)
225+
end handler
226+
227+
----------
228+
229+
private handler propertyEnum(in pProp as String) returns Integer
230+
return sPropertyMap[pProp]
165231
end handler
166232

167233
----------
168234

235+
private handler getBrowserProperty(in pProperty as String) returns any
236+
variable tValue as optional any
237+
238+
if pProperty is in sStringProps then
239+
/* UNCHECKED */
240+
MCBrowserGetStringProperty(mBrowser, propertyEnum(pProperty), tValue)
241+
else if pProperty is in sBoolProps then
242+
/* UNCHECKED */
243+
MCBrowserGetBoolProperty(mBrowser, propertyEnum(pProperty), tValue)
244+
end if
245+
246+
return tValue
247+
end handler
248+
249+
private handler setBrowserProperty(in pProperty as String, in pValue as any)
250+
if pProperty is in sStringProps then
251+
/* UNCHECKED */
252+
MCBrowserSetStringProperty(mBrowser, propertyEnum(pProperty), pValue)
253+
else if pProperty is in sBoolProps then
254+
/* UNCHECKED */
255+
MCBrowserSetBoolProperty(mBrowser, propertyEnum(pProperty), pValue)
256+
end if
257+
end handler
258+
259+
--
260+
261+
private handler setBrowserProperties(in pProperties as Array)
262+
variable tKey
263+
repeat for each key tKey in pProperties
264+
if mProperties[tKey] is not nothing then
265+
setBrowserProperty(tKey, pProperties[tKey])
266+
end if
267+
end repeat
268+
end handler
269+
270+
private handler getBrowserProperties() returns Array
271+
variable tProps
272+
put the empty array into tProps
273+
274+
variable tKey
275+
repeat for each key tKey in sPropertyMap
276+
put getBrowserProperty(tKey) into tProps[tKey]
277+
end repeat
278+
279+
return tProps
280+
end handler
281+
169282
end widget

0 commit comments

Comments
 (0)