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.
Attributes
Sets border around canvas, values âyesâ / ânoâ.
Enables the control to gain focus. Values âyesâ / ânoâ.
Defines the mouse shape / cursor for the canvas. Available cursors are shown in the Tecgraf documentation.
Size of the drawing area in pixels, as âwidthxheightâ.
Allows canvas to fill available space in indicated direction. Values ânoâ / âhorizontalâ / âverticalâ / âyesâ.
Margin in x and y directions, value as âmxnâ.
returns position in pixels within client window as âx,yâ.
Size of the canvas, in pixels, value as âwidthxheightâ.
returns position in pixels on screen
Tooltip string.
Public Class Methods
Source
# File lib/wrapped/canvas.rb, line 81 def initialize(handle = IupLib.IupCanvas('')) @handle = handle # run any provided block on instance, to set up further attributes yield self if block_given? end
Creates a new instance. If a block is given, the new instance is yielded to it.
-
handle- an optionalCanvashandle.
Public Instance Methods
Source
# File lib/wrapped/canvas.rb, line 151 def redraw IupLib.IupRedraw @handle end
redraws the underlying canvas.
CD drawing
Attributes
Accesses the background color.
Color may be one of:
-
string, in form âr g bâ
-
color constant, e.g. CD_BLACK, as listed in CD colors
Accesses backopacity value. See CD background opacity mode for background opacity mode values.
Accesses fillmode value. See CD fill mode for fill mode values.
Accesses the foreground color.
Color may be one of:
-
string, in form âr g bâ
-
color constant, e.g. CD_BLACK, as listed in CD colors
Accesses hatch value. See CD hatch type for hatch style values.
Accesses interiorstyle value. See CD interior style for interior style values.
Accesses linecap value. See CD line cap for available linecap values.
Accesses linejoin value. See CD line join for available linejoin values.
Accesses linestyle value. See CD line style for available linestyle values.
Accesses linewidth, in pixels.
Accesses the mark size, given in pixels. Must be >= 1, default is 10.
Accesses the marktype to display. See CD mark type for available marktype values.
Sets the current font.
Accesses the textalignment value. See CD text alignment.
Accesses the textorientation value. The orientation is an angle in degrees to the horizontal.
The font size.
Accesses. the writing mode for all primitives. See CD write mode for a list of available values.
Public Instance Methods
Source
# File lib/wrapped/canvas.rb, line 168 def activate CdLib.cdCanvasActivate @canvas end
call to activate canvas. (Not usually needed - depends on driver.)
Source
# File lib/wrapped/canvas.rb, line 342 def arc xc, yc, w, h, angle1, angle2 CdLib.cdCanvasArc @canvas, xc, yc, w, h, angle1, angle2 end
Draws an arc of an ellipse - a pie-shaped slice. Angles are in degrees, counterclockwise.
Source
# File lib/wrapped/canvas.rb, line 267 def begin_block mode CdLib.cdCanvasBegin @canvas, mode end
Begin the definition of a sequence of vertices to define a polygon. See CD polygon mode for polygon mode values.
Filled polygons are defined using begin_block, multiple calls to vertex, and ended with end_block.
Source
# File lib/wrapped/canvas.rb, line 404 def box xmin, xmax, ymin, ymax CdLib.cdCanvasBox @canvas, xmin, xmax, ymin, ymax end
Fills a rectangle, according to current interior style.
Source
# File lib/wrapped/canvas.rb, line 418 def chord xc, yc, w, h, angle1, angle2 CdLib.cdCanvasChord @canvas, xc, yc, w, h, angle1, angle2 end
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.
Source
# File lib/wrapped/canvas.rb, line 254 def clear CdLib.cdCanvasClear @canvas end
Clears the canvas.
Source
# File lib/wrapped/canvas.rb, line 174 def deactivate CdLib.cdCanvasDeactivate @canvas end
call to deactivate canvas. (Not usually needed - depends on driver.)
Source
# File lib/wrapped/canvas.rb, line 279 def end_block CdLib.cdCanvasEnd @canvas end
End the definition of a sequence of vertices. See begin_block.
Source
# File lib/wrapped/canvas.rb, line 498 def font typeface, style, size CdLib.cdCanvasFont @canvas, typeface, style, size end
Sets text font.
-
typeface- âCourierâ, âHelveticaâ, âTimesâ built-in, but any known font can be used -
style- see CD text style -
size- the font height, default is 12.
Source
# File lib/wrapped/canvas.rb, line 91 def init @canvas = CdLib.cdCreateCanvas(CdLib.cdContextIup, @handle) end
This must be called after the widget is mapped, to create and setup the canvas.
Source
# File lib/wrapped/canvas.rb, line 162 def kill CdLib.cdKillCanvas @canvas end
call when canvas no longer needed.
Source
# File lib/wrapped/canvas.rb, line 331 def line x1, y1, x2, y2 CdLib.cdCanvasLine @canvas, x1, y1, x2, y2 end
Draws a line from (x1, y1) to (x2, y2).
Source
# File lib/wrapped/canvas.rb, line 297 def mark x, y CdLib.cdCanvasMark @canvas, x, y end
Draws a mark at position (x, y).
Source
# File lib/wrapped/canvas.rb, line 285 def pathset action CdLib.cdCanvasPathSet @canvas, action end
Specify action for next point on path. See CD path action for path actions.
Source
# File lib/wrapped/canvas.rb, line 292 def pixel x, y, color CdLib.cdCanvasPixel @canvas, x, y, color end
Configures pixel (x, y) with color.
Source
# File lib/wrapped/canvas.rb, line 336 def rectangle xmin, xmax, ymin, ymax CdLib.cdCanvasRect @canvas, xmin, xmax, ymin, ymax end
Draws a rectangle bounding (xmin, ymin) to (xmax, ymax).
Source
# File lib/wrapped/canvas.rb, line 411 def sector xc, yc, w, h, angle1, angle2 CdLib.cdCanvasSector @canvas, xc, yc, w, h, angle1, angle2 end
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.
Source
# File lib/wrapped/canvas.rb, line 483 def stipple w, h, fgbg CdLib.cdCanvasStipple @canvas, w, h, fgbg end
Defines a stipple pattern of a specified size:
-
w- width of stipple pattern -
h- height of stipple pattern -
fgbg- a string representing the stipple pattern in binary
Source
# File lib/wrapped/canvas.rb, line 490 def text x, y, str CdLib.cdCanvasText @canvas, x, y, str end
Draw text str at position (x, y). Expects an ANSI-string.
Source
# File lib/wrapped/canvas.rb, line 543 def vectortext x, y, str CdLib.cdCanvasVectorText @canvas, x, y, str end
Draws str as vector text at position (x, y). Call vectortext_size to set the text size before calling this method.
Source
# File lib/wrapped/canvas.rb, line 548 def vectortext_direction x1, y1, x2, y2 CdLib.cdCanvasVectorTextDirection @canvas, x1, y1, x2, y2 end
Defines the text direction using two points: (x1, y1) and (x2, y2).
Source
# File lib/wrapped/canvas.rb, line 571 def vectortext_fontsize size_x, size_y CdLib.cdCanvasVectorFontSize @canvas, size_x, size_y end
Directly modifies the font size.
Source
# File lib/wrapped/canvas.rb, line 577 def vectortext_loadfont filename CdLib.cdCanvasVectorFont @canvas, filename end
Replaces current vector font with that loaded from given filename. Returns the font name, or null.
Source
# File lib/wrapped/canvas.rb, line 553 def vectortext_size width, height, str CdLib.cdCanvasVectorTextSize @canvas, width, height, str end
Modifies font size so str fits within a box width x height in size.
Source
# File lib/wrapped/canvas.rb, line 273 def vertex x, y CdLib.cdCanvasVertex @canvas, x, y end
Specify a vertex at position (x, y). See begin_block.
Source
# File lib/wrapped/canvas.rb, line 179 def viewport x1, y1, x2, y2 CdLib.wdCanvasViewport @canvas, x1, y1, x2, y2 end
Used to set coordinates of a viewport (the canvas coordinates).
Source
# File lib/wrapped/canvas.rb, line 184 def window x1, y1, x2, y2 CdLib.wdCanvasWindow @canvas, x1, y1, x2, y2 end
Used to set coordinates of a window (the world coordinates).
Callbacks
Attributes
Callback called when the canvas needs to be redrawn. Callback must respond to call and accepts 2 arguments: (posx, posy)
-
posx- position of horizonal scrollbar thumb -
posy- position of vertical scrollbar thumb
Typically redraws canvas: see CD drawing.
Callback called when the canvas gets or loses the keyboard focus. Callback must respond to call and takes a single parameter: (focus)
-
focus- non-zero if canvas gaining focus, else zero.
Callback called when a key is pressed or released. Callback must respond to call and takes a 2-argument callback: (character, pressed).
-
character- code for character pressed/released -
pressed- 1 if key is pressed, 0 if released
Callback called when the mouse is moved. Callback must respond to call and 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.
Callback called when the canvas size is changed. Callback must respond to call and takes 2 arguments: (width, height).
-
width-internal width of canvas (client width) -
height- internal height of canvas (client height)
Callback called when the mouse wheel is rotated. Callback must respond to call and accepts a 4-argument callback: (delta, x, y, status).
-
delta- the amount the wheel was rotated in notches. -
x- x position in the canvas where the event has occurred, in pixels. -
y- 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.