| 1 |
"""Interface with RISC OS window manager (wimp) and related functions. |
"""Interface with RISC OS window manager (wimp) and related functions. |
| 2 |
|
|
| 3 |
|
Licensed under the MIT License, |
| 4 |
|
http://www.opensource.org/licenses/mit-license |
| 5 |
|
Copyright 2005 James Bursa <james@semichrome.net> |
| 6 |
|
|
| 7 |
Class hierarchy: |
Class hierarchy: |
| 8 |
|
|
| 9 |
window -- base window class |
window -- base window class |
| 64 |
import string |
import string |
| 65 |
import sys |
import sys |
| 66 |
import types |
import types |
| 67 |
|
import rufl |
| 68 |
|
|
| 69 |
task_handle = 0 |
task_handle = 0 |
| 70 |
task_name = 'Application' |
task_name = 'Application' |
| 80 |
def task(name, dir, task, messages = (), debug = 0, profile = 0, mask = None): |
def task(name, dir, task, messages = (), debug = 0, profile = 0, mask = None): |
| 81 |
"Initialise the task with the wimp and load the messages file" |
"Initialise the task with the wimp and load the messages file" |
| 82 |
global task_name, directory, b, wimp_version, task_handle, territory, m, task_module, _trace |
global task_name, directory, b, wimp_version, task_handle, territory, m, task_module, _trace |
| 83 |
|
|
| 84 |
hourglass_on() |
hourglass_on() |
| 85 |
task_name = name |
task_name = name |
| 86 |
directory = dir |
directory = dir |
| 118 |
print _depth * ' ', event + ':', arg |
print _depth * ' ', event + ':', arg |
| 119 |
return _trace |
return _trace |
| 120 |
sys.settrace(_trace) |
sys.settrace(_trace) |
| 121 |
|
|
| 122 |
# buffer for various stuff |
# buffer for various stuff |
| 123 |
b = swi.block(64) |
b = swi.block(64) |
| 124 |
|
|
| 125 |
# initialise with wimp |
# initialise with wimp |
| 126 |
# place the message list in the buffer |
# place the message list in the buffer |
| 127 |
b[0] = 0x502 # Message_HelpRequest |
b[0] = 0x502 # Message_HelpRequest |
| 163 |
except: |
except: |
| 164 |
if debug > 1: |
if debug > 1: |
| 165 |
sys.settrace(None) |
sys.settrace(None) |
| 166 |
|
|
| 167 |
type, value, traceb = sys.exc_info() |
type, value, traceb = sys.exc_info() |
| 168 |
swi.swi(0x400df, 'sis', |
swi.swi(0x400df, 'sis', |
| 169 |
' Internal error - must exit (' + str(type) + ': ' + str(value) + ')', |
' Internal error - must exit (' + str(type) + ': ' + str(value) + ')', |
| 170 |
1, task_name) |
1, task_name) |
| 171 |
|
|
| 172 |
if debug: |
if debug: |
| 173 |
traceback.print_exc() |
traceback.print_exc() |
| 174 |
trace = traceback.extract_tb(traceb) |
trace = traceback.extract_tb(traceb) |
| 180 |
swi.swi(0x42589, '') |
swi.swi(0x42589, '') |
| 181 |
except: |
except: |
| 182 |
pass |
pass |
| 183 |
|
|
| 184 |
if sys.stderr.tell() > 0: |
if sys.stderr.tell() > 0: |
| 185 |
sys.stderr.close() |
sys.stderr.close() |
| 186 |
os.system('Filer_Run ' + directory + '.^.Output') |
os.system('Filer_Run ' + directory + '.^.Output') |
| 187 |
else: |
else: |
| 188 |
sys.stderr.close() |
sys.stderr.close() |
| 189 |
|
|
| 190 |
# Wimp_CloseDown |
# Wimp_CloseDown |
| 191 |
swi.swi(0x400dd, 'ii', task_handle, 0x4b534154) |
swi.swi(0x400dd, 'ii', task_handle, 0x4b534154) |
| 192 |
hourglass_off() |
hourglass_off() |
| 193 |
|
|
| 194 |
|
|
| 195 |
def _go(): |
def _go(): |
| 196 |
"Run the program" |
"Run the program" |
| 197 |
task_module.init() |
task_module.init() |
| 198 |
hourglass_off() |
hourglass_off() |
| 199 |
while 1: |
while 1: |
| 200 |
handle(poll(poll_mask)) |
handle(poll(poll_mask)) |
| 201 |
|
|
| 202 |
|
|
| 203 |
def quit(): |
def quit(): |
| 204 |
"Trigger quitting" |
"Trigger quitting" |
| 205 |
for f in fonts.values(): |
for f in fonts.values(): |
| 493 |
(screen_width + width) / 2, (screen_height + height) / 2, |
(screen_width + width) / 2, (screen_height + height) / 2, |
| 494 |
bb[5], bb[6], behind) |
bb[5], bb[6], behind) |
| 495 |
|
|
| 496 |
|
def open_full_size(self, behind = None): |
| 497 |
|
bb = swi.block(20) |
| 498 |
|
bb[0] = self.handle |
| 499 |
|
swi.swi('Wimp_GetWindowState', '.b', bb) |
| 500 |
|
bb[3] = 10000 |
| 501 |
|
bb[2] = -10000 |
| 502 |
|
if behind: |
| 503 |
|
bb[7] = behind |
| 504 |
|
swi.swi('Wimp_OpenWindow', '.b', bb) |
| 505 |
|
self.is_open = 1 |
| 506 |
|
|
| 507 |
def open_as_menu(self): |
def open_as_menu(self): |
| 508 |
"Open the window as a menu centred on the pointer" |
"Open the window as a menu centred on the pointer" |
| 509 |
bb = swi.block(20) |
bb = swi.block(20) |
| 524 |
# Wimp_CloseWindow |
# Wimp_CloseWindow |
| 525 |
swi.swi(0x400c6, '.b', bb) |
swi.swi(0x400c6, '.b', bb) |
| 526 |
self.is_open = 0 |
self.is_open = 0 |
| 527 |
|
|
| 528 |
def entering(self): |
def entering(self): |
| 529 |
"Pointer entering window" |
"Pointer entering window" |
| 530 |
|
|
| 531 |
def leaving(self): |
def leaving(self): |
| 532 |
"Pointer leaving window" |
"Pointer leaving window" |
| 533 |
|
|
| 534 |
def click(self, icon, buttons, x, y, sx, sy): |
def click(self, icon, buttons, x, y, sx, sy): |
| 535 |
"Mouse click in window" |
"Mouse click in window" |
| 536 |
self.contents.reverse() |
self.contents.reverse() |
| 542 |
return 1 |
return 1 |
| 543 |
self.contents.reverse() |
self.contents.reverse() |
| 544 |
return 0 |
return 0 |
| 545 |
|
|
| 546 |
def help(self, icon): |
def help(self, icon): |
| 547 |
"Return interactive help message" |
"Return interactive help message" |
| 548 |
if m.has_key('help.' + self.name + '.' + str(icon)): |
if m.has_key('help.' + self.name + '.' + str(icon)): |
| 550 |
elif m.has_key('help.' + self.name): |
elif m.has_key('help.' + self.name): |
| 551 |
return m['help.' + self.name] |
return m['help.' + self.name] |
| 552 |
return None |
return None |
| 553 |
|
|
| 554 |
def key(self, icon, key): |
def key(self, icon, key): |
| 555 |
"Handle key presses" |
"Handle key presses" |
| 556 |
# Wimp_ProcessKey |
# Wimp_ProcessKey |
| 557 |
swi.swi(0x400dc, 'i', key) |
swi.swi(0x400dc, 'i', key) |
| 558 |
|
|
| 559 |
def icon_text(self, icon, text = None): |
def icon_text(self, icon, text = None): |
| 560 |
"Read or write an icon's text contents" |
"Read or write an icon's text contents" |
| 561 |
if icon >= self.icons: |
if icon >= self.icons: |
| 582 |
swi.swi(0x400d3, '.b', bb) |
swi.swi(0x400d3, '.b', bb) |
| 583 |
if bb[0] == self.handle and bb[1] == icon: |
if bb[0] == self.handle and bb[1] == icon: |
| 584 |
self.put_caret(icon) |
self.put_caret(icon) |
| 585 |
|
|
| 586 |
def icon_crement(self, icon, min, max, step = 1): |
def icon_crement(self, icon, min, max, step = 1): |
| 587 |
"Increment or decrement a numerical icon" |
"Increment or decrement a numerical icon" |
| 588 |
number = self.icon_text(icon) |
number = self.icon_text(icon) |
| 599 |
else: |
else: |
| 600 |
number = number + step |
number = number + step |
| 601 |
self.icon_text(0, str(number)) |
self.icon_text(0, str(number)) |
| 602 |
|
|
| 603 |
def icon_set(self, icon, state = None): |
def icon_set(self, icon, state = None): |
| 604 |
"Return the state or set the state of an icon" |
"Return the state or set the state of an icon" |
| 605 |
if icon >= self.icons: |
if icon >= self.icons: |
| 618 |
# Wimp_SetIconState |
# Wimp_SetIconState |
| 619 |
bb[2] = bb[3] = bb[6] |
bb[2] = bb[3] = bb[6] |
| 620 |
swi.swi(0x400cd, '.b', bb) |
swi.swi(0x400cd, '.b', bb) |
| 621 |
|
|
| 622 |
def put_caret(self, icon = -1, x = 0, y = 0, height = 0x2000000): |
def put_caret(self, icon = -1, x = 0, y = 0, height = 0x2000000): |
| 623 |
"Place the caret in the window" |
"Place the caret in the window" |
| 624 |
if icon == -1: |
if icon == -1: |
| 642 |
def icon_ungrey(self, icon): |
def icon_ungrey(self, icon): |
| 643 |
"Ungrey an icon" |
"Ungrey an icon" |
| 644 |
self.icon_grey(icon, 0) |
self.icon_grey(icon, 0) |
| 645 |
|
|
| 646 |
def add_content(self, content): |
def add_content(self, content): |
| 647 |
"Add a content class to the window" |
"Add a content class to the window" |
| 648 |
self.contents.append(content) |
self.contents.append(content) |
| 649 |
|
|
| 650 |
def remove_content(self, content): |
def remove_content(self, content): |
| 651 |
"Remove a content class from the window" |
"Remove a content class from the window" |
| 652 |
self.contents.remove(content) |
self.contents.remove(content) |
| 653 |
|
|
| 654 |
def refresh(self, x0, y0, x1, y1): |
def refresh(self, x0, y0, x1, y1): |
| 655 |
"Force a redraw of an area of the window" |
"Force a redraw of an area of the window" |
| 656 |
swi.swi('Wimp_ForceRedraw', 'iiiii', self.handle, x0, -y1, x1, -y0) |
swi.swi('Wimp_ForceRedraw', 'iiiii', self.handle, x0, -y1, x1, -y0) |
| 657 |
|
|
| 658 |
def update(self, x0, y0, x1, y1): |
def update(self, x0, y0, x1, y1): |
| 659 |
"Update an area of the window" |
"Update an area of the window" |
| 660 |
bb = swi.block(20) |
bb = swi.block(20) |
| 677 |
# Wimp_GetRectangle |
# Wimp_GetRectangle |
| 678 |
more = swi.swi(0x400ca, '.b;i', bb) |
more = swi.swi(0x400ca, '.b;i', bb) |
| 679 |
self.redraw_stop() |
self.redraw_stop() |
| 680 |
|
|
| 681 |
def title(self, title = None): |
def title(self, title = None): |
| 682 |
"Change or return the window title" |
"Change or return the window title" |
| 683 |
if title is None: |
if title is None: |
| 706 |
bb[3] = -y0 |
bb[3] = -y0 |
| 707 |
# Wimp_SetExtent |
# Wimp_SetExtent |
| 708 |
swi.swi(0x400d7, 'ib', self.handle, bb) |
swi.swi(0x400d7, 'ib', self.handle, bb) |
| 709 |
|
|
| 710 |
def origin(self): |
def origin(self): |
| 711 |
"Return the window origin position on the screen" |
"Return the window origin position on the screen" |
| 712 |
bb = swi.block(20) |
bb = swi.block(20) |
| 715 |
self.ox = b[1] - b[5] |
self.ox = b[1] - b[5] |
| 716 |
self.oy = b[4] - b[6] |
self.oy = b[4] - b[6] |
| 717 |
return self.ox, self.oy |
return self.ox, self.oy |
| 718 |
|
|
| 719 |
def icon_box(self, icon): |
def icon_box(self, icon): |
| 720 |
"Return the bbox of an icon relative to the window" |
"Return the bbox of an icon relative to the window" |
| 721 |
bb = swi.block(20) |
bb = swi.block(20) |
| 723 |
bb[1] = icon |
bb[1] = icon |
| 724 |
swi.swi('Wimp_GetIconState', '.b', bb) |
swi.swi('Wimp_GetIconState', '.b', bb) |
| 725 |
return bb[2], -bb[5], bb[4], -bb[3] |
return bb[2], -bb[5], bb[4], -bb[3] |
| 726 |
|
|
| 727 |
def icon_drag(self, icon): |
def icon_drag(self, icon): |
| 728 |
"Start to drag the specified icon" |
"Start to drag the specified icon" |
| 729 |
self.origin() |
self.origin() |
| 737 |
|
|
| 738 |
|
|
| 739 |
class iconbar_icon(window): |
class iconbar_icon(window): |
| 740 |
|
|
| 741 |
def __init__(self, sprite, handler, indirected = 0): |
def __init__(self, sprite, handler, indirected = 0): |
| 742 |
"Create an iconbar icon with the specified sprite" |
"Create an iconbar icon with the specified sprite" |
| 743 |
bb = swi.block(20) |
bb = swi.block(20) |
| 761 |
self.name = 'iconbar' |
self.name = 'iconbar' |
| 762 |
self.handler = handler |
self.handler = handler |
| 763 |
windows[-2] = self |
windows[-2] = self |
| 764 |
|
|
| 765 |
def click(self, icon, buttons, x, y, sx, sy): |
def click(self, icon, buttons, x, y, sx, sy): |
| 766 |
"Handle a click on the iconbar" |
"Handle a click on the iconbar" |
| 767 |
self.handler(buttons, sx) |
self.handler(buttons, sx) |
| 768 |
|
|
| 769 |
def change(self, sprite): |
def change(self, sprite): |
| 770 |
self.icon_text(self.icon, sprite) |
self.icon_text(self.icon, sprite) |
| 771 |
|
|
| 772 |
|
|
| 773 |
class info_window(window): |
class info_window(window): |
| 774 |
"Info box window including email/web buttons" |
"Info box window including email/web buttons" |
| 775 |
|
|
| 776 |
def __init__(self): |
def __init__(self): |
| 777 |
"Load the window template" |
"Load the window template" |
| 778 |
window.__init__(self, 'info') |
window.__init__(self, 'info') |
| 779 |
|
|
| 780 |
def click(self, icon, buttons, x, y, sx, sy): |
def click(self, icon, buttons, x, y, sx, sy): |
| 781 |
"Handle mouse clicks" |
"Handle mouse clicks" |
| 782 |
try: |
try: |
| 796 |
|
|
| 797 |
class save_window(window): |
class save_window(window): |
| 798 |
"Standard RISC OS save window" |
"Standard RISC OS save window" |
| 799 |
|
|
| 800 |
def __init__(self, handler, type): |
def __init__(self, handler, type): |
| 801 |
"Load the window template" |
"Load the window template" |
| 802 |
window.__init__(self, 'save') |
window.__init__(self, 'save') |
| 803 |
self.handler = handler |
self.handler = handler |
| 804 |
self.type = type |
self.type = type |
| 805 |
|
|
| 806 |
def click(self, icon, buttons = 0, x = 0, y = 0, sx = 0, sy = 0): |
def click(self, icon, buttons = 0, x = 0, y = 0, sx = 0, sy = 0): |
| 807 |
"Handle mouse clicks" |
"Handle mouse clicks" |
| 808 |
if icon == 0 and buttons > 15: |
if icon == 0 and buttons > 15: |
| 870 |
|
|
| 871 |
class menu: |
class menu: |
| 872 |
"Wimp menu class" |
"Wimp menu class" |
| 873 |
|
|
| 874 |
def __init__(self, name, items): |
def __init__(self, name, items): |
| 875 |
"Load menu" |
"Load menu" |
| 876 |
self.blocks = [] |
self.blocks = [] |
| 877 |
self.menu_block = self._load(items) |
self.menu_block = self._load(items) |
| 878 |
self.name = name |
self.name = name |
| 879 |
|
|
| 880 |
def menu(self, x, y): |
def menu(self, x, y): |
| 881 |
"Display menu and return the selection" |
"Display menu and return the selection" |
| 882 |
global current_menu |
global current_menu |
| 913 |
"Create a wimp menu structure (recursively)" |
"Create a wimp menu structure (recursively)" |
| 914 |
menu_block = swi.block(7 + 6 * (len(items) - 1)) |
menu_block = swi.block(7 + 6 * (len(items) - 1)) |
| 915 |
self.blocks.append(menu_block) |
self.blocks.append(menu_block) |
| 916 |
|
|
| 917 |
title_block = swi.block(len(lookup(items[0])) / 4 + 1) |
title_block = swi.block(len(lookup(items[0])) / 4 + 1) |
| 918 |
self.blocks.append(title_block) |
self.blocks.append(title_block) |
| 919 |
title_block.padstring(lookup(items[0]), '\0') |
title_block.padstring(lookup(items[0]), '\0') |
| 920 |
menu_block[0] = title_block.start |
menu_block[0] = title_block.start |
| 921 |
|
|
| 922 |
menu_block[1] = menu_block[2] = -1 |
menu_block[1] = menu_block[2] = -1 |
| 923 |
menu_block[3] = 0x00070207 |
menu_block[3] = 0x00070207 |
| 924 |
menu_block[4] = 300 |
menu_block[4] = 300 |
| 925 |
menu_block[5] = 44 |
menu_block[5] = 44 |
| 926 |
menu_block[6] = 0 |
menu_block[6] = 0 |
| 927 |
|
|
| 928 |
offset = 7 |
offset = 7 |
| 929 |
|
|
| 930 |
for item in items[1:]: |
for item in items[1:]: |
| 931 |
if isinstance(item, types.StringType): |
if isinstance(item, types.StringType): |
| 932 |
text = item |
text = item |
| 937 |
submenu = item[1].handle |
submenu = item[1].handle |
| 938 |
else: |
else: |
| 939 |
submenu = self._load(item[1]).start |
submenu = self._load(item[1]).start |
| 940 |
|
grey = False |
| 941 |
|
if text[0] == '-': |
| 942 |
|
grey = True |
| 943 |
|
text = text[1:] |
| 944 |
|
|
| 945 |
item_block = swi.block(len(lookup(text)) / 4 + 1) |
item_block = swi.block(len(lookup(text)) / 4 + 1) |
| 946 |
self.blocks.append(item_block) |
self.blocks.append(item_block) |
| 948 |
menu_block[offset] = (0x100 * (offset == 7)) |
menu_block[offset] = (0x100 * (offset == 7)) |
| 949 |
menu_block[offset + 1] = submenu |
menu_block[offset + 1] = submenu |
| 950 |
menu_block[offset + 2] = 0x07000111 |
menu_block[offset + 2] = 0x07000111 |
| 951 |
|
if grey: |
| 952 |
|
menu_block[offset + 2] |= 1 << 22 |
| 953 |
menu_block[offset + 3] = item_block.start |
menu_block[offset + 3] = item_block.start |
| 954 |
menu_block[offset + 4] = menu_block[offset + 5] = -1 |
menu_block[offset + 4] = menu_block[offset + 5] = -1 |
| 955 |
|
|
| 956 |
offset = offset + 6 |
offset = offset + 6 |
| 957 |
|
|
| 958 |
menu_block[offset - 6] = menu_block[offset - 6] | 0x80 |
menu_block[offset - 6] = menu_block[offset - 6] | 0x80 |
| 959 |
|
|
| 960 |
return menu_block |
return menu_block |
| 961 |
|
|
| 962 |
|
|
| 963 |
class content: |
class content: |
| 964 |
"Base window content class" |
"Base window content class" |
| 965 |
|
|
| 966 |
def __init__(self, w, x0, y0, x1, y1): |
def __init__(self, w, x0, y0, x1, y1): |
| 967 |
"Initialise a window content" |
"Initialise a window content" |
| 968 |
self.window = w |
self.window = w |
| 969 |
self.pos = [x0, y0, x1, y1] |
self.pos = [x0, y0, x1, y1] |
| 970 |
|
|
| 971 |
def redraw(self, ox, oy): |
def redraw(self, ox, oy): |
| 972 |
"Redraw content" |
"Redraw content" |
| 973 |
pass |
pass |
| 974 |
|
|
| 975 |
def click(self, buttons, x, y): |
def click(self, buttons, x, y): |
| 976 |
"Handle clicks in the content" |
"Handle clicks in the content" |
| 977 |
return 0 |
return 0 |
| 978 |
|
|
| 979 |
def refresh(self): |
def refresh(self): |
| 980 |
"Force redraw of the content" |
"Force redraw of the content" |
| 981 |
self.window.refresh(self.pos[0] - 4, self.pos[1] - 4, self.pos[2] + 4, self.pos[3] + 4) |
self.window.refresh(self.pos[0] - 4, self.pos[1] - 4, |
| 982 |
|
self.pos[2] + 4, self.pos[3] + 4) |
| 983 |
|
|
| 984 |
def update(self): |
def update(self): |
| 985 |
"Update the content" |
"Update the content" |
| 986 |
self.window.update(self.pos[0] - 4, self.pos[1] - 4, self.pos[2] + 4, self.pos[3] + 4) |
self.window.update(self.pos[0] - 4, self.pos[1] - 4, |
| 987 |
|
self.pos[2] + 4, self.pos[3] + 4) |
| 988 |
|
|
| 989 |
|
|
| 990 |
class rectangle(content): |
class rectangle(content): |
| 991 |
"Example window content" |
"Example window content" |
| 992 |
|
|
| 993 |
def __init__(self, w, x0, y0, x1, y1, fill = None, border = None): |
def __init__(self, w, x0, y0, x1, y1, fill = None, border = None, |
| 994 |
|
text = None, font = None, text_colour = 0x000000): |
| 995 |
content.__init__(self, w, x0, y0, x1, y1) |
content.__init__(self, w, x0, y0, x1, y1) |
| 996 |
self.fill = fill |
self.fill = fill |
| 997 |
self.border = border |
self.border = border |
| 998 |
|
self.text = text |
| 999 |
|
self.font = font |
| 1000 |
|
self.text_colour = text_colour |
| 1001 |
|
|
| 1002 |
def redraw(self, ox, oy): |
def redraw(self, ox, oy): |
| 1003 |
if not self.fill is None: |
if not self.fill is None: |
| 1004 |
# ColourTrans_SetGCOL |
# ColourTrans_SetGCOL |
| 1015 |
swi.swi(0x45, '5ii', ox + self.pos[2], oy - self.pos[1]) |
swi.swi(0x45, '5ii', ox + self.pos[2], oy - self.pos[1]) |
| 1016 |
swi.swi(0x45, '5ii', ox + self.pos[0], oy - self.pos[1]) |
swi.swi(0x45, '5ii', ox + self.pos[0], oy - self.pos[1]) |
| 1017 |
swi.swi(0x45, '5ii', ox + self.pos[0], oy - self.pos[3]) |
swi.swi(0x45, '5ii', ox + self.pos[0], oy - self.pos[3]) |
| 1018 |
|
if not self.text is None and not self.font is None: |
| 1019 |
|
self.font.colour(0xdddddd, self.text_colour) |
| 1020 |
|
self.font.paint(self.text, 400 * (ox + self.pos[0]), |
| 1021 |
|
400 * (oy - (self.pos[1] + self.pos[3] * 3) / 4)) |
| 1022 |
|
|
| 1023 |
def click(self, buttons, x, y): |
def click(self, buttons, x, y): |
| 1024 |
self.window.remove_content(self) |
#self.window.remove_content(self) |
| 1025 |
self.refresh() |
#self.refresh() |
| 1026 |
return 1 |
return 0 |
| 1027 |
|
|
| 1028 |
|
|
| 1029 |
|
class paragraph(content): |
| 1030 |
|
"A paragraph of text" |
| 1031 |
|
|
| 1032 |
|
def __init__(self, w, x0, y0, x1, font_family, font_style, font_size, |
| 1033 |
|
text, text_colour = 0x000000, fill = None, padding = 10): |
| 1034 |
|
self.font_family = font_family |
| 1035 |
|
self.font_style = font_style |
| 1036 |
|
self.font_size = font_size |
| 1037 |
|
self.line_height = int(font_size * 0.2) |
| 1038 |
|
self.text = [] |
| 1039 |
|
self.text_colour = text_colour |
| 1040 |
|
self.fill = fill |
| 1041 |
|
self.padding = padding |
| 1042 |
|
text = text.encode('utf8') |
| 1043 |
|
width = x1 - x0 - padding - padding |
| 1044 |
|
while text: |
| 1045 |
|
(c, x) = rufl.split(font_family, font_style, font_size, text, |
| 1046 |
|
width) |
| 1047 |
|
s = text[:c] |
| 1048 |
|
space = s.rfind(' ') |
| 1049 |
|
if c != len(text) and space != -1: |
| 1050 |
|
c = space |
| 1051 |
|
s = text[:c + 1] |
| 1052 |
|
text = text[c + 1:] |
| 1053 |
|
self.text.append(s) |
| 1054 |
|
y1 = y0 + len(self.text) * self.line_height + padding + padding |
| 1055 |
|
content.__init__(self, w, x0, y0, x1, y1) |
| 1056 |
|
self.y1 = y1 |
| 1057 |
|
|
| 1058 |
def font(name, size_x, size_y): |
def redraw(self, ox, oy): |
| 1059 |
|
if not self.fill is None: |
| 1060 |
|
swi.swi('ColourTrans_SetGCOL', 'i..i0', self.fill << 8, 1 << 8) |
| 1061 |
|
swi.swi('OS_Plot', '4ii', ox + self.pos[0], oy - self.pos[3]) |
| 1062 |
|
swi.swi('OS_Plot', 'iii', 96 + 5, ox + self.pos[2], |
| 1063 |
|
oy - self.pos[1]) |
| 1064 |
|
swi.swi('ColourTrans_SetFontColours', 'iiii', 0, |
| 1065 |
|
0xffffff << 8, self.text_colour << 8, 14) |
| 1066 |
|
y = oy - self.pos[1] - self.padding - self.line_height * 0.75 |
| 1067 |
|
for line in self.text: |
| 1068 |
|
rufl.paint(self.font_family, self.font_style, self.font_size, |
| 1069 |
|
line, ox + self.pos[0] + self.padding, y, rufl.blend) |
| 1070 |
|
y -= self.line_height |
| 1071 |
|
|
| 1072 |
|
|
| 1073 |
|
def font(name, size_x, size_y, no_encoding = False): |
| 1074 |
f = (name, size_x, size_y) |
f = (name, size_x, size_y) |
| 1075 |
if fonts.has_key(f): |
if fonts.has_key(f): |
| 1076 |
font = fonts[f] |
font = fonts[f] |
| 1077 |
font.use = font.use + 1 |
font.use = font.use + 1 |
| 1078 |
return font |
return font |
| 1079 |
else: |
else: |
| 1080 |
fonts[f] = _font(name, size_x, size_y) |
fonts[f] = _font(name, size_x, size_y, no_encoding) |
| 1081 |
return fonts[f] |
return fonts[f] |
| 1082 |
|
|
| 1083 |
class _font: |
class _font: |
| 1084 |
|
|
| 1085 |
def __init__(self, name, size_x, size_y): |
def __init__(self, name, size_x, size_y, no_encoding): |
| 1086 |
self.name = name |
self.name = name |
| 1087 |
self.size_x = size_x |
self.size_x = size_x |
| 1088 |
self.size_y = size_y |
self.size_y = size_y |
| 1089 |
self.use = 1 |
self.use = 1 |
| 1090 |
# Font_FindFont |
if no_encoding: |
| 1091 |
self.handle = swi.swi(0x40081, '.sii00;i', self.name, self.size_x, self.size_y) |
self.handle = swi.swi('XFont_FindFont', '.sii00;i', |
| 1092 |
|
self.name, self.size_x, self.size_y) |
| 1093 |
|
self.utf8 = False |
| 1094 |
|
else: |
| 1095 |
|
try: |
| 1096 |
|
self.handle = swi.swi('XFont_FindFont', '.sii00;i', |
| 1097 |
|
self.name + '\EUTF8', self.size_x, self.size_y) |
| 1098 |
|
self.utf8 = True |
| 1099 |
|
except swi.error: |
| 1100 |
|
self.handle = swi.swi('XFont_FindFont', '.sii00;i', |
| 1101 |
|
self.name + '\ELatin1', self.size_x, self.size_y) |
| 1102 |
|
self.utf8 = False |
| 1103 |
|
|
| 1104 |
def lose(self): |
def lose(self): |
| 1105 |
self.use = self.use - 1 |
self.use = self.use - 1 |
| 1106 |
if self.use == 0: |
if self.use == 0: |
| 1109 |
|
|
| 1110 |
def paint(self, string, x, y, spacex = 0, spacey = 0, offx = 0, offy = 0, |
def paint(self, string, x, y, spacex = 0, spacey = 0, offx = 0, offy = 0, |
| 1111 |
x0 = 0, y0 = 0, x1 = 0, y1 = 0): |
x0 = 0, y0 = 0, x1 = 0, y1 = 0): |
| 1112 |
# Font_Paint |
if self.utf8: |
| 1113 |
|
s = string.encode('utf8') |
| 1114 |
|
else: |
| 1115 |
|
s = string.encode('latin1', 'replace') |
| 1116 |
if x0: |
if x0: |
| 1117 |
bb = swi.block(20) |
bb = swi.block(20) |
| 1118 |
bb[0] = spacex |
bb[0] = spacex |
| 1123 |
bb[5] = int(y0) |
bb[5] = int(y0) |
| 1124 |
bb[6] = int(x1) |
bb[6] = int(x1) |
| 1125 |
bb[7] = int(y1) |
bb[7] = int(y1) |
| 1126 |
swi.swi(0x40086, 'isiiib', self.handle, string, 0x322, x, y, bb) |
swi.swi('Font_Paint', 'isiiib', self.handle, s, 0x322, x, y, bb) |
| 1127 |
else: |
else: |
| 1128 |
swi.swi(0x40086, 'isiii', self.handle, string, 0x300, x, y) |
swi.swi('Font_Paint', 'isiii', self.handle, s, 0x300, x, y) |
| 1129 |
|
|
| 1130 |
def charbbox(self, char): |
def charbbox(self, char): |
| 1131 |
return swi.swi('Font_CharBBox', 'ii0;.iiii', self.handle, ord(char)) |
return swi.swi('Font_CharBBox', 'ii0;.iiii', self.handle, ord(char)) |
| 1132 |
|
|
| 1133 |
def colour(self, back, fore): |
def colour(self, back, fore): |
| 1134 |
# ColourTrans_SetFontColours |
# ColourTrans_SetFontColours |
| 1135 |
swi.swi(0x4074f, 'iiii', self.handle, back << 8, fore << 8, 14) |
swi.swi(0x4074f, 'iiii', self.handle, back << 8, fore << 8, 14) |
| 1139 |
return (0, 0, 0, 0) |
return (0, 0, 0, 0) |
| 1140 |
if string[-1] == ' ': |
if string[-1] == ' ': |
| 1141 |
string = string + ' ' |
string = string + ' ' |
| 1142 |
|
if self.utf8: |
| 1143 |
|
s = string.encode('utf8') |
| 1144 |
|
else: |
| 1145 |
|
s = string.encode('latin1', 'replace') |
| 1146 |
bb = swi.block(20) |
bb = swi.block(20) |
| 1147 |
bb[0] = bb[1] = bb[2] = bb[3] = 0 |
bb[0] = bb[1] = bb[2] = bb[3] = 0 |
| 1148 |
bb[4] = -1 |
bb[4] = -1 |
| 1149 |
# Font_ScanString |
# Font_ScanString |
| 1150 |
swi.swi(0x400a1, 'isiiib', self.handle, string, 0x40320, 0x7fffffff, 0x7fffffff, bb) |
swi.swi(0x400a1, 'isiiib', self.handle, s, 0x40320, 0x7fffffff, |
| 1151 |
|
0x7fffffff, bb) |
| 1152 |
return bb[5], bb[6], bb[7], bb[8] |
return bb[5], bb[6], bb[7], bb[8] |
| 1153 |
|
|
| 1154 |
def caretpos(self, string, x, y): |
def caretpos(self, string, x, y): |
| 1155 |
bb = swi.block(len(string) / 4 + 10) |
if self.utf8: |
| 1156 |
bb.padstring(string, chr(0)) |
s = string.encode('utf8') |
| 1157 |
|
else: |
| 1158 |
|
s = string.encode('latin1', 'replace') |
| 1159 |
|
bb = swi.block(len(s) / 4 + 10) |
| 1160 |
|
bb.padstring(s, chr(0)) |
| 1161 |
# Font_ScanString |
# Font_ScanString |
| 1162 |
pos, cx, cy = swi.swi(0x400a1, 'ibiii;.i.ii', self.handle, bb, 0x20300, x, y) |
pos, cx, cy = swi.swi(0x400a1, 'ibiii;.i.ii', self.handle, bb, |
| 1163 |
|
0x20300, x, y) |
| 1164 |
return pos - bb.start, cx, cy |
return pos - bb.start, cx, cy |
| 1165 |
|
|
| 1166 |
def caretxy(self, string, pos): |
def caretxy(self, string, pos): |
| 1167 |
|
if self.utf8: |
| 1168 |
|
s = string.encode('utf8') |
| 1169 |
|
else: |
| 1170 |
|
s = string.encode('latin1', 'replace') |
| 1171 |
# Font_ScanString |
# Font_ScanString |
| 1172 |
return swi.swi(0x400a1, 'isiii..i;...ii', self.handle, string, 0x20380, 0x7fffffff, 0x7fffffff, pos) |
return swi.swi(0x400a1, 'isiii..i;...ii', self.handle, s, |
| 1173 |
|
0x20380, 0x7fffffff, 0x7fffffff, pos) |
| 1174 |
|
|
| 1175 |
def fontbox(self): |
def fontbox(self): |
| 1176 |
return swi.swi('Font_ReadInfo', 'i;.iiii', self.handle) |
return swi.swi('Font_ReadInfo', 'i;.iiii', self.handle) |
| 1177 |
|
|
| 1178 |
def split(self, string, x, y): |
def split(self, string, x, y): |
| 1179 |
bb = swi.block(len(string) / 4 + 10) |
if self.utf8: |
| 1180 |
bb.padstring(string, chr(0)) |
s = string.encode('utf8') |
| 1181 |
|
else: |
| 1182 |
|
s = string.encode('latin1', 'replace') |
| 1183 |
|
bb = swi.block(len(s) / 4 + 10) |
| 1184 |
|
bb.padstring(s, chr(0)) |
| 1185 |
# Font_ScanString |
# Font_ScanString |
| 1186 |
pos = swi.swi(0x400a1, 'ibiii;.i', self.handle, bb, 0x300, x, y) |
pos = swi.swi(0x400a1, 'ibiii;.i', self.handle, bb, 0x300, x, y) |
| 1187 |
return pos - bb.start |
return pos - bb.start |
| 1214 |
self.views.remove(view) |
self.views.remove(view) |
| 1215 |
for content in self.contents: |
for content in self.contents: |
| 1216 |
content.remove_view(view) |
content.remove_view(view) |
| 1217 |
|
|
| 1218 |
def rescaled(self, view): |
def rescaled(self, view): |
| 1219 |
for content in self.contents: |
for content in self.contents: |
| 1220 |
content.remove_view(view) |
content.remove_view(view) |
| 1234 |
view.gx1 * 400 / scale - content.pos[0], |
view.gx1 * 400 / scale - content.pos[0], |
| 1235 |
view.gy1 * 400 / scale - content.pos[1]] |
view.gy1 * 400 / scale - content.pos[1]] |
| 1236 |
content.redraw(view, view.ox, view.oy, scale) |
content.redraw(view, view.ox, view.oy, scale) |
| 1237 |
|
|
| 1238 |
def redraw_stop(self, view, scale): |
def redraw_stop(self, view, scale): |
| 1239 |
pass |
pass |
| 1240 |
|
|
| 1241 |
def redraw_margin(self, view): |
def redraw_margin(self, view): |
| 1242 |
set_colour(0x777777) |
set_colour(0x777777) |
| 1243 |
plot(4, view.ox + view.gx0, view.oy) |
plot(4, view.ox + view.gx0, view.oy) |
| 1253 |
for content in self.contents: |
for content in self.contents: |
| 1254 |
if content.pos[0] <= x <= content.pos[2] and content.pos[1] <= y <= content.pos[3]: |
if content.pos[0] <= x <= content.pos[2] and content.pos[1] <= y <= content.pos[3]: |
| 1255 |
content.click(view, buttons, x - content.pos[0], y - content.pos[1]) |
content.click(view, buttons, x - content.pos[0], y - content.pos[1]) |
| 1256 |
|
|
| 1257 |
def refresh(self, x0, y0, x1, y1): |
def refresh(self, x0, y0, x1, y1): |
| 1258 |
for view in self.views: |
for view in self.views: |
| 1259 |
view.update(int(view.scale * x0 / 400 - 4), int(view.scale * y0 / 400 - 4), |
view.update(int(view.scale * x0 / 400 - 4), int(view.scale * y0 / 400 - 4), |
| 1260 |
int(view.scale * x1 / 400 + 4), int(view.scale * y1 / 400 + 4)) |
int(view.scale * x1 / 400 + 4), int(view.scale * y1 / 400 + 4)) |
| 1261 |
|
|
| 1262 |
def add_font(self, name, size_x, size_y): |
def add_font(self, name, size_x, size_y): |
| 1263 |
f = (name, size_x, size_y) |
f = (name, size_x, size_y) |
| 1264 |
if self.fonts.has_key(f): |
if self.fonts.has_key(f): |
| 1277 |
|
|
| 1278 |
|
|
| 1279 |
class document_view(window): |
class document_view(window): |
| 1280 |
|
|
| 1281 |
def __init__(self, document, id, scale): |
def __init__(self, document, id, scale): |
| 1282 |
window.__init__(self, id) |
window.__init__(self, id) |
| 1283 |
self.document = document |
self.document = document |
| 1291 |
|
|
| 1292 |
def redraw(self): |
def redraw(self): |
| 1293 |
self.document.redraw(self, self.scale) |
self.document.redraw(self, self.scale) |
| 1294 |
|
|
| 1295 |
def redraw_stop(self): |
def redraw_stop(self): |
| 1296 |
self.document.redraw_stop(self, self.scale) |
self.document.redraw_stop(self, self.scale) |
| 1297 |
|
|
| 1319 |
|
|
| 1320 |
|
|
| 1321 |
class document_content: |
class document_content: |
| 1322 |
|
|
| 1323 |
def __init__(self, document, x0, y0, x1, y1): |
def __init__(self, document, x0, y0, x1, y1): |
| 1324 |
self.pos = [x0, y0, x1, y1] |
self.pos = [x0, y0, x1, y1] |
| 1325 |
self.colour = 0x0000ff |
self.colour = 0x0000ff |
| 1331 |
|
|
| 1332 |
def remove_view(self, view): |
def remove_view(self, view): |
| 1333 |
pass |
pass |
| 1334 |
|
|
| 1335 |
def redraw(self, view, x, y, scale): |
def redraw(self, view, x, y, scale): |
| 1336 |
set_colour(self.colour) |
set_colour(self.colour) |
| 1337 |
plot(4, x + self.scaled_pos[0], y - self.scaled_pos[1]) |
plot(4, x + self.scaled_pos[0], y - self.scaled_pos[1]) |
| 1342 |
plot(21, x + self.scaled_pos[2], y - self.scaled_pos[3]) |
plot(21, x + self.scaled_pos[2], y - self.scaled_pos[3]) |
| 1343 |
plot(4, x + self.scaled_pos[2], y - self.scaled_pos[1]) |
plot(4, x + self.scaled_pos[2], y - self.scaled_pos[1]) |
| 1344 |
plot(21, x + self.scaled_pos[0], y - self.scaled_pos[3]) |
plot(21, x + self.scaled_pos[0], y - self.scaled_pos[3]) |
| 1345 |
|
|
| 1346 |
def click(self, view, buttons, x, y): |
def click(self, view, buttons, x, y): |
| 1347 |
self.colour = 0xffffff - self.colour |
self.colour = 0xffffff - self.colour |
| 1348 |
self.document.refresh(self.pos[0], self.pos[1], self.pos[2], self.pos[3]) |
self.document.refresh(self.pos[0], self.pos[1], self.pos[2], self.pos[3]) |