#!/usr/bin/env python
#-*- coding: UTF-8 -*-
 
import wx
from stomamisc import *


class SysTrayIcon(wx.TaskBarIcon):
    """ Minimize program to system tray and restore - some useful options """
    TBMENU_RESTORE = wx.NewId()
    TBMENU_CLOSE = wx.NewId()
    TBMENU_PREFS = wx.NewId()
    TBMENU_HELP = wx.NewId()

    def __init__(self, frame, icon, title):
        wx.TaskBarIcon.__init__(self)
        self.frame = frame
        self.icon = icon
        self.title = title

        self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.onRestore)
        self.Bind(wx.EVT_MENU, self.onRestore, id = self.TBMENU_RESTORE)
        self.Bind(wx.EVT_MENU, self.onTaskBarClose, id = self.TBMENU_CLOSE)
        self.Bind(wx.EVT_MENU, self.frame.onPreferences, id = self.TBMENU_PREFS)
        self.Bind(wx.EVT_MENU, self.frame.onHandbook, id = self.TBMENU_HELP)
    #-------------------------------------------------------------------------------------------

    def onRestore(self, event):
        self.frame.Iconize(False)
        self.frame.Show(True)
        self.RemoveIcon()
        self.frame.Raise()
    #-------------------------------------------------------------------------------------------

    def onTaskBarClose(self, event):
        self.RemoveIcon()
        self.frame.onCloseWindow(event)
    #-------------------------------------------------------------------------------------------

    def CreatePopupMenu(self):
        """
        This method is called by the base class when it needs to popup
        the menu for the default EVT_RIGHT_DOWN event.  Just create
        the menu how you want it and return it from this function,
        the base class takes care of the rest.
        """
        menu = wx.Menu()
        item = wx.MenuItem(menu, self.TBMENU_RESTORE, 'Restore StoreMan')
        item.SetBitmap(loadIcon('restore.png'))        
        menu.AppendItem(item)
        menu.AppendSeparator()
        item = wx.MenuItem(menu, self.TBMENU_PREFS, 'Preferences')
        item.SetBitmap(loadIcon('prefs.png'))
        menu.AppendItem(item)
        item = wx.MenuItem(menu, self.TBMENU_HELP, 'Help')
        item.SetBitmap(loadIcon('help.png'))
        menu.AppendItem(item)
        menu.AppendSeparator()
        item = wx.MenuItem(menu, self.TBMENU_CLOSE, 'Exit StoreMan')
        item.SetBitmap(loadIcon('exit.png'))
        menu.AppendItem(item)
        return menu
    #-------------------------------------------------------------------------------------------

    def minimizeToTaskbar(self):
        self.SetIcon(self.icon, self.title)
        self.frame.Iconize(True)
        self.frame.Show(False)
# - class SysTrayIcon(wx.TaskBarIcon):----------------------------------------------------------
