MySQL Workbench  6.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
code_editor.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; version 2 of the
7  * License.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 
20 #ifndef _CODE_EDITOR_H_
21 #define _CODE_EDITOR_H_
22 
23 #ifndef SCI_NAMESPACE
24 #define SCI_NAMESPACE
25 #endif
26 #include "Scintilla.h"
27 
28 #include <mforms/view.h>
29 #include <mforms/utilities.h>
30 
35 class TiXmlDocument;
36 class TiXmlElement;
37 
38 namespace mforms {
39 
40  class CodeEditor;
41  class Menu;
42  class FindPanel;
43 
50  LanguageHtml, // includes embedded xml, javascript, php, vb, python
53  LanguageCpp, // Lexer for C++, C, Java, and JavaScript (which includes JSON).
54 
55  LanguageMySQL = LanguageMySQL56, // Always the latest language.
56  };
57 
61  enum LineMarkup {
62  LineMarkupNone = 0, // No markup for the given line.
63  LineMarkupBreakpoint = 1 << 0, // Line has a marker set for a break point.
64  LineMarkupBreakpointHit = 1 << 1, // Line has a marker set for a break point which is currently hit.
65  LineMarkupError = 1 << 2, // Line's background is drawn in red to mark an execution error.
66  LineMarkupStatement = 1 << 3, // Marks a line as having a statement starting on it.
67  LineMarkupCurrent = 1 << 4, // Current execution line.
68 
69  LineMarkupAll = 0xFF, // All markup, useful for remove_markup.
70  };
71 
72 #ifndef SWIG
74  {
75  return (LineMarkup) ((int) a | (int) b);
76  }
77 #endif
78 
79  // Indicators for a portion of text. Can span more than a single line or only part of a line.
82  RangeIndicatorError = 1 << 0, // Red squiggles under a range of text.
83  };
84 
87  FeatureWrapText = 1 << 0, // Enables word wrapping.
88  FeatureGutter = 1 << 1, // Show/Hide gutter.
89  FeatureReadOnly = 1 << 2,
90  FeatureShowSpecial = 1 << 3, // Show white spaces and line ends with special chars.
91  FeatureUsePopup = 1 << 4, // Use built-in context menu.
92  FeatureConvertEolOnPaste = 1 << 5, // Convert line endings to the current value in the editor
93  // when pasting text.
94  FeatureScrollOnResize = 1 << 6, // Scroll caret into view if it would be hidden by a resize action.
95  FeatureFolding = 1 << 7, // Enable code folding.
96 
97  FeatureAll = 0xFFFF,
98  };
99 
100 #ifndef SWIG
102  {
103  return (CodeEditorFeature) ((int) a | (int) b);
104  }
105 #endif
106 
108  AutoCompletionSelection, // The user selected an entry in the auto completion list.
109  AutoCompletionCancelled, // Auto completion was cancelled.
110  AutoCompletionCharDeleted, // A character was deleted while auto completion was active.
111  };
112 
114  EolCRLF = 0,
115  EolCR = 1,
116  EolLF = 2, // Default
117  };
118 
119  enum FindFlags {
121  FindMatchCase = (1 << 0),
122  FindWrapAround = (1 << 1),
123  FindWholeWords = (1 << 2),
124  FindRegex = (1 << 3)
125  };
126 
127 #ifndef SWIG
129  {
130  return (FindFlags) ((int) a | (int) b);
131  }
132 
134  {
135  a = (FindFlags)((int) a | (int) b);
136  return a;
137  }
138 #endif
139 
140 #ifndef SWIG
141 
145 {
146 private:
147  std::vector<std::string> _languages;
149 
150  std::map<std::string, std::string> _keywords;
151  std::map<std::string, std::string> _properties;
152  std::map<std::string, std::string> _settings;
153  std::map<int, std::map<std::string, std::string> > _styles;
154 
155  TiXmlDocument *_document;
156  TiXmlElement *_language_element;
157 protected:
158  void parse_properties();
159  void parse_settings();
160  void parse_keywords();
161  void parse_styles();
162 
163 public:
165  ~CodeEditorConfig();
166 
167  std::vector<std::string> get_languages() { return _languages; };
168 
169  // TODO: add setters when customization is required.
170  std::map<std::string, std::string> get_keywords() { return _keywords; };
171  std::map<std::string, std::string> get_properties() { return _properties; };
172  std::map<std::string, std::string> get_settings() { return _settings; };
173  std::map<int, std::map<std::string, std::string> > get_styles() { return _styles; };
174 };
175 #endif // !SWIG
176 
177 #ifndef DOXYGEN_SHOULD_SKIP_THIS
178 #ifndef SWIG
179  struct CodeEditorImplPtrs
180  {
181  bool (*create)(CodeEditor* self);
182  sptr_t (*send_editor)(CodeEditor* self, unsigned int message, uptr_t wParam, sptr_t lParam);
183  void (*set_status_text)(CodeEditor* self, const std::string &text);
184  };
185 #endif
186 #endif
187 
189  {
190  public:
191  CodeEditor(void *host = NULL);
192  ~CodeEditor();
193 
195  void setup();
196 
198  void set_text(const char* text);
199 
200  void set_value(const std::string& text);
201 
205  void set_text_keeping_state(const char* text);
206 
210  void append_text(const char* text, int length);
211 
214  void replace_selected_text(const std::string& text);
215 
220  const std::string get_text(bool selection_only);
221  virtual std::string get_string_value() { return get_text(false); };
222 
226  const std::string get_text_in_range(int start, int end);
227 
236  std::pair<const char*, size_t> get_text_ptr();
237 
242  void set_selection(int start, int length);
243 
245  void get_selection(int &start, int &length);
246 
248  void clear_selection();
249 
251  bool get_range_of_line(int line, int &start, int &end);
252 
254  void set_language(SyntaxHighlighterLanguage language);
255 
257  void show_markup(LineMarkup markup, int line);
258 
264  void remove_markup(LineMarkup markup, int line);
265 
267  void show_indicator(RangeIndicator indicator, int start, int length);
268 
270  RangeIndicator indicator_at(int position);
271 
273  void remove_indicator(RangeIndicator indicator, int start, int length);
274 
276  int line_count();
277 
279  int text_length();
280 
282  int position_from_line(int line_number);
283 
285  int line_from_position(int position);
286 
287  void set_font(const std::string &fontDescription); // e.g. "Trebuchet MS bold 9"
288 
290  void set_features(CodeEditorFeature features, bool flag);
291 
293  void toggle_features(CodeEditorFeature features);
294 
296  void reset_dirty();
297  bool is_dirty();
298 
300  int get_caret_pos();
301  void set_caret_pos(int position);
302 
304  void get_line_column_pos(int position, int &line, int &column);
305 
307  bool can_undo();
308  void undo();
309  bool can_redo();
310  void redo();
311  bool can_cut();
312  void cut();
313  bool can_copy();
314  void copy();
315  bool can_paste();
316  void paste();
317  bool can_delete();
318  void do_delete();
319  void select_all();
320 
322  void set_status_text(const std::string& text);
323 
324  // ----- Find and replace
325  void show_find_panel(bool replace);
326  void hide_find_panel();
327  FindPanel* get_find_panel() { return _find_panel; };
328 
333  void set_show_find_panel_callback(boost::function<void (CodeEditor*, bool)> callback);
334 
337  bool find_and_highlight_text(const std::string& search_text, FindFlags flags,
338  bool scroll_to, bool backwards);
339 
342  int find_and_replace_text(const std::string& search_text, const std::string& new_text,
343  FindFlags flags, bool do_all);
344 
347  void jump_to_next_placeholder();
348 
349  //----- Auto completion -----
350 
359  void auto_completion_show(int chars_entered, const std::vector<std::pair<int, std::string> >& entries);
360  void auto_completion_show(int chars_entered, const std::vector<std::string>& entries);
361 
364  void auto_completion_cancel();
365 
377  void auto_completion_options(bool ignore_case, bool choose_single, bool auto_hide,
378  bool drop_rest_of_word, bool cancel_at_start);
379 
386  void auto_completion_max_size(int width, int height);
387 
393  void auto_completion_register_images(const std::vector<std::pair<int, std::string> >& images);
394 
396  bool auto_completion_active();
397 
399  void auto_completion_stops(const std::string& stops);
400 
404  void auto_completion_fillups(const std::string& fillups);
405 
407  void show_calltip(bool show, int position, const std::string& value);
408 
412  void set_eol_mode(mforms::EndOfLineMode mode, bool convert = false);
413 
418  void set_context_menu(Menu *menu) { _context_menu = menu; };
419 
421  Menu *get_context_menu() { return _context_menu; }
422 
424  void *get_host() { return _host; }
425 
427  sptr_t send_editor(unsigned int message, uptr_t wParam, sptr_t lParam);
428 
429 #ifndef SWIG
430 
437  boost::signals2::signal<void (int, int, int, bool)>* signal_changed() { return &_change_event; }
438 
445  boost::signals2::signal<void (int, int, mforms::ModifierKey)>* signal_gutter_clicked() { return &_gutter_clicked_event; }
446 
453  boost::signals2::signal<void (AutoCompletionEventType, int, const std::string&)>* signal_auto_completion() { return &_auto_completion_event; };
454 
462  boost::signals2::signal<void (bool, int, int, int)>* signal_dwell() { return &_dwell_event; }
463 
469  boost::signals2::signal<void (int)>* signal_char_added() { return &_char_added_event; }
470 
473  boost::signals2::signal<void ()>* signal_lost_focus() { return &_signal_lost_focus; }
474 #ifndef DOXYGEN_SHOULD_SKIP_THIS
475 
476  void on_notify(Scintilla::SCNotification* notification);
477 
479  void on_command(int command);
480  void lost_focus();
481 
482  virtual void resize();
483 
484 #endif
485 #endif
486  protected:
487  CodeEditorImplPtrs* _code_editor_impl;
490  std::map<int, void*> _images; // Registered RGBA images.
491  void *_host;
493 
494  void setup_marker(int marker, const std::string& name);
495  void load_configuration(SyntaxHighlighterLanguage language);
496 
497  boost::signals2::signal<void (int, int, int, bool)> _change_event;
498  boost::signals2::signal<void (int, int, mforms::ModifierKey)> _gutter_clicked_event;
499  boost::signals2::signal<void (AutoCompletionEventType, int, const std::string&)> _auto_completion_event;
500  boost::signals2::signal<void (bool, int, int, int)> _dwell_event;
501  boost::signals2::signal<void (int)> _char_added_event;
502  boost::signals2::signal<void ()> _signal_lost_focus;
503 
504  boost::function<void (CodeEditor*, bool)> _show_find_panel;
505  };
506 };
507 
508 #endif // _CODE_EDITOR_H_
boost::function< void(CodeEditor *, bool)> _show_find_panel
Definition: code_editor.h:504
boost::signals2::signal< void()> * signal_lost_focus()
Emited when editing ends (control loses focus)
Definition: code_editor.h:473
std::map< int, void * > _images
Definition: code_editor.h:490
std::map< std::string, std::string > get_keywords()
Definition: code_editor.h:170
void * get_host()
Returns the host which is controlling this editor instance (if any).
Definition: code_editor.h:424
LineMarkup operator|(LineMarkup a, LineMarkup b)
Definition: code_editor.h:73
std::map< int, std::map< std::string, std::string > > get_styles()
Definition: code_editor.h:173
boost::signals2::signal< void(AutoCompletionEventType, int, const std::string &)> * signal_auto_completion()
Event sent when auto completion notifications from Scintilla come in.
Definition: code_editor.h:453
FindPanel * get_find_panel()
Definition: code_editor.h:327
std::map< int, std::map< std::string, std::string > > _styles
Definition: code_editor.h:153
boost::signals2::signal< void(AutoCompletionEventType, int, const std::string &)> _auto_completion_event
Definition: code_editor.h:499
CodeEditorImplPtrs * _code_editor_impl
Definition: code_editor.h:487
boost::signals2::signal< void(bool, int, int, int)> * signal_dwell()
Signal emitted when the user keeps the mouse in one position for the dwell period or when when new ev...
Definition: code_editor.h:462
SyntaxHighlighterLanguage
Definition: code_editor.h:44
boost::signals2::signal< void(int, int, int, bool)> _change_event
Definition: code_editor.h:497
#define MFORMS_EXPORT
Definition: base.h:33
void set_context_menu(Menu *menu)
Sets a context menu to be attached to the editor, to be shown on right click.
Definition: code_editor.h:418
boost::signals2::signal< void(int)> _char_added_event
Definition: code_editor.h:501
std::map< std::string, std::string > _properties
Definition: code_editor.h:151
boost::signals2::signal< void(int)> * signal_char_added()
Signal emitted when the user typed an ordinary text character (as opposed to a command character)...
Definition: code_editor.h:469
std::map< std::string, std::string > get_properties()
Definition: code_editor.h:171
LineMarkup
A number of flags used to specify visual appearance of an editor line.
Definition: code_editor.h:61
Menu * get_context_menu()
Returns the context menu object attached to the editor.
Definition: code_editor.h:421
Helper class to manage editor configuration files.
Definition: code_editor.h:144
FindFlags & operator|=(FindFlags &a, FindFlags b)
Definition: code_editor.h:133
boost::signals2::signal< void(int, int, mforms::ModifierKey)> _gutter_clicked_event
Definition: code_editor.h:498
TiXmlDocument * _document
Definition: code_editor.h:155
boost::signals2::signal< void(int, int, int, bool)> * signal_changed()
Signal emitted when content is edited Parameters are: The (byte) position in the text where the chang...
Definition: code_editor.h:437
std::vector< std::string > _languages
Definition: code_editor.h:147
virtual std::string get_string_value()
Get string value from the view if it's holding some.
Definition: code_editor.h:221
std::map< std::string, std::string > _keywords
Definition: code_editor.h:150
RangeIndicator
Definition: code_editor.h:80
SyntaxHighlighterLanguage _used_language
Definition: code_editor.h:148
CodeEditorFeature
Definition: code_editor.h:85
std::map< std::string, std::string > _settings
Definition: code_editor.h:152
TiXmlElement * _language_element
Definition: code_editor.h:156
AutoCompletionEventType
Definition: code_editor.h:107
boost::signals2::signal< void(bool, int, int, int)> _dwell_event
Definition: code_editor.h:500
FindPanel * _find_panel
Definition: code_editor.h:489
boost::signals2::signal< void()> _signal_lost_focus
Definition: code_editor.h:502
std::map< std::string, std::string > get_settings()
Definition: code_editor.h:172
std::vector< std::string > get_languages()
Definition: code_editor.h:167
boost::signals2::signal< void(int, int, mforms::ModifierKey)> * signal_gutter_clicked()
Signal emitted when the user clicks on the gutter.
Definition: code_editor.h:445
MySQL Workbench 6.1 documentation