Пример #1
0
class SelectionMode(Mode):
    def __init__(self, frame, selection):
        self.frame     = frame
        self.selection = selection
        self.overlay   = Overlay(frame, self.render_overlay)

    def render_overlay(self, argon):
        argon.clear((0,0,0,0))
        render_selection(argon, self.frame, self.selection)

    def free(self):
        self.overlay.free()

    def insert_object(self, data):
        """
        Insert any object into the selection and adjust selection
        to cover the inserted object.
        """
        if isinstance(self.selection, mutable.Selection):
            if isinstance(self.selection.container, (mutable.Document, mutable.List)):
                self.selection.splice([data])
                return True
        else:
            mutable.get_document(self.selection).replace(self.selection, data)
            self.selection = data
            return True
Пример #2
0
 def __init__(self, frame, selection):
     self.frame     = frame
     self.selection = selection
     self.overlay   = Overlay(frame, self.render_overlay)
Пример #3
0
 def __init__(self, intron, mutator):
     self.intron  = intron
     self.mutator = mutator
     self.overlay = Overlay(frame, self.render_overlay)
Пример #4
0
class GenericMode(object):
    def __init__(self, intron, mutator):
        self.intron  = intron
        self.mutator = mutator
        self.overlay = Overlay(frame, self.render_overlay)

    def free(self):
        self.overlay.free()

    def render_overlay(self, argon):
        argon.clear(rgba(0,0,0,0))
        argon.render_rectangle(self.intron.rect, box7, color = rgba(0x00, 0x00, 0xFF, 0x80))
        mark = rgba(0x00, 0x00, 0xFF)
        if isinstance(self.mutator, Selection):
            start, stop = self.mutator.start, self.mutator.stop
            for box in self.intron.references():
                if box.in_range(start, stop):
                    rect = box.selection_marker(start, stop)
                    argon.render_rectangle(rect, color=mark)

    @classmethod
    def enter_click(cls, intron, pos):
        struct, index = intron.args
        mutator = Mutator(struct, index)
        if mutator.which in ('list', 'string', 'buffer'):
            index = intron.pick_offset(pos)
            mutator = Selection(mutator, index)
        set_mode( GenericMode(intron, mutator) )

    @classmethod
    def enter_descend(cls, intron, from_tail=False):
        struct, index = intron.args
        mutator = Mutator(struct, index)
        if mutator.which in ('list', 'string', 'buffer'):
            mutator = Selection(mutator, len(struct[index]) * from_tail)
        set_mode( GenericMode(intron, mutator) )

    def insert_text(self, text):
        mutator = self.mutator
        if isinstance(mutator, Selection):
            if mutator.which == 'list':
                # this is not nice way to do it.
                mutator.splice([language.Ref(u"")])
                struct = mutator.struct
                index0 = mutator.index
                index1 = mutator.start
                ref = struct[index0][index1]
                mutator = Selection(Mutator(ref, 0), 0)
                refresh(self.intron)
                self.intron = locate_intron(self.intron, ref, 0)
            if mutator.which != 'string':
                self.mutator = mutator
                refresh(self.intron)
                return False
            mutator.splice(text)
            mutator.start = mutator.stop
        else:
            mutator.replace(text)
            mutator = Selection(mutator, len(text))
        self.mutator = mutator
        refresh(self.intron)

    def keydown(self, key, modifiers, text):
        if text.isalnum() or text == '_':
            self.insert_text(text)
        print key
Пример #5
0
 def __init__(self, sel, click_response=False):
     self.sel = sel
     self.overlay = Overlay(main_frame, self.render_overlay)
     self.dragging = click_response
Пример #6
0
class EditMode(object):
    def __init__(self, sel, click_response=False):
        self.sel = sel
        self.overlay = Overlay(main_frame, self.render_overlay)
        self.dragging = click_response

    def set_dirtyfile(self, dirty=True):
        if dirty:
            argon.set_caption("macron[%s] %s*" % (current_language.name, filename))
        else:
            argon.set_caption("macron[%s] %s" % (current_language.name, filename))

    def free(self):
        self.overlay.free()
    
    def render_overlay(self, argon):
        argon.clear(rgba(0,0,0,0))
        slot = find_slot(main_frame.contents, self.sel.struct, self.sel.index)
        if slot is not None:
            argon.render_rectangle(slot.rect, box7, color = rgba(32, 32, 255, 128))
        if slot is not None and isinstance(self.sel, Selection):
            head = self.sel.head
            start, stop = self.sel.start, self.sel.stop
            for box in slot.references():
                if box.in_range(start, stop):
                    rect = box.selection_marker(start, stop)
                    argon.render_rectangle(rect, color = rgba(0, 0, 255, 128))
                if box.in_range(head, head):
                    rect = box.selection_marker(head, head)
                    argon.render_rectangle(rect, color = rgba(255, 255, 255, 255))
        if slot is None:
            argon.render_rectangle((0,0,main_frame.width, main_frame.height), box7, color = rgba(255, 255, 0, 192))

    def gen_struct(self, top):
        arg, marg = ndef[self.sel.struct.type.name][self.sel.index]
        if isinstance(self.sel, ListSelection):
            inst = autoinstantiate((), marg, top)
            if inst is not None:
                self.sel.splice(inst)
                self.sel = deep_climb_head(self.sel.struct, self.sel.index, self.sel.start)
            else:
                return
                print "cannot instantiate", top, "here"
        elif isinstance(self.sel, Mutator):
            inst = autoinstantiate(arg, marg, top)
            if inst is not None:
                self.sel.replace(inst)
                self.sel = deep_climb_head(self.sel.struct, self.sel.index, 0)
            else:
                return
                print "cannot instantiate", top, "here"
        assert self.sel is not None
        return True

    def put_struct(self, top):
        slot = find_slot(main_frame.contents, self.sel.struct, self.sel.index)
        if self.gen_struct(top):
            if isinstance(self.sel, Selection):
                self.sel.start = self.sel.stop
            if slot is not None:
                slot.rebuild()
            main_frame.dirty = True
            self.set_dirtyfile()

    def copy_selection(self):
        global copybuf
        if isinstance(self.sel, Selection):
            dup = self.sel.struct[self.sel.index][self.sel.start:self.sel.stop]
            if len(dup) == 1:
                dup = dup[0]
            copybuf = fullcopy(dup)
        else:
            dup = self.sel.struct[self.sel.index]
            copybuf = fullcopy(dup)
            print 'copy success'

    def paste_selection(self):
        slot = find_slot(main_frame.contents, self.sel.struct, self.sel.index)
        if isinstance(self.sel, ListSelection) and isinstance(copybuf, list):
            self.sel.splice(fullcopy(copybuf))
        elif isinstance(self.sel, ListSelection) and isinstance(copybuf, (Struct, Constant)):
            self.sel.splice([fullcopy(copybuf)])
        elif isinstance(self.sel, StringSelection) and isinstance(copybuf, unicode):
            self.sel.splice(copybuf)
        elif isinstance(self.sel, BufferSelection) and isinstance(copybuf, str):
            self.sel.splice(copybuf)
        elif isinstance(self.sel, Mutator) and isinstance(copybuf, (Struct, Constant, unicode, str)):
            self.sel.replace(fullcopy(copybuf))
        else:
            print 'paste unsuccessful'
            return
        if slot is not None:
            slot.rebuild()
        main_frame.dirty = True
        self.set_dirtyfile()

    def on_keydown(self, key, modifiers, text):
        shift = 'shift' in modifiers
        ctrl = 'ctrl' in modifiers
        alt = 'alt' in modifiers
        nxt = None

        if key == 'left' and isinstance(self.sel, Selection):
            bkg = self.sel.head > 0
            motion(self.sel, self.sel.head - bkg, shift)
        elif key == 'right' and isinstance(self.sel, Selection):
            fwd = self.sel.head < self.sel.length
            motion(self.sel, self.sel.head + fwd, shift)
        elif key == 'home' and isinstance(self.sel, Selection):
            motion(self.sel, 0, shift)
        elif key == 'end' and isinstance(self.sel, Selection):
            motion(self.sel, self.sel.length, shift)
        elif key == 'return' and shift:
            nxt = skip_to_previous(document, self.sel.struct, self.sel.index)  # it might need fixup. :P
        elif key == 'return':
            nxt = skip_to_next(document, self.sel.struct, self.sel.index)  # it might need fixup. :P
        elif key == 'up':
            nxt = wade_to_previous(document, self.sel)
        elif key == 'down' or (key == 'space' and not ctrl):
            nxt = wade_to_next(document, self.sel)
        elif ctrl and key == 'y':
            self.copy_selection()
        elif ctrl and key == 'p':
            self.paste_selection()
        elif ctrl and key == 's':
            save_file(filename, document)
            self.set_dirtyfile(False)
        elif ctrl and key == 'a':
            if self.sel.start == 0 and self.sel.stop == self.sel.length:
                nxt = select_this(document, self.sel.struct)
            else:
                self.sel.start = 0
                self.sel.stop  = self.sel.length
        elif key == 'delete':
            if isinstance(self.sel, Selection):
                if self.sel.start == self.sel.stop and self.sel.stop < self.sel.length:
                    self.sel.stop += 1
                self.sel.splice()
                self.set_dirtyfile()
            slot = find_slot(main_frame.contents, self.sel.struct, self.sel.index)
            if slot is not None:
                slot.rebuild()
            main_frame.dirty = True
        elif key == 'backspace':
            if isinstance(self.sel, Selection):
                if self.sel.start == self.sel.stop and self.sel.start > 0:
                    self.sel.start -= 1
                self.sel.splice()
                self.set_dirtyfile()
            slot = find_slot(main_frame.contents, self.sel.struct, self.sel.index)
            if slot is not None:
                slot.rebuild()
            main_frame.dirty = True
        elif key == 'space':
            slot = find_slot(main_frame.contents, self.sel.struct, self.sel.index)
            self.gen_struct(analyzer.String)
            if isinstance(self.sel, StringSelection):
                self.sel.splice(u' ')
                self.sel.start = self.sel.stop
            if slot is not None:
                slot.rebuild()
            main_frame.dirty = True
            self.set_dirtyfile()
        elif alt and text == 'w':
            self.put_struct(u"while")
        elif alt and text == 'e':
            self.put_struct(u"foreach")
        elif alt and text == 't':
            self.put_struct(u"true")
        elif alt and text == 'f':
            self.put_struct(u"false")
        elif len(text) > 0 and (text.isalnum() or text in u"_"):
            slot = find_slot(main_frame.contents, self.sel.struct, self.sel.index)
            self.gen_struct(("number" if text.isdigit() else analyzer.String))
                #self.gen_struct(analyzer.String)
            if isinstance(self.sel, StringSelection):
                self.sel.splice(text)
                self.sel.start = self.sel.stop
            if slot is not None:
                slot.rebuild()
            main_frame.dirty = True
            self.set_dirtyfile()
        elif text == '!':
            self.put_struct(u"buffer")
        elif text == '"':
            self.put_struct(u"string")
        elif text == '#':
            self.put_struct(u"constant")
        elif text == '(':
            self.put_struct(u"struct") or self.put_struct(u"call")
        elif text == '[':
            self.put_struct(u"list")
        elif text == '{':
            self.put_struct(u"group") or self.put_struct(u"lambda")
        elif text == '=':
            self.put_struct(u"let")
        elif text == '.':
            self.put_struct(u"attribute")
        elif text == '<':
            self.put_struct(u"return")
        elif text == '@':
            self.put_struct(u"dictionary")
        elif text == '[':
            self.put_struct(u"list")
        elif text == '?':
            self.put_struct(u"condition_rule")

        #fixme, maybe. :/
        if nxt is not None:
            self.sel = nxt
        self.overlay.dirty = True

    def on_mousedown(self, button, pos):
        return True # Execute default behavior

    def on_mousemotion(self, pos, vel):
        if self.dragging:
            slot = find_slot(main_frame.contents, self.sel.struct, self.sel.index)
            if slot is not None:
                self.sel.head = slot.pick_offset(pos)
                self.overlay.dirty = True

    def on_mouseup(self, button, pos):
        self.dragging = False