From 9e84259e6cb2102f3d2f996b0d2ab919abcd67a3 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Thu, 24 Mar 2005 04:07:19 +0000 Subject: [PATCH] Add auto-indent to code editor. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@4171 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- fluid/CodeEditor.cxx | 28 ++++++++++++++++++++++++++++ fluid/CodeEditor.h | 2 ++ 2 files changed, 30 insertions(+) diff --git a/fluid/CodeEditor.cxx b/fluid/CodeEditor.cxx index f979ce107..7e9fa72c0 100644 --- a/fluid/CodeEditor.cxx +++ b/fluid/CodeEditor.cxx @@ -332,6 +332,31 @@ void CodeEditor::style_update(int pos, int nInserted, int nDeleted, free(style); } +int CodeEditor::auto_indent(int, CodeEditor* e) { + if (e->buffer()->selected()) { + e->insert_position(e->buffer()->primary_selection()->start()); + e->buffer()->remove_selection(); + } + + int pos = e->insert_position(); + int start = e->line_start(pos); + char *text = e->buffer()->text_range(start, pos); + char *ptr; + + for (ptr = text; isspace(*ptr); ptr ++); + *ptr = '\0'; + e->insert("\n"); + if (*text) e->insert(text); + e->show_insert_position(); + e->set_changed(); + if (e->when()&FL_WHEN_CHANGED) e->do_callback(); + + free(text); + + return 1; +} + +// Create a CodeEditor widget... CodeEditor::CodeEditor(int X, int Y, int W, int H, const char *L) : Fl_Text_Editor(X, Y, W, H, L) { buffer(new Fl_Text_Buffer); @@ -353,8 +378,11 @@ CodeEditor::CodeEditor(int X, int Y, int W, int H, const char *L) : free(text); mBuffer->add_modify_callback(style_update, this); + add_key_binding(FL_Enter, FL_TEXT_EDITOR_ANY_STATE, + (Fl_Text_Editor::Key_Func)auto_indent); } +// Destroy a CodeEditor widget... CodeEditor::~CodeEditor() { Fl_Text_Buffer *buf = mStyleBuffer; // style_buffer(0); diff --git a/fluid/CodeEditor.h b/fluid/CodeEditor.h index 38fb56d2d..624eb2faa 100644 --- a/fluid/CodeEditor.h +++ b/fluid/CodeEditor.h @@ -61,6 +61,8 @@ class CodeEditor : public Fl_Text_Editor { int /*nRestyled*/, const char * /*deletedText*/, void *cbArg); + static int CodeEditor::auto_indent(int, CodeEditor* e); + public: CodeEditor(int X, int Y, int W, int H, const char *L=0);