home *** CD-ROM | disk | FTP | other *** search
/ Freelog 116 / FreelogNo116-JuilletSeptembre2013.iso / Bureautique / gImageReader / gimagereader_0.9-1_win32.exe / bin / pywin / dialogs / status.pyc (.txt) < prev   
Python Compiled Bytecode  |  2011-03-24  |  9KB  |  251 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. from pywin.mfc import dialog
  5. from pywin.mfc.thread import WinThread
  6. import threading
  7. import win32ui
  8. import win32con
  9. import win32api
  10. import time
  11.  
  12. def MakeProgressDlgTemplate(caption, staticText = ''):
  13.     style = win32con.DS_MODALFRAME | win32con.WS_POPUP | win32con.WS_VISIBLE | win32con.WS_CAPTION | win32con.WS_SYSMENU | win32con.DS_SETFONT
  14.     cs = win32con.WS_CHILD | win32con.WS_VISIBLE
  15.     w = 215
  16.     h = 36
  17.     h = 40
  18.     dlg = [
  19.         [
  20.             caption,
  21.             (0, 0, w, h),
  22.             style,
  23.             None,
  24.             (8, 'MS Sans Serif')]]
  25.     s = win32con.WS_TABSTOP | cs
  26.     dlg.append([
  27.         130,
  28.         staticText,
  29.         1000,
  30.         (7, 7, w - 7, h - 32),
  31.         cs | win32con.SS_LEFT])
  32.     return dlg
  33.  
  34.  
  35. class CStatusProgressDialog(dialog.Dialog):
  36.     
  37.     def __init__(self, title, msg = '', maxticks = 100, tickincr = 1):
  38.         self.initMsg = msg
  39.         templ = MakeProgressDlgTemplate(title, msg)
  40.         dialog.Dialog.__init__(self, templ)
  41.         self.maxticks = maxticks
  42.         self.tickincr = tickincr
  43.         self.pbar = None
  44.  
  45.     
  46.     def OnInitDialog(self):
  47.         rc = dialog.Dialog.OnInitDialog(self)
  48.         self.static = self.GetDlgItem(1000)
  49.         self.pbar = win32ui.CreateProgressCtrl()
  50.         self.pbar.CreateWindow(win32con.WS_CHILD | win32con.WS_VISIBLE, (10, 30, 310, 44), self, 1001)
  51.         self.pbar.SetRange(0, self.maxticks)
  52.         self.pbar.SetStep(self.tickincr)
  53.         self.progress = 0
  54.         self.pincr = 5
  55.         return rc
  56.  
  57.     
  58.     def Close(self):
  59.         self.EndDialog(0)
  60.  
  61.     
  62.     def SetMaxTicks(self, maxticks):
  63.         if self.pbar is not None:
  64.             self.pbar.SetRange(0, maxticks)
  65.  
  66.     
  67.     def Tick(self):
  68.         if self.pbar is not None:
  69.             self.pbar.StepIt()
  70.  
  71.     
  72.     def SetTitle(self, text):
  73.         self.SetWindowText(text)
  74.  
  75.     
  76.     def SetText(self, text):
  77.         self.SetDlgItemText(1000, text)
  78.  
  79.     
  80.     def Set(self, pos, max = None):
  81.         if self.pbar is not None:
  82.             self.pbar.SetPos(pos)
  83.             if max is not None:
  84.                 self.pbar.SetRange(0, max)
  85.             
  86.  
  87.  
  88. MYWM_SETTITLE = win32con.WM_USER + 10
  89. MYWM_SETMSG = win32con.WM_USER + 11
  90. MYWM_TICK = win32con.WM_USER + 12
  91. MYWM_SETMAXTICKS = win32con.WM_USER + 13
  92. MYWM_SET = win32con.WM_USER + 14
  93.  
  94. class CThreadedStatusProcessDialog(CStatusProgressDialog):
  95.     
  96.     def __init__(self, title, msg = '', maxticks = 100, tickincr = 1):
  97.         self.title = title
  98.         self.msg = msg
  99.         self.threadid = win32api.GetCurrentThreadId()
  100.         CStatusProgressDialog.__init__(self, title, msg, maxticks, tickincr)
  101.  
  102.     
  103.     def OnInitDialog(self):
  104.         rc = CStatusProgressDialog.OnInitDialog(self)
  105.         self.HookMessage(self.OnTitle, MYWM_SETTITLE)
  106.         self.HookMessage(self.OnMsg, MYWM_SETMSG)
  107.         self.HookMessage(self.OnTick, MYWM_TICK)
  108.         self.HookMessage(self.OnMaxTicks, MYWM_SETMAXTICKS)
  109.         self.HookMessage(self.OnSet, MYWM_SET)
  110.         return rc
  111.  
  112.     
  113.     def _Send(self, msg):
  114.         
  115.         try:
  116.             self.PostMessage(msg)
  117.         except win32ui.error:
  118.             pass
  119.  
  120.  
  121.     
  122.     def OnTitle(self, msg):
  123.         CStatusProgressDialog.SetTitle(self, self.title)
  124.  
  125.     
  126.     def OnMsg(self, msg):
  127.         CStatusProgressDialog.SetText(self, self.msg)
  128.  
  129.     
  130.     def OnTick(self, msg):
  131.         CStatusProgressDialog.Tick(self)
  132.  
  133.     
  134.     def OnMaxTicks(self, msg):
  135.         CStatusProgressDialog.SetMaxTicks(self, self.maxticks)
  136.  
  137.     
  138.     def OnSet(self, msg):
  139.         CStatusProgressDialog.Set(self, self.pos, self.max)
  140.  
  141.     
  142.     def Close(self):
  143.         if not self.threadid:
  144.             raise AssertionError, 'No thread!'
  145.         None.PostThreadMessage(self.threadid, win32con.WM_QUIT, 0, 0)
  146.  
  147.     
  148.     def SetMaxTicks(self, maxticks):
  149.         self.maxticks = maxticks
  150.         self._Send(MYWM_SETMAXTICKS)
  151.  
  152.     
  153.     def SetTitle(self, title):
  154.         self.title = title
  155.         self._Send(MYWM_SETTITLE)
  156.  
  157.     
  158.     def SetText(self, text):
  159.         self.msg = text
  160.         self._Send(MYWM_SETMSG)
  161.  
  162.     
  163.     def Tick(self):
  164.         self._Send(MYWM_TICK)
  165.  
  166.     
  167.     def Set(self, pos, max = None):
  168.         self.pos = pos
  169.         self.max = max
  170.         self._Send(MYWM_SET)
  171.  
  172.  
  173.  
  174. class ProgressThread(WinThread):
  175.     
  176.     def __init__(self, title, msg = '', maxticks = 100, tickincr = 1):
  177.         self.title = title
  178.         self.msg = msg
  179.         self.maxticks = maxticks
  180.         self.tickincr = tickincr
  181.         self.dialog = None
  182.         WinThread.__init__(self)
  183.         self.createdEvent = threading.Event()
  184.  
  185.     
  186.     def InitInstance(self):
  187.         self.dialog = CThreadedStatusProcessDialog(self.title, self.msg, self.maxticks, self.tickincr)
  188.         self.dialog.CreateWindow()
  189.         
  190.         try:
  191.             self.dialog.SetForegroundWindow()
  192.         except win32ui.error:
  193.             pass
  194.  
  195.         self.createdEvent.set()
  196.         return WinThread.InitInstance(self)
  197.  
  198.     
  199.     def ExitInstance(self):
  200.         return 0
  201.  
  202.  
  203.  
  204. def StatusProgressDialog(title, msg = '', maxticks = 100, parent = None):
  205.     d = CStatusProgressDialog(title, msg, maxticks)
  206.     d.CreateWindow(parent)
  207.     return d
  208.  
  209.  
  210. def ThreadedStatusProgressDialog(title, msg = '', maxticks = 100):
  211.     t = ProgressThread(title, msg, maxticks)
  212.     t.CreateThread()
  213.     end_time = time.time() + 10
  214.     while time.time() < end_time:
  215.         if t.createdEvent.isSet():
  216.             break
  217.         win32ui.PumpWaitingMessages()
  218.         time.sleep(0.1)
  219.     return t.dialog
  220.  
  221.  
  222. def demo():
  223.     d = StatusProgressDialog('A Demo', 'Doing something...')
  224.     import win32api
  225.     for i in range(100):
  226.         if i == 50:
  227.             d.SetText('Getting there...')
  228.         if i == 90:
  229.             d.SetText('Nearly done...')
  230.         win32api.Sleep(20)
  231.         d.Tick()
  232.     
  233.     d.Close()
  234.  
  235.  
  236. def thread_demo():
  237.     d = ThreadedStatusProgressDialog('A threaded demo', 'Doing something')
  238.     import win32api
  239.     for i in range(100):
  240.         if i == 50:
  241.             d.SetText('Getting there...')
  242.         if i == 90:
  243.             d.SetText('Nearly done...')
  244.         win32api.Sleep(20)
  245.         d.Tick()
  246.     
  247.     d.Close()
  248.  
  249. if __name__ == '__main__':
  250.     thread_demo()
  251.