EnableExplicit
;{- Enumerations / DataSections
;{ Windows
Enumeration
  #WindowOptionAccessMemoireVideo
EndEnumeration
;}
;{ Gadgets
Enumeration
  #ComboBox_Colonne
  #Text_Colonne
  #Text_Ligne
  #Text_Page
  #ComboBox_Ligne
  #ComboBox_Page
  #Text_TexteAAfficher
  #Editor_TexteAAfficher
  #Text_Code
  #Editor_Code
  #Button_CopierCode
  #Text_ColxLig
  #Button_TranslateCode
  #Text_couleurTexte
  #ComboBox_CouleurTexte
  #ComboBox_CouleurduFond
  #Text_CouleurduFond
EndEnumeration
;}
; Define.l Event, EventWindow, EventGadget, EventType, EventMenu, x, y
; Define Range.CHARRANGE
;}

Procedure OpenWindowMemoireAccesVideoBoutonAffiche()
  Protected page=Val(GetGadgetText(#ComboBox_Page))
  Protected colonne=Val(GetGadgetText(#ComboBox_Colonne))
  Protected ligne=Val(GetGadgetText(#ComboBox_Ligne))
  Protected couleurTexte=GetGadgetState(#ComboBox_CouleurTexte)
  Protected couleurFond=GetGadgetState(#ComboBox_CouleurduFond)
  Protected chaine.s, Ligne1.s, Ligne2.s, c$
  Protected Tout_Est_Ok.b, i, coordonnee00, ligneaafficher
  
  Tout_Est_Ok=#True
  If CountGadgetItems(#Editor_TexteAAfficher)>25
    Tout_Est_Ok=#False
    MessageRequester("Alerte", "Ne pas dépasser 25 lignes svp.")
  EndIf
  
  For i = 0 To  CountGadgetItems(#Editor_TexteAAfficher)-1
    If Len(GetGadgetItemText(#Editor_TexteAAfficher, i))>80
      Tout_Est_Ok=#False
      MessageRequester("Alerte", "Ne pas dépasser 80 caractères svp.")
    EndIf
  Next i
  
  If Tout_Est_Ok=#True      
    
    
    ;$B8000=753664
    coordonnee00= $B8000 + page*4000 + ligne*160 + colonne
    
    For ligneaafficher = 0 To CountGadgetItems(#Editor_TexteAAfficher)
      chaine=GetGadgetItemText(#Editor_TexteAAfficher, ligneaafficher)
      ;chaine.s="Noyau Chargé."
      Ligne1=""
      Ligne2=""
      
      For i = 0 To Len(chaine)-1 
        c$=Mid(chaine,i+1,1)
        ;l1=
        ligne1 + "mov byte [0x" + Hex(coordonnee00+2*i, #PB_Long) + "], '" + c$ + "'" 
        ligne2 = "mov byte [0x" + Hex(coordonnee00+2*i+1, #PB_Long) + "], 0x" + Hex(couleurFond) + Hex(couleurTexte)
        AddGadgetItem(#Editor_Code, -1, ligne1)
        AddGadgetItem(#Editor_Code, -1, ligne2)
        
        ligne1=""
        
      Next i
      coordonnee00 + 160
    Next ligneaafficher
    
  EndIf
  
  ; 
  ; mov byte [0xB8280], 'H'
  ; mov byte [0xB8281], 0xE2
EndProcedure

Procedure OpenWindowMemoireAccesVideoBoutonCopie()
  ClearClipboard()
  SetClipboardText(GetGadgetText(#Editor_Code))
EndProcedure


Procedure OpenWindowMemoireAccesVideo()
  Protected Dim couleur(16)
  Protected Dim couleur_de_Base(16)
  Protected i
  Protected.l Event, EventWindow, EventGadget, EventType, EventMenu, x, y
Protected Range.CHARRANGE
  ;     Black $000000
  ;     bleufonce=$800000
  ;     vert foncé=$008000
  ;     bleuvert=$808000
  ;     maroon$000080
  ;     mauve=$800080
  ;     jaune fonce=$008080
  ;     Gray$C0C0C0
  ;     grisfonce=$808080
  ;     Blue=$FF0000
  ;     Green=$00FF00
  ;     Cyan=$FFFF00
  ;     Red=$0000FF
  ;     Magenta=$FF00FF
  ;     Yellow=$00FFFF
  ;     White=$FFFFFF
  couleur_de_Base(0) = $000000
  couleur_de_Base(1) = $800000
  couleur_de_Base(2) = $008000
  couleur_de_Base(3) = $808000
  couleur_de_Base(4) = $000080
  couleur_de_Base(5) = $800080
  couleur_de_Base(6) = $008080
  couleur_de_Base(7) = $C0C0C0
  couleur_de_Base(8) = $808080
  couleur_de_Base(9) = $FF0000
  couleur_de_Base(10) = $00FF00
  couleur_de_Base(11) = $FFFF00
  couleur_de_Base(12) = $0000FF
  couleur_de_Base(13) = $FF00FF
  couleur_de_Base(14) = $00FFFF
  couleur_de_Base(15) = $FFFFFF
  
  If OpenWindow(#WindowOptionAccessMemoireVideo, 383, 22, 568, 602, "Code Accès direct à la Mémoire  Vidéo", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
    HideWindow(#WindowOptionAccessMemoireVideo,1)
    TextGadget(#Text_Colonne, 20, 5, 80, 25, "Colonne :")
    ComboBoxGadget(#ComboBox_Colonne, 20, 35, 80, 26); 
    For i=0 To 79
      AddGadgetItem(#ComboBox_Colonne, -1, Str(i))
    Next i
    SetGadgetState(#ComboBox_Colonne, 0)
    
    TextGadget(#Text_Ligne, 105, 5, 80, 25, "Ligne :")
    ComboBoxGadget(#ComboBox_Ligne, 105, 35, 75, 26)
    For i=0 To 24
      AddGadgetItem(#ComboBox_Ligne, -1, Str(i))
    Next i
    SetGadgetState(#ComboBox_Ligne, 0)
    
    TextGadget(#Text_Page, 185, 5, 80, 26, "Page :")
    ComboBoxGadget(#ComboBox_Page, 185, 35, 75, 26)
    For i=0 To 3
      AddGadgetItem(#ComboBox_Page, -1, Str(i))
    Next i
    SetGadgetState(#ComboBox_Page, 0)
    
    
    
    TextGadget(#Text_TexteAAfficher, 20, 90, 175, 25, "Texte à Afficher (80X25)")
    TextGadget(#Text_ColxLig, 200, 90, 300, 25, "00x00")
    EditorGadget(#Editor_TexteAAfficher, 20, 115, 530, 135)
    
    TextGadget(#Text_Code, 25, 275, 150, 25, "Code ")
    EditorGadget(#Editor_Code, 20, 300, 530, 230);, #PB_Editor_ReadOnly
    ButtonGadget(#Button_CopierCode, 20, 540, 530, 45, "Copie le Code dans le Presse-Papier")
    
    ButtonGadget(#Button_TranslateCode, 220, 260, 110, 30, "Go")
    
    For i=0 To 15
      couleur(i)=CreateImage(#PB_Any, 16, 16)
      StartDrawing( ImageOutput(couleur(i)))
      ;DrawingMode(#PB_2DDrawing_Transparent)
      Box(0,0,16,16,couleur_de_Base(i));bvr
      StopDrawing()
    Next i
    TextGadget(#Text_couleurTexte, 290, 5, 120, 25, "Couleur du Texte :")
    ComboBoxGadget(#ComboBox_CouleurTexte, 290, 35, 115, 26,#PB_ComboBox_Image)
    TextGadget(#Text_CouleurduFond, 425, 5, 120, 25, "Couleur du Fond :")
    ComboBoxGadget(#ComboBox_CouleurduFond, 430, 35, 115, 26,#PB_ComboBox_Image)
    For i=0 To 15
      AddGadgetItem(#ComboBox_couleurTexte, -1, "", ImageID(couleur(i)))
      AddGadgetItem(#ComboBox_CouleurduFond, -1, "", ImageID(couleur(i)))
      
    Next i  
    SetGadgetState(#ComboBox_couleurTexte, 15)
    SetGadgetState(#ComboBox_CouleurduFond, 0)
  EndIf
  HideWindow(#WindowOptionAccessMemoireVideo,0)
  
  ;{- Event loop
Repeat
  Event = WaitWindowEvent()
  Select Event
      ; ///////////////////
    Case #PB_Event_Gadget
      EventGadget = EventGadget()
      EventType = EventType()
      If EventGadget = #Button_CopierCode
        OpenWindowMemoireAccesVideoBoutonCopie()
        
      ElseIf EventGadget = #Button_TranslateCode
        OpenWindowMemoireAccesVideoBoutonAffiche()
       
      EndIf
      ; ////////////////////////
    Case #PB_Event_CloseWindow
      EventWindow = EventWindow()
      If EventWindow = #WindowOptionAccessMemoireVideo
        CloseWindow(#WindowOptionAccessMemoireVideo)
        Break
      EndIf
  EndSelect
  ; Affiche colonne et ligne
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    SendMessage_(GadgetID(#Editor_TexteAAfficher),#EM_EXGETSEL,0,Range) 
    x=Range\cpMax-(SendMessage_(GadgetID(#Editor_TexteAAfficher),#EM_LINEINDEX,SendMessage_(GadgetID(#Editor_TexteAAfficher),#EM_EXLINEFROMCHAR,0,Range\cpMin),0))+1 
    SendMessage_(GadgetID(#Editor_TexteAAfficher),#EM_EXGETSEL,0,Range.CHARRANGE) 
    y= SendMessage_(GadgetID(#Editor_TexteAAfficher),#EM_EXLINEFROMCHAR,0,Range\cpMin)+1
    SetGadgetText(#Text_ColxLig,Str(x)+"/"+Str(y)) 
    If x>80
      SetGadgetText(#Text_ColxLig,"Ne pas dépasser 80 caractères svp.")
    EndIf
    If y>25
      SetGadgetText(#Text_ColxLig, "Ne pas dépasser 25 lignes svp.")
    EndIf 
  CompilerEndIf
  
  
ForEver
EndProcedure

;}

OpenWindowMemoireAccesVideo()


;


; IDE Options = PureBasic 4.61 (Windows - x86)
; CursorPosition = 237
; FirstLine = 204
; Folding = --
; EnableXP
; CompileSourceDirectory