2021년 1월 2일 토요일

[IronPython] WinForm Library

 

1. WinForm Library

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
clr.AddReferenceToFileAndPath("ScintillaNET.dll")

import System
from System.Windows.Forms import *
from System.Drawing import * 

import ScintillaNET
from ScintillaNET import * #WrapMode, IndentView, MarginType

# Controls

class IpyCtrl():
	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)

class IpyButton(IpyCtrl):
	def __init__(self, text=None, icon=None, handler=None, x=None, y=None, w=None, h=None ):
		self.ctrl = Button()
		if text is not None: self.ctrl.Text = text;
		if icon is not None: self.ctrl.Image = icon
		if handler is not None: self.ctrl.Click += handler
		self.ctrl.Left = x if x is not None else 0
		self.ctrl.Top = y if y is not None else 0
		self.ctrl.Width = w if w is not None else -1
		self.ctrl.Height = h if h is not None else -1
		#self.ctrl.MinimumSize = Size(80, 22)
		self.ctrl.Dock = System.Windows.Forms.DockStyle.Fill

class IpyComboBox(IpyCtrl):
	def __init__(self, items=None, select=0, handler=None, x=None, y=None, w=None, h=None ):
		self.ctrl = ComboBox()
		if items is not None:
			for i in items: self.ctrl.Items.Add( i )
		if select is not None: self.SelectFirstItem()
		if handler is not None: self.ctrl.SelectedIndexChanged += handler;
		self.ctrl.Dock = System.Windows.Forms.DockStyle.Fill
		self.ctrl.Show()
	def AddItem(text): self.ctrl.Items.Add(text)
	def GetSelectedIndex(self): return self.ctrl.SelectedIndex
	def GetSelectedText(self): return self.ctrl.Items[self.ctrl.SelectedIndex].ToString()
	def SelectFirstItem(self):
		if self.ctrl.Items.Count > 0: self.ctrl.SelectedIndex = 0


class IpyListBox(IpyCtrl):
	def __init__(self, items=None, select=0, handler=None, height=None, fontsize=None):
		self.ctrl = ListBox()
		self.ctrl.ScrollAlwaysVisible = True
		if fontsize is not None: SetFontSize(fontsize);
		self.ctrl.Dock = System.Windows.Forms.DockStyle.Fill
		if items is not None:
			for i in items: self.ctrl.Items.Add( i )
		if handler is not None: self.ctrl.SelectedIndexChanged += handler;
		if height is not None: self.SetHeight(height)
	def AddItem(self, text): self.ctrl.Items.Add(text)
	def getSelectedIndex(self): return self.ctrl.SelectedIndex
	def getSelectedText(self): return self.ctrl.Items[self.ctrl.SelectedIndex].ToString();
	def selectFirstItem(self):
		if listbox.Items.Count > 0: listbox.SelectedIndex = 0
	def SetEventHandler(self, handler):
		if handler is not None: listbox.Click += handler

class IpyImageBox(IpyCtrl):
	def __init__(self,image=None,file=None):
		self.ctrl = PictureBox()
		self.ctrl.BorderStyle = BorderStyle.Fixed3D
		self.ctrl.SizeMode = PictureBoxSizeMode.StretchImage
		if file is not None: self.ctrl.Image = System.Drawing.Image.FromFile(file)
		if image is not None: self.ctrl.Image = image
	def SetImage(self,image):
		self.ctrl.Image = image

class IpyStc(IpyCtrl):
	def __init__(self,parent=None):
		self.ctrl = ScintillaNET.Scintilla()
		self.ctrl.Text = "StyledText"
		self.ctrl.Parent = parent
		self.ctrl.Location = Point(0, 0)
		#self.ctrl.Size = self.Size
		self.ctrl.Dock = System.Windows.Forms.DockStyle.Fill
		self.ctrl.WrapMode = WrapMode.None;
		self.ctrl.IndentationGuides = IndentView.LookBoth

		nums = self.ctrl.Margins[0]
		nums.Width = 36
		nums.Type = MarginType.Number
		nums.Sensitive = True
		nums.Mask = 0


class IpyTextBox(IpyCtrl):
	def __init__(self,parent=None,size=None):
		self.ctrl = TextBox()
		if size is not None: SetFontSize(size)
		self.ctrl.Dock = System.Windows.Forms.DockStyle.Fill
		#self.ctrl.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right
		self.ctrl.Multiline = False
		self.ctrl.ScrollBars = ScrollBars.Both
		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 EnableMultiLine(): self.ctrl.Multiline = True
	def EnableWordWrap(): self.ctrl.WordWrap = True
	def EnableScrollBar(): self.ctrl.ScrollBars = ScrollBars.Both
	def EnableDrop():  self.ctrl.AllowDrop = True

# Container

class IpyBorder(IpyCtrl):
	def __init__(self,parent=None):
		self.ctrl = ToolStripContainer()
		self.dockFill()
		if parent: parent.Controls.Add(self.ctrl)

		self.top = ToolStrip()
		self.right = ToolStrip()
		self.bottom = ToolStrip()
		self.left = ToolStrip()
		self.center = Panel()

		self.ctrl.ContentPanel.Controls.Add(self.center)
		self.ctrl.ContentPanel.Padding = Padding(10)

		self.ctrl.TopToolStripPanelVisible = False
		self.ctrl.RightToolStripPanelVisible = False
		self.ctrl.BottomToolStripPanelVisible = False
		self.ctrl.LeftToolStripPanelVisible = False

		self.ctrl.TopToolStripPanel.Controls.Add(self.top)
		self.ctrl.RightToolStripPanel.Controls.Add(self.right)
		self.ctrl.BottomToolStripPanel.Controls.Add(self.bottom)
		self.ctrl.LeftToolStripPanel.Controls.Add(self.left)

	def add_top(self,item):
		self.ctrl.TopToolStripPanelVisible = True
		self.top.Items.Add(item)
	def add_right(self,item):
		self.ctrl.RightToolStripPanelVisible = True
		self.right.Items.Add(item)
	def add_bottom(self,item):
		self.ctrl.BottomToolStripPanelVisible = True
		self.bottom.Items.Add(item)
	def add_left(self,item):
		self.ctrl.LeftToolStripPanelVisible = True
		self.left.Items.Add(item)
	def add_center(self,item):
		self.center.Controls.Add(item)

class IpySplit(IpyCtrl):
	def __init__(self,parent=None,vertical=True,ratio=0.5):
		self.ctrl = SplitContainer()
		self.ctrl.Orientation = Orientation.Vertical if vertical else Orientation.Horizontal
		self.ctrl.SplitterDistance = self.ctrl.ClientSize.Width * ratio
		self.dockFill();   
		if parent: parent.Controls.Add(self.ctrl)		 
		self.items = [ self.ctrl.Panel1, self.ctrl.Panel2 ]
	def add_item(self,index,ctrl=None): 
		self.items[index].Controls.Add(ctrl)
	def add_item1(self,ctrl): 
		self.ctrl.Panel1.Controls.Add(ctrl)
	def add_item2(self,ctrl): 
		self.ctrl.Panel2.Controls.Add(ctrl)

class IpyTab(IpyCtrl):
	def __init__(self,parent=None):
		self.ctrl = TabControl()
		self.dockFill();   
		if parent: parent.Controls.Add(self.ctrl)
		
	def add_item(self,text,ctrl=None): 
		tab = TabPage()
		tab.Text = text;
		tab.Padding = Padding(10);
		if ctrl: tab.Controls.Add(ctrl);
		self.ctrl.Controls.Add(tab);

class IpyTable(IpyCtrl):
	def __init__(self,row,col,parent=None):
		self.ctrl = TableLayoutPanel()
		self.table = self.ctrl
		self.table.Name = ""
		self.table.RowCount = row
		self.table.ColumnCount = col
		self.table.Location = Point(10, 10)
		self.ctrl.Dock = System.Windows.Forms.DockStyle.Fill

	def SetColumnSize( self, widths ):
		for i in range(self.table.ColumnCount):
			length = len(widths) if i < widths[i] else 0
			if	length == 0: style = ColumnStyle(SizeType.AutoSize)
			elif length > 0: style = ColumnStyle(SizeType.Percent, length)
			elif length < 0: style = ColumnStyle(SizeType.Absolute, -length)
			self.table.ColumnStyles.Add(style)

	def SetRowSize( self, heights ):
		for i in range(self.table.RowCount):
			length = len(heights) if i < heights[i] else 0
			if	length == 0: style = RowStyle(SizeType.AutoSize)
			elif length > 0: style = RowStyle(SizeType.Percent, length)
			elif length < 0: style = RowStyle(SizeType.Absolute, -length)
			self.table.RowStyles.Add(style)
	
	def Add( self, row, col, ctrl ):
		self.table.Controls.Add( ctrl, col, row )
	
	def SetColumnSpan( self, row, col, num ):
		ctrl = self.table.GetControlFromPosition( col, row )
		if ctrl != None: self.table.SetColumnSpan(ctl, num)
	
	def SetRowSpan( row, col, num ):
		ctrl = self.table.GetControlFromPosition( col, row )
		if ctrl != None: sekf,table.SetRowSpan(ctl, num)


class IpyVBox(IpyCtrl):
	def __init__(self,parent=None):
		self.ctrl = TableLayoutPanel()
		self.table = self.ctrl
		self.table.Name = ""
		self.table.RowCount = 0
		self.table.ColumnCount = 1
		self.table.Location = Point(10, 10)
		self.table.Dock = System.Windows.Forms.DockStyle.Fill
	def Add( self, ctrl, height=0):
		self.table.RowCount += 1
		self.table.Controls.Add( ctrl, 0, self.table.RowCount-1 )
		if height == 0: style = RowStyle(SizeType.Percent, 100)
		elif height < 0: style = ColumnStyle(SizeType.AutoSize)
		else: style = RowStyle(SizeType.Absolute, height)
		'''
		if height == 0: style = RowStyle(SizeType.AutoSize)
		elif height > 0: style = RowStyle(SizeType.Percent, height)
		else: style = RowStyle(SizeType.Absolute, -height)
		'''
		self.table.RowStyles.Add(style)

class IpyHBox(IpyCtrl):
	def __init__(self,parent=None):
		self.ctrl = TableLayoutPanel()
		self.table = self.ctrl
		self.table.Name = ""
		self.table.RowCount = 1
		self.table.ColumnCount = 0
		self.table.Location = Point(10, 10)
		self.table.Dock = System.Windows.Forms.DockStyle.Fill
	def Add( self, ctrl, width=0 ):
		self.table.ColumnCount += 1
		self.table.Controls.Add( ctrl, self.table.ColumnCount-1, 0 )
		if width == 0: style = ColumnStyle(SizeType.Percent, 100)
		elif width < 0: style = ColumnStyle(SizeType.AutoSize)
		else: style = ColumnStyle(SizeType.Absolute, width)
		'''
		if width == 0: style = ColumnStyle(SizeType.AutoSize)
		elif width > 0: style = ColumnStyle(SizeType.Percent, width)
		else: style = ColumnStyle(SizeType.Absolute, -width)
		'''
		self.table.ColumnStyles.Add(style)

# Windows Frame

class IpyMenuBar(IpyCtrl):
	def __init__(self,parent=None):
		self.menubar = MenuStrip()
		if parent: parent.Controls.Add(self.menubar)

	def add_menu(self,name,parent=None):
		self.menu = ToolStripMenuItem()
		self.menu.Name = name
		self.menu.Text = name
		if not parent: parent = self.menubar
		parent.Items.Add(self.menu)
		return self.menu
		
	def add_item(self,name,handler=None,icon=None,parent=None):
		item = ToolStripMenuItem()
		item.Name = name
		item.Text = name
		if handler: item.Click += handler
		if icon: item.Image = icon
		if not parent: parent = self.menu
		parent.DropDownItems.Add(item)
		return item
		
class IpyToolBar(IpyCtrl):
	def __init__(self,parent=None,size=16):
		self.toolbar = ToolStrip()
		self.toolbar.ImageScalingSize = Size(size, size);
		if parent: parent.Controls.Add(self.toolbar)
	def add_label(self,text):
		self.toolbar.Items.Add(ToolStripLabel(text))
	def add_separater(self):
		self.toolbar.Items.Add(ToolStripSeparator())
	def add_button(self,text=None,handler=None,icon=None,):
		button = ToolStripButton()
		if text: button.Text = text
		if icon: button.Image = icon
		if handler: button.Click += handler
		button.TextImageRelation = TextImageRelation.ImageAboveText
		self.toolbar.Items.Add(button)


class IpyStatusBar(IpyCtrl):
	def __init__(self,parent=None,size=32):
		self.status = StatusBar()
		self.status.ShowPanels = True
		self.panels = 0
		if parent: parent.Controls.Add(self.status)
	def add_panel(self,text):
		self.status.Panels.Add(text);
		self.status.Panels[self.panels].AutoSize = StatusBarPanelAutoSize.Spring
		self.status.Panels[self.panels].BorderStyle = StatusBarPanelBorderStyle.Raised
		self.panels += 1
	def set_text(self,index,text):
		self.status.Panels[index].Text = text
	def set_icon(self,index,icon):
		self.status.Panels[index].Icon = icon
		
		
if __name__ == "__main__":
	import wf_icon
	class IpyForm(Form):
		def __init__(self):
			import wf_icon
			self.Text = 'Text to Pcap'
			self.Size = Size(480,320)
			self.CenterToScreen()
			self.FormClosing += FormClosingEventHandler(self.on_close)

			layout = IpyVBox()
			tool = IpyHBox()
			tool.Add(IpyListBox(("apple","grape","pear","melon"),handler=self.on_combo,height=48).ctrl,-1)
			tool.Add(IpyComboBox(("apple","grape"),handler=self.on_combo).ctrl,-1)
			tool.Add(IpyTextBox().ctrl)
			tool.Add(IpyButton(icon=wf_icon.GetExitImage(),handler=self.on_exit).ctrl,48)
			tool.Add(IpyImageBox(file='D:/Lenna.png').ctrl)
			layout.Add(tool.ctrl,48)
			layout.Add(IpyStc().ctrl)

			self.Controls.Add( layout.ctrl )
			
		def on_close(self,sender,event):
			rv = MessageBox.Show("Do you want to close ?", "Warning", MessageBoxButtons.YesNo )
			if rv == System.Windows.Forms.DialogResult.Yes: event.Cancel = False
			else: event.Cancel = True

		def on_exit(self,sender,event):
			self.Close()
		def on_combo(self,sender,event):
			print(sender.Items[sender.SelectedIndex].ToString())
			
	Application.Run(IpyForm())