Search code examples
c++functionbuttonbgiwinbgi

Manually Programming a clickable button on Borland Graphic Interface


I'm doing an editor right now, and I need to do a clickable button on BGI (it has to be on it).

Right now I did something like using outtextxy, gave the coordinates then I made an if using coordinates got by mousex and mousey, if it is equal to the coordinates from the outtext, it does its job.

It is still not good enough for me, I want to create a function that when I use the outtext, it creates at the same time the if condition that I made (manually putting an if condition for each outtext is a massive coding job).

Can someone help me by showing me how can I do this function?


Solution

  • BGI was never designed to be a window/widget manager — it is a low-level primitive library. In order to do what you want you need to implement a form of WM on top.

    For simply adding buttons, all you need is a button class that encapsulates a button's position, code to draw the button, code to determine whether an (x,y) coordinate lies within the button, button draw state (hover, pressed, normal), and a click event.

    In your main loop, then, you will want to loop through all your buttons to see if the mouse (x,y) lies within the button. If the mouse event is a click, then invoke the button's click event.

    The same is true for any other widget type you have in your application, such as the text/edit window. This is how all window/widget managers work.

    The way you implement this in your application is completely up to you. I recommend a simple std::vector<BGIWidget> (or whatever you name your widget superclass). Then all you need to do is loop through your list of widgets to handle things like drawing, mouse hover/enter/leave/click events, etc.