Jump to content


Photo

AHK_L store winpos table with handle as key


  • Please log in to reply
8 replies to this topic

#1 MilesAhead

MilesAhead
  • Members
  • 429 posts

Posted 25 May 2012 - 06:46 AM

I've got a little utility that moves windows around on the screen, done in AHK_L.

This subroutine stores info for one undo operation. But I'm trying to figure out how to store a table in an object with the window handle as the key. The user hits a hotkey to move the active window. The old position is saved to undo. But I'd like to save maybe 1/2 dozen undo positions for each handle. I can't figure out from the examples how to insert the 4 window position values as a row in a table rather than just a simple array, then stick that in an associative array with window handle as key.

wArray := Object()

SaveUndoInfo:
  WinGetPos,x,y,w,h, ahk_id %AWindow%
  wPos := [x,y,w,h]
  wArray[AWindow] := wPos
return

What I'm trying to do in pseudo-code is:
If handle in assocarray as key, use it to fetch
table from object and assign to a variable.
Append new position info, or read old position
info out of the table. If appending, insert the
augmented table back into assocarray.

#2 just me

just me
  • Members
  • 1175 posts

Posted 25 May 2012 - 09:17 AM

SaveUndoInfo:

   WinGetPos,x,y,w,h, ahk_id %AWindow%

   If !wArray.HasKey(AWindow)

      wArray[AWindow] := {}

   wArray[AWindow].Insert({X: x, Y: y, W: w, H: h}) 

Return
:?:

#3 sinkfaze

sinkfaze
  • Moderators
  • 6089 posts

Posted 25 May 2012 - 12:43 PM

Strangely enough, you don't even need to use HasKey():

SaveUndoInfo:
	out=
	WinGetPos,x,y,w,h, ahk_id %AWindow%
	If	[color=#FF0000]!wArray[AWindow][/color]
		wArray[AWindow] :=	[x,y,w,h]
	else
		For each, attr in	wArray[AWindow]
			out .=	(!out ? "" : ",") attr
Return


#4 MilesAhead

MilesAhead
  • Members
  • 429 posts

Posted 25 May 2012 - 07:44 PM

Thanks for the replies. As far as I can see, the suggestions are what I have now. I can insert position info and retrieve it for one window position now. What I'm trying to do is

table.insert(x,y,w,h)

where I'll have a table of positions for each handle, then stick the table in the assoc array.

wArray[AWindow] := table

But I don't know how to initiate a table without sticking in a bunch of blank strings. iow, how do I tell AHK_L that the value in the assoc array is itself a 2 dimensional array?

Something like

table := wArray[AWindow]
table.insert(x,y,w,h)
wArray[AWindow] := table

I don't know how long it would take just by trial and error. I can't seem to find any example code. I'm trying to avoid having to calculate a bunch of x,y,w,h values just strung in the array. I'd like a true table where the last position entry could be retrieved with something like

wPos := table[table.MaxIndex()]

in which case MaxIndex() - 1 would have the window position before that etc..

#5 sinkfaze

sinkfaze
  • Moderators
  • 6089 posts

Posted 25 May 2012 - 07:50 PM

Well I think what just me and I understood you to be saying was that you wanted to get a collection of windows and each window's position info at some point in time. But you are saying that you want to have one window and a collection of different position infos for that one window?

#6 MilesAhead

MilesAhead
  • Members
  • 429 posts

Posted 25 May 2012 - 07:54 PM

Well I think what just me and I understood you to be saying was that you wanted to get a collection of windows and each window's position info at some point in time. But you are saying that you want to have one window and a collection of different position infos for that one window?


Not quite. What I want is a table of window positions for each handle. So whatever is the active window when the user presses the hotkey, I can get the handle, see if it's in the Undo info assoc array, if so, retrieve the table for that handle, then get the last position. If the user hits Undo again, go to 2nd last position and so on.

However many window handles are active I want to be able retrieve a table of undo info, if they were moved with my program. I already have the code to purge closed handles.

#7 sinkfaze

sinkfaze
  • Moderators
  • 6089 posts

Posted 25 May 2012 - 08:03 PM

Well this will fill in your tables:

if	!IsObject(wArray)
	wArray :=	{}
if	!wArray[hWnd]
	wArray[hWnd] :=	[[x,y,w,h]]
else	wArray[hWnd].Insert([x,y,w,h])

And to get the last saved position of a window you can use wArray[hWnd].MaxIndex() and gradually subtract from that number to undo back to the position(s) before that. Is that what you're looking for?

#8 MilesAhead

MilesAhead
  • Members
  • 429 posts

Posted 25 May 2012 - 08:11 PM

Thanks. I'll try it out. I don't recall ever using double brackets like that. That's probably what's throwing me. I'll paste it in a dummy app now and see if I can get it. :)

#9 MilesAhead

MilesAhead
  • Members
  • 429 posts

Posted 25 May 2012 - 08:23 PM

Thanks sinkfaze. That's getting me on the road. :)