001 #!ruby -Ks 002 003 require 'phi' 004 require 'dialogs' 005 require 'editor' 006 007 DEFAULT_TITLE = 'メモ帳' 008 009 class AboutDialog < Phi::Form 010 011 def initialize 012 super 013 self.border_style = Phi::BS_DIALOG 014 self.width = 300 015 self.height = 180 016 self.caption = 'このソフトについて' 017 018 margin = 15 019 w = self.width - margin * 2 020 021 button = Phi::Button.new(self, :button1, 'OK') 022 button.modal_result = Phi::MR_OK 023 button.width = w 024 button.left = margin 025 button.top = self.height - margin * 3 - button.height 026 027 label = Phi::Label.new(self, :label1) 028 label.caption = "このスクリプトはフリーソフトウェアです。\r\nご自由にお使い下さい。" 029 label.width = w 030 label.height = button.top - margin * 2 031 label.left = margin 032 label.top = margin 033 end 034 035 end 036 037 def error_dlg(msg) 038 result = Phi::message_dlg(msg, Phi::MT_ERROR, [Phi::MB_YES, Phi::MB_NO, Phi::MB_CANCEL], 0) 039 return result 040 end 041 042 def confirm_save 043 result = error_dlg('ファイルが変更されています。保存しますか?') 044 case result 045 when Phi::MR_YES 046 save_file 047 return true 048 when Phi::MR_NO 049 return true 050 when Phi::MR_CANCEL 051 return false 052 end 053 end 054 055 def save_file 056 if $filename.nil? 057 dlg = Phi::SaveDialog.new 058 dlg.filter = 'すべてのファイル(*.*)|*|テキスト文書(*.txt)|*.txt|' 059 if dlg.execute 060 $filename = dlg.file_name 061 end 062 end 063 064 if $filename and Phi::SCREEN.form1.editor1.modified 065 Phi::SCREEN.form1.editor1.lines.save($filename) 066 Phi::SCREEN.form1.editor1.modified = false 067 Phi::SCREEN.form1.caption = DEFAULT_TITLE + ' ' + $filename 068 end 069 end 070 071 def before_exit 072 if Phi::SCREEN.form1.editor1.modified 073 result = confirm_save 074 return result 075 end 076 return true 077 end 078 079 080 081 form = Phi::Form.new(:form1, DEFAULT_TITLE) 082 editor = Phi::Editor.new(form, :editor1, '') 083 editor.align = Phi::AL_CLIENT 084 editor.lines.clear 085 editor.modified = false 086 $filename = nil 087 form.width = 500 088 form.height = 400 089 090 Phi.new_menu(form, :menu1, [ 091 menu_file = Phi.new_item('ファイル(&F)', '', :mi_file).add( 092 menu_open = Phi.new_item('開く(&O)', 'CTRL+O', :mi_open), 093 menu_save = Phi.new_item('保存(&S)', 'CTRL+S', :mi_save), 094 Phi.new_line, 095 menu_exit = Phi.new_item('終了(&X)', '', :mi_exit) 096 ), 097 menu_edit = Phi.new_item('編集(&E)', '', :mi_edit).add( 098 menu_undo = Phi.new_item('元に戻す(&U)', 'Ctrl+Z', :mi_undo), 099 menu_redo = Phi.new_item('やり直し(&R)', 'Ctrl+A', :mi_redo), 100 Phi.new_line, 101 menu_cut = Phi.new_item('切り取り(&T)', 'Ctrl+X', :mi_cut), 102 menu_copy = Phi.new_item('コピー(&C)', 'Ctrl+C', :mi_copy), 103 menu_paste = Phi.new_item('貼り付け(&P)', 'Ctrl+V', :mi_paste), 104 menu_delete = Phi.new_item('削除(&D)', '', :mi_delete), 105 Phi.new_line, 106 menu_select_all = Phi.new_item('すべて選択(&A)', '', :mi_select_all) 107 ), 108 menu_help = Phi.new_item('ヘルプ(&H)', '', :mi_help).add( 109 menu_about = Phi.new_item('このソフトについて', '', :mi_about) 110 ) 111 ]) 112 113 # menu 114 menu_open.on_click = proc do 115 dlg = Phi::OpenDialog.new 116 dlg.filter = 'テキスト文書(*.txt)|*.txt|すべてのファイル(*.*)|*|' 117 if dlg.execute 118 editor.lines.load(dlg.file_name) 119 editor.modified = false 120 end 121 end 122 123 menu_save.on_click = proc do 124 if editor.modified 125 save_file 126 end 127 end 128 129 menu_exit.on_click = proc do 130 form.close 131 end 132 133 menu_undo.on_click = proc do 134 if editor.can_undo 135 editor.undo 136 end 137 end 138 139 menu_redo.on_click = proc do 140 if editor.can_redo 141 editor.redo 142 end 143 end 144 145 menu_cut.on_click = proc do 146 editor.cut_to_clipboard 147 end 148 149 menu_copy.on_click = proc do 150 editor.copy_to_clipboard 151 end 152 153 menu_paste.on_click = proc do 154 editor.paste_from_clipboard 155 end 156 157 menu_delete.on_click = proc do 158 editor.clear_selection 159 end 160 161 menu_select_all.on_click = proc do 162 editor.select_all 163 end 164 165 menu_about.on_click = proc do 166 dlg = AboutDialog.new 167 dlg.show_modal 168 end 169 170 # other control 171 form.on_close_query = proc do 172 before_exit 173 end 174 175 menu_file.on_click = proc do 176 menu_save.enabled = editor.modified 177 end 178 179 menu_edit.on_click = proc do 180 menu_undo.enabled = editor.can_undo 181 menu_redo.enabled = editor.can_redo 182 menu_cut.enabled = editor.selected 183 menu_copy.enabled = editor.selected 184 menu_delete.enabled = editor.selected 185 menu_paste.enabled = Phi::CLIPBOARD.has_format?(Win::CF_TEXT) 186 end 187 188 form.show 189 Phi::mainloop
「#!ruby」
については以前も述べたようにおまじないだと思って構わないです。