This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Sublime text toggle menu plugin with ctypes

By: a guest on Mar 26th, 2010  |  syntax: Python  |  size: 2.47 KB  |  views: 149  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # toggleMenubar plugin v1.0.1
  2. #
  3. # example keybinding:
  4. # <binding key="leftalt+m" command="toggleMenubar"/>
  5. #
  6. # Known issues:
  7. #       menu hidden by fullscreen mode can't be shown again
  8. #       saving any file makes toggleMenubar backup hwnd, hmenu and show
  9.  
  10. import sublime, sublimeplugin, os
  11. from ctypes import *
  12.  
  13. firstRun = 1
  14. needSave = 1
  15. show  = 1
  16. hwnd  = windll.User32.GetActiveWindow()
  17. hmenu = windll.User32.GetMenu(hwnd)
  18.  
  19. CSIDL_APPDATA = 26
  20. _SHGetFolderPath = windll.shell32.SHGetFolderPathA
  21. _SHGetFolderPath.argtypes = [wintypes.HWND,
  22.         wintypes.c_int,
  23.         wintypes.HANDLE,
  24.         wintypes.DWORD, wintypes.LPCSTR]
  25.  
  26. appdataPath = wintypes.create_string_buffer(wintypes.MAX_PATH)
  27. result = _SHGetFolderPath(0, CSIDL_APPDATA, 0, 0, appdataPath)
  28. if result <> 0:
  29.         print "[Error] SHGetFolderPath returned: " + str(result)
  30.  
  31. def loadGlobals():
  32.         if os.path.isfile(appdataPath.value+"\\sublime text\\Packages\\User\\toggleMenubar.tmp"):
  33.                 file = open(appdataPath.value+"\\sublime text\\Packages\\User\\toggleMenubar.tmp", 'r')
  34.                 try:
  35.                         globals()["hmenu"] = int(file.readline())
  36.                         globals()["hwnd"]  = int(file.readline())
  37.                         globals()["show"]  = int(file.readline())
  38.                 except:
  39.                         pass
  40.                 file.close()
  41.                 os.remove("toggleMenubar.tmp")
  42.         else:
  43.                 globals()["hwnd"]  = windll.User32.GetActiveWindow()
  44.                 globals()["hmenu"]  = windll.User32.GetMenu(globals()["hwnd"])
  45.  
  46. class toggleMenubarCommand(sublimeplugin.TextCommand):
  47.         def run(self, view, args):
  48.                 if globals()["firstRun"]:
  49.                         globals()["firstRun"] = 0
  50.                         globals()["show"]  = 1
  51.                         globals()["hwnd"]  = windll.User32.GetActiveWindow()
  52.                         globals()["hmenu"] = windll.User32.GetMenu(globals()["hwnd"])
  53.  
  54.                 if globals()["hmenu"] == 0 or globals()["hwnd"] == 0:
  55.                         loadGlobals()
  56.  
  57.                 newMenu = windll.User32.GetMenu(hwnd)
  58.                 if newMenu != 0 and newMenu != globals()["hmenu"]:
  59.                         globals()["hmenu"] = newMenu
  60.                
  61.                 if show:
  62.                         windll.User32.SetMenu(globals()["hwnd"], 0)
  63.                         globals()["show"] = 0
  64.                 else:
  65.                         windll.User32.SetMenu(globals()["hwnd"], globals()["hmenu"])
  66.                         globals()["show"] = 1
  67.  
  68.                 globals()["needSave"] = 1
  69.                        
  70. class toggleMenubarSecure(sublimeplugin.Plugin):
  71.         def onPreSave(self, view):
  72.                 if globals()["needSave"] == 1:
  73.                         globals()["needSave"] = 0
  74.                         file = open(appdataPath.value+"\\sublime text\\Packages\\User\\toggleMenubar.tmp", 'w')
  75.  
  76.                         file.write(str(globals()["hmenu"]))
  77.                         file.write("\n")
  78.                
  79.                         file.write(str(globals()["hwnd"]))
  80.                         file.write("\n")
  81.                
  82.                         file.write(str(globals()["show"]))
  83.                         file.close()
clone this paste RAW Paste Data