Skip to main content

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

Fl_Text_Editor
constructor
Creates a new text editor widget.
X
int
required
X position of the widget
Y
int
required
Y position of the widget
W
int
required
Width of the widget
H
int
required
Height of the widget
l
const char*
default:"0"
Optional label for the widget

Insert Mode

insert_mode
void
Sets the current insert mode.
b
int
required
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.
insert_mode
int
Gets the current insert mode.Returns non-zero if in insert mode, 0 if in overwrite mode.

Key Bindings

add_key_binding
void
Adds a key binding.
key
int
required
Key code (e.g., ‘a’, FL_F+1)
state
int
required
Modifier state (FL_COMMAND, FL_SHIFT, FL_CTRL, FL_ALT, or FL_TEXT_EDITOR_ANY_STATE)
f
Key_Func
required
Function to call when key is pressed
remove_key_binding
void
Removes a key binding.
key
int
required
Key code
state
int
required
Modifier state
remove_all_key_bindings
void
Removes all key bindings.
bound_key_function
Key_Func
Returns the function bound to a key.
key
int
required
Key code
state
int
required
Modifier state
Returns the function pointer, or NULL if not bound.
default_key_function
void
Sets the default key function for unassigned keys.
f
Key_Func
required
Default function

Built-in Key Functions

These static functions can be used with add_key_binding():
kf_default
static int
Default key handler - inserts the character.
kf_ignore
static int
Ignores the key press.
kf_backspace
static int
Backspace key handler.
kf_enter
static int
Enter/return key handler.
kf_delete
static int
Delete key handler.
kf_copy
static int
Copy selection to clipboard.
kf_cut
static int
Cut selection to clipboard.
kf_paste
static int
Paste from clipboard.
kf_undo
static int
Undo last operation.
kf_redo
static int
Redo last undone operation.
kf_select_all
static int
Select all text.
kf_move
static int
Move cursor (arrow keys).
kf_shift_move
static int
Move cursor with shift (extends selection).
kf_ctrl_move
static int
Move cursor with control (word movement).
kf_home
static int
Home key handler.
kf_end
static int
End key handler.
kf_page_up
static int
Page up handler.
kf_page_down
static int
Page down handler.

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