Class: Wx::SF::Thumbnail

Inherits:
Panel
  • Object
show all
Defined in:
lib/wx/shapes/thumbnail.rb

Defined Under Namespace

Modules: ID Classes: THUMBSTYLE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Thumbnail

Returns a new instance of Thumbnail.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wx/shapes/thumbnail.rb', line 24

def initialize(parent)
  super(parent, Wx::ID_ANY, size: [200, 150], style: Wx::TAB_TRAVERSAL | Wx::FULL_REPAINT_ON_RESIZE)
  set_extra_style(Wx::WS_EX_BLOCK_EVENTS)
  set_size_hints([10, 10])

  @canvas = nil
  @scale = 1.0
  @thumb_style = THUMBSTYLE::SHOW_ELEMENTS | THUMBSTYLE::SHOW_CONNECTIONS
  @prev_mouse_pos = nil

  @update_timer = Wx::Timer.new(self, ID::UPDATETIMER)

  set_background_style(Wx::BG_STYLE_PAINT)

  evt_paint :_on_paint
  evt_erase_background :_on_erase_background
  evt_motion :_on_mouse_move
  evt_left_down :_on_left_down
  evt_right_down :_on_right_down
  evt_timer(ID::UPDATETIMER, :_on_timer)
  evt_update_ui(ID::M_SHOWELEMENTS, :_on_update_show_elements)
  evt_update_ui(ID::M_SHOWCONNECTIONS, :_on_update_show_connections)
  evt_menu(ID::M_SHOWELEMENTS, :_on_show_elements)
  evt_menu(ID::M_SHOWCONNECTIONS, :_on_show_connections)
end

Instance Attribute Details

#thumb_styleObject

Access (get/set) thumbnail style.



51
52
53
# File 'lib/wx/shapes/thumbnail.rb', line 51

def thumb_style
  @thumb_style
end

Instance Method Details

#_on_paint(_event) ⇒ Object (protected)

Internally used event handler.

Parameters:

  • _event (Wx::PaintEvent)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/wx/shapes/thumbnail.rb', line 94

def _on_paint(_event)
  paint_buffered do |dc|
    # clear background
    dc.set_background(Wx::Brush.new(Wx::Colour.new(150, 150, 150)))
    dc.clear
    
    if @canvas
      sz_canvas = @canvas.get_client_size
      vsx, vsy = @canvas.get_virtual_size
      sz_canvas_offset = get_canvas_offset
      sz_thumb = get_client_size
      
      # scale and copy bitmap to DC
      cx = vsx.to_f
      cy = vsy.to_f
      tx = sz_thumb.x
      ty = sz_thumb.y
      
      if (tx/ty) > (cx/cy)
        @scale = ty/cy
      else
        @scale = tx/cx
      end
  
      # draw virtual canvas area
      dc.with_pen(Wx::WHITE_PEN) do
        dc.with_brush(Wx::Brush.new(Wx::Colour.new(240, 240, 240))) do
          dc.draw_rectangle(0, 0, (cx*@scale).to_i, (cy*@scale).to_i)

          # draw top level shapes
          Wx::ScaledDC.draw_on(dc, @scale * @canvas.get_scale)  do |sdc|
            draw_content(sdc)
          end

          # draw canvas client area
          dc.set_pen(Wx::RED_PEN)
          dc.set_brush(Wx::TRANSPARENT_BRUSH)
          dc.draw_rectangle((sz_canvas_offset.x.to_f*@scale).to_i, (sz_canvas_offset.y.to_f*@scale).to_i, (sz_canvas.x.to_f*@scale).to_i, (sz_canvas.y.to_f*@scale).to_i)
        end
      end
    end
    
    dc.set_background(Wx::NULL_BRUSH)
  end
end

#draw_content(dc) ⇒ Object

Parameters:

  • dc (Wx::DC)

    Reference to output device context



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/wx/shapes/thumbnail.rb', line 69

def draw_content(dc)
  # HINT: overload it for custom actions...
  bmp_pen = nil
  @canvas.get_diagram.get_all_shapes.each do |shape|
    if (@thumb_style & THUMBSTYLE::SHOW_CONNECTIONS) != 0 && shape.is_a?(LineShape)
      shape.draw(dc, WITHOUTCHILDREN)
    elsif (@thumb_style & THUMBSTYLE::SHOW_ELEMENTS) != 0
      if shape.is_a?(BitmapShape)
        bmp_pen ||= Wx::Pen.new(Wx::BLACK, 1, Wx::PENSTYLE_DOT)
        dc.with_pen(bmp_pen) do
          dc.with_brush(Wx::WHITE_BRUSH) do
            dc.draw_rectangle(shape.get_bounding_box)
          end
        end
      elsif !shape.is_a?(LineShape)
        shape.draw(dc, WITHOUTCHILDREN)
      end
    end
  end
end

#get_canvas_offsetWx::Size (protected)

Get offset (view start) of managed shape canvas defined in pixels.

Returns:

  • (Wx::Size)

    Canvas offset in pixels



142
143
144
145
146
147
148
149
150
# File 'lib/wx/shapes/thumbnail.rb', line 142

def get_canvas_offset
  if @canvas
    ux, uy = @canvas.get_scroll_pixels_per_unit
    offset_x, offset_y = @canvas.get_view_start

    return Wx::Size.new(offset_x*ux, offset_y*uy)
  end
  Wx::Size.new
end

#set_canvas(canvas) ⇒ Object

Set canvas managed by the thumbnail.

Parameters:



55
56
57
58
59
60
61
62
63
64
# File 'lib/wx/shapes/thumbnail.rb', line 55

def set_canvas(canvas)
  @canvas = canvas

  if @canvas
    @update_timer.start(100)
  else
    @update_timer.stop
    refresh(false)
  end
end