2020년 2월 25일 화요일

[C#] ezIronPython


1. ezIronPython


import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

#from System.Windows.Forms import Shortcut, MainMenu, MenuItem
from System.Windows.Forms import Application, Form, Padding, DockStyle, FlatStyle
from System.Windows.Forms import MenuStrip, StatusBar, AnchorStyles
from System.Windows.Forms import ToolStripContainer, TextImageRelation
from System.Windows.Forms import ToolStripMenuItem, ToolStripSeparator
from System.Windows.Forms import ToolStrip, ToolStripButton
from System.Windows.Forms import ToolStripLabel, ToolStripTextBox, ToolStripComboBox
from System.Windows.Forms import StatusStrip, ToolStripStatusLabel
from System.Windows.Forms import BorderStyle, ToolStripItemDisplayStyle
from System.Windows.Forms import ListView, View, SortOrder, SplitContainer
from System.Windows.Forms import Panel, ToolTip, Label, Button, TextBox, ScrollBars
from System.Windows.Forms import Orientation, TabControl, TabPage
from System.Windows.Forms import FlowLayoutPanel, FlowDirection
from System.Drawing import Size, Image, Point

_window__ctrl_table = {}

#
# Controls
#

class EzControl():
    def init(self):
        self.dockFill()
        #self.anchorAll()
    def SetSize(self,size): self.ctrl.Size = size
    def SetWidth(self,w): self.ctrl.Width = w;
    def SetHeight(self,h): self.ctrl.Height = h;
    def SetLocation(self,point): self.ctrl.Location = point
    def dockFill(self): self.ctrl.Dock = DockStyle.Fill
    def dockTop(self): self.ctrl.Dock = DockStyle.Top
    def dockBottom(self): self.ctrl.Dock = DockStyle.Bottom
    def dockLeft(self): self.ctrl.Dock = DockStyle.Left  
    def dockRight(self): self.ctrl.Dock = DockStyle.Right  
    def anchorAll(self): self.ctrl.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right     
    def Add(self,ctrl): self.ctrl.Controls.Add(ctrl)
    def SetBackColor(self,color): self.ctrl.BackColor = color
    def SetBackImage(self,filename): self.ctrl.BackgroundImage = Bitmap(filename)
    def SetBounds(self,bounds): self.ctrl.Bounds = bounds
    def Invalidate(self): self.ctrl.Invalidate()
    def SetTabIndex(self,i): self.ctrl.TabIndex = i
    def SetFontSize(self,size): self.ctrl.Font = Font(self.ctrl.Font.Name,size,self.ctrl.Font.Style,self.ctrl.Font.Unit)  
    def GetFontSize(self,size): return ctrl.Font.Size
    def SetToolTip(self,tip):
        toolTip = ToolTip()
        toolTip.AutoPopDelay = 5000
        toolTip.InitialDelay = 1000
        toolTip.ReshowDelay = 500
        toolTip.ShowAlways = True
        toolTip.IsBalloon = True
        toolTip.SetToolTip(self.ctrl, tip)
     
def GetControl(name):
    if _window__ctrl_table.get(name): return _window__ctrl_table[name]
    else: return None

class EzLabel(EzControl):
    def __init__(self,h):   
        self.ctrl = Label()
        if h.get('label'): self.ctrl.Text = h['label']
        if h.get('icon'):  self.ctrl.Image = Image.FromFile(h['icon'])
    def SetHandler(self,handler): self.ctrl.Click += handler
        
        
class EzButton(EzControl):
    def __init__(self,h):   
        self.ctrl = Button()
        if h.get('label'): self.ctrl.Text = h['label']
        if h.get('icon'):  self.ctrl.Image = Image.FromFile(h['icon'])
    def SetHandler(self,handler): self.ctrl.Click += handler

class EzTextBox(EzControl):
    def __init__(self,h,multiline):   
        self.ctrl = TextBox()
        self.ctrl.Multiline = multiline
        self.ctrl.ScrollBars = ScrollBars.Both if multiline else ScrollBars.None
        self.ctrl.WordWrap = False
        self.ctrl.AcceptsReturn = True
        self.ctrl.AcceptsTab = True 
        self.ctrl.Show()
    def ClearText(self): self.ctrl.Text = ""
    def AddText(self,text): self.ctrl.Text += text
    def SetMultiLine(v): self.ctrl.Multiline = v
    def SetWordWrap(v): self.ctrl.WordWrap = v
    def SetDrop(v): self.ctrl.AllowDrop = v
           
class EzListView(EzControl):
    def __init__(self,h):
        self.ctrl = ListView()
        self.ctrl.View = View.Details
        self.ctrl.GridLines = True
        self.ctrl.FullRowSelect = True
        self.ctrl.CheckBoxes = True
        self.ctrl.LabelEdit = True
        self.ctrl.AllowColumnReorder = True
        self.ctrl.Sorting = SortOrder.Ascending
        self.init()
             
#
# Container
#
class EzHBox(EzControl):
    def __init__(self):
        self.ctrl = FlowLayoutPanel()
        self.ctrl.AutoScroll = False;
        self.ctrl.AutoSize = True;
        self.ctrl.BorderStyle = BorderStyle.FixedSingle;
        self.ctrl.FlowDirection = FlowDirection.LeftToRight;
        #self.ctrl.FlowDirection = FlowDirection.TopDown;
        self.dockFill();     
    def AddItem(self,ctrl):
        self.ctrl.Controls.Add(ctrl);

class EzVBox(EzControl):
    def __init__(self):
        self.ctrl = Panel()
        self.ctrl.AutoScroll = True
        self.ctrl.AllowDrop = True
        self.ctrl.AutoSize = False
        self.dockFill();   
    def AddItem(self,ctrl):
        self.ctrl.Controls.Add(ctrl)
    def SetBorderStyleNone():
        self.ctrl.BorderStyle = BorderStyle.None;       
    def SetBorderStyleLine():
        self.ctrl.BorderStyle = BorderStyle.FixedSingle;        
    def SetBorderStyle3D():
        self.ctrl.BorderStyle = BorderStyle.Fixed3D;        

class EzSplit(EzControl):
    def __init__(self,h,vertical):
        self.ctrl = SplitContainer()
        self.ctrl.Orientation = Orientation.Vertical if vertical else Orientation.Horizontal
        self.ctrl.SplitterDistance = self.ctrl.ClientSize.Width * 0.8
        self.items = [ self.ctrl.Panel1, self.ctrl.Panel2 ]
        self.dockFill();   
    def AddItem(self,ctrl,index): self.items[index].Add(ctrl)

class EzNotebook(EzControl):
    def __init__(self,h,vertical):
        self.ctrl = TabControl()
        self.dockFill();   
    def AddItem(self,ctrl,text): 
        tab = TabPage()
        tab.Text = text;
        tab.Padding = Padding(10);
        tab.Controls.Add(ctrl);
        self.ctrl.Controls.Add(tab);

def EzLayout(content):
    vbox = EzVBox()
    for v in content:
        hbox = EzHBox()
        expand = False
        for h in v:
            name = h.get('name')
            if not name: continue
            if   name == 'Label':     f = EzLabel(h)
            elif name == 'Button':    f = EzButton(h)
            elif name == 'TextField': f = EzTextBox(h,False)
            elif name == 'TextArea':  f = EzTextBox(h,True)
            elif name == 'HSplit':    f = EzSplit(h,False)
            elif name == 'VSplit':    f = EzSplit(h,True)
            elif name == 'NoteBook':  f = EzNoteBook(h)
            else: continue
            
            if h.get('width') : f.SetWidth(h['width'])
            if h.get('height'): f.SetHeight(h['height'])
            if h.get('fontsize'): f.SetFontSize(h['fontsize'])               
            if h.get('tooltip'): f.SetToolTip(h['tooltip'])
            if h.get('handler'): f.SetHandler(h['handler'])
            if h.get('key'): _window__ctrl_table[h['key']] = f
            hbox.AddItem(f.ctrl)
        vbox.AddItem(hbox.ctrl)
    return vbox.ctrl

class EzBorder(EzControl):
    def __init__(self):
        self.ctrl = ToolStripContainer()
        self.ctrl.TopToolStripPanelVisible = False
        self.ctrl.RightToolStripPanelVisible = False
        self.ctrl.BottomToolStripPanelVisible = False
        self.ctrl.LeftToolStripPanelVisible = False
        self.init()
    def AddTop(self,item):
        self.ctrl.TopToolStripPanel.Controls.Add(item)
        self.ctrl.TopToolStripPanelVisible = True
    def AddRight(self,item):
        self.ctrl.RightToolStripPanel.Controls.Add(item)
        self.ctrl.RightToolStripPanelVisible = True
    def AddBottom(self,item):
        self.ctrl.BottomToolStripPanel.Controls.Add(item)
        self.ctrl.BottomToolStripPanelVisible = True
    def AddLeft(self,item):
        self.ctrl.LeftToolStripPanel.Controls.Add(item)
        self.ctrl.LeftToolStripPanelVisible = True
    def AddCenter(self,item):
        self.ctrl.ContentPanel.Controls.Add(item)
        self.ctrl.ContentPanel.Padding = Padding(10)
    
#
# Windows
#

class EzMenu(EzControl):
    def __init__(self,name,menu_table):
        self.ctrl = ToolStripMenuItem(name)
        for m in menu_table:
            if not m.get('name') or m['name'] == '-':
                self.ctrl.DropDownItems.Add(ToolStripSeparator())
                continue
            if not m.get('item'): continue # Disabled
            if type(m['item']) == list:
                self.ctrl.DropDownItems.Add(EzMenu(m['name'],m['item']).ctrl)
            else:
                item = ToolStripMenuItem(m['name'],None,m['item'])
                if m.get('icon'): item.Image = Image.FromFile(m['icon'])
                if m.get('check'): item.Checked = True
                self.ctrl.DropDownItems.Add(item)
    
class EzMenuBar(EzControl):
    def __init__(self,parent,menubar_table):
        self.ctrl = MenuStrip()
        self.ctrl.Parent = parent         
        for m in menubar_table:
            self.ctrl.Items.Add(EzMenu(m['name'],m['item']).ctrl) 
        self.init()
        
class EzToolBar(EzControl):
    def __init__(self,parent,toolbar_table):
        self.ctrl = ToolStrip()
        for m in toolbar_table:
            if not m.get('name') or m['name'] == '-':
                item = ToolStripSeparator()
                if m.get('key'): _window__ctrl_table[m['key']] = item
            elif m['name'] == "Label" and m.get('label'):
                item = ToolStripLabel(m['label'])
                if m.get('key'): _window__ctrl_table[m['key']] = item
            elif m['name'] == "Button":
                item = ToolStripButton()
                if m.get('handler'): item.Click += m['handler']
                if m.get('key'): _window__ctrl_table[m['key']] = item
                if m.get('label'): item.Text = m['label']
                if m.get('icon'):  item.Image = Image.FromFile(m['icon'])
                item.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
                item.TextImageRelation = TextImageRelation.ImageAboveText;
            elif m['name'] == 'TextBox':
                item = ToolStripTextBox()
                if m.get('handler'): item.KeyDown += m['handler']
                if m.get('key'): _window__ctrl_table[m['key']] = item
                if m.get('text'): item.Text += m['text']
                item.BorderStyle = BorderStyle.FixedSingle
            elif m['name'] == 'ComboBox':
                item = ToolStripComboBox()
                if m.get('handler'): item.SelectedIndexChanged += m['handler']
                if m.get('key'): _window__ctrl_table[m['key']] = item
                if m.get('items'):
                    for i in m['items']: item.Items.Add(i)
                    item.SelectedIndex = 0; 
                item.FlatStyle = FlatStyle.System
            else:
                continue      
            self.ctrl.Items.Add(item)
        self.init()
        
class EzStatusBar(EzControl):
    def __init__(self,parent):
        self.ctrl = StatusStrip()
        self.label = ToolStripStatusLabel()
        self.ctrl.Items.Add( self.label );
        self.init()
          
class Window(Form):
    def CreateWindow(self,title,width,height):
        self.ctrl = _window__ctrl_table
        self.Text = title
        self.Size = Size(width,height)
        border = EzBorder()
        self.Controls.Add(border.ctrl)
        border.AddCenter(EzLayout(self.content))
        for m in self.tool: border.AddTop(EzToolBar(self,m).ctrl)
        border.AddTop(EzMenuBar(self,self.menu).ctrl)
        self.sb = EzStatusBar(self)
        border.AddBottom(self.sb.ctrl)

        self.CenterToScreen()        
    def SetStatusText(self,text):
        self.sb.label.Text = text
    def GetControl(self,name):
        return GetControl(name)

#
# Application
#
      
class MonoApp(Window):
    def __init__(self):
        self.menu = [
            { 'name' : "File",
              'item' : [
                    { 'name' : "Exit" , 'item' : self.onExit, 'icon' : 'icon/exit.png' },
                    { 'name' : "-" ,  },
                    { 'name' : "Exit" , 'item' : self.onExit, 'icon' : 'icon/exit.png' } ]
            }, { 'name' : "Help",
              'item' : [
                    { 'name' : "About", 'item' : self.onAbout, 'check' : True, 'icon' : 'icon/new.png' } ]
            }]
        self.tool = [[
                { "name" : "Label",   "label" : "File:",  },
                { "name" : "TextBox", "handler" : self.onText, 'key' : 'text'   },
                { 'name' : "-" ,  },
                { "name" : "Button",  "label" : "Exit", 'icon' : 'icon/exit.png', "handler" : self.onExit, "tooltip" : "Quit"  },
            ],[
                { "name" : "ComboBox", 'items' : [ 'orange', 'apple' ], 'key' : 'combo', 'handler' : self.onCombo  },
            ]]
        tab1 = [[ { "name" : "TextArea", "expand" : True },
                  { "expand" : True }, ]]
        tab2 = [[ { "name" : "TextArea", "expand" : True },
                  { "expand" : True }, ]]   
        split1 = [[ { "name" : "TextArea", "expand" : True },
                    { "expand" : True }, ]]
        split2 = [[ { "name" : "TextArea", "expand" : True },
                    { "expand" : True }, ]]              
        self.content = [ # vbox
            [ # hbox
                { "name" : "Label", "label" : "Address:" },
                { "name" : "TextField", "key" : "text", "expand" : True },
                { "name" : "Button",  "label" : "Browse", "tooltip" : "Open File", "handler" : self.onBrowse  },
                { "name" : "Button",  "label" : "About", "handler" : self.onAbout  },
            ],
            [ # hbox
                { "name" : "TextArea", "expand" : True },
                { "expand" : True },
            ],
            [ # hbox
                { "name" : "TabPane", "labels" : [ "Tab1", "Tab2" ], "items" : [ tab2, tab2 ], "expand" : True },
                { "expand" : True },
            ],  
            [ # hbox
                { "name" : "HSplit", "items" : [ split1, split2 ] , "first" : 0.5, "expand" : True},
                { "expand" : True },
            ],                
        ]
        '''            
        self.content = [
            [
                {
                    "name" : "ListView", "key" : "listview", "handler" : self.onListView
                }
            ]]
        '''
        self.CreateWindow("ezIronPython Demo", 640, 400)
        self.SetStatusText("Ready")
    def onExit(self, sender, event):
        self.Close()
    def onAbout(self, sender, event):
        self.Close()
    def onBrowse(self, sender, event):
        self.Close()
    def onText(self, sender, event):
        text = self.GetControl('text')
        print(text.Text)
    def onCombo(self, sender, event):
        combo = self.GetControl('combo')
        text  = self.GetControl('text')
        text.Text = combo.Text
    def onListView(self, sender, event):
        print(1)
              
if __name__ == '__main__':
    Application.Run(MonoApp())

댓글 없음:

댓글 쓰기