root/lang/ruby/ruwin/lib/ruwin/edit_field/rich.rb @ 28996

Revision 28996, 4.3 kB (checked in by isaisstillalive, 4 years ago)
  • 段落フォーマットと区別するため、Rich::FormatをRich::Format::Charに変更。
Line 
1module Ruwin
2  class EditField
3    class Rich < EditField
4      class Selection < EditField::Selection
5        attr_reader :format
6       
7        def initialize *args
8          super
9          @format = Format::Char.new self, SCF_SELECTION
10        end
11       
12        def string
13          result = "\0"*length
14          sendMessage EM_GETSELTEXT, 0, result
15          result
16        end
17      end
18     
19      module Format
20        class Char
21          include Ruwin
22          include Const::EditField
23         
24          def initialize control, extent_flag
25            @control = control
26            @extent_flag = extent_flag
27          end
28         
29          {
30            :bold       => %w{CFM_BOLD      CFE_BOLD},
31            :italic     => %w{CFM_ITALIC    CFE_ITALIC},
32            :underline  => %w{CFM_UNDERLINE CFE_UNDERLINE},
33            :strikeout  => %w{CFM_STRIKEOUT CFE_STRIKEOUT},
34            :protected  => %w{CFM_PROTECTED CFE_PROTECTED},
35          }.each do |option, (mask, effect)|
36            class_eval <<-"END"
37              def #{option}= enable
38                set_char_format(#{mask}, :effects => (enable ? #{effect} : 0))
39              end
40             
41              def #{option}
42                get_char_format(#{mask})[2]&#{effect} == #{effect}
43              end
44            END
45          end
46         
47          {
48            :height     => %w{CFM_SIZE    twip},
49            :offset     => %w{CFM_OFFSET  twip},
50            :color      => %w{CFM_COLOR   color},
51          }.each do |option, (mask, variable)|
52            class_eval <<-"END"
53              def #{option}= #{variable}
54                set_char_format(#{mask}, :#{option} => #{variable})
55              end
56            END
57          end
58         
59          def height
60            SIGNEDLONG(get_char_format(CFM_SIZE)[3])
61          end
62         
63          def offset
64            SIGNEDLONG(get_char_format(CFM_OFFSET)[4])
65          end
66         
67          def color
68            get_char_format(CFM_OFFSET)[5]
69          end
70         
71         
72          private
73          CHARFORMAT = "LLLLLLCCU"
74         
75          def set_char_format mask, options
76            @control.sendMessage EM_SETCHARFORMAT, @extent_flag, char_format(mask, options)
77          end
78         
79          def get_char_format mask
80            charformat = char_format mask
81            @control.sendMessage EM_GETCHARFORMAT, @extent_flag, charformat
82            charformat.unpack CHARFORMAT
83          end
84         
85          def char_format mask, options = {}
86            [
87              60,
88              mask,
89              options[:effects] || 0,
90              options[:height]  || 0,
91              options[:offset]  || 0,
92              options[:color]   || 0,
93              0,
94              0,
95              0,
96            ].pack CHARFORMAT
97          end
98        end
99      end
100     
101      SELECTION_CLASS = Selection
102     
103      LoadLibrary = Win32API.new("kernel32","LoadLibrary",["P"],"I")
104      if LoadLibrary.call("riched20") != 0
105        CLASS_NAME = "RICHEDIT20A"
106      elsif LoadLibrary.call("riched32") != 0
107        CLASS_NAME = "RICHEDIT"
108      end
109     
110      property_accessor :background_color => nil, :color => nil
111      attr_reader :default
112     
113      def initialize
114        super
115       
116        self.background_color = @proparty[:background_color] if @proparty[:background_color]
117       
118        @default = Format::Char.new self, SCF_DEFAULT
119        @default.color = @proparty[:color] if @proparty[:color]
120      end
121     
122     
123      def background_color= color
124        if color
125          sendMessage EM_SETBKGNDCOLOR, 0, color
126        else
127          sendMessage EM_SETBKGNDCOLOR, 1, 0
128        end
129      end
130     
131     
132      def wm_notify msg
133        hwndFrom, idFrom, code = *SWin::Application.cstruct2array(msg.lParam, "UUU")
134        case code
135        when EN_MSGFILTER
136          en_msgfilter msg
137        end
138      end
139     
140      def en_msgfilter msg
141        nmhdr1, nmhdr2, nmhdr2, msg, wParam, lParam = *SWin::Application.cstruct2array(msg.lParam, "UUUUUU")
142        case msg
143        when WM_KEYDOWN
144          [:keydown, wParam]
145        end
146      end
147     
148      class Multiline < Rich
149        STYLE = EditField::Multiline::STYLE
150      end
151    end
152  end
153end
Note: See TracBrowser for help on using the browser.