Custom winid definition with EVT_MENU - event-handling

I am a beginner in wxWidgets and so this is a very basic thing I'm having trouble with. I want to know how to define a custom winid like "ID_MENU_CIRCLE" with EVT_MENU which already has defined winid's.
BEGIN_EVENT_TABLE(ShapeFrame,wxFrame)
EVT_MENU(ID_MENU_CIRCLE, ShapeFrame::OnModelCircle)
END_EVENT_TABLE()

You're probably mixing up event IDs and event types. All menu items generate events of wxEVT_MENU type, but each event carries its own unique ID, corresponding to the ID of the menu item that generated it. And menu item IDs are just unique integers allowing you to uniquely identify the items.
So your ID_MENU_CIRCLE can be just any integer and could be just
const int ID_MENU_CIRCLE = 100;
but it's common to use an enum to define these constants because you'd typically have many of them:
enum {
ID_MENU_CIRCLE = 100,
ID_MENU_SQUARE,
...
};

Related

Can choices in a list be changed after it has been been rendered?

I have a w2ui form that contains a w2ui Drop List of choices. The choices will be different depending on what the user selected to bring up the form. My question is: can the contents of a Drop List be changed after it has been rendered?
With standard HTML controls, I would do something like this:
$("#mySelect option[value='xyz']").remove();
or
$("#mySelect").append('<option value="abc">abc</option>');
Can these kinds of operations be done with a w2ui Drop List? Any example code?
In w2ui 1.5 you can use $jQueryElement.w2field() to access the w2fild object - and then manipulate it.
Example:
var field = $("#my_input").w2field();
field.options.items = ["my", "new", "items"];
// optionally: pre-select first item
field.setIndex(0);
// if you do NOT use "setIndex" you need to call "refresh" yourself!
// field.refresh();
Note: setIndex() internally calls refresh() - so as stated above, you do not need to call refresh yourself in that case.
If you want to completely clear/empty your field, you can call field.reset().
Edit: after clarification that it's about a form field:
// Note: ``this`` refers to the w2form
// ``field[8]`` refers to a field of type "select"
this.fields[8].options.items = ["my", "new", "items"];
this.record = {
field_select: 'new'
};
this.refresh();

Display all enums from an EMF model in a CheckBoxTable ?

I am trying to display a CheckBoxTable in an Eclipse page which enables the user to select any one of a number of items - the items that are available come from an EMF model and are enums.
I've got the content provider and the label provider set up correctly (I think) but I can't figure what to use to set the input in order to display the full list of enums.
So say my model has an enum called MyEnum which has values of ONE, TWO and THREE - I want to be able to display all three of those enums to the user as check boxes.
I need to call setInput(...) on the viewer but what do I pass into it to get those enums?
Although I've never done it for a CheckboxTableViewer, I have set an EEnum as the source of values for other StructuredViewer classes like ComboViewer. What I did was create a custom IStructuredContentProvider that is a subclass of ArrayList and takes the EEnum as a constructor argument (call this class EEnumContentProvider). In the constructor, I iterate over the EEnum's getELiterals() and call add() on each of their getInstance() values. Like this:
public EEnumContentProvider(EEnum source) {
List<EEnumLiteral> literals = source.getELiterals();
for (EEnumLiteral aLiteral : literals) {
add(aLiteral.getInstance());
}
}
You can easily implement IStructuredContentProvider.getElements(Object) by using returning the result of toArray() and you don't care about IContentProvider.setInput() because the contents aren't based on the input, they're static.
Then you can set an instance of EEnumContentProvider as the content provider for the viewer.
Simply you need to get the literals and add them to the control as follows:
/* Populate the Combo Box with the Literals */
EEnum cFEnum = Package.Literals.LITERAL_ENUMERATION;
/*
* Add an EMPTY item value so that the user can disable the specific
* feature
*/
this.cmbNames.add( EMPTY_STRING );
/*
* Add the Enumeration Literals to the
* appropriate SWT Combo widget.
*/
for (int i=0; i<cFEnum.getELiterals().size(); i++){
this.cmbNames.add( cFEnum.getEEnumLiteral( i ).toString() );
}
cFEnum = null;
String[] sortedTypes = this.cmbNames.getItems();
Arrays.sort( sortedTypes );
this.cmbNames.setItems( sortedTypes );

List/Object searching in CoffeeScript

I'm trying to get my head around using CoffeeScript comprehensions as efficiently as possible. I think I have basic mapping down -- turning one list into another -- but searching still seems verbose to me.
Say I have a map of items to shops:
shopMap:
toyStore: ["games", "puzzles"]
bookStore: ["novels", "picture books"]
and, given an item, I want to find out which shop it's in. What's the best way of doing that in CoffeeScript?
Here's how I could do in in JavaScript:
var shop = findShop(item);
function findShop(item) {
for (shop in shopMap)
itemList = shopMap[shop]
for (i = 0, ii = itemList.length; i<ii; i++) {
if (itemList[i] === item) {
return shop;
}
}
}
}
I used a function to allow it to quickly break out of the loops with the return statement, instead of using breaks, but the function is kind of fugly as this is only being used once.
So is there a shorter CS equivalent preferably one that doesn't require creating a new function?
You can try this:
findShop = (item) ->
for shop, items of shopMap
return shop if item in items
If you really want to try with a list comprehension, this is equivalent:
findShop = (item) ->
(shop for shop, items of shopMap when item in items)[0]
But i think the first one reads better (and also doesn't need to generate an intermediate array for the results). This would be a better approach IMO if you wanted to find all shops for a given item:
findShops = (item) ->
shop for shop, items of shopMap when item in items
If this is a common operation, you might be better off creating an intermediate data structure up front and doing the lookup directly.
shopMap =
toyStore: ["games", "puzzles"]
bookStore: ["novels", "picture books"]
categoryMap = {}
for k, v of shopMap
for category in v
categoryMap[category] = k
alert(categoryMap['puzzles'])
Demo
With this implementation you need to loop through the structure only once up front (plus possibly update it if shopMap changes). With yours and epidemian's answer, you have to loop every time you need to do this particular type of lookup. If you do this operation a lot, it could make a difference. On the other hand, if your shopMap is really large (like thousands of entries), then my implementation will take up more memory.
Depending upon how robust you want to make this, you might want to turn it into a Class and have any operations on it occur through the Class' interface. You'd need addCategory and deleteCategory methods as well as a getStoreFromCategory method, which is essentially what we are implementing above. This object-oriented approach would hide the internal data-structure/implementation so you could later alter the implementation to optimize for memory or speed.

Multiple Forms in Symfony

I do have a form. This form submits for example 3 words (beer, coke, wine). In the next action I do want to have a three choice widgets with one or more choices:
-beer: //first choice field
* buddy lighty //choice one
* busch //choice two
* miler //choice three
-coke: //second choice field
* coke diet
* coke
* coke vanilla
-wine: //third choice field
* bordeaux
* suave
* champange
<submit-button>
I want every choice in one action. So if somebodey make a choice busch, coke, suave will be submittet. How can I realise it?
Update:
Thanks for the comment. I might forget to say that I don't know how many dropdown menus I need. There might be just beer and coke or beer, coke, wine and juice. It depends from what the user fill out the number of forms the action before! I tried to do it with a foreach-loop in forms.class.php. But it doesn't help.
I use Doctrine.
One simple way to do this (depends on your model, too) is to configure each item as nullable, and then use form options to show/hide certain widgets. e.g., if your schema looks like this lazy example:
DrinkOrder:
columns:
# ...
beer:
type: enum
values: [Old Peculier,Tribute,Deuchars]
notnull: false
wine:
type: enum
values: [Bordeaux,Suave,Champagne]
notnull: false
# ...etc
Configure your form like this:
class DrinkOrderForm extends BaseDrinkOrderForm
{
public function configure()
{
if ($this->getOption('hide_wine'))
{
$this->widgetSchema['wine'] = new sfWidgetFormInputHidden;
}
// … etc
}
}
And then when the action of the previous form submits you can pass options to the form, like:
$this->form = new DrinkOrderForm($drink_order, array(
'hide_wine' => true,
'hide_beer' => false,
));
This is just a quick example - instead of an ENUM type, you could use relations to another table (e.g. wine_id and an sfWidgetFormDoctrineChoice widget & validator).
One thing you can't do is have 3-4 separate forms, because web browsers will only submit one of them. You either have to embed forms within each other, or use the simpler technique above, depending on how your model's set up.
If the number of types of choice isn't fixed, then you'd want to look into using something like the form system's embedRelation method (or the ahDoctrineEasyEmbeddedRelationsPlugin) to dynamically add sub-forms. It's hard to know from your example just how far you want to go. :)

AS3: creating a class with multiple and optional parameters?

I'm creating a slideshow where each slide can have:
- a video or a still
- 1 audio track or many (up to 3)
- 1 button or many (up to 3)
I was thinking that each slide can be it's own object, and then I would pass the video, audio, buttons, etc., into it as parameters:
package
{
import flash.media.Video;
public class Section
{
public function Section (video:Video, still:myPhotoClass, audiotrack:Sound, button:myButtonClass) {
// can have video OR a still
// can have 1 audio track or several
// can have 1 button or more
}
}
I'm not sure how to go about approaching this since there can be multiples of certain items (audio, buttons) and also two items are sort-of-optional in the sense that there can be ONE or the OTHER (video/still).
For example, is this something that I should just avoid passing as parameters altogether, using a different approach (getters/setters, maybe)?
Try "...(rest) parameter"
private var _optionalParam:Array;
public function exOptionalParam(arg1:Number, ...optionalParam) {
_optionalParam = optionalParam;
trace(_optionalParam ); // [all the additional arguments]
}
Let's see how this goes:
You can add all of your parameters and set them to null so they aren't needed, eg: video:Video = null, still:myPhotoClass = null, audiotrack1:Sound = null, audiotrack2:Sound, audiotrack3:null, button1... etc (Simple test worked)
Or just pass an array for the ones with multiple items, or a vector of the right type.