PageRenderTime 22ms CodeModel.GetById 16ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/MessageHooks.ahk

http://7plus.googlecode.com/
AutoHotKey | 270 lines | 234 code | 7 blank | 29 comment | 79 complexity | bc1be231a4baa6ee5afd0d8bd5215a55 MD5 | raw file
  1; see http://msdn.microsoft.com/en-us/library/dd318066(VS.85).aspxs
  2HookProc(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime ){ 
  3	global ResizeWindow, SlideWindows, WindowList
  4	ListLines, Off
  5	hwnd += 0
  6	;On dialog popup, check if its an explorer confirmation dialog
  7	if(event = 0x00008002) ;EVENT_OBJECT_SHOW
  8	{
  9		if(IsObject(Settings) && Settings.Explorer.AutoCheckApplyToAllFiles && WinVer >= WIN_Vista)
 10			FixExplorerConfirmationDialogs()
 11		return
 12	}
 13	if idObject or idChild ;Doesn't each much time, skip for profiling
 14		return
 15	WinGet, style, Style, ahk_id %hwnd%
 16	if (style & 0x40000000)	;return if hwnd is child window, for some reason idChild may be 0 for some children ?!?! ( I hate ms )
 17		return
 18	if(event = 0x0016) ;EVENT_SYSTEM_MINIMIZEEND
 19	{
 20		Trigger := new CWindowStateChangeTrigger()
 21		Trigger.Window := hwnd
 22		Trigger.Event := "Window minimized"
 23		EventSystem.OnTrigger(Trigger)
 24	}
 25	else if(event = 0x8001 && IsObject(Settings) && Settings.Explorer.Tabs.UseTabs) ;EVENT_OBJECT_DESTROY
 26	{
 27		; DecToHex(hwnd)
 28		; if(TabContainerList.ContainsHWND(hwnd))
 29	}
 30	else if(event = 0x800B) ;EVENT_OBJECT_LOCATIONCHANGE
 31	{
 32		WinGet, state, minmax, ahk_id %hwnd%
 33		if(state = 1)
 34		{
 35			Trigger := new CWindowStateChangeTrigger()
 36			Trigger.Window := hwnd
 37			Trigger.Event := "Window maximized"
 38			EventSystem.OnTrigger(Trigger)
 39		}
 40		if(InStr("CabinetWClass,ExploreWClass", WinGetClass("ahk_id " hwnd)))
 41			ExplorerMoved(hwnd)
 42		if(IsObject(SlideWindows))
 43			SlideWindows.CheckResizeReleaseCondition(hwnd)
 44		if(state != -1)
 45		{
 46			WindowList.MovedWindow := hwnd
 47			SetTimer, UpdateWindowPosition, -1000
 48		}
 49	}	
 50	else if(event = 0x000A && Settings.Windows.ShowResizeTooltip)
 51	{
 52		ResizeWindow := hwnd
 53		SetTimer, ResizeWindowTooltip, 50
 54		SlideWindows.CheckResizeReleaseCondition(hwnd)
 55	}
 56	else if(event = 0x000B)
 57	{
 58		ShowTip({Min : 4, Max : 7}, 0.1)
 59		if(Settings.Windows.ShowResizeTooltip)
 60		{
 61			ResizeWindow := ""
 62			SetTimer, ResizeWindowTooltip, Off
 63			ResizeWindowTooltip(true)
 64			Tooltip
 65		}
 66	}
 67	ListLines, On
 68}
 69ResizeWindowTooltip:
 70ResizeWindowTooltip()
 71return
 72ResizeWindowTooltip(reset = false)
 73{	
 74	global ResizeWindow
 75	static w,h
 76	if(reset)
 77	{
 78		w:=0
 79		h:=0
 80		return
 81	}
 82	WinGetPos, , , wn, hn, ahk_id %ResizeWindow%
 83	if(w && h && (w != wn || h != hn))
 84		Tooltip %w%/%h%
 85	w := wn
 86	h := hn
 87}
 88
 89;See http://msdn.microsoft.com/en-us/library/ms644991(VS.85).aspx
 90ShellMessage( wParam, lParam, Msg)
 91{
 92	WasCritical := A_IsCritical
 93	Critical
 94	ListLines, Off
 95	global BlinkingWindows, WindowList, Accessor, RecentCreateCloseEvents, ToolWindows, ExplorerWindows, LastWindow, LastWindowClass, SlideWindows, CurrentWindow, PreviousWindow, ExplorerHistory
 96	Trigger := new COnMessageTrigger()
 97	Trigger.Message := wParam
 98	Trigger.lParam := lParam
 99	Trigger.Msg := Msg
100	EventSystem.OnTrigger(Trigger)
101	if(wParam = 1 || wParam = 2) ;Window Created/Closed
102	{
103		lParam += 0
104		;Keep a list of recently received create/close messages, because they can be sent multiple times and we only want one.
105		if(!IsObject(RecentCreateCloseEvents))
106			RecentCreateCloseEvents := Array()
107		SetTimer, ClearRecentCreateCloseEvents, -300
108		if(!RecentCreateCloseEvents.HasKey(lParam))
109		{
110			RecentCreateCloseEvents[lParam] := 1
111			Trigger := wParam = 1 ? new CWindowCreatedTrigger() : new CWindowClosedTrigger()
112			class:= wParam = 1 ? WinGetClass("ahk_Id " lParam) : (IsObject(WindowList) && IsObject(WindowList[lParam]) ? WindowList[lParam].class : "INVALID WINDOW CLASS")
113			Trigger.Window := lParam
114			EventSystem.OnTrigger(Trigger)
115			;Keep a list of windows and their required info stored. This allows to identify windows which were closed recently.
116			WinGet, hwnds, list,,, Program Manager
117			Loop, %hwnds%
118			{
119				hwnd := hwnds%A_Index%+0
120				WinGetTitle, title, ahk_id %hwnd%
121				if(IsObject(WindowList[hwnd]))
122					WindowList[hwnd].title := title
123				else
124				{
125					WinGetClass, class, ahk_id %hwnd%
126					WinGet, exe, ProcessName, ahk_id %hwnd%
127					WinGet, Path, ProcessPath, ahk_id %hwnd%
128					WindowList[hwnd] := Object("class", class, "title", title, "Executable", exe, "Path", Path)
129				}
130			}
131		}
132		if(wParam = 2)
133		{
134			if(IsObject(WindowList[lParam]) && InStr("CabinetWClass,ExploreWClass", WindowList[lParam].class))
135				GoSub WaitForClose
136			else ;Code below is also executed in WaitForClose for separate Explorer handling (why can't explorer send close messages properly like a normal window??)
137			{
138				if(IsObject(ToolWindows))
139				{
140					Loop % ToolWindows.MaxIndex()
141					{
142						if(ToolWindows[A_Index].hParent = lParam && ToolWindows[A_Index].AutoClose)
143						{
144							WinClose % "ahk_id " ToolWindows[A_Index].hGui
145							ToolWindows.Remove(A_Index)
146							break
147						}
148					}
149				}
150				SlideWindows.WindowClosed(lParam)
151			}
152		}
153		if(wParam = 1)
154		{
155			if(IsObject(SlideWindows))
156				SlideWindows.WindowCreated(lParam)
157			AutoCloseWindowsUpdate(lParam)
158			;~ SlideWindows.CreatedWindow := lParam
159			;~ SetTimer, SlideWindows_WindowCreated, -100
160		} ;	SlideWindows.WindowCreated(lParam)
161	}	
162	;Blinking windows detection, add new blinking windows
163	else if(wParam = 32774)
164	{
165		lParam += 0
166		if(!BlinkingWindows.indexOf(lParam))
167		{
168			BlinkingWindows.Insert(lParam)
169			ShowTip(12)
170		}
171	}	
172	;Window Activation
173	else if(wParam = 4 || wParam = 32772) ;HSHELL_WINDOWACTIVATED||HSHELL_RUDEAPPACTIVATED
174	{
175		ShowTip(13, 0.05)
176		if(IsAltTabWindow(lParam))
177		{
178			PreviousWindow := CurrentWindow
179			CurrentWindow := lParam
180		}
181		lParam += 0
182		Trigger := new CWindowActivatedTrigger()
183		EventSystem.OnTrigger(Trigger)
184		;Blinking windows detection, remove activated windows
185		if(x := BlinkingWindows.indexOf(lParam))
186			BlinkingWindows.Delete(x)
187
188		if(IsObject(CAccessor.Instance.GUI) && CAccessor.Instance.Settings.CloseWhenDeactivated && WinExist("A") != CAccessor.Instance.GUI.hwnd)
189			CAccessor.Instance.Close()
190
191		;If we change from another program to explorer/desktop/dialog
192		if((IsExplorer := WinActive("ahk_group ExplorerGroup"))||WinActive("ahk_group DesktopGroup")||IsDialog())
193		{
194			if(!WinActive("ahk_group DesktopGroup")) ;By doing this, recall explorer path works also when double clicking desktop to launch explorer
195				Settings.Explorer.CurrentPath := Navigation.GetPath()
196			;Paste text/image as file file creation
197			CreateFileFromClipboard()
198			if((IsExplorer && ExplorerWindows.GetItemWithValue("hwnd", IsExplorer).Path != Settings.Explorer.CurrentPath) || !IsExplorer)
199			{
200				Entry := RichObject()
201				Name := Entry.Path := Settings.Explorer.CurrentPath
202				SplitPath, Name, Name
203				Entry.Name := IsExplorer ? Navigation.GetDisplayName(lParam) : Name
204				Entry.Usage := 0
205				Entry := ExplorerHistory.Push(Entry) ;This can return a different entry that already exists in the list!
206				Entry.Usage++
207			}
208		}
209		if(LastWindowClass && InStr("CabinetWClass,ExploreWClass", LastWindowClass) && !ExplorerWindows.TabContainerList.TabCreationInProgress && !ExplorerWindows.TabContainerList.TabActivationInProgress)
210			ExplorerDeactivated(LastWindow)
211		
212		LastWindow := lParam
213		LastWindowClass := WinGetClass("ahk_id " lParam)
214		
215		if(InStr("CabinetWClass,ExploreWClass", LastWindowClass) && LastWindowClass && !ExplorerWindows.TabContainerList.TabCreationInProgress && !ExplorerWindows.TabContainerList.TabActivationInProgress)
216			ExplorerActivated(LastWindow)
217		
218		if(IsObject(SlideWindows))
219			SlideWindows.WindowActivated()
220	}
221	;Redraw is fired on Explorer path change
222	else if(wParam = 6)
223	{
224		lParam += 0
225		;Detect changed path
226		if(InStr("CabinetWClass,ExploreWClass", WinGetClass("ahk_id " lParam)))
227		{
228			ExplorerPathChanged(ExplorerWindows.GetItemWithValue("hwnd", lParam))
229			; newpath := Navigation.GetPath()
230			; if(newpath && newpath != Settings.Explorer.CurrentPath)
231			; {
232				; outputdebug Explorer path changed from %ExplorerPath% to %newpath%
233				; ExplorerPathChanged(Settings.Explorer.CurrentPath, newpath)
234				; Settings.Explorer.PreviousPath := Settings.Explorer.CurrentPath
235				; Settings.Explorer.CurrentPath := newpath
236				; Trigger := new CExplorerPathChangedTrigger()
237				; EventSystem.OnTrigger(Trigger)
238				; if(Settings.Explorer.Tabs.UseTabs && !SuppressTabEvents && hwnd:=WinActive("ahk_group ExplorerGroup"))
239					; UpdateTabs()
240			; }
241		}
242	}
243	ListLines, On
244	if(!WasCritical)
245		Critical, Off
246}
247
248;Timer for clearing the list of recently received create/close events
249ClearRecentCreateCloseEvents:
250RecentCreateCloseEvents := Array()
251return
252UpdateWindowPosition:
253UpdateWindowPosition()
254return
255UpdateWindowPosition()
256{
257	global WindowList
258	WinGetPos, x, y, w, h, % "ahk_id " WindowList.MovedWindow
259	if(!IsObject(WindowList[WindowList.MovedWindow]))
260		return
261	WindowList[WindowList.MovedWindow].x := x
262	WindowList[WindowList.MovedWindow].y := y
263	WindowList[WindowList.MovedWindow].w := w
264	WindowList[WindowList.MovedWindow].h := h
265}
266WM_POWERBROADCAST(wParam, lParam, msg)
267{
268	if (wParam = 18)
269		AutoCloseWindowsUpdate(WinExist("Windows Update ahk_class #32770"))
270}