#include #include #include #include #include #include #include #include #include using namespace std; Fl_Double_Window *win; Fl_Text_Editor *ted; Fl_Button *buta; Fl_Text_Buffer *buf; Fl_Text_Buffer *style_buffer; Fl_Text_Editor::Style_Table_Entry styles[] = { {FL_BLACK, FL_COURIER, 15}, // 'A' {FL_RED, FL_BOLD, 16} // 'B' }; void change_cback(int pos, // I - Position of update int nInserted, // I - Number of inserted chars int nDeleted, // I - Number of deleted chars int nRestyled, // I - Number of restyled chars const char *deletedText, // I - Text that was deleted void *cbArg) { if (nDeleted > 0) style_buffer->remove(pos, pos + nDeleted); if (nInserted > 0) { for (int i = 0; i < nInserted; i++) { style_buffer->insert(pos, "A"); } } cout << "change_cb(" << pos << ',' << nInserted << ',' << nDeleted << ")" << " length(style_buffer) = " << style_buffer->length() << endl; } void style_unfinished_cb(int p, void *v) { // cout << "style_unfinished_cb(" << p << "," << v << ")" << endl; } void change_text(Fl_Widget *wid, void *data) { string s_char; int int_cnt = 0; int n_chars_selected = strlen(buf->selection_text()); for (int i = 0; i < n_chars_selected; i++) { s_char += "B"; int_cnt += i; } Fl_Text_Selection *ts = buf->primary_selection(); cout << "Selection = (" << ts->start() << ',' << ts->end() << ')' << endl; int start_pos = ts->start(); int end_pos = ts->start() + n_chars_selected; // should be: ts->end(); if (n_chars_selected > 0) style_buffer->replace(start_pos, end_pos, s_char.c_str()); cout << "change_text(" << start_pos << ',' << end_pos << ',' << n_chars_selected << ")" << " length(style_buffer) = " << style_buffer->length() << endl; ted->redraw(); } int main() { win = new Fl_Double_Window(800, 700, "Test_Window"); ted = new Fl_Text_Editor(50, 50, 600, 500); buta = new Fl_Button(50, 600, 80, 40, "Bold"); buf = new Fl_Text_Buffer(); ted->buffer(buf); style_buffer = new Fl_Text_Buffer(); ted->highlight_data(style_buffer, styles, 2, 'A', style_unfinished_cb, 0); buf->add_modify_callback(change_cback, ted); buta->callback(change_text); win->show(); return (Fl::run()); // sets event loop }