Documentation Index
Fetch the complete documentation index at: https://mintlify.com/fltk/fltk/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The Fl_Text_Editor widget allows users to edit multiple lines of text with support for scrolling, highlighting, undo/redo, and customizable key bindings. The buffer displayed is managed by Fl_Text_Buffer.
Constructor
Creates a new text editor widget.Optional label for the widget
Insert Mode
Sets the current insert mode.Non-zero for insert mode, 0 for overwrite mode
In insert mode, new text is inserted before the cursor. In overwrite mode, new text replaces text at cursor.
Gets the current insert mode.Returns non-zero if in insert mode, 0 if in overwrite mode.
Key Bindings
Adds a key binding.Key code (e.g., ‘a’, FL_F+1)
Modifier state (FL_COMMAND, FL_SHIFT, FL_CTRL, FL_ALT, or FL_TEXT_EDITOR_ANY_STATE)
Function to call when key is pressed
Removes all key bindings.
Returns the function bound to a key.Returns the function pointer, or NULL if not bound.
Sets the default key function for unassigned keys.
Built-in Key Functions
These static functions can be used with add_key_binding():
Default key handler - inserts the character.
Enter/return key handler.
Copy selection to clipboard.
Cut selection to clipboard.
Redo last undone operation.
Move cursor (arrow keys).
Move cursor with shift (extends selection).
Move cursor with control (word movement).
Example
#include <FL/Fl_Text_Editor.H>
#include <FL/Fl_Text_Buffer.H>
#include <FL/Fl_Window.H>
Fl_Text_Buffer *textbuf;
void changed_cb(int, int nInserted, int nDeleted,
int, const char*, void*) {
if (nInserted || nDeleted) {
printf("Text was modified\n");
}
}
int main() {
Fl_Window *win = new Fl_Window(640, 480, "Editor");
textbuf = new Fl_Text_Buffer();
textbuf->add_modify_callback(changed_cb, NULL);
Fl_Text_Editor *editor = new Fl_Text_Editor(10, 10, 620, 460);
editor->buffer(textbuf);
editor->textfont(FL_COURIER);
editor->textsize(14);
win->resizable(editor);
win->end();
win->show();
return Fl::run();
}
Custom Key Binding Example
// Custom function to insert timestamp
int insert_timestamp(int, Fl_Text_Editor *e) {
time_t t = time(NULL);
char buf[100];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&t));
e->insert(buf);
return 1;
}
int main() {
// ... create editor ...
// Bind F5 to insert timestamp
editor->add_key_binding(FL_F+5, 0, insert_timestamp);
// ... show window ...
}
Syntax Highlighting
#include <FL/Fl_Text_Display.H>
Fl_Text_Buffer *stylebuf;
// Style table for syntax highlighting
Fl_Text_Display::Style_Table_Entry styletable[] = {
{ FL_BLACK, FL_COURIER, 14 }, // A - Plain
{ FL_DARK_GREEN, FL_COURIER, 14 }, // B - Comments
{ FL_BLUE, FL_COURIER_BOLD, 14 }, // C - Keywords
};
void style_update(int pos, int nInserted, int nDeleted,
int, const char*, void *cbArg) {
// Parse text and update style buffer
// See FLTK editor.cxx example for full implementation
}
int main() {
// ... create editor and textbuf ...
stylebuf = new Fl_Text_Buffer();
editor->highlight_data(stylebuf, styletable,
sizeof(styletable)/sizeof(styletable[0]),
'A', NULL, 0);
textbuf->add_modify_callback(style_update, editor);
// ... show window ...
}
See Also