KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
8.4KB

  1. /* This file is part of the KDE project
  2. Copyright 1999-2006 The KSpread Team <koffice-devel@kde.org>
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. * Boston, MA 02110-1301, USA.
  15. */
  16. #ifndef __kspread_editors_h__
  17. #define __kspread_editors_h__
  18. #include <vector>
  19. #include <tqsyntaxhighlighter.h>
  20. #include <tqwidget.h>
  21. #include <kcombobox.h>
  22. #include <klineedit.h>
  23. #include <ksharedptr.h>
  24. class KTextEdit;
  25. class TQFont;
  26. class TQButton;
  27. class TQTextCursor;
  28. namespace KSpread
  29. {
  30. class Canvas;
  31. class Cell;
  32. class CellEditor;
  33. class LocationEditWidget;
  34. class Region;
  35. class Sheet;
  36. class Tokens;
  37. class View;
  38. /**
  39. * Colours cell references in formulas. Installed by CellEditor instances in
  40. * the constructor.
  41. */
  42. class FormulaEditorHighlighter : public TQSyntaxHighlighter
  43. {
  44. public:
  45. /**
  46. * Constructs a FormulaHighlighter to colour-code cell references in a TQTextEdit.
  47. *
  48. * @param textEdit The TQTextEdit widget which the highlighter should operate on
  49. * @param canvas The Canvas object
  50. */
  51. FormulaEditorHighlighter(TQTextEdit* textEdit, Canvas* canvas);
  52. virtual ~FormulaEditorHighlighter();
  53. /**
  54. * Called automatically by KTextEditor to highlight text when modified.
  55. */
  56. virtual int highlightParagraph(const TQString& text, int endStateOfLastPara);
  57. /**
  58. *
  59. */
  60. const Tokens& formulaTokens() const;
  61. /**
  62. *
  63. */
  64. uint rangeCount() const;
  65. /**
  66. * Returns true if any of the ranges or cells in the formula have changed since the
  67. * last call to @ref FormulaEditorHighlighter::rangeChanged()
  68. */
  69. bool rangeChanged() const;
  70. /**
  71. * Sets the highlighter's range changed flag to false.
  72. */
  73. void resetRangeChanged();
  74. protected:
  75. /**
  76. * Returns the position of the brace matching the one found at position pos
  77. */
  78. int findMatchingBrace(int pos);
  79. /**
  80. * Examines the brace (Token::LeftPar or Token::RightPar) operator token at the given index in the token vector
  81. * ( as returned by formulaTokens() ) and if the cursor is next to it, the token plus any matching brace will be highlighted
  82. */
  83. void handleBrace(uint index);
  84. private:
  85. class Private;
  86. Private* d;
  87. };
  88. /**
  89. * Provides autocompletition facilities in formula editors.
  90. * When the user types in the first few characters of a
  91. * function name in a CellEditor which has a FunctionCompletion
  92. * object installed on it, the FunctionCompletion object
  93. * creates and displays a list of possible names which the user
  94. * can select from. If the user selects a function name from the list,
  95. * the @ref FunctionCompletion::selectedCompletion() signal is emitted
  96. */
  97. class FunctionCompletion : public TQObject
  98. {
  99. Q_OBJECT
  100. public:
  101. FunctionCompletion( CellEditor* editor );
  102. ~FunctionCompletion();
  103. /**
  104. * Handles various keyboard and mouse actions which may occur on the autocompletion popup list
  105. */
  106. bool eventFilter( TQObject *o, TQEvent *e );
  107. /**
  108. * Hides the autocompletion list box if it is visible and emits the @ref selectedCompletion signal.
  109. */
  110. void doneCompletion();
  111. /**
  112. * Populates the autocompletion list box with the specified choices and shows it so that the user can view and select a function name.
  113. * @param choices A list of possible function names which match the characters that the user has already entered.
  114. */
  115. void showCompletion( const TQStringList &choices );
  116. private slots:
  117. void itemSelected( const TQString& item );
  118. signals:
  119. /**
  120. * Emitted, if the user selects a function name from the list.
  121. */
  122. void selectedCompletion( const TQString& item );
  123. private:
  124. class Private;
  125. Private* d;
  126. FunctionCompletion( const FunctionCompletion& );
  127. FunctionCompletion& operator=( const FunctionCompletion& );
  128. };
  129. /**
  130. * class CellEditor
  131. */
  132. class CellEditor : public TQWidget
  133. {
  134. Q_OBJECT
  135. public:
  136. /**
  137. * Creates a new CellEditor.
  138. * @param cell The spreadsheet cell to associate the cell text editor with
  139. * @param _parent The @ref Canvas object to associate this cell text editor with
  140. * @param captureAllKeyEvents Controls whether or not the text editor swallows arrow key events or sends them to the parent canvas instead. If this is set to true, pressing the arrow keys will navigate backwards and forwards through the text in the editor. If it is false, the key events will be sent to the parent canvas which will change the cell being edited (depending on the direction of the arrow pressed). Generally this should be set to true if the user double clicks on the cell to edit it, and false if the user initiates editing by typing whilst the cell is selected.
  141. * @param _name This parameter is sent to the TQObject constructor
  142. */
  143. CellEditor( Cell* cell, Canvas* _parent = 0, bool captureAllKeyEvents = false, const char* _name = 0 );
  144. ~CellEditor();
  145. Cell* cell() const;
  146. Canvas* canvas() const;
  147. void handleKeyPressEvent( TQKeyEvent* _ev );
  148. void handleIMEvent( TQIMEvent * _ev );
  149. void setEditorFont(TQFont const & font, bool updateSize);
  150. int cursorPosition() const;
  151. void setCursorPosition(int pos);
  152. void setText(TQString text);
  153. /** wrapper to KTextEdit::text() */
  154. TQString text() const;
  155. /** wrapper to KTextEdit::cut() */
  156. void cut();
  157. /** wrapper to KTextEdit::paste() */
  158. void paste();
  159. /** wrapper to KTextEdit::copy() */
  160. void copy();
  161. TQPoint globalCursorPosition() const;
  162. bool checkChoice();
  163. void setCheckChoice(bool b);
  164. void updateChoice();
  165. void setUpdateChoice(bool);
  166. void setCursorToRange(uint);
  167. private slots:
  168. void slotTextChanged();
  169. void slotCompletionModeChanged(TDEGlobalSettings::Completion _completion);
  170. void slotCursorPositionChanged(int para,int pos);
  171. void slotTextCursorChanged(TQTextCursor*);
  172. protected:
  173. void resizeEvent( TQResizeEvent* );
  174. /**
  175. * Steals some key events from the TQLineEdit and sends
  176. * it to the @ref Canvas ( its parent ) instead.
  177. */
  178. bool eventFilter( TQObject* o, TQEvent* e );
  179. protected slots:
  180. void checkFunctionAutoComplete();
  181. void triggerFunctionAutoComplete();
  182. void functionAutoComplete( const TQString& item );
  183. private:
  184. class Private;
  185. Private* d;
  186. };
  187. /**
  188. * ComboboxLocationEditWidget
  189. */
  190. class ComboboxLocationEditWidget : public KComboBox
  191. {
  192. Q_OBJECT
  193. public:
  194. ComboboxLocationEditWidget( TQWidget *_parent, View * _canvas );
  195. public slots:
  196. void slotAddAreaName( const TQString & );
  197. void slotRemoveAreaName( const TQString & );
  198. private:
  199. LocationEditWidget *m_locationWidget;
  200. };
  201. /**
  202. * A widget that allows the user to enter an arbitrary
  203. * cell location to goto or cell selection to highlight
  204. */
  205. class LocationEditWidget : public KLineEdit
  206. {
  207. Q_OBJECT
  208. public:
  209. LocationEditWidget( TQWidget *_parent, View * _canvas );
  210. View * view() const { return m_pView;}
  211. void addCompletionItem( const TQString &_item );
  212. void removeCompletionItem( const TQString &_item );
  213. private slots:
  214. void slotActivateItem();
  215. protected:
  216. virtual void keyPressEvent( TQKeyEvent * _ev );
  217. private:
  218. View * m_pView;
  219. TDECompletion completionList;
  220. bool activateItem();
  221. };
  222. /**
  223. * The widget that appears above the sheet and allows to
  224. * edit the cells content.
  225. */
  226. class EditWidget : public TQLineEdit
  227. {
  228. Q_OBJECT
  229. public:
  230. EditWidget( TQWidget *parent, Canvas *canvas,
  231. TQButton *cancelButton, TQButton *okButton);
  232. virtual void setText( const TQString& t );
  233. // Go into edit mode (enable the buttons)
  234. void setEditMode( bool mode );
  235. void showEditWidget(bool _show);
  236. public slots:
  237. void slotAbortEdit();
  238. void slotDoneEdit();
  239. protected:
  240. virtual void keyPressEvent ( TQKeyEvent* _ev );
  241. virtual void focusOutEvent( TQFocusEvent* ev );
  242. private:
  243. TQButton* m_pCancelButton;
  244. TQButton* m_pOkButton;
  245. Canvas* m_pCanvas;
  246. bool isArray;
  247. };
  248. } // namespace KSpread
  249. #endif