Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # To change this template, choose Tools | Templates
- # and open the template in the editor.
- __author__ = "Bob"
- __date__ = "$Apr 30, 2011 11:03:09 AM$"
- import os
- import ctypes
- from ctypes import wintypes
- import _thread
- import time
- import win32gui
- import hotkeyhelper
- import windowhelper
- from tiler import Tiler
- from win32con import GW_OWNER
- from win32con import MOD_ALT
- from win32con import MOD_SHIFT
- from win32con import WM_CLOSE
- from win32con import WM_HOTKEY
- from win32con import SW_SHOWNORMAL
- from win32con import SW_MINIMIZE
- def callback (window, resultList):
- #if the window is visible
- if win32gui.IsWindowVisible(window):
- #if the window doesn't have an owner(pop-ups)
- if not win32gui.GetWindow(window, GW_OWNER):
- #if the window command == show
- if win32gui.GetWindowPlacement(window)[1] == SW_SHOWNORMAL:
- #if the class is not empty
- if win32gui.GetClassName(window):
- #grab window
- resultList.append(window)
- return True
- def validate_ignorelist(window):
- ignore = (#Windows classnames that I like tiled
- #Keep this list to a minimum! the smaller it is the better the performance
- "Progman" #windows shell
- , "ComboLBox" #The class for the list box contained in a combo box.
- , "DDEMLEvent" #The class for Dynamic Data Exchange Management Library (DDEML) events.
- , "Message" #The class for a message-only window.
- , "#32768" #The class for a menu.
- , "#32769" #The class for the desktop window.
- , "#32770" #The class for a dialog box.
- , "#32771" #The class for the task switch window.
- , "#32772" #The class for icon titles.
- , "#32774" #The class for titles on close in command prompts.
- , "Button" #The class for a button.
- , "ComboBox" #he class for a combo box.
- , "Edit" #The class for an edit control.
- , "ListBox" #The class for a list box.
- , "MDIClient" #The class for an MDI client window.
- , "ScrollBar" #The class for a scroll bar.
- , "Static" #The class for a static control.
- , "ClockTooltipWindow" #the clock tooltip
- , "tooltips_class32" #tooltip in explorer
- , "DV2ControlHost" #start menu
- , "BaseBar" #shutdown options
- , "SynTrackCursorWindowClass" #no idea
- , "Shell_TrayWnd" #console extra handler
- , "TaskListThumbnailWnd" #windows 7 thumbnails
- , "WorkerW" #popped up after opening powerpoint?
- , "screenClass" #powerpoint fullscreen
- , "gdkWindowTempShadow" #pidgin contact details
- , "gdkWindowTemp" #pidgin contact details
- , "ShockwaveFlashFullScreen" #fullscreen flash (video)
- , "SynShapedFrameWindowClass" #firefox scrolling, god knows why
- , "MozillaDropShadowWindowClass"#firefox shadowwindow
- )
- if win32gui.GetClassName(window) not in ignore:
- return True
- return False
- def listen_to_hotkeys():
- byref = ctypes.byref
- user32 = ctypes.windll.user32
- hotkeys = {1: (MOD_ALT, ord("L"))
- , 2: (MOD_ALT, ord("H"))
- , 3: (MOD_ALT + MOD_SHIFT, ord("J"))
- , 4: (MOD_ALT + MOD_SHIFT, ord("K"))
- , 5: (MOD_ALT, ord("J"))
- , 6: (MOD_ALT, ord("K"))
- , 7: (MOD_ALT + MOD_SHIFT, ord("C"))
- , 8: (MOD_ALT + MOD_SHIFT, ord("V"))
- , 9: (MOD_ALT + MOD_SHIFT, ord("L"))
- , 10: (MOD_ALT + MOD_SHIFT, ord("H"))
- , 11: (MOD_ALT + MOD_SHIFT, ord("Q"))
- }
- hotkeyhandlers = {1: handler_alt_L
- , 2: handler_alt_H
- , 3: handler_shift_alt_J
- , 4: handler_shift_alt_K
- , 5: handler_alt_J
- , 6: handler_alt_K
- , 7: handler_shift_alt_C
- , 8: handler_shift_alt_V
- , 9: handler_shift_alt_L
- , 10: handler_shift_alt_H
- , 11: handler_shift_alt_Q
- }
- try:
- #register the hotkeys
- hotkeyhelper.register_hotkeys(hotkeys)
- #define msg
- msg = wintypes.MSG()
- #wait for a message
- while user32.GetMessageA(byref(msg), None, 0, 0) != 0:
- #check if message is a hotkey and if we have it registered
- if msg.message == WM_HOTKEY and msg.wParam in hotkeyhandlers.keys():
- #execute the hotkey handler in a different thread (smooth actions when hotkey is rapidly pressed)
- _thread.start_new_thread(hotkeyhandlers.get(msg.wParam), ())
- #user32.TranslateMessage(byref(msg))
- #user32.DispatchMessageA(byref(msg))
- finally:
- #unregister the hotkeys
- hotkeyhelper.unregister_hotkeys(hotkeys)
- def handler_alt_L():
- global tiler, threadlock
- threadlock.acquire()
- tiler.masterareaWidth += 100
- threadlock.release()
- print ("master area += 100")
- tiler.tile_windows()
- def handler_alt_H():
- global tiler, threadlock
- threadlock.acquire()
- tiler.masterareaWidth -= 100
- threadlock.release()
- print ("master area -= 100")
- tiler.tile_windows()
- def handler_shift_alt_J():
- global tiler, threadlock
- #only grab and move the window if it is in the tiler
- if win32gui.GetForegroundWindow() in tiler.windows:
- i = tiler.windows.index(windowhelper.get_focused_window())
- if i >= len(tiler.windows) - 1:
- threadlock.acquire()
- tiler.windows[0], tiler.windows[1:] = tiler.windows[i], tiler.windows[:i]
- threadlock.release()
- else:
- threadlock.acquire()
- tiler.windows[i], tiler.windows[i + 1] = tiler.windows[i + 1], tiler.windows[i]
- threadlock.release()
- print ("change order down")
- tiler.tile_windows()
- def handler_shift_alt_K():
- global tiler, threadlock
- #only grab and move the window if it is in the tiler
- if win32gui.GetForegroundWindow() in tiler.windows:
- i = tiler.windows.index(windowhelper.get_focused_window())
- if i <= 0:
- threadlock.acquire()
- j = len(tiler.windows) - 1
- tiler.windows[j], tiler.windows[:j] = tiler.windows[0], tiler.windows[1:]
- threadlock.release()
- else:
- threadlock.acquire()
- j = i - 1
- tiler.windows[i], tiler.windows[j] = tiler.windows[j], tiler.windows[i]
- threadlock.release()
- print ("change order up")
- tiler.tile_windows()
- def handler_alt_J():
- global tiler
- #only grab and move the focus if it is in the tiler
- if win32gui.GetForegroundWindow() in tiler.windows:
- i = tiler.windows.index(windowhelper.get_focused_window()) + 1
- if i >= len(tiler.windows):
- i = 0
- windowhelper.focus(tiler.windows[i])
- windowhelper.set_cursor_window(tiler.windows[i])
- print ("change focus down")
- def handler_alt_K():
- global tiler
- #only grab and move the focus if it is in the tiler
- if win32gui.GetForegroundWindow() in tiler.windows:
- i = tiler.windows.index(windowhelper.get_focused_window()) - 1
- if i < 0:
- i = len(tiler.windows) - 1
- windowhelper.focus(tiler.windows[i])
- windowhelper.set_cursor_window(tiler.windows[i])
- print ("change focus up")
- def handler_shift_alt_C():
- global tiler, threadlock, oldAmount
- window = windowhelper.get_focused_window()
- #only remove the window from the tiler if it is in the tiler
- if win32gui.GetForegroundWindow() in tiler.windows:
- threadlock.acquire()
- tiler.windows.remove(window)
- threadlock.release()
- win32gui.SendMessage(window, WM_CLOSE, 0, 0)
- threadlock.acquire()
- oldAmount -= 1
- threadlock.release()
- tiler.tile_windows()
- def handler_shift_alt_V():
- global tiler, threadlock, oldAmount
- window = windowhelper.get_focused_window()
- #only remove the window from the tiler if it is in the tiler
- if win32gui.GetForegroundWindow() in tiler.windows:
- threadlock.acquire()
- tiler.windows.remove(window)
- threadlock.release()
- win32gui.ShowWindow(window, SW_MINIMIZE)
- threadlock.acquire()
- oldAmount -= 1
- threadlock.release()
- tiler.tile_windows()
- def handler_shift_alt_L():
- global tiler, threadlock
- if tiler.masterareaSize > 1:
- threadlock.acquire()
- tiler.masterareaSize -= 1
- threadlock.release()
- print ("masterarea size -= 1")
- tiler.tile_windows()
- def handler_shift_alt_H():
- global tiler, threadlock
- if tiler.masterareaSize < len(tiler.windows):
- threadlock.acquire()
- tiler.masterareaSize += 1
- threadlock.release()
- print ("masterarea size += 1")
- tiler.tile_windows()
- def handler_shift_alt_Q():
- os._exit(0)
- if __name__ == "__main__":
- #initialization
- threadlock = _thread.allocate_lock()
- windows = []
- tiler = Tiler()
- #hotkeys
- _thread.start_new_thread(listen_to_hotkeys, ())
- #initial tile
- win32gui.EnumWindows(callback, windows)
- for window in windows:
- if validate_ignorelist(window):
- tiler.windows.append(window)
- #tile
- tiler.tile_windows()
- for window in tiler.windows:
- print (win32gui.GetClassName(window))
- #set initial control values
- oldAmount = len(windows)
- oldTiledWindows = set(tiler.windows)
- while True:
- time.sleep(0.1)
- windows = []
- reTile = False
- win32gui.EnumWindows(callback, windows)
- if len(windows) > oldAmount:
- for window in windows:
- if window not in tiler.windows:
- if validate_ignorelist(window):
- tiler.windows.append(window)
- reTile = True
- print ("Add handle: ", window, win32gui.GetClassName(window))
- elif oldAmount > len(windows):
- for window in (oldTiledWindows - set(windows)):
- if window in tiler.windows:
- tiler.windows.remove(window)
- reTile = True
- print ("Remove handle: ", window)
- if reTile:
- _thread.start_new_thread(tiler.tile_windows, ())
- oldAmount = len(windows)
- oldTiledWindows = set(tiler.windows)
Add Comment
Please, Sign In to add comment