!==================================================================================
! Класс для фиксирования багов в с11.
! Вячеслав Черников, г.Рязань, 2021г.
!==================================================================================

                     MEMBER

   INCLUDE('fsFix_C11.INC')
   INCLUDE('KEYCODES.CLW'),ONCE 

   MAP
     INCLUDE('BUILTINS.CLW'),ONCE   !функции RTL 
   END

!===================================================================================
! Инициализация класса
!
FsFix_C11.Init PROCEDURE 
     CODE

        loop SELF.Contr=1 to lastfield()    
            case SELF.Contr{prop:type}
               of CREATE:entry
             orof CREATE:spin  
               SELF.ContrFormat = SELF.Contr{prop:text}
               case sub(SELF.ContrFormat,1,1)
                  of 'N'
                orof 'n'
                  SELF.Contr{PROP:ALRT}=BSKey
                  SELF.Contr{PROP:ALRT}=DecimalKey
                  SELF.Contr{PROP:ALRT}=MouseLeft2 
               end    
            end    
        end    


!===================================================================================
! Деактивация класса
!
FsFix_C11.Kill PROCEDURE
      CODE

!===================================================================================
!
! Деструктор класса
!
FsFix_C11.Destruct PROCEDURE
   CODE
      SELF.Kill

!===================================================================================
! Обработка позиционирования в цифровых полях 
!
FsFix_C11.EventSelectNums PROCEDURE 
   CODE
      if event()<>event:selected
         return
      end 
        
      SELF.Contr = field()
      if SELF.Contr = 0
         return
      end      
        
      case SELF.Contr{prop:type}
         of CREATE:entry
      orof CREATE:spin          
      else
           return
      end      
        
      SELF.ContrFormat = SELF.Contr{prop:text}
      case sub(SELF.ContrFormat,1,1)
         of 'N'
       orof 'n'
         select(SELF.Contr,1) 
         select(SELF.Contr)       
      end      
        
!===================================================================================
! Обработка переключения фокуса приложения в цифровых полях 
!
FsFix_C11.EventGainFocusNums PROCEDURE 
   CODE
      if event()<>event:gainFocus
         return
      end 
        
      SELF.Contr = focus()
      if SELF.Contr = 0
         return
      end      
        
      case SELF.Contr{prop:type}
         of CREATE:entry
      orof CREATE:spin          
      else
           return
      end             
        
      SELF.ContrFormat = SELF.Contr{prop:text}
      case sub(SELF.ContrFormat,1,1)
         of 'N'
       orof 'n'
         select(SELF.Contr,1) 
      end              
        
!===================================================================================
! Обработка клавиш в цифровых полях
!
FsFix_C11.EventPreAlertNums PROCEDURE 
   CODE
      if event()<>EVENT:PreAlertKey
         return
      end 
        
      SELF.Contr = field()
      if SELF.Contr = 0
         return
      end      
        
      case SELF.Contr{prop:type}
         of CREATE:entry
      orof CREATE:spin          
      else
           return
      end             
        
      SELF.ContrFormat = SELF.Contr{prop:text}
      case sub(SELF.ContrFormat,1,1)
         of 'N'        
       orof 'n'        
         if lower(sub(SELF.ContrFormat,len(clip(SELF.ContrFormat)),1))='b'  !убираем бланк
            SELF.ContrFormat=sub(SELF.ContrFormat,1,len(clip(SELF.ContrFormat))-1)    
         end     
         case keycode()
            of MouseLeft2
               select(SELF.Contr)    
            of DecimalKey
               if instring('.',SELF.ContrFormat,1,1)>0 
                  SELF.ContrText=evaluate('format(' & clip(SELF.Contr{prop:value}) & ',@' & clip(SELF.ContrFormat) & ')') 
                  SELF.ContrDecimal=instring('.',SELF.ContrText,1,1)    
                  select(SELF.Contr,SELF.ContrDecimal+1)    
               end     
            else   
               SELF.ContrText=evaluate('format(' & clip(SELF.Contr{prop:value}) & ',@' & clip(SELF.ContrFormat) & ')') 
               SELF.ContrDecimal=instring('.',SELF.ContrText,1,1)                 
               if SELF.Contr{PROP:SelStart}=SELF.ContrDecimal and SELF.Contr{prop:ins}=1
                  SELF.ContrValue = SELF.Contr{prop:value}
                  if SELF.ContrValue>=1   
                     SELF.ContrValueInt=int(SELF.ContrValue)/10 
                     SELF.ContrValue=SELF.ContrValueInt+SELF.ContrValue-int(SELF.ContrValue) 
                     change(SELF.Contr,SELF.ContrValue) 
                     select(SELF.Contr) 
                     select(SELF.Contr,SELF.ContrDecimal) 
                  .    
               elsif SELF.Contr{PROP:SelStart}=SELF.ContrDecimal+1
                  SELF.ContrPosition= SELF.Contr{PROP:SelStart} - 1 
                  select(SELF.Contr) 
                  select(SELF.Contr,SELF.ContrPosition)         
               elsif SELF.Contr{PROP:SelStart}>1 
                  SELF.ContrValue = SELF.Contr{prop:value}   
                  SELF.ContrPosition= SELF.Contr{PROP:SelStart} - 1 
                  SELF.ContrText=sub(SELF.ContrText,1,SELF.ContrPosition-1) & '0' & sub(SELF.ContrText,SELF.ContrPosition+1,len(clip(SELF.ContrText))-SELF.ContrPosition)   
                  SELF.ContrValue=SELF.ContrText 
                  change(SELF.Contr,SELF.ContrValue) 
                  select(SELF.Contr) 
                  select(SELF.Contr,SELF.ContrPosition)         
               end      
         end  
      end    
        
        
        
            