mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Initial revision
This commit is contained in:
parent
6da6aebfdb
commit
453bd408bd
18 changed files with 2990 additions and 0 deletions
34
Demo/sgi/gl/glstdwin/fontchart.py
Normal file
34
Demo/sgi/gl/glstdwin/fontchart.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
import stdwingl
|
||||
|
||||
import stdwin
|
||||
from stdwinevents import *
|
||||
|
||||
def main():
|
||||
size = 12
|
||||
w = stdwin.open('Font chart ' + `size`)
|
||||
while 1:
|
||||
type, window, detail = stdwin.getevent()
|
||||
if type == WE_CLOSE:
|
||||
break
|
||||
if type == WE_DRAW:
|
||||
width, height = w.getwinsize()
|
||||
d = w.begindrawing()
|
||||
d.setsize(size)
|
||||
h, v = 0, 0
|
||||
for c in range(32, 256):
|
||||
ch = chr(c)
|
||||
chw = d.textwidth(ch)
|
||||
if h + chw > width:
|
||||
v = v + d.lineheight()
|
||||
h = 0
|
||||
if v >= height:
|
||||
break
|
||||
d.text((h, v), ch)
|
||||
h = h + chw
|
||||
del d
|
||||
if type == WE_MOUSE_UP:
|
||||
size = size + 1
|
||||
w.settitle('Font chart ' + `size`)
|
||||
w.change((0, 0), (2000, 2000))
|
||||
|
||||
main()
|
135
Demo/sgi/gl/glstdwin/glstdwdraw.py
Normal file
135
Demo/sgi/gl/glstdwin/glstdwdraw.py
Normal file
|
@ -0,0 +1,135 @@
|
|||
# Define drawing operations for GL stdwin
|
||||
|
||||
import gl
|
||||
import fm
|
||||
from GL import LO_XOR, LO_SRC
|
||||
from glstdwin import MASK
|
||||
|
||||
class DrawingObject:
|
||||
#
|
||||
def _init(self, win):
|
||||
self.fg = win._fg
|
||||
self.bg = win._bg
|
||||
self.font = win._font
|
||||
self.size = win._size
|
||||
self.width, self.height = win._area[1]
|
||||
gl.winset(win._gid)
|
||||
gl.color(self.fg)
|
||||
return self
|
||||
#
|
||||
def setfont(self, fontname):
|
||||
self.font = fm.findfont(fontname).scalefont(self.size)
|
||||
#
|
||||
def setsize(self, size):
|
||||
ratio = float(size) / float(self.size)
|
||||
self.size = size
|
||||
self.font = self.font.scalefont(ratio)
|
||||
#
|
||||
def setfgcolor(self, color):
|
||||
self.fg = color
|
||||
gl.color(self.fg)
|
||||
#
|
||||
def setbgcolor(self, color):
|
||||
self.bg = color
|
||||
#
|
||||
def cliprect(self, area):
|
||||
#print 'cliprect', area
|
||||
(left, top), (right, bottom) = area
|
||||
gl.scrmask(left, right, self.height-bottom, self.height-top)
|
||||
#
|
||||
def noclip(self):
|
||||
#print 'noclip()'
|
||||
gl.scrmask(0, self.width, 0, self.height)
|
||||
#
|
||||
def paint(self, ((left, top), (right, bottom))):
|
||||
gl.rectf(left, top, right, bottom)
|
||||
#
|
||||
def box(self, ((left, top), (right, bottom))):
|
||||
#print 'box', ((left, top), (right, bottom))
|
||||
gl.rect(left, top, right, bottom)
|
||||
#
|
||||
def circle(self, ((h, v), radius)):
|
||||
gl.circ(h, v, radius)
|
||||
#
|
||||
def elarc(self, (center, (rh, rv), a1, a2)):
|
||||
pass # XXX
|
||||
#
|
||||
def erase(self, ((left, top), (right, bottom))):
|
||||
#print 'erase', ((left, top), (right, bottom))
|
||||
gl.color(self.bg)
|
||||
gl.rectf(left, top, right, bottom)
|
||||
gl.color(self.fg)
|
||||
#
|
||||
def invert(self, ((left, top), (right, bottom))):
|
||||
#print 'invert', ((h0, v0), (h1, v1))
|
||||
gl.logicop(LO_XOR)
|
||||
gl.color(self.bg)
|
||||
gl.rectf(left, top, right, bottom)
|
||||
gl.color(self.fg)
|
||||
gl.logicop(LO_SRC)
|
||||
#
|
||||
def line(self, ((h0, v0), (h1, v1))):
|
||||
#print 'line', ((h0, v0), (h1, v1))
|
||||
gl.bgnline()
|
||||
gl.v2i(h0, v0)
|
||||
gl.v2i(h1, v1)
|
||||
gl.endline()
|
||||
#
|
||||
def xorline(self, ((h0, v0), (h1, v1))):
|
||||
#print 'xorline', ((h0, v0), (h1, v1))
|
||||
gl.logicop(LO_XOR)
|
||||
gl.color(self.bg)
|
||||
gl.bgnline()
|
||||
gl.v2i(h0, v0)
|
||||
gl.v2i(h1, v1)
|
||||
gl.endline()
|
||||
gl.color(self.fg)
|
||||
gl.logicop(LO_SRC)
|
||||
#
|
||||
def point(self, (h, v)):
|
||||
#print 'point', (h, v)
|
||||
gl.bgnpoint()
|
||||
gl.v2i(h, v)
|
||||
gl.endpoint()
|
||||
#
|
||||
def text(self, ((h, v), string)):
|
||||
#print 'text', ((h, v), string)
|
||||
if h < 0:
|
||||
# If the point is outside the window
|
||||
# the whole string isn't drawn.
|
||||
# Skip the beginning of the string.
|
||||
# XXX What if the font is bigger than 20 pixels?
|
||||
i, n = 0, len(string)
|
||||
while h < -MASK and i < n:
|
||||
h = h + self.font.getstrwidth(string[i])
|
||||
i = i + 1
|
||||
string = string[i:]
|
||||
gl.cmov2(h, v + self.baseline())
|
||||
self.font.setfont()
|
||||
fm.prstr(string)
|
||||
#
|
||||
def shade(self, ((h, v), percent)):
|
||||
pass # XXX
|
||||
#
|
||||
def baseline(self):
|
||||
(printermatched, fixed_width, xorig, yorig, xsize, ysize, \
|
||||
height, nglyphs) = self.font.getfontinfo()
|
||||
return height - yorig
|
||||
#
|
||||
def lineheight(self):
|
||||
(printermatched, fixed_width, xorig, yorig, xsize, ysize, \
|
||||
height, nglyphs) = self.font.getfontinfo()
|
||||
return height
|
||||
#
|
||||
def textbreak(self, (string, width)):
|
||||
# XXX Slooooow!
|
||||
n = len(string)
|
||||
nwidth = self.textwidth(string[:n])
|
||||
while nwidth > width:
|
||||
n = n-1
|
||||
nwidth = self.textwidth(string[:n])
|
||||
return n
|
||||
#
|
||||
def textwidth(self, string):
|
||||
return self.font.getstrwidth(string)
|
||||
#
|
400
Demo/sgi/gl/glstdwin/glstdwin.py
Normal file
400
Demo/sgi/gl/glstdwin/glstdwin.py
Normal file
|
@ -0,0 +1,400 @@
|
|||
# GL STDWIN
|
||||
#
|
||||
# See stdwingl for a convenient hack to use this instead of built-in stdwin
|
||||
# without modifying your application, except for one line in the main file.
|
||||
#
|
||||
# Intrinsic differences with built-in stdwin (hard or impossible to fix):
|
||||
# - Need to call w.close() to close a window !!!
|
||||
# - Need to call m.close() to remove a menu !!!
|
||||
# - Doesn't enforce the existence of at most one drawing object
|
||||
# - No textedit package
|
||||
# - No X11 selections
|
||||
#
|
||||
# Not yet implemented:
|
||||
# - shade drawing
|
||||
# - elliptical arc drawing (need to play with transformation)
|
||||
# - more than one mouse button
|
||||
# - scroll bars (need to redo viewport handling to get this)
|
||||
# - partial redraws
|
||||
# - dialog boxes
|
||||
# - timer events
|
||||
# - cursors
|
||||
#
|
||||
# Extra features:
|
||||
# - color (for now, you need to know the colormap index)
|
||||
|
||||
|
||||
import gl
|
||||
import fm
|
||||
from GL import *
|
||||
from DEVICE import *
|
||||
from stdwinevents import *
|
||||
|
||||
|
||||
# Customizable constants
|
||||
#
|
||||
DEF_FONT = 'Times-Roman' # Default font
|
||||
DEF_SIZE = 12 # Default font size (points)
|
||||
MASK = 20 # Viewport minus scrmask
|
||||
|
||||
|
||||
# A structure to hold global variables
|
||||
#
|
||||
class Struct: pass
|
||||
G = Struct()
|
||||
#
|
||||
G.queue = [] # Pending STDWIN events
|
||||
G.drawqueue = [] # Windows that need WE_REDRAW
|
||||
G.windowmap = {} # Map window id to window object
|
||||
G.windowmap['0'] = None # For convenience
|
||||
G.focus = None # Input focus
|
||||
G.fg = BLACK # Foreground color
|
||||
G.bg = WHITE # Background color
|
||||
G.def_size = 0, 0 # Default window size
|
||||
G.def_pos = 0, 0 # Default window position
|
||||
#
|
||||
G.size = DEF_SIZE
|
||||
G.font = fm.findfont(DEF_FONT).scalefont(G.size)
|
||||
|
||||
|
||||
# Initialize GL
|
||||
#
|
||||
gl.foreground()
|
||||
gl.noport()
|
||||
dummygid = gl.winopen('')
|
||||
|
||||
# Ask for all sorts of events
|
||||
#
|
||||
# Both REDRAW (= resize and/or redraw!) and INPUTCHANGE are implicitly queued
|
||||
#qdevice(REDRAW)
|
||||
#qdevice(INPUTCHANGE)
|
||||
#
|
||||
# Keyboard
|
||||
gl.qdevice(KEYBD)
|
||||
gl.qdevice(LEFTARROWKEY)
|
||||
gl.qdevice(RIGHTARROWKEY)
|
||||
gl.qdevice(UPARROWKEY)
|
||||
gl.qdevice(DOWNARROWKEY)
|
||||
gl.qdevice(LEFTALTKEY)
|
||||
gl.qdevice(RIGHTALTKEY)
|
||||
#
|
||||
# Mouse
|
||||
gl.qdevice(LEFTMOUSE)
|
||||
#gl.qdevice(MIDDLEMOUSE)
|
||||
gl.qdevice(RIGHTMOUSE) # Menu button
|
||||
# NB MOUSEX, MOUSEY events are queued on button down
|
||||
#
|
||||
# Window close requests
|
||||
gl.qdevice(WINQUIT)
|
||||
gl.qdevice(WINSHUT)
|
||||
#
|
||||
# These aren't needed
|
||||
#gl.qdevice(TIMER0)
|
||||
#gl.qdevice(WINFREEZE)
|
||||
#gl.qdevice(WINTHAW)
|
||||
#gl.qdevice(REDRAWICONIC)
|
||||
|
||||
|
||||
# STDWIN: create a new window
|
||||
#
|
||||
def open(title):
|
||||
h, v = G.def_pos
|
||||
width, height = G.def_size
|
||||
if h > 0 or v > 0:
|
||||
# Choose arbitrary defaults
|
||||
if h < 0: h = 10
|
||||
if v < 0: v = 30
|
||||
if width <= 0: width = 400
|
||||
if height <= 0: height = 300
|
||||
gl.prefposition(h, h+width, 1024-v, 1024-v-height)
|
||||
elif width > 0 or height > 0:
|
||||
if width <= 0: width = 400
|
||||
if height <= 0: height = 300
|
||||
gl.prefsize(width, height)
|
||||
from glstdwwin import WindowObject
|
||||
win = WindowObject()._init(title)
|
||||
G.windowmap[`win._gid`] = win
|
||||
return win
|
||||
|
||||
|
||||
# STDWIN: set default initial window position (0 means use default)
|
||||
#
|
||||
def setdefwinpos(h, v):
|
||||
G.def_pos = h, v
|
||||
|
||||
|
||||
# STDWIN: set default window size (0 means use default)
|
||||
#
|
||||
def setdefwinsize(width, height):
|
||||
G.def_size = width, height
|
||||
|
||||
|
||||
# STDWIN: beep or ring the bell
|
||||
#
|
||||
def fleep():
|
||||
gl.ringbell()
|
||||
|
||||
|
||||
# STDWIN: set default foreground color
|
||||
#
|
||||
def setfgcolor(color):
|
||||
G.fg = color
|
||||
|
||||
|
||||
# STDWIN: set default background color
|
||||
#
|
||||
def setbgcolor(color):
|
||||
G.bg = color
|
||||
|
||||
|
||||
# STDWIN: get default foreground color
|
||||
#
|
||||
def getfgcolor():
|
||||
return G.fgcolor
|
||||
|
||||
|
||||
# STDWIN: get default background color
|
||||
#
|
||||
def getbgcolor():
|
||||
return G.bgcolor
|
||||
|
||||
|
||||
# Table mapping characters to key codes
|
||||
#
|
||||
key2code = key = {}
|
||||
key['A'] = AKEY
|
||||
key['B'] = BKEY
|
||||
key['C'] = CKEY
|
||||
key['D'] = DKEY
|
||||
key['E'] = EKEY
|
||||
key['F'] = FKEY
|
||||
key['G'] = GKEY
|
||||
key['H'] = HKEY
|
||||
key['I'] = IKEY
|
||||
key['J'] = JKEY
|
||||
key['K'] = KKEY
|
||||
key['L'] = LKEY
|
||||
key['M'] = MKEY
|
||||
key['N'] = NKEY
|
||||
key['O'] = OKEY
|
||||
key['P'] = PKEY
|
||||
key['Q'] = QKEY
|
||||
key['R'] = RKEY
|
||||
key['S'] = SKEY
|
||||
key['T'] = TKEY
|
||||
key['U'] = UKEY
|
||||
key['V'] = VKEY
|
||||
key['W'] = WKEY
|
||||
key['X'] = XKEY
|
||||
key['Y'] = YKEY
|
||||
key['Z'] = ZKEY
|
||||
key['0'] = ZEROKEY
|
||||
key['1'] = ONEKEY
|
||||
key['2'] = TWOKEY
|
||||
key['3'] = THREEKEY
|
||||
key['4'] = FOURKEY
|
||||
key['5'] = FIVEKEY
|
||||
key['6'] = SIXKEY
|
||||
key['7'] = SEVENKEY
|
||||
key['8'] = EIGHTKEY
|
||||
key['9'] = NINEKEY
|
||||
del key
|
||||
#
|
||||
code2key = {}
|
||||
codelist = []
|
||||
for key in key2code.keys():
|
||||
code = key2code[key]
|
||||
code2key[`code`] = key
|
||||
codelist.append(code)
|
||||
del key
|
||||
|
||||
|
||||
# STDWIN: wait for the next event
|
||||
#
|
||||
commands = {}
|
||||
commands['\r'] = WC_RETURN
|
||||
commands['\b'] = WC_BACKSPACE
|
||||
commands['\t'] = WC_TAB
|
||||
#
|
||||
def getevent():
|
||||
while 1:
|
||||
#
|
||||
# Get next event from the processed queue, if any
|
||||
#
|
||||
if G.queue:
|
||||
event = G.queue[0]
|
||||
del G.queue[0]
|
||||
#print 'getevent from queue -->', event
|
||||
return event
|
||||
#
|
||||
# Get next event from the draw queue, if any,
|
||||
# but only if there is nothing in the system queue.
|
||||
#
|
||||
if G.drawqueue and not gl.qtest():
|
||||
win = G.drawqueue[0]
|
||||
del G.drawqueue[0]
|
||||
gl.winset(win._gid)
|
||||
gl.color(win._bg)
|
||||
gl.clear()
|
||||
event = WE_DRAW, win, win._area
|
||||
#print 'getevent from drawqueue -->', event
|
||||
return event
|
||||
#
|
||||
# Get next event from system queue, blocking if necessary
|
||||
# until one is available.
|
||||
# Some cases immediately return the event, others do nothing
|
||||
# or append one or more events to the processed queue.
|
||||
#
|
||||
dev, val = gl.qread()
|
||||
#
|
||||
if dev == REDRAW:
|
||||
win = G.windowmap[`val`]
|
||||
old_area = win._area
|
||||
win._fixviewport()
|
||||
win._needredraw()
|
||||
if old_area <> win._area:
|
||||
#print 'getevent --> WE_SIZE'
|
||||
return WE_SIZE, win, None
|
||||
elif dev == KEYBD:
|
||||
if val == 3:
|
||||
raise KeyboardInterrupt # Control-C in window
|
||||
character = chr(val)
|
||||
if commands.has_key(character):
|
||||
return WE_COMMAND, G.focus, commands[character]
|
||||
return WE_CHAR, G.focus, character
|
||||
elif dev == LEFTARROWKEY:
|
||||
if val:
|
||||
return WE_COMMAND, G.focus, WC_LEFT
|
||||
elif dev == RIGHTARROWKEY:
|
||||
if val:
|
||||
return WE_COMMAND, G.focus, WC_RIGHT
|
||||
elif dev == UPARROWKEY:
|
||||
if val:
|
||||
return WE_COMMAND, G.focus, WC_UP
|
||||
elif dev == DOWNARROWKEY:
|
||||
if val:
|
||||
return WE_COMMAND, G.focus, WC_DOWN
|
||||
elif dev in (LEFTALTKEY, RIGHTALTKEY):
|
||||
if val:
|
||||
for code in codelist:
|
||||
gl.qdevice(code)
|
||||
else:
|
||||
for code in codelist:
|
||||
gl.unqdevice(code)
|
||||
elif dev in codelist:
|
||||
if val:
|
||||
event = G.focus._doshortcut(code2key[`dev`])
|
||||
if event:
|
||||
return event
|
||||
elif dev == LEFTMOUSE:
|
||||
G.mousex = gl.getvaluator(MOUSEX)
|
||||
G.mousey = gl.getvaluator(MOUSEY)
|
||||
if val:
|
||||
type = WE_MOUSE_DOWN
|
||||
gl.qdevice(MOUSEX)
|
||||
gl.qdevice(MOUSEY)
|
||||
else:
|
||||
type = WE_MOUSE_UP
|
||||
gl.unqdevice(MOUSEX)
|
||||
gl.unqdevice(MOUSEY)
|
||||
return _mouseevent(type)
|
||||
elif dev == MOUSEX:
|
||||
G.mousex = val
|
||||
return _mouseevent(WE_MOUSE_MOVE)
|
||||
elif dev == MOUSEY:
|
||||
G.mousey = val
|
||||
return _mouseevent(WE_MOUSE_MOVE)
|
||||
elif dev == RIGHTMOUSE: # Menu button press/release
|
||||
if val: # Press
|
||||
event = G.focus._domenu()
|
||||
if event:
|
||||
return event
|
||||
elif dev == INPUTCHANGE:
|
||||
if G.focus:
|
||||
G.queue.append(WE_DEACTIVATE, G.focus, None)
|
||||
G.focus = G.windowmap[`val`]
|
||||
if G.focus:
|
||||
G.queue.append(WE_ACTIVATE, G.focus, None)
|
||||
elif dev in (WINSHUT, WINQUIT):
|
||||
return WE_CLOSE, G.windowmap[`val`], None
|
||||
else:
|
||||
print '*** qread() --> dev:', dev, 'val:', val
|
||||
|
||||
# Helper routine to construct a mouse (up, move or down) event
|
||||
#
|
||||
def _mouseevent(type):
|
||||
gl.winset(G.focus._gid)
|
||||
orgx, orgy = gl.getorigin()
|
||||
sizex, sizey = gl.getsize()
|
||||
x = G.mousex - orgx
|
||||
y = G.mousey - orgy
|
||||
return type, G.focus, ((x, sizey-y), 1, 0, 0)
|
||||
|
||||
|
||||
|
||||
|
||||
# STDWIN: text measuring functions
|
||||
|
||||
def baseline():
|
||||
(printermatched, fixed_width, xorig, yorig, xsize, ysize, \
|
||||
height, nglyphs) = G.font.getfontinfo()
|
||||
return height - yorig
|
||||
|
||||
def lineheight():
|
||||
(printermatched, fixed_width, xorig, yorig, xsize, ysize, \
|
||||
height, nglyphs) = G.font.getfontinfo()
|
||||
return height
|
||||
|
||||
def textbreak(string, width):
|
||||
# XXX Slooooow!
|
||||
n = len(string)
|
||||
nwidth = textwidth(string[:n])
|
||||
while nwidth > width:
|
||||
n = n-1
|
||||
nwidth = textwidth(string[:n])
|
||||
return n
|
||||
|
||||
def textwidth(string):
|
||||
return G.font.getstrwidth(string)
|
||||
|
||||
|
||||
# STDWIN: set default font and size
|
||||
|
||||
def setfont(fontname):
|
||||
G.font = fm.findfont(fontname).scalefont(G.size)
|
||||
|
||||
def setsize(size):
|
||||
ratio = float(size) / float(G.size)
|
||||
G.size = size
|
||||
G.font = G.font.scalefont(ratio)
|
||||
|
||||
|
||||
# Utility functions
|
||||
|
||||
# Exclusive-or of two BYTES
|
||||
#
|
||||
def xor(x, y):
|
||||
a = bits(x)
|
||||
b = bits(y)
|
||||
c = [0, 0, 0, 0, 0, 0, 0, 0]
|
||||
for i in range(8):
|
||||
c[i] = (a[i] + b[i]) % 2
|
||||
return stib(c)
|
||||
|
||||
# Return the bits of a byte as a list of 8 integers
|
||||
#
|
||||
def bits(x):
|
||||
b = [0, 0, 0, 0, 0, 0, 0, 0]
|
||||
for i in range(8):
|
||||
x, b[i] = divmod(x, 2)
|
||||
return b
|
||||
|
||||
# Convert a list of 8 integers (0|1) to a byte
|
||||
#
|
||||
def stib(b):
|
||||
x = 0
|
||||
shift = 1
|
||||
for i in range(8):
|
||||
x = x + b[i]*shift
|
||||
shift = shift*2
|
||||
return x
|
60
Demo/sgi/gl/glstdwin/glstdwmenu.py
Normal file
60
Demo/sgi/gl/glstdwin/glstdwmenu.py
Normal file
|
@ -0,0 +1,60 @@
|
|||
# Define menu operations for GL stdwin
|
||||
|
||||
import gl
|
||||
from glstdwin import key2code
|
||||
|
||||
class MenuObject:
|
||||
#
|
||||
def _init(self, (win, title)):
|
||||
self._win = win
|
||||
self._title = title
|
||||
self._items = []
|
||||
return self
|
||||
#
|
||||
def close(self):
|
||||
self._win.remove(self)
|
||||
del self._win
|
||||
#
|
||||
def additem(self, arg):
|
||||
if type(arg) == type(()):
|
||||
text, shortcut = arg
|
||||
else:
|
||||
text, shortcut = arg, None
|
||||
self._items.append([text, shortcut, 1, 0])
|
||||
#
|
||||
def setitem(self, (i, text)):
|
||||
self._items[i][0] = text
|
||||
#
|
||||
def enable(self, (i, flag)):
|
||||
self._items[i][2] = flag
|
||||
#
|
||||
def check(self, (i, flag)):
|
||||
self._items[i][3] = flag
|
||||
#
|
||||
def _makepup(self, firstitem):
|
||||
pup = gl.newpup()
|
||||
if self._title:
|
||||
gl.addtopup(pup, self._title + '%t', 0)
|
||||
for item in self._items:
|
||||
text = item[0]
|
||||
if not item[2]: # Disabled
|
||||
text = ' ( ' + text + ' )%x-1'
|
||||
else:
|
||||
if item[3]: # Check mark
|
||||
text = '-> ' + text
|
||||
else:
|
||||
text = ' ' + text
|
||||
if key2code.has_key(item[1]):
|
||||
text = text + ' [Alt-' + item[1] + ']'
|
||||
text = text + '%x' + `firstitem`
|
||||
gl.addtopup(pup, text, 0)
|
||||
firstitem = firstitem + 1
|
||||
return pup
|
||||
#
|
||||
def _checkshortcut(self, char):
|
||||
for i in range(len(self._items)):
|
||||
item = self._items[i]
|
||||
if item[2] and item[1] == char:
|
||||
return i
|
||||
return -1
|
||||
#
|
139
Demo/sgi/gl/glstdwin/glstdwwin.py
Normal file
139
Demo/sgi/gl/glstdwin/glstdwwin.py
Normal file
|
@ -0,0 +1,139 @@
|
|||
# Define window operations for STDWIN
|
||||
|
||||
import gl
|
||||
from stdwinevents import *
|
||||
from glstdwin import G # Global variables
|
||||
from glstdwin import MASK # Tunable constant
|
||||
|
||||
class WindowObject:
|
||||
#
|
||||
def _init(self, title):
|
||||
self._docsize = (0, 0)
|
||||
self._fg = G.fg
|
||||
self._bg = G.bg
|
||||
self._title = title
|
||||
self._font = G.font
|
||||
self._size = G.size
|
||||
self._menus = []
|
||||
self._gid = gl.winopen(title)
|
||||
gl.winconstraints() # To remove prefsize() effect
|
||||
self._fixviewport()
|
||||
self._needredraw()
|
||||
return self
|
||||
#
|
||||
def close(self):
|
||||
del G.windowmap[`self._gid`]
|
||||
gl.winclose(self._gid)
|
||||
self._gid = 0
|
||||
#
|
||||
def _needredraw(self):
|
||||
if self in G.drawqueue:
|
||||
G.drawqueue.remove(self)
|
||||
G.drawqueue.append(self)
|
||||
#
|
||||
def begindrawing(self):
|
||||
from glstdwdraw import DrawingObject
|
||||
return DrawingObject()._init(self)
|
||||
#
|
||||
def change(self, area):
|
||||
self._needredraw()
|
||||
# XXX Should record the area to be drawn?
|
||||
#
|
||||
def gettitle(self):
|
||||
return self._title
|
||||
#
|
||||
def getdocsize(self):
|
||||
return self._docsize
|
||||
#
|
||||
def getorigin(self):
|
||||
return self._area[0]
|
||||
#
|
||||
def getwinsize(self):
|
||||
return self._area[1]
|
||||
#
|
||||
def scroll(self, (area, by)):
|
||||
# XXX ought to use gl.rectcopy()
|
||||
if by <> (0, 0):
|
||||
self.change(area)
|
||||
#
|
||||
def setdocsize(self, docsize):
|
||||
self._docsize = docsize
|
||||
#
|
||||
def setorigin(self, origin):
|
||||
pass # XXX
|
||||
#
|
||||
def settimer(self, decisecs):
|
||||
pass # XXX
|
||||
#
|
||||
def settitle(self, title):
|
||||
self._title = title
|
||||
gl.wintitle(title)
|
||||
#
|
||||
def show(self, area):
|
||||
pass # XXX
|
||||
#
|
||||
def _fixviewport(self):
|
||||
#
|
||||
# Called after redraw or resize, and initially.
|
||||
#
|
||||
# Fix the coordinate system so that (0, 0) is top left,
|
||||
# units are pixels, and positive axes point right and down.
|
||||
#
|
||||
# Make the viewport slightly larger than the window,
|
||||
# and set the screenmask exactly to the window; this
|
||||
# help fixing character clipping.
|
||||
#
|
||||
# Set self._area to the window rectangle in STDWIN coords.
|
||||
#
|
||||
gl.winset(self._gid)
|
||||
gl.reshapeviewport()
|
||||
x0, x1, y0, y1 = gl.getviewport()
|
||||
width, height = x1-x0, y1-y0
|
||||
gl.viewport(x0-MASK, x1+MASK, y0-MASK, y1+MASK)
|
||||
gl.scrmask(x0, x1, y0, y1)
|
||||
gl.ortho2(-MASK, width+MASK, height+MASK, -MASK)
|
||||
self._area = (0, 0), (width, height)
|
||||
#
|
||||
def menucreate(self, title):
|
||||
from glstdwmenu import MenuObject
|
||||
menu = MenuObject()._init(self, title)
|
||||
self._menus.append(menu)
|
||||
return menu
|
||||
#
|
||||
def _domenu(self):
|
||||
if not self._menus:
|
||||
return None
|
||||
if len(self._menus) == 1:
|
||||
pup = self._menus[0]._makepup(0)
|
||||
val = gl.dopup(pup)
|
||||
gl.freepup(pup)
|
||||
if val < 0:
|
||||
return None
|
||||
return WE_MENU, self, (self._menus[0], val)
|
||||
#
|
||||
# More than one menu: use nested menus.
|
||||
#
|
||||
pups = []
|
||||
firstitem = 0
|
||||
for menu in self._menus:
|
||||
pups.append(menu._makepup(firstitem))
|
||||
firstitem = firstitem + 100
|
||||
pup = gl.newpup()
|
||||
for i in range(len(self._menus)):
|
||||
gl.addtopup(pup, self._menus[i]._title + '%m', pups[i])
|
||||
val = gl.dopup(pup)
|
||||
gl.freepup(pup)
|
||||
for pup in pups:
|
||||
gl.freepup(pup)
|
||||
if val < 0:
|
||||
return None
|
||||
i_menu, i_item = divmod(val, 100)
|
||||
return WE_MENU, self, (self._menus[i_menu], i_item)
|
||||
#
|
||||
def _doshortcut(self, char):
|
||||
for menu in self._menus:
|
||||
i = menu._checkshortcut(char)
|
||||
if i >= 0:
|
||||
return WE_MENU, self, (menu, i)
|
||||
return None
|
||||
#
|
10
Demo/sgi/gl/glstdwin/stdwingl.py
Normal file
10
Demo/sgi/gl/glstdwin/stdwingl.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
# If you put 'import stdwin_gl' in front of the main program of a program
|
||||
# using stdwin (before it has a chance to import the real stdwin!),
|
||||
# it will use glstdwin and think it is stdwin.
|
||||
|
||||
import sys
|
||||
if sys.modules.has_key('stdwin'):
|
||||
raise RuntimeError, 'too late -- stdwin has already been imported'
|
||||
|
||||
import glstdwin
|
||||
sys.modules['stdwin'] = glstdwin
|
43
Demo/sgi/gl/glstdwin/tcolor.py
Normal file
43
Demo/sgi/gl/glstdwin/tcolor.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
# Try colors -- display all 256 possible colors, with their color index
|
||||
|
||||
# import stdwingl
|
||||
|
||||
import stdwin
|
||||
from stdwinevents import *
|
||||
|
||||
NROWS = 16
|
||||
NCOLS = 16
|
||||
|
||||
def main():
|
||||
stdwin.setdefwinsize(NCOLS * stdwin.textwidth('12345'), \
|
||||
NROWS * stdwin.lineheight() * 3)
|
||||
w = stdwin.open('TestColors')
|
||||
#
|
||||
while 1:
|
||||
type, window, detail = stdwin.getevent()
|
||||
if type == WE_CLOSE:
|
||||
print 'Bye.'
|
||||
break
|
||||
elif type == WE_SIZE:
|
||||
w.change((0,0), (10000, 10000))
|
||||
elif type == WE_DRAW:
|
||||
width, height = w.getwinsize()
|
||||
d = w.begindrawing()
|
||||
for row in range(NROWS):
|
||||
for col in range(NCOLS):
|
||||
color = row*NCOLS + col
|
||||
d.setfgcolor(color)
|
||||
p = col*width/NCOLS, row*height/NROWS
|
||||
q = (col+1)*width/NCOLS, \
|
||||
(row+1)*height/NROWS
|
||||
d.paint(p, q)
|
||||
d.setfgcolor(0)
|
||||
d.box(p, q)
|
||||
d.text(p, `color`)
|
||||
p = p[0] , p[1]+ d.lineheight()
|
||||
d.setfgcolor(7)
|
||||
d.text(p, `color`)
|
||||
del d
|
||||
#
|
||||
|
||||
main()
|
70
Demo/sgi/gl/glstdwin/tglsw.py
Normal file
70
Demo/sgi/gl/glstdwin/tglsw.py
Normal file
|
@ -0,0 +1,70 @@
|
|||
import sys
|
||||
|
||||
if len(sys.argv) < 2:
|
||||
import stdwingl
|
||||
color = 1
|
||||
needclose = 1
|
||||
else:
|
||||
color = 0
|
||||
needclose = 0
|
||||
|
||||
import stdwin
|
||||
import time
|
||||
from stdwinevents import *
|
||||
from GL import BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE
|
||||
|
||||
def main():
|
||||
#
|
||||
stdwin.setdefwinsize(300, 300)
|
||||
stdwin.setdefwinpos(0, 0)
|
||||
if color: stdwin.setbgcolor(YELLOW)
|
||||
w1 = stdwin.open('Hello, world')
|
||||
w1.box = (10, 10), (90, 90)
|
||||
#
|
||||
stdwin.setdefwinsize(0, 0)
|
||||
stdwin.setdefwinpos(50, 50)
|
||||
if color: stdwin.setbgcolor(GREEN)
|
||||
w2 = stdwin.open('Second window')
|
||||
w2.box = (10, 10), (90, 90)
|
||||
#
|
||||
while w1 or w2:
|
||||
type, window, detail = stdwin.getevent()
|
||||
if type == WE_DRAW:
|
||||
d = window.begindrawing()
|
||||
if window == w1:
|
||||
if color: d.setfgcolor(BLACK)
|
||||
d.box((50, 50), (250, 250))
|
||||
if color: d.setfgcolor(RED)
|
||||
d.cliprect((50, 50), (250, 250))
|
||||
d.paint(w1.box)
|
||||
d.noclip()
|
||||
if color: d.setfgcolor(BLUE)
|
||||
d.line((0, 0), w1.box[0])
|
||||
elif window == w2:
|
||||
if color: d.setfgcolor(WHITE)
|
||||
d.box(w2.box)
|
||||
if color: d.setfgcolor(BLACK)
|
||||
d.text(w2.box[0], 'Hello world')
|
||||
else:
|
||||
print 'Strange draw???', window, detail
|
||||
del d
|
||||
elif type == WE_CLOSE:
|
||||
if needclose: window.close()
|
||||
if window == w1:
|
||||
w1 = None
|
||||
elif window == w2:
|
||||
w2 = None
|
||||
else:
|
||||
print 'weird close event???', window, detail
|
||||
elif type in (WE_MOUSE_DOWN, WE_MOUSE_MOVE, WE_MOUSE_UP):
|
||||
h, v = detail[0]
|
||||
window.box = (h, v), (h+80, v+80)
|
||||
window.change((0,0), (2000, 2000))
|
||||
elif type == WE_CHAR:
|
||||
print 'character', `detail`
|
||||
else:
|
||||
print type, window, detail
|
||||
#
|
||||
|
||||
main()
|
||||
print 'Done.'
|
44
Demo/sgi/gl/glstdwin/tmenu.py
Normal file
44
Demo/sgi/gl/glstdwin/tmenu.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
# Test menus
|
||||
|
||||
import stdwingl
|
||||
|
||||
import stdwin
|
||||
from stdwinevents import *
|
||||
|
||||
def main():
|
||||
w = stdwin.open('TestMenus')
|
||||
#
|
||||
items1 = 'Aap', 'Noot', 'Mies'
|
||||
m1 = w.menucreate('Menu-1')
|
||||
for item in items1:
|
||||
m1.additem(item, item[0])
|
||||
#
|
||||
items2 = 'Wim', 'Zus', 'Jet', 'Teun', 'Vuur'
|
||||
m2 = w.menucreate('Menu-2')
|
||||
for item in items2:
|
||||
m2.additem(item, `len(item)`)
|
||||
#
|
||||
m1.enable(1, 0)
|
||||
m2.check(1, 1)
|
||||
#
|
||||
while 1:
|
||||
type, window, detail = stdwin.getevent()
|
||||
if type == WE_CLOSE:
|
||||
break
|
||||
elif type == WE_DRAW:
|
||||
d = w.begindrawing()
|
||||
d.box((50,50), (100,100))
|
||||
del d
|
||||
elif type == WE_MENU:
|
||||
mp, i = detail
|
||||
if mp == m1:
|
||||
print 'Choice:', items1[i]
|
||||
elif mp == m2:
|
||||
print 'Choice:', items2[i]
|
||||
else:
|
||||
print 'Not one of my menus!'
|
||||
elif type == WE_CHAR:
|
||||
print 'Character', `detail`
|
||||
#
|
||||
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue