Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added uiEntry + fixed a wrong function call #27

Merged
6 commits merged into from
Oct 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions examples/entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
Shows three entries :
- a simple entry
- a password entry
- a search entry

"""

from pylibui.core import App
from pylibui.controls import (Window, Entry, SearchEntry, PasswordEntry,
VerticalBox)


class MyWindow(Window):

def onClose(self, data):
super().onClose(data)
app.stop()


class MyEntry(Entry):

def onChanged(self, data):
print('entry changed!')


app = App()

window = MyWindow('Entry example')
window.setMargined(1)

entry = MyEntry()
search_entry = SearchEntry()
password_entry = PasswordEntry()

vbox = VerticalBox()
vbox.setPadded(1)
vbox.append(entry)
vbox.append(search_entry)
vbox.append(password_entry)

window.setChild(vbox)
window.show()

app.start()
# app.close() # SEE https://github.com/joaoventura/pylibui/issues/18
1 change: 1 addition & 0 deletions pylibui/controls/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .control import Control
from .combobox import Combobox
from .checkbox import Checkbox
from .entry import Entry, PasswordEntry, SearchEntry
from .label import Label
from .progressbar import ProgressBar
from .slider import Slider
Expand Down
107 changes: 107 additions & 0 deletions pylibui/controls/entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""
Python wrapper for libui.

"""

from pylibui import libui
from .control import Control


class BaseEntry(Control):

def setText(self, text):
"""
Sets the text of the entry.

:param text: string
:return: None
"""
libui.uiEntrySetText(self.control, text)

def getText(self):
"""
Returns the text of the entry.

:return: string
"""
return libui.uiEntryText(self.control)

def setReadOnly(self, read_only):
"""
Sets whether the entry is read only or not.

:param read_only: bool
:return: None
"""
libui.uiEntrySetReadOnly(self.control, read_only)

def getReadOnly(self):
"""
Returns whether the entry is read only or not.

:return: bool
"""
return libui.uiEntryReadOnly(self.control)

def onChanged(self, data):
"""
Executes when the entry is changed.

:param data: data
:return: None
"""
pass


class Entry(BaseEntry):

def __init__(self):
"""
Creates a new entry.

"""
super().__init__()
self.control = libui.uiNewEntry()

def handler(window, data):
self.onChanged(data)
return 0

self.changedHandler = libui.uiEntryOnChanged(self.control, handler,
None)


class PasswordEntry(Entry):

def __init__(self):
"""
Creates a new password entry.

"""
super().__init__()
self.control = libui.uiNewPasswordEntry()

def handler(window, data):
self.onChanged(data)
return 0

self.changedHandler = libui.uiEntryOnChanged(self.control, handler,
None)


class SearchEntry(Entry):

def __init__(self):
"""
Creates a new search entry.

"""
super().__init__()
self.control = libui.uiNewSearchEntry()

def handler(window, data):
self.onChanged(data)
return 0

self.changedHandler = libui.uiEntryOnChanged(self.control, handler,
None)
1 change: 1 addition & 0 deletions pylibui/libui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from .control import *
from .combobox import *
from .checkbox import *
from .entry import *
from .label import *
from .main import *
from .progressbar import *
Expand Down
2 changes: 1 addition & 1 deletion pylibui/libui/checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def uiCheckboxOnToggled(checkbox, callback, data):
ctypes.c_int, ctypes.POINTER(uiCheckbox), ctypes.c_void_p)
c_callback = c_type(callback)

clibui.uiButtonOnClicked(checkbox, c_callback, data)
clibui.uiCheckboxOnToggled(checkbox, c_callback, data)

return c_callback

Expand Down
138 changes: 138 additions & 0 deletions pylibui/libui/entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
"""
Python wrapper for libui.

"""

import ctypes
from . import clibui


class uiEntry(ctypes.Structure):
"""Wrapper for the uiEntry C struct."""

pass


def uiEntryPointer(obj):
"""
Casts an object to uiEntry pointer type.

:param obj: a generic object
:return: uiEntry
"""

return ctypes.cast(obj, ctypes.POINTER(uiEntry))


# - char *uiEntryText(uiEntry *e);
def uiEntryText(entry):
"""
Returns the text of the entry.

:param entry: uiEntry
:return: string
"""

clibui.uiEntryText.restype = ctypes.c_char_p
text = clibui.uiEntryText(entry)

return text.decode()


# - void uiEntrySetText(uiEntry *e, const char *text);
def uiEntrySetText(entry, text):
"""
Sets the text of the entry.

:param entry: uiEntry
:param text: string
:return: None
"""

clibui.uiEntrySetText(entry, bytes(text, 'utf-8'))


# - void uiEntryOnChanged(uiEntry *e, void (*f)(uiEntry *e, void *data), void *data);
def uiEntryOnChanged(entry, callback, data):
"""
Executes the callback function on entry change.

:param entry: uiEntry
:param callback: function
:param data: data
:return: reference to C callback function
"""
c_type = ctypes.CFUNCTYPE(
ctypes.c_int, ctypes.POINTER(uiEntry), ctypes.c_void_p)
c_callback = c_type(callback)

clibui.uiEntryOnChanged(entry, c_callback, data)

return c_callback


# - int uiEntryReadOnly(uiEntry *e);
def uiEntryReadOnly(entry):
"""
Returns whether the entry is read only or not.

:param entry: uiEntry
:return: string
"""

return bool(clibui.uiEntryReadOnly(entry))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast should be in wrapper I think, and return value should be int instead of string, in docstring.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are right. The reason I did not do it is simply because I wrote the code before the issue was submitted. (24 Jun)
I can fix it on this PR; however I already did another branch -which I will submit in a PR after this one- which removes all the casts in pylibui/libui/*.py files. It's which one you prefer :-)

Concerning the "int" instead of "string", you are right

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. It's okay, we can merge this one, then send the others 👍



# - void uiEntrySetReadOnly(uiEntry *e, int readonly);
def uiEntrySetReadOnly(entry, read_only):
"""
Sets whether the entry is read only or not.

:param entry: uiEntry
:param read_only: int
:return: None
"""

clibui.uiEntrySetReadOnly(entry, read_only)


# - uiEntry *uiNewEntry(void);
def uiNewEntry():
"""
Creates a new entry.

:return: uiEntry
"""

# Set return type
clibui.uiNewEntry.restype = ctypes.POINTER(uiEntry)

return clibui.uiNewEntry()


# uiEntry *uiNewPasswordEntry(void);
def uiNewPasswordEntry():
"""
Creates a new password entry.

:return: uiEntry
"""

# Set return type
clibui.uiNewPasswordEntry.restype = ctypes.POINTER(uiEntry)

return clibui.uiNewPasswordEntry()


# uiEntry *uiNewSearchEntry(void);
def uiNewSearchEntry():
"""
Creates a new search entry.

:return: uiEntry
"""

# Set return type
clibui.uiNewSearchEntry.restype = ctypes.POINTER(uiEntry)

return clibui.uiNewSearchEntry()
41 changes: 41 additions & 0 deletions tests/test_entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
Pylibui test suite.

"""

from pylibui.controls import Entry, PasswordEntry, SearchEntry
from tests.utils import WindowTestCase


class EntryTest(WindowTestCase):

def setUp(self):
super().setUp()
self.entry = Entry()

def test_text_initial_value(self):
"""Tests the entry's `text` initial value is empty."""
self.assertEqual(self.entry.getText(), '')

def test_text_can_be_changed(self):
"""Tests the entry's `text` attribute can be changed."""
text = 'My entry'
self.entry.setText(text)
self.assertEqual(self.entry.getText(), text)

def test_read_only_initial_value(self):
"""Tests the entry's `read_only` initial value is False."""
self.assertEqual(self.entry.getReadOnly(), False)

def test_read_only_can_be_set_to_true(self):
"""Tests the entry's `read_only` attribute can be set to True."""
self.entry.setReadOnly(True)
self.assertEqual(self.entry.getReadOnly(), True)

def test_read_only_can_be_set_to_false(self):
"""Tests the entry's `read_only` attribute can be set to False."""
self.entry.setReadOnly(False)
self.assertEqual(self.entry.getReadOnly(), False)

def test_onChanged_is_called(self):
raise # TODO: how do we simulate mouse press ?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we will remove all lines like this, why did you add one here, maybe a mistake?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as comment above, because I wrote the code before. I already did another commit to remove these raises (https://github.com/superzazu/pylibui/commit/4a4f7f6e66db6506a9eef020a372e3595ed61eb5)