Colorize the border of a Fl_Input and BUG with Fl_Input using frame?

36 views
Skip to first unread message

Apprentice

unread,
Feb 18, 2023, 11:50:54 AM2/18/23
to fltk.general
System: Linux, Slackware64-Current
Compiler: g++
Fltk: v.1.4.0

Good afternoon everybody!

Is there any way or method, or something magical I can color the border of a Fl_Input?

And I'm not able to use any frame with Fl_Input that gives the problem in the image below, when I write anything in it!

image.png

Mo_Al_

unread,
Feb 18, 2023, 4:50:04 PM2/18/23
to fltk.general
Good evening

I'm not sure how it can be done in FLUID, maybe others can help here.
However you can do it in code.
You can either override the draw method:

#include <FL/Fl.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Window.H>
#include <FL/fl_draw.H>
#include <FL/Enumerations.H>

class MyInput: public Fl_Input {
    public:
        MyInput(int x, int y, int w, int h, const char *l = 0): Fl_Input(x, y, w, h, l) {
            box(FL_FLAT_BOX);
        }
        void draw() override {
            Fl_Input::draw();
            fl_color(FL_GREEN);
            fl_line_style(FL_SOLID, 2);
            fl_rect(x(), y(), w(), h());

        }
};

int main(void) {
    auto w = new Fl_Window(400, 300);
    auto i = new MyInput(100, 100, 160, 30, "Enter name:");
    w->end();
    w->show();
    return Fl::run();
}

Or you can override the FL_DOWN_BOX that's used by Fl_Input:
void my_down_box(int x, int y, int w, int h, Fl_Color) {
    fl_color(FL_WHITE);
    fl_rectf(x, y, w, h);
    fl_color(FL_GREEN);
    fl_line_style(FL_SOLID, 2);
    fl_rect(x, y, w, h);
}

int main(void) {
    Fl::set_boxtype(FL_DOWN_BOX, my_down_box, 0, 0, 0, 0);
    auto w = new Fl_Window(400, 300);
    auto i = new Fl_Input(100, 100, 160, 30, "Enter name:");
    w->end();
    w->show();
    return Fl::run();
}

Or set the boxtype to FL_FREE_BOXTYPE:
Fl::set_boxtype(FL_DOWN_BOX, my_down_box, 0, 0, 0, 0);
i->box(FL_FREE_BOXTYPE);

Frames don't have a solid interior color, and as such will show artifacts unless you redraw with every change.

Apprentice

unread,
Feb 18, 2023, 7:25:55 PM2/18/23
to fltk.general
Perfect, that's what I needed! Thank you, problem solved!
Now I can choose the Box I like and change its borders!

I use Fluid just to draw the window and controls, know where to position it, and help show some commands, but I don't use Fluid to create the code!
thanks again!

Based on your idea, and as I needed the rounded corners, I did it like below! It can help other people!

void mycolor(int x, int y, int w, int h, Fl_Color) {
    int r = 10;

    fl_color(FL_WHITE);
    fl_rectf(x, y, w, h);
    fl_color(FL_GREEN);
    fl_line_style(FL_SOLID, 1);
    fl_arc(x, y, 2*r, 2*r, 90, 180);
    fl_arc(x+w-2*r, y, 2*r, 2*r, 0, 90);
    fl_arc(x, y+h-2*r, 2*r, 2*r, 180, 270);
    fl_arc(x+w-2*r, y+h-2*r, 2*r, 2*r, 270, 360);
    fl_line(x+r, y, x+w-r, y);
    fl_line(x+r, y+h, x+w-r, y+h);
    fl_line(x, y+r, x, y+h-r);
    fl_line(x+w, y+r, x+w, y+h-r);
}


image.png
Reply all
Reply to author
Forward
0 new messages