[Zope-CVS] CVS: PythonNet/demo - splitter.py:1.2 wordpad.py:1.3

Brian Lloyd cvs-admin at zope.org
Mon Oct 27 21:07:30 EST 2003


Update of /cvs-repository/PythonNet/demo
In directory cvs.zope.org:/tmp/cvs-serv9370/demo

Modified Files:
	splitter.py wordpad.py 
Log Message:
Checkin for b1, before the cable bugs out again :(



=== PythonNet/demo/splitter.py 1.1 => 1.2 ===
--- PythonNet/demo/splitter.py:1.1	Tue Sep 30 20:43:11 2003
+++ PythonNet/demo/splitter.py	Mon Oct 27 21:06:59 2003
@@ -1,4 +1,4 @@
-# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# Copyright (c) 2003 Zope Corporation and Contributors.
 #
 # All Rights Reserved.
 #
@@ -14,7 +14,7 @@
 from CLR import System
 
 
-class Application(WinForms.Form):
+class Splitter(WinForms.Form):
     """A WinForms example transcribed to Python from the MSDN article:
        'Creating a Multipane User Interface with Windows Forms'."""
 
@@ -83,16 +83,16 @@
 
         self.Text = "Intricate UI Example"
 
+    def Dispose(self):
+        self.components.Dispose()
+        WinForms.Form.Dispose(self)
 
 
 
-    def run(self):
-        WinForms.Application.Run(self)
-
-
 def main():
-    Application().run()
-
+    app = Splitter()
+    WinForms.Application.Run(app)
+    app.Dispose()
 
 if __name__ == '__main__':
     main()


=== PythonNet/demo/wordpad.py 1.2 => 1.3 ===
--- PythonNet/demo/wordpad.py:1.2	Wed Oct 22 22:53:09 2003
+++ PythonNet/demo/wordpad.py	Mon Oct 27 21:06:59 2003
@@ -1,4 +1,4 @@
-# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# Copyright (c) 2003 Zope Corporation and Contributors.
 #
 # All Rights Reserved.
 #
@@ -11,6 +11,8 @@
 
 import CLR.System.Windows.Forms as WinForms
 from CLR.System.Drawing import Color, Size, Point
+from CLR.System.Text import Encoding
+from CLR.System.IO import File
 from CLR import System
 
 
@@ -18,14 +20,14 @@
     """A simple example winforms application similar to wordpad."""
 
     def __init__(self):
+        self.filename = ''
         self.word_wrap = 1
         self.doctype = 1
-        self.changed = 0
         self.InitializeComponent()
+        self.NewDocument()
 
     def InitializeComponent(self):
         """Initialize form components."""
-
         self.components = System.ComponentModel.Container()
 
         self.openFileDialog = WinForms.OpenFileDialog()
@@ -165,20 +167,20 @@
         # ===================================================================
         # Format Menu
         # ===================================================================
-        
-        self.menuFormatFont.Text = "Fo&nt"
-        self.menuFormatFont.Index = 0
-        self.menuFormatFont.Click += self.OnClickFormatFont
 
         self.menuFormatWordWrap.Text = "Word Wrap"
         self.menuFormatWordWrap.Checked = self.word_wrap
         self.menuFormatWordWrap.Index = 1
         self.menuFormatWordWrap.Click += self.OnClickFormatWordWrap
 
+        self.menuFormatFont.Text = "Fo&nt"
+        self.menuFormatFont.Index = 0
+        self.menuFormatFont.Click += self.OnClickFormatFont
+
         self.formatMenu.Text = "F&ormat"
         self.formatMenu.Index = 2
 
-        items = (self.menuFormatFont, self.menuFormatWordWrap)
+        items = (self.menuFormatWordWrap, self.menuFormatFont)
 
         self.formatMenu.MenuItems.AddRange(items)
 
@@ -195,11 +197,11 @@
         self.aboutMenu.Index = 3
         self.aboutMenu.MenuItems.Add(self.menuHelpAbout)
 
-
+        self.statusBarPanel1.Dock = WinForms.DockStyle.Fill
         self.statusBarPanel1.Text = "Ready"
         self.statusBarPanel1.Width = 755
 
-
+        self.richTextBox.Dock = WinForms.DockStyle.Fill
         self.richTextBox.Size = System.Drawing.Size(795, 485)
         self.richTextBox.TabIndex = 0
         self.richTextBox.AutoSize = 1
@@ -239,49 +241,48 @@
         self.Controls.Add(self.richTextBox)
         self.statusBarPanel1.EndInit()
 
-
     def Dispose(self):
         self.components.Dispose()
-        #WinForms.Form.Dispose(self, 1)
-
-
-
-
-
+        WinForms.Form.Dispose(self)
+        
 
     def OnClickFileNew(self, sender, args):
-        pass
+        self.SaveChangesDialog()
+        self.NewDocument()
 
     def OnClickFileOpen(self, sender, args):
-        pass
+        self.SaveChangesDialog()
+        self.OpenDocument()
 
     def OnClickFileSave(self, sender, args):
-        pass
+        self.SaveDocument()
 
     def OnClickFileSaveAs(self, sender, args):
-        pass
+        self.filename = ''
+        self.SaveDocument()
 
     def OnClickFileExit(self, sender, args):
-        pass
+        self.SaveChangesDialog()
+        self.Close()
 
 
     def OnClickEditUndo(self, sender, args):
-        pass
+        self.richTextBox.Undo()
 
     def OnClickEditRedo(self, sender, args):
-        pass
+        self.richTextBox.Redo()
 
     def OnClickEditCut(self, sender, args):
-        pass
+        self.richTextBox.Cut()
 
     def OnClickEditCopy(self, sender, args):
-        pass
+        self.richTextBox.Copy()
 
     def OnClickEditPaste(self, sender, args):
-        pass
+        self.richTextBox.Paste()
 
     def OnClickEditSelectAll(self, sender, args):
-        pass
+        self.richTextBox.SelectAll()
 
 
     def OnClickFormatWordWrap(self, sender, args):
@@ -291,13 +292,88 @@
         self.word_wrap = value
 
     def OnClickFormatFont(self, sender, args):
-        pass
-
+        if self.fontDialog.ShowDialog() == WinForms.DialogResult.OK:
+            self.richTextBox.SelectionFont = self.fontDialog.Font
 
     def OnClickHelpAbout(self, sender, args):
         AboutForm().ShowDialog(self)
 
 
+    def NewDocument(self):
+        self.doctype = 1
+        self.richTextBox.Rtf = ''
+        self.richTextBox.Text = ''
+        self.Text = 'Python Wordpad - (New Document)'
+        self.filename = ''
+
+    def OpenDocument(self):
+        if self.openFileDialog.ShowDialog() != WinForms.DialogResult.OK:
+            return
+
+        filename = self.openFileDialog.FileName
+
+        stream = File.OpenRead(filename)
+
+        buff = System.Array.CreateInstance(System.Byte, 1024)
+        data = []
+        read = -1
+
+        while (read != 0):
+            buff.Initialize()
+            read = stream.Read(buff, 0, 1024)
+            temp = Encoding.ASCII.GetString(buff, 0, 1024)
+            data.append(temp)
+
+        data = ''.join(data)
+        stream.Close()
+
+        filename = self.filename = filename.lower()
+        
+        if filename.endswith('.rtf'):
+            self.richTextBox.Rtf = data
+            self.doctype = 2
+        else:
+            self.richTextBox.Text = data
+            self.doctype = 1
+
+        self.Text = 'Python Wordpad - %s' % filename
+        self.richTextBox.Select(0, 0)
+
+    def SaveDocument(self):
+        filename = self.filename
+
+        if not filename:
+            if self.saveFileDialog.ShowDialog() != WinForms.DialogResult.OK:
+                return
+            filename = self.saveFileDialog.FileName
+        
+        filename = self.filename = filename.lower()
+        self.Text = 'Python Wordpad - %s' % filename
+        
+        self.richTextBox.Select(0, 0)
+
+        stream = File.OpenWrite(filename)
+
+        if filename.endswith('.rtf'):
+            data = self.richTextBox.Rtf
+        else:
+            data = self.richTextBox.Text
+
+        data = System.Text.Encoding.ASCII.GetBytes(System.String(data))
+
+        stream.Write(data, 0, data.Length)
+        stream.Close()
+
+    def SaveChangesDialog(self):
+        if self.richTextBox.Modified:
+            if WinForms.MessageBox.Show(
+                "Save changes?", "Word Pad",
+                WinForms.MessageBoxButtons.OK |
+                WinForms.MessageBoxButtons.YesNo
+                ) == WinForms.DialogResult.Yes: 
+                self.SaveDocument()
+                return 1
+        return 0
 
 
 class AboutForm(WinForms.Form):
@@ -322,9 +398,10 @@
 
         self.label1.Location = System.Drawing.Point(20, 20)
         self.label1.Name = "label1"
-        self.label1.Size = System.Drawing.Size(156, 16)
+        self.label1.Size = System.Drawing.Size(296, 140)
         self.label1.TabIndex = 2
-        self.label1.Text = "Wordpad example application"
+        self.label1.Text = "Python Wordpad - an example winforms " \
+                           "application using Python for .NET"
 
         self.AutoScaleBaseSize = System.Drawing.Size(5, 13)
         self.ClientSize = System.Drawing.Size(300, 150)
@@ -339,19 +416,16 @@
         self.StartPosition = WinForms.FormStartPosition.CenterScreen
         self.Text = "About"
         self.ResumeLayout(0)
-        
 
     def OnClickClose(self, sender, args):
         self.Close()
 
 
 
-
 def main():
     app = Wordpad()
     WinForms.Application.Run(app)
     app.Dispose()
-
 
 if __name__ == '__main__':
     main()




More information about the Zope-CVS mailing list