Input 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. // Code by: Jeremy Moles (cubicool) 2007-2008
  14. #ifndef OSGWIDGET_INPUT
  15. #define OSGWIDGET_INPUT
  16. #include <osgWidget/Label>
  17. namespace osgWidget {
  18. // This is a string of values we use to try and determine the best Y
  19. // descent value (yoffset); you're welcome to use what works best for
  20. // your font.
  21. const std::string DESCENT_STRING("qpl");
  22. class OSGWIDGET_EXPORT Input: public Label
  23. {
  24. public:
  25. Input(const std::string& = "", const std::string& = "", unsigned int = 20);
  26. virtual void parented (Window*);
  27. virtual void positioned ();
  28. virtual bool focus (const WindowManager*);
  29. virtual bool unfocus (const WindowManager*);
  30. virtual bool keyUp (int, int, const WindowManager*);
  31. virtual bool keyDown (int, int, const WindowManager*);
  32. virtual bool mouseDrag (double, double, const WindowManager*);
  33. virtual bool mousePush (double x, double y, const WindowManager*);
  34. virtual bool mouseRelease (double, double, const WindowManager*);
  35. void setCursor (Widget*);
  36. unsigned int calculateBestYOffset (const std::string& = "qgl");
  37. void clear();
  38. void setXOffset(point_type xo) {
  39. _xoff = xo;
  40. }
  41. void setYOffset(point_type yo) {
  42. _yoff = yo;
  43. }
  44. void setXYOffset(point_type xo, point_type yo) {
  45. _xoff = xo;
  46. _yoff = yo;
  47. }
  48. osg::Drawable* getCursor() {
  49. return _cursor.get();
  50. }
  51. const osg::Drawable* getCursor() const {
  52. return _cursor.get();
  53. }
  54. point_type getXOffset() const {
  55. return _xoff;
  56. }
  57. point_type getYOffset() const {
  58. return _yoff;
  59. }
  60. XYCoord getXYOffset() const {
  61. return XYCoord(_xoff, _yoff);
  62. }
  63. protected:
  64. virtual void _calculateSize(const XYCoord&);
  65. void _calculateCursorOffsets();
  66. point_type _xoff;
  67. point_type _yoff;
  68. unsigned int _index;
  69. unsigned int _size;
  70. unsigned int _cursorIndex;
  71. unsigned int _maxSize;
  72. std::vector<point_type> _offsets;
  73. std::vector<unsigned int> _wordsOffsets;
  74. std::vector<point_type> _widths;
  75. osg::ref_ptr<Widget> _cursor;
  76. bool _insertMode; // Insert was pressed --> true --> typing will overwrite existing text
  77. osg::ref_ptr<Widget> _selection;
  78. unsigned int _selectionStartIndex;
  79. unsigned int _selectionEndIndex;
  80. unsigned int _selectionIndex;
  81. point_type _mouseClickX;
  82. };
  83. }
  84. #endif