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. 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.

Direct Known Subclasses

BackgroundBox, ColorBar, Dial, ScrollBox

Instance Attribute Summary

Attributes inherited from Widget

#handle

Instance Method Summary collapse

Methods included from ButtonCallback

#button_cb=

Methods included from ScrollBarAttributes

#dx, #dx=, #dy, #dy=, #linex, #linex=, #liney, #liney=, #posx, #posx=, #posy, #posy=, #scroll_cb=, #scrollbar, #xautohide, #xmax, #xmax=, #xmin, #xmin=, #yautohide, #ymax, #ymax=, #ymin, #ymin=

Methods included from AttributeBuilders

#define_attribute, #define_id_attribute, #define_id_reader, #define_id_writer, #define_property_attribute, #define_property_reader, #define_property_writer, #define_reader, #define_writer

Methods included from DragDropAttributes

#dragbegin_cb=, #dragdata_cb=, #dragdatasize_cb=, #dragend_cb=, #dragsource, #dragsourcemove, #dragtypes, #dropdata_cb=, #dropmotion_cb=, #droptarget, #droptypes

Methods inherited from Widget

#active, #assign_handle, #bgcolor, #destroy, #enterwindow_cb=, #fgcolor, #getfocus_cb=, #help_cb=, #k_any=, #killfocus_cb=, #leavewindow_cb=, #map_cb=, #maxsize, #minsize, #open_controls, #size, #unmap_cb=, #visible, #wid, #zorder

Methods included from CallbackSetter

#define_callback

Constructor Details

#initialize(handle = IupLib.IupCanvas('')) {|_self| ... } ⇒ Canvas

Creates a new instance. If a block is given, the new instance is yielded to it.

  • handle - an optional Canvas handle.

Yields:

  • (_self)

Yield Parameters:

  • _self (Iup::Canvas)

    the object that the method was called on



81
82
83
84
85
86
# 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

Instance Method Details

#action=(callback) ⇒ Object

--



593
594
595
596
597
598
599
600
601
# File 'lib/wrapped/canvas.rb', line 593

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.)



168
169
170
# File 'lib/wrapped/canvas.rb', line 168

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.



342
343
344
# 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

#backgroundObject

--



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

def background
  CdLib.cdCanvasBackground(@canvas, Iup::CD_QUERY)
end

#background=(color) ⇒ Object

:nodoc:



227
228
229
230
231
232
233
234
235
# File 'lib/wrapped/canvas.rb', line 227

def background= color # :nodoc:
  case color
  when String # assume in form 'RR GG BB'
    r, g, b = color.split(' ').collect(&:to_i)
    CdLib.cdCanvasBackground(@canvas, CdLib.cdEncodeColor(r, g, b))
  else
    CdLib.cdCanvasBackground(@canvas, color)
  end
end

#backopacityObject

--



429
430
431
# File 'lib/wrapped/canvas.rb', line 429

def backopacity
  CdLib.cdCanvasBackOpacity(@canvas, Iup::CD_QUERY)
end

#backopacity=(opacity) ⇒ Object

:nodoc:



433
434
435
# File 'lib/wrapped/canvas.rb', line 433

def backopacity= opacity # :nodoc:
  CdLib.cdCanvasBackOpacity(@canvas, opacity)
end

#begin_block(mode) ⇒ Object

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.



267
268
269
# File 'lib/wrapped/canvas.rb', line 267

def begin_block mode
  CdLib.cdCanvasBegin @canvas, mode
end

#borderObject

:attr: border Sets border around canvas, values 'yes' / 'no'.



100
# File 'lib/wrapped/canvas.rb', line 100

define_attribute :border

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

Fills a rectangle, according to current interior style.



404
405
406
# File 'lib/wrapped/canvas.rb', line 404

def box xmin, xmax, ymin, ymax
  CdLib.cdCanvasBox @canvas, xmin, xmax, ymin, ymax
end

#canfocusObject

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



105
# File 'lib/wrapped/canvas.rb', line 105

define_attribute :canfocus

#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.



418
419
420
# 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

#clearObject

Clears the canvas.



254
255
256
# File 'lib/wrapped/canvas.rb', line 254

def clear
  CdLib.cdCanvasClear @canvas
end

#cursorObject

:attr: cursor Defines the mouse shape / cursor for the canvas. Available cursors are shown in the Tecgraf documentation.



112
# File 'lib/wrapped/canvas.rb', line 112

define_attribute :cursor

#deactivateObject

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



174
175
176
# File 'lib/wrapped/canvas.rb', line 174

def deactivate
  CdLib.cdCanvasDeactivate @canvas
end

#drawsizeObject

:attr: drawsize Size of the drawing area in pixels, as "widthxheight".



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

define_attribute :drawsize

#end_blockObject

End the definition of a sequence of vertices. See #begin_block.



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

def end_block 
  CdLib.cdCanvasEnd @canvas
end

#expandObject

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



123
# File 'lib/wrapped/canvas.rb', line 123

define_attribute :expand

#fillmodeObject

--



443
444
445
# File 'lib/wrapped/canvas.rb', line 443

def fillmode 
  CdLib.cdCanvasFillMode(@canvas, Iup::CD_QUERY)
end

#fillmode=(mode) ⇒ Object

:nodoc:



447
448
449
# File 'lib/wrapped/canvas.rb', line 447

def fillmode= mode # :nodoc:
  CdLib.cdCanvasFillMode(@canvas, mode)
end

#focus_cb=(callback) ⇒ Object

--



612
613
614
615
616
617
618
619
620
# File 'lib/wrapped/canvas.rb', line 612

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 CD text style
  • size - the font height, default is 12.


498
499
500
# File 'lib/wrapped/canvas.rb', line 498

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

#foregroundObject

--



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

def foreground
  CdLib.cdCanvasForeground(@canvas, Iup::CD_QUERY)
end

#foreground=(color) ⇒ Object

:nodoc:



203
204
205
206
207
208
209
210
211
# File 'lib/wrapped/canvas.rb', line 203

def foreground= color # :nodoc:
  case color
  when String # assume in form 'RR GG BB'
    r, g, b = color.split(' ').collect(&:to_i)
    CdLib.cdCanvasForeground(@canvas, CdLib.cdEncodeColor(r, g, b))
  else
    CdLib.cdCanvasForeground(@canvas, color)
  end
end

#hatchObject

--



471
472
473
# File 'lib/wrapped/canvas.rb', line 471

def hatch
  CdLib.cdCanvasHatch(@canvas, Iup::CD_QUERY)
end

#hatch=(style) ⇒ Object

:nodoc:



475
476
477
# File 'lib/wrapped/canvas.rb', line 475

def hatch= style # :nodoc:
  CdLib.cdCanvasHatch(@canvas, style)
end

#initObject

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



91
92
93
# File 'lib/wrapped/canvas.rb', line 91

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

#interiorstyleObject

--



457
458
459
# File 'lib/wrapped/canvas.rb', line 457

def interiorstyle
  CdLib.cdCanvasInteriorStyle(@canvas, Iup::CD_QUERY)
end

#interiorstyle=(style) ⇒ Object

:nodoc:



461
462
463
# File 'lib/wrapped/canvas.rb', line 461

def interiorstyle= style # :nodoc:
  CdLib.cdCanvasInteriorStyle(@canvas, style)
end

#keypress_cb=(callback) ⇒ Object

--



630
631
632
633
634
635
636
637
638
# File 'lib/wrapped/canvas.rb', line 630

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.



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

def kill
  CdLib.cdKillCanvas @canvas
end

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

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



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

def line x1, y1, x2, y2
  CdLib.cdCanvasLine @canvas, x1, y1, x2, y2
end

#linecapObject

--



393
394
395
# File 'lib/wrapped/canvas.rb', line 393

def linecap
  CdLib.cdCanvasLineCap(@canvas, Iup::CD_QUERY)
end

#linecap=(style) ⇒ Object

:nodoc:



397
398
399
# File 'lib/wrapped/canvas.rb', line 397

def linecap= style # :nodoc:
  CdLib.cdCanvasLineCap(@canvas, style)
end

#linejoinObject

--



379
380
381
# File 'lib/wrapped/canvas.rb', line 379

def linejoin
  CdLib.cdCanvasLineJoin(@canvas, Iup::CD_QUERY)
end

#linejoin=(style) ⇒ Object

:nodoc:



383
384
385
# File 'lib/wrapped/canvas.rb', line 383

def linejoin= style # :nodoc:
  CdLib.cdCanvasLineJoin(@canvas, style)
end

#linestyleObject

--



352
353
354
# File 'lib/wrapped/canvas.rb', line 352

def linestyle
  CdLib.cdCanvasLineStyle(@canvas, Iup::CD_QUERY)
end

#linestyle=(style) ⇒ Object

:nodoc:



356
357
358
# File 'lib/wrapped/canvas.rb', line 356

def linestyle= style # :nodoc:
  CdLib.cdCanvasLineStyle(@canvas, style)
end

#linewidthObject

--



365
366
367
# File 'lib/wrapped/canvas.rb', line 365

def linewidth
  CdLib.cdCanvasLineWidth(@canvas, Iup::CD_QUERY)
end

#linewidth=(width) ⇒ Object

:nodoc:



369
370
371
# File 'lib/wrapped/canvas.rb', line 369

def linewidth= width # :nodoc:
  CdLib.cdCanvasLineWidth(@canvas, width)
end

#mark(x, y) ⇒ Object

Draws a mark at position (x, y).



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

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

#marksizeObject

--



320
321
322
# File 'lib/wrapped/canvas.rb', line 320

def marksize
  CdLib.cdCanvasMarkSize(@canvas, Iup::CD_QUERY)
end

#marksize=(size) ⇒ Object

:nodoc:



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

def marksize= size # :nodoc:
  CdLib.cdCanvasMarkSize(@canvas, size)
end

#marktypeObject

--



307
308
309
# File 'lib/wrapped/canvas.rb', line 307

def marktype
  CdLib.cdCanvasMarkType(@canvas, Iup::CD_QUERY)
end

#marktype=(type) ⇒ Object

:nodoc:



311
312
313
# File 'lib/wrapped/canvas.rb', line 311

def marktype= type # :nodoc:
  CdLib.cdCanvasMarkType(@canvas, type)
end

#motion_cb=(callback) ⇒ Object

--



649
650
651
652
653
654
655
656
657
# File 'lib/wrapped/canvas.rb', line 649

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

--



507
508
509
# File 'lib/wrapped/canvas.rb', line 507

def nativefont= font
  CdLib.cdCanvasNativefont(@canvas, font)
end

#paddingObject

:attr: padding Margin in x and y directions, value as "mxn".



128
# File 'lib/wrapped/canvas.rb', line 128

define_attribute :padding

#pathset(action) ⇒ Object

Specify action for next point on path. See CD path action for path actions.



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

def pathset action
  CdLib.cdCanvasPathSet @canvas, action
end

#pixel(x, y, color) ⇒ Object

Configures pixel (x, y) with color.



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

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

#positionObject

:attr_reader: position returns position in pixels within client window as "x,y".



133
# File 'lib/wrapped/canvas.rb', line 133

define_reader :position

#rastersizeObject

:attr: rastersize Size of the canvas, in pixels, value as "widthxheight".



138
# File 'lib/wrapped/canvas.rb', line 138

define_attribute :rastersize

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

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



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

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

#redrawObject

redraws the underlying canvas.



151
152
153
# File 'lib/wrapped/canvas.rb', line 151

def redraw
  IupLib.IupRedraw @handle
end

#resize_cb=(callback) ⇒ Object

--



667
668
669
670
671
672
673
674
675
# File 'lib/wrapped/canvas.rb', line 667

def resize_cb= callback # :nodoc:
  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

#screenpositionObject

:attr_reader: screenposition returns position in pixels on screen



143
# File 'lib/wrapped/canvas.rb', line 143

define_reader :screenposition

#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.



411
412
413
# 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

#stipple(w, h, fgbg) ⇒ Object

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


483
484
485
# File 'lib/wrapped/canvas.rb', line 483

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.



490
491
492
# File 'lib/wrapped/canvas.rb', line 490

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

#textalignmentObject

--



517
518
519
# File 'lib/wrapped/canvas.rb', line 517

def textalignment 
  CdLib.cdCanvasTextAlignment(@canvas, Iup::CD_QUERY)
end

#textalignment=(alignment) ⇒ Object

:nodoc:



521
522
523
# File 'lib/wrapped/canvas.rb', line 521

def textalignment= alignment # :nodoc:
  CdLib.cdCanvasTextAlignment(@canvas, alignment)
end

#textorientationObject

--



531
532
533
# File 'lib/wrapped/canvas.rb', line 531

def textorientation 
  CdLib.cdCanvasTextOrientation(@canvas, Iup::CD_QUERY)
end

#textorientation=(orientation) ⇒ Object

:nodoc:



535
536
537
# File 'lib/wrapped/canvas.rb', line 535

def textorientation= orientation # :nodoc:
  CdLib.cdCanvasTextOrientation(@canvas, orientation)
end

#tipObject

:attr: tip Tooltip string.



148
# File 'lib/wrapped/canvas.rb', line 148

define_attribute :tip

#vectortext(x, y, str) ⇒ Object

Draws str as vector text at position (x, y). Call #vectortext_size to set the text size before calling this method.



543
544
545
# File 'lib/wrapped/canvas.rb', line 543

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

#vectortext_charsizeObject

--



562
563
564
# File 'lib/wrapped/canvas.rb', line 562

def vectortext_charsize
  CdLib.cdCanvasVectorCharSize(@canvas, Iup::CD_QUERY)
end

#vectortext_charsize=(size) ⇒ Object

:nodoc:



566
567
568
# File 'lib/wrapped/canvas.rb', line 566

def vectortext_charsize= size # :nodoc:
  CdLib.cdCanvasVectorCharSize(@canvas, size)
end

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

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



548
549
550
# File 'lib/wrapped/canvas.rb', line 548

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.



571
572
573
# File 'lib/wrapped/canvas.rb', line 571

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.



577
578
579
# File 'lib/wrapped/canvas.rb', line 577

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.



553
554
555
# File 'lib/wrapped/canvas.rb', line 553

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

#vertex(x, y) ⇒ Object

Specify a vertex at position (x, y). See #begin_block.



273
274
275
# File 'lib/wrapped/canvas.rb', line 273

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).



179
180
181
# File 'lib/wrapped/canvas.rb', line 179

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

#wheel_cb=(callback) ⇒ Object

--



687
688
689
690
691
692
693
694
695
696
# File 'lib/wrapped/canvas.rb', line 687

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).



184
185
186
# File 'lib/wrapped/canvas.rb', line 184

def window x1, y1, x2, y2
  CdLib.wdCanvasWindow @canvas, x1, y1, x2, y2
end

#writemodeObject

--



243
244
245
# File 'lib/wrapped/canvas.rb', line 243

def writemode
  CdLib.cdCanvasWriteMode(@canvas, Iup::CD_QUERY)
end

#writemode=(mode) ⇒ Object

:nodoc:



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

def writemode= mode # :nodoc:
  CdLib.cdCanvasWriteMode(@canvas, mode)
end