Create CheckBox in a form

2 posts / 0 new
Last post
bruno
bruno's picture
Offline
Joined: 29 Jan 2010
Posts:
Create CheckBox in a form

Hi.

Here is the code for creating a checkbox :

Checkbox.h

#ifndef CHECKBOX_H_
#define CHECKBOX_H_

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

using namespace MAUI;

class CheckBox : public Widget {
  public:
    CheckBox(int x, int y, int height, int width, Widget* parent);
    ~CheckBox();

    void setResources(MAHandle unchecked, MAHandle checked);

    void drawWidget();
    void flip();
    bool isChecked();
    void setChecked (bool checked) {
    	_checked = checked;
    	updateCheckBox();
    }
  private:
    Label* text;
    Image* image;
    bool _checked;

    MAHandle resChecked;
    MAHandle resUnchecked;

    void updateCheckBox();
};

#endif /* CHECKBOX_H_ */

Checkbox.cpp

#include "Checkbox.h"

CheckBox::CheckBox(int x, int y, int width, int height, Widget* parent = NULL)
              : Widget(x, y, width, height, parent)
{
  image = new Image(0, 0, 20, 20, this);
  text = new Label(image->getWidth(), 0, width - image->getWidth(), height, this);

  _checked = false;
}

CheckBox::~CheckBox()
{}

void CheckBox::drawWidget()
{}

void CheckBox::setResources(MAHandle unchecked, MAHandle checked)
{
  resChecked = checked;
  resUnchecked = unchecked;

  updateCheckBox();
}

void CheckBox::flip()
{
   _checked = !_checked;
   updateCheckBox();
}

bool CheckBox::isChecked()
{
  return _checked;
}

void CheckBox::updateCheckBox()
{
  if(_checked)
     image->setResource(resChecked);
  else
     image->setResource(resUnchecked);
}

Then you add in the ressource file (You must draw the png files) :

.res RES_CHECKBOX_CHECKED
.image "chechbox_checked.png"

.res RES_CHECKBOX_UNCHECKED
.image "chechbox_unchecked.png"

Then to create one :

	label = createLabel("Visible", 32);
	CheckBox * checkBox1 = new CheckBox(scrWidth - 50,0,20, 20, label);
	checkBox1->setResources(RES_CHECKBOX_UNCHECKED, RES_CHECKBOX_CHECKED);
	listBox->add(label);

And :

void ConfigurationService::keyPressEvent(int keyCode) {
	switch(keyCode) {
			case MAK_FIRE:
				if (listBox->getSelectedIndex() ==1) {
					CheckBox * cb = (CheckBox *)((Label*)listBox->getChildren()[Box->getSelectedIndex()])->getChildren()[0];
					cb->flip();
				}
				break;
	}
}

[/]

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Anthony Hartley
Hartley's picture
Offline
Mobile Wizard
Joined: 25 Sep 2007
Posts:

Thanks Bruno,

Do you want to contribute this code back to the project ?

We are happy to take it Smile

And of course you'll be on the contrib list.

/Tony

PS. Have you read the contrib rules, http://www.mosync.com/contributions