Class: Gtk2SVG::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/gtk2svg.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(svg, irb: false, title: 'Window', debug: @debug) ⇒ Main

Returns a new instance of Main.



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/gtk2svg.rb', line 304

def initialize(svg, irb: false, title: 'Window', debug: @debug)
  
  @svg, @debug = svg, debug
  @doc = Svgle.new(svg, callback: self, debug: debug)

  @area = area = Gtk::DrawingArea.new
  
  doc = @doc
  
  def @doc.element_by_id(id)
    self.root.element("//*[@id='#{id}']")
  end            
  

  

  client_code = []
  
  puts ('title: ' + title.inspect).debug if @debug
  window = Gtk::Window.new title
  @width, @height = %i(width height).map{|x| @doc.root.attributes[x].to_i }
  
  if @width and @height then
    window.set_default_size(@width, @height)
  end
  
  @dirty = true
  
  @doc.root.xpath('//script').each {|x| eval x.text.unescape }
  
  area.signal_connect("expose_event") do      

    if @dirty then
      
      Thread.new { @doc.root.xpath('//script').each {|x| eval x.text.unescape } }

      @instructions = Gtk2SVG::Render.new(@doc, debug: debug).to_a
    end
    
    drawing = DrawingInstructions.new area, debug: debug
    drawing.render @instructions
    @dirty = false
    
  end
  
  area.add_events(Gdk::Event::POINTER_MOTION_MASK) 

  area.signal_connect('motion_notify_event') do |item,  event|

    @doc.root.xpath('//*[@onmousemove]').each do |x|
                
      eval x.onmousemove() if x.hotspot? event.x, event.y
      
    end
  end

  area.add_events(Gdk::Event::BUTTON_PRESS_MASK) 

  area.signal_connect "button_press_event" do |item,event| 

    @doc.root.xpath('//*[@onmousedown]').each do |x|
                
      eval x.onmousedown() if x.hotspot? event.x, event.y
      
    end        
  end      
  
  window.add(area).show_all


  @doc.root.xpath('//*[@onload]').each do |x|
              
    eval x.onload()
    
  end
  
  #Thread.new do
  #  3.times { x1 -= 1; @area.queue_draw; sleep 0.1}
  #end

  window.show_all.signal_connect("destroy"){Gtk.main_quit}

  irb ? Thread.new {Gtk.main  } : Gtk.main
end

Instance Attribute Details

#docObject

Returns the value of attribute doc.



301
302
303
# File 'lib/gtk2svg.rb', line 301

def doc
  @doc
end

#heightObject (readonly)

Returns the value of attribute height.



302
303
304
# File 'lib/gtk2svg.rb', line 302

def height
  @height
end

#svgObject

Returns the value of attribute svg.



301
302
303
# File 'lib/gtk2svg.rb', line 301

def svg
  @svg
end

#widthObject (readonly)

Returns the value of attribute width.



302
303
304
# File 'lib/gtk2svg.rb', line 302

def width
  @width
end

Class Method Details

.element_by_id(id) ⇒ Object



313
314
315
# File 'lib/gtk2svg.rb', line 313

def @doc.element_by_id(id)
  self.root.element("//*[@id='#{id}']")
end

Instance Method Details

#onmousemove(x, y) ⇒ Object



389
390
391
# File 'lib/gtk2svg.rb', line 389

def onmousemove(x,y)
  
end

#timeout(duration, loopx = true) ⇒ Object



398
399
400
401
402
403
404
405
406
# File 'lib/gtk2svg.rb', line 398

def timeout(duration, loopx=true)
  
  GLib::Timeout.add(duration) do 
    yield
    @area.queue_draw
    loopx
  end

end