1 module awebview.wrapper.webviewlistener;
2 
3 import awebview.gui.application,
4        awebview.gui.activity;
5 
6 import awebview.wrapper.cpp;
7 import awebview.wrapper.webview : WebView;
8 import awebview.wrapper.weburl : WebURL;
9 import awebview.wrapper.weakref : weakRef;
10 import awebview.wrapper.webstring : WebStringCpp;
11 
12 
13 static struct WebViewListener
14 {
15     static
16     Activity findActivity(Application application, Awesomium.WebView wv)
17     {
18         Activity activity;
19         application.opApplyActivities((Activity a){
20             if(a.view.cppObj == wv){
21                 activity = a;
22                 return 1;
23             }
24 
25             return 0;
26         });
27 
28         return activity;
29     }
30 
31 
32     static class Menu : Awesomium4D.IMenuListenerD
33     {
34         this(Application app)
35         {
36             _app = app;
37 
38             auto mid = MemoryManager.instance.register(cast(void*)this);
39             _cppObj = MenuListenerD2CppMember.newCtor(this, mid);
40         }
41 
42 
43         final
44         inout(CppObj) cppObj() inout pure nothrow @safe @nogc
45         {
46             return _cppObj;
47         }
48 
49 
50         void onShowPopupMenu(Activity activity, const Awesomium.WebPopupMenuInfo*){}
51         void onShowContextMenu(Activity activity, const Awesomium.WebContextMenuInfo*) {}
52 
53         extern(C++)
54         {
55             final
56             void onShowPopupMenu(Awesomium.WebView wv, const Awesomium.WebPopupMenuInfo* p)
57             {
58                 Activity activity = findActivity(_app, wv);
59                 assert(activity);
60 
61                 onShowPopupMenu(activity, p);
62             }
63 
64             final
65             void onShowContextMenu(Awesomium.WebView wv, const Awesomium.WebContextMenuInfo* p)
66             {
67                 Activity activity = findActivity(_app, wv);
68                 assert(activity);
69 
70                 onShowContextMenu(activity, p);
71             }
72         }
73 
74       private:
75         alias CppObj = Awesomium4D.MenuListenerD2Cpp;
76 
77         Application _app;
78         CppObj _cppObj;
79     }
80 
81 
82     static class View : Awesomium4D.IViewListenerD
83     {
84         this(Application app)
85         {
86             _app = app;
87 
88             auto mid = MemoryManager.instance.register(cast(void*)this);
89             _cppObj = ViewListenerD2CppMember.newCtor(this, mid);
90         }
91 
92 
93         final
94         inout(CppObj) cppObj() inout pure nothrow @safe @nogc { return _cppObj; }
95 
96 
97         void onChangeTitle(WebView view, Activity activity, scope const(wchar)[] title) {}
98         void onChangeAddressBar(WebView view, Activity activity, WebURL url) {}
99         void onChangeTooltip(WebView view, Activity activity, scope const(wchar)[] s) {}
100         void onChangeTargetURL(WebView view, Activity activity, WebURL url) {}
101         void onChangeCursor(WebView view, Activity activity, Awesomium.Cursor cursor) {}
102         void onChangeFocus(WebView view, Activity activity, Awesomium.FocusedElementType fet) {}
103         void onAddConsoleMessage(WebView view, Activity activity, scope const(wchar)[] msg, uint lineNum, scope const(wchar)[] src) {}
104         void onShowCreatedWebView(WebView view, Activity activity, WebView newView, WebURL openerURL, WebURL targetURL, Rect rect, bool isPopup) {}
105 
106       extern(C++)
107       {
108         void onChangeTitle(Awesomium.WebView wv, const Awesomium.WebString title)
109         {
110             auto activity = findActivity(_app, wv);
111             auto ws = title.weakRef!WebStringCpp;
112 
113             onChangeTitle(WebView(wv), activity, ws.data);
114         }
115 
116 
117         void onChangeAddressBar(Awesomium.WebView wv, const Awesomium.WebURL url)
118         {
119             auto activity = findActivity(_app, wv);
120             WebURL wu = url;
121 
122             onChangeAddressBar(WebView(wv), activity, wu);
123         }
124 
125 
126         void onChangeTooltip(Awesomium.WebView wv, const Awesomium.WebString tip)
127         {
128             auto activity = findActivity(_app, wv);
129             auto ws = tip.weakRef!WebStringCpp;
130 
131             onChangeTooltip(WebView(wv), activity, ws.data);
132         }
133 
134 
135         void onChangeTargetURL(Awesomium.WebView wv, const Awesomium.WebURL url)
136         {
137             auto activity = findActivity(_app, wv);
138             WebURL wu = url;
139 
140             onChangeTargetURL(WebView(wv), activity, wu);
141         }
142 
143 
144         void onChangeCursor(Awesomium.WebView wv, Awesomium.Cursor c)
145         {
146             auto activity = findActivity(_app, wv);
147 
148             onChangeCursor(WebView(wv), activity, c);
149         }
150 
151 
152         void onChangeFocus(Awesomium.WebView wv, Awesomium.FocusedElementType fet)
153         {
154             auto activity = findActivity(_app, wv);
155 
156             onChangeFocus(WebView(wv), activity, fet);
157         }
158 
159 
160         void onAddConsoleMessage(Awesomium.WebView wv, const Awesomium.WebString msg, int lineNum, const Awesomium.WebString src)
161         {
162             auto activity = findActivity(_app, wv);
163             auto m = msg.weakRef!WebStringCpp;
164             auto s = src.weakRef!WebStringCpp;
165 
166             onAddConsoleMessage(WebView(wv), activity, m.data, lineNum, s.data);
167         }
168 
169 
170         void onShowCreatedWebView(Awesomium.WebView wv, Awesomium.WebView newView, const Awesomium.WebURL openerURL, const Awesomium.WebURL targetURL, const(Awesomium.Rect)* initialPos, bool isPopup)
171         {
172             auto activity = findActivity(_app, wv);
173             WebURL op = openerURL;
174             WebURL tg = targetURL;
175             Rect pos = *initialPos;
176 
177             onShowCreatedWebView(WebView(wv), activity, WebView(newView), op, tg, pos, isPopup);
178         }
179       }
180 
181       private:
182         alias CppObj = Awesomium4D.ViewListenerD2Cpp;
183 
184         Application _app;
185         CppObj _cppObj;
186     }
187 }