diff --git a/examples/controls/dropdown.py b/examples/controls/dropdown.py new file mode 100644 index 0000000..ddfc79a --- /dev/null +++ b/examples/controls/dropdown.py @@ -0,0 +1,21 @@ +"""The most basic possible use case for py_cui + +@author: Jakub Wlodek +@created: 12-Aug-2019 +""" + +# Import the lib +import py_cui + +# create the CUI object. Will have a 3 by 3 grid with indexes from 0,0 to 2,2 +root = py_cui.PyCUI(3, 3) + +# Add a label to the center of the CUI in the 1,1 grid position +dropdown = root.add_custom_widget(py_cui.widgets.DropdownMenu, 'Example Dropdown', 1, 1, 1, 1, 1, 0, 10) +textbox = root.add_text_box('Test Text Box', 0, 1, 1, 1, 1, 0, "Hi") + +for i in range(15): + dropdown.add_item(f'Test{i}') + +# Start/Render the CUI +root.start() diff --git a/py_cui/__init__.py b/py_cui/__init__.py index ee6b30d..8b139f4 100644 --- a/py_cui/__init__.py +++ b/py_cui/__init__.py @@ -179,7 +179,7 @@ def __init__( # Initialize grid, renderer, and widget dict self._grid = py_cui.grid.Grid( - num_rows, num_cols, self._height, self._width, self._logger + self, num_rows, num_cols, self._height, self._width, self._logger ) self._stdscr: Any = None self._refresh_timeout = -1 diff --git a/py_cui/grid.py b/py_cui/grid.py index 640bff6..e4494da 100644 --- a/py_cui/grid.py +++ b/py_cui/grid.py @@ -31,7 +31,7 @@ class Grid: """ - def __init__(self, num_rows: int, num_columns: int, height: int, width: int, logger: 'py_cui.debug.PyCUILogger'): + def __init__(self, parent, num_rows: int, num_columns: int, height: int, width: int, logger: 'py_cui.debug.PyCUILogger'): """Constructor for the Grid class Parameters @@ -46,6 +46,7 @@ def __init__(self, num_rows: int, num_columns: int, height: int, width: int, log The width in characters of the terminal window """ + self._parent = parent self._num_rows = num_rows self._num_columns = num_columns self._height = height diff --git a/py_cui/widget_set.py b/py_cui/widget_set.py index 9f3deac..8bf14da 100644 --- a/py_cui/widget_set.py +++ b/py_cui/widget_set.py @@ -60,7 +60,7 @@ def __init__(self, num_rows: int, num_cols: int, logger: 'py_cui.debug.PyCUILogg status_bars_height = self._root.title_bar.get_height() + self._root.status_bar.get_height() self._height = self._height - status_bars_height - 2 - self._grid = py_cui.grid.Grid(num_rows, num_cols, self._height, self._width, logger) + self._grid = py_cui.grid.Grid(root, num_rows, num_cols, self._height, self._width, logger) self._selected_widget: Optional[int] = None self._logger = logger diff --git a/py_cui/widgets.py b/py_cui/widgets.py index 5aa5262..3669c53 100644 --- a/py_cui/widgets.py +++ b/py_cui/widgets.py @@ -91,6 +91,13 @@ def __init__(self, id, title: str, grid: 'py_cui.grid.Grid', row: int, column: i self._context_menu = None + def _get_parent_ui(self): + """Function used to get reference to parent UI instance for interfacing with popups and context menus + """ + + return self._grid._parent + + def add_key_command(self, key: Union[int, List[int]], command: Callable[[],Any]) -> None: """Maps a keycode to a function that will be executed when in focus mode diff --git a/tests/conftest.py b/tests/conftest.py index 1343096..d13d528 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,6 +10,14 @@ def LOGGER(): return dbg.PyCUILogger('PYCUI TEST') +@pytest.fixture +def PYCUI(): + + def _PYCUI(rows, cols, height, width): + return py_cui.PyCUI(rows, cols, simulated_terminal=[height, width]) + + return _PYCUI + @pytest.fixture def RENDERER(request, LOGGER): @@ -17,11 +25,11 @@ def RENDERER(request, LOGGER): @pytest.fixture -def GRID(request, LOGGER): +def GRID(request, PYCUI, LOGGER): def _GRID(rows, cols, height, width): - return py_cui.grid.Grid(rows, cols, height, width, LOGGER) + return py_cui.grid.Grid(PYCUI, rows, cols, height, width, LOGGER) return _GRID @@ -43,15 +51,6 @@ def _CUSTOMWIDGET(id, name, row, col, rowspan, colspan): return _CUSTOMWIDGET -@pytest.fixture -def PYCUI(): - - def _PYCUI(rows, cols, height, width): - return py_cui.PyCUI(rows, cols, simulated_terminal=[height, width]) - - return _PYCUI - - @pytest.fixture def WIDGETSET(request, LOGGER):