Browse Source

support cursor_pos for candidate() functions, and trigger background pgEntry on candidate action

David Rose 22 years ago
parent
commit
169eee50c0

+ 3 - 2
panda/src/display/graphicsWindowInputDevice.cxx

@@ -200,7 +200,8 @@ keystroke(int keycode, double time) {
 ////////////////////////////////////////////////////////////////////
 void GraphicsWindowInputDevice::
 candidate(const wstring &candidate_string, size_t highlight_start, 
-          size_t highlight_end, double time) {
+          size_t highlight_end, size_t cursor_pos) {
   _button_events.push_back(ButtonEvent(candidate_string, 
-                                       highlight_start, highlight_end));
+                                       highlight_start, highlight_end,
+                                       cursor_pos));
 }

+ 2 - 1
panda/src/display/graphicsWindowInputDevice.h

@@ -67,7 +67,8 @@ public:
   void button_up(ButtonHandle button, double time = ClockObject::get_global_clock()->get_frame_time());
   void keystroke(int keycode, double time = ClockObject::get_global_clock()->get_frame_time());
   void candidate(const wstring &candidate_string, size_t highlight_start, 
-                 size_t higlight_end, double time = ClockObject::get_global_clock()->get_frame_time());
+                 size_t higlight_end, size_t cursor_pos);
+
   INLINE void set_pointer_in_window(int x, int y);
   INLINE void set_pointer_out_of_window();
 

+ 5 - 2
panda/src/event/buttonEvent.I

@@ -70,14 +70,15 @@ ButtonEvent(short keycode, double time) :
 ////////////////////////////////////////////////////////////////////
 INLINE ButtonEvent::
 ButtonEvent(const wstring &candidate_string, size_t highlight_start, 
-            size_t highlight_end, double time) :
+            size_t highlight_end, size_t cursor_pos) :
   _button(ButtonHandle::none()),
   _keycode(0),
   _candidate_string(candidate_string),
   _highlight_start(highlight_start),
   _highlight_end(highlight_end),
+  _cursor_pos(cursor_pos),
   _type(T_candidate),
-  _time(time)
+  _time(ClockObject::get_global_clock()->get_frame_time())
 {
 }
 
@@ -93,6 +94,7 @@ ButtonEvent(const ButtonEvent &copy) :
   _candidate_string(copy._candidate_string),
   _highlight_start(copy._highlight_start),
   _highlight_end(copy._highlight_end),
+  _cursor_pos(copy._cursor_pos),
   _type(copy._type),
   _time(copy._time)
 {
@@ -110,6 +112,7 @@ operator = (const ButtonEvent &copy) {
   _candidate_string = copy._candidate_string;
   _highlight_start = copy._highlight_start;
   _highlight_end = copy._highlight_end;
+  _cursor_pos = copy._cursor_pos;
   _type = copy._type;
   _time = copy._time;
 }

+ 2 - 0
panda/src/event/buttonEvent.cxx

@@ -85,6 +85,7 @@ write_datagram(Datagram &dg) const {
                                             TextEncoder::get_default_encoding()));
     dg.add_uint16(_highlight_start);
     dg.add_uint16(_highlight_end);
+    dg.add_uint16(_cursor_pos);
   }
 }
 
@@ -112,5 +113,6 @@ read_datagram(DatagramIterator &scan) {
                                                  TextEncoder::get_default_encoding());
     _highlight_start = scan.get_uint16();
     _highlight_end = scan.get_uint16();
+    _cursor_pos = scan.get_uint16();
   }
 }

+ 2 - 1
panda/src/event/buttonEvent.h

@@ -84,7 +84,7 @@ public:
   INLINE ButtonEvent(ButtonHandle button, Type type, double time = ClockObject::get_global_clock()->get_frame_time());
   INLINE ButtonEvent(short keycode, double time = ClockObject::get_global_clock()->get_frame_time());
   INLINE ButtonEvent(const wstring &candidate_string, size_t highlight_start, 
-                     size_t higlight_end, double time = ClockObject::get_global_clock()->get_frame_time());
+                     size_t highlight_end, size_t cursor_pos);
   INLINE ButtonEvent(const ButtonEvent &copy);
   INLINE void operator = (const ButtonEvent &copy);
 
@@ -112,6 +112,7 @@ public:
   wstring _candidate_string;
   size_t _highlight_start;
   size_t _highlight_end;
+  size_t _cursor_pos;
 
   // This is the type of the button event (see above).
   Type _type;

+ 5 - 1
panda/src/pgui/pgEntry.cxx

@@ -47,6 +47,7 @@ PGEntry(const string &name) :
   _cursor_stale = true;
   _candidate_highlight_start = 0;
   _candidate_highlight_end = 0;
+  _candidate_cursor_pos = 0;
   _max_chars = 0;
   _max_width = 0.0f;
   _num_lines = 1;
@@ -468,7 +469,9 @@ candidate(const MouseWatcherParameter &param, bool background) {
       _candidate_wtext = param.get_candidate_string();
       _candidate_highlight_start = param.get_highlight_start();
       _candidate_highlight_end = param.get_highlight_end();
+      _candidate_cursor_pos = param.get_cursor_pos();
       _text_geom_stale = true;
+      type(param);
     }
   }
   PGItem::candidate(param, background);
@@ -835,10 +838,11 @@ update_cursor() {
     update_text();
 
     _cursor_position = min(_cursor_position, (int)_wtext.length());
+    _candidate_cursor_pos = min(_candidate_cursor_pos, _candidate_wtext.length());
 
     // Determine the row and column of the cursor.
     int row = 0;
-    int column = _cursor_position;
+    int column = _cursor_position + _candidate_cursor_pos;
     while (row + 1 < (int)_ww_lines.size() &&
            column > (int)_ww_lines[row]._str.length()) {
       column -= _ww_lines[row]._str.length();

+ 1 - 0
panda/src/pgui/pgEntry.h

@@ -142,6 +142,7 @@ private:
   wstring _candidate_wtext;
   size_t _candidate_highlight_start;
   size_t _candidate_highlight_end;
+  size_t _candidate_cursor_pos;
 
   int _max_chars;
   float _max_width;

+ 17 - 0
panda/src/pgui/pgItem.cxx

@@ -504,6 +504,23 @@ background_keystroke(const MouseWatcherParameter &param) {
   }
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: PGItem::background_candidate
+//       Access: Public, Static
+//  Description: Calls candidate() on all the PGItems with background
+//               focus.
+////////////////////////////////////////////////////////////////////
+void PGItem::
+background_candidate(const MouseWatcherParameter &param) {
+  BackgroundFocus::const_iterator fi;
+  for (fi = _background_focus.begin(); fi != _background_focus.end(); ++fi) {
+    PGItem *item = *fi;
+    if (!item->get_focus()) {
+      item->candidate(param, true);
+    }
+  }
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: PGItem::set_active
 //       Access: Published, Virtual

+ 1 - 0
panda/src/pgui/pgItem.h

@@ -83,6 +83,7 @@ public:
   static void background_press(const MouseWatcherParameter &param);
   static void background_release(const MouseWatcherParameter &param);
   static void background_keystroke(const MouseWatcherParameter &param);
+  static void background_candidate(const MouseWatcherParameter &param);
 
 PUBLISHED:
   INLINE void set_frame(float left, float right, float bottom, float top);

+ 11 - 0
panda/src/pgui/pgMouseWatcherBackground.cxx

@@ -77,3 +77,14 @@ void PGMouseWatcherBackground::
 keystroke(const MouseWatcherParameter &param) {
   PGItem::background_keystroke(param);
 }
+
+////////////////////////////////////////////////////////////////////
+//     Function: PGMouseWatcherBackground::candidate
+//       Access: Public, Virtual
+//  Description: This is a callback hook function, called whenever
+//               the user uses the IME.
+////////////////////////////////////////////////////////////////////
+void PGMouseWatcherBackground::
+candidate(const MouseWatcherParameter &param) {
+  PGItem::background_candidate(param);
+}

+ 1 - 0
panda/src/pgui/pgMouseWatcherBackground.h

@@ -38,6 +38,7 @@ PUBLISHED:
   virtual void press(const MouseWatcherParameter &param);
   virtual void release(const MouseWatcherParameter &param);
   virtual void keystroke(const MouseWatcherParameter &param);
+  virtual void candidate(const MouseWatcherParameter &param);
 
 public:
   static TypeHandle get_class_type() {

+ 3 - 3
panda/src/tform/mouseWatcher.cxx

@@ -745,9 +745,9 @@ keystroke(int keycode) {
 ////////////////////////////////////////////////////////////////////
 void MouseWatcher::
 candidate(const wstring &candidate_string, size_t highlight_start, 
-          size_t highlight_end) {
+          size_t highlight_end, size_t cursor_pos) {
   MouseWatcherParameter param;
-  param.set_candidate(candidate_string, highlight_start, highlight_end);
+  param.set_candidate(candidate_string, highlight_start, highlight_end, cursor_pos);
   param.set_modifier_buttons(_mods);
   param.set_mouse(_mouse);
 
@@ -1016,7 +1016,7 @@ do_transmit_data(const DataNodeTransmit &input, DataNodeTransmit &output) {
         break;
 
       case ButtonEvent::T_candidate:
-        candidate(be._candidate_string, be._highlight_start, be._highlight_end);
+        candidate(be._candidate_string, be._highlight_start, be._highlight_end, be._cursor_pos);
         break;
 
       case ButtonEvent::T_resume_down:

+ 1 - 1
panda/src/tform/mouseWatcher.h

@@ -140,7 +140,7 @@ protected:
   void release(ButtonHandle button);
   void keystroke(int keycode);
   void candidate(const wstring &candidate, size_t highlight_start, 
-                 size_t highlight_end);
+                 size_t highlight_end, size_t cursor_pos);
                  
   void global_keyboard_press(const MouseWatcherParameter &param);
   void global_keyboard_release(const MouseWatcherParameter &param);

+ 14 - 1
panda/src/tform/mouseWatcherParameter.I

@@ -96,10 +96,12 @@ set_keycode(int keycode) {
 ////////////////////////////////////////////////////////////////////
 INLINE void MouseWatcherParameter::
 set_candidate(const wstring &candidate_string,
-              size_t highlight_start, size_t highlight_end) {
+              size_t highlight_start, size_t highlight_end,
+              size_t cursor_pos) {
   _candidate_string = candidate_string;
   _highlight_start = highlight_start;
   _highlight_end = highlight_end;
+  _cursor_pos = cursor_pos;
   _flags |= F_has_candidate;
 }
 
@@ -257,6 +259,17 @@ get_highlight_end() const {
   return _highlight_end;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: MouseWatcherParameter::get_cursor_pos
+//       Access: Published
+//  Description: Returns the position of the user's edit cursor within
+//               the candidate string.
+////////////////////////////////////////////////////////////////////
+INLINE size_t MouseWatcherParameter::
+get_cursor_pos() const {
+  return _cursor_pos;
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: MouseWatcherParameter::get_modifier_buttons
 //       Access: Published

+ 4 - 1
panda/src/tform/mouseWatcherParameter.h

@@ -43,7 +43,8 @@ public:
   INLINE void set_keycode(int keycode);
   INLINE void set_candidate(const wstring &candidate_string,
                             size_t highlight_start, 
-                            size_t higlight_end);
+                            size_t higlight_end,
+                            size_t cursor_pos);
   INLINE void set_modifier_buttons(const ModifierButtons &mods);
   INLINE void set_mouse(const LPoint2f &mouse);
   INLINE void set_outside(bool flag);
@@ -65,6 +66,7 @@ PUBLISHED:
   INLINE string get_candidate_string_encoded(TextEncoder::Encoding encoding) const;
   INLINE size_t get_highlight_start() const;
   INLINE size_t get_highlight_end() const;
+  INLINE size_t get_cursor_pos() const;
 
   INLINE const ModifierButtons &get_modifier_buttons() const;
 
@@ -81,6 +83,7 @@ public:
   wstring _candidate_string;
   size_t _highlight_start;
   size_t _highlight_end;
+  size_t _cursor_pos;
   ModifierButtons _mods;
   LPoint2f _mouse;