home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / gui / pref / prefsdialog.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  14.9 KB  |  406 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. import wx
  6. from gui.textutil import default_font
  7. from gui.toolbox import build_button_sizer, snap_pref, persist_window_pos, AutoDC, calllimit
  8. from gui.validators import LengthLimit
  9. from gui.pref.prefstrings import all as allprefstrings, tabnames
  10. from util import import_function, Delegate, traceguard
  11. from common import profile
  12. from logging import getLogger
  13. log = getLogger('prefsdialog')
  14. prefstrings = dict((lambda .0: for name, module in .0:
  15. (name, ' '.join((lambda .0: for s in .0:
  16. s.lower().replace('&', ''))(module.itervalues())))
  17.     
  18. )(allprefstrings.iteritems()))
  19. EXPAND_ALL = wx.EXPAND | wx.ALL
  20. wxMac = 'wxMac' in wx.PlatformInfo
  21.  
  22. def show(tabname):
  23.     if not isinstance(tabname, str):
  24.         raise TypeError('prefsdialog.show takes a tab name')
  25.     
  26.     tabindex = [ c[0] for c in tabnames ].index(tabname)
  27.     return show_prefs_window(None, tabindex)
  28.  
  29.  
  30. def show_prefs_window(parent, tab = 0):
  31.     win = PrefsDialog.RaiseExisting()
  32.     if win is not None:
  33.         win.show_tab(tab)
  34.         return win
  35.     
  36.     pdiag = PrefsDialog(None, initial_tab = tab)
  37.     persist_window_pos(pdiag, position_only = True)
  38.     wx.CallAfter(pdiag.Show)
  39.     return pdiag
  40.  
  41.  
  42. class PrefsSearch(wx.SearchCtrl):
  43.     
  44.     def __init__(self, parent):
  45.         wx.SearchCtrl.__init__(self, parent, -1, style = wx.TE_PROCESS_ENTER, validator = LengthLimit(128))
  46.         self.ShowSearchButton(True)
  47.  
  48.  
  49.  
  50. class PrefsTabs(wx.VListBox):
  51.     item_height = 27
  52.     
  53.     def __init__(self, parent):
  54.         wx.VListBox.__init__(self, parent, size = (130, -1))
  55.         self.ItemCount = len(tabnames)
  56.         self.spotlight_indices = set()
  57.         self.Bind(wx.EVT_MOTION, self.OnMotion)
  58.         self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeave)
  59.         self.Bind(wx.EVT_KEY_DOWN, self.OnKey)
  60.         self.UpdateSkin()
  61.         self.tab_indices = dict((lambda .0: for modname, nicename in .0:
  62. (modname, i))(enumerate(tabnames)))
  63.         self.hoveridx = -1
  64.  
  65.     
  66.     def OnKey(self, e):
  67.         e.Skip()
  68.         if e.KeyCode == wx.WXK_ESCAPE:
  69.             self.Top.Close()
  70.         
  71.  
  72.     
  73.     def OnLeave(self, e):
  74.         self.Hover = -1
  75.         e.Skip(True)
  76.  
  77.     
  78.     def OnMotion(self, e):
  79.         self.Hover = self.HitTest(e.Position)
  80.         e.Skip(True)
  81.  
  82.     
  83.     def get_hover(self):
  84.         return self.hoveridx
  85.  
  86.     
  87.     def set_hover(self, i):
  88.         old = self.hoveridx
  89.         self.hoveridx = i
  90.         if i != old:
  91.             if old != -1:
  92.                 self.RefreshLine(old)
  93.             
  94.             if i != -1:
  95.                 self.RefreshLine(i)
  96.             
  97.         
  98.  
  99.     Hover = property(get_hover, set_hover)
  100.     
  101.     def UpdateSkin(self):
  102.         skin = skin
  103.         import gui
  104.         self.bgs = skin.get('AppDefaults.preftabs.backgrounds')
  105.         self.BackgroundColour = wx.Colour(*self.bgs.normal)
  106.  
  107.     
  108.     def tabname(self, index):
  109.         return tabnames[index][1]
  110.  
  111.     
  112.     def spotlight(self, tab_names):
  113.         old = self.spotlight_indices
  114.         self.spotlight_indices = (set,)((lambda .0: for name in .0:
  115. self.tab_indices[name])(tab_names))
  116.         self.RefreshAll()
  117.  
  118.     
  119.     def OnMeasureItem(self, n):
  120.         return self.item_height
  121.  
  122.     
  123.     def OnDrawItem(self, dc, rect, n):
  124.         selected = self.GetSelection() == n
  125.         font = default_font()
  126.         if selected:
  127.             font.SetWeight(wx.FONTWEIGHT_BOLD)
  128.         
  129.         fh = font.Height
  130.         dc.Font = font
  131.         dc.TextForeground = wx.BLACK
  132.         pt = wx.Point(*rect[:2]) + (5, self.item_height / 2 - fh / 2)
  133.         dc.DrawText(self.tabname(n), *pt)
  134.  
  135.     
  136.     def OnDrawBackground(self, dc, rect, n):
  137.         selected = self.Selection == n
  138.         hover = self.Hover == n
  139.         dc.Pen = wx.TRANSPARENT_PEN
  140.         if n in self.spotlight_indices:
  141.             self.bgs.search.Draw(dc, rect, n)
  142.         elif selected:
  143.             self.bgs.selected.Draw(dc, rect, n)
  144.         elif hover:
  145.             self.bgs.hover.Draw(dc, rect, n)
  146.         else:
  147.             self.bgs.normal.Draw(dc, rect, n)
  148.  
  149.  
  150. prefs_dialog_style = wx.DEFAULT_FRAME_STYLE & ~(wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX)
  151.  
  152. class PrefsDialog(wx.Frame):
  153.     border = 5
  154.     default_selected_tab = 1
  155.     default_size = (700, 525)
  156.     
  157.     def __init__(self, parent, initial_tab = default_selected_tab):
  158.         wx.Frame.__init__(self, parent, title = _('Digsby Preferences'), size = self.default_size, style = prefs_dialog_style, name = 'Preferences Window')
  159.         self.loaded_panels = { }
  160.         self.SetMinSize(self.default_size)
  161.         self.create_gui()
  162.         self.bind_events()
  163.         self.layout_gui()
  164.         self.exithooks = Delegate()
  165.         traceguard.__enter__()
  166.         
  167.         try:
  168.             skin = skin
  169.             import gui
  170.             self.SetFrameIcon(skin.get('AppDefaults.TaskbarIcon'))
  171.         finally:
  172.             pass
  173.  
  174.         if not wxMac:
  175.             self.BackgroundColour = wx.WHITE
  176.             self.Bind(wx.EVT_ERASE_BACKGROUND, (lambda e: pass))
  177.         
  178.         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
  179.         self.Bind(wx.EVT_PAINT, self.OnPaint)
  180.         self.tabs.SetSelection(initial_tab)
  181.         self.on_tab_selected(initial_tab)
  182.         self.Bind(wx.EVT_CLOSE, self.on_close)
  183.         self._loaded = 0
  184.         snap_pref(self)
  185.         profile.prefs.add_observer(self.incoming_network_prefs)
  186.         KeyCatcher = KeyCatcher
  187.         import gui.uberwidgets.keycatcher
  188.         k.OnDown('ctrl+w', self.Close)
  189.         k.OnDown('escape', self.Close)
  190.  
  191.     
  192.     def OnPaint(self, e):
  193.         dc = AutoDC(self)
  194.         crect = self.ClientRect
  195.         if not wxMac:
  196.             dc.Brush = wx.WHITE_BRUSH
  197.             dc.Pen = wx.TRANSPARENT_PEN
  198.             dc.DrawRectangleRect(crect)
  199.         
  200.         self.tabs.bgs.normal.Draw(dc, wx.Rect(0, 0, self.tabs.ClientRect.width, crect.height))
  201.  
  202.     
  203.     def incoming_network_prefs(self, src, *a):
  204.         wx.CallAfter(self._gui_incoming_network_prefs, src)
  205.  
  206.     
  207.     def _gui_incoming_network_prefs(self, src):
  208.         if src.isflagged('network'):
  209.             wx.CallAfter(self.reload)
  210.         
  211.  
  212.     _gui_incoming_network_prefs = calllimit(2)(_gui_incoming_network_prefs)
  213.     
  214.     def on_close(self, e):
  215.         self.Hide()
  216.         e.Skip()
  217.         profile = profile
  218.         import common
  219.         profile.prefs.remove_observer(self.incoming_network_prefs)
  220.         self.exithooks()
  221.         for panel in self.loaded_panels.itervalues():
  222.             if hasattr(panel, 'on_close'):
  223.                 traceguard.__enter__()
  224.                 
  225.                 try:
  226.                     panel.on_close()
  227.                 finally:
  228.                     pass
  229.  
  230.                 continue
  231.             traceguard
  232.         
  233.         profile.save('prefs')
  234.         profile.save('notifications')
  235.         memory_event = memory_event
  236.         import gui.native
  237.         memory_event()
  238.  
  239.     
  240.     def create_gui(self):
  241.         self.tabs = PrefsTabs(self)
  242.         self.search = PrefsSearch(self)
  243.         self.search.Bind(wx.EVT_TEXT, self.search_text)
  244.         self.search.Bind(wx.EVT_TEXT_ENTER, self.search_enter)
  245.         if not wxMac:
  246.             self.save_button = wx.Button(self, wx.ID_SAVE, _('&Done'))
  247.             self.save_button.SetDefault()
  248.             self.save_button.Bind((wx.EVT_BUTTON,), (lambda e: self.Close()))
  249.         
  250.  
  251.     
  252.     def search_enter(self, e):
  253.         if len(self.tabs.spotlight_indices) == 1:
  254.             i = self.tabs.spotlight_indices.copy().pop()
  255.             self.tabs.SetSelection(i)
  256.             self.on_tab_selected(i)
  257.         
  258.  
  259.     
  260.     def search_text(self, e):
  261.         wx.CallAfter(self._search)
  262.  
  263.     
  264.     def _search(self):
  265.         val = self.search.Value
  266.         if val == 'Search':
  267.             return None
  268.         
  269.         val = val.lower()
  270.         if val == '':
  271.             return self.tabs.spotlight([])
  272.         
  273.         tab_highlights = set()
  274.         for module_name, stringset in prefstrings.iteritems():
  275.             if val in stringset:
  276.                 tab_highlights.add(module_name)
  277.                 continue
  278.         
  279.         self.tabs.spotlight(tab_highlights)
  280.  
  281.     
  282.     def bind_events(self):
  283.         self.tabs.Bind(wx.EVT_LISTBOX, self.on_tab_selected)
  284.  
  285.     
  286.     def layout_gui(self):
  287.         self.content_sizer = wx.BoxSizer(wx.VERTICAL)
  288.         hz = wx.BoxSizer(wx.HORIZONTAL)
  289.         hz.Add(self.build_tab_sizer(), 0, EXPAND_ALL)
  290.         hz.Add(self.content_sizer, 1, EXPAND_ALL)
  291.         v = wx.BoxSizer(wx.VERTICAL)
  292.         v.Add(hz, 1, EXPAND_ALL)
  293.         if not wxMac:
  294.             v.Add(build_button_sizer(self.save_button, border = self.border), 0, wx.EXPAND | wx.SOUTH | wx.EAST, 4)
  295.         
  296.         self.Sizer = v
  297.  
  298.     
  299.     def build_tab_sizer(self):
  300.         sz = wx.BoxSizer(wx.VERTICAL)
  301.         sz.Add(self.search, 0, EXPAND_ALL, self.border)
  302.         if wxMac:
  303.             sz.AddSpacer(6)
  304.         
  305.         sz.Add(self.tabs, 1, EXPAND_ALL)
  306.         return sz
  307.  
  308.     
  309.     def on_tab_selected(self, e):
  310.         index = None if isinstance(e, int) else e.Int
  311.         if index != -1:
  312.             self.tabs.Update()
  313.             wx.CallAfter(self.show_panel, self.panel_for_tab(index))
  314.         
  315.  
  316.     
  317.     def show_tab(self, n):
  318.         log.info('show_tab %d', n)
  319.         self.tabs.SetSelection(n)
  320.         self.show_panel(self.panel_for_tab(n))
  321.  
  322.     
  323.     def reload(self):
  324.         log.info('reloading Prefs dialog')
  325.         self.Frozen().__enter__()
  326.         
  327.         try:
  328.             child = self.content_sizer.Children[0]
  329.             child.Show(False)
  330.             self.content_sizer.Detach(0)
  331.             for panel in self.loaded_panels.itervalues():
  332.                 
  333.                 try:
  334.                     panel.on_close()
  335.                 except AttributeError:
  336.                     self.Frozen()
  337.                     self.Frozen()
  338.                 except:
  339.                     self.Frozen()
  340.  
  341.                 panel.Destroy()
  342.             
  343.             self.loaded_panels.clear()
  344.             del self.exithooks[:]
  345.             self.show_tab(self.tabs.GetSelection())
  346.         finally:
  347.             pass
  348.  
  349.  
  350.     
  351.     def show_panel(self, panel):
  352.         if not isinstance(panel, wx.WindowClass):
  353.             raise TypeError('show_panel takes a Window, you gave %r' % panel)
  354.         
  355.         self.FrozenQuick().__enter__()
  356.         
  357.         try:
  358.             s = self.content_sizer
  359.             if len(s.GetChildren()) > 0:
  360.                 s.Show(0, False)
  361.                 s.Detach(0)
  362.             
  363.             s.Add(panel, 1, EXPAND_ALL, 10)
  364.             self.Layout()
  365.         finally:
  366.             pass
  367.  
  368.         wx.CallAfter(panel.Show)
  369.  
  370.     
  371.     def panel_for_tab(self, i):
  372.         module_name = tabnames[i][0]
  373.         if module_name not in self.loaded_panels:
  374.             log.info('loading panel "%s"', module_name)
  375.             func = import_function('gui.pref.pg_%s.panel' % module_name)
  376.             panel = self._construct_sub_panel(func)
  377.             self.loaded_panels[module_name] = panel
  378.         
  379.         return self.loaded_panels[module_name]
  380.  
  381.     
  382.     def _construct_sub_panel(self, func):
  383.         PrefsPanel = PrefsPanel
  384.         import gui.pref.prefcontrols
  385.         p = PrefsPanel(self)
  386.         p.Sizer = sz = wx.BoxSizer(wx.VERTICAL)
  387.         szAdd = sz.Add
  388.         PrefPanel = PrefPanel
  389.         PrefCollection = PrefCollection
  390.         import gui.uberwidgets.PrefPanel
  391.         
  392.         def addgroup(titleortuple, *workers, **options):
  393.             if isinstance(titleortuple, tuple):
  394.                 (title, prefix) = titleortuple
  395.             else:
  396.                 title = titleortuple
  397.                 prefix = ''
  398.             group = PrefCollection(*workers, **options)
  399.             panel = PrefPanel(p, group, title, prefix = prefix)
  400.             szAdd(panel, 0, EXPAND_ALL, 3)
  401.             return panel
  402.  
  403.         return func(p, sz, addgroup, self.exithooks)
  404.  
  405.  
  406.