[comp.sys.mac.programmer] WindowObject in Object Pascal

treinies@asterix.luftfahrt.uni-stuttgart.de (Klaus-Achim Treinies) (11/05/90)

I'm just starting OOP with Object Pascal under MPW. The first object I'm
working on is a window object defined as

  	TWindow = object(TObject)
			fRecord  : WindowRecord;
			fVScroll : ControlHandle;
			fHScroll : ControlHandle;
			procedure  Install;
 			...
                        procedure  Something;
			...
	    	  end;


	procedure TWindow.Install;
	var
	  aWindowPtr : WindowPtr;
	begin
   	  ...
   	  aWindowPtr := GetNewWindow(..., @self.fRecord, ...);
	  ...
          ShowWindow(aWindowPtr);
	  ...
	end;


Installing the window works nice when calling

	procedure WindowOpen;
	var
	  aWindowObject : TWindow;
	begin
	  New(aWindowObject);
	  aWindowObject.Install;
	  ...
	end;

Now the problem: How do I get a link between aWindowPtr and aWindowObject
in a procedure which deals with events like grow, update etc. What I want
to do in particular is something like

	procedure DoSomething(theEvent : EventRecord);
	var
	  aWindowPtr    : WindowPtr;
	  aWindowObject : TWindow;
	  what		: integer;
	begin
	  what := FindWindow(theEvent.where, aWindowPtr);

	  { here I want the link	    }
	  { aWindowPtr ----> aWindowObject; }
	  { so I can go on with             }

	  case what of
	    ...
	    something : aWindowObject.Something;
	    ...
	  end;
	end;


For all the answers I hope to receive I want to say THANK YOU VERY MUCH!
-- 

============================================================================
 ACHIM TREINIES         |    treinies@asterix.luftfahrt.rus.uni-stuttgart.de
 ISD, UNI. STUTTGART    |                
 PFAFFENWALDRING 27     |
 D-7000 STUTTGART 80    |
 GERMANY                |
============================================================================


 
 

murat@farcomp.UUCP (Murat Konar) (11/07/90)

[ someone wants to define an object that represents a window in Object Pascal ]

If I understand the problem correctly, given a window ptr, you want to
to get to the window object.  You can do this by stuffing a reference to
the window object in the window record's refCon field.  MacApp does something
like this.  You must be careful not to assume that whatever the refCon points
to is infact an object (i.e. you must test it for objectness) since there may 
be windows with non-nil refcons that point to other things.
-- 
____________________________________________________________________
Have a day. :^|             
Murat N. Konar	
murat@farcomp.UUCP             -or-          farcomp!murat@apple.com

aries@rhi.hi.is (Reynir Hugason) (11/10/90)

The way I usually solve this is to introduce a new data structure, some-
thing like this:

   MyWindowRec = RECORD
                   oldWindowRec: WindowRec;

                  { what-ever stuff I need to use }
                 END;
   MyWindowPeek = ^MyWindowRec;
   MyWindowPtr = WindowPtr;

This is the same trick as Apple uses to hide the special window parameters
behind a grafport, so you can simply say SetPort(myWindow).

Then I stuff a windowlist structure in there too, mainly to reduce heap
fragmentation if the windows are opened and closed frequently but also
to enable me to check whether the window reference I receive is actually
one of my windows. This is very easy to implement, you either declare a
constant array of a fixed size or a dynamic list of fixed size buckets
(in either case, the storage should be non-relocatable). You initialize
it, by NILing some field, in each slot, that shouldn't be NIL if the
slot was occupied. Then when-ever you need a window you find a free slot
and pass the address of the slot to GetNewWindow. And when your through
with the window you simply call CloseWindow (NOT DisposeWindow!!!).
On receiving mouse-clicks you check whether the window is actually in
your windowlist, if it ain't then the event is SEP (Somebody Elses Problem).

Pretty strightforward stuff, eh??

Mimir (aries@rhi.hi.is)
Aries, inc.

/// With greetings from Sweet Mama Jankins ... and her friends.