[Gambas-user] [Gambas-devel] Creating a Control

Tobias Boege taboege at ...626...
Tue Sep 9 23:15:19 CEST 2014


On Tue, 09 Sep 2014, John Leake wrote:
> So to elaborate: If a user wishes to create a new type of control then
> it is highly likely that the control will be based on existing controls.
>  So as an example, if the new control contains a ToggleButton and the
> way that ToggleButton is used in the new control is no different from
> the original stock ToggleButton then the user should not be required to
> write any code to implement the ToggleButton.
> 

I can't imagine what you are trying to do. It sounds like you want to take a
class A and derive another class A' from it -- but so that A = A' ("=" means
equality of code). The way to go here would be: just use A. No coding effort
needed then. (See, optimising this task seems pointless because nobody ever
does it.)

BUT, what you *may* want to do is to compose a new control out of existing
ones, one of which is of class A. This we would call (at least I do it) a
"compound control". (If that's not your goal you might still want to remember
this for later.) You can't do that without some coding because most likely
you write such a thing for a purpose (so that it does something special,
which, in turn, you must code) -- but it's a task that is actually kinda
fun (!) in Gambas.

There is a small compound control of mine which I could rip out of its
project for you to see: it is a smaller version of the ColorChooser from
gb.form with discrete colour steps. (I needed a dialog-less, visual and
not-so-fine-grained way to pick some colours out of little space on a Form,
so the MinimalColorChooser was born.)

The general strategy to create a compound control whose name shall be A, is
the following:

 1. Create a form named F##A (you know the C preprocessor's token paste
    operator, right?),
 2. Arrange the controls you want A to be made of on the form (the good
    thing is that you can design this by using the IDE's Form editor),
 3. Wire the controls' actions together in the Form's class file,
 4. Create properties, methods and events for your purpose in F##A,
 5. Create your actual class A which is Export'd and Inherits UserControl.
    You create an instance of F##A on yourself in the constructor of that
    class (since you inherit UserControl, you can create controls on your
    own surface and a Form can be created as a container on a window, too,
    not only as a top-level window),
 6. Expose the needed parts of F##A's interface to public through the
    interface of your exported class A,
 7. Do the IDE integration stuff, i.e. draw an icon and set the public
    constants which govern IDE integration, in A.

That's at least what I do and just did to create the attached project. The
relevant parts are in the directory MinimalColorChooser. I think it's normal
if it takes you a while to get to grips with it as I consider custom control
creation an advanced topic. But it's beautiful to do with Gambas.

If you have any questions, don't hesitate!

[ You *will* also want to explore the automatic layouting capabilities of
  Gambas, if you don't already have experience with it from other languages.
  You will see that the FMain in my project is full of containers like HBox
  and VBox. These arrange their children in special ways. The key properties
  to look at here are: Arrangement, Expand, Margin, Spacing and sometimes
  AutoResize. Take a weekend or so off (judging from my own experience) to
  experiment with these and how a wrong setting can mess up everything :-)

  To get motivated: arrange some controls on a Form, run your project and
  resize the Form -- not very pretty. Now resize the window in my project. ]

> If this idea is extrapolated to its simplest form then a user should be
> able to create a new ToggleButton by informing the IDE that this is a
> new control and that the control is based on the stock ToggleButton and
> that is all that needs to be done. After all what is the point of
> inheritance if you have to add code just to get back the functionality
> that was already there in the first place.

Note that you are talking about IDE integration here (right?). You can write

--8<--[ MyToggleButton.class ]----------------------------------------------
' Gambas class file

Inherits ToggleButton
--8<------------------------------------------------------------------------

and the interpreter will be happy and everything will be there. Integrating
a control into the IDE requires some more things. I don't see yet why it is
so important to make it require even less effort. Maybe you've got real
world experience here [ heck, you've worked longer as a developer than I am
alive :-) ], but I deem custom controls to be a thing which newbies should
stay away from.

Regards,
Tobi

PS: I insist on keeping gambas-user CC'd :-P

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
-------------- next part --------------
A non-text attachment was scrubbed...
Name: MinimalColorChooser-0.0.1.tar.gz
Type: application/octet-stream
Size: 6345 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20140909/597e61b6/attachment.obj>


More information about the User mailing list