Add round lookup table support.

This commit is contained in:
Huang Peng 2009-02-09 10:28:19 +08:00
Родитель c9dab029a3
Коммит 30bff48671
4 изменённых файлов: 48 добавлений и 12 удалений

Просмотреть файл

@ -29,10 +29,11 @@ from exception import *
class LookupTable(Serializable):
__NAME__ = "IBusLookupTable"
def __init__(self, page_size=5, cursor_pos=0, coursor_visible=True, candidates=None):
def __init__(self, page_size=5, cursor_pos=0, coursor_visible=True, round=False, candidates=None):
super(LookupTable, self).__init__()
self.__cursor_visible = True
self.__cursor_pos = cursor_pos
self.__cursor_visible = True
self.__round = round
if candidates == None:
self.__candidates = list()
else:
@ -94,7 +95,15 @@ class LookupTable(Serializable):
def page_up(self):
if self.__cursor_pos < self.__page_size:
return False
if self.__round:
nr_candidates = len(self.__candidates)
max_page = nr_candidates / self.__page_size
self.__cursor_pos += max_page * self.__page_size
if self.__cursor_pos > nr_candidates - 1:
self.__cursor_pos = nr_candidates - 1
return True
else:
return False
self.__cursor_pos -= self.__page_size
return True
@ -105,25 +114,37 @@ class LookupTable(Serializable):
max_page = nr_candidates / self.__page_size
if current_page >= max_page:
return False
if self.__round:
self.__cursor_pos %= self.__page_size
return True
else:
return False
pos = self.__cursor_pos + self.__page_size
if pos >= nr_candidates:
return False
pos = nr_candidates - 1
self.__cursor_pos = pos
return True
def cursor_up(self):
if self.__cursor_pos == 0:
return False
if self.__round:
self.__cursor_pos = len(self.__candidates) - 1
return True
else:
return False
self.__cursor_pos -= 1
return True
def cursor_down(self):
if self.__cursor_pos == len(self.__candidates) - 1:
return False
if self.__round:
self.__cursor_pos = 0
return True
else:
return False
self.__cursor_pos += 1
return True
@ -158,6 +179,7 @@ class LookupTable(Serializable):
struct.append(dbus.UInt32(self.__page_size))
struct.append(dbus.UInt32(self.__cursor_pos))
struct.append(dbus.Boolean(self.__cursor_visible))
struct.append(dbus.Boolean(self.__round))
candidates = map(lambda c: serialize_object(c), self.__candidates)
struct.append(dbus.Array(candidates, signature="v"))
@ -166,6 +188,7 @@ class LookupTable(Serializable):
return LookupTable(self.__page_size,
self.__cursor_pos % self.__page_size,
self.__cursor_visible,
self.__round,
candidates)
def deserialize(self, struct):
@ -174,6 +197,7 @@ class LookupTable(Serializable):
self.__page_size = struct.pop(0)
self.__cursor_pos = struct.pop(0)
self.__cursor_visible = struct.pop(0)
self.__round = struct.pop(0)
self.__candidates = map(deserialize_object, struct.pop(0))

Просмотреть файл

@ -73,7 +73,7 @@ ibus_lookup_table_class_init (IBusLookupTableClass *klass)
serializable_class->deserialize = (IBusSerializableDeserializeFunc) ibus_lookup_table_deserialize;
serializable_class->copy = (IBusSerializableCopyFunc) ibus_lookup_table_copy;
g_string_append (serializable_class->signature, "uubav");
g_string_append (serializable_class->signature, "uubbav");
}
static void
@ -121,6 +121,9 @@ ibus_lookup_table_serialize (IBusLookupTable *table,
retval = ibus_message_iter_append (iter, G_TYPE_BOOLEAN, &table->cursor_visible);
g_return_val_if_fail (retval, FALSE);
retval = ibus_message_iter_append (iter, G_TYPE_BOOLEAN, &table->round);
g_return_val_if_fail (retval, FALSE);
retval = ibus_message_iter_open_container (iter,
IBUS_TYPE_ARRAY,
@ -165,6 +168,9 @@ ibus_lookup_table_deserialize (IBusLookupTable *table,
retval = ibus_message_iter_get (iter, G_TYPE_BOOLEAN, &table->cursor_visible);
g_return_val_if_fail (retval, FALSE);
retval = ibus_message_iter_get (iter, G_TYPE_BOOLEAN, &table->round);
g_return_val_if_fail (retval, FALSE);
retval = ibus_message_iter_recurse (iter, IBUS_TYPE_ARRAY, &array_iter);
g_return_val_if_fail (retval, FALSE);
@ -213,15 +219,18 @@ ibus_lookup_table_copy (IBusLookupTable *dest,
IBusLookupTable *
ibus_lookup_table_new (guint page_size,
gboolean cursor_visible)
guint cursor_pos,
gboolean cursor_visible,
gboolean round)
{
IBusLookupTable *table;
table= g_object_new (IBUS_TYPE_LOOKUP_TABLE, NULL);
table->cursor_pos = 0;
table->page_size = page_size;
table->cursor_pos = cursor_pos;
table->cursor_visible = cursor_visible;
table->round = round;
return table;
}

Просмотреть файл

@ -52,6 +52,7 @@ struct _IBusLookupTable {
guint page_size;
guint cursor_pos;
gboolean cursor_visible;
gboolean round;
GArray *candidates;
};
@ -63,7 +64,9 @@ struct _IBusLookupTableClass {
GType ibus_lookup_table_get_type (void);
IBusLookupTable *ibus_lookup_table_new (guint page_size,
gboolean cursor_visible);
guint cursor_pos,
gboolean cursor_visible,
gboolean round);
void ibus_lookup_table_append_candidate
(IBusLookupTable *table,
IBusText *text);

Просмотреть файл

@ -8,7 +8,7 @@ int main()
IBusError *error;
gboolean retval;
table = ibus_lookup_table_new (9, TRUE);
table = ibus_lookup_table_new (9, 0, TRUE, FALSE);
ibus_lookup_table_append_candidate (table, ibus_text_new_from_static_string ("Hello"));
ibus_lookup_table_append_candidate (table, ibus_text_new_from_static_string ("Cool"));