class Iup::Canvas

A Canvas is a working area for the application.

Many methods working on the Canvas are drawn from the CD library: webserver2.tecgraf.puc-rio.br/cd/

In this example, notice how the dialog is mapped, the canvas initialised, and finally the dialog is shown.

mainloop do
  cnv = Canvas.new do
    rastersize '300x200'
    action ->(x, y){ # called on redraw: (x,y) refer to scrollbar positions, if used
      clear
      foreground CD_BLUE
      box 10, 100, 10, 100
      foreground CD_RED
      rectangle 10, 100, 10, 100
      text 200, 180, 'hello from Ruby'
      DEFAULT
    }
  end

  dlg = Dialog.new VBox.new(cnv){ margin '10x10' } do
    title 'IupCanvas + Canvas Draw'
    size '350x220'
  end.map

  cnv.init # this has to be done after mapping the dialog

  dlg.show
end

Attributes

canfocus

Enables the control to gain focus. Values 'yes' / 'no'.

cursor

Defines the mouse shape / cursor for the canvas.

drawsize

Size of the drawing area in pixels, as “widthxheight”.

expand

Allows canvas to fill available space in indicated direction. Values 'no' / 'horizontal' / 'vertical' / 'yes'.

padding

Margin in x and y directions, value as “mxn”.

position

read-only returns position in pixels within client window as “x,y”.

rastersize

Size of the canvas, in pixels, value as “widthxheight”.

screenposition

read-only returns position in pixels on screen

scrollbar

Selects 'no' / 'horizontal' / 'vertical' / 'yes' (for both) scrollbars.

tip

Tooltip string.

Public Class Methods

new(handle = IupLib.IupCanvas(''), &block) click to toggle source

Creates an instance of the canvas.

handle

an optional Canvas handle.

block

optional block to set up Canvas attributes.

# File lib/wrapped/canvas.rb, line 58
def initialize handle = IupLib.IupCanvas(''), &block
  @handle = handle

  # run any provided block on instance, to set up further attributes
  self.instance_eval &block if block_given?
end

Public Instance Methods

action(callback) click to toggle source

Action generated when the canvas needs to be redrawn. Action takes a callback which accepts 2 arguments (posx, posy). posx, posy are the position of the horizontal and vertical thumbs of the scrollbar.

# File lib/wrapped/canvas.rb, line 380
def action callback
  unless callback.arity == 2
    raise ArgumentError, 'action must take 2 arguments: (posx, posy)'
  end
  cb = Proc.new do |ih, posx, posy|
    callback.call posx, posy
  end
  define_callback cb, 'ACTION', :ff_i
end
activate() click to toggle source

call to activate canvas. (Not usually needed - depends on driver.)

# File lib/wrapped/canvas.rb, line 95
def activate
  CdLib.cdCanvasActivate @canvas
end
arc(xc, yc, w, h, angle1, angle2) click to toggle source

Draws an arc of an ellipse - a pie-shaped slice. Angles are in degrees, counterclockwise.

# File lib/wrapped/canvas.rb, line 229
def arc xc, yc, w, h, angle1, angle2
  CdLib.cdCanvasArc @canvas, xc, yc, w, h, angle1, angle2
end
background(colour=CD_QUERY) click to toggle source

Sets the background colour, if a value is given, else returns current value.

Colour may be one of:

  • string, in form “RR GG BB”

  • colour constant, e.g. CD_BLACK, as listed in Iup

# File lib/wrapped/canvas.rb, line 142
def background colour=CD_QUERY
  case colour
  when String # assume in form 'RR GG BB'
    r, g, b = colour.split(' ').collect &:to_i
    CdLib.cdCanvasBackground @canvas, CdLib.cdEncodeColor(r, g, b)
  else
    CdLib.cdCanvasBackground @canvas, colour
  end
end
backopacity(opacity=CD_QUERY) click to toggle source

Sets the opacity, if a value is given, else returns current value. See Iup for background opacity mode values.

# File lib/wrapped/canvas.rb, line 279
def backopacity opacity=CD_QUERY
  CdLib.cdCanvasBackOpacity @canvas, opacity
end
begin_block(mode) click to toggle source

Begin the definition of a sequence of vertices. Valid polygon mode values are listed in Iup.

# File lib/wrapped/canvas.rb, line 171
def begin_block mode
  CdLib.cdCanvasBegin @canvas, mode
end
box(xmin, xmax, ymin, ymax) click to toggle source

Fills a rectangle, according to current interior style.

# File lib/wrapped/canvas.rb, line 259
def box xmin, xmax, ymin, ymax
  CdLib.cdCanvasBox @canvas, xmin, xmax, ymin, ymax
end
chord(xc, yc, w, h, angle1, angle2) click to toggle source

Fills a chord, according to current interior style. A chord is an arc of an ellipse - with the arc points connected. Angles are in degrees, counterclockwise.

# File lib/wrapped/canvas.rb, line 273
def chord xc, yc, w, h, angle1, angle2
  CdLib.cdCanvasChord @canvas, xc, yc, w, h, angle1, angle2
end
clear() click to toggle source

Clears the canvas.

# File lib/wrapped/canvas.rb, line 162
def clear
  CdLib.cdCanvasClear @canvas
end
deactivate() click to toggle source

call to deactivate canvas. (Not usually needed - depends on driver.)

# File lib/wrapped/canvas.rb, line 101
def deactivate
  CdLib.cdCanvasDeactivate @canvas
end
end_block() click to toggle source

End the definition of a sequence of vertices.

# File lib/wrapped/canvas.rb, line 181
def end_block 
  CdLib.cdCanvasEnd @canvas
end
fillmode(mode=CD_QUERY) click to toggle source

Sets the fill mode, if a value is given, else returns current value. See Iup for fill mode values.

# File lib/wrapped/canvas.rb, line 285
def fillmode mode=CD_QUERY
  CdLib.cdCanvasFillMode @canvas, mode
end
focus_cb(callback) click to toggle source

Called when the canvas gets or loses the keyboard focus. Callback takes a single parameter: (focus)

focus

non-zero if canvas gaining focus, else zero.

# File lib/wrapped/canvas.rb, line 395
def focus_cb callback
  unless callback.arity == 1
    raise ArgumentError, 'focus_cb callback must take 1 argument, the focus'
  end
  cb = Proc.new do |ih, focus|
    callback.call focus
  end
  define_callback cb, 'FOCUS_CB', :i_i
end
font(typeface, style, size) click to toggle source

Sets text font.

typeface

'Courier', 'Helvetica', 'Times' built-in, but any known font can be used

style

see Iup for list of text styles.

size

the font height, default is 12.

# File lib/wrapped/canvas.rb, line 318
def font typeface, style, size
  CdLib.cdCanvasFont @canvas, typeface, style, size
end
foreground(colour=CD_QUERY) click to toggle source

Sets the foreground colour, if a value is given, else returns current value.

Colour may be one of:

  • string, in form “RR GG BB”

  • colour constant, e.g. CD_BLACK, as listed in Iup

# File lib/wrapped/canvas.rb, line 127
def foreground colour=CD_QUERY
  case colour
  when String # assume in form 'RR GG BB'
    r, g, b = colour.split(' ').collect &:to_i
    CdLib.cdCanvasForeground @canvas, CdLib.cdEncodeColor(r, g, b)
  else
    CdLib.cdCanvasForeground @canvas, colour
  end
end
hatch(style=CD_QUERY) click to toggle source

Sets the hatch style, if a value is given, else returns current value. See Iup for hatch style values.

# File lib/wrapped/canvas.rb, line 297
def hatch style=CD_QUERY
  CdLib.cdCanvasHatch @canvas, style
end
init() click to toggle source

This must be called after the widget is mapped, to create and setup the canvas.

# File lib/wrapped/canvas.rb, line 67
def init
  @canvas = CdLib.cdCreateCanvas CdLib.cdContextIup, @handle
end
interiorstyle(style=CD_QUERY) click to toggle source

Sets the interior style, if a value is given, else returns current value. See Iup for interior style values.

# File lib/wrapped/canvas.rb, line 291
def interiorstyle style=CD_QUERY
  CdLib.cdCanvasInteriorStyle @canvas, style
end
keypress_cb(callback) click to toggle source

Action generated when a key is pressed or released. keypress_cb takes a 2-argument callback: (character, pressed). pressed == 1 if key is pressed, pressed == 0 if key is released.

# File lib/wrapped/canvas.rb, line 409
def keypress_cb callback
  unless callback.arity == 2
    raise ArgumentError, 'keypress_cb callback must take 2 arguments: (char, press)'
  end
  cb = Proc.new do |ih, char, press|
    callback.call char, press
  end
  define_callback cb, 'KEYPRESS_CB', :ii_i
end
kill() click to toggle source

call when canvas no longer needed.

# File lib/wrapped/canvas.rb, line 89
def kill
  CdLib.cdKillCanvas @canvas
end
line(x1, y1, x2, y2) click to toggle source

Draws a line from (x1, y1) to (x2, y2).

# File lib/wrapped/canvas.rb, line 218
def line x1, y1, x2, y2
  CdLib.cdCanvasLine @canvas, x1, y1, x2, y2
end
linecap(style=CD_QUERY) click to toggle source

Sets linecap, if a value is given, else returns the current value. See Iup for available linecap values.

# File lib/wrapped/canvas.rb, line 252
def linecap style=CD_QUERY
  CdLib.cdCanvasLineCap @canvas, style
end
linejoin(style=CD_QUERY) click to toggle source

Sets linejoin, if a value is given, else returns the current value. See Iup for available linejoin values.

# File lib/wrapped/canvas.rb, line 246
def linejoin style=CD_QUERY
  CdLib.cdCanvasLineJoin @canvas, style
end
linestyle(style=CD_QUERY) click to toggle source

Sets linestyle, if a value is given, else returns the current style. See Iup for available linestyle values.

# File lib/wrapped/canvas.rb, line 235
def linestyle style=CD_QUERY
  CdLib.cdCanvasLineStyle @canvas, style
end
linewidth(width=CD_QUERY) click to toggle source

Sets linewidth, if a pixel value is given, else returns the current width.

# File lib/wrapped/canvas.rb, line 240
def linewidth width=CD_QUERY
  CdLib.cdCanvasLineWidth @canvas, width
end
mark(x, y) click to toggle source

Draws a mark at position (x, y).

# File lib/wrapped/canvas.rb, line 199
def mark x, y
  CdLib.cdCanvasMark @canvas, x, y
end
marksize(size=CD_QUERY) click to toggle source

Sets the marksize, if a pixel value is given, else, returns the current value.

# File lib/wrapped/canvas.rb, line 211
def marksize size=CD_QUERY
  CdLib.cdCanvasMarkSize size
end
marktype(type=CD_QUERY) click to toggle source

Sets the marktype, if a value is given, else returns the current value. See Iup for available marktype values.

# File lib/wrapped/canvas.rb, line 205
def marktype type=CD_QUERY
  CdLib.cdCanvasMarkType @canvas, type
end
motion_cb(callback) click to toggle source

Action generated when the mouse is moved. Callback takes 3 arguments: (x, y, state)

x

x position of mouse

y

y position of mouse

state

status of mouse buttons and certain keyboard keys at the moment the event was generated.

# File lib/wrapped/canvas.rb, line 427
def motion_cb callback
  unless callback.arity == 3
    raise ArgumentError, 'motion_cb callback must take 3 arguments: (x, y, state)'
  end
  cb = Proc.new do |ih, x, y, state|
    callback.call x, y, state
  end
  define_callback cb, 'MOTION_CB', :iis_i
end
nativefont(font) click to toggle source

Sets the current font.

# File lib/wrapped/canvas.rb, line 323
def nativefont font
  CdLib.cdCanvasNativefont @canvas, font
end
pathset(action) click to toggle source

Specify action for next point on path. Valid path actions are listed in Iup.

# File lib/wrapped/canvas.rb, line 187
def pathset action
  CdLib.cdCanvasPathSet @canvas, action
end
pixel(x, y, colour) click to toggle source

Configures pixel (x, y) with colour.

# File lib/wrapped/canvas.rb, line 194
def pixel x, y, colour
  CdLib.cdCanvasPixel @canvas, x, y, colour
end
rectangle(xmin, xmax, ymin, ymax) click to toggle source

Draws a rectangle bounding (xmin, ymin) to (xmax, ymax).

# File lib/wrapped/canvas.rb, line 223
def rectangle xmin, xmax, ymin, ymax
  CdLib.cdCanvasRect @canvas, xmin, xmax, ymin, ymax
end
redraw() click to toggle source

redraws the underlying canvas.

# File lib/wrapped/canvas.rb, line 116
def redraw
  IupLib.IupRedraw @handle
end
resize_cb(callback) click to toggle source

Action generated when the canvas size is changed. resize_cb a 2-argument callback: (width, height).

width

internal width of canvas (client width)

height

internal height of canvas (client height)

# File lib/wrapped/canvas.rb, line 441
def resize_cb callback
  unless callback.arity == 2
    raise ArgumentError, 'resize_cb callback must take 2 arguments: (width, height)'
  end
  cb = Proc.new do |ih, width, height|
    callback.call width, height
  end
  define_callback cb, 'RESIZE_CB', :ii_i
end
sector(xc, yc, w, h, angle1, angle2) click to toggle source

Fills a sector, according to current interior style. A sector is an arc of an ellipse - a pie-shaped slice. Angles are in degrees, counterclockwise.

# File lib/wrapped/canvas.rb, line 266
def sector xc, yc, w, h, angle1, angle2
  CdLib.cdCanvasSector @canvas, xc, yc, w, h, angle1, angle2
end
stipple(w, h, fgbg) click to toggle source

fgbg is a string representing the stipple pattern in binary

# File lib/wrapped/canvas.rb, line 302
def stipple w, h, fgbg
  CdLib.cdCanvasStipple @canvas, w, h, fgbg
end
text(x, y, str) click to toggle source

Draw text str at position (x, y). Expects an ANSI-string.

# File lib/wrapped/canvas.rb, line 309
def text x, y, str
  CdLib.cdCanvasText @canvas, x, y, str
end
textalignment(alignment=CD_QUERY) click to toggle source

Sets vertical and horizontal alignment if a value is given, else returns current value. See Iup for text alignment values.

# File lib/wrapped/canvas.rb, line 330
def textalignment alignment=CD_QUERY
  CdLib.cdCanvasTextAlignment @canvas, alignment
end
textorientation(orientation=CD_QUERY) click to toggle source

Sets orientation angle if a value is given, else returns current value.

# File lib/wrapped/canvas.rb, line 336
def textorientation orientation=CD_QUERY
  CdLib.cdCanvasTextOrientation @canvas, orientation
end
vectortext(x, y, str) click to toggle source

Draws str as vector text at position (x, y). Respects

# File lib/wrapped/canvas.rb, line 344
def vectortext x, y, str
  CdLib.cdCanvasVectorText @canvas, x, y, str
end
vectortext_charsize(size=CD_QUERY) click to toggle source

If a size is given, then the font size is given that height. Otherwise, the current value is returned.

# File lib/wrapped/canvas.rb, line 360
def vectortext_charsize size=CD_QUERY
  CdLib.cdCanvasVectorCharSize @canvas, size
end
vectortext_direction(x1, y1, x2, y2) click to toggle source

Defines the text direction using two points: (x1, y1) and (x2, y2).

# File lib/wrapped/canvas.rb, line 349
def vectortext_direction x1, y1, x2, y2
  CdLib.cdCanvasVectorTextDirection @canvas, x1, y1, x2, y2
end
vectortext_fontsize(size_x, size_y) click to toggle source

Directly modifies the font size.

# File lib/wrapped/canvas.rb, line 365
def vectortext_fontsize size_x, size_y
  CdLib.cdCanvasVectorFontSize @canvas, size_x, size_y
end
vectortext_loadfont(filename) click to toggle source

Replaces current vector font with that loaded from given filename. Returns the font name, or null.

# File lib/wrapped/canvas.rb, line 371
def vectortext_loadfont filename
  CdLib.cdCanvasVectorFont @canvas, filename
end
vectortext_size(width, height, str) click to toggle source

Modifies font size so str fits within a box width x height in size.

# File lib/wrapped/canvas.rb, line 354
def vectortext_size width, height, str
  CdLib.cdCanvasVectorTextSize @canvas, width, height, str
end
vertex(x, y) click to toggle source

Specify a vertex at position (x, y).

# File lib/wrapped/canvas.rb, line 176
def vertex x, y
  CdLib.cdCanvasVertex @canvas, x, y
end
viewport(x1, y1, x2, y2) click to toggle source

Used to set coordinates of a viewport (the canvas coordinates).

# File lib/wrapped/canvas.rb, line 106
def viewport x1, y1, x2, y2
  CdLib.wdCanvasViewport @canvas, x1, y1, x2, y2
end
wheel_cb(callback) click to toggle source

Action generated when the mouse wheel is rotated. wheel_cb a 4-argument callback: (delta, x, y, status).

delta

the amount the wheel was rotated in notches.

x, y

position in the canvas where the event has occurred, in pixels.

status

status of mouse buttons and certain keyboard keys at the moment the event was generated.

# File lib/wrapped/canvas.rb, line 456
def wheel_cb callback
  unless callback.arity == 4
    raise ArgumentError, 'wheel_cb callback must take 4 arguments: (delta, x, y, status)'
  end
  cb = Proc.new do |ih, delta, x, y, status_ptr|
    status = FFI::Pointer.new(status_ptr).read_string
    callback.call delta, x, y, status
  end
  define_callback cb, 'WHEEL_CB', :fiis_i
end
window(x1, y1, x2, y2) click to toggle source

Used to set coordinates of a window (the world coordinates).

# File lib/wrapped/canvas.rb, line 111
def window x1, y1, x2, y2
  CdLib.wdCanvasWindow @canvas, x1, y1, x2, y2
end
writemode(mode=CD_QUERY) click to toggle source

Sets the writing mode for all primitives, if a value is given. Else returns the current value. See Iup for a list of available writemode values.

# File lib/wrapped/canvas.rb, line 155
def writemode mode=CD_QUERY
  CdLib.cdCanvasWriteMode @canvas, mode
end