examples

Cooperating Windows

December 3, 2004 9:51:18.662

A common need in any application is Windows that cooperate - child windows that close when their parent closes, etc. It turns out that it's pretty easy to set that up in VisualWorks - I've needed it in BottomFeeder, and I'm sure that most Cincom Smalltalk developers need this capability at some point. So how do you do it? Let's start with the 'main application window' - add this to the #postBuildWithMethod:


postBuildWith: bldr
	super postBuildWith: bldr.
	bldr window 
		application: self;
		beMaster.

That sets the 'main application' window up as a master - child windows will receive most of the same window events and follow them. Now you need to set up the child window. Let's say that it opens up from some method in the main application:


openSomeTool
	| tool |
	tool := SomeTool new.
	toolBuilder := tool allButOpenInterface: #windowSpec.
	toolBuilder
		window application: self;
		beSlave.
	tool finallyOpen.

What's going on here is that you are registering a specific window as the 'master', and another as the 'slave' - which means that a set of events received by the master window will be passed along. If you want events passed equally, then use #bePartner to all involved windows.

It turns out that you can customize the set of events you care about - a quick look at #beMaster and #beSlave in class ApplicationModel shows this:


beMaster
	"Send windowState events to other ApplicationWindows that have the same 
	application as us. Effectively the dual of beSlave."

	self sendWindowEvents: #(#close #collapse #expand ); receiveWindowEvents: #(#expand )


beSlave
	"Receive windowState events from some other ApplicationWindow that has the 
	same application as us. Effectively the dual of beMaster."

	self receiveWindowEvents: #(#close #collapse #expand ); sendWindowEvents: #(#expand )

You could easily add your own custom versions of #beMaster and #beSlave - and either expand or contract the list of window events. You can also use #sendWindowEvents: and #receiveWindowEvents: to tailor the event stream. It's all pretty easy to manage.

 Share Tweet This
-->