Cairo_Snippets.py :  » GUI » wxPython » wxPython-src-2.8.11.0 » wxPython » demo » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » GUI » wxPython 
wxPython » wxPython src 2.8.11.0 » wxPython » demo » Cairo_Snippets.py

import wx

try:
    import wx.lib.wxcairo
    import cairo
    haveCairo = True
except ImportError:
    haveCairo = False

from math import pi
from Main import opj,DemoCodeEditor

#----------------------------------------------------------------------

# TODO:  Add the ability for the user to edit and render their own snippet

class DisplayPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, style=wx.BORDER_SIMPLE)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.curr_snippet = ''


    def OnPaint(self, evt):
        dc = wx.PaintDC(self)
        
        if self.curr_snippet:
            width, height = self.GetClientSize()
            cr = wx.lib.wxcairo.ContextFromDC(dc)
            exec(self.curr_snippet, globals(), locals())


    def SetSnippet(self, text):
        self.curr_snippet = text
        self.Refresh()

        

class TestPanel(wx.Panel):
    def __init__(self, parent, log):
        self.log = log
        wx.Panel.__init__(self, parent, -1)

        self.lb = wx.ListBox(self, choices=snip_list)
        self.canvas = DisplayPanel(self)
        self.editor = DemoCodeEditor(self, style=wx.BORDER_SIMPLE)
        self.editor.SetEditable(False)
        
        self.Bind(wx.EVT_LISTBOX, self.OnListBoxSelect, self.lb)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.lb, 0, wx.EXPAND)
        sizer.Add((15,1))
        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.canvas, 1, wx.EXPAND)
        vbox.Add((1, 15))
        vbox.Add(self.editor, 1, wx.EXPAND)
        sizer.Add(vbox, 1, wx.EXPAND)
        border = wx.BoxSizer()
        border.Add(sizer, 1, wx.EXPAND|wx.ALL, 30)
        self.SetSizer(border)

        
    def OnListBoxSelect(self, evt):
        snippet_file = opj('snippets/%s.py' % evt.GetString())
        text = file(snippet_file).read()
        self.canvas.SetSnippet(text)
        self.editor.SetValue(text)
        
        
#----------------------------------------------------------------------

if not haveCairo:
    from Main import MessagePanel
    def runTest(frame, nb, log):
        win = MessagePanel(nb, 'This demo requires the Pycairo package,\n'
                           'or there is some other unmet dependency.',
                       'Sorry', wx.ICON_WARNING)
        return win
else:
    
    def runTest(frame, nb, log):
        win = TestPanel(nb, log)
        return win

#----------------------------------------------------------------------

if haveCairo:
    extra = "\n<h3>wx.lib.wxcairo</h3>\n%s" % (
        wx.lib.wxcairo.__doc__.replace('\n\n', '\n<p>'))
else:
    extra = '\n<p>See the docstring in the wx.lib.wxcairo module for details about installing dependencies.'

    
overview = """<html><body>
<h2><center>Cairo Integration</center></h2>

The wx.lib.wxcairo module provides a bit of glue that will allow you to
use the Pycairo package drawing directly on wx.DC's.

<p> This sample draws the standard 'snippet' examples that come with
the Pycairo pacakge, and a few others.  The C version of the samples
can be seen at http://cairographics.org/samples/

<p> In most snippets you'll see a call to a snippet_normalize()
function.  This is part of the demo and not part of Cairo.  It is
simply scaling the context such that the range of 0.0 to 1.0 is the
min(width, height) of the window in pixels.  In other words, it allows
the rendering code to use a range or 0.0 to 1.0 and it will always fit
in the drawing area.  (Try resizing the demo and reselecting a snippet
to see this.)

<pre>
def snippet_normalize(ctx, width, height):
    size = min(width, height)
    ctx.scale(size, size)
    ctx.set_line_width(0.04)
</pre>

%s
</body></html>
""" % extra



if __name__ == '__main__':
    import sys,os
    import run
    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.