`
utensil
  • 浏览: 150553 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

wxRuby尝鲜

阅读更多

更新:终于成功弄到显示行号并自适应宽度了~

 

wxRuby最好玩的地方是它对wxWidgets多加了一层糖衣的语法。

 

安装了

 

gem install wxruby

 

之后,还要安装

 

gem install wx_sugar

 

这样子,我们不仅具有了一些:param_name => value这样的糖衣,可以使用attr_*族来暴露实例变量,还有了用do...end块来做layout和菜单的能力,比较爽的一点还有用block来处理事件。

 

简单讲解一下怎么使用:param_name => value这样的糖衣:比如wxRuby中某函数f(a,b),有了wx_sugar之后,就可以写f(:a=>5, :b=>'string')来调用。

 

下面这个小程序使用了wxAUI和wxStyledTextCtrl。用途是在非常长的文本文件中,同时搜索多个关键字,建立索引,包含上下文和定位的链接。

 

multi_searcher.rb

 

require 'rubygems'
require 'wx'
require 'wx_sugar/all'
require 'erb'
require 'ya2yaml'

include Wx

$KCODE = 'utf8'

def min(a, b)
	a >= b ? b : a
end

def max(a, b)
	a >= b ? a : b
end

class KeywordsPane < Panel
	def initialize(parent, options = {})
		super(parent, options.merge!(:size => [500, 500]))
		arrange_vertically do
			add @keyword = TextCtrl.new(self, :style => Wx::TE_MULTILINE, :size => [-1, 200]), :proportion => 1			
			
			arrange_horizontally do
				add @status = StaticText.new(self, :label => '', :size => [150, -1]), :proportion => 1
				add Button.new(self, :label => '查找') do |button|
					evt_button(button) do |event|
						#search
						get_parent.filecontent_pane.search(@keyword.get_value)
					end					
				end				
			end
		end		
	end
	
	attr_reader :keyword, :status
end

class ReportPane < Panel 
	def initialize(parent, options = {})
		super(parent, options.merge!(:size => [500, 500]))
		arrange_vertically do
			add @report = HtmlWindow.new(self), :proportion => 1 do |report|
				evt_html_link_clicked(report) do | event |
					position = event.get_link_info.get_href.sub(/position\:\/\//, '').split(',')
					get_parent.filecontent_pane.file.set_selection position[0].to_i, position[1].to_i
				end
			end
			add Button.new(self, :label => '保存报告') do |button|
				evt_button(button) do |event|
					ask4file = FileDialog.new(self, :style => FD_SAVE, :message => '保存报告', :wildcard => 'HTML files(*.html)|*.html')
					File.new(ask4file.get_path, 'w').write @report_text if ask4file.show_modal == Wx::ID_OK
				end
			end
		end
		
	end
	
	attr_reader :report
	attr_writer :report_text
end

class FilecontentPane < Panel 
	def initialize(parent, options = {})
		super(parent, options.merge!(:size => [500, 500]))
		
		arrange_vertically do
			add Button.new(self, :label => '打开要查找的文件') do |button|
				evt_button(button) do |event|
					#open file
					ask4file = FileDialog.new(self, :message => '打开要查找的文件')
					@file.load_file(ask4file.get_path) if ask4file.show_modal == Wx::ID_OK
					# auto adapt to the width of max line count
					@file.set_margin_width 0, @file.text_width(33, "__#{@file.get_line_count}") 
				end
			end
			add @file = StyledTextCtrl.new(self), :proportion => 1
			@file.set_margin_width 0, @file.text_width(33, '999') # show line number
			add StaticText.new(self, :label => '小提示:按住Ctrl+鼠标滚轮可以缩放文字大小')
		end
	end
	
	def search(keywords)
		keywords_a = keywords.split(', ')
		@search_result = {}
		file_length = @file.get_text_length
		
		beg_pos = 0		
		
		keywords_a.each do |k|
			@search_result[k] = []
			
			@file.goto_pos 0
			@file.search_anchor
			
			while((beg_pos = @file.search_next(0,k)) != -1)				
				
			    end_pos = beg_pos + k.length
				
				context = @file.get_text_range max(beg_pos - 520, 0), min(end_pos + 520, file_length)
				
				context.gsub! k, '<font size=7><b><u>\0</u></b></font>'	
				# coord = [@file.line_from_position(beg_pos), @file.get_column(beg_pos)]
				@search_result[k] << { :context => context, :position => [beg_pos, end_pos], :coord => [@file.line_from_position(beg_pos), @file.get_column(beg_pos)]}
				
				get_parent.keywords_pane.status.set_label "正在搜索#{k}...完成%3.2f%% ..." % (beg_pos * 100.0 / file_length)	
				
				@file.goto_pos end_pos + 1
				@file.search_anchor
			end
		end
		get_parent.keywords_pane.status.set_label '搜索完成,正在生成报告...'
		get_parent.report_pane.report.set_page report_text = ERB.new(File.read('report.html.erb'), 0, "%<>").result(binding)
			
		get_parent.keywords_pane.status.set_label ''
		get_parent.report_pane.report_text = report_text
	end	
	
	attr_reader :file
end

class MyFrame < Frame
   def initialize(title)
		super(nil, :title => title, :size => [800, 600])
		@mgr = AuiManager.new
		@mgr.set_managed_window(self)
		
		pi = AuiPaneInfo.new
		pi.set_name('keywords_pane').set_caption('在下面键入关键字,每个关键字之间用 一个逗号一个空格 隔开(例如keyword1, keyword2):')
		pi.top.set_maximize_button.set_close_button(false)		
		@mgr.add_pane(@keywords_pane = KeywordsPane.new(self), pi)
				
		pi = AuiPaneInfo.new
		pi.set_name('report_pane').set_caption('查找结果报告')
		pi.left.set_maximize_button.set_close_button(false)		
		@mgr.add_pane(@report_pane = ReportPane.new(self), pi)
		
		pi = AuiPaneInfo.new
		pi.set_name('filecontent_pane').set_caption('被查找文件')
		pi.center.set_maximize_button.set_close_button(false)
		@mgr.add_pane(@filecontent_pane = FilecontentPane.new(self), pi)	
		
		@mgr.update
   end
   
   attr_reader :keywords_pane, :report_pane, :filecontent_pane
end


class MyApp < App
   def on_init
     frame = MyFrame.new('多关键字搜索助手')
     frame.show
   end
end

a = MyApp.new
a.main_loop
 

 

程序中使用的erb模板report.html.erb:

 

<html>
<body>
<% @search_result.each do |keyword, info| %>
<dt><%= keyword %>(找到<%= info.length %>处)</dt>
<dd>
<% unless info.empty? %>
	<table border=1>
		<tr>
			<th>位置</th>
			<th>上下文</th>
		</tr>
		<% info.each do |v|%>
			<tr>
				<td><a href='position://<%= v[:position].join ',' %>'><%= "#{v[:coord][0]}行#{v[:coord][1]}列" %></a></td>
				<td><pre><%= v[:context] %></pre></td>
			</tr>
		<% end %>
	</table>
<% end %>
</dd>
<% end %>
</body>
</html>
 

 

0
0
分享到:
评论
1 楼 scncpb 2012-03-03  
引用
[img][/img][/url][url]

相关推荐

    wxruby文档

    wxruby文档离线包 wxRuby是一个开源的ruby界面开发包。它提供wxWidgets这个跨平台的C++界面框架的Ruby支持。wxWidgets是一个成熟的,拥 有众多特性的界面开发包,它使用本地控件来提供Linux、Windows和OS X本地的...

    wxruby windows安装包

    wxruby windows安装包 附带API

    mingw32-ruby-1.9.1-wxruby-2.0.1-setup.exe

    mingw32-ruby-1.9.1-wxruby-2.0.1-setup.exe

    wxruby and shadow

    NULL 博文链接:https://eroshn.iteye.com/blog/1075591

    map_creator:Reaktor样本图生成器

    没有wxruby依赖性的命令行版本。

    wxSU-开源

    wxSU是Google SketchUp的插件,它通过wxRuby的功能扩展了SketchUp API。 wxSU捆绑了wxRuby,以提供一个跨平台的纯Ruby解决方案,用于在SketchUp插件中实现GUI对话框。

    wxWidgets-3.1.1源码安装包

    函式庫本身使用C++語言開發,但也有其它不同程式語言的綁紮,例如:Python(wxPython)、Lua(wxlua)、Perl(wxPerl)、Ruby(wxRuby)、Smalltalk(wxSmalltalk)、Java(wx4j)、甚至是JavaScript(wxjs)等。...

    Script.NET脚本语言开发平台2.1.1版本

    刚刚发布的2.1.1版本新增了Ruby脚本的支持,可以支持Ruby脚本的开发、调试、生成可执行文件、控制台等功能,可以支持wxRuby、RubyTk等界面库,目前还不支持Rails的调试,后续版本会不断完善。 下载地址和详细介绍请...

    FarPy GUIE-开源

    GUIE(GUI编辑器)为许多动态语言提供了一个简单的WYSIWYG GUI编辑器。 目前,FarPy支持wxPython,wxRuby,wxPerl和IronPython!

    Lua开发包 for Script.NET V2.1.1

    刚刚发布的2.1.1版本新增了Ruby脚本的支持,可以支持Ruby脚本的开发、调试、生成可执行文件、控制台等功能,可以支持wxRuby、RubyTk等界面库,目前还不支持Rails的调试,后续版本会不断完善。 下载地址和详细介绍请...

    Tcl开发包 for Script.NET V2.1.1

    刚刚发布的2.1.1版本新增了Ruby脚本的支持,可以支持Ruby脚本的开发、调试、生成可执行文件、控制台等功能,可以支持wxRuby、RubyTk等界面库,目前还不支持Rails的调试,后续版本会不断完善。 下载地址和详细介绍请...

    Ruby开发包 for Script.NET V2.1.1

    刚刚发布的2.1.1版本新增了Ruby脚本的支持,可以支持Ruby脚本的开发、调试、生成可执行文件、控制台等功能,可以支持wxRuby、RubyTk等界面库,目前还不支持Rails的调试,后续版本会不断完善。 下载地址和详细介绍请...

    NSIS开发包 for Script.NET V2.1.1

    刚刚发布的2.1.1版本新增了Ruby脚本的支持,可以支持Ruby脚本的开发、调试、生成可执行文件、控制台等功能,可以支持wxRuby、RubyTk等界面库,目前还不支持Rails的调试,后续版本会不断完善。 下载地址和详细介绍请...

Global site tag (gtag.js) - Google Analytics