list.py :  » Windows » pyExcelerator » pywin32-214 » pythonwin » pywin » dialogs » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Windows » pyExcelerator 
pyExcelerator » pywin32 214 » pythonwin » pywin » dialogs » list.py
from pywin.mfc import dialog
import win32ui, win32con, commctrl, win32api

class ListDialog (dialog.Dialog):
  
  def __init__ (self, title, list):
    dialog.Dialog.__init__ (self, self._maketemplate(title))
    self.HookMessage (self.on_size, win32con.WM_SIZE)
    self.HookNotify(self.OnListItemChange, commctrl.LVN_ITEMCHANGED)
    self.HookCommand(self.OnListClick, win32ui.IDC_LIST1)
    self.items = list

  def _maketemplate(self, title):
    style = win32con.WS_DLGFRAME | win32con.WS_SYSMENU | win32con.WS_VISIBLE
    ls = (
      win32con.WS_CHILD           |
      win32con.WS_VISIBLE         |
      commctrl.LVS_ALIGNLEFT      |
      commctrl.LVS_REPORT
        )
    bs = (
      win32con.WS_CHILD           |
      win32con.WS_VISIBLE
         )
    return [ [title, (0, 0, 200, 200), style, None, (8, "MS Sans Serif")],
      ["SysListView32", None, win32ui.IDC_LIST1, (0, 0, 200, 200), ls], 
      [128,  "OK", win32con.IDOK, (10, 0, 50, 14), bs | win32con.BS_DEFPUSHBUTTON],
      [128,  "Cancel",win32con.IDCANCEL,(0, 0, 50, 14), bs],
        ]

  def FillList(self):
    size = self.GetWindowRect()
    width = size[2] - size[0] - (10)
    itemDetails = (commctrl.LVCFMT_LEFT, width, "Item", 0)
    self.itemsControl.InsertColumn(0, itemDetails)
    index = 0
    for item in self.items:
      index = self.itemsControl.InsertItem(index+1, str(item), 0)
  
  def OnListClick(self, id, code):
    if code==commctrl.NM_DBLCLK:
      self.EndDialog(win32con.IDOK)
    return 1
  
  def OnListItemChange(self,std, extra):
    (hwndFrom, idFrom, code), (itemNotify, sub, newState, oldState, change, point, lparam) = std, extra
    oldSel = (oldState & commctrl.LVIS_SELECTED)!=0
    newSel = (newState & commctrl.LVIS_SELECTED)!=0
    if oldSel != newSel:
      try:
        self.selecteditem = itemNotify
        self.butOK.EnableWindow(1)
      except win32ui.error:
        self.selecteditem = None
  
  
  def OnInitDialog (self):
    rc = dialog.Dialog.OnInitDialog (self)
    self.itemsControl = self.GetDlgItem(win32ui.IDC_LIST1)
    self.butOK = self.GetDlgItem(win32con.IDOK)
    self.butCancel = self.GetDlgItem(win32con.IDCANCEL)
    
    self.FillList()    
    
    size = self.GetWindowRect()
    self.LayoutControls(size[2]-size[0], size[3]-size[1])
    self.butOK.EnableWindow(0) # wait for first selection
    return rc
    
  def LayoutControls(self, w, h):
    self.itemsControl.MoveWindow((0,0,w,h-30))
    self.butCancel.MoveWindow((10, h-24, 60, h-4))
    self.butOK.MoveWindow((w-60, h-24, w-10, h-4))
  
  def on_size (self, params):
    lparam = params[3]
    w = win32api.LOWORD(lparam)
    h = win32api.HIWORD(lparam)
    self.LayoutControls(w, h)

class ListsDialog(ListDialog):
  def __init__(self, title, list, colHeadings = ['Item']):
    ListDialog.__init__(self, title, list)
    self.colHeadings = colHeadings

  def FillList(self):
    index = 0
    size = self.GetWindowRect()
    width = size[2] - size[0] - (10) - win32api.GetSystemMetrics(win32con.SM_CXVSCROLL)
    numCols = len(self.colHeadings)

    for col in self.colHeadings:
      itemDetails = (commctrl.LVCFMT_LEFT, width/numCols, col, 0)
      self.itemsControl.InsertColumn(index, itemDetails)
      index = index + 1
    index = 0
    for items in self.items:
      index = self.itemsControl.InsertItem(index+1, str(items[0]), 0)
      for itemno in range(1,numCols):
        item = items[itemno]
        self.itemsControl.SetItemText(index, itemno, str(item))
  
def SelectFromList (title, lst):
  dlg = ListDialog(title, lst)
  if dlg.DoModal()==win32con.IDOK:
    return dlg.selecteditem
  else:
    return None
      
def SelectFromLists (title, lists, headings):
  dlg = ListsDialog(title, lists, headings)
  if dlg.DoModal()==win32con.IDOK:
    return dlg.selecteditem
  else:
    return None

def test():
#  print SelectFromList('Single list',  [1,2,3])
  print SelectFromLists('Multi-List', [ ('1',1, 'a'), ('2',2, 'b'), ('3',3, 'c' )], ['Col 1', 'Col 2'])

if __name__=='__main__':  
  test()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.