# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.3 on Tue Feb 17 17:45:14 2009

import wx, intl, __builtin__

# begin wxGlade: dependencies
# end wxGlade

# begin wxGlade: extracode

# end wxGlade

class ElementPropertiesDialog(wx.Dialog):
    def __init__(self, *args, **kwds):
        # Set app language
        intl.setLanguage()
        # begin wxGlade: ElementPropertiesDialog.__init__
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)
        self.sizer_5_staticbox = wx.StaticBox(self, -1, _(u"Formatação"))
        self.label_1 = wx.StaticText(self, -1, _("Tipo:"))
        self.comboType = wx.ComboBox(self, -1, choices=[""], style=wx.CB_DROPDOWN|wx.CB_DROPDOWN|wx.CB_READONLY|wx.CB_SORT)
        self.label_2 = wx.StaticText(self, -1, _(u"Língua:"))
        self.textLang = wx.TextCtrl(self, -1, "", style=wx.TE_PROCESS_TAB)
        self.checkbox_1 = wx.CheckBox(self, -1, _("Negrito"))
        self.checkbox_2 = wx.CheckBox(self, -1, _(u"Itálico"))
        self.checkbox_3 = wx.CheckBox(self, -1, _("Sublinhado"))
        self.button_ok = wx.Button(self, -1, _("Ok"))
        self.button_cancel = wx.Button(self, -1, _("Cancelar"))

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.OnOkButtonClick, self.button_ok)
        self.Bind(wx.EVT_BUTTON, self.OnCancelButtonClick, self.button_cancel)
        # end wxGlade

        self.undo_info = None
        self.el = None
        self.el_types = {'sc':[], 'sce':[], 'p':[], 's':[]}
        for p in __builtin__.cfg.get(u'Preferences', u'ElementTypes').split(','):
            while p.count('|') <= 2:
                p += '|'
            el, desc, pos, css = p.split('|')
            if el == _(u'Seção'):
                self.el_types['sc'].append(desc)
            elif el == _(u'Elemento de Seção'):
                self.el_types['sce'].append(desc)
            elif el == _(u'Parágrafo'):
                self.el_types['p'].append(desc)
            elif el == _(u'Sentença'):
                self.el_types['s'].append(desc)

        self.comboType.SetFocus()
        self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyDown)

    def OnKeyDown(self, event):
        keycode = event.GetKeyCode()
        if keycode == wx.WXK_ESCAPE:
            self.Close(True)
        event.Skip()

    def __set_properties(self):
        # begin wxGlade: ElementPropertiesDialog.__set_properties
        self.SetTitle(_("Propriedades"))
        self.comboType.SetMinSize((150, 27))
        self.comboType.SetSelection(0)
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: ElementPropertiesDialog.__do_layout
        sizer_12 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_14 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_5 = wx.StaticBoxSizer(self.sizer_5_staticbox, wx.HORIZONTAL)
        sizer_6 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_1_copy = wx.FlexGridSizer(3, 2, 2, 2)
        grid_sizer_1_copy.Add(self.label_1, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 2)
        grid_sizer_1_copy.Add(self.comboType, 0, wx.ALL, 2)
        grid_sizer_1_copy.Add(self.label_2, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 2)
        grid_sizer_1_copy.Add(self.textLang, 0, wx.ALL, 2)
        sizer_14.Add(grid_sizer_1_copy, 3, 0, 0)
        sizer_6.Add(self.checkbox_1, 1, wx.EXPAND, 10)
        sizer_6.Add(self.checkbox_2, 1, wx.EXPAND, 10)
        sizer_6.Add(self.checkbox_3, 1, wx.EXPAND, 10)
        sizer_5.Add(sizer_6, 1, wx.EXPAND, 0)
        sizer_14.Add(sizer_5, 2, wx.LEFT|wx.EXPAND, 10)
        sizer_12.Add(sizer_14, 1, wx.ALL|wx.EXPAND, 5)
        sizer_2.Add(self.button_ok, 1, wx.LEFT|wx.RIGHT|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 30)
        sizer_2.Add(self.button_cancel, 1, wx.LEFT|wx.RIGHT|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 30)
        sizer_12.Add(sizer_2, 0, wx.ALL|wx.EXPAND, 10)
        self.SetSizer(sizer_12)
        sizer_12.Fit(self)
        self.Layout()
        # end wxGlade

    def OnOkButtonClick(self, event): # wxGlade: ElementPropertiesDialog.<event_handler>
        if self.el is not None:
            if self.undo_info[0] is not None:
                self.undo_info[0].insert(0, ['CELL-PROP', self.el, self.el.node.__deepcopy__(False), 
                                             self.el.parent.elements_list.index(self.el),
                                             self.el.node.getparent().index(self.el.node), self.undo_info[1], 
                                             self.undo_info[2], _(u'Desfazer alteração de propriedades.')])
            
            # Type
            self.el.setType(self.comboType.GetStringSelection())
            # Language
            self.el.setLanguage(self.textLang.GetValue())
            # Formats
            formats = []
            if self.checkbox_1.GetValue(): formats.append('b')
            if self.checkbox_2.GetValue(): formats.append('i')
            if self.checkbox_3.GetValue(): formats.append('u')
            self.el.node.set('f',','.join(formats))
            self.el.setFormat(self.el.node.get('f'))

            # Close window
            self.EndModal(wx.ID_OK)
        else:
            self.OnCancelButtonClick(None)

    def OnCancelButtonClick(self, event): # wxGlade: ElementPropertiesDialog.<event_handler>
        self.EndModal(wx.ID_CANCEL)
        
    def setElement(self, el, undo_stack=None, undo_text=None, undo_pg=1):
        if el is None:
            wx.MessageBox(_(u'Elemento é nulo.'),_(u'E-Dictor'))
            return False

        self.undo_info = [undo_stack, undo_text, undo_pg]
            
        self.el = el
        el_ref  = el.node.get('id')
        # Initialize combo (types)
        type = el_ref[0:el_ref.rfind('_')]

        if type not in self.el_types.keys():
            wx.MessageBox(_(u'Elemento não suportado.'),_(u'E-Dictor'))
            return False

        # Dialog label
        self.SetTitle(self.GetTitle() + ' [' + el_ref + ']')
        
        # Type
        for t in self.el_types[type]:
            self.comboType.Append(t)
        if len(self.el_types[type]) == 0:
            self.comboType.Enable(False)
        if len(self.el_types[type]) > 0 and el.getType() != '':
            self.comboType.SetStringSelection(el.getType())
        # Language
        if el.getLanguage() != '':
            self.textLang.ChangeValue(el.getLanguage())
        # Formats
        if el.node.get('f') is not None:
            self.checkbox_1.SetValue(el.node.get('f').find('b') >= 0)
            self.checkbox_2.SetValue(el.node.get('f').find('i') >= 0)
            self.checkbox_3.SetValue(el.node.get('f').find('u') >= 0)

        return True
    
# end of class ElementPropertiesDialog


