1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
# -*- coding: utf-8 -*-
require 'gtk3'
# Messageをレンダリングする際の、各パーツの座標の取得設定のためのモジュール
module Gdk::Coordinate
attr_accessor :width, :color, :icon_width, :icon_height, :icon_margin
CoordinateStruct = Struct.new(:main_icon, :main_text, :header_text, :reply)
DEPTH = Gdk::Visual.system.depth
SCALE = Gdk::Visual.system.screen.resolution / 100
class Region
extend Memoist
def initialize(x, y, w, h)
@x, @y, @w, @h = x, y, w, h end
def point_in?(mx, my)
left <= mx and mx <= right and top <= my and my <= bottom
end
[:x, :y, :w, :h].each{ |node|
define_method(node){
n = instance_eval("@#{node}")
if n.is_a?(Proc)
n.call
else
n end }
memoize node }
def bottom
y + h end
def right
x + w end
alias :left :x
alias :top :y
alias :width :w
alias :height :h
end
# 高さを計算して返す
def height
@height ||= Hash.new
@height[width] ||=
[(main_message.size[1] + header_left.size[1]) / Pango::SCALE, icon_height].max + icon_margin*2 + subparts_height
end
def mainpart_height
@mainpart_height ||= Hash.new
@mainpart_height[width] ||= height - subparts_height - icon_margin
end
def reset_height
if !@sid_modified && tree
@sid_modified ||= ssc_atonce(:modified, tree) do
tree.get_column(0).queue_resize
signal_handler_disconnect(@sid_modified) if signal_handler_is_connected?(@sid_modified)
@sid_modified = nil
false
end
@height&.clear
@mainpart_height&.clear
on_modify
end
self
rescue Gdk::MiraclePainter::DestroyedError
self
end
def width=(new)
if(@width != new)
@width = [new, 1].max
on_modify(true) end
new
end
def scale(val)
Gdk.scale(val)
end
protected
# 寸法の初期化
def coordinator(width)
@width, @color, @icon_width, @icon_height, @icon_margin = [width, 1].max, DEPTH, scale(48), scale(48), scale(2)
end
# 座標系を構造体にまとめて返す
def coordinate
@coordinate ||= CoordinateStruct.new(Region.new(icon_margin, # メインアイコン
icon_margin,
icon_width,
icon_height),
Region.new(icon_width + icon_margin * 2, # つぶやき本文
lambda{ pos.header_text.bottom },
width - icon_width - icon_margin * 4,
0),
Region.new(icon_width + icon_margin * 2, # ヘッダ
icon_margin,
width - (icon_width + icon_margin * 4),
lambda{ header_left.size[1] / Pango::SCALE })
)
end
alias :pos :coordinate
end
|