scribble.py :  » GUI » FXPy » FXPy-1.0.5 » examples » 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 » FXPy 
FXPy » FXPy 1.0.5 » examples » scribble.py
#! /usr/bin/env python

from FXPy.fox import *

# Main window
class ScribbleWindow(FXMainWindow):
    # Message IDs
    ID_CANVAS = FXMainWindow.ID_LAST
    ID_CLEAR = ID_CANVAS + 1
    ID_LAST = ID_CLEAR + 1

    # Constructor
    def __init__(self, app):
        FXMainWindow.__init__(self, app, "Scribble Application", w=800, h=600)

        # Set up message map
        FXMAPFUNC(self, SEL_COMMAND, self.ID_CLEAR, ScribbleWindow.onCmdClear)
        FXMAPFUNC(self, SEL_UPDATE,  self.ID_CLEAR, ScribbleWindow.onUpdClear)
        FXMAPFUNC(self, SEL_PAINT,   self.ID_CANVAS,ScribbleWindow.onPaint)
        FXMAPFUNC(self, SEL_LEFTBUTTONPRESS, self.ID_CANVAS, ScribbleWindow.onMouseDown)
        FXMAPFUNC(self, SEL_LEFTBUTTONRELEASE, self.ID_CANVAS, ScribbleWindow.onMouseUp)
        FXMAPFUNC(self, SEL_MOTION, self.ID_CANVAS, ScribbleWindow.onMouseMove)

        # Main frame
        self.contents = FXHorizontalFrame(self,
            LAYOUT_SIDE_TOP|LAYOUT_FILL_X|LAYOUT_FILL_Y,0,0,0,0, 0,0,0,0)

        # Left pane to contain the canvas
        self.canvasFrame = FXVerticalFrame(self.contents,
            FRAME_SUNKEN|LAYOUT_FILL_X|LAYOUT_FILL_Y|LAYOUT_TOP|LAYOUT_LEFT,
            0,0,0,0,10,10,10,10)

        # Label above the canvas
        FXLabel(self.canvasFrame, "Canvas Frame", None,
            JUSTIFY_CENTER_X|LAYOUT_FILL_X)

        # Horizontal divider line
        FXHorizontalSeparator(self.canvasFrame, SEPARATOR_GROOVE|LAYOUT_FILL_X)

        # Drawing canvas
        self.canvas = FXCanvas(self.canvasFrame, self, self.ID_CANVAS,
            FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_X|LAYOUT_FILL_Y|LAYOUT_TOP|LAYOUT_LEFT)

        # Right pane for the buttons
        self.buttonFrame = FXVerticalFrame(self.contents,
            FRAME_SUNKEN|LAYOUT_FILL_Y|LAYOUT_TOP|LAYOUT_LEFT,
            0,0,0,0,10,10,10,10)

        # Label above the buttons
        FXLabel(self.buttonFrame, "Button Frame", None,
            JUSTIFY_CENTER_X|LAYOUT_FILL_X)

        # Horizontal divider line
        FXHorizontalSeparator(self.buttonFrame, SEPARATOR_RIDGE|LAYOUT_FILL_X)

        # Button to clear
        self.clear = FXButton(self.buttonFrame, "&Clear", None, self,
            self.ID_CLEAR,
            FRAME_THICK|FRAME_RAISED|LAYOUT_FILL_X|LAYOUT_TOP|LAYOUT_LEFT,
            0,0,0,0,10,10,5,5)

        # Exit button
        FXButton(self.buttonFrame, "&Exit", None, app, FXApp.ID_QUIT,
            FRAME_THICK|FRAME_RAISED|LAYOUT_FILL_X|LAYOUT_TOP|LAYOUT_LEFT,
            0,0,0,0,10,10,5,5)

        # Initialize private variables
        self.drawColor = FXRGB(255,0,0)
        self.mdflag = 0
        self.dirty = 0

    # Create and initialize
    def create(self):
        FXMainWindow.create(self)
        self.show(PLACEMENT_SCREEN)

    # Mouse button was pressed somewhere
    def onMouseDown(self, sender, sel, ptr):
        self.mdflag = 1

    # Mouse has moved, so draw a line
    def onMouseMove(self, sender, sel, ev):
        if self.mdflag == 1:
            dc = FXDCWindow(self.canvas)
            dc.setForeground(self.drawColor)
            dc.drawLine(ev.last_x, ev.last_y, ev.win_x, ev.win_y)
            self.dirty = 1

    # The mouse button was released again
    def onMouseUp(self, sender, sel, ev):
        if self.mdflag == 1:
            dc = FXDCWindow(self.canvas)
            dc.setForeground(self.drawColor)
            dc.drawLine(ev.last_x, ev.last_y, ev.win_x, ev.win_y)
            self.dirty = 1
            self.mdflag = 0

    # Paint the canvas
    def onPaint(self, sender, sel, ev):
        dc = FXDCWindow(self.canvas, ev)
        dc.setForeground(self.canvas.getBackColor())
        dc.fillRectangle(ev.rect.x, ev.rect.y, ev.rect.w, ev.rect.h)

    # Handle the clear message
    def onCmdClear(self, sender, sel, ptr):
        dc = FXDCWindow(self.canvas)
        dc.setForeground(self.canvas.getBackColor())
        dc.fillRectangle(0, 0, self.canvas.getWidth(), self.canvas.getHeight())
        self.dirty = 0

    # Update the clear button
    def onUpdClear(self, sender, sel, ptr):
        if self.dirty == 1:
            sender.handle(self,MKUINT(FXWindow.ID_ENABLE,SEL_COMMAND),NULL)
        else:
            sender.handle(self,MKUINT(FXWindow.ID_DISABLE,SEL_COMMAND),NULL)

def runme():
    # Make application
    application = FXApp("Scribble", "Test")

    # Start app
    import sys
    application.init(sys.argv)

    # Create scribble window
    scribble = ScribbleWindow(application)

    # Create the application's windows
    application.create()

    # Run the application
    application.run()

# Main program begins here
if __name__ == "__main__":
    runme()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.