#!/usr/env ruby -Ks #-- MemoDialog.rb require 'phi' require 'jpeg' begin ; require "fold" ; rescue Exception ; end =begin MemoDialog モジュール キャプション、プロンプト、メモ入力窓、イメージ、ボタン指定が可能なダイアログ。 等倍フォントを使用する。 引数: キャプション(String)とプロンプト(String)は必須。 メモ入力窓は、memo_text=nil なら出ない。memo_text="" なら出る。 ボタンはデフォルトは「:ok」。指定する場合にはボタンのidもしくは文字列の配列で指定する。 イメージはパスで指定する。image_pth=nilなら出ない。 戻り値は: メモ入力窓が出た場合には[mr、メモ窓の内容] メモ入力窓が出ない場合にはmr mrは、ボタンidで指定した場合には Phi::MR_XX、文字列で指定した場合には101+配置番号。 =end module MemoDialog module_function def execute(caption, prompt_text, memo_text=nil, btn_ids=[:ok], image_path=nil,*options) font_size=12 font_name="fixed" control_class=nil while (opt = options.shift) case opt when Numeric font_size = opt when String font_name = opt when Class control_class = opt ##when Hash else raise "invalid option #{opt.inspect}" end end btn_ids = [:ok] unless btn_ids form = Phi::Form.new :note, caption form.font.name = font_name form.font.size = font_size form.font.pitch = Phi::FP_FIXED form.auto_scroll = false form.position = Phi::PO_SCREEN_CENTER if image_path image = Phi::Image.new form, :image image.picture.load(image_path) image.set_bounds(8,8,image.picture.width,image.picture.height) else image = Phi::Label.new form, :image, '' image.set_bounds(0,0,0,0) end prompt = Phi::Memo.new form, :prompt prompt.set_bounds(image.right + 0,0,0,0) prompt.border_style = 0 prompt.color = prompt.parent.color prompt.read_only = true prompt.tab_stop = false prompt.word_wrap = false if defined? "".fold list = [] prompt_text.split(/\r?\n/).each{|line| line.fold(40){|i| list.push i}} else list = prompt_text.split(/\r?\n/) end unless list.empty? prompt.text = list.join("\r\n") prompt.left+= 8 prompt.top+= 8 prompt.width = list.collect{|i| form.canvas.text_width(i)}.max n = 0 list.each{|i| n+= form.canvas.text_height(i)} prompt.height = n end case memo_text when Array control_class ||= Phi::ListBox memo = control_class.new form, :memo memo.items.clear memo.text = "" if defined? memo.text memo_text.each{|n| memo.items.add(n.chomp) } memo.left = 8 memo.top = [image.bottom, prompt.bottom].max + 8 memo.on_dbl_click = proc{ form.modal_result = Phi::MR_OK} when String control_class ||= Phi::Memo memo = control_class.new form, :memo, memo_text.gsub(/\r?\n/, "\r\n") memo.left = 8 memo.top = [image.bottom, prompt.bottom].max + 8 memo.scroll_bars = Phi::SS_VERTICAL if defined? memo.scroll_bars else memo = Phi::Label.new form, :memo, '' memo.set_bounds(8,[image.bottom, prompt.bottom].max,0,0) end panel = Phi::Panel.new form, :panel, '' panel.align = Phi::AL_BOTTOM panel.bevel_outer = Phi::BV_NONE btns = [] btn_ids.each_with_index do |id,index| btn = Phi::Button.new(panel) cap = (id.is_a? Symbol) ? id.to_s.upcase : id.to_s btn.caption = cap name = "MR_#{cap}" btn.modal_result = Phi.const_defined?(name) ? Phi.const_get(name) : index+101 btn.default = [:ok, :yes].include?(id) btn.cancel = [:cancel, :no].include?(id) btn.width = [btn.width, form.canvas.text_width(cap)+8].max btns.push btn end btns_space = 8 unless btns.empty? btns_height = btns.collect{|i| i.height}.max panel.height = btns_height + btns_space*2 else btns_height = 0 panel.height = btns_height + btns_space end form.client_height = memo.bottom + panel.height btns_width = 0 btn_space = 8 unless btns.empty? btns.each{|btn| btns_width+= btn.width} btns_width+= btn_space*(btns.size-1) end memo.width = [prompt.right - memo.left, btns_width, caption.size*form.font.size/2+80].max form.client_width = [prompt.right + 8, btns_width + btns_space*2, memo.width+20 ].max unless btns.empty? btns[0].left = panel.width - btns_width - btns_space btns[0].top = 8 btns[0].anchors = [Phi::AK_TOP, Phi::AK_RIGHT] end for i in 1...btns.size btns[i].left = btns[i-1].right + btn_space btns[i].top = 8 btns[i].anchors = [Phi::AK_TOP, Phi::AK_RIGHT] end prompt.anchors = [Phi::AK_LEFT, Phi::AK_TOP] memo.anchors = [Phi::AK_LEFT, Phi::AK_TOP, Phi::AK_RIGHT, Phi::AK_BOTTOM] mr = form.show_modal case memo_text when Array return mr, nil if mr == Phi::MR_CANCEL || memo.item_index < 0 return mr, memo.items[memo.item_index] when String return mr, memo.lines.text if defined? memo.lines return mr, memo.text else return mr end end end # module if __FILE__ == $0 image = File.dirname(File.dirname(Phi::APPLICATION.exe_name))+"/sample/arima.ico" ##image = 'c:/program files/apollo/sample/arima.ico' caption = "Rubyの冒険 - はじめに" text = <