# -*- coding: utf-8 -*-

import gtk
import sys
import adesk
import gobject

try:
    INSTALL_PREFIX = open("/etc/gnomenu/prefix").read()[:-1] 
except:
    INSTALL_PREFIX = '/usr'
    
sys.path.append(INSTALL_PREFIX + '/lib/gnomenu')

import gettext
gettext.textdomain('gnomenu')
gettext.install('gnomenu', INSTALL_PREFIX +  '/share/locale')
gettext.bindtextdomain('gnomenu', INSTALL_PREFIX +  '/share/locale')

import backend
import utils

class Plugin(adesk.Plugin):
    def __init__(self, bar, settings):
        adesk.Plugin.__init__(self, bar, settings)
        self.bar = bar
        self.can_zoom = True
        self.gnomenu = GnoMenu(self)
        
        gobject.timeout_add(2000, self.gnomenu.run)

    def onClick(self, widget, event):
        self.gnomenu.button_press(widget, event)

    def resize(self):
        self.set_size_request(self.cfg['icon_size'], self.cfg['icon_size'])

class GnoMenu ():
    icons = {}
    def __init__(self, plugin):
        self.plugin = plugin
        self.bar = plugin.bar
        
    def run(self):
        #~ self.applet.set_tooltip_text("GnoMenu")

        #~ if self.applet.get_pos_type() == gtk.POS_TOP:
            #~ backend.save_setting('orientation', 'top')
        #~ else:
            #~ backend.save_setting('orientation', 'bottom')
        backend.save_setting('orientation', 'bottom')
            
        #Get the default icon theme
        import Globals as Globals
        self.Globals = Globals
        self.theme = gtk.icon_theme_get_default()
        self.icons['stock_folder'] = gtk.gdk.pixbuf_new_from_file(Globals.StartButton[0])


        if Globals.Settings['Distributor_Logo']:
            import IconFactory as iconfactory
            self.iconfactory = iconfactory
            self.applet_button = self.iconfactory.GetSystemIcon('distributor-logo')
        else: self.applet_button = Globals.Applogo

        #~ self.applet.icon.file(self.applet_button, size=awnlib.Icon.APPLET_SIZE)
        
        #Connect to signals
        #~ self.applet.connect('button-press-event', self.button_press)
        #~ self.applet.connect_size_changed(self.size_changed)
        #self.theme.connect('changed', self.icon_theme_changed)
        
        from Menu_Main import Main_Menu
        self.hwg = Main_Menu(self.HideMenu)
        #print self.hwg.window.window.is_visible()
        return False

    def button_press(self, widget, event):
        print "GnoMenu press ...."
        if event.button in (1, 2):
            if not self.hwg.window.window:
                self.ShowMenu()
            else:
                if not self.hwg.window.window.is_visible():
                    self.ShowMenu()
                else: self.HideMenu()

        elif event.button == 3:
            self.HideMenu()


    def ShowMenu(self):
        # Display the start menu!!!
        #~ origin = self.applet.get_window().get_origin()
        
        offset_popup = 2
        plugin_x, plugin_y, plugin_w, plugin_h = self.plugin.get_allocation()
        window_x, window_y = self.bar.win.get_position()
        window_w, window_h = self.bar.win.get_size()
        (w, h) = self.Globals.MenuWidth, self.Globals.MenuHeight
        
        x, y = 0, 0

        if self.bar.cfg['position'] == "top" or self.bar.cfg['position'] == "bottom":
            x = window_x + plugin_x - int( (w - self.bar.cfg['icon_size'])/2.0 )

            # check if window isn't out of screen
            if x < 5:
                x=5

            if self.bar.cfg['position'] == "top":
                y = window_y + window_h + offset_popup
            else:
                y = window_y - h - offset_popup

            while x >= 5 and x + w + 5 > gtk.gdk.screen_width():
                x = x - 1

        else:
            y = window_y + plugin_y

            if self.bar.cfg['position'] == "left":
                x = window_x + window_w + offset_popup
            else:
                x = window_x - w - offset_popup

            while y >= 0 and y + h > gtk.gdk.screen_height():
                y = y - 1
        
        
        self.hwg.Adjust_Window_Dimensions(x,y)
        self.hwg.show_window()

    def HideMenu(self):
        if self.hwg:
            if self.hwg.window.window:
                if self.hwg.window.window.is_visible()== True:
                    self.hwg.hide_window()
