Simplify surrounding-text initialization.
authorDaiki Ueno <daiki.ueno@gmail.com>
Sat, 18 Jun 2011 04:03:07 +0000 (00:03 -0400)
committerPeng Huang <shawn.p.huang@gmail.com>
Sat, 18 Jun 2011 04:03:07 +0000 (00:03 -0400)
Currently the immodule tries to retrieve surrounding-text unconditionally
on focus_in and enabled.  These calls could be eliminated if engine were
able to proclaim that it will need surrounding-text.

This patch extends ibus_engine_get_surrounding_text() to allow this.
Engines that need surrounding-text are expected to have:

    /* Indicate we will use surrounding-text. */
    ibus_engine_get_surrounding_text (engine, NULL, NULL);

in their enable() method.  This would work because enable() is called before
SetCapabilities DBus call.

BUG=none
TEST=manually with ibus-m17n, with the above change.

Review URL: http://codereview.appspot.com/4613043
Patch from Daiki Ueno <daiki.ueno@gmail.com>.

client/gtk2/ibusimcontext.c
src/ibusengine.c
src/ibusengine.h

index ec764ef..a4e7a16 100644 (file)
@@ -147,8 +147,7 @@ static gboolean _slave_delete_surrounding_cb
                                              gint                offset_from_cursor,
                                              guint               nchars,
                                              IBusIMContext      *context);
-static void     _request_surrounding_text   (IBusIMContext      *context,
-                                             gboolean            force);
+static void     _request_surrounding_text   (IBusIMContext      *context);
 static void     _create_fake_input_context  (void);
 
 
@@ -268,17 +267,13 @@ _process_key_event_done (GObject      *object,
 /* emit "retrieve-surrounding" glib signal of GtkIMContext, if
  * context->caps has IBUS_CAP_SURROUNDING_TEXT and the current IBus
  * engine needs surrounding-text.
- *
- * if "force" is TRUE, emit the signal regardless of whether the
- * engine needs surrounding-text.
  */
 static void
-_request_surrounding_text (IBusIMContext *context, gboolean force)
+_request_surrounding_text (IBusIMContext *context)
 {
     if (context && context->enable &&
         (context->caps & IBUS_CAP_SURROUNDING_TEXT) != 0 &&
-        (force ||
-         ibus_input_context_needs_surrounding_text (context->ibuscontext))) {
+        ibus_input_context_needs_surrounding_text (context->ibuscontext)) {
         gboolean return_value;
         IDEBUG ("requesting surrounding text");
         g_signal_emit (context, _signal_retrieve_surrounding_id, 0,
@@ -374,7 +369,7 @@ _key_snooper_cb (GtkWidget   *widget,
     } while (0);
 
     if (ibusimcontext != NULL) {
-        _request_surrounding_text (ibusimcontext, FALSE);
+        _request_surrounding_text (ibusimcontext);
         ibusimcontext->time = event->time;
     }
 
@@ -684,7 +679,7 @@ ibus_im_context_filter_keypress (GtkIMContext *context,
         if (ibusimcontext->client_window == NULL && event->window != NULL)
             gtk_im_context_set_client_window ((GtkIMContext *)ibusimcontext, event->window);
 
-        _request_surrounding_text (ibusimcontext, FALSE);
+        _request_surrounding_text (ibusimcontext);
 
         if (ibusimcontext != NULL) {
             ibusimcontext->time = event->time;
@@ -767,7 +762,7 @@ ibus_im_context_focus_in (GtkIMContext *context)
 
     /* retrieve the initial surrounding-text (regardless of whether
      * the current IBus engine needs surrounding-text) */
-    _request_surrounding_text (ibusimcontext, TRUE);
+    _request_surrounding_text (ibusimcontext);
 
     g_object_add_weak_pointer ((GObject *) context,
                                (gpointer *) &_focus_im_context);
@@ -1005,7 +1000,7 @@ _ibus_context_commit_text_cb (IBusInputContext *ibuscontext,
 
     g_signal_emit (ibusimcontext, _signal_commit_id, 0, text->text);
 
-    _request_surrounding_text (ibusimcontext, FALSE);
+    _request_surrounding_text (ibusimcontext);
 }
 
 static gboolean
@@ -1301,7 +1296,7 @@ _ibus_context_show_preedit_text_cb (IBusInputContext   *ibuscontext,
     g_signal_emit (ibusimcontext, _signal_preedit_start_id, 0);
     g_signal_emit (ibusimcontext, _signal_preedit_changed_id, 0);
 
-    _request_surrounding_text (ibusimcontext, FALSE);
+    _request_surrounding_text (ibusimcontext);
 }
 
 static void
@@ -1328,7 +1323,7 @@ _ibus_context_enabled_cb (IBusInputContext *ibuscontext,
 
     /* retrieve the initial surrounding-text (regardless of whether
      * the current IBus engine needs surrounding-text) */
-    _request_surrounding_text (ibusimcontext, TRUE);
+    _request_surrounding_text (ibusimcontext);
 }
 
 static void
index f545bef..620d07f 100644 (file)
@@ -1382,13 +1382,15 @@ ibus_engine_get_surrounding_text (IBusEngine   *engine,
     IBusEnginePrivate *priv;
 
     g_return_if_fail (IBUS_IS_ENGINE (engine));
-    g_return_if_fail (text != NULL);
-    g_return_if_fail (cursor_pos != NULL);
+    g_return_if_fail ((text != NULL && cursor_pos != NULL) ||
+                      (text == NULL && cursor_pos == NULL));
 
     priv = IBUS_ENGINE_GET_PRIVATE (engine);
 
-    *text = g_object_ref (priv->surrounding_text);
-    *cursor_pos = priv->surrounding_cursor_pos;
+    if (text && cursor_pos) {
+        *text = g_object_ref (priv->surrounding_text);
+        *cursor_pos = priv->surrounding_cursor_pos;
+    }
 
     /* tell the client that this engine will utilize surrounding-text
      * feature, which causes periodical update.  Note that the client
index 29b8f1d..6da342a 100644 (file)
@@ -407,11 +407,16 @@ void ibus_engine_delete_surrounding_text(IBusEngine         *engine,
 /**
  * ibus_engine_get_surrounding_text:
  * @engine: An IBusEngine.
- * @text: Location to store surrounding text.
- * @cursor_pos: Cursor position in characters in @text.
+ * @text: (allow-none): Location to store surrounding text.
+ * @cursor_pos: (allow-none): Cursor position in characters in @text.
  *
  * Get surrounding text.
  *
+ * It is also used to tell the input-context that the engine will
+ * utilize surrounding-text.  In that case, it must be called in
+ * #IBusEngine::enable handler, with both @text and @cursor set to
+ * %NULL.
+ *
  * @see_also #IBusEngine::set-surrounding-text
  */
 void ibus_engine_get_surrounding_text(IBusEngine         *engine,