def main(term): initialize_curses_settings() height, width = term.getmaxyx() default = Button() default.draw(term, utils.point(0, 0)) term.getch()
def main(): terminal.open() terminal.set("window.title='Scrollable Calendar Grid'") terminal.set("input.filter={keyboard, mouse+}") escape_codes = [terminal.TK_Q, terminal.TK_ESCAPE, 224] b = TextBox(point(0, 0), 80, 25, "asdf" * 10) b.split_x() b.l.split_y() b.r.split_y() c = None while True: terminal.clear() for (x, y, s) in b.blt_border(): terminal.puts(x, y, s) for (x, y, s) in b.blt_text(): terminal.puts(x, y, s) terminal.refresh() c = terminal.read() if c in escape_codes: break if b.l.split: b.l.join() continue elif b.r.split: b.r.join() continue b.join()
def __init__(self, label=None, size=None, flags=None, onpress=None): super().__init__() # defaults to button label if no label given self.label = label if label else Button.label # bounds of the screen only range from [0 -> n] # negative units will always be overwritten self.pivot = utils.point(-1, -1) if not size: # default to Button width if shorter than default label if len(self.label) < Button.width - 2: self.width = Button.width else: self.width = len(self.label) + 2 else: # these are manually set so assume correct input self.width = size.width self.height = size.height if flags: self.selected = flags & Control.SELECTED if flags & Control.NOBORDER: self.bordered = False self.centered = flags & Control.CENTERED self.onpress_handler = onpress
def main(term): initialize_curses_settings() filebtn = Button("File", size=utils.size(1, 4), flags=Control.NOBORDER) editbtn = Button("Edit", size=utils.size(1, 4), flags=Control.NOBORDER) viewbtn = Button("View", size=utils.size(1, 4), flags=Control.NOBORDER) helpbtn = Button("Help", size=utils.size(1, 4), flags=Control.NOBORDER) while True: term.erase() filebtn.draw(term, point(1, 0)) editbtn.draw(term, point(7, 0)) viewbtn.draw(term, point(13, 0)) helpbtn.draw(term, point(19, 0)) ch = term.getch() if ch == ord('q'): break
def draw(self, term, pivot): ''' +------+ |Button| +------+ 3 8 ''' if self.pivot != pivot: self.pivot = pivot p2 = utils.point(pivot.x + self.width, pivot.y + self.height) self.bounds = pivot, p2 x, y = self.pivot color = curses.color_pair(1) if self.selected: color = curses.color_pair(7) if self.bordered: utils.border(term, x, y, self.width, self.height) # determine starting point for label if self.centered: # centered has no relation to y px = (self.width - len(self.label)) // 2 + x else: width_offset = 1 if self.bordered else 0 px = x + width_offset # draw the button label label = self.label if len(self.label) != self.width - 2: if px == x + 1: # default label placement, append to end label += " " * (self.width - len(self.label) - 2) else: # probably centered, anyways check length from ends of width - 2 # check end first since label can start from 0 -> width-1 and # can only overfill to the right # TODO pass # configures height within the box allowable_height = self.height if self.bordered: allowable_height -= 2 if allowable_height == 1: height_offset = allowable_height else: height_offset = allowable_height // 2 term.addstr(y + height_offset, px, label, color)
def __init__(self, label=None, size=None, appearance=None, flags=None): super().__init__() # the flags would have the properties with boolean?(undeterminded) self.label = label if label else Button.label # begin with starting point (0, 0) initially and overwrite on draw self.pivot = utils.point(0, 0) self.bounds = None # will be a tuple of two points if not size: if len(self.label) < Button.width - 2: self.width = Button.width else: self.width = len(self.label) + 2 self.height = Button.height else: # these are manually set so assume they are correct self.width = size.width self.height = size.height if appearance: self.appearance = color(appearance) else: self.appearance = curses.color_pair(1) if flags: self.focused = False self.selected = flags & Control.SELECTED # property changes the border color self.disabled = False self.bordered = Control.bordered # | Control.bordered # (flag & enum | default) if flags & Control.NOBORDER: self.bordered = False if not size: self.height = 1 self.centered = flags & Control.CENTERED self.handlers = dict()
def main(s): from source.utils import initialize_curses_settings initialize_curses_settings() b = Button() b.draw(s, utils.point(0, 0)) s.getch()
def main(term): initialize_curses_settings() height, width = term.getmaxyx() default = Button() selected = Button('Text') centered = Button('Text', flags=Control.CENTERED) large = Button('Large', size=utils.size(14, 6)) large_centered = Button('Large', size=utils.size(14, 6), flags=Control.CENTERED) # selected through constructor flags parameter flagged = Button('Flagged', flags=Control.SELECTED) # both selected and centered properties selected_centered = Button('Text1', flags=Control.SELECTED | Control.CENTERED) # manually select selected.select() unbordered = Button('NoBorder', flags=Control.NOBORDER) mouse_down = False element_clicked = None point_clicked = None mouse_scroll = 0 mouse_move = False px, py = 0, 0 i = 0 while True: term.erase() if mouse_down: term.addstr(13, 0, "Mouse click") if mouse_move: term.addstr(14, 0, "Mouse moved") if element_clicked: term.addstr(15, 0, "element") element_clicked.clicked(term) elif point_clicked: term.addstr(15, 0, f"point(x={point_clicked[0]}, y={point_clicked[1]})") term.addstr( 16, 0, f"other(a={other_mouse[0]}, buttonmask={other_mouse[1]})") if mouse_scroll: term.addstr(17, 0, f"scrolling {'up' if mouse_scroll == 1 else 'down'}") default.draw(term, point(0, 0)) selected.draw(term, point(8, 0)) large.draw(term, point(16, 0)) large_centered.draw(term, point(30, 0)) flagged.draw(term, point(44, 0)) centered.draw(term, point(53, 3)) selected_centered.draw(term, point(53, 0)) unbordered.draw(term, point(61, 0)) term.addstr(7, 0, "Default button shows button with all defaults.") term.addstr( 8, 0, "Selected button shows button with selected property as true manually." ) term.addstr( 9, 0, "Large button shows button with a size input. ex. Size=(14, 6).") term.addstr( 10, 0, "Flagged button shows button with selected property as true through constructor." ) term.addstr( 11, 0, "Next button shows button initialized with multiple flags: Selected and Centered." ) term.addstr(20, 0, f"width={width}, height={height}") term.addstr(21, 0, f"Button1 release={curses.BUTTON1_RELEASED}") term.addstr(22, 0, f"Button1 pressed={curses.BUTTON1_PRESSED}") term.addstr(23, 0, f"Button1 clicked={curses.BUTTON1_CLICKED}") term.addstr(24, i % width, 'o') # i += 1 # curses does not support mouse hover -- so button hover state is a no a, mx, my, c, mask = curses.getmouse() # mx, my = term.getyx() term.addstr(25, 0, f"{mx}, {my}") # term.addstr() # term.refresh() term.timeout(60) key = term.getch() term.addstr(6, 0, str(key)) if key == ord('q'): break # when selecting/unselecting, there is a cursor artifact even # with curses.set_curs(0) ie. no cursor. Maybe calling it # again will fix if key == ord('s'): default.select() if key == ord('S'): default.unselect() if key == curses.KEY_MOUSE: # probably need to write a mouse handler # ex curses_mouse_handler as mouse mouse_down = True a, px, py, _, mask = curses.getmouse() # probably only want to make sure that the mouse was # only 'LEFT CLICKED' before choosing clicked method() e = 0 for element in Control.elements: if element.covers(point(px, py)): element_clicked = element point_clicked = None break else: if mask == 65536: mouse_scroll = 1 elif mask == 2097152: mouse_scroll = 2 element_clicked = None point_clicked = px, py other_mouse = a, mask else: mouse_down = False element_clicked = None if key == curses.KEY_MOVE: mouse_move = True else: mose_move = False