summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/input_method/ibus_ui_controller.h
blob: 888149092efede0ffe58e1b434641d2d157cb0be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// The header files provides APIs for monitoring and controlling input
// method UI status. The APIs encapsulate the APIs of IBus, the underlying
// input method framework.

#ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_IBUS_UI_CONTROLLER_H_
#define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_IBUS_UI_CONTROLLER_H_
#pragma once

#include <sstream>
#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/observer_list.h"

namespace chromeos {
namespace input_method {

// A key for attaching the |ibus_service_panel_| object to |ibus_|.
const char kPanelObjectKey[] = "panel-object";

// The struct represents the input method lookup table (list of candidates).
// Used for InputMethodUpdateLookupTableMonitorFunction.
struct InputMethodLookupTable {
  enum Orientation {
    kVertical,
    kHorizontal,
  };

  InputMethodLookupTable()
      : visible(false),
        cursor_absolute_index(0),
        page_size(0),
        orientation(kHorizontal) {
  }

  // Returns a string representation of the class. Used for debugging.
  // The function has to be defined here rather than in the .cc file.  If
  // it's defined in the .cc file, the code will be part of libcros.so,
  // which cannot be accessed from clients directly. libcros.so is loaded
  // by dlopen() so all functions are unbound unless explicitly bound by
  // dlsym().
  std::string ToString() const {
    std::stringstream stream;
    stream << "visible: " << visible << "\n";
    stream << "cursor_absolute_index: " << cursor_absolute_index << "\n";
    stream << "page_size: " << page_size << "\n";
    stream << "orientation: " << orientation << "\n";
    stream << "candidates:";
    for (size_t i = 0; i < candidates.size(); ++i) {
      stream << " [" << candidates[i] << "]";
    }
    stream << "\nlabels:";
    for (size_t i = 0; i < labels.size(); ++i) {
      stream << " [" << labels[i] << "]";
    }
    return stream.str();
  }

  // True if the lookup table is visible.
  bool visible;

  // Zero-origin index of the current cursor position in the all
  // candidates. If the cursor is pointing to the third candidate in the
  // second page when the page size is 10, the value will be 12 as it's
  // 13th candidate.
  int cursor_absolute_index;

  // Page size is the max number of candidates shown in a page. Usually
  // it's about 10, depending on the backend conversion engine.
  int page_size;

  // Candidate strings in UTF-8.
  std::vector<std::string> candidates;

  // The orientation of the candidates in the candidate window.
  Orientation orientation;

  // Label strings in UTF-8 (ex. "1", "2", "3", ...).
  std::vector<std::string> labels;

  // Annotation strings in UTF-8 (ex. "Hankaku Katakana").
  std::vector<std::string> annotations;
};

// IBusUiController is used to interact with the IBus daemon.
class IBusUiController {
 public:
  class Observer {
   public:
    // Called when the auxiliary text becomes hidden.
    virtual void OnHideAuxiliaryText() = 0;

    // Called when the lookup table becomes hidden.
    virtual void OnHideLookupTable() = 0;

    // Called when the preedit text becomes hidden.
    virtual void OnHidePreeditText() = 0;

    // Called when the cursor location is set.
    virtual void OnSetCursorLocation(int x, int y, int width, int height) = 0;

    // Called when the auxiliary text is updated.
    virtual void OnUpdateAuxiliaryText(const std::string& text,
                                       bool visible) = 0;

    // Called when the lookup table is updated.
    virtual void OnUpdateLookupTable(const InputMethodLookupTable& table) = 0;

    // Called when the preedit text is updated.
    virtual void OnUpdatePreeditText(const std::string& utf8_text,
                                     unsigned int cursor, bool visible) = 0;

    // Called when the connection is changed. |connected| is true if the
    // connection is established, and false if the connection is closed.
    virtual void OnConnectionChange(bool connected) = 0;
  };

  // Creates an instance of the class. The constructor is unused.
  static IBusUiController* Create();

  virtual ~IBusUiController();

  // Connects to the IBus daemon.
  virtual void Connect() = 0;

  // Adds and removes observers for IBus UI notifications. Clients must be
  // sure to remove the observer before they go away. To capture the
  // initial connection change, you should add an observer before calling
  // Connect().
  virtual void AddObserver(Observer* observer) = 0;
  virtual void RemoveObserver(Observer* observer) = 0;

  // Notifies that a candidate is clicked. |CandidateClicked| signal will be
  // sent to the ibus-daemon.
  //
  // - |index| Index in the Lookup table. The semantics is same with
  //   |cursor_absolute_index|.
  // - |button| GdkEventButton::button (1: left button, etc.)
  // - |state|  GdkEventButton::state (key modifier flags)
  virtual void NotifyCandidateClicked(int index, int button, int flags) = 0;

  // Notifies that the cursor up button is clicked. |CursorUp| signal will be
  // sent to the ibus-daemon
  virtual void NotifyCursorUp() = 0;

  // Notifies that the cursor down button is clicked. |CursorDown| signal
  // will be sent to the ibus-daemon
  virtual void NotifyCursorDown() = 0;

  // Notifies that the page up button is clicked. |PageUp| signal will be
  // sent to the ibus-daemon
  virtual void NotifyPageUp() = 0;

  // Notifies that the page down button is clicked. |PageDown| signal will be
  // sent to the ibus-daemon
  virtual void NotifyPageDown() = 0;
};

}  // namespace input_method
}  // namespace chromeos

#endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_IBUS_UI_CONTROLLER_H_