#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; //UNNEEDED int position = 0; //UNNEEDED int inserted = 0; //UNNEEDED int deleted = 0; //UNNEEDED const char* deleted_text; //UNNEEDED int *start_s = NULL; //UNNEEDED int *end_s = NULL; Fl_Text_Editor::Style_Table_Entry styles[] = { {FL_BLACK,FL_HELVETICA,16}, // "A" (our "default font") {FL_RED,FL_HELVETICA_BOLD,16} // "B" (our "bold font") }; // void change_cback(..) { } // UNNEEDED FOR THIS DEMO void change_text(Fl_Widget *wid, void *data) { // Get text buffer's text selection const Fl_Text_Selection *ts = buf->primary_selection(); int start = ts->start(); int end = ts->end(); // No selection? do nothing.. if ( start == end ) return; // no selection? do nothing.. // Make style buffer changes to the selected region string s_char; for (int i=start; ireplace(start, end-1, s_char.c_str()); ted->redraw(); } // When text buffer is modified, sync changes with the style buffer void text_modified(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) // I - Callback data { // Text buffer changed? Update style buffer too.. if (nInserted > 0) { // Insert characters into the style buffer... char *style = new char[nInserted + 1]; memset(style, 'A', nInserted); // use default font style[nInserted] = '\0'; style_buffer->replace(pos, pos + nDeleted, style); delete[] style; } else { // Just delete characters from style buffer... style_buffer->remove(pos, pos + nDeleted); } } 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(); // Associate style buffer with editor int styles_size = sizeof(styles)/sizeof(Fl_Text_Display::Style_Table_Entry); ted->highlight_data(style_buffer, styles, styles_size, 'A', // default style 0, 0); buf->add_modify_callback(text_modified, ted); // updates style buffer when user types // Now preload editor with some text. text_modified() will handle setting default style. buf->text("0123456789012345678901234567890123456789"); // buf->add_modify_callback(change_cback,ted); // UNNEEDED FOR DEMO buta->callback(change_text); win->show(); return(Fl::run()); // sets event loop }