////////////////////////////////////////////////////////////////////////////////
// GLCheckBox.h
// Copyright Bjoern Ganster 2009
////////////////////////////////////////////////////////////////////////////////

#ifndef GLCheckBox__H
#define GLCheckBox__H

#include "BGCPString.h"
#include "GLButton.h"
#include "GLLabel.h"

////////////////////////////////////////////////////////////////////////////////
// Base class for a button

class GLCheckBox: public GLWindowContainer {
public:
   // Constructor
   GLCheckBox (GLWindow* _parent = NULL, int _left = 0, int _top = 0, 
             int _width = 0, int _height = 0)
   : GLWindowContainer ("GLCheckBox", GLCheckBox_ID, _parent, _left, _top, _width, _height)
   {
      button = new GLButton (_parent, left, 0, _height, _height);
      add (button);
      button->setListener (this);
      label = new GLLabel (_parent, _left+2*height, 0, _width-2*height, _height);
      add (label);
   }

   // Destructor
   ~GLCheckBox() OVERRIDE
   {}

   // Title access
   const BGCPString& getTitle () const
   { return label->getTitle(); }
   void setTitle (const BGCPString& newTitle) 
   { label->setTitle (newTitle); }

   virtual void receiveMessage (VirtualClass* sender, int MessageType, VirtualClass* data = nullptr)
   {
      if (sender == button && MessageType == ButtonClickedEvent) {
         if (isChecked()) {
            button->setTitle ("");
            if (listener != NULL)
               listener->receiveMessage(this, CheckBoxUnchecked, data);
         } else {
            button->setTitle ("X");
            if (listener != NULL)
               listener->receiveMessage(this, CheckBoxChecked, data);
         }
      }
   }

   bool isChecked () const
   { 
      return button->getTitle() == "X";
   }
   void setChecked (bool newVal)
   {
         if (newVal)
            button->setTitle ("X");
         else
            button->setTitle ("");
   }

protected:
   GLLabel* label;
   GLButton* button;
};

#endif
