//+------------------------------------------------------------------+
//|                                             ChangeSymbol-All.mq4 |
//|                                         Copyright © 2011, zznbrm |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, zznbrm"
#property show_inputs

#import "user32.dll"
   int  SendMessageA(int hWnd,int Msg,int wParam,int lParam);
   int  GetWindow(int hWnd,int uCmd);
   int  GetParent(int hWnd);
   void keybd_event( int bVk, int bScan, int dwFlags, int dwExtraInfo );
#import

#define GW_HWNDFIRST       0
#define GW_HWNDNEXT        2
#define WM_MDIACTIVATE     0x0222
#define VK_RETURN          0x0D
#define VK_BACK            0x08
#define VK_ESCAPE          0x1B
#define VK_CAPITAL         0x14
#define VK_COMMA           0xBC
#define KEYEVENTF_KEYUP    0x0002

extern string estrSymbol = "eurusd";
extern int eintSleep = 50;

int gaintCharts[0];
    
//+------------------------------------------------------------------+
//| expert Start function                                            |
//+------------------------------------------------------------------+               
int start()
{      
   string strSymbol = upper( estrSymbol );
   Print( "Changing " + populateChartArray() + " charts to: " + strSymbol );
      
   // Change the symbol for all charts except for the one this script is attached to
   for ( int inx = 0; inx < ArraySize( gaintCharts ) - 1; inx++ )
   {
      SendMessageA( GetParent( gaintCharts[inx] ), WM_MDIACTIVATE, gaintCharts[inx], 0 );
      Sleep( eintSleep );
      changeChart( strSymbol, "" );
      Sleep( eintSleep );
   }
   
   // Change the symbol for the chart running this script
   SendMessageA( GetParent( gaintCharts[inx] ), WM_MDIACTIVATE, gaintCharts[inx], 0 );
   Sleep( eintSleep );
   changeChart( strSymbol, "" );
   
   return( 0 );  
}

//+------------------------------------------------------------------+
//| populateChartArray                                               |
//+------------------------------------------------------------------+
int populateChartArray()
{
   int intCount = 0;
   int intCurrentChart = GetParent( WindowHandle( Symbol(), Period() ) );  
   
   if ( intCurrentChart <= 0 )  return( 0 );
   
   int intChild = GetWindow( intCurrentChart, GW_HWNDFIRST );
   
   while ( intChild > 0 )
   {
      if ( intChild != intCurrentChart )
      {
         intCount++;
      
         ArrayResize( gaintCharts, intCount );
      
         gaintCharts[intCount-1] = intChild;
      }
      
      intChild = GetWindow( intChild, GW_HWNDNEXT );
   }
   
   intCount++;
   ArrayResize( gaintCharts, intCount );
      
   gaintCharts[intCount-1] = intCurrentChart;
   
   return( intCount );
}

//+------------------------------------------------------------------+
//| changeChart                                                      |
//+------------------------------------------------------------------+
void changeChart( string strSymbol, string strPeriod )
{
   // We are not sure if the symbol entry field is currently visible.
   // So, let's send the ESC key to remove it just in case.
   sendKey( VK_ESCAPE );
   
   // Display the symbol entry field - use the ENTER key for this
   sendKey( VK_RETURN );
   
   // The symbol entry field may already have data in it.
   // Send some BACK keys (15 should be enough for most use cases)
   // to erase this data
   sendMultipleKeys( VK_BACK, 15 );
   
   // Turn on caps lock
   sendKey( VK_CAPITAL );
   
   // Send each character of symbol string
   sendKeysForString( strSymbol );
   
   if ( StringLen( strPeriod ) > 0 )   
   {
      // Send comma to seperate symbol from Period
      sendKey( VK_COMMA );
      
      // Send each character of period string
      sendKeysForString( strPeriod );
   }
   
   // Turn off caps lock
   sendKey( VK_CAPITAL );
   
   // Submit the data - use the ENTER key for this
   sendKey( VK_RETURN );
}

//+------------------------------------------------------------------+
//| upper                                                            |
//+------------------------------------------------------------------+
string upper( string input )
{
   string str = input;
   int intChar;
   int intLen = StringLen( str ) - 1;
   
   for ( int inx = 0; inx < StringLen( str ); inx++ )
   {
       intChar = StringGetChar( str, inx );
       
       if( ( intChar > 96 ) && ( intChar < 123 ) )
       {
           str = StringSetChar( str, inx, intChar - 32 );       
       }
   }
   
   return( str );
}

//+------------------------------------------------------------------+
//| sendKeysForString                                                |
//+------------------------------------------------------------------+
void sendKeysForString( string str )
{
   // Loop through all characters of the string, sending a key for each
   for ( int jnx = 0; jnx < StringLen( str ); jnx++ )
   {
      sendKey( StringGetChar( str, jnx ) );
   }
}

//+------------------------------------------------------------------+
//| sendMultipleKeys                                                 |
//+------------------------------------------------------------------+
void sendMultipleKeys( int intKey, int intCount )
{
   // Send the key multiple times
   for ( int inx = 0; inx < intCount; inx++ )
   {
      sendKey( intKey );
   }
}

//+------------------------------------------------------------------+
//| sendKey                                                          |
//+------------------------------------------------------------------+
void sendKey( int intKey )
{
   keybd_event( intKey, 0, 0, 0 );
   keybd_event( intKey, 0, KEYEVENTF_KEYUP, 0 );
}



