1
23
2012
0

WxRuby高亮代码(StyledTextCtrl)辅助

WxRuby真是我见过最糟糕的UI库……没有之一,文档写的不全,常数各种难找,有的甚至去翻了相应库的源代码才找到= =好在范例还是挺给力的。

 使用的时候只要 Helper.createStyledTextCtrl(self)即可。部份代码参照了http://wxruby.rubyforge.org/svn/trunk/wxruby/samples/text/scintilla.rb

这个范例。

以及

https://metacpan.org/source/AZAWAWI/Wx-Scintilla-0.20/wx-scintilla2/src/scintilla/src/LexRuby.cxx

LuxRuby的源代码。

module Helper
  module_function
  def createStyledTextCtrl(window)
    font = Font.new(12, TELETYPE, NORMAL, NORMAL,false,"Lucida Console")
    @sci = StyledTextCtrl.new(window)  
    @sci.set_edge_mode(STC_EDGE_LINE)
    @sci.set_margin_width 0, @sci.text_width(STC_STYLE_LINENUMBER, '_9999')
    @sci.set_tab_width(2)
    @sci.set_use_tabs(false)
    @sci.set_tab_indents(true)
    @sci.set_back_space_un_indents(true)
    @sci.set_indent(2)
    @sci.set_edge_column(80)
    @sci.style_set_font(STC_STYLE_DEFAULT, font);
    @sci.style_set_foreground(STC_STYLE_DEFAULT, BLACK);
    @sci.style_set_background(STC_STYLE_DEFAULT, WHITE);

    @sci.style_clear_all()
    @sci.ensure_caret_visible();
    @sci.set_caret_line_visible(true)
    @sci.set_caret_line_background(Colour.new(200,200,248))
    @sci.set_caret_line_back_alpha(40)
    @sci.style_set_foreground(STC_STYLE_LINENUMBER, LIGHT_GREY);
    @sci.style_set_background(STC_STYLE_LINENUMBER, WHITE);
    @sci.style_set_foreground(STC_STYLE_INDENTGUIDE, LIGHT_GREY);
    @sci.set_lexer(STC_LEX_RUBY)
    @sci.style_set_foreground(0, Colour.new(0,0,0))           #SCE_RB_DEFAULT
    @sci.style_set_foreground(2,Colour.new(0,128,0))          #SCE_RB_COMMENTLINE
    @sci.style_set_foreground(3, Colour.new(0,128,0))         #SCE_RB_POD
    @sci.style_set_foreground(4, Colour.new(128,0,0))         #SCE_RB_NUMBER
    @sci.style_set_foreground(5, BLUE)                        #SCE_RB_WORD
    @sci.style_set_foreground(6, Colour.new(128,0,128))       #SCE_RB_STRING
    @sci.style_set_foreground(7, Colour.new(128,0,128))       #SCE_RB_CHARACTER
    @sci.style_set_foreground(8, Colour.new(0,0,128))         #SCE_RB_CLASSNAME
    @sci.style_set_foreground(9, Colour.new(0,0,128))         #SCE_RB_DEFNAME
    @sci.style_set_foreground(10, Colour.new(0,128,192))      #SCE_RB_OPERATOR
    #@sci.style_set_bold(11,true)                              #SCE_RB_IDENTIFIER 局部变量 函数。。
    #@sci.style_set_italic(11,true)
    @sci.style_set_foreground(12,Colour.new(128,0,128))       #SCE_RB_REGEX
    @sci.style_set_italic(13,true)                            #SCE_RB_GLOBAL
    @sci.style_set_bold(13,true)
    @sci.style_set_foreground(14,Colour.new(192,96,0))        #SCE_RB_SYMBOL
    @sci.style_set_foreground(15, Colour.new(0,0,128))        #SCE_RB_MODULE_NAME
    @sci.style_set_italic(16,true)                            #SCE_RB_INSTANCE_VAR
    #@sci.style_set_bold(16,true)                             #SCE_RB_INSTANCE_VAR
    @sci.style_set_italic(17,true)                            #SCE_RB_CLASS_VAR
    @sci.style_set_bold(17,true)                              #SCE_RB_CLASS_VAR
    @sci.style_set_foreground(18, Colour.new(128,0,128))      #SCE_RB_BACKTICKS
    #SCE_RB_DATASECTION
    @sci.style_set_foreground(19, Colour.new(0,0,0))          #SCE_RB_DATASECTION
    @sci.style_set_foreground(20, Colour.new(128,0,128))      #SCE_RB_HERE_DELIM
    @sci.style_set_foreground(21, Colour.new(128,0,128))      #SCE_RB_HERE_DELIM Q
    @sci.style_set_foreground(22, Colour.new(128,0,128))      #SCE_RB_HERE_DELIM QQ
    @sci.style_set_foreground(23, Colour.new(128,0,128))      #SCE_RB_HERE_DELIM QX
    @sci.style_set_foreground(24, Colour.new(128,0,128))      #SCE_RB_STRING Q  
    @sci.style_set_foreground(25, Colour.new(128,0,128))      #SCE_RB_STRING QQ
    @sci.style_set_foreground(26, Colour.new(128,0,128))      #SCE_RB_STRING QX
    @sci.style_set_foreground(27, Colour.new(128,0,128))      #SCE_RB_STRING QR
    @sci.style_set_foreground(28, Colour.new(128,0,128))      #SCE_RB_STRING QW 
    @sci.style_set_foreground(29, BLUE)                       #SCE_RB_WORD_DEMOTED
    @sci.style_set_foreground(30, BLUE)                       #SCE_RB_STDIN
    @sci.style_set_foreground(31, BLUE)                       #SCE_RB_STDOUT
    @sci.style_set_foreground(40, BLUE)                       #SCE_RB_STDERR
    
    #SCE_RB_UPPER_BOUND
    @sci.set_key_words(0, "attr_accessor attr_reader attr_writer module_function begin break elsif module retry unless end case next return until class ensure nil self when def false not super while alias defined? for or then yield and do if redo true else in rescue undef")
    
    
    @sci.set_property("fold","1")
    @sci.set_property("fold.compact", "0")
    @sci.set_property("fold.comment", "1")
    @sci.set_property("fold.preprocessor", "1")
    
    @sci.set_margin_width(1, 0)
    @sci.set_margin_type(1, STC_MARGIN_SYMBOL)
    @sci.set_margin_mask(1, STC_MASK_FOLDERS)
    @sci.set_margin_width(1, 20)

    @sci.marker_define(STC_MARKNUM_FOLDER, STC_MARK_PLUS)
    @sci.marker_define(STC_MARKNUM_FOLDEROPEN, STC_MARK_MINUS)
    @sci.marker_define(STC_MARKNUM_FOLDEREND, STC_MARK_EMPTY)
    @sci.marker_define(STC_MARKNUM_FOLDERMIDTAIL, STC_MARK_EMPTY)
    @sci.marker_define(STC_MARKNUM_FOLDEROPENMID, STC_MARK_EMPTY)
    @sci.marker_define(STC_MARKNUM_FOLDERSUB, STC_MARK_EMPTY)
    @sci.marker_define(STC_MARKNUM_FOLDERTAIL, STC_MARK_EMPTY)
    @sci.set_fold_flags(16)

    @sci.set_margin_sensitive(1,1)

    window.evt_stc_charadded(@sci.get_id) {|evt| onCharadded(evt)}
    window.evt_stc_marginclick(@sci.get_id) {|evt| onMarginClick(evt)}
  end

  def onCharadded(evt)
    chr =  evt.get_key
    curr_line = @sci.get_current_line

    if(chr == 13)
        if curr_line > 0
          line_ind = @sci.get_line_indentation(curr_line - 1)
          if line_ind > 0
            @sci.set_line_indentation(curr_line, line_ind)
            @sci.goto_pos(@sci.position_from_line(curr_line)+line_ind)
          end
        end
    end
  end

  def onMarginClick(evt)
    line_num = @sci.line_from_position(evt.get_position)
    margin = evt.get_margin

    if(margin == 1)
      @sci.toggle_fold(line_num)
    end  
  end
end
Category: RMPlus | Tags: | Read Count: 1729

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com