import wx
from WidgetStack import WidgetStack

_ = wx.GetTranslation

class PreferencesDialog(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, -1, _('Preferences'))
        sizer = wx.BoxSizer(wx.VERTICAL)

        # Add Toolbar
        style = wx.TB_HORIZONTAL | wx.NO_BORDER | wx.TB_FLAT | wx.CLIP_CHILDREN | wx.TB_TEXT | wx.TAB_TRAVERSAL 
        self._toolbar = wx.ToolBar(self, style = style)
        sizer.Add(self._toolbar, 0, wx.EXPAND)

        # Add Book (WidgetStack)
        self._book = WidgetStack(self)        
        sizer.Add(self._book, 1, wx.EXPAND|wx.ALL, 1)
                
        # Add the pages
        self.Initialise()

        # Add Buttons
        sizer.Add(wx.StaticLine(self), 0, wx.GROW, 0)
        buttonsBox = wx.BoxSizer(wx.HORIZONTAL)
        okButton = wx.Button(self, wx.ID_OK, _("OK"))
        self.Bind(wx.EVT_BUTTON, self.onOK, id = wx.ID_OK)
        buttonsBox.Add(okButton, 0)
        cancelButton = wx.Button(self, wx.ID_CANCEL, _("Cancel"))
        buttonsBox.Add(cancelButton, 0)
        applyButton = wx.Button(self, -1, _("Apply"))
        self.Bind(wx.EVT_BUTTON, self.onApply, applyButton)
        buttonsBox.Add(applyButton, 0)
        sizer.Add(buttonsBox, 0, wx.CENTER, 0)
        
        self.SetSizerAndFit(sizer)



    def Initialise(self):
        """
        Initialzes the preferences pages.
        """
        def addPage(page, title, helpStr, filename = 'prefGeneral.png', selected = False):
            page = page(self._book, helpStr)
            self._book.AddWidget(page)
            item = self._toolbar.AddRadioLabelTool(-1, title, wx.Bitmap(filename, wx.BITMAP_TYPE_PNG))
            self.map[item.GetId()] = page
            if selected:
                self.visited[page] = 1
            
        self.map = {}
        self.visited = {}

        addPage(DummyPage, _("Page1"),  _("This is Page1..."), selected = True)
        addPage(DummyPage, _("Page2"),  _("and Page2"), selected = False)
        addPage(DummyPage, _("Page3"),  _("And finally... last but not least...Panel3"), selected = False)
        
        self._toolbar.SetToolBitmapSize(wx.Size(32, 32))
        self._toolbar.Realize()
        
        self.Bind(wx.EVT_TOOL, self.onPageChanged)

    def onPageChanged(self, event):
        page = self.map[event.GetId()]
        page.SetFocus()
        self.visited[page] = 1
        self._book.RaiseWidget(page)
        self.Fit()
        event.Skip()
        
    def onOK(self, event):
        self.onApply()
        event.Skip()
        
    def onApply(self, event = None):
        for page in self.visited:
            page.apply()
        if event:
            event.Skip()

class DummyPage(wx.Panel):
    def __init__(self, parent, helpStr):
        wx.Panel.__init__(self, parent, -1)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(wx.StaticText(self, -1, helpStr * 4))
        sizer.Add(wx.StaticText(self, -1, helpStr * 4))
        sizer.Add(wx.StaticText(self, -1, helpStr * 4))

        self.SetSizerAndFit(sizer)
        
    def apply(self):
        pass
    
if __name__ == "__main__":
    app = wx.App(0)
    
    dlg = PreferencesDialog(None)
    dlg.ShowModal()
    dlg.Destroy()
