##    Fonty Python Copyright (C) 2006, 2007, 2008 Donn.C.Ingle
##    Contact: donn.ingle@gmail.com - I hope this email lasts.
##
##    This file is part of Fonty Python.
##    Fonty Python is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    Fonty Python is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with Fonty Python.  If not, see <http://www.gnu.org/licenses/>.

## Message topics : they identify the functions to run from CPubsub
update_font_view = 10
show_error = 20
show_error_and_abort = 30
show_message = 40
reset_to_page_one = 50
add_item_to_notebook = 60
remove_item_from_notebook = 70
print_to_status_bar = 80
install_pog = 90
uninstall_pog = 100
main_button_click = 110
toggle_main_button = 120
pog_selected = 130
change_pog_icon = 140
toggle_buttons = 150
select_no_target_pog = 160
select_no_view_pog = 170

get_font_view_width = 180
menu_settings = 190


## A hack to allow values to return from published topics
globRetVal = {}

## The thing that is held in the dictionary inside CListener
class CTopic:
    def __init__(self, function, topic, key):
        self.function = function
        self.topic = topic
        self.key = key
        
## When you have a function you want to make available, sub() it.
## When you want something to happen somewhere else, then pub () it.
class CPubsub:
    def __init__(self):
        self.__ears = {}
        self.__key = 0
    def __del__(self):
        del self.__ears
        
    ## Makes a topic object and stores it internally.
    ## Keeps a constantly increasing internal key.
    ## I used a dictionary, but it prob should just be a list.
    ## Ah well.
    def sub(self, topic, function): #SUBSCRIBE (was newEar)
        t = CTopic(function, topic, self.__key)
        self.__ears [ self.__key ] = t
        self.__key += 1
        
    ## Go thru all the topics, find any that match and call their functions, passing any args too.
    ## This means that one topic can have many handlers. e.g. "pog_selected" goes to two handlers.
    def pub(self, topic, *args): #PUBLISH (was shout)
        global globRetVal
        #m = CMessage(topic, messagelist)
        for key, top in self.__ears.iteritems():
            if top.topic == topic:
                function = top.function
                ## Edited this Dec 2007 to allow returns.
                ## I had added: return result,
                ## but that aborted this loop and screwed the pooch.
                ## I then tried yield result, but that turns this all
                ## into a generator and confuses me.
                ## So, if there is a return value it goes into a global dict.
                ## and you fetch it via the topic key.
                ## Live with it -- or fix it and let me know!
                globRetVal[topic] = function(args) #Pass the args only.

## Sample of the use of this stuff:
if __name__ == "__main__":

    def dox(*args):
        print "i run", args
    def detox(*args):
        print "yup de too", args
        
    top_dox = 1
    
    p = CPubsub()
    #we subscribe two handlers to one topic
    p.sub(top_dox, dox)
    p.sub(top_dox, detox)
    
    #we  pretend we are in another class/widget and we want to send a message:
    p.pub(top_dox, 10,20,"AX","BX")
