Add auto-indent to code editor.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@4171 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
parent
e01bdbfbf2
commit
9e84259e6c
@ -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);
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user