示例#1
0
    def putEvent(self, event):
        """
        Sets a pending event.

        Puts an event in the pending state, by storing a copy of the `event`
        structure in the `pending` variable, a member of `Program`.

        Only one event is allowed to be pending. The next call to
        `getEvent()` will return this pending event even if there are
        other events in the system queue to be handled.
        :param event: Event to place in the queue
        """
        e = Event(evNothing)
        e.setFrom(event)
        Program.pending.put(e)
示例#2
0
    def popup(self):
        bounds = Rect(0, 0, 0, 0)
        # for clarity, we'll create separately the menu to insert into the menubox
        theMenu = Menu(
            MenuItem("Item ~1~",
                     cmOne,
                     kbAltI,
                     hcNoContext,
                     "Alt-1",
                     nextItem=MenuItem("Item ~2~",
                                       cmTwo,
                                       kbAltT,
                                       hcNoContext,
                                       "Alt-2",
                                       nextItem=MenuItem(
                                           "E~x~it", cmQuit, kbAltX,
                                           hcNoContext, "Alt-X"))))

        # now create the menu with no parent
        mb = MenuBox(bounds, theMenu, 0)

        # however, if we don't specify appropriate bounds, we must do something to ensure it is appropriately
        # positioned on the desktop
        mb.options |= ofCentered

        # then make sure it's valid and execute it
        if self.validView(mb):
            result = self.desktop.execView(mb)
            self.destroy(mb)

        event = Event(evCommand)
        event.message.command = result
        self.putEvent(event)
        self.clearEvent(event)
示例#3
0
    def press(self):
        message(self.owner, evBroadcast, cmRecordHistory, None)

        if self._flags & bfBroadcast:
            message(self.owner, evBroadcast, self._command, self)
        else:
            e = Event(evCommand)
            e.message.command = self._command
            e.message.infoPtr = self
            self.putEvent(e)
示例#4
0
def message(receiver, what, command, infoPtr):
    if not receiver:
        return None

    event = Event(what)
    event.message.infoPtr = infoPtr
    event.message.command = command

    receiver.handleEvent(event)
    if event.what == evNothing:
        return event.message.infoPtr
    return None
示例#5
0
    def handleKeyboard(self):
        keyType = 0

        # see if there is data available
        try:
            code = self.stdscr.get_wch()
        except curses.error:
            # getch() returns -1, get_wch() raises an exception
            code = curses.ERR

        if not isinstance(code, (int,)):
            code = ord(code)

        if code == curses.KEY_MOUSE:
            return self.handleMouse()

        if code != curses.ERR:
            # grab the escape key and start the timer
            if code == 27 and not kbEscTimer.isRunning():
                return kbEscTimer.start(DELAY_ESCAPE)

            # key pressed within time limit
            if kbEscTimer.isRunning() and not kbEscTimer.isExpired():
                kbEscTimer.stop()
                if code != 27:  # simulate Alt-letter code
                    code = chr(code & 0xFF).upper()
                    keyType = TALT
        elif kbEscTimer.isExpired():
            # timeout condition: generate standard Esc code
            kbEscTimer.stop()
            code = 27
        else:  # code == curses.ERR
            return

        modifiers = 0
        if PLATFORM_IS_WINDOWS:
            # Handle ALT+ keys in DOS window
            if (code & 0x120) == 0x120:
                code = chr(code - 0x160)
                keyType = TALT
                modifiers = 0
        else:
            code, modifiers = self.kbReadShiftState(code)

        code = self.kbMapKey(code, keyType, modifiers)

        if code != kbNoKey:
            event = Event(evKeyDown)
            event.keyDown.keyCode = code
            event.keyDown.controlKeyState = modifiers
            TScreen.putEvent(event)
示例#6
0
    def handleEvent(self, event):
        """
        Handles events sent to the status line by calling
        `super().handleEvent()`, then checking for three kinds of special
        events.
       
        - Mouse clicks that fall within the rectangle occupied by any status
          item generate a command event, with event.what set to the command in
          that status item.
        - Key events are checked against the keyCode data member in each
          item; a match causes a command event with that item's command.
        - Broadcast events with the command cmCommandSetChanged cause the
          status line to redraw itself to reflect any hot keys that might have
          been enabled or disabled.

        :param event: The event to handle
        """
        super().handleEvent(event)

        what = event.what
        item = None
        if what == evMouseDown:
            processing = True
            while processing:
                mouse = self.makeLocal(event.mouse.where)
                if item is not self.__itemMouseIsIn(mouse):
                    item = self.__itemMouseIsIn(mouse)
                    self.__drawSelect(item)
                processing = self.mouseEvent(event, evMouseMove)

            if item and self.commandEnabled(item.command):
                e = Event(evCommand)
                e.message.command = item.command
                e.message.infoPtr = None
                self.putEvent(e)
                logger.info(event)
                self.drawView()
                return
            self.clearEvent(event)
            self.drawView()
        elif what == evKeyDown:
            item = self._itemCodes.get(event.keyDown.keyCode)
            if item and self.commandEnabled(item.command):
                event.what = evCommand
                event.message.command = item.command
                event.message.infoPtr = None
                return
        elif what == evBroadcast:
            if event.message.command == cmCommandSetChanged:
                self.drawView()
示例#7
0
 def execute(self):
     done = False
     while not done:
         self.endState = 0
         alsoDone = False
         while not alsoDone:
             e = Event(evNothing)
             self.getEvent(e)
             if e.what != evNothing:
                 self.handleEvent(e)
             if e.what != evNothing:
                 self.eventError(e)
             alsoDone = (self.endState != 0)
         done = (self.valid(self.endState))
     return self.endState
示例#8
0
    def valid(self, command):
        if command != cmOK:
            return True
        rec = DataRecord()
        self.dirInput.getData()
        path = rec.value
        if not path:
            event = Event(evCommand)
            event.message.command = cmChangeDir
            self.putEvent(event)
            return False

        path = fexpand(path)
        if path and path[-1] == os.sep:
            path = path[:-1]

        if self.changeDir(path):
            messageBox(self.invalidText, mfError, (mfOKButton, ))
            return False
        return True
示例#9
0
    def execute(self):
        """
        Executes a menu view until the user selects a menu item or cancels the
        process. Returns the command assigned to the selected menu item, or
        zero if the menu was canceled.
       
        Should never be called except by `Group.execView()`.
        """
        autoSelect = False
        result = 0
        itemShown = None
        e = Event(evNothing)
        self._current = self.menu.default
        action = MenuAction.doNothing

        while action != MenuAction.doReturn:
            action = MenuAction.doNothing
            self.getEvent(e)
            what = e.what
            if what == evMouseDown:
                if self.mouseInView(e.mouse.where) or self.__mouseInOwner(e):
                    self.__trackMouse(e)
                    if self.size.y == 1:
                        autoSelect = True
                else:
                    action = MenuAction.doReturn
            elif what == evMouseUp:
                action = self.__handleMouseUp(action, e)
            elif what == evMouseMove:
                if e.mouse.buttons:
                    self.__trackMouse(e)
                    if not (self.mouseInView(e.mouse.where) or
                            self.__mouseInOwner(e)) and self.__mouseInMenus(e):
                        action = MenuAction.doReturn
            elif what == evKeyDown:
                action, autoSelect, result = self.__handleEventKeyDown(
                    action, autoSelect, e, result)
            elif what == evCommand:
                if e.message.command == cmMenu:
                    autoSelect = False
                    if self._parentMenu:
                        action = MenuAction.doReturn
                else:
                    action = MenuAction.doReturn

            if itemShown != self._current:
                itemShown = self._current
                self.drawView()

            result = self.__handleMenuAction(action, autoSelect, e, result)

            if result and self.commandEnabled(result):
                action = MenuAction.doReturn
                self.clearEvent(e)
            else:
                result = 0

        if e.what != evNothing and (self._parentMenu or e.what == evCommand):
            self.putEvent(e)

        if self._current:
            self.menu.default = self._current
            self._current = None
            self.drawView()

        return result
示例#10
0
    def ListDialog(self):
        r = self.getExtent()
        r.grow(-3, -3)
        pd = GridViewDialog(r, 'Grid View', headings, 1, ListData, columns,
                            rows, widths, [
                                2,
                            ] * columns)
        pd.flags |= wfGrow
        self.desktop.execView(pd)
        self.destroy(pd)


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    with open(sys.argv[1], 'rt', newline='') as fp:
        rows = csv.reader(fp)
        headings = [next(rows)]
        widths = [len(h) + 2 for h in headings[0]]
        for i, row in enumerate(rows):
            for j, col in enumerate(row):
                columns = max(columns, j)
                widths[j] = max(len(col) + 2, widths[j])
                ListData[i, j] = ListRec(col, True)
    rows = j - 1
    app = GridApp()
    event = Event(evCommand)
    event.message.command = cmList
    app.putEvent(event)
    app.run()
示例#11
0
    def handleMouse(self):
        event = Event(evNothing)

        try:
            me = MouseEvent(*curses.getmouse())
        except Exception as e:
            # No mouse? No Problem.
            return

        event.mouse.controlKeyState = 0

        if me.bstate & curses.BUTTON_SHIFT:
            event.mouse.controlKeyState |= kbLeftShift | kbRightShift
        if me.bstate & curses.BUTTON_CTRL:
            event.mouse.controlKeyState |= kbLeftCtrl | kbRightCtrl
        if me.bstate & curses.BUTTON_ALT:
            event.mouse.controlKeyState |= kbLeftAlt | kbRightAlt

        event.mouse.where.x = clamp(me.x, 0, self.screenWidth - 1)
        event.mouse.where.y = clamp(me.y, 0, self.screenHeight - 1)

        # Convert buttons.
        buttons = mbLeftButton
        if me.bstate & BUTTON1:
            buttons = mbLeftButton
        elif me.bstate & BUTTON3:
            buttons = mbRightButton

        # Check clicks.
        if me.bstate & BUTTON_CLICKED:
            self.putMouseEvent(event, buttons, 0, evMouseDown)
            self.msOldButtons = buttons

            msAutoTimer.stop()
            self.putMouseEvent(event, buttons, 0, evMouseUp)
            self.msOldButtons &= ~buttons

        # Double clicked...
        if me.bstate & BUTTON_DOUBLE_CLICKED:
            msAutoTimer.stop()
            self.putMouseEvent(event, buttons, meDoubleClick, evMouseDown)
            self.msOldButtons &= ~buttons

        if event.mouse.where != self.msWhere:
            # Undraw the mouse
            self.drawMouse(False)
            if me.bstate & BUTTON_PRESSED:
                self.putMouseEvent(event, buttons, meMouseMoved, evMouseMove)
                self.msWhere.x = event.mouse.where.x
                self.msWhere.y = event.mouse.where.y
                msAutoTimer.start(DELAY_AUTOCLICK_FIRST)
                self.putMouseEvent(event, buttons, 0, evMouseDown)
                self.msOldButtons = buttons

            if me.bstate & BUTTON_RELEASED:
                self.putMouseEvent(event, buttons, meMouseMoved, evMouseMove)
                self.msWhere.x = event.mouse.where.x
                self.msWhere.y = event.mouse.where.y
                msAutoTimer.stop()
                self.putMouseEvent(event, buttons, 0, evMouseUp)
                self.msOldButtons &= ~buttons
        else:
            if me.bstate & BUTTON_PRESSED:
                msAutoTimer.start(DELAY_AUTOCLICK_FIRST)
                self.putMouseEvent(event, buttons, 0, evMouseDown)
                self.msOldButtons = buttons
            if me.bstate & BUTTON_RELEASED:
                msAutoTimer.stop()
                self.putMouseEvent(event, buttons, 0, evMouseUp)
                self.msOldButtons &= ~buttons
        return True
示例#12
0
            time.sleep(0.1)
        logger.info('Sending "Pepe".')
        postInfo(2, '   Pepe')
        logger.info('Done.')


class PostHandler(logging.Handler):
    def __init__(self):
        super().__init__()

    def emit(self, record):
        with self.lock:
            postMessage(record.getMessage())


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger('vindauga')
    logger.propagate = False
    handlers = logger.handlers
    for handler in handlers:
        # Remove everything else
        logger.removeHandler(handler)
    # Add the message window
    logger.addHandler(PostHandler())
    init = Event(evCommand)
    init.message.command = cmTest
    app = MessageWindowApp()
    app.putEvent(init)
    app.run()