home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2514 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  9.5 KB  |  228 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from win32api import *
  5.  
  6. try:
  7.     from winxpgui import *
  8. except ImportError:
  9.     from win32gui import *
  10.  
  11. from win32gui_struct import *
  12. import win32con
  13. import sys
  14. import os
  15. import struct
  16. import array
  17. this_dir = os.path.split(sys.argv[0])[0]
  18.  
  19. class MainWindow:
  20.     
  21.     def __init__(self):
  22.         message_map = {
  23.             win32con.WM_DESTROY: self.OnDestroy,
  24.             win32con.WM_COMMAND: self.OnCommand,
  25.             win32con.WM_USER + 20: self.OnTaskbarNotify,
  26.             win32con.WM_MEASUREITEM: self.OnMeasureItem,
  27.             win32con.WM_DRAWITEM: self.OnDrawItem }
  28.         wc = WNDCLASS()
  29.         hinst = wc.hInstance = GetModuleHandle(None)
  30.         wc.lpszClassName = 'PythonTaskbarDemo'
  31.         wc.lpfnWndProc = message_map
  32.         classAtom = RegisterClass(wc)
  33.         style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
  34.         self.hwnd = CreateWindow(classAtom, 'Taskbar Demo', style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hinst, None)
  35.         UpdateWindow(self.hwnd)
  36.         iconPathName = os.path.abspath(os.path.join(sys.prefix, 'pyc.ico'))
  37.         if not os.path.isfile(iconPathName):
  38.             iconPathName = os.path.abspath(os.path.join(os.path.split(sys.executable)[0], 'DLLs', 'pyc.ico'))
  39.         
  40.         if not os.path.isfile(iconPathName):
  41.             iconPathName = os.path.abspath(os.path.join(os.path.split(sys.executable)[0], '..\\PC\\pyc.ico'))
  42.         
  43.         if os.path.isfile(iconPathName):
  44.             icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
  45.             hicon = LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags)
  46.         else:
  47.             iconPathName = None
  48.             print "Can't find a Python icon file - using default"
  49.             hicon = LoadIcon(0, win32con.IDI_APPLICATION)
  50.         self.iconPathName = iconPathName
  51.         ncm = SystemParametersInfo(win32con.SPI_GETNONCLIENTMETRICS)
  52.         self.font_menu = CreateFontIndirect(ncm['lfMenuFont'])
  53.         self.menu_icon_height = GetSystemMetrics(win32con.SM_CYMENU) - 4
  54.         self.menu_icon_width = self.menu_icon_height
  55.         self.icon_x_pad = 8
  56.         self.menu_item_map = { }
  57.         self.createMenu()
  58.         flags = NIF_ICON | NIF_MESSAGE | NIF_TIP
  59.         nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, hicon, 'Python Demo')
  60.         Shell_NotifyIcon(NIM_ADD, nid)
  61.         print 'Please right-click on the Python icon in the taskbar'
  62.  
  63.     
  64.     def createMenu(self):
  65.         self.hmenu = menu = CreatePopupMenu()
  66.         (item, extras) = PackMENUITEMINFO(text = 'Exit', hbmpItem = win32con.HBMMENU_MBAR_CLOSE, wID = 1000)
  67.         InsertMenuItem(menu, 0, 1, item)
  68.         (item, extras) = PackMENUITEMINFO(text = 'Text only item', wID = 1001)
  69.         InsertMenuItem(menu, 0, 1, item)
  70.         load_bmp_flags = win32con.LR_LOADFROMFILE | win32con.LR_LOADTRANSPARENT
  71.         hbmp = LoadImage(0, os.path.join(this_dir, 'images/smiley.bmp'), win32con.IMAGE_BITMAP, 20, 20, load_bmp_flags)
  72.         (item, extras) = PackMENUITEMINFO(text = 'Menu with bitmap', hbmpItem = hbmp, wID = 1002)
  73.         InsertMenuItem(menu, 0, 1, item)
  74.         ico_x = GetSystemMetrics(win32con.SM_CXSMICON)
  75.         ico_y = GetSystemMetrics(win32con.SM_CYSMICON)
  76.         if self.iconPathName:
  77.             hicon = LoadImage(0, self.iconPathName, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE)
  78.         else:
  79.             shell_dll = os.path.join(GetSystemDirectory(), 'shell32.dll')
  80.             (large, small) = win32gui.ExtractIconEx(shell_dll, 4, 1)
  81.             hicon = small[0]
  82.             DestroyIcon(large[0])
  83.         index = 0
  84.         self.menu_item_map[index] = (hicon, 'Menu with owner-draw icon')
  85.         (item, extras) = PackMENUITEMINFO(fType = win32con.MFT_OWNERDRAW, dwItemData = index, wID = 1009)
  86.         InsertMenuItem(menu, 0, 1, item)
  87.         index = 1
  88.         self.menu_item_map[index] = (hicon, None)
  89.         (item, extras) = PackMENUITEMINFO(text = 'Menu with o-d icon 2', dwItemData = index, hbmpItem = win32con.HBMMENU_CALLBACK, wID = 1010)
  90.         InsertMenuItem(menu, 0, 1, item)
  91.         hdcBitmap = CreateCompatibleDC(0)
  92.         hdcScreen = GetDC(0)
  93.         hbm = CreateCompatibleBitmap(hdcScreen, ico_x, ico_y)
  94.         hbmOld = SelectObject(hdcBitmap, hbm)
  95.         SetBkMode(hdcBitmap, win32con.TRANSPARENT)
  96.         brush = GetSysColorBrush(win32con.COLOR_MENU)
  97.         FillRect(hdcBitmap, (0, 0, 16, 16), brush)
  98.         DrawIconEx(hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL)
  99.         SelectObject(hdcBitmap, hbmOld)
  100.         DeleteDC(hdcBitmap)
  101.         (item, extras) = PackMENUITEMINFO(text = 'Menu with icon', hbmpItem = hbm.Detach(), wID = 1011)
  102.         InsertMenuItem(menu, 0, 1, item)
  103.         self.sub_menu = sub_menu = CreatePopupMenu()
  104.         (item, extras) = PackMENUITEMINFO(fState = win32con.MFS_CHECKED, text = 'Checkbox menu', hbmpItem = hbmp, wID = 1003)
  105.         InsertMenuItem(sub_menu, 0, 1, item)
  106.         InsertMenu(sub_menu, 0, win32con.MF_BYPOSITION, win32con.MF_SEPARATOR, None)
  107.         (item, extras) = PackMENUITEMINFO(fType = win32con.MFT_RADIOCHECK, fState = win32con.MFS_CHECKED, text = 'Checkbox menu - bullet 1', hbmpItem = hbmp, wID = 1004)
  108.         InsertMenuItem(sub_menu, 0, 1, item)
  109.         (item, extras) = PackMENUITEMINFO(fType = win32con.MFT_RADIOCHECK, fState = win32con.MFS_UNCHECKED, text = 'Checkbox menu - bullet 2', hbmpItem = hbmp, wID = 1005)
  110.         InsertMenuItem(sub_menu, 0, 1, item)
  111.         (item, extras) = PackMENUITEMINFO(text = 'Sub-Menu', hSubMenu = sub_menu)
  112.         InsertMenuItem(menu, 0, 1, item)
  113.         SetMenuDefaultItem(menu, 1000, 0)
  114.  
  115.     
  116.     def OnDestroy(self, hwnd, msg, wparam, lparam):
  117.         nid = (self.hwnd, 0)
  118.         Shell_NotifyIcon(NIM_DELETE, nid)
  119.         PostQuitMessage(0)
  120.  
  121.     
  122.     def OnTaskbarNotify(self, hwnd, msg, wparam, lparam):
  123.         if lparam == win32con.WM_RBUTTONUP:
  124.             print 'You right clicked me.'
  125.             pos = GetCursorPos()
  126.             SetForegroundWindow(self.hwnd)
  127.             TrackPopupMenu(self.hmenu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None)
  128.             PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
  129.         elif lparam == win32con.WM_LBUTTONDBLCLK:
  130.             print 'You double-clicked me'
  131.             cmd = GetMenuDefaultItem(self.hmenu, False, 0)
  132.             if cmd == -1:
  133.                 print "Can't find a default!"
  134.             
  135.             self.OnCommand(hwnd, win32con.WM_COMMAND, cmd, 0)
  136.         
  137.         return 1
  138.  
  139.     
  140.     def OnCommand(self, hwnd, msg, wparam, lparam):
  141.         id = LOWORD(wparam)
  142.         if id == 1000:
  143.             print 'Goodbye'
  144.             DestroyWindow(self.hwnd)
  145.         elif id in (1003, 1004, 1005):
  146.             state = GetMenuState(self.sub_menu, id, win32con.MF_BYCOMMAND)
  147.             if state == -1:
  148.                 raise RuntimeError, 'No item found'
  149.             state == -1
  150.             if state & win32con.MF_CHECKED:
  151.                 check_flags = win32con.MF_UNCHECKED
  152.                 print 'Menu was checked - unchecking'
  153.             else:
  154.                 check_flags = win32con.MF_CHECKED
  155.                 print 'Menu was unchecked - checking'
  156.             if id == 1003:
  157.                 rc = CheckMenuItem(self.sub_menu, id, win32con.MF_BYCOMMAND | check_flags)
  158.             else:
  159.                 rc = CheckMenuRadioItem(self.sub_menu, 1004, 1005, id, win32con.MF_BYCOMMAND)
  160.             (buf, extras) = EmptyMENUITEMINFO()
  161.             win32gui.GetMenuItemInfo(self.sub_menu, id, False, buf)
  162.             (fType, fState, wID, hSubMenu, hbmpChecked, hbmpUnchecked, dwItemData, text, hbmpItem) = UnpackMENUITEMINFO(buf)
  163.             if fState & win32con.MF_CHECKED != check_flags:
  164.                 raise RuntimeError, "The new item didn't get the new checked state!"
  165.             fState & win32con.MF_CHECKED != check_flags
  166.         else:
  167.             print 'OnCommand for ID', id
  168.  
  169.     
  170.     def OnMeasureItem(self, hwnd, msg, wparam, lparam):
  171.         fmt = '5iP'
  172.         buf = PyMakeBuffer(struct.calcsize(fmt), lparam)
  173.         data = struct.unpack(fmt, buf)
  174.         (ctlType, ctlID, itemID, itemWidth, itemHeight, itemData) = data
  175.         (hicon, text) = self.menu_item_map[itemData]
  176.         if text is None:
  177.             cx = self.menu_icon_width
  178.             cy = self.menu_icon_height
  179.         else:
  180.             dc = GetDC(hwnd)
  181.             oldFont = SelectObject(dc, self.font_menu)
  182.             (cx, cy) = GetTextExtentPoint32(dc, text)
  183.             SelectObject(dc, oldFont)
  184.             ReleaseDC(hwnd, dc)
  185.             cx += GetSystemMetrics(win32con.SM_CXMENUCHECK)
  186.             cx += self.menu_icon_width + self.icon_x_pad
  187.             cy = GetSystemMetrics(win32con.SM_CYMENU)
  188.         new_data = struct.pack(fmt, ctlType, ctlID, itemID, cx, cy, itemData)
  189.         PySetMemory(lparam, new_data)
  190.         return True
  191.  
  192.     
  193.     def OnDrawItem(self, hwnd, msg, wparam, lparam):
  194.         fmt = '5i2P4iP'
  195.         data = struct.unpack(fmt, PyGetString(lparam, struct.calcsize(fmt)))
  196.         (ctlType, ctlID, itemID, itemAction, itemState, hwndItem, hDC, left, top, right, bot, itemData) = data
  197.         rect = (left, top, right, bot)
  198.         (hicon, text) = self.menu_item_map[itemData]
  199.         if text is None:
  200.             DrawIconEx(hDC, left, top, hicon, right - left, bot - top, 0, 0, win32con.DI_NORMAL)
  201.         else:
  202.             selected = itemState & win32con.ODS_SELECTED
  203.             if selected:
  204.                 crText = SetTextColor(hDC, GetSysColor(win32con.COLOR_HIGHLIGHTTEXT))
  205.                 crBkgnd = SetBkColor(hDC, GetSysColor(win32con.COLOR_HIGHLIGHT))
  206.             
  207.             each_pad = self.icon_x_pad / 2
  208.             x_icon = left + GetSystemMetrics(win32con.SM_CXMENUCHECK) + each_pad
  209.             x_text = x_icon + self.menu_icon_width + each_pad
  210.             hfontOld = SelectObject(hDC, self.font_menu)
  211.             ExtTextOut(hDC, x_text, top + 2, win32con.ETO_OPAQUE, rect, text)
  212.             SelectObject(hDC, hfontOld)
  213.             DrawIconEx(hDC, x_icon, top + 2, hicon, self.menu_icon_width, self.menu_icon_height, 0, 0, win32con.DI_NORMAL)
  214.             if selected:
  215.                 SetTextColor(hDC, crText)
  216.                 SetBkColor(hDC, crBkgnd)
  217.             
  218.  
  219.  
  220.  
  221. def main():
  222.     w = MainWindow()
  223.     PumpMessages()
  224.  
  225. if __name__ == '__main__':
  226.     main()
  227.  
  228.