Class: Iup::Canvas

Inherits:
Widget show all
Includes:
ButtonCallback, DragDropAttributes, ScrollBarAttributes
Defined in:
lib/wrapped/canvas.rb

Overview

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.

Instance Attribute Summary

Attributes inherited from Widget

#handle

Instance Method Summary collapse

Methods included from ButtonCallback

#button_cb

Methods included from ScrollBarAttributes

#dx, #dy, #linex, #liney, #posx, #posy, #scroll_cb, #xmax, #xmin, #ymax, #ymin

Methods included from AttributeBuilders

#define_attribute, #define_id_attribute, #define_id_readonly, #define_id_writeonly, #define_property_attribute, #define_property_writeonly, #define_readonly, #define_writeonly

Methods included from DragDropAttributes

#dragbegin_cb, #dragdata_cb, #dragdatasize_cb, #dragend_cb, #dropdata_cb, #dropmotion_cb

Methods inherited from Widget

#assign_handle, #enterwindow_cb, #getfocus_cb, #help_cb, #k_any, #killfocus_cb, #leavewindow_cb, #map_cb, #open_controls, #unmap_cb

Methods included from CallbackSetter

#define_callback

Constructor Details

#initialize(handle = IupLib.IupCanvas(''), &block) ⇒ Canvas

Creates an instance of the canvas.

handle

an optional Canvas handle.

block

optional block to set up Canvas attributes.



58
59
60
61
62
63
# 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

Instance Method Details

#action(callback) ⇒ Object

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.



380
381
382
383
384
385
386
387
388
# 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

#activateObject

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



95
96
97
# File 'lib/wrapped/canvas.rb', line 95

def activate
  CdLib.cdCanvasActivate @canvas
end

#arc(xc, yc, w, h, angle1, angle2) ⇒ Object

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



229
230
231
# 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) ⇒ Object

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



142
143
144
145
146
147
148
149
150
# 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) ⇒ Object

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



279
280
281
# File 'lib/wrapped/canvas.rb', line 279

def backopacity opacity=CD_QUERY
  CdLib.cdCanvasBackOpacity @canvas, opacity
end

#begin_block(mode) ⇒ Object

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



171
172
173
# File 'lib/wrapped/canvas.rb', line 171

def begin_block mode
  CdLib.cdCanvasBegin @canvas, mode
end

#box(xmin, xmax, ymin, ymax) ⇒ Object

Fills a rectangle, according to current interior style.



259
260
261
# 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) ⇒ Object

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.



273
274
275
# 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

#clearObject

Clears the canvas.



162
163
164
# File 'lib/wrapped/canvas.rb', line 162

def clear
  CdLib.cdCanvasClear @canvas
end

#deactivateObject

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



101
102
103
# File 'lib/wrapped/canvas.rb', line 101

def deactivate
  CdLib.cdCanvasDeactivate @canvas
end

#end_blockObject

End the definition of a sequence of vertices.



181
182
183
# File 'lib/wrapped/canvas.rb', line 181

def end_block 
  CdLib.cdCanvasEnd @canvas
end

#fillmode(mode = CD_QUERY) ⇒ Object

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



285
286
287
# File 'lib/wrapped/canvas.rb', line 285

def fillmode mode=CD_QUERY
  CdLib.cdCanvasFillMode @canvas, mode
end

#focus_cb(callback) ⇒ Object

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.



395
396
397
398
399
400
401
402
403
# 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) ⇒ Object

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.



318
319
320
# File 'lib/wrapped/canvas.rb', line 318

def font typeface, style, size
  CdLib.cdCanvasFont @canvas, typeface, style, size
end

#foreground(colour = CD_QUERY) ⇒ Object

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



127
128
129
130
131
132
133
134
135
# 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) ⇒ Object

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



297
298
299
# File 'lib/wrapped/canvas.rb', line 297

def hatch style=CD_QUERY
  CdLib.cdCanvasHatch @canvas, style
end

#initObject

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



67
68
69
# File 'lib/wrapped/canvas.rb', line 67

def init
  @canvas = CdLib.cdCreateCanvas CdLib.cdContextIup, @handle
end

#interiorstyle(style = CD_QUERY) ⇒ Object

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



291
292
293
# File 'lib/wrapped/canvas.rb', line 291

def interiorstyle style=CD_QUERY
  CdLib.cdCanvasInteriorStyle @canvas, style
end

#keypress_cb(callback) ⇒ Object

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.



409
410
411
412
413
414
415
416
417
# 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

#killObject

call when canvas no longer needed.



89
90
91
# File 'lib/wrapped/canvas.rb', line 89

def kill
  CdLib.cdKillCanvas @canvas
end

#line(x1, y1, x2, y2) ⇒ Object

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



218
219
220
# 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) ⇒ Object

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



252
253
254
# File 'lib/wrapped/canvas.rb', line 252

def linecap style=CD_QUERY
  CdLib.cdCanvasLineCap @canvas, style
end

#linejoin(style = CD_QUERY) ⇒ Object

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



246
247
248
# File 'lib/wrapped/canvas.rb', line 246

def linejoin style=CD_QUERY
  CdLib.cdCanvasLineJoin @canvas, style
end

#linestyle(style = CD_QUERY) ⇒ Object

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



235
236
237
# File 'lib/wrapped/canvas.rb', line 235

def linestyle style=CD_QUERY
  CdLib.cdCanvasLineStyle @canvas, style
end

#linewidth(width = CD_QUERY) ⇒ Object

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



240
241
242
# File 'lib/wrapped/canvas.rb', line 240

def linewidth width=CD_QUERY
  CdLib.cdCanvasLineWidth @canvas, width
end

#mark(x, y) ⇒ Object

Draws a mark at position (x, y).



199
200
201
# File 'lib/wrapped/canvas.rb', line 199

def mark x, y
  CdLib.cdCanvasMark @canvas, x, y
end

#marksize(size = CD_QUERY) ⇒ Object

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



211
212
213
# File 'lib/wrapped/canvas.rb', line 211

def marksize size=CD_QUERY
  CdLib.cdCanvasMarkSize size
end

#marktype(type = CD_QUERY) ⇒ Object

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



205
206
207
# File 'lib/wrapped/canvas.rb', line 205

def marktype type=CD_QUERY
  CdLib.cdCanvasMarkType @canvas, type
end

#motion_cb(callback) ⇒ Object

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.

– TODO: include functions, as in button_cb



427
428
429
430
431
432
433
434
435
# 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) ⇒ Object

Sets the current font.



323
324
325
# File 'lib/wrapped/canvas.rb', line 323

def nativefont font
  CdLib.cdCanvasNativefont @canvas, font
end

#pathset(action) ⇒ Object

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



187
188
189
# File 'lib/wrapped/canvas.rb', line 187

def pathset action
  CdLib.cdCanvasPathSet @canvas, action
end

#pixel(x, y, colour) ⇒ Object

Configures pixel (x, y) with colour.



194
195
196
# File 'lib/wrapped/canvas.rb', line 194

def pixel x, y, colour
  CdLib.cdCanvasPixel @canvas, x, y, colour
end

#rectangle(xmin, xmax, ymin, ymax) ⇒ Object

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



223
224
225
# File 'lib/wrapped/canvas.rb', line 223

def rectangle xmin, xmax, ymin, ymax
  CdLib.cdCanvasRect @canvas, xmin, xmax, ymin, ymax
end

#redrawObject

redraws the underlying canvas.



116
117
118
# File 'lib/wrapped/canvas.rb', line 116

def redraw
  IupLib.IupRedraw @handle
end

#resize_cb(callback) ⇒ Object

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)



441
442
443
444
445
446
447
448
449
# 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) ⇒ Object

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.



266
267
268
# 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) ⇒ Object

fgbg is a string representing the stipple pattern in binary



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

def stipple w, h, fgbg
  CdLib.cdCanvasStipple @canvas, w, h, fgbg
end

#text(x, y, str) ⇒ Object

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



309
310
311
# File 'lib/wrapped/canvas.rb', line 309

def text x, y, str
  CdLib.cdCanvasText @canvas, x, y, str
end

#textalignment(alignment = CD_QUERY) ⇒ Object

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



330
331
332
# File 'lib/wrapped/canvas.rb', line 330

def textalignment alignment=CD_QUERY
  CdLib.cdCanvasTextAlignment @canvas, alignment
end

#textorientation(orientation = CD_QUERY) ⇒ Object

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



336
337
338
# File 'lib/wrapped/canvas.rb', line 336

def textorientation orientation=CD_QUERY
  CdLib.cdCanvasTextOrientation @canvas, orientation
end

#vectortext(x, y, str) ⇒ Object

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



344
345
346
# File 'lib/wrapped/canvas.rb', line 344

def vectortext x, y, str
  CdLib.cdCanvasVectorText @canvas, x, y, str
end

#vectortext_charsize(size = CD_QUERY) ⇒ Object

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



360
361
362
# File 'lib/wrapped/canvas.rb', line 360

def vectortext_charsize size=CD_QUERY
  CdLib.cdCanvasVectorCharSize @canvas, size
end

#vectortext_direction(x1, y1, x2, y2) ⇒ Object

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



349
350
351
# 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) ⇒ Object

Directly modifies the font size.



365
366
367
# 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) ⇒ Object

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



371
372
373
# File 'lib/wrapped/canvas.rb', line 371

def vectortext_loadfont filename
  CdLib.cdCanvasVectorFont @canvas, filename
end

#vectortext_size(width, height, str) ⇒ Object

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



354
355
356
# File 'lib/wrapped/canvas.rb', line 354

def vectortext_size width, height, str
  CdLib.cdCanvasVectorTextSize @canvas, width, height, str
end

#vertex(x, y) ⇒ Object

Specify a vertex at position (x, y).



176
177
178
# File 'lib/wrapped/canvas.rb', line 176

def vertex x, y
  CdLib.cdCanvasVertex @canvas, x, y
end

#viewport(x1, y1, x2, y2) ⇒ Object

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



106
107
108
# File 'lib/wrapped/canvas.rb', line 106

def viewport x1, y1, x2, y2
  CdLib.wdCanvasViewport @canvas, x1, y1, x2, y2
end

#wheel_cb(callback) ⇒ Object

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.



456
457
458
459
460
461
462
463
464
465
# 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) ⇒ Object

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



111
112
113
# 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) ⇒ Object

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.



155
156
157
# File 'lib/wrapped/canvas.rb', line 155

def writemode mode=CD_QUERY
  CdLib.cdCanvasWriteMode @canvas, mode
end