PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/src/ui/EventTools.hx

http://github.com/blackdog66/bdog-gtk
Haxe | 84 lines | 54 code | 8 blank | 22 comment | 6 complexity | 582fb219ea324cb74884c8fb9ff3763c MD5 | raw file
 1package ui;
 2
 3import js.Node;
 4import gtk.Model;
 5import gtk.Server;
 6import gtk.Gtk;
 7import gtk.Glade;
 8
 9class EventTools {
10  static var pending = new IntHash<{w:Widget,e:Event,fn:CallBack}>();
11  static var widgetEvents = new Hash<Int>();
12
13  public static function
14  poll(win) {
15    Gtk.widgetShowAll(win);
16    Node.setInterval(function() {
17        Gtk.serverCallback("UPDATE",function(event) {
18            try {
19              var 
20                id = Std.parseInt(event),
21                obj = pending.get(id);
22              if (obj != null) {
23                try {
24                  obj.fn(obj.w,obj.e);
25                } catch(exc:Dynamic) {
26                  trace("Err with "+obj);
27                }
28              }
29            } catch (ex:Dynamic) { trace("poll:"+ex);
30            }
31              trace("got an event"+event);
32            });
33      },200,[]);
34  }
35  
36  
37  public static function
38  event(w:Widget,e:Event,?f:CallBack) {
39    var
40      id = Std.int(Math.random()*1000000),
41      sig = Type.enumConstructor(e);
42    if (f != null)
43      pending.set(id,{w:w,e:e,fn:f});
44
45    widgetEvents.set(w+sig,id);
46  }
47
48  public static function
49  removeEvent(w:Widget,e:Event) {
50    var
51      l = w + Type.enumConstructor(e),
52      id = widgetEvents.get(l);
53    if (id != null) {
54      Gtk.serverDisconnect(function(s) {
55          trace(s);
56        });
57      widgetEvents.remove(l);
58      pending.remove(id);
59    }
60  }
61
62  /*
63  public static function
64  poll(win) {    
65    Gtk.widgetShowAll(win);
66    do {
67      Gtk.serverCallback("WAIT",function(event) {
68          try {
69            var 
70              id = Std.parseInt(event),
71              obj = pending.get(id);
72            if (obj != null) {
73              try {
74                obj.fn(obj.w,obj.e);
75              } catch(exc:Dynamic) {
76                trace("Err with "+obj);
77              }
78            }
79          } catch (ex:Dynamic) { trace("poll:"+ex);}
80        });
81    } while (true);
82  }
83  */
84}