How to add italic and bold support to fl_texteditor?

51 views
Skip to first unread message

Xander

unread,
Apr 20, 2021, 4:02:11 PM4/20/21
to fltk.general

So, I would like to use italic and bold text in a fl_texteditor, but have some, not all text having said states (bold, italic) applied. Is that at all possible with current fltk, or do I need to - somehow - inherit from fl_texteditor and change something?

Albrecht Schlosser

unread,
Apr 20, 2021, 4:15:07 PM4/20/21
to fltkg...@googlegroups.com
On 4/20/21 10:02 PM Xander wrote:
>
> So, I would like to use italic and bold text in a fl_texteditor, but
> have /some/, not /all/ text having said states (bold, italic) applied.
> Is that at all possible with current fltk, or do I need to - somehow -
> inherit from fl_texteditor and change something?

It is possible to do that w/o deriving your own class or such. The way
to do is is to have a second buffer that holds the "type" value of each
character in the main buffer.

A good example is test/editor.cxx in the FLTK source.

That code however has big parts that deal with parsing the text in the
main buffer to find syntax elements and to set the type per character.
This is something your code will likely not need, or you may already
have it implemented.

You can find most of the related documentation in the Fl_Text_Display
class, particularly
https://fltk.gitlab.io/fltk/classFl__Text__Display.html#ae09d61739b737a32868ffe0295a25dec

which documents how to define data in the "highlight buffer" (the
display "type" per character I mentioned above).

If you have more detailed questions, feel free to ask again.

Xander

unread,
Apr 20, 2021, 4:19:40 PM4/20/21
to fltk.general
Thanks,

I'm sorry for being too terse in my question.

--Xander

Op dinsdag 20 april 2021 om 22:15:07 UTC+2 schreef Albrecht Schlosser:

Greg Ercolano

unread,
Apr 20, 2021, 4:38:20 PM4/20/21
to fltkg...@googlegroups.com


On 4/20/21 1:02 PM, Xander wrote:

So, I would like to use italic and bold text in a fl_texteditor, but have some, not all text having said states (bold, italic) applied. Is that at all possible with current fltk, or do I need to - somehow - inherit from fl_texteditor and change something?


    In addition to Albrecht's suggestions, there's also Fl_Simple_Terminal (in 1.4.x)
    which lets you define a 'style table' that maps ANSI escape sequences to custom
    font color/face/size, so you can define your own escape sequences to trigger
    e.g. italics or bold fonts, etc.

    The default style table just supports colors at present, all with the FL_COURIER
    font. But you can also make your own custom table and apply it to the terminal..
    then while the app is running,  you can enter italic or bold modes by sending
    the escape sequences you specify. Or that's how it's supposed to work...

Xander

unread,
Apr 20, 2021, 5:31:56 PM4/20/21
to fltk.general
I noticed that I can get a full list of fonts using set_fonts. It appears face information is stored (which is nice).
Fl_style_table_entry does have the font field. Are you saying it is currently just being ignored silently by fl_texteditor/fl_textdisplay?

I'd prefer to stick to the stable version of fltk.

Op dinsdag 20 april 2021 om 22:38:20 UTC+2 schreef er...@seriss.com:

Albrecht Schlosser

unread,
Apr 20, 2021, 6:06:29 PM4/20/21
to fltkg...@googlegroups.com
On 4/20/21 11:31 PM Xander wrote:
> I noticed that I can get a full list of fonts using set_fonts. It
> appears face information is stored (which is nice).
> Fl_style_table_entry does have the font field. Are you saying it is
> currently just being ignored silently by fl_texteditor/fl_textdisplay?

No, what Greg was saying is that there's no font information in the
/default/ settings of /Fl_Simple_Terminal/ which is new to 1.4.0.

> I'd prefer to stick to the stable version of fltk.

Fl_Text_Display and Fl_Text_Editor are fully supported in 1.3 and 1.4
including font faces and text color. If you build the FLTK library and
test programs you can use test/editor(.cxx) and open a C/C++ source file
to see what it can do.

Side note: please try not to top-post here in this group and trim the
citation to the part you refer to in your answer. This is the style
we're trying to use here in fltk.general and fltk.coredev for better
reading and understanding. Thanks.

https://en.wikipedia.org/wiki/Posting_style#Top-posting

Ian MacArthur

unread,
Apr 21, 2021, 6:16:28 PM4/21/21
to fltkg...@googlegroups.com
> On 4/20/21 11:31 PM Xander wrote:
>> I noticed that I can get a full list of fonts using set_fonts. It appears face information is stored (which is nice).
>> Fl_style_table_entry does have the font field. Are you saying it is currently just being ignored silently by fl_texteditor/fl_textdisplay?
>


So since it was being asked, here’s a very cut down version of the editor demo that just updates the style buffer. Continuously...

It’s of no actual use, but might illustrate how the style buffer can be used for setting font faces / style / etc.

hack_editor.cxx

Greg Ercolano

unread,
Apr 21, 2021, 6:33:32 PM4/21/21
to fltkg...@googlegroups.com

On 4/21/21 3:16 PM, Ian MacArthur wrote:
So since it was being asked, here’s a very cut down version of the editor demo that just updates the style buffer. Continuously... It’s of no actual use, but might illustrate how the style buffer can be used for setting font faces / style / etc.

    A similar resource that comes with fltk 1.4.x is the examples/textdisplay-with-colors.cxx
   
which is a fairly concise bit of code:

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Text_Display.H>
int main() {
   // Style table
   Fl_Text_Display::Style_Table_Entry stable[] = {
       // FONT COLOR      FONT FACE   FONT SIZE
       // --------------- ----------- --------------
       {  FL_RED,         FL_COURIER, 18 }, // A - Red
       {  FL_DARK_YELLOW, FL_COURIER, 18 }, // B - Yellow
       {  FL_DARK_GREEN,  FL_COURIER, 18 }, // C - Green
       {  FL_BLUE,        FL_COURIER, 18 }, // D - Blue
   };
   Fl_Window *win = new Fl_Window(640, 480, "Simple Text Display With Colors");
   Fl_Text_Display *disp = new Fl_Text_Display(20, 20, 640-40, 480-40);
   Fl_Text_Buffer *tbuff = new Fl_Text_Buffer();        // text buffer
   Fl_Text_Buffer *sbuff = new Fl_Text_Buffer();        // style buffer
   disp->buffer(tbuff);
   int stable_size = sizeof(stable)/sizeof(stable[0]);  // # entries in style table (4)
   disp->highlight_data(sbuff, stable, stable_size, 'A', 0, 0);
   // Text
   tbuff->text("Red Line 1\nYel Line 2\nGrn Line 3\nBlu Line 4\n"
               "Red Line 5\nYel Line 6\nGrn Line 7\nBlu Line 8\n");
   // Style for text
   sbuff->text("AAAAAAAAAA\nBBBBBBBBBB\nCCCCCCCCCC\nDDDDDDDDDD\n"
               "AAAAAAAAAA\nBBBBBBBBBB\nCCCCCCCCCC\nDDDDDDDDDD\n");
   win->resizable(*disp);
   win->show();
   return(Fl::run());

}

When run, it looks like this:


Note that you can change the FL_COURIER references to other fonts/styles to access Bold and Italic features.


imm

unread,
Apr 22, 2021, 7:48:12 AM4/22/21
to general fltk
On Wed, 21 Apr 2021 at 23:33, Greg Ercolano wrote:

    A similar resource that comes with fltk 1.4.x is the examples/textdisplay-with-colors.cxx
    which is a fairly concise bit of code:


Doh! Greg's right - his example is much more concise and economical, that would have been a much better place for me to stat from, rather than cutting down the whole editor example...

Anyway, as is my wont, I then took Greg's example and made it unnecessarily complex again - the matrix, basically.
See attached...




 
matrix_text_display.cxx
Reply all
Reply to author
Forward
0 new messages