fRADIOCONTROL


Quick Data

Header file:button.h
Object name:fRADIOCONTROL
Object index:2 of 2 Objects (the other is fBUTTON)

General Description

The radio controller is designed to look after a set of radio controls. It can intercept a click on one radio control, and will then go through the group and uncheck all others, finally checking the one that was clicked. Windows occasionally provides support for this - that is, when it feels like doing so. This way there is no "maybe" about radio control handling.

Please note that this controller isn't just limited to radio buttons - you can use checkboxes or toggle buttons. Please do not use other types of controls with this class.

By default this object is included with button.h. If you do not want this object to be included, just define F_BUTTON_H_NO_RADIO_CONTROLLER (what a mouthfull!), before you include button.h, and it will not be included.

This controller will not automatically be registered with the GUI controller - but it doesn't have to be. The GUI controller will still know about it, via the buttons that you add to it. So don't worry about events being handled, if you are using the GUI controller. Everything will work right. If you are using the GUI controller, I would advise against using the OnClick methods of the Radiocontroller itself, unless you want a double-OnClick proc. Just use the ordinary OnClick and you will be fine.


Properties


Name Description

int NumberControls; This member is a count of the number of controls. You can read or write it, but it's really only a good idea to read it. This number is changed internally when controls are added or removed, so please, just read it unless you know what you are doing...

Methods


Prototype Description

void AllocateMemory(int Number); This function allocates the internal database memory required for the specified number of controls. This component does not resize the database as required, and thus you must know how many controls to allocate memory for. Allocating memory will clear whatever was already in the database. Bug Alert! Please only allocate exactly as many as you need; otherwise you might get some access violations.
void AddRadio(int Index, fBUTTON* Radio, void (*OnClickProc)(fBUTTON* Sender)); This function adds a radio button to the database. The first parameter is the index of the button in the database. The index is Zero based, and thus the first item would be index 0. The second parameter is a pointer to the radio button object. Say that your object is called MyRadio, you would put this parameter as &MyRadio (a pointer to MyRadio). The third parameter is a function to call when the button is clicked. You do not have to use this if you have a GUI controller, but you still can should you wish to. The function which this would point to will have the prototype as follows: void OnClick(fBUTTON* Sender);, where you replace OnClick with your function's name. You can pass NULL if you do not want an OnClick function. One last thing to note: should you wish to remove a button from the chain, but leave the rest, just call this function with the object's index, and pass NULL as the pointer to the object. The class has been designed to ignore any objects in the database with a null pointer, thereby saving otherwise annoying access violations.
void Destroy(void); This function destroys the button database. The button objects themselves are not touched.
void DoChecked(wParam, lParam); This function is called from your message queue, or is called from the GUI controller (it knows when the gui controller is available, and it automatically registers itself when it is created - in other words, if you have a GUI controller, this function is not relevant to you). You simply pass along the wParam and lParam values, and from there it can work out which button was clicked, and do something about it. Should you have OnClick functions assigned, it will run those functions as required.
void CheckOne(int Index); It would be useful to set one item to start off with, or maybe in the middle of the program. Don't despair - you don't have to cycle through all of them yourself. Instead, just use this function and pass along the index of the button that you would like checked, and this function will go along, check that one, and uncheck the rest. Again, this value is Zero based (Button 1 == 0). This does not call the OnClick handler for the button that you select.
void UnCheckAll(void); This function goes through and unchecks all the buttons in the set.
fBUTTON* FindWhichChecked(void); This function returns a pointer to the button that is currently checked, if only one is. If more than one button is checked, it will return only the pointer to the first one.

Sample Code

The following snippet gives a basic idea of how to use this component.


//Say we have some buttons...
fBUTTON RadioButton1, RadioButton2;
RadioButton1.CreateWin(Window, "Choose me!",
                       ID_RB1, bsUsual,
                       bsRadioButton);
RadioButton2.CreateWin(Window, "No, Choose me!!",
                       ID_RB2, bsUsual,
                       bsRadioButton);
//And then a radio controller...
fRADIOCONTROL Controller;
Controller.AllocateMemory(2);
Controller.AddRadio(0, &RadioButton1, NULL);
Controller.AddRadio(1, &RadioButton2, NULL);
//And tada! You're done!

//But, in the message queue (if you don't have
//a GUI controller) you'll need this...
LRESULT WindowProc(...)
...
case WM_COMMAND:
     Controller.DoChecked(wParam, lParam);
     break;
...

Back to indexThe FreeFoote Foundation Classes Documentation