電柱日報

日々の由無し事

趣味の世界

ここ2〜3日,仕事が終わってからそのままIronRubyで遊んでいて,帰宅が22時過ぎとかになっております。
今日は一度大学の夜ゼミへ行ってから職場へ戻って試行錯誤してました。
期待のLDAP周りについては,使えそうな雰囲気はあるものの,System.DirectorySearvices.Protocolsをrequireする際のStrong Nameが判らずペンディング中。
メニューやツールバーの定型処理を組み込んだカスタムフォームは今んとここんな感じです。

require 'mscorlib'
require 'System.Windows.Forms'

include System::Windows::Forms

module AT
	
	class AtForm < Form
		
		# アプリケーション実行
		def self.run()
			Application.EnableVisualStyles()
			Application.Run(self.new)
		end
		
		attr_reader(:menu, :top)
		
		# コンストラクタ
		# _title: ウィンドウタイトル
		def initialize(_title = 'IronRuby')
			# ウィンドウタイトル
			self.Text = _title.to_s
			# ウィンドウ上部パネル
			@top = ToolStripPanel.new()
			self.Controls.Add(@top)
			# ツールバーエリア
			@panel_top = ToolStripPanel.new()
			@panel_top.Dock = DockStyle.Top
			self.Controls.Add(@panel_top)
			# メニューバー
			@menu = MenuStrip.new()
			@menu.Dock = DockStyle.Top
			self.Controls.Add(@menu)
			self.MainMenuStrip = @menu
			# アクション
			@action = {}
			# コンポーネント初期化
			component_init()
		end
		
		# コンポーネント初期化メソッド
		# (サブクラスで実装)
		def component_init()
		end
		
		# アクション追加
		# _symbol: アクション識別シンボル
		# _text: キャプション文字列
		def add_action(_symbol, _text)
			raise "#{_symbol} is not a Symbol." unless _symbol.is_a?(Symbol)
			block = Proc.new
			@action[_symbol] = AtAction.new(_text.to_s, &block)
		end
		
		# メニュー項目追加
		# _id: 識別情報
		# _parent: 親メニュー項目
		def add_menu(_id, _parent = nil)
			mi = ToolStripMenuItem.new()
			
			if @action[_id] then
				# アクション定義があれば設定
				@action[_id].add_control(mi)
			else
				# なければTextだけ指定
				mi.Text = _id.to_s
			end
			
			if _parent then
				# 親メニューが指定されていればそちらへ追加
				_parent.DropDownItems.Add(mi)
			else
				# なければMenuStripに直接追加
				@menu.Items.Add(mi)
			end
			
			return mi
			
		end
		
		# ツールバー追加
		def add_tool_bar()
			ts = ToolStrip.new()
			@panel_top.Controls.Add(ts)
			return ts
		end
		
		# ツールバーボタン追加
		def add_tool_bar_button(_symbol, _tool_bar)
			btn = ToolStripButton.new()
			@action[_symbol].add_control(btn)
			_tool_bar.Items.add(btn)
			return btn
		end
		
	end
	
	# アクション定義クラス
	class AtAction
	
		attr_reader(:click, :text, :shortcut, :image, :enabled, :checked)
	
		def initialize(_text)
			# クリックイベントハンドラ
			@click = Proc.new
			# 表示テキスト
			@text = _text.to_s
			# ショートカットキー
			@shortcut = Keys.None
			# イメージ
			@image = nil
			# 有効/無効フラグ
			@enabled = true
			# チェックフラグ
			@checked = false
			# 配下コントロール
			@controls = []
		end
		
		# 配下コントロール追加
		def add_control(_control)
			sync_item(_control, true)
			@controls << _control
		end
		
		# 全コントロール同期
		def sync_all()
			@controls.each do |control|
				sync_item(control)
			end
		end
		
		# コントロール同期
		def sync_item(_control, _sync_proc = false)
			_control.Text = @text
			_control.ShortcutKeys = @shortcut if _control.class.method_defined?('ShortcutKeys=')
			_control.Image = @image
			_control.Enabled = @enabled
			_control.Checked = @checked
			_control.Click(&@click) if _sync_proc
		end
		
		# アクション有効フラグ設定
		def enabled=(_enabled)
			@enabled = _enabled ? true : false
			sync_all()
		end
		
		# アクションチェックフラグ設定
		def checked=(_checked)
			@checked = _checked ? true : false
			sync_all()
		end
		
		# ショートカットキー設定
		def shortcut=(_shortcut)
			@shortcut = _shortcut
			sync_all()
		end
		
	end
	
end

if $0 == __FILE__ then

# 動作テスト部
class TestForm < AT::AtForm

	def initialize()
		super('Test Form')
	end
	
	def component_init()
		# 終了アクション
		add_action(:quit, '終了(&X)') { |sender, e| self.close }
		@action[:quit].shortcut = Keys.Control | Keys.Q
		
		# チェックアクション
		add_action(:check, 'チェック(&C)') { |sender, e|
			@action[:check].checked = !(@action[:check].checked)
			@action[:quit].enabled = !(@action[:quit].enabled)
		}
		@action[:check].shortcut = Keys.Control | Keys.C
		
		# メニューバー構築
		@menu_file = add_menu('ファイル(&F)')
		@menu_file_new = add_menu('サブメニュー(&S)', @menu_file)
		@menu_file_new_check = add_menu(:check, @menu_file_new)
		@menu_file.DropDownItems.Add(ToolStripSeparator.new())
		@menu_file_quit = add_menu(:quit, @menu_file)
		
		@menu_edit = add_menu('編集(&E)')
		
		# ツールバー構築
		@tool_bar = add_tool_bar()
		@btn_quit = add_tool_bar_button(:quit, @tool_bar)
		
	end

end

TestForm.run()
# 動作テスト部 ここまで

end