def __update(self): prepinyin_len = len(self.__prepinyin_string) attrs = ibus.AttrList() self.__lookup_table.clean() if prepinyin_len > 0: #attrs.append(ibus.AttributeForeground(0x0000ff, 0, prepinyin_len)) num = im_search(self.__prepinyin_string.encode('utf8')) self.__candidate_num = num self.__lookup_candidate_num = min(num, self.__lookup_table.get_page_size() + 1) for i in range(self.__lookup_candidate_num): text = im_get_candidate(i) self.__lookup_table.append_candidate(ibus.Text(text)) pass pass preedit_string = self.__lookup_table \ and self.__lookup_table.get_number_of_candidates() \ and self.__lookup_table.get_candidate(0).text.decode('utf8') or u"" preedit_len = len(preedit_string) preedit_pos = im_get_fixed_len() self.update_auxiliary_text(ibus.Text(self.__prepinyin_string, attrs), prepinyin_len > 0) attrs.append(ibus.AttributeUnderline(pango.UNDERLINE_SINGLE, 0, preedit_len)) if prepinyin_len > preedit_pos: attrs.append(ibus.AttributeBackground(0xc8c8f0, preedit_pos, preedit_len)) self.update_preedit_text(ibus.Text(preedit_string, attrs), preedit_pos, preedit_len > 0) self.__update_lookup_table() self.__is_invalidate = False
def __update(self): preedit_len = len(self.__preedit_string) attrs = ibus.AttrList() if preedit_len > 0: attrs.append(ibus.AttributeForeground(0xff0000, 0, preedit_len)) self.update_auxiliary_text( ibus.Text(self.__aux_string, ibus.AttrList()), len(self.__aux_string) > 0) attrs.append( ibus.AttributeUnderline(pango.UNDERLINE_SINGLE, 0, preedit_len)) self.update_preedit_text(ibus.Text(self.__preedit_string, attrs), preedit_len, preedit_len > 0) self.__is_invalidate = False
def _update_aux(self): '''Update Aux String in UI''' ''' _ic = self._editor.get_strings () if _ic: res = patt_edit.match (_ic) if res: _ic = u'' ures = patt_uncommit.match (res.group(1)) if ures: _ic = u''.join (ures.groups()) else: _ic += res.group (1) _ic += res.group(2) _ic += res.group(3) if _ic in self._editor._ap_dict: _ic = self._editor.get_aux_strings()+self._editor._ap_dict[_ic] ''' _ic = self._editor.get_aux_strings() ins_str = '' _ic = None if _ic: attrs = ibus.AttrList( [ibus.AttributeForeground(0x9515b5, 0, len(_ic))]) #attrs = [ scim.Attribute(0,len(_ic),scim.ATTR_FOREGROUND,0x5540c1)] super(tabengine, self).update_auxiliary_text(ibus.Text(_ic, attrs), True) else: self.hide_auxiliary_text()
def ap_candidate(self, candi): '''append candidate to lookup_table''' _p_index = self.get_index('phrase') # _fkey = self.get_index('m0') _tbks = u''.join( map(self._deparser, candi[len(self._tabkey_list):_p_index])) _phrase = candi[_p_index] # further color implementation needed :) # here -2 is the pos of num, -1 is the pos of . 0 is the pos of string #attrs = ibus.AttrList ([ibus.AttributeForeground (0x8e2626, -2, 1)]) attrs = ibus.AttrList() # this is the part of tabkey #attrs.append( ibus.AttributeForeground ( 0x1973a2, 0, \ # len(_phrase) + len(_tbks))) if candi[-2] < 0: # this is a user defined phrase: attrs.append(ibus.AttributeForeground(0x7700c3, 0, len(_phrase))) elif candi[-1] > 0: # this is a sys phrase used by user: attrs.append(ibus.AttributeForeground(0x000000, 0, len(_phrase))) else: # this is a system phrase haven't been used: attrs.append(ibus.AttributeForeground(0x000000, 0, len(_phrase))) self._lookup_table.append_candidate(ibus.Text(_phrase, attrs)) self._lookup_table.show_cursor(False)
def _update_preedit(self): '''Update Preedit String in UI''' _str = self._preedit_str if _str == '': super(tabengine, self).update_preedit_text(ibus.Text('', None), 0, False) else: attrs = ibus.AttrList() attrs.append(ibus.AttributeForeground(0x1b3f03, 0, len(_str))) # because ibus now can only insert preedit into txt, so... attrs = ibus.AttrList() attrs.append( ibus.AttributeUnderline(ibus.ATTR_UNDERLINE_SINGLE, 0, len(_str))) super(tabengine, self).update_preedit_text( ibus.Text(_str.decode('utf-8'), attrs), len(_str), True)
def __update(self): preedit_len = len(self.__preedit_string) attrs = ibus.AttrList() self.__lookup_table.clean() if preedit_len > 0: if not self.__dict.check(self.__preedit_string): attrs.append(ibus.AttributeForeground(0xff0000, 0, preedit_len)) for text in self.__dict.suggest(self.__preedit_string): self.__lookup_table.append_candidate(ibus.Text(text)) self.update_auxiliary_text(ibus.Text(self.__preedit_string, attrs), preedit_len > 0) attrs.append( ibus.AttributeUnderline(pango.UNDERLINE_SINGLE, 0, preedit_len)) self.update_preedit_text(ibus.Text(self.__preedit_string, attrs), preedit_len, preedit_len > 0) self.__update_lookup_table() self.__is_invalidate = False
def updateTableEntries(self): #Update our lookup table entries self.__lookup_table.clean() if self.__preedit_string: #Loop through each entry; add its unicode value (for display) words = self.model.getPossibleWords() for word in words: candidate = self.model.getWordKeyStrokes(word, libwaitzar.encoding.unicode) self.__lookup_table.append_candidate(ibus.Text(candidate))
def __check_handled(self, handled, output): if output: self.commit_text(ibus.Text(output)) if handled: gobject.idle_add(self.__tutcode.usrdict.save, priority=gobject.PRIORITY_LOW) self.__update() return True return False
def update_candidates(self, candidates): '''更新候選列表''' self.__lookup_table.clean() self.__lookup_table.show_cursor(False) if not candidates: self.__cand = (0, 0, 0, []) else: for c in candidates: self.__lookup_table.append_candidate(ibus.Text(c[0])) self.__update_page()
def _update_aux(self): '''Update Aux String in UI''' _aux = self._aux_str if _aux: attrs = ibus.AttrList( [ibus.AttributeForeground(0x9515b5, 0, len(_aux))]) super(tabengine, self).update_auxiliary_text( ibus.Text(_aux.decode('utf-8'), attrs), True) else: self.hide_auxiliary_text()
def __update(self): try: #First, update our strings self.updateAuxString() self.updateGuessString() self.updateTableEntries() #Debug: update log #append_log('update called: ' + str([self.__typed_string, self.__preedit_string])) #Cache lengths preedit_len = len(self.__preedit_string) aux_len = len(self.__aux_string) prefix_len = len(self.__prefix_string) guess_len = len(self.__guess_string) #Get an attribute list for the preedit string attrs = ibus.AttrList() if self.__preedit_string: #Put the entire string in red; underline only the current "guessed" part attrs.append(ibus.AttributeForeground(0xff0000, 0, preedit_len)) attrs.append(ibus.AttributeUnderline(pango.UNDERLINE_SINGLE, prefix_len, prefix_len + guess_len)) #Now update all strings self.update_preedit_text(ibus.Text(self.__preedit_string, attrs), prefix_len, preedit_len > 0) #Additional attributes attrs = ibus.AttrList() attrs.append(ibus.AttributeForeground(0x000000, 0, aux_len)) attrs.append(ibus.AttributeUnderline(pango.UNDERLINE_SINGLE, 0, 0)) self.update_auxiliary_text(ibus.Text(self.__aux_string, attrs), aux_len > 0) #Update our lookup table, too self.__lookup_table.set_cursor_pos(self.model.getCurrSelectedID()) self.update_lookup_table(self.__lookup_table, aux_len>0) #Done - now in a valid state self.__is_invalidate = False except: append_error() return False
def update_candidates(self, candidates): '''更新候選列表 ''' self.__lookup_table.clean() self.__lookup_table.show_cursor(False) if not candidates: self.hide_lookup_table() else: for c in candidates: self.__lookup_table.append_candidate(ibus.Text(c[0])) self.update_lookup_table(self.__lookup_table, True, True)
def __lookup_more_candidates(self): if self.__lookup_candidate_num < self.__candidate_num: num = min(self.__candidate_num, self.__lookup_candidate_num + self.__lookup_table.get_page_size() + 1) for i in range(self.__lookup_candidate_num, num): text = im_get_candidate(i) self.__lookup_table.append_candidate(ibus.Text(text)) pass self.__lookup_candidate_num = num self.__update_lookup_table() pass pass
def update_candidates(self, candidates): self.__lookup_table.clean() self.__lookup_table.show_cursor(False) if not candidates: #self.hide_lookup_table() pass else: for c in candidates: self.__lookup_table.append_candidate(ibus.Text(c[0])) #self.update_lookup_table(self.__lookup_table, True, True) self.__candidates = candidates self.__update_lookup_table()
def test(): vkb = VKBProxy(ibus.Bus().get_dbusconn()) print "Send VKB String" vkb.SendText(serializable.serialize_object(ibus.Text("TEST VKB STRING"))) print "Send Retrun Key" vkb.SendKey(keysyms.Return, 0) import gobject loop = gobject.MainLoop() loop.run()
def __update(self): preedit_len = len(self.__preedit_string) attrs = ibus.AttrList() self.__lookup_table.clean() if self.__input_method == "en_US": if preedit_len > 0 : autocompletion_list = self.__get_autocompletion() for text in autocompletion_list: self.__lookup_table.append_candidate(ibus.Text(text)) else: if preedit_len > 0: attrs.append(ibus.AttributeForeground(0xff0000, 0, preedit_len)) candidate_list = self.__keymap.get_candidates(self.__preedit_string) for text in candidate_list: self.__lookup_table.append_candidate(ibus.Text(text)) self.update_auxiliary_text(ibus.Text(self.__preedit_string, attrs), preedit_len > 0) attrs.append(ibus.AttributeUnderline(pango.UNDERLINE_SINGLE, 0, preedit_len)) self.update_preedit_text(ibus.Text(self.__preedit_string, attrs), preedit_len, preedit_len > 0) self.__update_lookup_table() self.__is_invalidate = False
def candidate_clicked(self, index, button, state): try: index = self.__candidate_selector.pos_to_index(index) handled, output = self.__tutcode.select_candidate(index) if handled: if output: self.commit_text(ibus.Text(output)) gobject.idle_add(self.__tutcode.usrdict.save, priority=gobject.PRIORITY_LOW) self.__lookup_table.clean() self.__update() except IndexError: pass
def __update(self): prompt, prefix, word, suffix = self.__tutcode.preedit_components() prefix_start = len(prompt) word_start = prefix_start + len(prefix) suffix_start = word_start + len(word) suffix_end = suffix_start + len(suffix) attrs = ibus.AttrList() # Display "[DictEdit]" way different from other components # (black/lightsalmon). attrs.append( ibus.AttributeForeground(ibus.RGB(0, 0, 0), 0, prefix_start)) attrs.append( ibus.AttributeBackground(ibus.RGB(255, 160, 122), 0, prefix_start)) if self.__tutcode.conv_state == tutcode.CONV_STATE_SELECT: # Use colors from tutcode-henkan-face-default (black/darkseagreen2). attrs.append( ibus.AttributeForeground(ibus.RGB(0, 0, 0), word_start, suffix_start)) attrs.append( ibus.AttributeBackground(ibus.RGB(180, 238, 180), word_start, suffix_start)) attrs.append( ibus.AttributeUnderline(ibus.ATTR_UNDERLINE_SINGLE, suffix_start, suffix_end)) else: attrs.append( ibus.AttributeUnderline(ibus.ATTR_UNDERLINE_SINGLE, word_start, suffix_end)) # Color cursor, currently disabled. # # if self.__tutcode.abbrev: # cursor_color = self.ABBREV_CURSOR_COLOR # else: # cursor_color = self.INPUT_MODE_CURSOR_COLORS.get(\ # self.__tutcode.input_mode) # attrs.append(ibus.AttributeBackground(ibus.RGB(*cursor_color), # suffix_end, suffix_end + 1)) # preedit = ''.join((prompt, prefix, word, suffix, u' ')) # preedit = ''.join((prompt, prefix, word, suffix)) self.update_preedit_text(ibus.Text(preedit, attrs), len(preedit), len(preedit) > 0) visible = self.__candidate_selector.lookup_table_visible() self.update_lookup_table(self.__lookup_table, visible) self.__update_input_mode() if self.__tutcode.conv_state is not tutcode.CONV_STATE_SELECT: gobject.idle_add(self.__possibly_update_config, priority=gobject.PRIORITY_LOW) self.__is_invalidate = False
def _update_preedit(self): '''Update Preedit String in UI''' _str = self._editor.get_preedit_strings() if _str == u'': super(tabengine, self).update_preedit_text(ibus.Text(u'', None), 0, False) else: attrs = ibus.AttrList() res = patt_edit.match(_str) if res: _str = u'' ures = patt_uncommit.match(res.group(1)) if ures: _str = u''.join(ures.groups()) lc = len(ures.group(1)) lu = len(ures.group(2)) attrs.append(ibus.AttributeForeground(0x1b3f03, 0, lc)) attrs.append(ibus.AttributeForeground(0x0895a2, lc, lu)) lg1 = len(_str) else: _str += res.group(1) lg1 = len(res.group(1)) attrs.append(ibus.AttributeForeground(0x1b3f03, 0, lg1)) _str += res.group(2) _str += res.group(3) lg2 = len(res.group(2)) lg3 = len(res.group(3)) attrs.append(ibus.AttributeForeground(0x0e0ea0, lg1, lg2)) attrs.append(ibus.AttributeForeground(0x1b3f03, lg1 + lg2, lg3)) else: attrs.append(ibus.AttributeForeground(0x1b3f03, 0, len(_str))) # attrs = ibus.AttrList() # attrs.append(ibus.AttributeUnderline(ibus.ATTR_UNDERLINE_SINGLE, 0, len(_str))) super(tabengine, self).update_preedit_text(ibus.Text(_str, attrs), self._editor.get_caret(), True)
def update_aux(self, s, start=0, end=0): '''更新輔助串 [start, end) 定義了串中的高亮區間 ''' #logger.debug(u'aux: [%s]' % s) if not s: super(RimeSession, self).hide_auxiliary_text() return length = len(s) attrs = ibus.AttrList() if start < end: attrs.append(ibus.AttributeBackground(ibus.RGB(255, 255, 128), start, end)) attrs.append(ibus.AttributeForeground(ibus.RGB(0, 0, 0), start, end)) t = ibus.Text(s, attrs) super(RimeSession, self).update_auxiliary_text(t, True)
def __init__(self, bus, object_path): super(Engine, self).__init__(bus, object_path) self.__is_invalidate = False labels = [ibus.Text(c + u':') for c in self.__select_keys] page_size = self.config.get_value('page_size') pagination_start = self.config.get_value('pagination_start') self.__lookup_table = ibus.LookupTable(page_size=page_size, round=False, labels=labels) #if hasattr(self.__lookup_table, 'set_orientation'): # self.__lookup_table.set_orientation(ibus.ORIENTATION_HORIZONTAL) self.__candidate_selector = CandidateSelector(self.__lookup_table, self.__select_keys, page_size, pagination_start) if hasattr(self, 'get_surrounding_text'): self.__surrounding_text = SurroundingText(self) else: self.__surrounding_text = None usrdict = skkdict.UsrDict(self.config.usrdict_path) self.__tutcode = tutcode.Context(usrdict, self.sysdict, self.__candidate_selector, self.__surrounding_text) self.__tutcode.tutcode_rule = self.config.get_value('tutcode_rule') self.__initial_input_mode = self.config.get_value('initial_input_mode') self.__use_with_vi = self.config.get_value('use_with_vi') self.__vi_escape_keys = self.config.get_value('vi_escape_keys') self.__tutcode.translated_strings['dict-edit-prompt'] = \ _(u'DictEdit').decode('UTF-8') self.__tutcode.custom_tutcode_rule = \ self.config.get_value('custom_tutcode_rule') self.__tutcode.on_keys = self.config.get_value('on_keys') self.__tutcode.off_keys = self.config.get_value('off_keys') self.__tutcode.cancel_keys = self.config.get_value('cancel_keys') self.__tutcode.backspace_keys = self.config.get_value('backspace_keys') self.__tutcode.conv_keys = self.config.get_value('conv_keys') self.__tutcode.next_keys = self.config.get_value('next_keys') self.__tutcode.prev_keys = self.config.get_value('prev_keys') self.__tutcode.commit_keys = self.config.get_value('commit_keys') self.__tutcode.purge_keys = self.config.get_value('purge_keys') self.__tutcode.reset() self.__tutcode.activate_input_mode(self.__initial_input_mode) self.__prop_dict = dict() self.__prop_list = self.__init_props() self.__input_mode = tutcode.INPUT_MODE_HIRAGANA self.__update_input_mode() self.__suspended_mode = None
def update_preedit(self, s, start=0, end=0): '''更新寫作串 [start, end) 定義了串中的高亮區間 ''' #logger.debug(u'preedit: [%s]' % s) if not s: super(RimeSession, self).hide_preedit_text() return length = len(s) attrs = ibus.AttrList() attrs.append(ibus.AttributeUnderline(ibus.ATTR_UNDERLINE_SINGLE, 0, length)) if start < end: attrs.append(ibus.AttributeBackground(ibus.RGB(255, 255, 128), start, end)) attrs.append(ibus.AttributeForeground(ibus.RGB(0, 0, 0), start, end)) t = ibus.Text(s, attrs) super(RimeSession, self).update_preedit_text(t, length, True)
def _update_lookup_table(self): '''Update Lookup Sdim in UI''' if self._cands_str == '': self.hide_lookup_table() return _cands = self._cands_str.split() _cands = [_str_percent_decode(str) for str in _cands] self._lookup_table.clean() for cand in _cands: self._lookup_table.append_candidate( ibus.Text(cand.decode('utf-8'), None)) index = int(self._cand_idx) % 10 self._lookup_table.set_cursor_pos_in_current_page(index) self._lookup_table.show_cursor(True) self.update_lookup_table(self._lookup_table, True, True)
def fill_lookup_table(self, candidates): self.__lookup_table.clean() for candidate in candidates: self.__lookup_table.append_candidate(ibus.Text(candidate)) self.__lookup_table_hidden = False
def set_candidates(self, candidates): super(CandidateSelector, self).set_candidates(candidates) if len(candidates) > self.pagination_start: self.__lookup_table.clean() for candidate, annotation in candidates[self.pagination_start:]: self.__lookup_table.append_candidate(ibus.Text(candidate))
def _on_commit(self, widget, string): self.commit_text(ibus.Text(string))
def __commit_string(self, text): self.commit_text(ibus.Text(text)) self.__typed_string = u"" self.__update()
def commit_string(self): if self._commit_str == '': return commit = self._commit_str self._commit_str = '' super(tabengine, self).commit_text(ibus.Text(commit.decode('utf-8')))
def process_key_event(self, keyval, keycode, state): # ignore key release events if state & modifier.RELEASE_MASK: return False # ignore alt+key events if state & modifier.MOD1_MASK: return False if self.__tutcode.conv_state == tutcode.CONV_STATE_SELECT: if keyval == keysyms.Page_Up or keyval == keysyms.KP_Page_Up: self.page_up() return True elif keyval == keysyms.Page_Down or keyval == keysyms.KP_Page_Down: self.page_down() return True elif keyval == keysyms.Up or keyval == keysyms.Left: self.__tutcode.previous_candidate(False) self.__update() return True elif keyval == keysyms.Down or keyval == keysyms.Right: self.__tutcode.next_candidate(False) self.__update() return True elif state & modifier.CONTROL_MASK == 0 and \ self.__candidate_selector.lookup_table_visible(): try: index = self.__candidate_selector.\ key_to_index(unichr(keyval).lower()) handled, output = self.__tutcode.select_candidate(index) if handled: if output: self.commit_text(ibus.Text(output)) gobject.idle_add(self.__tutcode.usrdict.save, priority=gobject.PRIORITY_LOW) self.__lookup_table.clean() self.__update() return True except IndexError: pass elif (self.__tutcode.dict_edit_level() > 0 or \ self.__tutcode.conv_state == tutcode.CONV_STATE_START) and \ (state & modifier.CONTROL_MASK) and \ unichr(keyval).lower() in (u'y', u'v'): if unichr(keyval).lower() == u'y': clipboard = clipboard_get("PRIMARY") else: clipboard = clipboard_get("CLIPBOARD") if clipboard: clipboard.request_text(self.__get_clipboard) if keyval == keysyms.Tab: keychr = u'\t' elif keyval == keysyms.Return: keychr = u'return' elif keyval == keysyms.Escape: keychr = u'escape' elif keyval == keysyms.BackSpace: keychr = u'backspace' else: keychr = unichr(keyval) if 0x20 > ord(keychr) or ord(keychr) > 0x7E: # If the pre-edit buffer is visible, always handle key events: # http://github.com/ueno/ibus-skk/issues/#issue/5 return len(self.__tutcode.preedit) > 0 if state & modifier.CONTROL_MASK: # Some systems return 'J' if ctrl:nocaps xkb option is # enabled and the user press CapsLock + 'j': # http://github.com/ueno/ibus-skk/issues/#issue/22 keychr = u'ctrl+' + keychr.lower() if keychr in self.__vi_escape_keys and self.__use_with_vi: self.__tutcode.reset() self.property_activate(u"InputMode.Latin", ibus.PROP_STATE_CHECKED) return False # pass 'escape' to vi return self.__tutcode_press_key(keychr)
def __commit_string(self, text): self.commit_text(ibus.Text(text)) self.__prepinyin_string = u"" self.__prev_char = text and text[-1] or u'' self.__update()