📄 folderselector.py
字号:
"valid:", details valid = False if not valid: if result_valid: # are we the first invalid? self.manager.ReportInformation( "Please select a child folder - top-level folders " \ "can not be used.") self.UnselectItem(info) result_valid = result_valid and valid return result_valid finally: self.in_check_selections_valid = False # Message processing# def GetMessageMap(self): def OnInitDialog (self, hwnd, msg, wparam, lparam): FolderSelector_Parent.OnInitDialog(self, hwnd, msg, wparam, lparam) caption = "%s folder" % (self.select_desc_noun,) if not self.single_select: caption += "(s)" win32gui.SendMessage(hwnd, win32con.WM_SETTEXT, 0, caption) self.SetDlgItemText("IDC_BUT_SEARCHSUB", self.checkbox_text) child = self.GetDlgItem("IDC_BUT_SEARCHSUB") if self.checkbox_state is None: win32gui.ShowWindow(child, win32con.SW_HIDE) else: win32gui.SendMessage(child, win32con.BM_SETCHECK, self.checkbox_state) self.list = self.GetDlgItem("IDC_LIST_FOLDERS") import resources mod_handle, mod_bmp, extra_flags = \ resources.GetImageParamsFromBitmapID(self.dialog_parser, "IDB_FOLDERS") bitmapMask = win32api.RGB(0,0,255) self.imageList = win32gui.ImageList_LoadImage(mod_handle, mod_bmp, 16, 0, bitmapMask, win32con.IMAGE_BITMAP, extra_flags) win32gui.SendMessage( self.list, commctrl.TVM_SETIMAGELIST, commctrl.TVSIL_NORMAL, self.imageList ) if self.single_select: # Remove the checkbox style from the list for single-selection style = win32api.GetWindowLong(self.list, win32con.GWL_STYLE) style = style & ~commctrl.TVS_CHECKBOXES win32api.SetWindowLong(self.list, win32con.GWL_STYLE, style) # Hide "clear all" child = self.GetDlgItem("IDC_BUT_CLEARALL") win32gui.ShowWindow(child, win32con.SW_HIDE) # Extended MAPI version of the tree. # Build list of all ids to expand - ie, list includes all # selected folders, and all parents. dlgutils.SetWaitCursor(1) self.expand_ids = self._DetermineFoldersToExpand() tree = BuildFolderTreeMAPI(self.manager.message_store.session, self.exclude_prop_ids) self._InsertSubFolders(0, tree) self.selected_ids = [] # Only use this while creating dialog. self.expand_ids = [] # Only use this while creating dialog. self._UpdateStatus() dlgutils.SetWaitCursor(0) def OnDestroy(self, hwnd, msg, wparam, lparam): import timer if self.timer_id is not None: timer.kill_timer(self.timer_id) self.item_map = None if self.imageList: win32gui.ImageList_Destroy(self.imageList) FolderSelector_Parent.OnDestroy(self, hwnd, msg, wparam, lparam) def OnCommand(self, hwnd, msg, wparam, lparam): FolderSelector_Parent.OnCommand(self, hwnd, msg, wparam, lparam) id = win32api.LOWORD(wparam) id_name = self._GetIDName(id) code = win32api.HIWORD(wparam) if code == win32con.BN_CLICKED: if id in (win32con.IDOK, win32con.IDCANCEL) and self.in_label_edit: cancel = id == win32con.IDCANCEL win32gui.SendMessage(self.list, commctrl.TVM_ENDEDITLABELNOW, cancel,0) return # Button clicks if id == win32con.IDOK: if not self._CheckSelectionsValid(True): return self.selected_ids, self.checkbox_state = self.GetSelectedIDs() win32gui.EndDialog(hwnd, id) elif id == win32con.IDCANCEL: win32gui.EndDialog(hwnd, id) elif id_name == "IDC_BUT_CLEARALL": for info, spec in self._YieldCheckedChildren(): self.UnselectItem(info) elif id_name == "IDC_BUT_NEW": # Force a new entry in the tree at our location, and begin # editing. # Add the new item to the tree. h = win32gui.SendMessage(self.list, commctrl.TVM_GETNEXTITEM, commctrl.TVGN_CARET, commctrl.TVI_ROOT) parent_item = self._GetTVItem(h) if parent_item[6]==0: # eeek - parent has no existig children - say we have one # so we can be expanded. update_item, extra = PackTVITEM(h, None, None, None, None, None, 1, None) win32gui.SendMessage(self.list, commctrl.TVM_SETITEM, 0, update_item) item_id = self._MakeItemParam(None) temp_spec = FolderSpec(None, "New folder") hnew = self._InsertFolder(h, temp_spec, None, commctrl.TVI_FIRST) win32gui.SendMessage(self.list, commctrl.TVM_ENSUREVISIBLE, 0, hnew) win32gui.SendMessage(self.list, commctrl.TVM_SELECTITEM, commctrl.TVGN_CARET, hnew) # Allow label editing s = win32api.GetWindowLong(self.list, win32con.GWL_STYLE) s |= commctrl.TVS_EDITLABELS win32api.SetWindowLong(self.list, win32con.GWL_STYLE, s) win32gui.SetFocus(self.list) self.in_label_edit = True win32gui.SendMessage(self.list, commctrl.TVM_EDITLABEL, 0, hnew) self._UpdateStatus() def _DoUpdateStatus(self, id, timeval): import timer # Kill the timer first to prevent it firing again. self.timer_id = None timer.kill_timer(id) self._CheckSelectionsValid() names = [] num_checked = 0 for info, spec in self._YieldCheckedChildren(): num_checked += 1 if len(names) < 20: names.append(info[3]) status_string = "%s%s %d folder" % (self.select_desc_noun, self.select_desc_noun_suffix, num_checked) if num_checked != 1: status_string += "s" self.SetDlgItemText("IDC_STATUS1", status_string) self.SetDlgItemText("IDC_STATUS2", "; ".join(names)) def _UpdateStatus(self): # We have problems with the order of events - we get the notification # events before the new states are available via GetItem. # Therefore, we start a one-shot, immediate timer, which ends up # at the end of the message queue, and we work. import timer if self.timer_id is not None: timer.kill_timer(self.timer_id) self.timer_id = timer.set_timer (0, self._DoUpdateStatus) def OnNotify(self, msg, hwnd, wparam, lparam): FolderSelector_Parent.OnNotify(self, hwnd, msg, wparam, lparam) format = "iii" buf = win32gui.PyMakeBuffer(struct.calcsize(format), lparam) hwndFrom, id, code = struct.unpack(format, buf) code += commctrl.PY_0U # work around silly old pywin32 bug id_name = self._GetIDName(id) if id_name == "IDC_LIST_FOLDERS": if code == commctrl.NM_CLICK: self._UpdateStatus() elif code == commctrl.NM_DBLCLK: # No special dblclick handling - default behaviour is to # expand/collapse tree, and auto-closing the dialog, even # when the folder has no children, doesn't really make sense. pass elif code == commctrl.TVN_ITEMEXPANDING: ignore, ignore, ignore, action, itemOld, itemNew = \ UnpackTVNOTIFY(lparam) if action == 1: return 0 # contracting, not expanding itemHandle = itemNew[0] info = itemNew folderSpec = self.item_map[info[7]] if folderSpec.children is None: folderSpec.children = _BuildFoldersMAPI(self.manager, folderSpec) self._InsertSubFolders(itemHandle, folderSpec) elif code == commctrl.TVN_SELCHANGED: self._UpdateStatus() elif code == commctrl.TVN_ENDLABELEDIT: ignore, ignore, ignore, item = UnpackTVDISPINFO(lparam) handle = item[0] stay_in_edit = False try: name = item[3] if name is None: # User cancelled folder creation - delete the item win32gui.SendMessage(self.list, commctrl.TVM_DELETEITEM, 0, handle) return # Attempt to create a folder of that name. parent_handle = win32gui.SendMessage(self.list, commctrl.TVM_GETNEXTITEM, commctrl.TVGN_PARENT, handle) parent_item = self._GetTVItem(parent_handle) parent_spec = self.item_map[parent_item[7]] parent_folder = self.manager.message_store.GetFolder(parent_spec.folder_id) try: new_folder = parent_folder.CreateFolder(name) # Create a new FolderSpec for this folder, and stash new_spec = FolderSpec(new_folder.GetID(), name) # The info passed by the notify message appears to # not have the lparam (even though the docs say it # does.) Fetch it spec_key = self._GetTVItem(handle)[7] self.item_map[spec_key] = new_spec # And update the tree with the new item buf, extra = PackTVITEM(handle, None, None, name, None, None, None, None) win32gui.SendMessage(self.list, commctrl.TVM_SETITEM, 0, buf) except pythoncom.com_error, details: hr, msg, exc, arg = details if hr == mapi.MAPI_E_COLLISION: user_msg = "A folder with that name already exists" else: user_msg = "MAPI error %s" % mapiutil.GetScodeString(hr) self.manager.ReportError("Could not create the folder\r\n\r\n" + user_msg) stay_in_edit = True finally: if stay_in_edit: win32gui.SendMessage(self.list, commctrl.TVM_EDITLABEL, 0, handle) else: # reset to no label edits s = win32api.GetWindowLong(self.list, win32con.GWL_STYLE) s &= ~commctrl.TVS_EDITLABELS win32api.SetWindowLong(self.list, win32con.GWL_STYLE, s) self.in_label_edit = Falsedef Test(): single_select =False import sys, os sys.path.append(os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), ".."))) import manager mgr = manager.GetManager() if mgr.dialog_parser is None: import dialogs mgr.dialog_parser = dialogs.LoadDialogs() ids = [("0000","0000"),] # invalid ID for testing. d=FolderSelector(0, mgr, ids, single_select = single_select) if d.DoModal() != win32con.IDOK: print "Cancelled" return ids, include_sub = d.GetSelectedIDs() d=FolderSelector(0, mgr, ids, single_select = single_select, checkbox_state = include_sub) d.DoModal()if __name__=='__main__': verbose = 1 Test()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -