home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / gui / textutil.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  13.0 KB  |  379 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import wx
  5. import string
  6. from util.introspect import memoize
  7. from util.primitives import Point2HTMLSize
  8. from util.xml_tag import tag
  9. from util.lrucache import LRU
  10.  
  11. def FindAny(s, chrs, start = None, end = None):
  12.     chrs = set(chrs)
  13.     for i in xrange(0, len(s)):
  14.         if s[i] in chrs:
  15.             return i
  16.     return -1
  17.  
  18.  
  19. def rFindAny(s, chrs, start = None, end = None):
  20.     chrs = set(chrs)
  21.     for i in xrange(len(s) - 1, 0, -1):
  22.         if s[i] in chrs:
  23.             return i
  24.     return -1
  25.  
  26.  
  27. def ContainsNumbers(s):
  28.     return FindAny(s, string.digits) != -1
  29.  
  30. rtlCharRanges = [
  31.     (8235, 8235),
  32.     (1424, 1535),
  33.     (1536, 1791),
  34.     (1872, 1919),
  35.     (64285, 64320),
  36.     (64336, 65023),
  37.     (65136, 65279)]
  38.  
  39. def isRTL(char):
  40.     charcode = ord(char)
  41.     for charRange in rtlCharRanges:
  42.         if charcode >= charRange[0] and charcode <= charRange[1]:
  43.             return True
  44.     
  45.     return False
  46.  
  47. VISTA_SHELL_FONT = u'Segoe UI'
  48. VISTA_SHELL_FONT_SIZE = 9
  49. _return_default_font = None
  50.  
  51. def _find_default():
  52.     global _return_default_font, _return_default_font
  53.     faces = GetFonts()
  54.     
  55.     try:
  56.         import ctypes
  57.         ctypes.windll.dwmapi
  58.         vista = True
  59.     except:
  60.         vista = False
  61.  
  62.     if vista and VISTA_SHELL_FONT in faces:
  63.         
  64.         _return_default_font = lambda : wx.Font(VISTA_SHELL_FONT_SIZE, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, VISTA_SHELL_FONT)
  65.     else:
  66.         
  67.         _return_default_font = lambda : wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
  68.  
  69.  
  70. def default_font():
  71.     if _return_default_font is None:
  72.         _find_default()
  73.     
  74.     return _return_default_font()
  75.  
  76.  
  77. def shorten_all_links(textctrl, ondone = None, timeoutms = 5000):
  78.     import sys
  79.     LinkAccumulator = LinkAccumulator
  80.     get_short_url = get_short_url
  81.     is_short_url = is_short_url
  82.     import util.net
  83.     threaded = threaded
  84.     import util
  85.     links = LinkAccumulator(textctrl.Value)
  86.     all_links = set(links.links)
  87.     all_links = _[1]
  88.     if not all_links:
  89.         return None
  90.     links.count = len(all_links)
  91.     links.finished = False
  92.     
  93.     def finish():
  94.         
  95.         def after():
  96.             if not links.finished:
  97.                 links.finished = True
  98.                 textctrl.Enable()
  99.                 if ondone is not None:
  100.                     ondone()
  101.                 
  102.             
  103.  
  104.         after = (None, None, wx.CallAfter)(after)
  105.  
  106.     if timeoutms is not None:
  107.         wx.CallLater(timeoutms, finish)
  108.     
  109.     textctrl.Disable()
  110.     for link in all_links:
  111.         
  112.         def shorten(link = (None, None, None, None, None, link)):
  113.             
  114.             def one_link_shortened():
  115.                 links.count -= 1
  116.                 if links.count == 0:
  117.                     finish()
  118.                 
  119.  
  120.             
  121.             def success(short_url):
  122.                 
  123.                 def after():
  124.                     if links.finished:
  125.                         return None
  126.                     textctrl.SetValue(textctrl.Value.replace(link, short_url))
  127.                     one_link_shortened()
  128.  
  129.                 after = (None, None, None, None, wx.CallAfter)(after)
  130.  
  131.             
  132.             def error():
  133.                 print >>sys.stderr, 'error shortening link: %r' % link
  134.                 one_link_shortened()
  135.  
  136.             threaded(get_short_url)(link, success = success, error = error)
  137.  
  138.         shorten(link)
  139.     
  140.  
  141.  
  142. class VisualCharacterLimit(object):
  143.     
  144.     def __init__(self, textctrl, limit, length = None):
  145.         self.ctrl = textctrl
  146.         self.limit = limit
  147.         self.length = None if length is not None else len
  148.         self.default_style = wx.TextAttr(wx.BLACK, wx.WHITE)
  149.         self.limit_style = wx.TextAttr(wx.WHITE, wx.RED)
  150.         self.needs_style_update = False
  151.         self.in_set_style = False
  152.         self.ctrl.Bind(wx.EVT_TEXT, self._on_text)
  153.         self._on_text()
  154.  
  155.     
  156.     def SetLimit(self, limit, refresh = True):
  157.         self.limit = limit
  158.         if refresh:
  159.             self._on_text()
  160.         
  161.  
  162.     
  163.     def _on_text(self, e = None):
  164.         if e is not None:
  165.             e.Skip()
  166.         
  167.         if self.in_set_style:
  168.             return None
  169.         value = self.ctrl.Value
  170.         limit = self.in_set_style if callable(self.limit) else self.limit
  171.         if self.needs_style_update or self.length(value) > limit:
  172.             self.needs_style_update = True
  173.             self.in_set_style = True
  174.             self.ctrl.SetStyle(0, limit, self.default_style)
  175.             self.ctrl.SetStyle(limit, self.ctrl.LastPosition, self.limit_style)
  176.             self.in_set_style = False
  177.         else:
  178.             self.needs_style_update = False
  179.  
  180.  
  181. aimsizes = {
  182.     8: 1,
  183.     10: 2,
  184.     12: 3,
  185.     14: 4,
  186.     18: 5,
  187.     24: 6,
  188.     36: 7 }
  189.  
  190. def attrToHTML(textattr):
  191.     font = textattr.Font
  192.     attrs = { }
  193.     if textattr.HasTextColour() and textattr.GetTextColour() != wx.BLACK:
  194.         attrs['color'] = textattr.GetTextColour().GetAsString(wx.C2S_HTML_SYNTAX)
  195.     
  196.     attrs.update(size = str(aimsizes.get(font.PointSize // 15, 4)), face = unicode(font.FaceName))
  197.     start = tag('font', **attrs)._to_xml(self_closing = False, pretty = False)[:-7]
  198.     end = '</font>'
  199.     if font.Weight == wx.FONTWEIGHT_BOLD:
  200.         start += '<b>'
  201.         end = '</b>' + end
  202.     
  203.     if font.Style == wx.FONTSTYLE_ITALIC:
  204.         start += '<i>'
  205.         end = '</i>' + end
  206.     
  207.     if font.Underlined:
  208.         start += '<u>'
  209.         end = '</u>' + end
  210.     
  211.     return (start, end)
  212.  
  213.  
  214. def TagFont(string, fonttype, fonts):
  215.     font = fonts[fonttype]
  216.     color = fonts['%sfc' % fonttype]
  217.     tag = None([
  218.         None,
  219.         None,
  220.         None,
  221.         None,
  222.         None,
  223.         None,
  224.         u''.join,
  225.         '<font face="',
  226.         font.FaceName,
  227.         '" size="',
  228.         str(Point2HTMLSize(fonts[fonttype].PointSize)),
  229.         '" color="',
  230.         color,
  231.         '">' if font.Weight == wx.BOLD else '' if font.Style == wx.ITALIC else '' if font.Underlined else '' if isinstance(string, unicode) else string.decode('utf-8') if font.Underlined else '' if font.Style == wx.ITALIC else '' if font.Weight == wx.BOLD else '',
  232.         '</font>'])
  233.     return tag
  234.  
  235.  
  236. def tagfontxml(string, fonttype, fonts):
  237.     E = E
  238.     import lxml.builder
  239.     font = fonts[fonttype]
  240.     color = fonts['%sfc' % fonttype]
  241.     out = string
  242.     if isinstance(out, str):
  243.         out = out.decode('utf-8')
  244.     
  245.     conditions = ((font.Underlined, 'u'), (font.Style == wx.ITALIC, 'i'), (font.Weight == wx.BOLD, 'b'))
  246.     for condition, tag in conditions:
  247.         if condition:
  248.             out = getattr(E, tag)(out)
  249.             continue
  250.     
  251.     out = E.span(out, style = 'font-family: %(facename)s; font-size: %(size)ipt; color: %(color)s;' % {
  252.         'facename': font.FaceName,
  253.         'size': font.PointSize,
  254.         'color': color })
  255.     return out
  256.  
  257. font_init_args = ('pointSize', 'family', 'style', 'weight', 'underline', 'faceName', 'encoding')
  258.  
  259. def CopyFont(font, **kwargs):
  260.     f = fontkwargs = dict(pointSize = font.PointSize, family = font.Family, style = font.Style, weight = font.Weight, underline = font.Underlined, faceName = font.FaceName, encoding = font.Encoding)
  261.     fontkwargs.update(kwargs)
  262.     if 'underlined' in fontkwargs:
  263.         fontkwargs['underline'] = fontkwargs.pop('underlined')
  264.     
  265.     init_args = font_init_args
  266.     if hasattr(wx.Font, 'init_args'):
  267.         init_args = wx.Font.init_args
  268.     
  269.     return (wx.Font,)(*(lambda .0: for a in .0:
  270. f.get(a))(init_args))
  271.  
  272. if 'wxMac' in wx.PlatformInfo:
  273.     
  274.     get_measuring_context = lambda : wx.ClientDC(wx.GetTopLevelWindows()[0])
  275. else:
  276.     get_measuring_context = wx.MemoryDC
  277. _sizecache = LRU(100)
  278.  
  279. def GetMultilineTextSize(text, font = None, dc = None):
  280.     if not dc:
  281.         pass
  282.     dc = get_measuring_context()
  283.     if font:
  284.         dc.SetFont(font)
  285.     else:
  286.         font = dc.Font
  287.     nativeinfo = font.NativeFontInfoDesc + text
  288.     
  289.     try:
  290.         ext = _sizecache[nativeinfo]
  291.     except KeyError:
  292.         _sizecache[nativeinfo] = ext = dc.GetMultiLineTextExtent(text)[:2]
  293.  
  294.     return wx.Size(*ext)
  295.  
  296. _widthcache = LRU(100)
  297.  
  298. def GetTextWidth(line, font = None, dc = None):
  299.     if not dc:
  300.         pass
  301.     dc = get_measuring_context()
  302.     if font:
  303.         dc.SetFont(font)
  304.     else:
  305.         font = dc.Font
  306.     nativeinfo = font.NativeFontInfoDesc + line
  307.     
  308.     try:
  309.         width = _widthcache[nativeinfo]
  310.     except KeyError:
  311.         _widthcache[nativeinfo] = width = dc.GetFullTextExtent(line)[0]
  312.  
  313.     return width
  314.  
  315.  
  316. wx.Font.__hash__ = lambda f: hash(f.NativeFontInfoDesc)
  317. _heightcache = LRU(100)
  318.  
  319. def GetFontHeight(font = None, dc = None, line_height = False, descent = False):
  320.     if not dc:
  321.         pass
  322.     dc = get_measuring_context()
  323.     if font:
  324.         dc.SetFont(font)
  325.     else:
  326.         font = dc.Font
  327.     nativeinfo = font.NativeFontInfoDesc
  328.     
  329.     try:
  330.         extents = _heightcache[nativeinfo]
  331.     except KeyError:
  332.         _heightcache[nativeinfo] = extents = dc.GetFullTextExtent(string.ascii_letters)
  333.  
  334.     if line_height:
  335.         return extents[1]
  336.     if descent:
  337.         return extents[2]
  338.     return (extents[1] - extents[2]) + extents[3]
  339.  
  340. wx.Font.GetHeight = GetFontHeight
  341. wx.Font.Height = property((lambda f: GetFontHeight(f)))
  342. wx.Font.LineHeight = property((lambda f: GetFontHeight(f, line_height = True)))
  343. wx.Font.Descent = property((lambda f: GetFontHeight(f, descent = True)))
  344.  
  345. def GetTextExtent(text, font = None, dc = None):
  346.     if not dc:
  347.         pass
  348.     dc = get_measuring_context()
  349.     if font:
  350.         dc.SetFont(font)
  351.     
  352.     return dc.GetTextExtent(text)
  353.  
  354.  
  355. def DeAmp(text):
  356.     return text.replace('&', '', 1)
  357.  
  358. from cgui import truncateText as TruncateText
  359.  
  360. def dcDTTfunc(self, text, rect, alignment = wx.ALIGN_LEFT | wx.ALIGN_TOP, indexAccel = -1):
  361.     self.DrawLabel(TruncateText(text, rect.width, None, self), rect, alignment, indexAccel)
  362.  
  363. wx.DC.DrawTruncatedText = dcDTTfunc
  364.  
  365. def dcDTTfuncInfo(self, text, rect, alignment = wx.ALIGN_LEFT | wx.ALIGN_TOP, indexAccel = -1):
  366.     txt = TruncateText(text, rect.width, None, self)
  367.     self.DrawLabel(txt, rect, alignment, indexAccel)
  368.     return txt == text
  369.  
  370. wx.DC.DrawTruncatedTextInfo = dcDTTfuncInfo
  371.  
  372. def GetFonts():
  373.     return sorted(set((lambda .0: for f in .0:
  374. f.lstrip('@'))(wx.FontEnumerator().GetFacenames())))
  375.  
  376. GetFonts = memoize(GetFonts)
  377. fontattrs = ('PointSize', 'Family', 'Style', 'Weight', 'Underlined', 'FaceName')
  378. from cgui import Wrap
  379.