Select menu in C++/MoSync?

11 posts / 0 new
Last post
turbo@bayour.com
turbo@bayour.com's picture
Offline
Mobile Conjurer
Joined: 25 Feb 2010
Posts:
Select menu in C++/MoSync?

I'm trying to convert a PHP application to a native mobile app (don't want to be/go online, otherwise I could just use the existing site Smile.

In this application, I have a <select> with three options... Does anyone have code examples for this?

NOTE: I'm not a C++ coder (yet Smile. I've coded in a whole bunch of other languages (such as Perl, PHP and C), but nothing major in C++. I can read it perfectly, and I can even modify existing C++ code, but I can't create something from scratch. Yet. So any help with this would be much appreciated!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Sam Pickard
rival's picture
Offline
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Hi, do you mean radio buttons? When you select one, the other two become unselected?

Sam Pickard
rival's picture
Offline
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Right, I've not really run this, but it does compile. You should be able to adapt it as you need to.

There are two objects, RadioButton which is the MAUI widget which will go on screen, and RadioButtonGroup which controls them. To use it, create RadioButtons for each of the options. Add them to a layout or a listbox if you want to. You *MUST* create two images, one for the selected button and one for the unselected button, and add the handles of the two images to each button. You can set the label on the radio button using setCaption.

Your code will look something like this (not tested, just written into the forum)

  ListBox* options = new ListBox(0, 0, 240, 320, NULL);
  RadioButton* option1 = new RadioButton(0, 0, 240, 32, options, RBSEL, RBUNSEL);//Image references from MAHeaders.h
  option1->setCaption("Red");

  RadioButton* option2 = new RadioButton(0, 0, 240, 32, options, RBSEL, RBUNSEL);//Image references from MAHeaders.h
  option2->setCaption("Blue");

  RadioButtonGroup* group = new RadioButtonGroup();
  group->addRadioButton(option1);
  group->addRadioButton(option2);
  group->setSelectedButton(0);
  

RadioButton.h

/*
 * RadioButton.h
 *
 *  Created on: 2 Mar 2010
 *      Author: sjp
 */

#ifndef RADIOBUTTON_H_
#define RADIOBUTTON_H_

#include <MAUI/Label.h>
#include <MAUI/Image.h>

using namespace MAUI;

class RadioButton : public Widget
{
  public:
    RadioButton(int x, int y, int width, int height, Widget* parent = NULL, MAHandle selectedImage = 0, MAHandle unselectedImage = 0);
    ~RadioButton();

    void setResources(MAHandle selectedImage, MAHandle unselectedImage);

    void drawWidget();

    void setSelected(bool selected);
    void setCaption(String& caption);
  private:
    Image* _button;
    Label* _label;
    bool _selected;
    MAHandle _sel;
    MAHandle _unsel;
};
#endif /* RADIOBUTTON_H_ */

RadioButton.cpp

/*
 * RadioButton.cpp
 *
 *  Created on: 2 Mar 2010
 *      Author: sjp
 */
#include "RadioButton.h"

RadioButton::RadioButton(int x, int y, int width, int height, Widget* parent, MAHandle selectedImage, MAHandle unselectedImage)
            : Widget(x, y, width, height, parent),
              _sel(selectedImage),
              _unsel(unselectedImage)
{
  _button = new Image(0, 0, 20, 20, this, true, true, unselectedImage);
  _label = new Label(_button->getWidth(), 0, width - _button->getWidth(), height, this);
}

RadioButton::~RadioButton()
{}

void RadioButton::drawWidget()
{}

void RadioButton::setSelected(bool selected)
{
  _selected = selected;

  if(_selected && _sel != NULL)
    _button->setResource(_sel);
  if(!_selected && _unsel != NULL)
    _button->setResource(_unsel);
}

void RadioButton::setResources(MAHandle selectedImage, MAHandle unselectedImage)
{
  _sel = selectedImage;
  _unsel = unselectedImage;
}

void RadioButton::setCaption(String& caption)
{
  _label->setCaption(caption);
}

RadioButtonGroup.h

/*
 * RadioButtonGroup.h
 *
 *  Created on: 2 Mar 2010
 *      Author: sjp
 */

#ifndef RADIOBUTTONGROUP_H_
#define RADIOBUTTONGROUP_H_

#include "RadioButton.h"
#include <MAUtil/Vector.h>
using namespace MAUtil;

class RadioButtonGroup
{
  public:
    RadioButtonGroup(){};
    ~RadioButtonGroup(){};

    void addRadioButton(RadioButton* rb);
    void setSelectedButton(int index);

  private:
    Vector<RadioButton*> _buttons;

};
#endif /* RADIOBUTTONGROUP_H_ */

RadioButtonGroup.cpp

/*
 * RadioButtonGroup.cpp
 *
 *  Created on: 2 Mar 2010
 *      Author: sjp
 */

#include "RadioButtonGroup.h"

void RadioButtonGroup::addRadioButton(RadioButton* rb)
{
   _buttons.add(rb);
}

void RadioButtonGroup::setSelectedButton(int index)
{
  int ctr = 0;
  Vector_each(RadioButton*, itr, _buttons)
   (*itr)->setSelected(ctr++ == index ? true : false);
}
turbo@bayour.com
turbo@bayour.com's picture
Offline
Mobile Conjurer
Joined: 25 Feb 2010
Posts:

I could live with a radio button, I guess. I'm currently in the process of implementing a radio button myself, but I'd rather have the <select> <option> combo if anyone have an example...

Sam Pickard
rival's picture
Offline
Mobile Archmage
Joined: 19 Mar 2009
Posts:

I see, a drop down list (not everyone can read HTML). It's a bit tricker to do, as you'll have to pop it up over other widgets. The best thing to do is to put in a normal listbox with the options for the user to select. You can make it drop down a list, but it will be 100x the effort.

turbo@bayour.com
turbo@bayour.com's picture
Offline
Mobile Conjurer
Joined: 25 Feb 2010
Posts:

Thanx a milion for the RadioBox code. It actually turned out just fine, helped a lot!

Now I'm almost ashamed to ask for more Smile. I get some weird redraw problem... I have my RadioButtons (Image+Label) inside a Listbox which is inside a Label which is inside a ListBox and then another Label.

Layout
  Listbox
    Label
      EditBox
    ....
    Label
      ListBox
        Image
        Label
    Label
      EditBox
    ....

I know, i know..

Anyway, when the screen is drawn, the first RadioButton is about 160 pixels down from Top. I can see all three RadioButtons, then the screen ends (scrolls to next Label/EditBox pair). The whole screen (initial Label/ListBox) is about 480 pixels high (scrollable ListBox). The Screen looks something like this:

LABEL1
  Editbox1
LABEL2
  Editbox2
LABEL3
  RadioButton1 RadioButtonLabel1
  RadioButton2 RadioButtonLabel2
  RadioButton3 RadioButtonLabel3
LABEL4
  EditBox4
....

The problem is that when i leave RadioButton[1-3] (I can leave the whole LABEL3 Label/ListBox by MAK_RIGHT), RadioButton[12] (including the corresponding label) vanishes! It won't show up again until I'm at LABEL1/Editbox1 again!!
What's weird is that RadioButton3 and it's label is still visable!! And if I venture further down (so that LABEL3 is still visable), then RadioButton3 also vanishes...

I tried to do a force (re)draw on the Initial Label, but that made things worse...

Sam Pickard
rival's picture
Offline
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Is it possible for you to post a screenshot before and after - I'm not quite sure what is vanishing.

Also, is there a container for these widgets? Are you putting them all into a listbox or a layout?

Thanks

turbo@bayour.com
turbo@bayour.com's picture
Offline
Mobile Conjurer
Joined: 25 Feb 2010
Posts:

Initial screen:
First problem:
Even worse:

As you can see, the RadioButtons in the Label/ListBox combo named 'Axeltyp, framaxel' is lost once it starts scrolling.. ('Axeltyp, bakaxel' will look exacly the same, but I want to get the first Radiobutton combo working first).

This whole screen is a Lable/ListBox, so the radiobutton(Drunk have four layers to go through to get to the top. See post 5: http://www.mosync.com/forum/viewtopic.php?f=19&t=507#p1044

Sam Pickard
rival's picture
Offline
Mobile Archmage
Joined: 19 Mar 2009
Posts:

OK, I think that this is a bug in the implementation of ListBox. It occurs when a ListBox is an element of another ListBox. The sub listbox is making incorrect decisions about which elements are on screen.[attachment=1]ListBoxBug1.png[/attachment][attachment=0]ListBoxBug2.png[/attachment]

I've registered a bug with the developers, so you should hear from them soon.

ListBoxBug1.png ListBoxBug2.png
Sam Pickard
rival's picture
Offline
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Just for the sake of the archives, this bug has now been fixed.

noddy
noddy's picture
Offline
Mobile Conjurer
Joined: 20 Aug 2010
Posts:

Hello all,

I am trying to implement a similar radio box, i.e. one ListBox inside another. But I am struggling with events
keyPressEvent / pointerPressEvent / selectionChanged. Does anyone have a sample to demonstrate this?

Thank you.