import chart
import clr

from System import Byte

clr.AddReference("Ans.UI.Toolkit")
clr.AddReference("Ans.UI.Toolkit.Base")
clr.AddReference("Ans.Utilities")

import Ansys.UI.Toolkit
import Ansys.UI.Toolkit.Drawing
import Ansys.Utilities
       
# -----------------------------------
#   MechanicalPanelHelper
# -----------------------------------

class MechanicalPanelHelper():

    def __init__(self, api, control, location):
        self.api = api
        self.control = control
        self.location = location
        
        self.panel = self.api.Graphics.CreateWindowOnPanel(self.location)
        if self.panel != None:
            self.panel.SuspendLayout()
            self.panel.BackColor = Ansys.Utilities.Color(Byte(160), Byte(160), Byte(160))
            self.panel.Columns.Add(Ansys.UI.Toolkit.TableLayoutSizeType.Percent, 100.0)
            self.panel.Rows.Add(Ansys.UI.Toolkit.TableLayoutSizeType.Percent, 100.0)
            self.panel.Controls.Add(self.control, 0, 0)
            self.panel.ResumeLayout()

        self.hide()

    def show(self):
        if self.panel != None:
            self.api.Graphics.ShowPanel(self.location)
            self.panel.Show()

    def hide(self):
        if self.panel != None:
            self.panel.Hide()

    def dispose(self):
        self.component = None
        if self.panel != None:
            self.panel.Hide()
            self.panel.Dispose()
            self.panel = None

# -----------------------------------
#   ContextMenuHelper
# -----------------------------------

class ContextMenuHelper(Ansys.UI.Toolkit.ContextMenu):

    def __init__(self, control, menunames, actions):
        Ansys.UI.Toolkit.ContextMenu.__init__(self)

        for name, action in zip(menunames, actions):
            if name == '-': 
                self.AddMenuSeparator()
            else:
                menu = self.AddMenuItem(name)
                menu.Clicked += lambda sender, args: action()

        self.control = control
        self.control.MouseUp += self._onMouseUp

    def __del__(self):
        if self.control != None:
            self.control.MouseUp -= self._onMouseUp

    # Mouse Event
    def _onMouseUp(self, sender, args):
        if args.Button == Ansys.UI.Toolkit.MouseButton.Right:
            self.Show(args.GlobalX, args.GlobalY)

    
