class Iup::Canvas

A canvas is a working area for the application. The API documentation can only hint at the use of this control. For more information see Tecgraf’s documentation on the Canvas and on the CD graphics library, from which many of the drawing commands and primitives are taken, documented under CD drawing.

Example

In the following example, notice how the canvas is placed in a dialog, which is mapped, and then init is called on the canvas before the dialog is shown.

class MyCanvas < Iup::Canvas
  def initialize
    super()

    self.scrollbar = 'yes' # explicit receivers are needed in subclass
    self.xmax = 599
    self.ymax = 399

    self.scroll_cb = ->(op, posx, posy){ # redraw on scroll
      redraw(posx, posy)
      Iup::DEFAULT
    }

    self.resize_cb = ->(w, h){ # redraw on resize
      self.dx = w
      self.dy = h
      activate # redraws the canvas
      Iup::DEFAULT
    }

    self.action = ->(posx, posy){ # called to redraw canvas
      redraw(posx, posy)
    }
  end

  def redraw(posx, posy) # the redraw logic, uses some CD commands
    iposx = posx.to_i
    iposy = posy.to_i
    # invert scroll reference (YMAX-DY - POSY)
    iposy = 399-dy - iposy

    clear
    self.foreground = Iup::CD_RED
    line(0-iposx, 0-iposy, 599-iposx, 399-iposy)
    line(0-iposx, 399-iposy, 599-iposx, 0-iposy)

    Iup::DEFAULT
  end 
end

Iup.mainloop do 
  canvas = MyCanvas.new do |c| # 1. make the canvas
    c.rastersize = '300x200'
  end

  dialog = Iup::Dialog.new(canvas) do |d| # 2. put it in a dialog
    d.title = 'Scrollbar Test'
  end.map # 3. map the dialog

  canvas.init # 4. init the canvas
  canvas.rastersize = nil # release the minimum limitation

  dialog.show # 5. show the dialog
end

Also see BackgroundBox.