Class: Fzeet::BasicWindow

Inherits:
Handle
  • Object
show all
Includes:
WindowMethods
Defined in:
lib/fzeet/windows/user/Window/Common.rb

Constant Summary collapse

Prefix =
{
	xstyle: [:ws_ex_],
	style: [:ws_, :ds_],
	message: [:wm_],
	notification: []
}
WindowProc =
FFI::Function.new(:long, [:pointer, :uint, :uint, :long], convention: :stdcall) { |hwnd, uMsg, wParam, lParam|
	begin
		if uMsg == Windows::WM_NCCREATE
			@@instances[hwnd.to_i] = ObjectSpace._id2ref(
				Windows::CREATESTRUCT.new(FFI::Pointer.new(lParam))[:lpCreateParams].to_i
			)

			@@instances[hwnd.to_i].instance_variable_set(:@handle, hwnd)
		end

		result = @@instances[hwnd.to_i].windowProc(hwnd, uMsg, wParam, lParam) if @@instances[hwnd.to_i]
	rescue
		answer = Fzeet.message %Q{#{$!}\n\n#{$!.backtrace.join("\n")}}, buttons: [:abort, :retry, :ignore], icon: :error

		Application.quit if answer.abort?
	ensure
		if uMsg == Windows::WM_NCDESTROY
			@@instances[hwnd.to_i].dialog = false

			@@instances.delete(hwnd.to_i)
		end
	end

	result || Windows.DefWindowProc(hwnd, uMsg, wParam, lParam)
}
FrameProc =
FFI::Function.new(:long, [:pointer, :uint, :uint, :long], convention: :stdcall) { |hwnd, uMsg, wParam, lParam|
	begin
		if uMsg == Windows::WM_NCCREATE
			@@instances[hwnd.to_i] = ObjectSpace._id2ref(
				Windows::CREATESTRUCT.new(FFI::Pointer.new(lParam))[:lpCreateParams].to_i
			)

			@@instances[hwnd.to_i].instance_variable_set(:@handle, hwnd)
		end

		if uMsg == Windows::WM_CREATE
			@@instances[hwnd.to_i].menu = Menu.new.
				append(:popup, '&Windows', PopupMenu.new.
					append(:string, 'Tile &Horizontal', :tileHorz).
					append(:string, 'Tile &Vertical', :tileVert).
					append(:string, '&Cascade', :cascade).
					append(:string, 'Arrange &Icons', :arrangeIcons)
				)

			ccs = Windows::CLIENTCREATESTRUCT.new

			ccs[:hWindowMenu] = @@instances[hwnd.to_i].menu.submenus[0].handle
			ccs[:idFirstChild] = 50000

			hwndClient = Windows.DetonateLastError(FFI::Pointer::NULL, :CreateWindowEx,
				0, 'MDIClient', nil, Fzeet.flags([:child, :clipsiblings, :clipchildren, :visible], :ws_),
				0, 0, 0, 0,
				hwnd, nil, Windows.GetModuleHandle(nil), ccs
			)

			@@instances[hwnd.to_i].instance_variable_set(:@client, Handle.wrap(hwndClient, WindowMethods))

			@@instances[hwnd.to_i].
				on(:command, :tileHorz) { Windows.TileWindows(hwndClient, Windows::MDITILE_HORIZONTAL, nil, 0, nil) }.
				on(:command, :tileVert) { Windows.TileWindows(hwndClient, Windows::MDITILE_VERTICAL, nil, 0, nil) }.
				on(:command, :cascade) { Windows.CascadeWindows(hwndClient, Windows::MDITILE_ZORDER, nil, 0, nil) }.
				on(:command, :arrangeIcons) { Windows.ArrangeIconicWindows(hwndClient) }.

				on(:initmenupopup) { |args|
					[:tileHorz, :tileVert, :cascade, :arrangeIcons].each { |command|
						@@instances[hwnd.to_i].menu[command].enabled = !Windows.GetWindow(hwndClient, Windows::GW_CHILD).null?
					}
				}
		end

		result = @@instances[hwnd.to_i].windowProc(hwnd, uMsg, wParam, lParam) if @@instances[hwnd.to_i]
	rescue
		answer = Fzeet.message %Q{#{$!}\n\n#{$!.backtrace.join("\n")}}, buttons: [:abort, :retry, :ignore], icon: :error

		Application.quit if answer.abort?
	ensure
		if uMsg == Windows::WM_NCDESTROY
			@@instances[hwnd.to_i].dialog = false

			@@instances.delete(hwnd.to_i)
		end
	end

	hwndClient = @@instances[hwnd.to_i] && @@instances[hwnd.to_i].client && @@instances[hwnd.to_i].client.handle

	result || Windows.DefFrameProc(hwnd, hwndClient, uMsg, wParam, lParam)
}
MDIChildProc =
FFI::Function.new(:long, [:pointer, :uint, :uint, :long], convention: :stdcall) { |hwnd, uMsg, wParam, lParam|
	begin
		if uMsg == Windows::WM_NCCREATE
			@@instances[hwnd.to_i] = ObjectSpace._id2ref(
				Windows::MDICREATESTRUCT.new(
					Windows::CREATESTRUCT.new(FFI::Pointer.new(lParam))[:lpCreateParams]
				)[:lParam]
			)

			@@instances[hwnd.to_i].instance_variable_set(:@handle, hwnd)
		end

		result = @@instances[hwnd.to_i].windowProc(hwnd, uMsg, wParam, lParam) if @@instances[hwnd.to_i]
	rescue
		answer = Fzeet.message %Q{#{$!}\n\n#{$!.backtrace.join("\n")}}, buttons: [:abort, :retry, :ignore], icon: :error

		Application.quit if answer.abort?
	ensure
		if uMsg == Windows::WM_NCDESTROY
			@@instances[hwnd.to_i].dialog = false

			@@instances.delete(hwnd.to_i)
		end
	end

	result || Windows.DefMDIChildProc(hwnd, uMsg, wParam, lParam)
}
DialogProc =
FFI::Function.new(:int, [:pointer, :uint, :uint, :long], convention: :stdcall) { |hwnd, uMsg, wParam, lParam|
	begin
		if uMsg == Windows::WM_INITDIALOG
			@@instances[hwnd.to_i] = ObjectSpace._id2ref(lParam)

			@@instances[hwnd.to_i].instance_variable_set(:@handle, hwnd)
		end

		result = @@instances[hwnd.to_i].windowProc(hwnd, uMsg, wParam, lParam) if @@instances[hwnd.to_i]
	rescue
		answer = Fzeet.message %Q{#{$!}\n\n#{$!.backtrace.join("\n")}}, buttons: [:abort, :retry, :ignore], icon: :error

		Application.quit if answer.abort?
	ensure
		if uMsg == Windows::WM_NCDESTROY
			@@instances[hwnd.to_i].dialog = false

			@@instances.delete(hwnd.to_i)
		end
	end

	(result.nil?) ? 0 : 1
}
WindowClass =
Fzeet::WindowClass.new(nil, 'Fzeet.BasicWindow')

Constants included from WindowMethods

WindowMethods::EnumChildProc

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WindowMethods

#[], #capture=, #capture?, #dialog=, #dialog?, #dlgmsg?, #drawMenuBar, #eachChild, #enabled=, #enabled?, #focus=, #focus?, #invalidate, #location, #location=, #long, #menu, #menu=, #message, #paint, #position, #position=, #postmsg, #question, #rect, #reframe, #sendmsg, #show, #size, #size=, #style, #style?, #text, #text=, #textlen, #topmost=, #topmost?, #update, #visible=, #visible?, #xstyle, #xstyle?

Methods included from Toggle

#toggle

Constructor Details

#initialize(opts = {}) {|_self, _opts| ... } ⇒ BasicWindow

Returns a new instance of BasicWindow.

Yields:

  • (_self, _opts)

Yield Parameters:



718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
# File 'lib/fzeet/windows/user/Window/Common.rb', line 718

def initialize(opts = {})
	_opts = {
		xstyle: [],
		caption: Application.name,
		style: [],
		x: Windows::CW_USEDEFAULT,
		y: Windows::CW_USEDEFAULT,
		width: Windows::CW_USEDEFAULT,
		height: Windows::CW_USEDEFAULT,
		parent: nil,
		menu: nil,
		position: [],
		anchor: nil
	}
	badopts = opts.keys - _opts.keys; raise "Bad option(s): #{badopts.join(', ')}." unless badopts.empty?
	_opts.merge!(opts)

	yield self, _opts if block_given?

	_opts[:xstyle] = Fzeet.flags(_opts[:xstyle], *self.class::Prefix[:xstyle])
	_opts[:caption] = _opts[:caption].to_s
	_opts[:style] = Fzeet.flags(_opts[:style], *self.class::Prefix[:style])
	_opts[:x], _opts[:y], _opts[:width], _opts[:height] = _opts[:position] unless _opts[:position].empty?

	@messages ||= {}
	@commands ||= {}
	@notifies ||= {}

	@processed = [0, 0]

	@handle = Windows.CreateWindowEx(
		_opts[:xstyle],  self.class::WindowClass.name, _opts[:caption], _opts[:style],
		_opts[:x], _opts[:y], _opts[:width], _opts[:height],
		_opts[:parent] && _opts[:parent].handle, nil, Windows.GetModuleHandle(nil), FFI::Pointer.new(object_id)
	)

	if @handle.null?
		raise "CreateWindowEx failed (last error #{Windows.GetLastError()})." unless [
			[Windows::WM_NCCREATE, 0], [Windows::WM_CREATE, -1],
			[Windows::WM_DESTROY, 0], [Windows::WM_NCDESTROY, 0]
		].include?(@processed)
	else
		@parent = _opts[:parent]
		self.menu = _opts[:menu] if _opts[:menu]

		on(:destroy) {
			menu.rdetach if self.menu

			eachChild(&:dispose)
		}

		case _opts[:anchor]
		when :ltr
			@parent.on(:size) { |args|
				self.position = @parent.rect.tap { |r| r[:bottom] = _opts[:height] }.to_a

				args[:result] = nil if @parent.class == MDIChild
			}
		when :lrb
			@parent.on(:size) { |args|
				self.position = @parent.rect.tap { |r| r[:top] = r[:bottom] - _opts[:height]; r[:bottom] = _opts[:height] }.to_a

				args[:result] = nil if @parent.class == MDIChild
			}
		when :ltrb
			@parent.on(:size) { |args|
				self.position = @parent.rect.to_a

				args[:result] = nil if @parent.class == MDIChild
			}
		else raise ArgumentError, "Bad anchor spec: #{_opts[:anchor]}."
		end if _opts[:anchor] && @parent
	end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



793
794
795
# File 'lib/fzeet/windows/user/Window/Common.rb', line 793

def client
  @client
end

#parentObject (readonly)

Returns the value of attribute parent.



793
794
795
# File 'lib/fzeet/windows/user/Window/Common.rb', line 793

def parent
  @parent
end

#ribbonObject (readonly)

Returns the value of attribute ribbon.



793
794
795
# File 'lib/fzeet/windows/user/Window/Common.rb', line 793

def ribbon
  @ribbon
end

Class Method Details

.[](name, opts = {}) ⇒ Object



667
668
669
670
671
# File 'lib/fzeet/windows/user/Window/Common.rb', line 667

def self.[](name, opts = {})
	Class.new(self) {
		const_set(:WindowClass, Fzeet::WindowClass.new(self::WindowClass, name, opts))
	}
end

.crackMessage(hwnd, uMsg, wParam, lParam) ⇒ Object



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
# File 'lib/fzeet/windows/user/Window/Common.rb', line 673

def self.crackMessage(hwnd, uMsg, wParam, lParam)
	window = @@instances[hwnd.to_i]

	args = {
		hwnd: hwnd,
		uMsg: uMsg,
		wParam: wParam,
		lParam: lParam,
		result: (window.kind_of?(Dialog)) ? 1 : 0,
		window: window,
		sender: window
	}

	case uMsg
	when Windows::WM_CREATE
		args[:cs] = Windows::CREATESTRUCT.new(FFI::Pointer.new(lParam))
	when Windows::WM_NCCREATE
		args[:result] = 1
		args[:cs] = Windows::CREATESTRUCT.new(FFI::Pointer.new(lParam))
	when Windows::WM_COMMAND
		args[:command], args[:notification], args[:hctl] = Windows.LOWORD(wParam), Windows.HIWORD(wParam), FFI::Pointer.new(lParam)
		args[:sender] = @@instances[args[:hctl].to_i] unless args[:hctl].null?
	when Windows::WM_NOTIFY
		args[:nmh] = Windows::NMHDR.new(FFI::Pointer.new(lParam))
		args[:command], args[:notification], args[:hctl] = args[:nmh][:idFrom], args[:nmh][:code], args[:nmh][:hwndFrom]
		(args[:sender] = @@instances[args[:hctl].to_i]).class.crackNotification(args)
	when \
		Windows::WM_MOUSEMOVE,
		Windows::WM_LBUTTONDOWN, Windows::WM_LBUTTONUP, Windows::WM_LBUTTONDBLCLK,
		Windows::WM_RBUTTONDOWN, Windows::WM_RBUTTONUP, Windows::WM_RBUTTONDBLCLK,
		Windows::WM_MBUTTONDOWN, Windows::WM_MBUTTONUP, Windows::WM_MBUTTONDBLCLK,
		Windows::WM_CONTEXTMENU

		args[:x], args[:y] = Windows.GET_X_LPARAM(lParam), Windows.GET_Y_LPARAM(lParam)

		if uMsg == Windows::WM_CONTEXTMENU && args[:x] == -1
			Windows.DetonateLastError(0, :GetCursorPos, pt = Point.new)

			args[:keyboard], args[:x], args[:y] = true, pt[:x], pt[:y]
		end
	end

	args
end

Instance Method Details

#disposeObject



795
# File 'lib/fzeet/windows/user/Window/Common.rb', line 795

def dispose; Windows.DestroyWindow(@handle) end

#on(*args, &block) ⇒ Object



869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
# File 'lib/fzeet/windows/user/Window/Common.rb', line 869

def on(*args, &block)
	args[0] =~ /^draw$/ and
		return onMessage(:paint) { |_args| paint { |dc| dc.select(*args[1..-1]) {
			case block.arity
			when 0; block.call
			when 1; block.call(dc)
			else block.call(dc, _args)
			end
		}}}

	case args.length
	when 1; onMessage(*args, &block)
	when 2, 3
		case Fzeet.constant(args.shift, :wm_)
		when Windows::WM_COMMAND; onCommand(*args, &block)
		when Windows::WM_NOTIFY; onNotify(*args, &block)
		else raise ArgumentError
		end
	else raise ArgumentError
	end
end

#onCommand(cmd, notification = :all, &block) ⇒ Object



851
852
853
854
855
856
857
858
# File 'lib/fzeet/windows/user/Window/Common.rb', line 851

def onCommand(cmd, notification = :all, &block)
	notification = Fzeet.constant(notification, *self[cmd].class::Prefix[:notification]) unless
		notification.kind_of?(Integer) || notification == :all

	(((@commands ||= {})[Command[cmd]] ||= {})[notification] ||= []) << block

	self
end

#onMessage(msg, &block) ⇒ Object



845
846
847
848
849
# File 'lib/fzeet/windows/user/Window/Common.rb', line 845

def onMessage(msg, &block)
	((@messages ||= {})[Fzeet.constant(msg, :wm_)] ||= []) << block

	self
end

#onNotify(cmd, notification = :all, &block) ⇒ Object



860
861
862
863
864
865
866
867
# File 'lib/fzeet/windows/user/Window/Common.rb', line 860

def onNotify(cmd, notification = :all, &block)
	notification = Fzeet.constant(notification, *self[cmd].class::Prefix[:notification]) unless
		notification.kind_of?(Integer) || notification == :all

	(((@notifies ||= {})[Command[cmd]] ||= {})[notification] ||= []) << block

	self
end

#stubNotImplementedCommandsObject



891
892
893
894
895
896
897
898
899
# File 'lib/fzeet/windows/user/Window/Common.rb', line 891

def stubNotImplementedCommands
	Command.instance_variable_get(:@ids).each { |command, id|
		next if @commands.find { |_id, handlers| _id == id }

		on(:command, id) { message "Not Implemented (#{command})", icon: :warning }
	}

	self
end

#windowProc(hwnd, uMsg, wParam, lParam) ⇒ Object



797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
# File 'lib/fzeet/windows/user/Window/Common.rb', line 797

def windowProc(hwnd, uMsg, wParam, lParam)
	args, result = nil, nil

	if (handlers = @messages[uMsg])
		args ||= self.class.crackMessage(hwnd, uMsg, wParam, lParam)

		handlers.each { |handler|
			(handler.arity == 0) ? handler.call : handler.call(args)

			result = args[:result]; @processed[0], @processed[1] = uMsg, result
		}
	end

	if uMsg == Windows::WM_COMMAND && (handlers = @commands[Windows.LOWORD(wParam)])
		args ||= self.class.crackMessage(hwnd, uMsg, wParam, lParam)

		handlers[:all].each { |handler|
			(handler.arity == 0) ? handler.call : handler.call(args)

			result = args[:result]; @processed[0], @processed[1] = uMsg, result
		} if handlers[:all]

		handlers[Windows.HIWORD(wParam)].each { |handler|
			(handler.arity == 0) ? handler.call : handler.call(args)

			result = args[:result]; @processed[0], @processed[1] = uMsg, result
		} if handlers[Windows.HIWORD(wParam)]
	end

	if uMsg == Windows::WM_NOTIFY && (handlers = @notifies[(nmh = Windows::NMHDR.new(FFI::Pointer.new(lParam)))[:idFrom]])
		args ||= self.class.crackMessage(hwnd, uMsg, wParam, lParam)

		handlers[:all].each { |handler|
			(handler.arity == 0) ? handler.call : handler.call(args)

			result = args[:result]; @processed[0], @processed[1] = uMsg, result
		} if handlers[:all]

		handlers[nmh[:code]].each { |handler|
			(handler.arity == 0) ? handler.call : handler.call(args)

			result = args[:result]; @processed[0], @processed[1] = uMsg, result
		} if handlers[nmh[:code]]
	end

	result
end