# !NewsStats.py.Iconbar/py

import wimp
import MainW
import Newsbase


def init():
    "Setup iconbar icon"
    global iconbar, window_info, menu_iconbar
    if Newsbase.active:
        iconbar = wimp.iconbar_icon('!newsstats', click, 1)
    else:
        iconbar = wimp.iconbar_icon('!newsstats2', click, 1)
    window_info = wimp.info_window()
    menu_iconbar = wimp.menu('iconbar', ('task', ('info', window_info), 'quit'))


def click(buttons, x):
    "Handle click on iconbar"
    if buttons == 2:
        reopen = 1
        while reopen:
            selection, reopen = menu_iconbar.menu(x - 64, 184)
            if selection is None:
                pass
            elif selection[0] == 0:
                window_info.open_as_menu()
                reopen = 0
            elif selection[0] == 1:
                wimp.quit()
    elif MainW.analysing:
        MainW.window_status.open_centred(-1)
    else:
        if Newsbase.active:
            MainW.w.open_centred(-1)
            MainW.w.put_caret()
        else:
            wimp.warning(wimp.m['warning.newsbase.noreply'])


def update():
    if Newsbase.active:
        iconbar.change('!newsstats')
    else:
        iconbar.change('!newsstats2')
        MainW.w.close()
