forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyruler
executable file
·115 lines (94 loc) · 3.17 KB
/
pyruler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
# Show a ruler, in pixels.
# Copyright 2007 by Akkana Peck.
# You may use, distribute or modify this program under the terms of the GPL.
VersionString = "PyTopo Version 0.5, Copyright (c)2006 by Akkana Peck."
import sys, os, gtk
gHorizontal = True
gStep = 25
# X/gtk graphics global variables we need:
gDrawing_area = 0
gGc = 0
gWidth = 1024
gHeight = 70
gWin = 0
# Need a null event for the spurious focus events:
def nop(*args) :
return True
def expose(widget, event) :
global gGc, Width, gHeight
if gGc == 0 :
gGc = gDrawing_area.window.new_gc()
gWidth, gHeight = gDrawing_area.window.get_size()
if gHorizontal :
bigdim = gWidth
smalldim = gHeight
else :
bigdim = gHeight
smalldim = gWidth
ticsize = smalldim / 7
for i in range(gStep, bigdim, gStep) :
if i % 100 == 0 :
thistic = ticsize * 4
elif i % 50 == 0 :
thistic = ticsize * 2
else :
thistic = ticsize
if gHorizontal :
gDrawing_area.window.draw_line(gGc, i, 0, i, thistic)
gDrawing_area.window.draw_line(gGc, i, smalldim,
i, smalldim-thistic)
else :
gDrawing_area.window.draw_line(gGc, 0, i, thistic, i)
gDrawing_area.window.draw_line(gGc, smalldim, i,
smalldim-thistic, i)
#gDrawing_area.window.draw_line(gGc, 0, 0, gWidth, gHeight)
return True
def resize(new_width, new_height) :
global gWidth, gHeight
gWidth = new_width
gHeight = new_height
gDrawing_area.set_size_request(10, 10)
# For some reason, resizing gDrawing_area.window or
# gDrawing_area.parent.window doesn't work; we have
# to save the original window.
gWin.resize(gWidth, gHeight)
def key_press(widget, event) :
global gWidth, gHeight, gHorizontal
if event.string == "q" :
sys.exit(0)
return True
if event.string == "h" and not gHorizontal :
gHorizontal = True
resize(gHeight, gWidth)
expose
return True
if event.string == "v" and gHorizontal :
gHorizontal = False
resize(gHeight, gWidth)
expose
return True
return True
def init() :
global gWin, gDrawing_area, gGc, gWidth, gHeight
gWin = gtk.Window()
gWin.set_name("PyRuler")
gWin.connect("destroy", gtk.main_quit)
gDrawing_area = gtk.DrawingArea()
gDrawing_area.set_size_request(gWidth, gHeight)
gWin.add(gDrawing_area)
gDrawing_area.show()
gDrawing_area.set_events(gtk.gdk.EXPOSURE_MASK)
gDrawing_area.connect("expose-event", expose)
# The default focus in/out handlers on drawing area cause
# spurious expose events. Trap the focus events, to block that:
gDrawing_area.connect("focus-in-event", nop)
gDrawing_area.connect("focus-out-event", nop)
# Handle key presses on the drawing area.
# If seeing spurious expose events, try setting them on win instead,
# and comment out gtk.CAN_FOCUS.
gDrawing_area.set_flags(gtk.CAN_FOCUS)
gDrawing_area.connect("key-press-event", key_press)
gWin.show()
gtk.main()
init()