//+------------------------------------------------------------------+
//| Properties                                                       |
//+------------------------------------------------------------------+
#property version   "1.01"
//+------------------------------------------------------------------+
//| Include files and define object                                  |
//+------------------------------------------------------------------+
#include <Object Controls\CDropList.mqh>
CDropList drop;
//+------------------------------------------------------------------+
//| Global variables                                                 |
//+------------------------------------------------------------------+
int  g_scroll_off_count          = 0;
bool g_scroll_original           = true;
bool g_scroll_applied            = false; // whether we have changed it from original
bool g_previous_mouse_move_event = false;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Set chart properties and save previous states
   g_previous_mouse_move_event = ChartGetInteger(ChartID(), CHART_EVENT_MOUSE_MOVE, 0);
   g_scroll_original           = (bool)ChartGetInteger(ChartID(), CHART_MOUSE_SCROLL, 0);
   ChartSetInteger(ChartID(), CHART_EVENT_MOUSE_MOVE, true);

//--- Set the prefix before creating the control so that the objects
//--- are created with the correct names from the start.
   if(!drop.Prefix("asadi"))
     {
      Print("OnInit: failed to set prefix");
      return(INIT_FAILED);
     }

//--- Create the drop-down object
   if(!drop.Create())
     {
      Print("OnInit: failed to create drop-down control");
      return(INIT_FAILED);
     }

//--- Configure the number of visible items
   if(!drop.ShowItems(3))
     {
      Print("OnInit: failed to set visible item count");
      return(INIT_FAILED);
     }

//--- Add items
   drop.AddItem("White Chart");
   drop.AddItem("Black Chart");
   drop.AddItem("Blue Chart");
   drop.AddItem("Brown Chart");
   drop.AddItem("Other");

//--- Determine the desired initial selection from the chart background
   color bg = (color)ChartGetInteger(ChartID(), CHART_COLOR_BACKGROUND, 0);
   string desired_mode = "Other";
   switch(bg)
     {
      case clrWhite:  desired_mode = "White Chart";  break;
      case clrBlack:  desired_mode = "Black Chart";  break;
      case clrBlue:   desired_mode = "Blue Chart";   break;
      case clrBrown:  desired_mode = "Brown Chart";  break;
      default:        desired_mode = "Other";        break;
     }

//--- Sync the control selection with the chart background
   if(drop.SelectedItem() != desired_mode)
     {
      if(!drop.SelectedItem(desired_mode))
        {
         Print("OnInit: failed to set initial selection to ", desired_mode);
         // Not fatal; the control will keep its default selection.
        }
     }

   ChartRedraw();

//--- Return the initialization result
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- Delete the drop-down object
   drop.Destroy();
//--- Restore default mouse move event and chart scroll
   ChartSetInteger(ChartID(), CHART_MOUSE_SCROLL, g_scroll_original);
   ChartSetInteger(ChartID(), CHART_EVENT_MOUSE_MOVE, g_previous_mouse_move_event);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int     id,
                  const long   &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//--- Change the chart background color based on the selection
   ENUM_CHARTEVENT_DROPLIST droplist_retcode = drop.ChartEvents(id, lparam, dparam, sparam);
   string mode = drop.SelectedItem();
   if(droplist_retcode==DROPLIST_EVENT_ITEM_CLICK)
     {
      if(mode=="White Chart")
         ChartSetInteger(ChartID(), CHART_COLOR_BACKGROUND, clrWhite);
      else
         if(mode=="Black Chart")
            ChartSetInteger(ChartID(), CHART_COLOR_BACKGROUND, clrBlack);
         else
            if(mode=="Blue Chart")
               ChartSetInteger(ChartID(), CHART_COLOR_BACKGROUND, clrBlue);
            else
               if(mode=="Brown Chart")
                  ChartSetInteger(ChartID(), CHART_COLOR_BACKGROUND, clrBrown);
     }
//--- Keep the drop-down in sync if the user changes the chart background manually
   if(id==CHARTEVENT_CHART_CHANGE)
     {
      color bg = (color)ChartGetInteger(ChartID(), CHART_COLOR_BACKGROUND, 0);
      string new_mode = "Other";
      switch(bg)
        {
         case clrWhite:
            new_mode = "White Chart";
            break;
         case clrBlack:
            new_mode = "Black Chart";
            break;
         case clrBlue:
            new_mode = "Blue Chart";
            break;
         case clrBrown:
            new_mode = "Brown Chart";
            break;
         default:
            new_mode = "Other";
            break;
        }
      if(mode != new_mode)
         drop.SelectedItem(new_mode);
     }
   if(id==CHARTEVENT_MOUSE_MOVE)
     {
      //--- Handle the request to disable chart scrolling
      if(droplist_retcode==DROPLIST_EVENT_SCROLL_OFF)
        {
         g_scroll_off_count++;
         //--- If this is the first control to request it, save the original state
         //--- and actually disable chart scrolling
         if(g_scroll_off_count == 1)
           {
            g_scroll_original = (bool)ChartGetInteger(ChartID(), CHART_MOUSE_SCROLL, 0);
            ChartSetInteger(ChartID(), CHART_MOUSE_SCROLL, false);
            g_scroll_applied = true;
           }
        }
      //--- Handle the request to re-enable chart scrolling
      else
         if(droplist_retcode==DROPLIST_EVENT_SCROLL_ON)
           {
            //--- Decrement the counter, but never below zero
            if(g_scroll_off_count > 0)
               g_scroll_off_count--;
            //--- If no control needs scrolling disabled anymore and we had changed it,
            //--- restore the original chart scroll state
            if(g_scroll_off_count == 0 && g_scroll_applied)
              {
               ChartSetInteger(ChartID(), CHART_MOUSE_SCROLL, g_scroll_original);
               g_scroll_applied = false;
              }
           }
     }
   if(droplist_retcode!=DROPLIST_EVENT_NO)
     ChartRedraw();
  }
//+------------------------------------------------------------------+
