fix #7440 - GTK4 version is now master
[roobuilder] / src / Builder4 / DialogPluginWebkit.vala
1 static Xcls_DialogPluginWebkit  _DialogPluginWebkit;
2
3 public class Xcls_DialogPluginWebkit : Object
4 {
5     public Gtk.Dialog el;
6     private Xcls_DialogPluginWebkit  _this;
7
8     public static Xcls_DialogPluginWebkit singleton()
9     {
10         if (_DialogPluginWebkit == null) {
11             _DialogPluginWebkit= new Xcls_DialogPluginWebkit();
12         }
13         return _DialogPluginWebkit;
14     }
15     public Xcls_webview webview;
16
17         // my vars (def)
18     public signal void complete (string result);
19     public string cls;
20     public string tmpjs;
21     public string result_json;
22
23     // ctor
24     public Xcls_DialogPluginWebkit()
25     {
26         _this = this;
27         this.el = new Gtk.Dialog();
28
29         // my vars (dec)
30
31         // set gobject values
32         this.el.title = "Add / Edit Component";
33         this.el.default_height = 500;
34         this.el.default_width = 750;
35         this.el.deletable = true;
36         this.el.modal = true;
37         var child_0 = new Xcls_Box2( _this );
38         child_0.ref();
39         this.el.get_content_area().append (  child_0.el  );
40         var child_1 = new Xcls_Button5( _this );
41         child_1.ref();
42         this.el.add_action_widget (  child_1.el , 3 );
43         var child_2 = new Xcls_Button6( _this );
44         child_2.ref();
45         this.el.add_action_widget (  child_2.el , 0 );
46         var child_3 = new Xcls_Button7( _this );
47         child_3.ref();
48         this.el.add_action_widget (  child_3.el , 1 );
49
50         //listeners
51         this.el.response.connect( (response_id) => {
52         
53                          
54                              if (response_id == 1) { // OK...
55                                  var loop = new MainLoop();
56                                  // run toBJS to get the data... (calls back into alert handler)
57                                     _this.result_json = "";
58                                      this.webview.el.run_javascript.begin("Editor." + this.cls + ".panel.toBJS();", null, (obj, res) => {
59                                          try {
60                                             this.webview.el.run_javascript.end(res);
61                                         } catch(Error e) {
62                                     
63                                          }
64                                          loop.quit();
65                                      });
66                                      loop.run();
67                                      _this.complete(_this.result_json);
68                                      
69                                  
70                         //           print("LOOP END?");
71                                  // try and get the resopse...
72                                 break;
73                              }
74                             if (response_id < 1) {
75                                 this.el.hide();
76                                  _this.complete("");
77                             }
78                             // keep showing...?
79                         });
80     }
81
82     // user defined functions
83     public bool has_plugin (string cls) {
84     
85          return GLib.FileUtils.test(
86                 BuilderApplication.configDirectory() + "/resources/Editors/Editor." + cls + ".js",
87                 GLib.FileTest.IS_REGULAR
88           );
89         
90     
91     
92     }
93     public void showIt // for result hook into complete
94      
95      (Gtk.Window ?parent, Project.Project project, string cls, string tbl) {// JsRender.Node node) {
96      
97         this.cls = cls;
98      
99         if (parent  != null) {
100             this.el.set_transient_for(parent);
101             this.el.modal = true;
102         }
103         this.result_json = "";
104          var  db = project.roo_database;
105           
106          this.el.show();
107           
108      
109         
110             var runhtml = "<script type=\"text/javascript\">\n" ;
111             string builderhtml;
112             
113             try {
114                 GLib.FileUtils.get_contents(BuilderApplication.configDirectory() + "/resources/roo.builder.js", out builderhtml);
115             } catch (Error e) {
116                 builderhtml = "";
117             }
118             
119     
120             runhtml += builderhtml + "\n";
121             
122             
123             runhtml += "\n" +
124                 "Builder.saveHTML = function() {};\n" + 
125             "Roo.onReady(function() {\n" +
126     
127             "Roo.XComponent.build();\n" +
128             "});\n";
129         
130         
131             
132     
133             var ar = db.readForeignKeys(tbl);
134             var  generator = new Json.Generator ();
135             var  root = new Json.Node(Json.NodeType.OBJECT);
136             root.init_object(ar);
137             generator.set_root (root);
138             
139             generator.pretty = true;
140             generator.indent = 4;
141             
142             runhtml += "\n" +
143             " Roo.XComponent.on('buildcomplete', function() {\n" +
144              "    Editor." + cls + ".panel.loadData(" + generator.to_data (null) + "); " +
145             "});\n";
146     
147             
148         
149         
150     
151             runhtml += "</script>\n" ;
152     
153             print(runhtml);
154             // fix to make sure they are the same..
155             
156             // need to modify paths
157     
158             string inhtml;
159             try {
160                 GLib.FileUtils.get_contents(
161                     BuilderApplication.configDirectory() + "/resources/roo.builder.html"
162                         , out inhtml);
163             
164             } catch (Error e) {
165                 inhtml = "";
166             }
167             // fetch the json from the database...
168             
169             //print(runhtml);
170             
171             var html = inhtml.replace("</head>", runhtml + // + this.runhtml + 
172                 "<script type=\"text/javascript\" src=\"resources://localhost/Editors/Editor." + cls + ".js\"></script>" + 
173           
174                             
175             "</head>");
176             //print("LOAD HTML " + html);
177             
178              //var rootURL = _this.file.project.rootURL;
179        
180             
181             
182             this.webview.el.load_html( html , 
183                 //fixme - should be a config option!
184                 "xhttp://localhost/roobuilder/"
185             );
186         
187            
188         
189         
190         
191     }
192     public class Xcls_Box2 : Object
193     {
194         public Gtk.Box el;
195         private Xcls_DialogPluginWebkit  _this;
196
197
198             // my vars (def)
199
200         // ctor
201         public Xcls_Box2(Xcls_DialogPluginWebkit _owner )
202         {
203             _this = _owner;
204             this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
205
206             // my vars (dec)
207
208             // set gobject values
209             this.el.homogeneous = false;
210             var child_0 = new Xcls_ScrolledWindow3( _this );
211             child_0.ref();
212             this.el.append (  child_0.el  );
213         }
214
215         // user defined functions
216     }
217     public class Xcls_ScrolledWindow3 : Object
218     {
219         public Gtk.ScrolledWindow el;
220         private Xcls_DialogPluginWebkit  _this;
221
222
223             // my vars (def)
224
225         // ctor
226         public Xcls_ScrolledWindow3(Xcls_DialogPluginWebkit _owner )
227         {
228             _this = _owner;
229             this.el = new Gtk.ScrolledWindow();
230
231             // my vars (dec)
232
233             // set gobject values
234             this.el.hexpand = true;
235             this.el.vexpand = true;
236             var child_0 = new Xcls_webview( _this );
237             child_0.ref();
238             this.el.set_child (  child_0.el  );
239
240             // init method
241
242             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
243         }
244
245         // user defined functions
246     }
247     public class Xcls_webview : Object
248     {
249         public WebKit.WebView el;
250         private Xcls_DialogPluginWebkit  _this;
251
252
253             // my vars (def)
254
255         // ctor
256         public Xcls_webview(Xcls_DialogPluginWebkit _owner )
257         {
258             _this = _owner;
259             _this.webview = this;
260             this.el = new WebKit.WebView();
261
262             // my vars (dec)
263
264             // set gobject values
265
266             // init method
267
268             {
269                 // this may not work!?
270                 var settings =  this.el.get_settings();
271                 settings.enable_write_console_messages_to_stdout = true;
272                  
273                 var fs= new FakeServer(this.el);
274                 fs.ref();
275                 // this was an attempt to change the url perms.. did not work..
276                 // settings.enable_file_access_from_file_uris = true;
277                 // settings.enable_offline_web_application_cache - true;
278                 // settings.enable_universal_access_from_file_uris = true;
279                
280                  
281                 
282                 
283                 
284             
285                  // FIXME - base url of script..
286                  // we need it so some of the database features work.
287                 this.el.load_html( "Render not ready" , 
288                         //fixme - should be a config option!
289                         // or should we catch stuff and fix it up..
290                         "xhttp://localhost/roobuilder/"
291                 );
292                     
293                     
294                 
295                 
296             }
297
298             //listeners
299             this.el.script_dialog.connect( (dialog) => {
300                 if (this.el == null) {
301                     return true;
302                 }
303                 
304                  var msg = dialog.get_message();
305                  if (msg.length < 4) {
306                     return false;
307                  }
308                  if (msg.substring(0,4) != "IPC:") {
309                      return false;
310                  }
311                  var ar = msg.split(":", 3);
312                 if (ar.length < 3) {
313                     return false;
314                 }
315                 print("CMD: %s\n",ar[1]);
316                 print("ARGS: %s\n",ar[2]);
317                 switch(ar[1]) {
318                 
319                     case "SAVEHTML":
320                         // print("%sw",ar[2]);
321                         //  _this.file.saveHTML(ar[2]);
322                         return true;
323                         
324                     case "OUT":
325                         _this.result_json = ar[2];
326                         return true;
327                         
328                     default:
329                         return true;
330                 }
331                 
332             });
333         }
334
335         // user defined functions
336     }
337
338
339
340     public class Xcls_Button5 : Object
341     {
342         public Gtk.Button el;
343         private Xcls_DialogPluginWebkit  _this;
344
345
346             // my vars (def)
347
348         // ctor
349         public Xcls_Button5(Xcls_DialogPluginWebkit _owner )
350         {
351             _this = _owner;
352             this.el = new Gtk.Button();
353
354             // my vars (dec)
355
356             // set gobject values
357             this.el.label = "Reload";
358         }
359
360         // user defined functions
361     }
362
363     public class Xcls_Button6 : Object
364     {
365         public Gtk.Button el;
366         private Xcls_DialogPluginWebkit  _this;
367
368
369             // my vars (def)
370
371         // ctor
372         public Xcls_Button6(Xcls_DialogPluginWebkit _owner )
373         {
374             _this = _owner;
375             this.el = new Gtk.Button();
376
377             // my vars (dec)
378
379             // set gobject values
380             this.el.label = "Cancel";
381         }
382
383         // user defined functions
384     }
385
386     public class Xcls_Button7 : Object
387     {
388         public Gtk.Button el;
389         private Xcls_DialogPluginWebkit  _this;
390
391
392             // my vars (def)
393
394         // ctor
395         public Xcls_Button7(Xcls_DialogPluginWebkit _owner )
396         {
397             _this = _owner;
398             this.el = new Gtk.Button();
399
400             // my vars (dec)
401
402             // set gobject values
403             this.el.label = "OK";
404         }
405
406         // user defined functions
407     }
408
409 }