Class: Fox::Canvas::ShapeCanvas

Inherits:
FXCanvas show all
Includes:
Responder
Defined in:
lib/fox16/canvas.rb

Constant Summary collapse

FLAG_SHOWN =

Window state flags

0x00000001
FLAG_ENABLED =

Is shown

0x00000002
FLAG_UPDATE =

Able to receive input

0x00000004
FLAG_DROPTARGET =

Is subject to GUI update

0x00000008
FLAG_FOCUSED =

Drop target

0x00000010
FLAG_DIRTY =

Has focus

0x00000020
FLAG_RECALC =

Needs layout

0x00000040
FLAG_TIP =

Needs recalculation

0x00000080
FLAG_HELP =

Show tip

0x00000100
FLAG_DEFAULT =

Show help

0x00000200
FLAG_INITIAL =

Default widget

0x00000400
FLAG_SHELL =

Initial widget

0x00000800
FLAG_ACTIVE =

Shell window

0x00001000
FLAG_PRESSED =

Window is active

0x00002000
FLAG_KEY =

Button has been pressed

0x00004000
FLAG_CARET =

Keyboard key pressed

0x00008000
FLAG_CHANGED =

Caret is on

0x00010000
FLAG_LASSO =

Window data changed

0x00020000
FLAG_TRYDRAG =

Lasso mode

0x00040000
FLAG_DODRAG =

Tentative drag mode

0x00080000
FLAG_SCROLLINSIDE =

Doing drag mode

0x00100000
FLAG_SCROLLING =

Scroll only when inside

0x00200000

Instance Attribute Summary collapse

Attributes inherited from FXWindow

#accelTable, #backColor, #defaultCursor, #dragCursor, #first, #focus, #key, #last, #layoutHints, #next, #numChildren, #owner, #parent, #prev, #root, #selector, #shell, #target, #x, #y

Attributes inherited from FXDrawable

#height, #visual, #width

Attributes inherited from FXId

#app, #userData, #xid

Instance Method Summary collapse

Methods included from Responder

#FXMAPFUNC, #FXMAPFUNCS, #FXMAPTYPE, #FXMAPTYPES, #addMapEntry, #assocIndex, #identifier, #messageMap

Methods inherited from FXWindow

#acceptDrop, #acquireClipboard, #acquireSelection, #active?, #addHotKey, #after?, after?, #before?, before?, #beginDrag, #canFocus?, #changeFocus, #childAtIndex, #childOf?, #children, #clearDragRectangle, #clearShape, colorType, colorTypeName, commonAncestor, #composeContext, #composite?, #contains?, #containsChild?, #create, #createComposeContext, #cursorPosition, #default?, #defaultHeight, #defaultWidth, deleteType, deleteTypeName, #destroy, #destroyComposeContext, #detach, #didAccept, #disable, #doesSaveUnder?, #dragging?, #dropDisable, #dropEnable, #dropEnabled?, #dropFinished, #dropTarget?, #each_child, #each_child_recursive, #enable, #enabled?, #endDrag, #forceRefresh, #getChildAt, #getDNDData, #getHeightForWidth, #getWidthForHeight, #grab, #grabKeyboard, #grabbed?, #grabbedKeyboard?, #handleDrag, #hasClipboard?, #hasFocus?, #hasSelection?, #height, #height=, #hide, imageType, #inFocusChain?, #indexOfChild, #initial?, #inquireDNDAction, #inquireDNDTypes, #killFocus, #layout, #linkAfter, #linkBefore, #lower, #move, octetType, octetTypeName, #offeredDNDType?, #position, #raiseWindow, #recalc, #releaseClipboard, #releaseSelection, #remHotKey, #removeChild, #repaint, #reparent, #resize, #scroll, #setCursorPosition, #setDNDData, #setDefault, #setDragRectangle, #setFocus, #setInitial, #setShape, #shell?, #show, #shown?, stringType, textType, textTypeName, #tr, #translateCoordinatesFrom, #translateCoordinatesTo, #underCursor?, #ungrab, #ungrabKeyboard, #update, urilistType, urilistTypeName, utf16Type, utf16TypeName, utf8Type, utf8TypeName, #visible=, #width, #width=

Methods included from Responder2

#connect

Methods inherited from FXDrawable

#resize

Methods inherited from FXId

#create, #created?, #destroy, #detach, #runOnUiThread

Methods inherited from FXObject

#bind, #handle, #load, #save, subclasses

Constructor Details

#initialize(p, tgt = nil, sel = 0, opts = FRAME_NORMAL, x = 0, y = 0, w = 0, h = 0) ⇒ ShapeCanvas

Returns a new instance of ShapeCanvas.



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/fox16/canvas.rb', line 406

def initialize(p, tgt=nil, sel=0, opts=FRAME_NORMAL, x=0, y=0, w=0, h=0)
  # Initialize base class
  super(p, tgt, sel, opts, x, y, w, h)

  # Start with an empty group
  @scene = ShapeGroup.new

  # Selection policy
  @selectionPolicy = SingleSelectionPolicy.new(self)

  @flags = 0

  # Map
  FXMAPFUNC(SEL_PAINT, 0, "onPaint")
  FXMAPFUNC(SEL_MOTION, 0, "onMotion")
  FXMAPFUNC(SEL_LEFTBUTTONPRESS, 0, "onLeftBtnPress")
  FXMAPFUNC(SEL_LEFTBUTTONRELEASE, 0, "onLeftBtnRelease")
  FXMAPFUNC(SEL_CLICKED,0,"onClicked")
  FXMAPFUNC(SEL_DOUBLECLICKED,0,"onDoubleClicked")
  FXMAPFUNC(SEL_TRIPLECLICKED,0,"onTripleClicked")
  FXMAPFUNC(SEL_COMMAND,0,"onCommand")
end

Instance Attribute Details

#sceneObject

Returns the value of attribute scene.



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

def scene
  @scene
end

Instance Method Details

#deselectShape(shape, notify = false) ⇒ Object

Deselect one shape



480
481
482
483
484
485
486
# File 'lib/fox16/canvas.rb', line 480

def deselectShape(shape, notify=false)
  if @scene.include?(shape)
    @selectionPolicy.deselectShape(shape, notify)
  else
    raise CanvasError
  end
end

#disableShape(shape) ⇒ Object

Disable one shape



459
460
461
462
463
464
465
466
467
468
# File 'lib/fox16/canvas.rb', line 459

def disableShape(shape)
  if @scene.include?(shape)
    if shape.enabled?
      shape.disable
      updateShape(shape)
    end
  else
    raise CanvasError
  end
end

#enableShape(shape) ⇒ Object

Enable one shape



447
448
449
450
451
452
453
454
455
456
# File 'lib/fox16/canvas.rb', line 447

def enableShape(shape)
  if @scene.include?(shape)
    unless shape.enabled?
      shape.enable
      updateShape(shape)
    end
  else
    raise CanvasError
  end
end

#findShape(x, y) ⇒ Object

Find the shape of the least depth containing this point



430
431
432
433
434
435
# File 'lib/fox16/canvas.rb', line 430

def findShape(x, y)
  @scene.reverse_each do |shape|
    return shape if shape.hit?(x, y)
  end
  nil
end

#killSelection(notify) ⇒ Object

Kill selection



489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/fox16/canvas.rb', line 489

def killSelection(notify)
  changes = false
  @scene.each do |shape|
    if shape.selected?
      shape.deselect
      updateShape(shape)
      changes = true
      if notify && (target != nil)
        target.handle(self, Fox.MKUINT(message, SEL_DESELECTED), shape)
      end
    end
  end
  changes
end

#onClicked(sender, sel, ptr) ⇒ Object

Clicked on canvas



622
623
624
# File 'lib/fox16/canvas.rb', line 622

def onClicked(sender, sel, ptr)
  return target && target.handle(self, Fox.MKUINT(message, SEL_CLICKED), ptr)
end

#onCommand(sender, sel, ptr) ⇒ Object

Command message



617
618
619
# File 'lib/fox16/canvas.rb', line 617

def onCommand(sender, sel, ptr)
  return target && target.handle(self, Fox.MKUINT(message, SEL_COMMAND), ptr)
end

#onDoubleClicked(sender, sel, ptr) ⇒ Object

Double-clicked on canvas



627
628
629
# File 'lib/fox16/canvas.rb', line 627

def onDoubleClicked(sender, sel, ptr)
  return target && target.handle(self, Fox.MKUINT(message, SEL_DOUBLECLICKED), ptr)
end

#onLeftBtnPress(sender, sel, evt) ⇒ Object

Left button press



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/fox16/canvas.rb', line 537

def onLeftBtnPress(sender, sel, evt)
  handle(self, Fox.MKUINT(0, SEL_FOCUS_SELF), evt)
  if enabled?
    grab
    flags &= ~FLAG_UPDATE

    # Give target the first chance at handling this
    return 1 if target && (target.handle(self, Fox.MKUINT(message, SEL_LEFTBUTTONPRESS), evt) != 0)

    # Locate shape
    shape = findShape(evt.win_x, evt.win_y)

    # No shape here
    if shape.nil?
      return 1
    end

    # Change current shape
    @currentShape = shape

    # Change item selection
    if shape.enabled? && !shape.selected?
      selectShape(shape, true)
    end

    # Are we dragging?
    if shape.selected? && shape.draggable?
      flags |= FLAG_TRYDRAG
    end

    flags |= FLAG_PRESSED
    return 1
  end
  return 0
end

#onLeftBtnRelease(sender, sel, evt) ⇒ Object

Left button release



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# File 'lib/fox16/canvas.rb', line 574

def onLeftBtnRelease(sender, sel, evt)
  flg = @flags
  if enabled?
    ungrab
    @flags |= FLAG_UPDATE
    @flags &= ~(FLAG_PRESSED|FLAG_TRYDRAG|FLAG_LASSO|FLAG_DODRAG)

    # First chance callback
    return 1 if target && target.handle(self, Fox.MKUINT(message, SEL_LEFTBUTTONRELEASE), evt) != 0

    # Was dragging
    if (flg & FLAG_DODRAG) != 0
      handle(self, Fox.MKUINT(0, SEL_ENDDRAG), evt)
      return 1
    end

    # Must have pressed
    if (flg & FLAG_PRESSED) != 0
      # Change selection
      if @currentShape && @currentShape.enabled?
        deselectShape(@currentShape, true)
      end

      # Generate clicked callbacks
      if evt.click_count == 1
        handle(self, Fox.MKUINT(0, SEL_CLICKED), @currentShape)
      elsif evt.click_count == 2
        handle(self, Fox.MKUINT(0, SEL_DOUBLECLICKED), @currentShape)
      elseif evt.click_count == 3
        handle(self, Fox.MKUINT(0, SEL_TRIPLECLICKED), @currentShape)
      end

      # Generate command callback only when clicked on item
      if @currentShape && @currentShape.enabled?
        handle(self, Fox.MKUINT(0, SEL_COMMAND), @currentShape)
      end
      return 1
    end
    return 0
  end
end

#onMotion(sender, sel, evt) ⇒ Object

Motion



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/fox16/canvas.rb', line 517

def onMotion(sender, sel, evt)
  # Drag and drop mode
  if (@flags & FLAG_DODRAG) != 0
    handle(self, Fox.MKUINT(0, SEL_DRAGGED), evt)
    return 1
  end

  # Tentative drag and drop
  if (@flags & FLAG_TRYDRAG) != 0
    if evt.moved?
      @flags &= ~FLAG_TRYDRAG
      if handle(this, Fox.MKUINT(0, SEL_BEGINDRAG), evt) != 0
        @flags |= FLAG_DODRAG
      end
    end
    return 1
  end
end

#onPaint(sender, sel, evt) ⇒ Object

Paint



505
506
507
508
509
510
511
512
513
514
# File 'lib/fox16/canvas.rb', line 505

def onPaint(sender, sel, evt)
  dc = FXDCWindow.new(self, evt)
  dc.foreground = backColor
  dc.fillRectangle(evt.rect.x, evt.rect.y, evt.rect.w, evt.rect.h)
  @scene.each do |shape|
    shape.draw(dc)
  end
  dc.end
  return 1
end

#onTripleClicked(sender, sel, ptr) ⇒ Object

Triple-clicked on canvas



632
633
634
# File 'lib/fox16/canvas.rb', line 632

def onTripleClicked(sender, sel, ptr)
  return target && target.handle(self, Fox.MKUINT(message, SEL_TRIPLECLICKED), ptr)
end

#selectShape(shape, notify = false) ⇒ Object

Select one shape



471
472
473
474
475
476
477
# File 'lib/fox16/canvas.rb', line 471

def selectShape(shape, notify=false)
  if @scene.include?(shape)
    @selectionPolicy.selectShape(shape, notify)
  else
    raise CanvasError
  end
end

#updateShape(shape) ⇒ Object

Repaint



438
439
440
441
442
443
444
# File 'lib/fox16/canvas.rb', line 438

def updateShape(shape)
  if @scene.include?(shape)
    update
  else
    raise CanvasError
  end
end