| 60 |
import string |
import string |
| 61 |
import sys |
import sys |
| 62 |
import types |
import types |
| 63 |
|
import rufl |
| 64 |
|
|
| 65 |
task_handle = 0 |
task_handle = 0 |
| 66 |
task_name = 'Application' |
task_name = 'Application' |
| 76 |
def task(name, dir, task, messages = (), debug = 0, profile = 0, mask = None): |
def task(name, dir, task, messages = (), debug = 0, profile = 0, mask = None): |
| 77 |
"Initialise the task with the wimp and load the messages file" |
"Initialise the task with the wimp and load the messages file" |
| 78 |
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 |
| 79 |
|
|
| 80 |
hourglass_on() |
hourglass_on() |
| 81 |
task_name = name |
task_name = name |
| 82 |
directory = dir |
directory = dir |
| 114 |
print _depth * ' ', event + ':', arg |
print _depth * ' ', event + ':', arg |
| 115 |
return _trace |
return _trace |
| 116 |
sys.settrace(_trace) |
sys.settrace(_trace) |
| 117 |
|
|
| 118 |
# buffer for various stuff |
# buffer for various stuff |
| 119 |
b = swi.block(64) |
b = swi.block(64) |
| 120 |
|
|
| 121 |
# initialise with wimp |
# initialise with wimp |
| 122 |
# place the message list in the buffer |
# place the message list in the buffer |
| 123 |
b[0] = 0x502 # Message_HelpRequest |
b[0] = 0x502 # Message_HelpRequest |
| 159 |
except: |
except: |
| 160 |
if debug > 1: |
if debug > 1: |
| 161 |
sys.settrace(None) |
sys.settrace(None) |
| 162 |
|
|
| 163 |
type, value, traceb = sys.exc_info() |
type, value, traceb = sys.exc_info() |
| 164 |
swi.swi(0x400df, 'sis', |
swi.swi(0x400df, 'sis', |
| 165 |
' Internal error - must exit (' + str(type) + ': ' + str(value) + ')', |
' Internal error - must exit (' + str(type) + ': ' + str(value) + ')', |
| 166 |
1, task_name) |
1, task_name) |
| 167 |
|
|
| 168 |
if debug: |
if debug: |
| 169 |
traceback.print_exc() |
traceback.print_exc() |
| 170 |
trace = traceback.extract_tb(traceb) |
trace = traceback.extract_tb(traceb) |
| 176 |
swi.swi(0x42589, '') |
swi.swi(0x42589, '') |
| 177 |
except: |
except: |
| 178 |
pass |
pass |
| 179 |
|
|
| 180 |
if sys.stderr.tell() > 0: |
if sys.stderr.tell() > 0: |
| 181 |
sys.stderr.close() |
sys.stderr.close() |
| 182 |
os.system('Filer_Run ' + directory + '.^.Output') |
os.system('Filer_Run ' + directory + '.^.Output') |
| 183 |
else: |
else: |
| 184 |
sys.stderr.close() |
sys.stderr.close() |
| 185 |
|
|
| 186 |
# Wimp_CloseDown |
# Wimp_CloseDown |
| 187 |
swi.swi(0x400dd, 'ii', task_handle, 0x4b534154) |
swi.swi(0x400dd, 'ii', task_handle, 0x4b534154) |
| 188 |
hourglass_off() |
hourglass_off() |
| 189 |
|
|
| 190 |
|
|
| 191 |
def _go(): |
def _go(): |
| 192 |
"Run the program" |
"Run the program" |
| 193 |
task_module.init() |
task_module.init() |
| 194 |
hourglass_off() |
hourglass_off() |
| 195 |
while 1: |
while 1: |
| 196 |
handle(poll(poll_mask)) |
handle(poll(poll_mask)) |
| 197 |
|
|
| 198 |
|
|
| 199 |
def quit(): |
def quit(): |
| 200 |
"Trigger quitting" |
"Trigger quitting" |
| 201 |
for f in fonts.values(): |
for f in fonts.values(): |
| 489 |
(screen_width + width) / 2, (screen_height + height) / 2, |
(screen_width + width) / 2, (screen_height + height) / 2, |
| 490 |
bb[5], bb[6], behind) |
bb[5], bb[6], behind) |
| 491 |
|
|
| 492 |
|
def open_full_size(self, behind = None): |
| 493 |
|
bb = swi.block(20) |
| 494 |
|
bb[0] = self.handle |
| 495 |
|
swi.swi('Wimp_GetWindowState', '.b', bb) |
| 496 |
|
bb[3] = 10000 |
| 497 |
|
bb[2] = -10000 |
| 498 |
|
if behind: |
| 499 |
|
bb[7] = behind |
| 500 |
|
swi.swi('Wimp_OpenWindow', '.b', bb) |
| 501 |
|
self.is_open = 1 |
| 502 |
|
|
| 503 |
def open_as_menu(self): |
def open_as_menu(self): |
| 504 |
"Open the window as a menu centred on the pointer" |
"Open the window as a menu centred on the pointer" |
| 505 |
bb = swi.block(20) |
bb = swi.block(20) |
| 520 |
# Wimp_CloseWindow |
# Wimp_CloseWindow |
| 521 |
swi.swi(0x400c6, '.b', bb) |
swi.swi(0x400c6, '.b', bb) |
| 522 |
self.is_open = 0 |
self.is_open = 0 |
| 523 |
|
|
| 524 |
def entering(self): |
def entering(self): |
| 525 |
"Pointer entering window" |
"Pointer entering window" |
| 526 |
|
|
| 527 |
def leaving(self): |
def leaving(self): |
| 528 |
"Pointer leaving window" |
"Pointer leaving window" |
| 529 |
|
|
| 530 |
def click(self, icon, buttons, x, y, sx, sy): |
def click(self, icon, buttons, x, y, sx, sy): |
| 531 |
"Mouse click in window" |
"Mouse click in window" |
| 532 |
self.contents.reverse() |
self.contents.reverse() |
| 538 |
return 1 |
return 1 |
| 539 |
self.contents.reverse() |
self.contents.reverse() |
| 540 |
return 0 |
return 0 |
| 541 |
|
|
| 542 |
def help(self, icon): |
def help(self, icon): |
| 543 |
"Return interactive help message" |
"Return interactive help message" |
| 544 |
if m.has_key('help.' + self.name + '.' + str(icon)): |
if m.has_key('help.' + self.name + '.' + str(icon)): |
| 546 |
elif m.has_key('help.' + self.name): |
elif m.has_key('help.' + self.name): |
| 547 |
return m['help.' + self.name] |
return m['help.' + self.name] |
| 548 |
return None |
return None |
| 549 |
|
|
| 550 |
def key(self, icon, key): |
def key(self, icon, key): |
| 551 |
"Handle key presses" |
"Handle key presses" |
| 552 |
# Wimp_ProcessKey |
# Wimp_ProcessKey |
| 553 |
swi.swi(0x400dc, 'i', key) |
swi.swi(0x400dc, 'i', key) |
| 554 |
|
|
| 555 |
def icon_text(self, icon, text = None): |
def icon_text(self, icon, text = None): |
| 556 |
"Read or write an icon's text contents" |
"Read or write an icon's text contents" |
| 557 |
if icon >= self.icons: |
if icon >= self.icons: |
| 578 |
swi.swi(0x400d3, '.b', bb) |
swi.swi(0x400d3, '.b', bb) |
| 579 |
if bb[0] == self.handle and bb[1] == icon: |
if bb[0] == self.handle and bb[1] == icon: |
| 580 |
self.put_caret(icon) |
self.put_caret(icon) |
| 581 |
|
|
| 582 |
def icon_crement(self, icon, min, max, step = 1): |
def icon_crement(self, icon, min, max, step = 1): |
| 583 |
"Increment or decrement a numerical icon" |
"Increment or decrement a numerical icon" |
| 584 |
number = self.icon_text(icon) |
number = self.icon_text(icon) |
| 595 |
else: |
else: |
| 596 |
number = number + step |
number = number + step |
| 597 |
self.icon_text(0, str(number)) |
self.icon_text(0, str(number)) |
| 598 |
|
|
| 599 |
def icon_set(self, icon, state = None): |
def icon_set(self, icon, state = None): |
| 600 |
"Return the state or set the state of an icon" |
"Return the state or set the state of an icon" |
| 601 |
if icon >= self.icons: |
if icon >= self.icons: |
| 614 |
# Wimp_SetIconState |
# Wimp_SetIconState |
| 615 |
bb[2] = bb[3] = bb[6] |
bb[2] = bb[3] = bb[6] |
| 616 |
swi.swi(0x400cd, '.b', bb) |
swi.swi(0x400cd, '.b', bb) |
| 617 |
|
|
| 618 |
def put_caret(self, icon = -1, x = 0, y = 0, height = 0x2000000): |
def put_caret(self, icon = -1, x = 0, y = 0, height = 0x2000000): |
| 619 |
"Place the caret in the window" |
"Place the caret in the window" |
| 620 |
if icon == -1: |
if icon == -1: |
| 638 |
def icon_ungrey(self, icon): |
def icon_ungrey(self, icon): |
| 639 |
"Ungrey an icon" |
"Ungrey an icon" |
| 640 |
self.icon_grey(icon, 0) |
self.icon_grey(icon, 0) |
| 641 |
|
|
| 642 |
def add_content(self, content): |
def add_content(self, content): |
| 643 |
"Add a content class to the window" |
"Add a content class to the window" |
| 644 |
self.contents.append(content) |
self.contents.append(content) |
| 645 |
|
|
| 646 |
def remove_content(self, content): |
def remove_content(self, content): |
| 647 |
"Remove a content class from the window" |
"Remove a content class from the window" |
| 648 |
self.contents.remove(content) |
self.contents.remove(content) |
| 649 |
|
|
| 650 |
def refresh(self, x0, y0, x1, y1): |
def refresh(self, x0, y0, x1, y1): |
| 651 |
"Force a redraw of an area of the window" |
"Force a redraw of an area of the window" |
| 652 |
swi.swi('Wimp_ForceRedraw', 'iiiii', self.handle, x0, -y1, x1, -y0) |
swi.swi('Wimp_ForceRedraw', 'iiiii', self.handle, x0, -y1, x1, -y0) |
| 653 |
|
|
| 654 |
def update(self, x0, y0, x1, y1): |
def update(self, x0, y0, x1, y1): |
| 655 |
"Update an area of the window" |
"Update an area of the window" |
| 656 |
bb = swi.block(20) |
bb = swi.block(20) |
| 673 |
# Wimp_GetRectangle |
# Wimp_GetRectangle |
| 674 |
more = swi.swi(0x400ca, '.b;i', bb) |
more = swi.swi(0x400ca, '.b;i', bb) |
| 675 |
self.redraw_stop() |
self.redraw_stop() |
| 676 |
|
|
| 677 |
def title(self, title = None): |
def title(self, title = None): |
| 678 |
"Change or return the window title" |
"Change or return the window title" |
| 679 |
if title is None: |
if title is None: |
| 702 |
bb[3] = -y0 |
bb[3] = -y0 |
| 703 |
# Wimp_SetExtent |
# Wimp_SetExtent |
| 704 |
swi.swi(0x400d7, 'ib', self.handle, bb) |
swi.swi(0x400d7, 'ib', self.handle, bb) |
| 705 |
|
|
| 706 |
def origin(self): |
def origin(self): |
| 707 |
"Return the window origin position on the screen" |
"Return the window origin position on the screen" |
| 708 |
bb = swi.block(20) |
bb = swi.block(20) |
| 711 |
self.ox = b[1] - b[5] |
self.ox = b[1] - b[5] |
| 712 |
self.oy = b[4] - b[6] |
self.oy = b[4] - b[6] |
| 713 |
return self.ox, self.oy |
return self.ox, self.oy |
| 714 |
|
|
| 715 |
def icon_box(self, icon): |
def icon_box(self, icon): |
| 716 |
"Return the bbox of an icon relative to the window" |
"Return the bbox of an icon relative to the window" |
| 717 |
bb = swi.block(20) |
bb = swi.block(20) |
| 719 |
bb[1] = icon |
bb[1] = icon |
| 720 |
swi.swi('Wimp_GetIconState', '.b', bb) |
swi.swi('Wimp_GetIconState', '.b', bb) |
| 721 |
return bb[2], -bb[5], bb[4], -bb[3] |
return bb[2], -bb[5], bb[4], -bb[3] |
| 722 |
|
|
| 723 |
def icon_drag(self, icon): |
def icon_drag(self, icon): |
| 724 |
"Start to drag the specified icon" |
"Start to drag the specified icon" |
| 725 |
self.origin() |
self.origin() |
| 733 |
|
|
| 734 |
|
|
| 735 |
class iconbar_icon(window): |
class iconbar_icon(window): |
| 736 |
|
|
| 737 |
def __init__(self, sprite, handler, indirected = 0): |
def __init__(self, sprite, handler, indirected = 0): |
| 738 |
"Create an iconbar icon with the specified sprite" |
"Create an iconbar icon with the specified sprite" |
| 739 |
bb = swi.block(20) |
bb = swi.block(20) |
| 757 |
self.name = 'iconbar' |
self.name = 'iconbar' |
| 758 |
self.handler = handler |
self.handler = handler |
| 759 |
windows[-2] = self |
windows[-2] = self |
| 760 |
|
|
| 761 |
def click(self, icon, buttons, x, y, sx, sy): |
def click(self, icon, buttons, x, y, sx, sy): |
| 762 |
"Handle a click on the iconbar" |
"Handle a click on the iconbar" |
| 763 |
self.handler(buttons, sx) |
self.handler(buttons, sx) |
| 764 |
|
|
| 765 |
def change(self, sprite): |
def change(self, sprite): |
| 766 |
self.icon_text(self.icon, sprite) |
self.icon_text(self.icon, sprite) |
| 767 |
|
|
| 768 |
|
|
| 769 |
class info_window(window): |
class info_window(window): |
| 770 |
"Info box window including email/web buttons" |
"Info box window including email/web buttons" |
| 771 |
|
|
| 772 |
def __init__(self): |
def __init__(self): |
| 773 |
"Load the window template" |
"Load the window template" |
| 774 |
window.__init__(self, 'info') |
window.__init__(self, 'info') |
| 775 |
|
|
| 776 |
def click(self, icon, buttons, x, y, sx, sy): |
def click(self, icon, buttons, x, y, sx, sy): |
| 777 |
"Handle mouse clicks" |
"Handle mouse clicks" |
| 778 |
try: |
try: |
| 792 |
|
|
| 793 |
class save_window(window): |
class save_window(window): |
| 794 |
"Standard RISC OS save window" |
"Standard RISC OS save window" |
| 795 |
|
|
| 796 |
def __init__(self, handler, type): |
def __init__(self, handler, type): |
| 797 |
"Load the window template" |
"Load the window template" |
| 798 |
window.__init__(self, 'save') |
window.__init__(self, 'save') |
| 799 |
self.handler = handler |
self.handler = handler |
| 800 |
self.type = type |
self.type = type |
| 801 |
|
|
| 802 |
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): |
| 803 |
"Handle mouse clicks" |
"Handle mouse clicks" |
| 804 |
if icon == 0 and buttons > 15: |
if icon == 0 and buttons > 15: |
| 866 |
|
|
| 867 |
class menu: |
class menu: |
| 868 |
"Wimp menu class" |
"Wimp menu class" |
| 869 |
|
|
| 870 |
def __init__(self, name, items): |
def __init__(self, name, items): |
| 871 |
"Load menu" |
"Load menu" |
| 872 |
self.blocks = [] |
self.blocks = [] |
| 873 |
self.menu_block = self._load(items) |
self.menu_block = self._load(items) |
| 874 |
self.name = name |
self.name = name |
| 875 |
|
|
| 876 |
def menu(self, x, y): |
def menu(self, x, y): |
| 877 |
"Display menu and return the selection" |
"Display menu and return the selection" |
| 878 |
global current_menu |
global current_menu |
| 909 |
"Create a wimp menu structure (recursively)" |
"Create a wimp menu structure (recursively)" |
| 910 |
menu_block = swi.block(7 + 6 * (len(items) - 1)) |
menu_block = swi.block(7 + 6 * (len(items) - 1)) |
| 911 |
self.blocks.append(menu_block) |
self.blocks.append(menu_block) |
| 912 |
|
|
| 913 |
title_block = swi.block(len(lookup(items[0])) / 4 + 1) |
title_block = swi.block(len(lookup(items[0])) / 4 + 1) |
| 914 |
self.blocks.append(title_block) |
self.blocks.append(title_block) |
| 915 |
title_block.padstring(lookup(items[0]), '\0') |
title_block.padstring(lookup(items[0]), '\0') |
| 916 |
menu_block[0] = title_block.start |
menu_block[0] = title_block.start |
| 917 |
|
|
| 918 |
menu_block[1] = menu_block[2] = -1 |
menu_block[1] = menu_block[2] = -1 |
| 919 |
menu_block[3] = 0x00070207 |
menu_block[3] = 0x00070207 |
| 920 |
menu_block[4] = 300 |
menu_block[4] = 300 |
| 921 |
menu_block[5] = 44 |
menu_block[5] = 44 |
| 922 |
menu_block[6] = 0 |
menu_block[6] = 0 |
| 923 |
|
|
| 924 |
offset = 7 |
offset = 7 |
| 925 |
|
|
| 926 |
for item in items[1:]: |
for item in items[1:]: |
| 927 |
if isinstance(item, types.StringType): |
if isinstance(item, types.StringType): |
| 928 |
text = item |
text = item |
| 933 |
submenu = item[1].handle |
submenu = item[1].handle |
| 934 |
else: |
else: |
| 935 |
submenu = self._load(item[1]).start |
submenu = self._load(item[1]).start |
| 936 |
|
grey = False |
| 937 |
|
if text[0] == '-': |
| 938 |
|
grey = True |
| 939 |
|
text = text[1:] |
| 940 |
|
|
| 941 |
item_block = swi.block(len(lookup(text)) / 4 + 1) |
item_block = swi.block(len(lookup(text)) / 4 + 1) |
| 942 |
self.blocks.append(item_block) |
self.blocks.append(item_block) |
| 944 |
menu_block[offset] = (0x100 * (offset == 7)) |
menu_block[offset] = (0x100 * (offset == 7)) |
| 945 |
menu_block[offset + 1] = submenu |
menu_block[offset + 1] = submenu |
| 946 |
menu_block[offset + 2] = 0x07000111 |
menu_block[offset + 2] = 0x07000111 |
| 947 |
|
if grey: |
| 948 |
|
menu_block[offset + 2] |= 1 << 22 |
| 949 |
menu_block[offset + 3] = item_block.start |
menu_block[offset + 3] = item_block.start |
| 950 |
menu_block[offset + 4] = menu_block[offset + 5] = -1 |
menu_block[offset + 4] = menu_block[offset + 5] = -1 |
| 951 |
|
|
| 952 |
offset = offset + 6 |
offset = offset + 6 |
| 953 |
|
|
| 954 |
menu_block[offset - 6] = menu_block[offset - 6] | 0x80 |
menu_block[offset - 6] = menu_block[offset - 6] | 0x80 |
| 955 |
|
|
| 956 |
return menu_block |
return menu_block |
| 957 |
|
|
| 958 |
|
|
| 959 |
class content: |
class content: |
| 960 |
"Base window content class" |
"Base window content class" |
| 961 |
|
|
| 962 |
def __init__(self, w, x0, y0, x1, y1): |
def __init__(self, w, x0, y0, x1, y1): |
| 963 |
"Initialise a window content" |
"Initialise a window content" |
| 964 |
self.window = w |
self.window = w |
| 965 |
self.pos = [x0, y0, x1, y1] |
self.pos = [x0, y0, x1, y1] |
| 966 |
|
|
| 967 |
def redraw(self, ox, oy): |
def redraw(self, ox, oy): |
| 968 |
"Redraw content" |
"Redraw content" |
| 969 |
pass |
pass |
| 970 |
|
|
| 971 |
def click(self, buttons, x, y): |
def click(self, buttons, x, y): |
| 972 |
"Handle clicks in the content" |
"Handle clicks in the content" |
| 973 |
return 0 |
return 0 |
| 974 |
|
|
| 975 |
def refresh(self): |
def refresh(self): |
| 976 |
"Force redraw of the content" |
"Force redraw of the content" |
| 977 |
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, |
| 978 |
|
self.pos[2] + 4, self.pos[3] + 4) |
| 979 |
|
|
| 980 |
def update(self): |
def update(self): |
| 981 |
"Update the content" |
"Update the content" |
| 982 |
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, |
| 983 |
|
self.pos[2] + 4, self.pos[3] + 4) |
| 984 |
|
|
| 985 |
|
|
| 986 |
class rectangle(content): |
class rectangle(content): |
| 987 |
"Example window content" |
"Example window content" |
| 988 |
|
|
| 989 |
def __init__(self, w, x0, y0, x1, y1, fill = None, border = None): |
def __init__(self, w, x0, y0, x1, y1, fill = None, border = None, |
| 990 |
|
text = None, font = None, text_colour = 0x000000): |
| 991 |
content.__init__(self, w, x0, y0, x1, y1) |
content.__init__(self, w, x0, y0, x1, y1) |
| 992 |
self.fill = fill |
self.fill = fill |
| 993 |
self.border = border |
self.border = border |
| 994 |
|
self.text = text |
| 995 |
|
self.font = font |
| 996 |
|
self.text_colour = text_colour |
| 997 |
|
|
| 998 |
def redraw(self, ox, oy): |
def redraw(self, ox, oy): |
| 999 |
if not self.fill is None: |
if not self.fill is None: |
| 1000 |
# ColourTrans_SetGCOL |
# ColourTrans_SetGCOL |
| 1011 |
swi.swi(0x45, '5ii', ox + self.pos[2], oy - self.pos[1]) |
swi.swi(0x45, '5ii', ox + self.pos[2], oy - self.pos[1]) |
| 1012 |
swi.swi(0x45, '5ii', ox + self.pos[0], oy - self.pos[1]) |
swi.swi(0x45, '5ii', ox + self.pos[0], oy - self.pos[1]) |
| 1013 |
swi.swi(0x45, '5ii', ox + self.pos[0], oy - self.pos[3]) |
swi.swi(0x45, '5ii', ox + self.pos[0], oy - self.pos[3]) |
| 1014 |
|
if not self.text is None and not self.font is None: |
| 1015 |
|
self.font.colour(0xdddddd, self.text_colour) |
| 1016 |
|
self.font.paint(self.text, 400 * (ox + self.pos[0]), |
| 1017 |
|
400 * (oy - (self.pos[1] + self.pos[3] * 3) / 4)) |
| 1018 |
|
|
| 1019 |
def click(self, buttons, x, y): |
def click(self, buttons, x, y): |
| 1020 |
self.window.remove_content(self) |
#self.window.remove_content(self) |
| 1021 |
self.refresh() |
#self.refresh() |
| 1022 |
return 1 |
return 0 |
| 1023 |
|
|
| 1024 |
|
|
| 1025 |
|
class paragraph(content): |
| 1026 |
|
"A paragraph of text" |
| 1027 |
|
|
| 1028 |
|
def __init__(self, w, x0, y0, x1, font_family, font_style, font_size, |
| 1029 |
|
text, text_colour = 0x000000, fill = None, padding = 10): |
| 1030 |
|
self.font_family = font_family |
| 1031 |
|
self.font_style = font_style |
| 1032 |
|
self.font_size = font_size |
| 1033 |
|
self.line_height = int(font_size * 0.2) |
| 1034 |
|
self.text = [] |
| 1035 |
|
self.text_colour = text_colour |
| 1036 |
|
self.fill = fill |
| 1037 |
|
self.padding = padding |
| 1038 |
|
text = text.encode('utf8') |
| 1039 |
|
width = x1 - x0 - padding - padding |
| 1040 |
|
while text: |
| 1041 |
|
(c, x) = rufl.split(font_family, font_style, font_size, text, |
| 1042 |
|
width) |
| 1043 |
|
s = text[:c] |
| 1044 |
|
space = s.rfind(' ') |
| 1045 |
|
if c != len(text) and space != -1: |
| 1046 |
|
c = space |
| 1047 |
|
s = text[:c + 1] |
| 1048 |
|
text = text[c + 1:] |
| 1049 |
|
self.text.append(s) |
| 1050 |
|
y1 = y0 + len(self.text) * self.line_height + padding + padding |
| 1051 |
|
content.__init__(self, w, x0, y0, x1, y1) |
| 1052 |
|
self.y1 = y1 |
| 1053 |
|
|
| 1054 |
def font(name, size_x, size_y): |
def redraw(self, ox, oy): |
| 1055 |
|
if not self.fill is None: |
| 1056 |
|
swi.swi('ColourTrans_SetGCOL', 'i..i0', self.fill << 8, 1 << 8) |
| 1057 |
|
swi.swi('OS_Plot', '4ii', ox + self.pos[0], oy - self.pos[3]) |
| 1058 |
|
swi.swi('OS_Plot', 'iii', 96 + 5, ox + self.pos[2], |
| 1059 |
|
oy - self.pos[1]) |
| 1060 |
|
swi.swi('ColourTrans_SetFontColours', 'iiii', 0, |
| 1061 |
|
0xffffff << 8, self.text_colour << 8, 14) |
| 1062 |
|
y = oy - self.pos[1] - self.padding - self.line_height * 0.75 |
| 1063 |
|
for line in self.text: |
| 1064 |
|
rufl.paint(self.font_family, self.font_style, self.font_size, |
| 1065 |
|
line, ox + self.pos[0] + self.padding, y, rufl.blend) |
| 1066 |
|
y -= self.line_height |
| 1067 |
|
|
| 1068 |
|
|
| 1069 |
|
def font(name, size_x, size_y, no_encoding = False): |
| 1070 |
f = (name, size_x, size_y) |
f = (name, size_x, size_y) |
| 1071 |
if fonts.has_key(f): |
if fonts.has_key(f): |
| 1072 |
font = fonts[f] |
font = fonts[f] |
| 1073 |
font.use = font.use + 1 |
font.use = font.use + 1 |
| 1074 |
return font |
return font |
| 1075 |
else: |
else: |
| 1076 |
fonts[f] = _font(name, size_x, size_y) |
fonts[f] = _font(name, size_x, size_y, no_encoding) |
| 1077 |
return fonts[f] |
return fonts[f] |
| 1078 |
|
|
| 1079 |
class _font: |
class _font: |
| 1080 |
|
|
| 1081 |
def __init__(self, name, size_x, size_y): |
def __init__(self, name, size_x, size_y, no_encoding): |
| 1082 |
self.name = name |
self.name = name |
| 1083 |
self.size_x = size_x |
self.size_x = size_x |
| 1084 |
self.size_y = size_y |
self.size_y = size_y |
| 1085 |
self.use = 1 |
self.use = 1 |
| 1086 |
# Font_FindFont |
if no_encoding: |
| 1087 |
self.handle = swi.swi(0x40081, '.sii00;i', self.name, self.size_x, self.size_y) |
self.handle = swi.swi('XFont_FindFont', '.sii00;i', |
| 1088 |
|
self.name, self.size_x, self.size_y) |
| 1089 |
|
self.utf8 = False |
| 1090 |
|
else: |
| 1091 |
|
try: |
| 1092 |
|
self.handle = swi.swi('XFont_FindFont', '.sii00;i', |
| 1093 |
|
self.name + '\EUTF8', self.size_x, self.size_y) |
| 1094 |
|
self.utf8 = True |
| 1095 |
|
except swi.error: |
| 1096 |
|
self.handle = swi.swi('XFont_FindFont', '.sii00;i', |
| 1097 |
|
self.name + '\ELatin1', self.size_x, self.size_y) |
| 1098 |
|
self.utf8 = False |
| 1099 |
|
|
| 1100 |
def lose(self): |
def lose(self): |
| 1101 |
self.use = self.use - 1 |
self.use = self.use - 1 |
| 1102 |
if self.use == 0: |
if self.use == 0: |
| 1105 |
|
|
| 1106 |
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, |
| 1107 |
x0 = 0, y0 = 0, x1 = 0, y1 = 0): |
x0 = 0, y0 = 0, x1 = 0, y1 = 0): |
| 1108 |
# Font_Paint |
if self.utf8: |
| 1109 |
|
s = string.encode('utf8') |
| 1110 |
|
else: |
| 1111 |
|
s = string.encode('latin1', 'replace') |
| 1112 |
if x0: |
if x0: |
| 1113 |
bb = swi.block(20) |
bb = swi.block(20) |
| 1114 |
bb[0] = spacex |
bb[0] = spacex |
| 1119 |
bb[5] = int(y0) |
bb[5] = int(y0) |
| 1120 |
bb[6] = int(x1) |
bb[6] = int(x1) |
| 1121 |
bb[7] = int(y1) |
bb[7] = int(y1) |
| 1122 |
swi.swi(0x40086, 'isiiib', self.handle, string, 0x322, x, y, bb) |
swi.swi('Font_Paint', 'isiiib', self.handle, s, 0x322, x, y, bb) |
| 1123 |
else: |
else: |
| 1124 |
swi.swi(0x40086, 'isiii', self.handle, string, 0x300, x, y) |
swi.swi('Font_Paint', 'isiii', self.handle, s, 0x300, x, y) |
| 1125 |
|
|
| 1126 |
def charbbox(self, char): |
def charbbox(self, char): |
| 1127 |
return swi.swi('Font_CharBBox', 'ii0;.iiii', self.handle, ord(char)) |
return swi.swi('Font_CharBBox', 'ii0;.iiii', self.handle, ord(char)) |
| 1128 |
|
|
| 1129 |
def colour(self, back, fore): |
def colour(self, back, fore): |
| 1130 |
# ColourTrans_SetFontColours |
# ColourTrans_SetFontColours |
| 1131 |
swi.swi(0x4074f, 'iiii', self.handle, back << 8, fore << 8, 14) |
swi.swi(0x4074f, 'iiii', self.handle, back << 8, fore << 8, 14) |
| 1135 |
return (0, 0, 0, 0) |
return (0, 0, 0, 0) |
| 1136 |
if string[-1] == ' ': |
if string[-1] == ' ': |
| 1137 |
string = string + ' ' |
string = string + ' ' |
| 1138 |
|
if self.utf8: |
| 1139 |
|
s = string.encode('utf8') |
| 1140 |
|
else: |
| 1141 |
|
s = string.encode('latin1', 'replace') |
| 1142 |
bb = swi.block(20) |
bb = swi.block(20) |
| 1143 |
bb[0] = bb[1] = bb[2] = bb[3] = 0 |
bb[0] = bb[1] = bb[2] = bb[3] = 0 |
| 1144 |
bb[4] = -1 |
bb[4] = -1 |
| 1145 |
# Font_ScanString |
# Font_ScanString |
| 1146 |
swi.swi(0x400a1, 'isiiib', self.handle, string, 0x40320, 0x7fffffff, 0x7fffffff, bb) |
swi.swi(0x400a1, 'isiiib', self.handle, s, 0x40320, 0x7fffffff, |
| 1147 |
|
0x7fffffff, bb) |
| 1148 |
return bb[5], bb[6], bb[7], bb[8] |
return bb[5], bb[6], bb[7], bb[8] |
| 1149 |
|
|
| 1150 |
def caretpos(self, string, x, y): |
def caretpos(self, string, x, y): |
| 1151 |
bb = swi.block(len(string) / 4 + 10) |
if self.utf8: |
| 1152 |
bb.padstring(string, chr(0)) |
s = string.encode('utf8') |
| 1153 |
|
else: |
| 1154 |
|
s = string.encode('latin1', 'replace') |
| 1155 |
|
bb = swi.block(len(s) / 4 + 10) |
| 1156 |
|
bb.padstring(s, chr(0)) |
| 1157 |
# Font_ScanString |
# Font_ScanString |
| 1158 |
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, |
| 1159 |
|
0x20300, x, y) |
| 1160 |
return pos - bb.start, cx, cy |
return pos - bb.start, cx, cy |
| 1161 |
|
|
| 1162 |
def caretxy(self, string, pos): |
def caretxy(self, string, pos): |
| 1163 |
|
if self.utf8: |
| 1164 |
|
s = string.encode('utf8') |
| 1165 |
|
else: |
| 1166 |
|
s = string.encode('latin1', 'replace') |
| 1167 |
# Font_ScanString |
# Font_ScanString |
| 1168 |
return swi.swi(0x400a1, 'isiii..i;...ii', self.handle, string, 0x20380, 0x7fffffff, 0x7fffffff, pos) |
return swi.swi(0x400a1, 'isiii..i;...ii', self.handle, s, |
| 1169 |
|
0x20380, 0x7fffffff, 0x7fffffff, pos) |
| 1170 |
|
|
| 1171 |
def fontbox(self): |
def fontbox(self): |
| 1172 |
return swi.swi('Font_ReadInfo', 'i;.iiii', self.handle) |
return swi.swi('Font_ReadInfo', 'i;.iiii', self.handle) |
| 1173 |
|
|
| 1174 |
def split(self, string, x, y): |
def split(self, string, x, y): |
| 1175 |
bb = swi.block(len(string) / 4 + 10) |
if self.utf8: |
| 1176 |
bb.padstring(string, chr(0)) |
s = string.encode('utf8') |
| 1177 |
|
else: |
| 1178 |
|
s = string.encode('latin1', 'replace') |
| 1179 |
|
bb = swi.block(len(s) / 4 + 10) |
| 1180 |
|
bb.padstring(s, chr(0)) |
| 1181 |
# Font_ScanString |
# Font_ScanString |
| 1182 |
pos = swi.swi(0x400a1, 'ibiii;.i', self.handle, bb, 0x300, x, y) |
pos = swi.swi(0x400a1, 'ibiii;.i', self.handle, bb, 0x300, x, y) |
| 1183 |
return pos - bb.start |
return pos - bb.start |
| 1210 |
self.views.remove(view) |
self.views.remove(view) |
| 1211 |
for content in self.contents: |
for content in self.contents: |
| 1212 |
content.remove_view(view) |
content.remove_view(view) |
| 1213 |
|
|
| 1214 |
def rescaled(self, view): |
def rescaled(self, view): |
| 1215 |
for content in self.contents: |
for content in self.contents: |
| 1216 |
content.remove_view(view) |
content.remove_view(view) |
| 1230 |
view.gx1 * 400 / scale - content.pos[0], |
view.gx1 * 400 / scale - content.pos[0], |
| 1231 |
view.gy1 * 400 / scale - content.pos[1]] |
view.gy1 * 400 / scale - content.pos[1]] |
| 1232 |
content.redraw(view, view.ox, view.oy, scale) |
content.redraw(view, view.ox, view.oy, scale) |
| 1233 |
|
|
| 1234 |
def redraw_stop(self, view, scale): |
def redraw_stop(self, view, scale): |
| 1235 |
pass |
pass |
| 1236 |
|
|
| 1237 |
def redraw_margin(self, view): |
def redraw_margin(self, view): |
| 1238 |
set_colour(0x777777) |
set_colour(0x777777) |
| 1239 |
plot(4, view.ox + view.gx0, view.oy) |
plot(4, view.ox + view.gx0, view.oy) |
| 1249 |
for content in self.contents: |
for content in self.contents: |
| 1250 |
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]: |
| 1251 |
content.click(view, buttons, x - content.pos[0], y - content.pos[1]) |
content.click(view, buttons, x - content.pos[0], y - content.pos[1]) |
| 1252 |
|
|
| 1253 |
def refresh(self, x0, y0, x1, y1): |
def refresh(self, x0, y0, x1, y1): |
| 1254 |
for view in self.views: |
for view in self.views: |
| 1255 |
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), |
| 1256 |
int(view.scale * x1 / 400 + 4), int(view.scale * y1 / 400 + 4)) |
int(view.scale * x1 / 400 + 4), int(view.scale * y1 / 400 + 4)) |
| 1257 |
|
|
| 1258 |
def add_font(self, name, size_x, size_y): |
def add_font(self, name, size_x, size_y): |
| 1259 |
f = (name, size_x, size_y) |
f = (name, size_x, size_y) |
| 1260 |
if self.fonts.has_key(f): |
if self.fonts.has_key(f): |
| 1273 |
|
|
| 1274 |
|
|
| 1275 |
class document_view(window): |
class document_view(window): |
| 1276 |
|
|
| 1277 |
def __init__(self, document, id, scale): |
def __init__(self, document, id, scale): |
| 1278 |
window.__init__(self, id) |
window.__init__(self, id) |
| 1279 |
self.document = document |
self.document = document |
| 1287 |
|
|
| 1288 |
def redraw(self): |
def redraw(self): |
| 1289 |
self.document.redraw(self, self.scale) |
self.document.redraw(self, self.scale) |
| 1290 |
|
|
| 1291 |
def redraw_stop(self): |
def redraw_stop(self): |
| 1292 |
self.document.redraw_stop(self, self.scale) |
self.document.redraw_stop(self, self.scale) |
| 1293 |
|
|
| 1315 |
|
|
| 1316 |
|
|
| 1317 |
class document_content: |
class document_content: |
| 1318 |
|
|
| 1319 |
def __init__(self, document, x0, y0, x1, y1): |
def __init__(self, document, x0, y0, x1, y1): |
| 1320 |
self.pos = [x0, y0, x1, y1] |
self.pos = [x0, y0, x1, y1] |
| 1321 |
self.colour = 0x0000ff |
self.colour = 0x0000ff |
| 1327 |
|
|
| 1328 |
def remove_view(self, view): |
def remove_view(self, view): |
| 1329 |
pass |
pass |
| 1330 |
|
|
| 1331 |
def redraw(self, view, x, y, scale): |
def redraw(self, view, x, y, scale): |
| 1332 |
set_colour(self.colour) |
set_colour(self.colour) |
| 1333 |
plot(4, x + self.scaled_pos[0], y - self.scaled_pos[1]) |
plot(4, x + self.scaled_pos[0], y - self.scaled_pos[1]) |
| 1338 |
plot(21, x + self.scaled_pos[2], y - self.scaled_pos[3]) |
plot(21, x + self.scaled_pos[2], y - self.scaled_pos[3]) |
| 1339 |
plot(4, x + self.scaled_pos[2], y - self.scaled_pos[1]) |
plot(4, x + self.scaled_pos[2], y - self.scaled_pos[1]) |
| 1340 |
plot(21, x + self.scaled_pos[0], y - self.scaled_pos[3]) |
plot(21, x + self.scaled_pos[0], y - self.scaled_pos[3]) |
| 1341 |
|
|
| 1342 |
def click(self, view, buttons, x, y): |
def click(self, view, buttons, x, y): |
| 1343 |
self.colour = 0xffffff - self.colour |
self.colour = 0xffffff - self.colour |
| 1344 |
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]) |