FORUM DE DISCUSSION SUR LE LANGAGE PANORAMIC
Vous souhaitez réagir à ce message ? Créez un compte en quelques clics ou connectez-vous pour continuer.
FORUM DE DISCUSSION SUR LE LANGAGE PANORAMIC

Développement d'applications avec le langage Panoramic
 
AccueilAccueil  RechercherRechercher  Dernières imagesDernières images  S'enregistrerS'enregistrer  MembresMembres  Connexion  
Derniers sujets
» Gestion d'un système client-serveur.
Simulation d'un objet TreeView - Page 2 Emptypar Klaus Ven 17 Mai 2024 - 14:02

» item_index(résolu)
Simulation d'un objet TreeView - Page 2 Emptypar jjn4 Mar 14 Mai 2024 - 19:38

» Bataille terrestre
Simulation d'un objet TreeView - Page 2 Emptypar jjn4 Lun 13 Mai 2024 - 15:01

» SineCube
Simulation d'un objet TreeView - Page 2 Emptypar Marc Sam 11 Mai 2024 - 12:38

» Editeur EliP 6 : Le Tiny éditeur avec 25 onglets de travail
Simulation d'un objet TreeView - Page 2 Emptypar Marc Sam 11 Mai 2024 - 12:22

» Philharmusique
Simulation d'un objet TreeView - Page 2 Emptypar jjn4 Ven 10 Mai 2024 - 13:58

» PANORAMIC V 1
Simulation d'un objet TreeView - Page 2 Emptypar papydall Jeu 9 Mai 2024 - 3:22

» select intégrés [résolu]
Simulation d'un objet TreeView - Page 2 Emptypar jjn4 Mer 8 Mai 2024 - 17:00

» number_mouse_up
Simulation d'un objet TreeView - Page 2 Emptypar jjn4 Mer 8 Mai 2024 - 11:59

» Aide de PANORAMIC
Simulation d'un objet TreeView - Page 2 Emptypar jjn4 Mer 8 Mai 2024 - 11:16

» trop de fichiers en cours
Simulation d'un objet TreeView - Page 2 Emptypar lepetitmarocain Mer 8 Mai 2024 - 10:43

» Je teste PANORAMIC V 1 beta 1
Simulation d'un objet TreeView - Page 2 Emptypar papydall Mer 8 Mai 2024 - 4:17

» bouton dans autre form que 0(résolu)
Simulation d'un objet TreeView - Page 2 Emptypar leclode Lun 6 Mai 2024 - 13:59

» KGF_dll - nouvelles versions
Simulation d'un objet TreeView - Page 2 Emptypar Klaus Lun 6 Mai 2024 - 11:41

» @Jack
Simulation d'un objet TreeView - Page 2 Emptypar Jack Mar 30 Avr 2024 - 20:40

Navigation
 Portail
 Index
 Membres
 Profil
 FAQ
 Rechercher
Rechercher
 
 

Résultats par :
 
Rechercher Recherche avancée
Mai 2024
LunMarMerJeuVenSamDim
  12345
6789101112
13141516171819
20212223242526
2728293031  
CalendrierCalendrier
Le deal à ne pas rater :
ETB Pokémon Fable Nébuleuse : où acheter le coffret dresseur ...
Voir le deal

 

 Simulation d'un objet TreeView

Aller en bas 
+3
Jicehel
Nardo26
Klaus
7 participants
Aller à la page : Précédent  1, 2
AuteurMessage
Klaus

Klaus


Nombre de messages : 12301
Age : 75
Localisation : Ile de France
Date d'inscription : 29/12/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyJeu 6 Déc 2012 - 9:45

Merci ! Je vais regarder ça.

EDIT

C'est corrigé. Mauvais choix de variable pour l'affichage du niveau, mais sans influence pour les traitements.

Je commence à normaliser les noms des variables pour une utilisation ultérieure par #INCLUDE.

Voici la version corrigée:
Code:
' treeview.bas

label click

dim x%, n%, a%, b%, s$, i%, flag%, a1$, a2$, n2%, n3%, s1$
dim ldet%, nsect%, lorg%, sind%, nindex%, niveau1%, niveau2%, niveau3%
dim chk%      : chk% = 0
dim montrer%  : montrer% = 1
dim sp3$      : sp3$ = string$(3," ")

width 0,800
CreateTreeViewObject(1,10,10,300,300)
on_click 1,click
CreateTreeViewRoot(TVO,"Racine")
niveau1% = 1

AddTreeViewChildByIndex(TVO,niveau1%,"aaaa")
niveau2% = AddTreeViewChildByIndex

AddTreeViewChildByIndex(TVO,niveau2%,"a1111111111")
AddTreeViewChildByIndex(TVO,niveau2%,"a2222222222")
AddTreeViewChildByIndex(TVO,niveau1%,"bbbb")
AddTreeViewChildByIndex(TVO,niveau1%,"cccc")
niveau2% = AddTreeViewChildByIndex
AddTreeViewChildByIndex(TVO,niveau2%,"11111111")
AddTreeViewChildByIndex(TVO,niveau2%,"22222222")
niveau3% = AddTreeViewChildByIndex
AddTreeViewChildByIndex(TVO,niveau3%,"2aaaaaaaaa")
AddTreeViewChildByIndex(TVO,niveau3%,"2bbbbbbbbb")
AddTreeViewChildByIndex(TVO,niveau2%,"33333333")
AddTreeViewChildByIndex(TVO,niveau2%,"44444444")
niveau3% = AddTreeViewChildByIndex
AddTreeViewChildByIndex(TVO,niveau3%,"4aaaaaaaaa")
AddTreeViewChildByIndex(TVO,niveau3%,"4bbbbbbbbb")
AddTreeViewChildByIndex(TVO,niveau1%,"dddd")
niveau2% = AddTreeViewChildByIndex
AddTreeViewChildByIndex(TVO,niveau2%,"d11111111")
AddTreeViewChildByIndex(TVO,niveau2%,"d22222222")


end

' positionnement des [+]
' niveau 1:  5...22 ==> 17
' niveau 2: 37...54 ==> 17
' niveau 3: 69...86 ==> 17
' niveau n:  a...b  n=int((x-5+32)/32)  a = (n-1)*32+5  b=a+17
click:
  x% = mouse_x_left_down(1)      : ' positionnement jorizontal du clic
  n% = int((x%-5+32)/32)        : ' multiple de 32 pixels avec offset de 5 pixels
  a% = (n%-1)*32 + 5            : ' pixel de gauche pour [x]
  b% = a% + 16                  : ' pixel de droite pour [x]
  s$ = item_index$(1)            : ' la ligne cliquée
  CalculateLineLevel(s$)        : ' n1 = niveau d'arborescence
  CalculateTreeViewLevelVector(1,item_index(1),s$)
  TVO_n1% = TVO_levels%(0)
  s$ = item_index$(1)
' message "click: ind="+str$(item_index(1))+"  x="+str$(x%)+"  a="+str$(a%)+"  b="+str$(b%)+"  n1="+str$(TVO_n1%)+"  s$="+s$
  flag% = 0
  if n%=TVO_n1%
    if mid$(s$,(n%-1)*4+2,1)="+" then flag% = 1
    if mid$(s$,(n%-1)*4+2,1)="-" then flag% = 2
  end_if
 
'  dump("0")
  select flag%
    ' on a sélectionné une ligne
    case 0
      message "Ligne "+str$(item_index(1))+" niveau "+str$(TVO_n1%)
     
    ' on veut déplier une ligne ayant des lignes enfant
    case 1
'      message "déplier ligne "+str$(item_index(1))+" niveau "+str$(n%)
      UnfoldTreeViewNode(1,item_index(1),n%)
     
    ' on veut replier une ligne pour escamoter ses enfants
    case 2
'      message "replier ligne "+str$(item_index(1))+" niveau "+str$(n%)
      FoldTreeViewNode(1,item_index(1),n%)
  end_select
 
  return
 
sub UnfoldTreeViewNode(o,lig%,niv%)
  dim_local s$, s1$, i1%, i2%
  s$ = item_read$(o,lig%)
  s1$ = ""
  if n%>1 then s1$ = string$((niv%-1)*4," ")
  s$ = s1$ + "[-] "+mid$(s$,niv%*4+1,len(s$))
  item_delete o,lig%
  item_insert o,lig%,s$
  CalculateLineLevel(s$)
  CalculateTreeViewLevelVector(o,lig%,s$)
  FindTreeViewSource(o)
  i1% = lig%
  i2% = TVO_sources%(1)
  while i2%<count(o+1)
    i2% = i2% + 1
    s$ = item_read$(o+1,i2%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      i1% = i1% + 1
      item_insert o,i1%,s$
    else
      exit_sub
    end_if
  end_while
end_sub

sub FoldTreeViewNode(o,lig%,niv%)
  dim_local s$, s1$, i%
  s$ = item_read$(o,lig%)
  s1$ = ""
  if n%>1 then s1$ = string$((niv%-1)*4," ")
  s$ = s1$ + "[+] "+mid$(s$,niv%*4+1,len(s$))
  item_delete o,lig%
  item_insert o,lig%,s$
  i% = lig%
  while i%<count(o)
    i% = i% + 1
    s$ = item_read$(o,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o,i%
      i% = i% - 1
    else
      exit_sub
    end_if
  end_while

end_sub

  ' determiner le niveau d'une ligne dans la section
sub CalculateLineLevel(s$)
  dim_local i%
  i% = 1
  while mid$(s$,i%,1)=" "
    i% = i% + 1
  end_while
  if mid$(s$,i%,1)<>"[" then i% = i% - 4
  i% = i% - 1
  TVO_n1% = i%/4 + 1
end_sub

' déterminer le vecteur des niveaux imbriqués
sub CalculateTreeViewLevelVector(o,ind%,r$)
  dim_local s$
  s$ = r$
  TVO_levels%(0) = 0
  i% = ind%
  CalculateLineLevel(s$)

  while TVO_n1%>1
    ldet% = 0
    n2% = TVO_n1%
    if n2%>1
      TVO_indexes%(TVO_levels%(0)+1) = i%
      while TVO_n1%>=n2%
        if TVO_n1%=n2% then ldet% = ldet% + 1
        i% = i% - 1
        s$ = item_read$(o,i%)
        CalculateLineLevel(s$)
      end_while
      TVO_levels%(0) = TVO_levels%(0) + 1
      TVO_levels%(TVO_levels%(0)) = ldet%
    end_if
  end_while
  TVO_levels%(0) = TVO_levels%(0) + 1
  TVO_levels%(TVO_levels%(0)) = 1
  TVO_indexes%(TVO_levels%(0)) = 1
end_sub

sub CreateTreeViewObject(no%,t%,l%,w%,h%)
  if (object_exists(no%)+object_exists(no%+1))>0
    message "TreeView - error 1"
    exit_sub
  end_if
  if variable("TVO")=0
    dim TVO, TVO_n1%, TVO_levels%(50), TVO_indexes%(50), TVO_sources%(50)
  end_if
 
  TVO = no%
  list TVO : top TVO,t% : left TVO,l%
    width TVO,w% : height TVO,h%
    font_name TVO,"Courier"
  if montrer%=0
    dlist TVO%+1
  else
    list TVO+1 : top TVO+1,t% : left TVO+1,l%+w%+10
      width TVO+1,w% : height TVO+1,h%
      font_name TVO+1,"Courier"
  end_if
end_sub

sub CreateTreeViewRoot(o,txt$)
  if count(o)>0
    message "TreeView - error 2"
    exit_sub
  end_if
  item_add o,"    "+txt$
  item_add o+1,"    "+txt$
end_sub

sub AddTreeViewChildByIndex(o,ind%,txt$)
  if variable("AddTreeViewChildByIndex")=0 then dim AddTreeViewChildByIndex
  dim_local a$, index%, n2%
  if ind%>count(o)
    message "TreeView - error 3"
    exit_sub
  end_if
  i% = ind%
  s$ = item_read$(o,i%)
if chk%=1 then message "AddTreeViewChildByIndex 1: i%="+str$(i%)+"  s$="+s$
  CalculateLineLevel(s$)
if chk%=1 then message "AddTreeViewChildByIndex 1: n1="+str$(TVO_n1%)+"  s$=["+s$+"]"
  n2% = TVO_n1%
  CalculateTreeViewLevelVector(o,ind%,s$)
if chk%=1 then dump("1 "+str$(ind%))
  FindTreeViewSource(o)
if chk%=1 then dump("2 "+str$(ind%))
  FindTreeViewEndLevel(o,n2%)
if chk%=1 then dump("3 "+str$(ind%))
  index% = TVO_indexes%(1)
if chk%=1 then message "index%="+str$(index%)
  s$ = item_read$(o,index%)
  a$ = mid$(s$,(n2%-1)*4+1,3)
  CalculateLineLevel(s$)
' message "xxx: index="+str$(index%)+"  a$="+a$+"  s$="+s$+"  txt$="+txt$

  if a$="[+]"
    message "déplier"
  end_if
  if (a$=sp3$) or (a$="[-]")
' message "D: n2="+str$(n2%)+"  index%="+str$(index%)+"  s$="+s$+"  a$="+a$+"  "+txt$
    if (a$=sp3$) and (TVO_n1%=n2%)
      s$ = item_read$(o+1,index%)
      item_delete o,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o,index%+1,string$(n2%*4," ")+"    "+txt$
    AddTreeViewChildByIndex = index%+1
    index% = TVO_sources%(1)
    if a$=sp3$ and (TVO_n1%=n2%)
      s$ = item_read$(o+1,index%)
      item_delete o+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o+1,index%+1,string$(n2%*4," ")+"    "+txt$
  end_if
end_sub

sub AddTreeViewChildByVector(o,ind%)

end_sub

sub FindTreeViewSource(o)
  dim_local k%, n%, s$
  sind% = 1
  s$ = item_read$(o+1,sind%)
  CalculateLineLevel(s$)
if chk%=1 then message "A1: sind="+str$(sind%)+"  n1="+str$(TVO_n1%)+"  "+s$+"  "+str$(TVO_levels%(0))
  for k%=TVO_levels%(0) to 1 step -1
    n% = TVO_levels%(k%)
if chk%=1 then message "A2: sind="+str$(sind%)+"  k="+str$(k%)+"  n="+str$(n%)
    while (n%>1)
      sind% = sind% + 1
      if sind%>count(o+1)
        TVO_sources%(k%) = sind%
        exit_sub
      end_if
      s$ = item_read$(o+1,sind%)
      CalculateLineLevel(s$)
if chk%=1 then message "A3: sind="+str$(sind%)+"  n="+str$(n%)+"  k="+str$(k%)+"  n1="+str$(TVO_n1%)+"  "+s$+"  TVO_levels(0)="+str$(TVO_levels%(0))
      if TVO_n1%=(TVO_levels%(0)+1-k%) then n% = n% - 1
    end_while
    TVO_sources%(k%) = sind%
    sind% = sind% + 1
  next k%
if chk%=1 then message "A4: Source: sind="+str$(sind%)+"  niveau="+str$(TVO_n1%)+"  "+s$
end_sub

sub FindTreeViewEndLevel(o,nx%)
  dim_local s$, k%, l%
  k% = TVO_sources%(1)
  while k%<count(o+1)
    k% = k% + 1
    l% = l% + 1
    s$ = item_read$(o+1,k%)
    CalculateLineLevel(s$)
    if TVO_n1%<nx%
' message "A: k="+str$(k%)+"  l="+str$(l%)
      TVO_sources%(1) = k%
      TVO_levels%(1) = TVO_levels%(1) + l%
      TVO_indexes%(1) = TVO_indexes%(1) + l%
' dump("A")
      exit_sub
    end_if
  end_while
' message "B: k="+str$(k%)+"  l="+str$(l%)
  TVO_sources%(1) = k%
  TVO_levels%(1) = TVO_levels%(1) + l%
  TVO_indexes%(1) = TVO_indexes%(1) + l%
' dump("B")
end_sub

sub dump(t$)
  message t$+": niveaux: "+str$(TVO_levels%(0))+","+str$(TVO_levels%(1))+","+str$(TVO_levels%(2))+","+str$(TVO_levels%(3))
  message t$+": indices: "+str$(TVO_indexes%(0))+","+str$(TVO_indexes%(1))+","+str$(TVO_indexes%(2))+","+str$(TVO_indexes%(3))
  message t$+": sources: "+str$(TVO_sources%(0))+","+str$(TVO_sources%(1))+","+str$(TVO_sources%(2))+","+str$(TVO_sources%(3))
end_sub
Revenir en haut Aller en bas
http://klauspanoramic.comxa.com/index.html
Nardo26

Nardo26


Nombre de messages : 2294
Age : 55
Localisation : Valence
Date d'inscription : 02/07/2010

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyJeu 6 Déc 2012 - 10:35

Bonjour Klaus !


Je sais pas comment tu t'en sort avec des variables comme celles-ci : Oo0.... mais suivant la fonte utilisée sur l'éditeur, c'est kif-kif....Simulation d'un objet TreeView - Page 2 19

Entre le O et le 0 c'est pas forcement évident surtout lorsque tu l'utilises comme indice de tableau : Tab(O) ou Tab(0) ?
Simulation d'un objet TreeView - Page 2 50
Revenir en haut Aller en bas
http://nardo26.lescigales.org
Klaus

Klaus


Nombre de messages : 12301
Age : 75
Localisation : Ile de France
Date d'inscription : 29/12/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyJeu 6 Déc 2012 - 11:19

Cette variable "o" - tu as raison. C'est provisoire, et ce sera remplacé à terme.

J'ai ajouté la possibilité d'insérer un noeud fille à n'importe quelle ligne cliquée dans la liste, MEME si la ligne est repliée ! Il y a un bouton pour cela à droite. Les deux autres boutons sont en cours de réalisation.
Code:
' treeview.bas

labels()
constantes()
variables()
form0()
GUI()
initialisations()
end





sub labels()
  label click, creerfille, creeravant, creerapres
end_sub

sub constantes()
  dim chk%      : chk% = 0
  dim montrer%  : montrer% = 1
  dim sp3$      : sp3$ = string$(3," ")
end_sub

sub variables()
  dim no%, x%, n%, a%, b%, s$, i%, flag%, a1$, a2$, n2%, n3%, s1$
  dim nadd%
  dim no_TVO%
  dim ldet%, nsect%, lorg%, sind%, nindex%, niveau1%, niveau2%, niveau3%
end_sub

sub form0()
  width 0,800
  caption 0,"Test d'un objet TreeView
end_sub

sub GUI()
  no% = no% + 1 : button no% : top no%,10 : left no%,630 : width no%,120
    caption no%,"Créer noeud fille" : on_click no%,creerfille
  no% = no% + 1 : button no% : top no%,40 : left no%,630 : width no%,120
    caption no%,"Créer noeud avant" : on_click no%,creeravant
  no% = no% + 1 : button no% : top no%,70 : left no%,630 : width no%,120
    caption no%,"Créer noeud après" : on_click no%,creerapres
end_sub

sub initialisations()
  no% = no% + 1 : no_TVO% = no%
  CreateTreeViewObject(no_TVO%,10,10,300,300)
  on_click no%,click
  CreateTreeViewRoot(TVO,"Racine")
  niveau1% = 1

  AddTreeViewChildByIndex(TVO,niveau1%,"aaaa")
  niveau2% = AddTreeViewChildByIndex

  AddTreeViewChildByIndex(TVO,niveau2%,"a1111111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"a2222222222")
  AddTreeViewChildByIndex(TVO,niveau1%,"bbbb")
  AddTreeViewChildByIndex(TVO,niveau1%,"cccc")
  niveau2% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau2%,"11111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"22222222")
  niveau3% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau3%,"2aaaaaaaaa")
  AddTreeViewChildByIndex(TVO,niveau3%,"2bbbbbbbbb")
  AddTreeViewChildByIndex(TVO,niveau2%,"33333333")
  AddTreeViewChildByIndex(TVO,niveau2%,"44444444")
  niveau3% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau3%,"4aaaaaaaaa")
  AddTreeViewChildByIndex(TVO,niveau3%,"4bbbbbbbbb")
  AddTreeViewChildByIndex(TVO,niveau1%,"dddd")
  niveau2% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau2%,"d11111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"d22222222")
end_sub







' positionnement des [+]
' niveau 1:  5...22 ==> 17
' niveau 2: 37...54 ==> 17
' niveau 3: 69...86 ==> 17
' niveau n:  a...b  n=int((x-5+32)/32)  a = (n-1)*32+5  b=a+17
click:
  TVO_index% = item_index(number_click) : ' mémoriser la ligne active
  x% = mouse_x_left_down(number_click)  : ' positionnement jorizontal du clic
  n% = int((x%-5+32)/32)                : ' multiple de 32 pixels avec offset de 5 pixels
  a% = (n%-1)*32 + 5                    : ' pixel de gauche pour [x]
  b% = a% + 16                          : ' pixel de droite pour [x]
  s$ = item_index$(number_click)        : ' la ligne cliquée
  CalculateLineLevel(s$)                : ' n1 = niveau d'arborescence
  CalculateTreeViewLevelVector(number_click,item_index(number_click),s$)
  TVO_n1% = TVO_levels%(0)              : ' le niveau d'arborescence
  s$ = item_index$(number_click)        : ' lire la ligne choisie
' message "click: ind="+str$(item_index(number_click))+"  x="+str$(x%)+"  a="+str$(a%)+"  b="+str$(b%)+"  n1="+str$(TVO_n1%)+"  s$="+s$
  flag% = 0                            : ' pas d'action par défaut
  if n%=TVO_n1%                        : ' si clic dans [x]:
    if mid$(s$,(n%-1)*4+2,1)="+" then flag% = 1  : ' action "déplier" si +
    if mid$(s$,(n%-1)*4+2,1)="-" then flag% = 2  : ' action "replier" si -
  end_if
 
'  dump("0")
  select flag%
    ' on a sélectionné une ligne
    case 0
      message "Ligne "+str$(item_index(number_click))+" niveau "+str$(TVO_n1%)
     
    ' on veut déplier une ligne ayant des lignes enfant
    case 1
'      message "déplier ligne "+str$(item_index(number_click))+" niveau "+str$(n%)
      UnfoldTreeViewNode(number_click,item_index(number_click),n%)
     
    ' on veut replier une ligne pour escamoter ses enfants
    case 2
'      message "replier ligne "+str$(item_index(number_click))+" niveau "+str$(n%)
      FoldTreeViewNode(number_click,item_index(number_click),n%)
  end_select
 
  return
 
creerfille:
  if TVO_index%=0
    message "Il fait choisir la ligne parent"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewChildByIndex(TVO,TVO_index%,"xxx "+str$(nadd%)+" xxx")
  return
 
creeravant:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if

  return

creerapres:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if

  return


 
 
sub UnfoldTreeViewNode(o,lig%,niv%)
  dim_local s$, s1$, i1%, i2%
  s$ = item_read$(o,lig%)
  s1$ = ""
  if n%>1 then s1$ = string$((niv%-1)*4," ")
  s$ = s1$ + "[-] "+mid$(s$,niv%*4+1,len(s$))
  item_delete o,lig%
  item_insert o,lig%,s$
  CalculateLineLevel(s$)
  CalculateTreeViewLevelVector(o,lig%,s$)
  FindTreeViewSource(o)
  i1% = lig%
  i2% = TVO_sources%(1)
  while i2%<count(o+1)
    i2% = i2% + 1
    s$ = item_read$(o+1,i2%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      i1% = i1% + 1
      item_insert o,i1%,s$
    else
      TVO_index% = 0
      exit_sub
    end_if
  end_while
  TVO_index% = 0
end_sub

sub FoldTreeViewNode(o,lig%,niv%)
  dim_local s$, s1$, i%
  s$ = item_read$(o,lig%)
  s1$ = ""
  if n%>1 then s1$ = string$((niv%-1)*4," ")
  s$ = s1$ + "[+] "+mid$(s$,niv%*4+1,len(s$))
  item_delete o,lig%
  item_insert o,lig%,s$
  i% = lig%
  while i%<count(o)
    i% = i% + 1
    s$ = item_read$(o,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o,i%
      i% = i% - 1
    else
      TVO_index% = 0
      exit_sub
    end_if
    TVO_index% = 0
  end_while

end_sub

  ' determiner le niveau d'une ligne dans la section
sub CalculateLineLevel(s$)
  dim_local i%
  i% = 1
  while mid$(s$,i%,1)=" "
    i% = i% + 1
  end_while
  if mid$(s$,i%,1)<>"[" then i% = i% - 4
  i% = i% - 1
  TVO_n1% = i%/4 + 1
end_sub

' déterminer le vecteur des niveaux imbriqués
sub CalculateTreeViewLevelVector(o,ind%,r$)
  dim_local s$
  s$ = r$
  TVO_levels%(0) = 0
  i% = ind%
  CalculateLineLevel(s$)

  while TVO_n1%>1
    ldet% = 0
    n2% = TVO_n1%
    if n2%>1
      TVO_indexes%(TVO_levels%(0)+1) = i%
      while TVO_n1%>=n2%
        if TVO_n1%=n2% then ldet% = ldet% + 1
        i% = i% - 1
        s$ = item_read$(o,i%)
        CalculateLineLevel(s$)
      end_while
      TVO_levels%(0) = TVO_levels%(0) + 1
      TVO_levels%(TVO_levels%(0)) = ldet%
    end_if
  end_while
  TVO_levels%(0) = TVO_levels%(0) + 1
  TVO_levels%(TVO_levels%(0)) = 1
  TVO_indexes%(TVO_levels%(0)) = 1
end_sub

sub CreateTreeViewObject(no%,t%,l%,w%,h%)
  if (object_exists(no%)+object_exists(no%+1))>0
    message "TreeView - error 1"
    exit_sub
  end_if
  if variable("TVO")=0
    dim TVO, TVO_n1%, TVO_levels%(50), TVO_indexes%(50), TVO_sources%(50)
    dim TVO_index%
  end_if
 
  TVO = no%
  list TVO : top TVO,t% : left TVO,l%
    width TVO,w% : height TVO,h%
    font_name TVO,"Courier"
  if montrer%=0
    dlist TVO%+1
  else
    list TVO+1 : top TVO+1,t% : left TVO+1,l%+w%+10
      width TVO+1,w% : height TVO+1,h%
      font_name TVO+1,"Courier"
  end_if
end_sub

sub CreateTreeViewRoot(o,txt$)
  if count(o)>0
    message "TreeView - error 2"
    exit_sub
  end_if
  item_add o,"    "+txt$
  item_add o+1,"    "+txt$
end_sub

sub AddTreeViewChildByIndex(o,ind%,txt$)
  if variable("AddTreeViewChildByIndex")=0 then dim AddTreeViewChildByIndex
  dim_local a$, index%, n2%
  if ind%>count(o)
    message "TreeView - error 3"
    exit_sub
  end_if
  i% = ind%
  s$ = item_read$(o,i%)
  CalculateLineLevel(s$)
  n2% = TVO_n1%
  CalculateTreeViewLevelVector(o,ind%,s$)
  FindTreeViewSource(o)
  FindTreeViewEndLevel(o,n2%)
  index% = TVO_indexes%(1)
  s$ = item_read$(o,index%)
  a$ = mid$(s$,(n2%-1)*4+1,3)
  CalculateLineLevel(s$)

  if a$="[+]"
    index% = TVO_sources%(1)
    if a$=sp3$ and (TVO_n1%=n2%)
      s$ = item_read$(o+1,index%)
      item_delete o+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o+1,index%+1,string$(n2%*4," ")+"    "+txt$
    AddTreeViewChildByIndex = index%+1
    TVO_index% = 0
  end_if
  if (a$=sp3$) or (a$="[-]")
    if (a$=sp3$) and (TVO_n1%=n2%)
      s$ = item_read$(o,index%)
      item_delete o,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o,index%+1,string$(n2%*4," ")+"    "+txt$
    AddTreeViewChildByIndex = index%+1
    index% = TVO_sources%(1)
    if a$=sp3$ and (TVO_n1%=n2%)
      s$ = item_read$(o+1,index%)
      item_delete o+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o+1,index%+1,string$(n2%*4," ")+"    "+txt$
  end_if
  TVO_index% = 0
end_sub

sub AddTreeViewChildByVector(o,ind%)

end_sub

sub FindTreeViewSource(o)
  dim_local k%, n%, s$
  sind% = 1
  s$ = item_read$(o+1,sind%)
  CalculateLineLevel(s$)
  for k%=TVO_levels%(0) to 1 step -1
    n% = TVO_levels%(k%)
    while (n%>1)
      sind% = sind% + 1
      if sind%>count(o+1)
        TVO_sources%(k%) = sind%
        exit_sub
      end_if
      s$ = item_read$(o+1,sind%)
      CalculateLineLevel(s$)
      if TVO_n1%=(TVO_levels%(0)+1-k%) then n% = n% - 1
    end_while
    TVO_sources%(k%) = sind%
    sind% = sind% + 1
  next k%
end_sub

sub FindTreeViewEndLevel(o,nx%)
  dim_local s$, k%, l%
  k% = TVO_sources%(1)
  while k%<count(o+1)
    k% = k% + 1
    l% = l% + 1
    s$ = item_read$(o+1,k%)
    CalculateLineLevel(s$)
    if TVO_n1%<=nx%
      TVO_sources%(1) = k% - 1
      if instr(item_read$(o,TVO_indexes%(1)),"[+]")=0
        TVO_levels%(1) = TVO_levels%(1) + l% - 1
        TVO_indexes%(1) = TVO_indexes%(1) + l% - 1
      end_if
      exit_sub
    end_if
  end_while
  TVO_sources%(1) = k%
  TVO_levels%(1) = TVO_levels%(1) + l%
  TVO_indexes%(1) = TVO_indexes%(1) + l%
end_sub

sub dump(t$)
  message t$+": niveaux: "+str$(TVO_levels%(0))+","+str$(TVO_levels%(1))+","+str$(TVO_levels%(2))+","+str$(TVO_levels%(3))
  message t$+": indices: "+str$(TVO_indexes%(0))+","+str$(TVO_indexes%(1))+","+str$(TVO_indexes%(2))+","+str$(TVO_indexes%(3))
  message t$+": sources: "+str$(TVO_sources%(0))+","+str$(TVO_sources%(1))+","+str$(TVO_sources%(2))+","+str$(TVO_sources%(3))
end_sub
Revenir en haut Aller en bas
http://klauspanoramic.comxa.com/index.html
Klaus

Klaus


Nombre de messages : 12301
Age : 75
Localisation : Ile de France
Date d'inscription : 29/12/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyJeu 6 Déc 2012 - 11:46

Et voici une version qui permet d'insérer un eligne "soeur" devant ou derrière une ligne ciblée par un clic, au même niveau:
Code:
' treeview.bas

labels()
constantes()
variables()
form0()
GUI()
initialisations()
end





sub labels()
  label click, creerfille, creeravant, creerapres
end_sub

sub constantes()
  dim chk%      : chk% = 0
  dim montrer%  : montrer% = 1
  dim sp3$      : sp3$ = string$(3," ")
end_sub

sub variables()
  dim no%, x%, n%, a%, b%, s$, i%, flag%, a1$, a2$, n2%, n3%, s1$
  dim nadd%
  dim no_TVO%
  dim ldet%, nsect%, lorg%, sind%, nindex%, niveau1%, niveau2%, niveau3%
end_sub

sub form0()
  width 0,800
  caption 0,"Test d'un objet TreeView
end_sub

sub GUI()
  no% = no% + 1 : button no% : top no%,10 : left no%,630 : width no%,120
    caption no%,"Créer noeud fille" : on_click no%,creerfille
  no% = no% + 1 : button no% : top no%,40 : left no%,630 : width no%,120
    caption no%,"Créer noeud avant" : on_click no%,creeravant
  no% = no% + 1 : button no% : top no%,70 : left no%,630 : width no%,120
    caption no%,"Créer noeud après" : on_click no%,creerapres
end_sub

sub initialisations()
  no% = no% + 1 : no_TVO% = no%
  CreateTreeViewObject(no_TVO%,10,10,300,300)
  on_click no%,click
  CreateTreeViewRoot(TVO,"Racine")
  niveau1% = 1

  AddTreeViewChildByIndex(TVO,niveau1%,"aaaa")
  niveau2% = AddTreeViewChildByIndex

  AddTreeViewChildByIndex(TVO,niveau2%,"a1111111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"a2222222222")
  AddTreeViewChildByIndex(TVO,niveau1%,"bbbb")
  AddTreeViewChildByIndex(TVO,niveau1%,"cccc")
  niveau2% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau2%,"11111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"22222222")
  niveau3% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau3%,"2aaaaaaaaa")
  AddTreeViewChildByIndex(TVO,niveau3%,"2bbbbbbbbb")
  AddTreeViewChildByIndex(TVO,niveau2%,"33333333")
  AddTreeViewChildByIndex(TVO,niveau2%,"44444444")
  niveau3% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau3%,"4aaaaaaaaa")
  AddTreeViewChildByIndex(TVO,niveau3%,"4bbbbbbbbb")
  AddTreeViewChildByIndex(TVO,niveau1%,"dddd")
  niveau2% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau2%,"d11111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"d22222222")
end_sub







' positionnement des [+]
' niveau 1:  5...22 ==> 17
' niveau 2: 37...54 ==> 17
' niveau 3: 69...86 ==> 17
' niveau n:  a...b  n=int((x-5+32)/32)  a = (n-1)*32+5  b=a+17
click:
  TVO_index% = item_index(number_click) : ' mémoriser la ligne active
  x% = mouse_x_left_down(number_click)  : ' positionnement jorizontal du clic
  n% = int((x%-5+32)/32)                : ' multiple de 32 pixels avec offset de 5 pixels
  a% = (n%-1)*32 + 5                    : ' pixel de gauche pour [x]
  b% = a% + 16                          : ' pixel de droite pour [x]
  s$ = item_index$(number_click)        : ' la ligne cliquée
  CalculateLineLevel(s$)                : ' n1 = niveau d'arborescence
  CalculateTreeViewLevelVector(number_click,item_index(number_click),s$)
  TVO_n1% = TVO_levels%(0)              : ' le niveau d'arborescence
  s$ = item_index$(number_click)        : ' lire la ligne choisie
' message "click: ind="+str$(item_index(number_click))+"  x="+str$(x%)+"  a="+str$(a%)+"  b="+str$(b%)+"  n1="+str$(TVO_n1%)+"  s$="+s$
  flag% = 0                            : ' pas d'action par défaut
  if n%=TVO_n1%                        : ' si clic dans [x]:
    if mid$(s$,(n%-1)*4+2,1)="+" then flag% = 1  : ' action "déplier" si +
    if mid$(s$,(n%-1)*4+2,1)="-" then flag% = 2  : ' action "replier" si -
  end_if
 
'  dump("0")
  select flag%
    ' on a sélectionné une ligne
    case 0
      message "Ligne "+str$(item_index(number_click))+" niveau "+str$(TVO_n1%)
     
    ' on veut déplier une ligne ayant des lignes enfant
    case 1
'      message "déplier ligne "+str$(item_index(number_click))+" niveau "+str$(n%)
      UnfoldTreeViewNode(number_click,item_index(number_click),n%)
     
    ' on veut replier une ligne pour escamoter ses enfants
    case 2
'      message "replier ligne "+str$(item_index(number_click))+" niveau "+str$(n%)
      FoldTreeViewNode(number_click,item_index(number_click),n%)
  end_select
 
  return
 
creerfille:
  if TVO_index%=0
    message "Il fait choisir la ligne parent"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewChildByIndex(TVO,TVO_index%,"xxx "+str$(nadd%)+" xxx")
  return
 
creeravant:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewSisterByIndex(TVO,TVO_index%,0,"xxx "+str$(nadd%)+" xxx")
  return

creerapres:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewSisterByIndex(TVO,TVO_index%,1,"xxx "+str$(nadd%)+" xxx")
  return


 
 
sub UnfoldTreeViewNode(o,lig%,niv%)
  dim_local s$, s1$, i1%, i2%
  s$ = item_read$(o,lig%)
  s1$ = ""
  if n%>1 then s1$ = string$((niv%-1)*4," ")
  s$ = s1$ + "[-] "+mid$(s$,niv%*4+1,len(s$))
  item_delete o,lig%
  item_insert o,lig%,s$
  CalculateLineLevel(s$)
  CalculateTreeViewLevelVector(o,lig%,s$)
  FindTreeViewSource(o)
  i1% = lig%
  i2% = TVO_sources%(1)
  while i2%<count(o+1)
    i2% = i2% + 1
    s$ = item_read$(o+1,i2%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      i1% = i1% + 1
      item_insert o,i1%,s$
    else
      TVO_index% = 0
      exit_sub
    end_if
  end_while
  TVO_index% = 0
end_sub

sub FoldTreeViewNode(o,lig%,niv%)
  dim_local s$, s1$, i%
  s$ = item_read$(o,lig%)
  s1$ = ""
  if n%>1 then s1$ = string$((niv%-1)*4," ")
  s$ = s1$ + "[+] "+mid$(s$,niv%*4+1,len(s$))
  item_delete o,lig%
  item_insert o,lig%,s$
  i% = lig%
  while i%<count(o)
    i% = i% + 1
    s$ = item_read$(o,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o,i%
      i% = i% - 1
    else
      TVO_index% = 0
      exit_sub
    end_if
    TVO_index% = 0
  end_while

end_sub

  ' determiner le niveau d'une ligne dans la section
sub CalculateLineLevel(s$)
  dim_local i%
  i% = 1
  while mid$(s$,i%,1)=" "
    i% = i% + 1
  end_while
  if mid$(s$,i%,1)<>"[" then i% = i% - 4
  i% = i% - 1
  TVO_n1% = i%/4 + 1
end_sub

' déterminer le vecteur des niveaux imbriqués
sub CalculateTreeViewLevelVector(o,ind%,r$)
  dim_local s$
  s$ = r$
  TVO_levels%(0) = 0
  i% = ind%
  CalculateLineLevel(s$)

  while TVO_n1%>1
    ldet% = 0
    n2% = TVO_n1%
    if n2%>1
      TVO_indexes%(TVO_levels%(0)+1) = i%
      while TVO_n1%>=n2%
        if TVO_n1%=n2% then ldet% = ldet% + 1
        i% = i% - 1
        s$ = item_read$(o,i%)
        CalculateLineLevel(s$)
      end_while
      TVO_levels%(0) = TVO_levels%(0) + 1
      TVO_levels%(TVO_levels%(0)) = ldet%
    end_if
  end_while
  TVO_levels%(0) = TVO_levels%(0) + 1
  TVO_levels%(TVO_levels%(0)) = 1
  TVO_indexes%(TVO_levels%(0)) = 1
end_sub

sub CreateTreeViewObject(no%,t%,l%,w%,h%)
  if (object_exists(no%)+object_exists(no%+1))>0
    message "TreeView - error 1"
    exit_sub
  end_if
  if variable("TVO")=0
    dim TVO, TVO_n1%, TVO_levels%(50), TVO_indexes%(50), TVO_sources%(50)
    dim TVO_index%
  end_if
 
  TVO = no%
  list TVO : top TVO,t% : left TVO,l%
    width TVO,w% : height TVO,h%
    font_name TVO,"Courier"
  if montrer%=0
    dlist TVO%+1
  else
    list TVO+1 : top TVO+1,t% : left TVO+1,l%+w%+10
      width TVO+1,w% : height TVO+1,h%
      font_name TVO+1,"Courier"
  end_if
end_sub

sub CreateTreeViewRoot(o,txt$)
  if count(o)>0
    message "TreeView - error 2"
    exit_sub
  end_if
  item_add o,"    "+txt$
  item_add o+1,"    "+txt$
end_sub

sub AddTreeViewChildByIndex(o,ind%,txt$)
  if variable("AddTreeViewChildByIndex")=0 then dim AddTreeViewChildByIndex
  dim_local a$, index%, n2%
  if ind%>count(o)
    message "TreeView - error 3"
    exit_sub
  end_if
  i% = ind%
  s$ = item_read$(o,i%)
  CalculateLineLevel(s$)
  n2% = TVO_n1%
  CalculateTreeViewLevelVector(o,ind%,s$)
  FindTreeViewSource(o)
  FindTreeViewEndLevel(o,n2%)
  index% = TVO_indexes%(1)
  s$ = item_read$(o,index%)
  a$ = mid$(s$,(n2%-1)*4+1,3)
  CalculateLineLevel(s$)

  if a$="[+]"
    index% = TVO_sources%(1)
    if a$=sp3$ and (TVO_n1%=n2%)
      s$ = item_read$(o+1,index%)
      item_delete o+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o+1,index%+1,string$(n2%*4," ")+"    "+txt$
    AddTreeViewChildByIndex = index%+1
    TVO_index% = 0
  end_if
  if (a$=sp3$) or (a$="[-]")
    if (a$=sp3$) and (TVO_n1%=n2%)
      s$ = item_read$(o,index%)
      item_delete o,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o,index%+1,string$(n2%*4," ")+"    "+txt$
    AddTreeViewChildByIndex = index%+1
    index% = TVO_sources%(1)
    if a$=sp3$ and (TVO_n1%=n2%)
      s$ = item_read$(o+1,index%)
      item_delete o+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o+1,index%+1,string$(n2%*4," ")+"    "+txt$
  end_if
  TVO_index% = 0
end_sub

sub AddTreeViewSisterByIndex(o,ind%,off%,txt$)
  if variable("AddTreeViewSisterByIndex")=0 then dim AddTreeViewSisterByIndex
  dim_local a$, index%, n2%
  if ind%>count(o)
    message "TreeView - error 3"
    exit_sub
  end_if
  if (ind%=1) and (off%=0)
    message "TreeView - error 3"
    exit_sub
  end_if
  i% = ind%
  s$ = item_read$(o,i%)
  CalculateLineLevel(s$)
  n2% = TVO_n1%
  CalculateTreeViewLevelVector(o,ind%,s$)
  FindTreeViewSource(o)
  if off%=1 then FindTreeViewEndLevel(o,n2%)
  index% = TVO_indexes%(1)
  s$ = item_read$(o,index%)
  a$ = mid$(s$,(n2%-1)*4+1,3)
  CalculateLineLevel(s$)
  item_insert o,index%+off%,string$((n2%-1)*4," ")+"    "+txt$
  AddTreeViewSisterByIndex = index%+1
  index% = TVO_sources%(1)
  TVO_index% = 0
end_sub


sub FindTreeViewSource(o)
  dim_local k%, n%, s$
  sind% = 1
  s$ = item_read$(o+1,sind%)
  CalculateLineLevel(s$)
  for k%=TVO_levels%(0) to 1 step -1
    n% = TVO_levels%(k%)
    while (n%>1)
      sind% = sind% + 1
      if sind%>count(o+1)
        TVO_sources%(k%) = sind%
        exit_sub
      end_if
      s$ = item_read$(o+1,sind%)
      CalculateLineLevel(s$)
      if TVO_n1%=(TVO_levels%(0)+1-k%) then n% = n% - 1
    end_while
    TVO_sources%(k%) = sind%
    sind% = sind% + 1
  next k%
end_sub

sub FindTreeViewEndLevel(o,nx%)
  dim_local s$, k%, l%
  k% = TVO_sources%(1)
  while k%<count(o+1)
    k% = k% + 1
    l% = l% + 1
    s$ = item_read$(o+1,k%)
    CalculateLineLevel(s$)
    if TVO_n1%<=nx%
      TVO_sources%(1) = k% - 1
      if instr(item_read$(o,TVO_indexes%(1)),"[+]")=0
        TVO_levels%(1) = TVO_levels%(1) + l% - 1
        TVO_indexes%(1) = TVO_indexes%(1) + l% - 1
      end_if
      exit_sub
    end_if
  end_while
  TVO_sources%(1) = k%
  TVO_levels%(1) = TVO_levels%(1) + l%
  TVO_indexes%(1) = TVO_indexes%(1) + l%
end_sub

sub dump(t$)
  message t$+": niveaux: "+str$(TVO_levels%(0))+","+str$(TVO_levels%(1))+","+str$(TVO_levels%(2))+","+str$(TVO_levels%(3))
  message t$+": indices: "+str$(TVO_indexes%(0))+","+str$(TVO_indexes%(1))+","+str$(TVO_indexes%(2))+","+str$(TVO_indexes%(3))
  message t$+": sources: "+str$(TVO_sources%(0))+","+str$(TVO_sources%(1))+","+str$(TVO_sources%(2))+","+str$(TVO_sources%(3))
end_sub
Revenir en haut Aller en bas
http://klauspanoramic.comxa.com/index.html
Nardo26

Nardo26


Nombre de messages : 2294
Age : 55
Localisation : Valence
Date d'inscription : 02/07/2010

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyJeu 6 Déc 2012 - 12:58

Ca marche bien !
A part un petit détail....Simulation d'un objet TreeView - Page 2 10 (je chipote car en utilisation normale, cela n'arrivera pas si on l'utilise en statique et non pas en dynamique comme pour la démo)
  • Tu sélectionnes une ligne
  • Tu cliques sur "créer noeud après" -> Ok ça marche ! (au passage, l'item "parait" toujours sélectionné)
  • tu cliques sur "créer noeud avant" -> Tu obtiens un message d'erreur "il faut choisir la ligne cible" (je me la joue Papydall Laughing )


@Papy: Comment tu écris "Je me la joue..." il faut un "e" à jou ?... (du verbe jouer et non pas bajoue) scratch


Dernière édition par Nardo26 le Jeu 6 Déc 2012 - 13:05, édité 1 fois
Revenir en haut Aller en bas
http://nardo26.lescigales.org
Jean Claude

Jean Claude


Nombre de messages : 5950
Age : 69
Localisation : 83 Var
Date d'inscription : 07/05/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyJeu 6 Déc 2012 - 13:04

Je viens d'essayer ton code, Klaus, et j'ai deux suggestions....

La première: plutôt que de parler de noeud avant ou après, ne serait-il pas mieux de parler de noeud Parent, enfant ou frère (de même niveau).

La deuxième: remplacer les boutons par un menu contextuel issu d'un clic droit

Je suis cela avec intérêt......

A+

PS: on vient de se croiser avec Nardo (et comme je suis en début de page, voir le message de Nardo à la fin de la page précédente)
Revenir en haut Aller en bas
Nardo26

Nardo26


Nombre de messages : 2294
Age : 55
Localisation : Valence
Date d'inscription : 02/07/2010

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyJeu 6 Déc 2012 - 13:08

@Jean claude : Ca n'existe pas le "menu contextuel", c'est un truc que j'ai aussi en prévision dans mes cartons (faire une librairie facile à utiliser)
Peut être que Klaus a déjà réfléchi à la chose (mais là on sort du sujet de ce post...)

Revenir en haut Aller en bas
http://nardo26.lescigales.org
Jean Claude

Jean Claude


Nombre de messages : 5950
Age : 69
Localisation : 83 Var
Date d'inscription : 07/05/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyJeu 6 Déc 2012 - 13:13

Oui Nardo, le menu contextuel n'existe pas mais il me semble qu'un Panoramicien l'avais simulé (Je crois Bignono ou Cosmos) et cela résoudrait le problème de sélection (ou pas) de la ligne de référence pour créer un noeud.

A+

PS: je ne crois pas être hors sujet, le menu contextuel doit pouvoir être codé pour ce programme.
Revenir en haut Aller en bas
Nardo26

Nardo26


Nombre de messages : 2294
Age : 55
Localisation : Valence
Date d'inscription : 02/07/2010

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyJeu 6 Déc 2012 - 13:20

Peut être, mais il me semble que cela ne fonctionnai pas tip-top... (des aléas lors de la sélection)
Faudra que je recherche ce code pour voir un peu si on peu l'améliorer... Wink



Revenir en haut Aller en bas
http://nardo26.lescigales.org
Jean Claude

Jean Claude


Nombre de messages : 5950
Age : 69
Localisation : 83 Var
Date d'inscription : 07/05/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyJeu 6 Déc 2012 - 13:23

Je viens de retrouver le code de Bignono (c'était lui) et aparament çà marche

Code:
dim r,ouiclic,nonclic
label quitter,deplace,taille,rotation,copie,ctxt,montrcach,supprimer,souris

scene3d 1:left 1,200:color 1,20,20,20

main_menu 10
sub_menu 11:parent 11,10:caption 11,"Fichier"
sub_menu 14:parent 14,10:caption 14,"Commandes"
sub_menu 22:parent 22,11:caption 22,"Quitter"        :on_click 22,quitter

sub_menu 42:parent 42,14:caption 42,"Position"      :on_click 42,deplace
sub_menu 43:parent 43,14:caption 43,"Taille"        :on_click 43,taille
sub_menu 44:parent 44,14:caption 44,"Rotation"      :on_click 44,rotation
sub_menu 47:parent 47,14:caption 47,"Dupliquer objet":on_click 47,copie
sub_menu 50:parent 50,14:caption 50,"Changer texte 3d_Text"  :on_click 50,ctxt
sub_menu 51:parent 51,14:caption 51,"Cacher/Montrer" :on_click 51,montrcach
sub_menu 52:parent 52,14:caption 52,"Supprimer"      :on_click 52,supprimer

timer 149:timer_interval 149,1:timer_off 149:on_timer 149,souris
list 150:hide 150:width 150,160:height 150,90
item_add 150,"Position objet":item_add 150,"Taille objet"
item_add 150,"Rotation objet":item_add 150,"Dupliquer objet"
item_add 150,"Supprimer objet":item_add 150,"      FERMER"

timer_on 149
end

souris:
timer_off 149
ouiclic=mouse_right_down(1):nonclic=mouse_right_up(1)
if (ouiclic=1 and nonclic=0)
  left 150,mouse_x_right_down(1):top 150,abs(90-mouse_y_right_down(1)):show 150
end_if
if clicked(150)=1
  r=item_index(150)
  item_insert 150,item_index(150),item_index$(150):item_delete 150,r+1
  hide 150
end_if
  if r=1 then gosub deplace
  if r=2 then gosub taille
  if r=3 then gosub rotation
  if r=4 then gosub copie
  if r=5 then gosub supprimer
  if r=6 then r=0
r=0
timer_on 149
return

deplace:
cls:print "déplacement objet":return
taille:
cls:print "taille objet":return
rotation:
cls:print "rotation objet":return
copie:
cls:print "dupliquer objet":return
ctxt:
cls:print "changer 3d_text":return
montrcach:
cls:print "montrer objet":print "cacher objet":return
supprimer:
cls:print "supprimer objet":return
quitter:
timer_off_all
terminate

A+
Revenir en haut Aller en bas
Klaus

Klaus


Nombre de messages : 12301
Age : 75
Localisation : Ile de France
Date d'inscription : 29/12/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyJeu 6 Déc 2012 - 14:09

Je vais revoir le nommage des boutons pour que ce soit plus clair.

J'ai déjà signalé que je décrète que CHAQUE ACTION sur la structure du treeview désélectionne la ligne, MEME si elle reste en bleu ! Il faut impérativement resélectionner la ligne pour lancer l'action suivante. C'est un comportement voulu.

Je regarderai plus tard si l'on peut tenir trace de la ligne active, mais tant que je n'utilise pas KGF.dll pour forcer la sélection d'une ligne dans la liste, il n'y aura pas forcément un lien entre la ligne active de l'objet treeview et la ligne bleue qui s'affiche à l'écran. D'où ma décision, pour le moment, de désactiver la ligne sélectionnée systématiquement après chaque action qui change la structure.

Je m'attaque aux suppressions...
Revenir en haut Aller en bas
http://klauspanoramic.comxa.com/index.html
Klaus

Klaus


Nombre de messages : 12301
Age : 75
Localisation : Ile de France
Date d'inscription : 29/12/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyJeu 6 Déc 2012 - 18:05

J'ai fait la suppression d'un noeud, simple ou composé. En plus, j'ai ajouté un bouton pour supprimer complètement l'objet treeview, et un autre pour reprendre un nouveau test, qui génère donc exactement la configuration de départ. Cela évite d'arrêter le programme et de le relancer, lorsque le contenu ne convient plus.

J'ai changé les noms des boutons par noeud "soeur aînée" pour la ligne au-dessus, et "soeur cadette" pour le lien en-dessous.

Voici le source:
Code:
' treeview.bas

labels()
constantes()
variables()
form0()
GUI()
initialisations()
end





sub labels()
  label click, creerfille, creeravant, creerapres, supprimer
  label supprimerTVO, nouveautest
end_sub

sub constantes()
  dim chk%      : chk% = 0
  dim montrer%  : montrer% = 1
  dim sp3$      : sp3$ = string$(3," ")
end_sub

sub variables()
  dim no%, x%, n%, a%, b%, s$, i%, flag%, a1$, a2$, n2%, n3%, s1$
  dim nadd%
  dim no_TVO%
  dim ldet%, nsect%, lorg%, sind%, nindex%, niveau1%, niveau2%, niveau3%
end_sub

sub form0()
  width 0,800
  caption 0,"Test d'un objet TreeView
end_sub

sub GUI()
  no% = no% + 1 : button no% : top no%,10 : left no%,630 : width no%,130
    caption no%,"Créer noeud fille" : on_click no%,creerfille
  no% = no% + 1 : button no% : top no%,40 : left no%,630 : width no%,130
    caption no%,"Créer noeud soeur ainée" : on_click no%,creeravant
  no% = no% + 1 : button no% : top no%,70 : left no%,630 : width no%,130
    caption no%,"Créer noeud soeur cadette" : on_click no%,creerapres
  no% = no% + 1 : button no% : top no%,100 : left no%,630 : width no%,130
    caption no%,"Supprimer noeud" : on_click no%,supprimer

  no% = no% + 1 : button no% : top no%,150 : left no%,630 : width no%,130
    caption no%,"Supprimer TreeView" : on_click no%,supprimerTVO
  no% = no% + 1 : button no% : top no%,180 : left no%,630 : width no%,130
    caption no%,"Nouveau test" : on_click no%,nouveautest
end_sub

sub initialisations()
  no% = no% + 1 : no_TVO% = no%
  creertest()
end_sub

sub creertest()
  CreateTreeViewObject(no_TVO%,10,10,300,300)
  on_click no%,click
  CreateTreeViewRoot(TVO,"Racine")
  niveau1% = 1

  AddTreeViewChildByIndex(TVO,niveau1%,"aaaa")
  niveau2% = AddTreeViewChildByIndex

  AddTreeViewChildByIndex(TVO,niveau2%,"a1111111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"a2222222222")
  AddTreeViewChildByIndex(TVO,niveau1%,"bbbb")
  AddTreeViewChildByIndex(TVO,niveau1%,"cccc")
  niveau2% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau2%,"11111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"22222222")
  niveau3% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau3%,"2aaaaaaaaa")
  AddTreeViewChildByIndex(TVO,niveau3%,"2bbbbbbbbb")
  AddTreeViewChildByIndex(TVO,niveau2%,"33333333")
  AddTreeViewChildByIndex(TVO,niveau2%,"44444444")
  niveau3% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau3%,"4aaaaaaaaa")
  AddTreeViewChildByIndex(TVO,niveau3%,"4bbbbbbbbb")
  AddTreeViewChildByIndex(TVO,niveau1%,"dddd")
  niveau2% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau2%,"d11111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"d22222222")
end_sub







' positionnement des [+]
' niveau 1:  5...22 ==> 17
' niveau 2: 37...54 ==> 17
' niveau 3: 69...86 ==> 17
' niveau n:  a...b  n=int((x-5+32)/32)  a = (n-1)*32+5  b=a+17
click:
  TVO_index% = item_index(number_click) : ' mémoriser la ligne active
  x% = mouse_x_left_down(number_click)  : ' positionnement jorizontal du clic
  n% = int((x%-5+32)/32)                : ' multiple de 32 pixels avec offset de 5 pixels
  a% = (n%-1)*32 + 5                    : ' pixel de gauche pour [x]
  b% = a% + 16                          : ' pixel de droite pour [x]
  s$ = item_index$(number_click)        : ' la ligne cliquée
  CalculateLineLevel(s$)                : ' n1 = niveau d'arborescence
  CalculateTreeViewLevelVector(number_click,item_index(number_click),s$)
  TVO_n1% = TVO_levels%(0)              : ' le niveau d'arborescence
  s$ = item_index$(number_click)        : ' lire la ligne choisie
' message "click: ind="+str$(item_index(number_click))+"  x="+str$(x%)+"  a="+str$(a%)+"  b="+str$(b%)+"  n1="+str$(TVO_n1%)+"  s$="+s$
  flag% = 0                            : ' pas d'action par défaut
  if n%=TVO_n1%                        : ' si clic dans [x]:
    if mid$(s$,(n%-1)*4+2,1)="+" then flag% = 1  : ' action "déplier" si +
    if mid$(s$,(n%-1)*4+2,1)="-" then flag% = 2  : ' action "replier" si -
  end_if
 
'  dump("0")
  select flag%
    ' on a sélectionné une ligne
    case 0
      message "Ligne "+str$(item_index(number_click))+" niveau "+str$(TVO_n1%)
     
    ' on veut déplier une ligne ayant des lignes enfant
    case 1
'      message "déplier ligne "+str$(item_index(number_click))+" niveau "+str$(n%)
      UnfoldTreeViewNode(number_click,item_index(number_click),n%)
     
    ' on veut replier une ligne pour escamoter ses enfants
    case 2
'      message "replier ligne "+str$(item_index(number_click))+" niveau "+str$(n%)
      FoldTreeViewNode(number_click,item_index(number_click),n%)
  end_select
 
  return
 
creerfille:
  if TVO_index%=0
    message "Il fait choisir la ligne parent"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewChildByIndex(TVO,TVO_index%,"xxx "+str$(nadd%)+" xxx")
  return
 
creeravant:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewSisterByIndex(TVO,TVO_index%,0,"xxx "+str$(nadd%)+" xxx")
  return

creerapres:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewSisterByIndex(TVO,TVO_index%,1,"xxx "+str$(nadd%)+" xxx")
  return
 
supprimer:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  DeleteTreeViewNode(no_TVO%,item_index(no_TVO%))
  return
 
supprimerTVO:
  if variable("TVO")=0
    message "L'objet TVO n'est pas défini."
    return
  end_if
  delete no_TVO%
  delete no_TVO%+1
  free TVO
  free TVO_n1%
  free TVO_levels%
  free TVO_indexes%
  free TVO_sources%
  free TVO_index%
  return
 
nouveautest:
  creertest()
  return

 
 
sub UnfoldTreeViewNode(o,lig%,niv%)
  dim_local s$, s1$, i1%, i2%
  s$ = item_read$(o,lig%)
  s1$ = ""
  if n%>1 then s1$ = string$((niv%-1)*4," ")
  s$ = s1$ + "[-] "+mid$(s$,niv%*4+1,len(s$))
  item_delete o,lig%
  item_insert o,lig%,s$
  CalculateLineLevel(s$)
  CalculateTreeViewLevelVector(o,lig%,s$)
  FindTreeViewSource(o)
  i1% = lig%
  i2% = TVO_sources%(1)
  while i2%<count(o+1)
    i2% = i2% + 1
    s$ = item_read$(o+1,i2%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      i1% = i1% + 1
      item_insert o,i1%,s$
    else
      TVO_index% = 0
      exit_sub
    end_if
  end_while
  TVO_index% = 0
end_sub

sub FoldTreeViewNode(o,lig%,niv%)
  dim_local s$, s1$, i%
  s$ = item_read$(o,lig%)
  s1$ = ""
  if niv%>1 then s1$ = string$((niv%-1)*4," ")
  s$ = s1$ + "[+] "+mid$(s$,niv%*4+1,len(s$))
  item_delete o,lig%
  item_insert o,lig%,s$
  i% = lig%
  while i%<count(o)
    i% = i% + 1
    s$ = item_read$(o,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o,i%
      i% = i% - 1
    else
      TVO_index% = 0
      exit_sub
    end_if
    TVO_index% = 0
  end_while

end_sub

  ' determiner le niveau d'une ligne dans la section
sub CalculateLineLevel(s$)
  dim_local i%
  i% = 1
  while mid$(s$,i%,1)=" "
    i% = i% + 1
  end_while
  if mid$(s$,i%,1)<>"[" then i% = i% - 4
  i% = i% - 1
  TVO_n1% = i%/4 + 1
end_sub

' déterminer le vecteur des niveaux imbriqués
sub CalculateTreeViewLevelVector(o,ind%,r$)
  dim_local s$
  s$ = r$
  TVO_levels%(0) = 0
  i% = ind%
  CalculateLineLevel(s$)

  while TVO_n1%>1
    ldet% = 0
    n2% = TVO_n1%
    if n2%>1
      TVO_indexes%(TVO_levels%(0)+1) = i%
      while TVO_n1%>=n2%
        if TVO_n1%=n2% then ldet% = ldet% + 1
        i% = i% - 1
        s$ = item_read$(o,i%)
        CalculateLineLevel(s$)
      end_while
      TVO_levels%(0) = TVO_levels%(0) + 1
      TVO_levels%(TVO_levels%(0)) = ldet%
    end_if
  end_while
  TVO_levels%(0) = TVO_levels%(0) + 1
  TVO_levels%(TVO_levels%(0)) = 1
  TVO_indexes%(TVO_levels%(0)) = 1
end_sub

sub CreateTreeViewObject(no%,t%,l%,w%,h%)
  if (object_exists(no%)+object_exists(no%+1))>0
    message "TreeView - error 1"
    exit_sub
  end_if
  if variable("TVO")=0
    dim TVO, TVO_n1%, TVO_levels%(50), TVO_indexes%(50), TVO_sources%(50)
    dim TVO_index%
  end_if
 
  TVO = no%
  list TVO : top TVO,t% : left TVO,l%
    width TVO,w% : height TVO,h%
    font_name TVO,"Courier"
  if montrer%=0
    dlist TVO%+1
  else
    list TVO+1 : top TVO+1,t% : left TVO+1,l%+w%+10
      width TVO+1,w% : height TVO+1,h%
      font_name TVO+1,"Courier"
  end_if
end_sub

sub CreateTreeViewRoot(o,txt$)
  if count(o)>0
    message "TreeView - error 2"
    exit_sub
  end_if
  item_add o,"    "+txt$
  item_add o+1,"    "+txt$
end_sub

sub AddTreeViewChildByIndex(o,ind%,txt$)
  if variable("AddTreeViewChildByIndex")=0 then dim AddTreeViewChildByIndex
  dim_local a$, index%, n2%
  if ind%>count(o)
    message "TreeView - error 3"
    exit_sub
  end_if
  i% = ind%
  s$ = item_read$(o,i%)
  CalculateLineLevel(s$)
  n2% = TVO_n1%
  CalculateTreeViewLevelVector(o,ind%,s$)
  FindTreeViewSource(o)
  FindTreeViewEndLevel(o,n2%)
  index% = TVO_indexes%(1)
  s$ = item_read$(o,index%)
  a$ = mid$(s$,(n2%-1)*4+1,3)
  CalculateLineLevel(s$)

  if a$="[+]"
    index% = TVO_sources%(1)
    if a$=sp3$ and (TVO_n1%=n2%)
      s$ = item_read$(o+1,index%)
      item_delete o+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o+1,index%+1,string$(n2%*4," ")+"    "+txt$
    AddTreeViewChildByIndex = index%+1
    TVO_index% = 0
  end_if
  if (a$=sp3$) or (a$="[-]")
    if (a$=sp3$) and (TVO_n1%=n2%)
      s$ = item_read$(o,index%)
      item_delete o,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o,index%+1,string$(n2%*4," ")+"    "+txt$
    AddTreeViewChildByIndex = index%+1
    index% = TVO_sources%(1)
    if a$=sp3$ and (TVO_n1%=n2%)
      s$ = item_read$(o+1,index%)
      item_delete o+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o+1,index%+1,string$(n2%*4," ")+"    "+txt$
  end_if
  TVO_index% = 0
end_sub

sub AddTreeViewSisterByIndex(o,ind%,off%,txt$)
  if variable("AddTreeViewSisterByIndex")=0 then dim AddTreeViewSisterByIndex
  dim_local a$, index%, n2%
  if ind%>count(o)
    message "TreeView - error 3"
    exit_sub
  end_if
  if (ind%=1) and (off%=0)
    message "TreeView - error 3"
    exit_sub
  end_if
  i% = ind%
  s$ = item_read$(o,i%)
  CalculateLineLevel(s$)
  n2% = TVO_n1%
  CalculateTreeViewLevelVector(o,ind%,s$)
  FindTreeViewSource(o)
  if off%=1 then FindTreeViewEndLevel(o,n2%)
  index% = TVO_indexes%(1)
  s$ = item_read$(o,index%)
  a$ = mid$(s$,(n2%-1)*4+1,3)
  CalculateLineLevel(s$)
  item_insert o,index%+off%,string$((n2%-1)*4," ")+"    "+txt$
  AddTreeViewSisterByIndex = index%+1
  index% = TVO_sources%(1)
  TVO_index% = 0
end_sub


sub FindTreeViewSource(o)
  dim_local k%, n%, s$
  sind% = 1
  s$ = item_read$(o+1,sind%)
  CalculateLineLevel(s$)
  for k%=TVO_levels%(0) to 1 step -1
    n% = TVO_levels%(k%)
    while (n%>1)
      sind% = sind% + 1
      if sind%>count(o+1)
        TVO_sources%(k%) = sind%
        exit_sub
      end_if
      s$ = item_read$(o+1,sind%)
      CalculateLineLevel(s$)
      if TVO_n1%=(TVO_levels%(0)+1-k%) then n% = n% - 1
    end_while
    TVO_sources%(k%) = sind%
    sind% = sind% + 1
  next k%
end_sub

sub FindTreeViewEndLevel(o,nx%)
  dim_local s$, k%, l%
  k% = TVO_sources%(1)
  while k%<count(o+1)
    k% = k% + 1
    l% = l% + 1
    s$ = item_read$(o+1,k%)
    CalculateLineLevel(s$)
    if TVO_n1%<=nx%
      TVO_sources%(1) = k% - 1
      if instr(item_read$(o,TVO_indexes%(1)),"[+]")=0
        TVO_levels%(1) = TVO_levels%(1) + l% - 1
        TVO_indexes%(1) = TVO_indexes%(1) + l% - 1
      end_if
      exit_sub
    end_if
  end_while
  TVO_sources%(1) = k%
  TVO_levels%(1) = TVO_levels%(1) + l%
  TVO_indexes%(1) = TVO_indexes%(1) + l%
end_sub

sub DeleteTreeViewNode(o,lig%)
  dim_local s$, s1$, i%, niv%
  s$ = item_read$(o,lig%)
  CalculateLineLevel(s$)
  niv% = TVO_n1%
  CalculateTreeViewLevelVector(o,lig%,s$)
  FindTreeViewSource(o)
  s1$ = ""
  i% = lig%
  while i%<count(o)
    i% = i% + 1
    s$ = item_read$(o,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o,i%
      i% = i% - 1
    else
      TVO_index% = 0
'      item_delete o,lig%
'      exit_sub
    end_if
    TVO_index% = 0
  end_while
  if lig%>1
    item_delete o,lig%
  else
    s$ = item_read$(o,lig%)
    s$ = string$(4," ")+mid$(s$,5,len(s$))
    item_delete o,lig%
    item_insert o,lig%,s$
  end_if

  i% = TVO_sources%(1)
  while i%<count(o+1)
    i% = i% + 1
    s$ = item_read$(o+1,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o+1,i%
      i% = i% - 1
    else
'      TVO_index% = 0
'      item_delete o+1,lig%
'      exit_sub
    end_if
'    TVO_index% = 0
  end_while
  lig% = TVO_sources%(1)
  if lig%>1
    item_delete o+1,lig%
  else
    s$ = item_read$(o+1,lig%)
    s$ = string$(4," ")+mid$(s$,5,len(s$))
    item_delete o+1,lig%
    item_insert o+1,lig%,s$
  end_if
end_sub

sub dump(t$)
  message t$+": niveaux: "+str$(TVO_levels%(0))+","+str$(TVO_levels%(1))+","+str$(TVO_levels%(2))+","+str$(TVO_levels%(3))+","+str$(TVO_levels%(4))
  message t$+": indices: "+str$(TVO_indexes%(0))+","+str$(TVO_indexes%(1))+","+str$(TVO_indexes%(2))+","+str$(TVO_indexes%(3))+","+str$(TVO_indexes%(4))
  message t$+": sources: "+str$(TVO_sources%(0))+","+str$(TVO_sources%(1))+","+str$(TVO_sources%(2))+","+str$(TVO_sources%(3))+","+str$(TVO_sources%(4))
end_sub
Revenir en haut Aller en bas
http://klauspanoramic.comxa.com/index.html
Klaus

Klaus


Nombre de messages : 12301
Age : 75
Localisation : Ile de France
Date d'inscription : 29/12/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyJeu 6 Déc 2012 - 23:40

Et voici une version qui montre comment utiliser cet outil pour afficher une arborescence de répertoires, comme le fait l'explorateur de fichiers de Windows. Le programme prend dir_current$ comme dossier de départ, et il affiche tout le contenu, fichiers et sous-dossiers, à n'importe quelle profondeur. On peut replier ou déplier n'importe quel dossier, à n'importe quel niveau:
Code:
' treeview.bas

labels()
constantes()
variables()
form0()
GUI()
initialisations()
end





sub labels()
  label click, creerfille, creeravant, creerapres, supprimer
  label supprimerTVO, nouveautest, explorateur
end_sub

sub constantes()
  dim chk%      : chk% = 0
  dim montrer%  : montrer% = 1
  dim sp3$      : sp3$ = string$(3," ")
end_sub

sub variables()
  dim no%, x%, n%, a%, b%, s$, i%, flag%, a1$, a2$, n2%, n3%, s1$
  dim nadd%
  dim no_TVO%
  dim ldet%, nsect%, lorg%, sind%, nindex%, niveau1%, niveau2%, niveau3%
  dim f$, d$, k%, d1$, f1$, nodir%
end_sub

sub form0()
  width 0,1000
  caption 0,"Test d'un objet TreeView
end_sub

sub GUI()
  no% = no% + 1 : button no% : top no%,10 : left no%,830 : width no%,130
    caption no%,"Créer noeud fille" : on_click no%,creerfille
  no% = no% + 1 : button no% : top no%,40 : left no%,830 : width no%,130
    caption no%,"Créer noeud soeur ainée" : on_click no%,creeravant
  no% = no% + 1 : button no% : top no%,70 : left no%,830 : width no%,130
    caption no%,"Créer noeud soeur cadette" : on_click no%,creerapres
  no% = no% + 1 : button no% : top no%,100 : left no%,830 : width no%,130
    caption no%,"Supprimer noeud" : on_click no%,supprimer

  no% = no% + 1 : button no% : top no%,150 : left no%,830 : width no%,130
    caption no%,"Supprimer TreeView" : on_click no%,supprimerTVO
  no% = no% + 1 : button no% : top no%,180 : left no%,830 : width no%,130
    caption no%,"Nouveau test" : on_click no%,nouveautest
  no% = no% + 1 : button no% : top no%,210 : left no%,830 : width no%,130
    caption no%,"Explorateur" : on_click no%,explorateur
end_sub

sub initialisations()
  no% = no% + 1 : no_TVO% = no%
  creertest()
end_sub

sub creertest()
  CreateTreeViewObject(no_TVO%,10,10,300,300)
  on_click no%,click
  CreateTreeViewRoot(TVO,"Racine")
  niveau1% = 1

  AddTreeViewChildByIndex(TVO,niveau1%,"aaaa")
  niveau2% = AddTreeViewChildByIndex

  AddTreeViewChildByIndex(TVO,niveau2%,"a1111111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"a2222222222")
  AddTreeViewChildByIndex(TVO,niveau1%,"bbbb")
  AddTreeViewChildByIndex(TVO,niveau1%,"cccc")
  niveau2% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau2%,"11111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"22222222")
  niveau3% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau3%,"2aaaaaaaaa")
  AddTreeViewChildByIndex(TVO,niveau3%,"2bbbbbbbbb")
  AddTreeViewChildByIndex(TVO,niveau2%,"33333333")
  AddTreeViewChildByIndex(TVO,niveau2%,"44444444")
  niveau3% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau3%,"4aaaaaaaaa")
  AddTreeViewChildByIndex(TVO,niveau3%,"4bbbbbbbbb")
  AddTreeViewChildByIndex(TVO,niveau1%,"dddd")
  niveau2% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau2%,"d11111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"d22222222")
end_sub







' positionnement des [+]
' niveau 1:  5...22 ==> 17
' niveau 2: 37...54 ==> 17
' niveau 3: 69...86 ==> 17
' niveau n:  a...b  n=int((x-5+32)/32)  a = (n-1)*32+5  b=a+17
click:
  TVO_index% = item_index(number_click) : ' mémoriser la ligne active
  x% = mouse_x_left_down(number_click)  : ' positionnement jorizontal du clic
  n% = int((x%-5+32)/32)                : ' multiple de 32 pixels avec offset de 5 pixels
  a% = (n%-1)*32 + 5                    : ' pixel de gauche pour [x]
  b% = a% + 16                          : ' pixel de droite pour [x]
  s$ = item_index$(number_click)        : ' la ligne cliquée
  CalculateLineLevel(s$)                : ' n1 = niveau d'arborescence
  CalculateTreeViewLevelVector(number_click,item_index(number_click),s$)
  TVO_n1% = TVO_levels%(0)              : ' le niveau d'arborescence
  s$ = item_index$(number_click)        : ' lire la ligne choisie
' message "click: ind="+str$(item_index(number_click))+"  x="+str$(x%)+"  a="+str$(a%)+"  b="+str$(b%)+"  n1="+str$(TVO_n1%)+"  s$="+s$
  flag% = 0                            : ' pas d'action par défaut
  if n%=TVO_n1%                        : ' si clic dans [x]:
    if mid$(s$,(n%-1)*4+2,1)="+" then flag% = 1  : ' action "déplier" si +
    if mid$(s$,(n%-1)*4+2,1)="-" then flag% = 2  : ' action "replier" si -
  end_if
 
'  dump("0")
  select flag%
    ' on a sélectionné une ligne
    case 0
      message "Ligne "+str$(item_index(number_click))+" niveau "+str$(TVO_n1%)
     
    ' on veut déplier une ligne ayant des lignes enfant
    case 1
'      message "déplier ligne "+str$(item_index(number_click))+" niveau "+str$(n%)
      UnfoldTreeViewNode(number_click,item_index(number_click),n%)
     
    ' on veut replier une ligne pour escamoter ses enfants
    case 2
'      message "replier ligne "+str$(item_index(number_click))+" niveau "+str$(n%)
      FoldTreeViewNode(number_click,item_index(number_click),n%)
  end_select
 
  return
 
creerfille:
  if TVO_index%=0
    message "Il fait choisir la ligne parent"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewChildByIndex(TVO,TVO_index%,"xxx "+str$(nadd%)+" xxx")
  return
 
creeravant:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewSisterByIndex(TVO,TVO_index%,0,"xxx "+str$(nadd%)+" xxx")
  return

creerapres:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewSisterByIndex(TVO,TVO_index%,1,"xxx "+str$(nadd%)+" xxx")
  return
 
supprimer:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  DeleteTreeViewNode(no_TVO%,item_index(no_TVO%))
  return
 
supprimerTVO:
  DeleteTreeViewObject(no_TVO%)
  return

nouveautest:
  creertest()
  return
 
explorateur:
  montrer% = 0
  DeleteTreeViewObject(no_TVO%)
  CreateTreeViewObject(no_TVO%,10,10,800,300)
  on_click no%,click
  d$ = dir_current$
  f$ = f$
  i% = instr("\",f$)
  while i%>0
    f$ = mid$(f$,i%+1,len(f$))
    i% = instr("\",f$)
  end_while
  CreateTreeViewRoot(TVO,f$)
  d$ = d$ + "\"
 
  niveau1% = 1
  nodir% = 1
  f$ = file_find_first$
  while f$<>"_"
    if dir_exists(d$+f$)=1
      AddTreeViewChildByIndex(TVO,niveau1%,f$+"²"+d$+"²")
      nodir% = 0
    else
      AddTreeViewChildByIndex(TVO,niveau1%,f$)
    end_if
    f$ = file_find_next$
  end_while
  file_find_close

  while nodir%=0
    nodir% = 1
    k% = 2
    while k%<=count(no_TVO%)
      s$ = item_read$(no_TVO%,k%)
      if right$(s$,1)="²"
        i% = instr(s$,"²")
        f$ = trim$(left$(s$,i%-1))
        d1$ = trim$(mid$(s$,i%+1,len(s$)))
        d1$ = left$(d1$,len(d1$)-1)+f$+"\"
        DeleteTreeViewNode(no_TVO%,k%)
        AddTreeViewSisterByIndex(TVO,k%,0,f$)
        dir_change d1$
        niveau2% = k%
        f1$ = file_find_first$
        while f1$<>"_"
          AddTreeViewChildByIndex(TVO,niveau2%,f1$)
          if dir_exists(d1$+f1$)=1
            AddTreeViewChildByIndex(TVO,niveau2%,f1$+"²"+d1$+"²")
            nodir% = 0
          else
            AddTreeViewChildByIndex(TVO,niveau2%,f1$)
          end_if
          k% = k% + 1
          f1$ = file_find_next$
        end_while
        file_find_close
       
      end_if
      k% = k% + 1
    end_while
  end_while

  return

 
 
sub UnfoldTreeViewNode(o,lig%,niv%)
  dim_local s$, s1$, i1%, i2%
  s$ = item_read$(o,lig%)
  s1$ = ""
  if n%>1 then s1$ = string$((niv%-1)*4," ")
  s$ = s1$ + "[-] "+mid$(s$,niv%*4+1,len(s$))
  item_delete o,lig%
  item_insert o,lig%,s$
  CalculateLineLevel(s$)
  CalculateTreeViewLevelVector(o,lig%,s$)
  FindTreeViewSource(o)
  i1% = lig%
  i2% = TVO_sources%(1)
  while i2%<count(o+1)
    i2% = i2% + 1
    s$ = item_read$(o+1,i2%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      i1% = i1% + 1
      item_insert o,i1%,s$
    else
      TVO_index% = 0
      exit_sub
    end_if
  end_while
  TVO_index% = 0
end_sub

sub FoldTreeViewNode(o,lig%,niv%)
  dim_local s$, s1$, i%
  s$ = item_read$(o,lig%)
  s1$ = ""
  if niv%>1 then s1$ = string$((niv%-1)*4," ")
  s$ = s1$ + "[+] "+mid$(s$,niv%*4+1,len(s$))
  item_delete o,lig%
  item_insert o,lig%,s$
  i% = lig%
  while i%<count(o)
    i% = i% + 1
    s$ = item_read$(o,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o,i%
      i% = i% - 1
    else
      TVO_index% = 0
      exit_sub
    end_if
    TVO_index% = 0
  end_while

end_sub

  ' determiner le niveau d'une ligne dans la section
sub CalculateLineLevel(s$)
  dim_local i%
  i% = 1
  while mid$(s$,i%,1)=" "
    i% = i% + 1
  end_while
  if mid$(s$,i%,1)<>"[" then i% = i% - 4
  i% = i% - 1
  TVO_n1% = i%/4 + 1
end_sub

' déterminer le vecteur des niveaux imbriqués
sub CalculateTreeViewLevelVector(o,ind%,r$)
  dim_local s$
  s$ = r$
  TVO_levels%(0) = 0
  i% = ind%
  CalculateLineLevel(s$)

  while TVO_n1%>1
    ldet% = 0
    n2% = TVO_n1%
    if n2%>1
      TVO_indexes%(TVO_levels%(0)+1) = i%
      while TVO_n1%>=n2%
        if TVO_n1%=n2% then ldet% = ldet% + 1
        i% = i% - 1
        s$ = item_read$(o,i%)
        CalculateLineLevel(s$)
      end_while
      TVO_levels%(0) = TVO_levels%(0) + 1
      TVO_levels%(TVO_levels%(0)) = ldet%
    end_if
  end_while
  TVO_levels%(0) = TVO_levels%(0) + 1
  TVO_levels%(TVO_levels%(0)) = 1
  TVO_indexes%(TVO_levels%(0)) = 1
end_sub

sub CreateTreeViewObject(no%,t%,l%,w%,h%)
  if (object_exists(no%)+object_exists(no%+1))>0
    message "TreeView - error 1"
    exit_sub
  end_if
  if variable("TVO")=0
    dim TVO, TVO_n1%, TVO_levels%(50), TVO_indexes%(50), TVO_sources%(50)
    dim TVO_index%
  end_if
 
  TVO = no%
  list TVO : top TVO,t% : left TVO,l%
    width TVO,w% : height TVO,h%
    font_name TVO,"Courier"
  if montrer%=0
    dlist TVO+1
  else
    list TVO+1 : top TVO+1,t% : left TVO+1,l%+w%+10
      width TVO+1,w% : height TVO+1,h%
      font_name TVO+1,"Courier"
  end_if
end_sub

sub DeleteTreeViewObject(n%)
  if variable("TVO")=1
    delete n%
    delete n%+1
    free TVO
    free TVO_n1%
    free TVO_levels%
    free TVO_indexes%
    free TVO_sources%
    free TVO_index%
  end_if
end_sub

sub CreateTreeViewRoot(o,txt$)
  if count(o)>0
    message "TreeView - error 2"
    exit_sub
  end_if
  item_add o,"    "+txt$
  item_add o+1,"    "+txt$
end_sub

sub AddTreeViewChildByIndex(o,ind%,txt$)
  if variable("AddTreeViewChildByIndex")=0 then dim AddTreeViewChildByIndex
  dim_local a$, index%, n2%
  if ind%>count(o)
    message "TreeView - error 3"
    exit_sub
  end_if
  i% = ind%
  s$ = item_read$(o,i%)
  CalculateLineLevel(s$)
  n2% = TVO_n1%
  CalculateTreeViewLevelVector(o,ind%,s$)
  FindTreeViewSource(o)
  FindTreeViewEndLevel(o,n2%)
  index% = TVO_indexes%(1)
  s$ = item_read$(o,index%)
  a$ = mid$(s$,(n2%-1)*4+1,3)
  CalculateLineLevel(s$)

  if a$="[+]"
    index% = TVO_sources%(1)
    if a$=sp3$ and (TVO_n1%=n2%)
      s$ = item_read$(o+1,index%)
      item_delete o+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o+1,index%+1,string$(n2%*4," ")+"    "+txt$
    AddTreeViewChildByIndex = index%+1
    TVO_index% = 0
  end_if
  if (a$=sp3$) or (a$="[-]")
    if (a$=sp3$) and (TVO_n1%=n2%)
      s$ = item_read$(o,index%)
      item_delete o,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o,index%+1,string$(n2%*4," ")+"    "+txt$
    AddTreeViewChildByIndex = index%+1
    index% = TVO_sources%(1)
    if a$=sp3$ and (TVO_n1%=n2%)
      s$ = item_read$(o+1,index%)
      item_delete o+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o+1,index%+1,string$(n2%*4," ")+"    "+txt$
  end_if
  TVO_index% = 0
end_sub

sub AddTreeViewSisterByIndex(o,ind%,off%,txt$)
  if variable("AddTreeViewSisterByIndex")=0 then dim AddTreeViewSisterByIndex
  dim_local a$, index%, n2%
  if ind%>count(o)
    message "TreeView - error 3"
    exit_sub
  end_if
  if (ind%=1) and (off%=0)
    message "TreeView - error 3"
    exit_sub
  end_if
  i% = ind%
  s$ = item_read$(o,i%)
  CalculateLineLevel(s$)
  n2% = TVO_n1%
  CalculateTreeViewLevelVector(o,ind%,s$)
  FindTreeViewSource(o)
  if off%=1 then FindTreeViewEndLevel(o,n2%)
  index% = TVO_indexes%(1)
  s$ = item_read$(o,index%)
  a$ = mid$(s$,(n2%-1)*4+1,3)
  CalculateLineLevel(s$)
  item_insert o,index%+off%,string$((n2%-1)*4," ")+"    "+txt$
  AddTreeViewSisterByIndex = index%+1
  index% = TVO_sources%(1)
  TVO_index% = 0
end_sub


sub FindTreeViewSource(o)
  dim_local k%, n%, s$
  sind% = 1
  s$ = item_read$(o+1,sind%)
  CalculateLineLevel(s$)
  for k%=TVO_levels%(0) to 1 step -1
    n% = TVO_levels%(k%)
    while (n%>1)
      sind% = sind% + 1
      if sind%>count(o+1)
        TVO_sources%(k%) = sind%
        exit_sub
      end_if
      s$ = item_read$(o+1,sind%)
      CalculateLineLevel(s$)
      if TVO_n1%=(TVO_levels%(0)+1-k%) then n% = n% - 1
    end_while
    TVO_sources%(k%) = sind%
    sind% = sind% + 1
  next k%
end_sub

sub FindTreeViewEndLevel(o,nx%)
  dim_local s$, k%, l%
  k% = TVO_sources%(1)
  while k%<count(o+1)
    k% = k% + 1
    l% = l% + 1
    s$ = item_read$(o+1,k%)
    CalculateLineLevel(s$)
    if TVO_n1%<=nx%
      TVO_sources%(1) = k% - 1
      if instr(item_read$(o,TVO_indexes%(1)),"[+]")=0
        TVO_levels%(1) = TVO_levels%(1) + l% - 1
        TVO_indexes%(1) = TVO_indexes%(1) + l% - 1
      end_if
      exit_sub
    end_if
  end_while
  TVO_sources%(1) = k%
  TVO_levels%(1) = TVO_levels%(1) + l%
  TVO_indexes%(1) = TVO_indexes%(1) + l%
end_sub

sub DeleteTreeViewNode(o,lig%)
  dim_local s$, s1$, i%, niv%
  s$ = item_read$(o,lig%)
  CalculateLineLevel(s$)
  niv% = TVO_n1%
  CalculateTreeViewLevelVector(o,lig%,s$)
  FindTreeViewSource(o)
  s1$ = ""
  i% = lig%
  while i%<count(o)
    i% = i% + 1
    s$ = item_read$(o,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o,i%
      i% = i% - 1
    else
      TVO_index% = 0
'      item_delete o,lig%
'      exit_sub
    end_if
    TVO_index% = 0
  end_while
  if lig%>1
    item_delete o,lig%
  else
    s$ = item_read$(o,lig%)
    s$ = string$(4," ")+mid$(s$,5,len(s$))
    item_delete o,lig%
    item_insert o,lig%,s$
  end_if

  i% = TVO_sources%(1)
  while i%<count(o+1)
    i% = i% + 1
    s$ = item_read$(o+1,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o+1,i%
      i% = i% - 1
    else
'      TVO_index% = 0
'      item_delete o+1,lig%
'      exit_sub
    end_if
'    TVO_index% = 0
  end_while
  lig% = TVO_sources%(1)
  if lig%>1
    item_delete o+1,lig%
  else
    s$ = item_read$(o+1,lig%)
    s$ = string$(4," ")+mid$(s$,5,len(s$))
    item_delete o+1,lig%
    item_insert o+1,lig%,s$
  end_if
end_sub

sub dump(t$)
  message t$+": niveaux: "+str$(TVO_levels%(0))+","+str$(TVO_levels%(1))+","+str$(TVO_levels%(2))+","+str$(TVO_levels%(3))+","+str$(TVO_levels%(4))
  message t$+": indices: "+str$(TVO_indexes%(0))+","+str$(TVO_indexes%(1))+","+str$(TVO_indexes%(2))+","+str$(TVO_indexes%(3))+","+str$(TVO_indexes%(4))
  message t$+": sources: "+str$(TVO_sources%(0))+","+str$(TVO_sources%(1))+","+str$(TVO_sources%(2))+","+str$(TVO_sources%(3))+","+str$(TVO_sources%(4))
end_sub

Maintenant, je vais finir la transcription en quelque chose d'utilisable par #INCLUDE.
Revenir en haut Aller en bas
http://klauspanoramic.comxa.com/index.html
Jicehel

Jicehel


Nombre de messages : 5947
Age : 51
Localisation : 77500
Date d'inscription : 18/04/2011

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyVen 7 Déc 2012 - 0:01

Klaus super boulot Wink
Par contre les fichiers s'affiche 2 fois chacun dans l'explorateur chez moi
Revenir en haut Aller en bas
Klaus

Klaus


Nombre de messages : 12301
Age : 75
Localisation : Ile de France
Date d'inscription : 29/12/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyVen 7 Déc 2012 - 0:31

Tu as raison: la ligne 228 est en trop - en résidu de la phase des tests.

Voici la version épurée:
Code:
' treeview.bas

labels()
constantes()
variables()
form0()
GUI()
initialisations()
end





sub labels()
  label click, creerfille, creeravant, creerapres, supprimer
  label supprimerTVO, nouveautest, explorateur
end_sub

sub constantes()
  dim chk%      : chk% = 0
  dim montrer%  : montrer% = 1
  dim sp3$      : sp3$ = string$(3," ")
end_sub

sub variables()
  dim no%, x%, n%, a%, b%, s$, i%, flag%, a1$, a2$, n2%, n3%, s1$
  dim nadd%
  dim no_TVO%
  dim ldet%, nsect%, lorg%, sind%, nindex%, niveau1%, niveau2%, niveau3%
  dim f$, d$, k%, d1$, f1$, nodir%
end_sub

sub form0()
  width 0,1000
  caption 0,"Test d'un objet TreeView
end_sub

sub GUI()
  no% = no% + 1 : button no% : top no%,10 : left no%,830 : width no%,130
    caption no%,"Créer noeud fille" : on_click no%,creerfille
  no% = no% + 1 : button no% : top no%,40 : left no%,830 : width no%,130
    caption no%,"Créer noeud soeur ainée" : on_click no%,creeravant
  no% = no% + 1 : button no% : top no%,70 : left no%,830 : width no%,130
    caption no%,"Créer noeud soeur cadette" : on_click no%,creerapres
  no% = no% + 1 : button no% : top no%,100 : left no%,830 : width no%,130
    caption no%,"Supprimer noeud" : on_click no%,supprimer

  no% = no% + 1 : button no% : top no%,150 : left no%,830 : width no%,130
    caption no%,"Supprimer TreeView" : on_click no%,supprimerTVO
  no% = no% + 1 : button no% : top no%,180 : left no%,830 : width no%,130
    caption no%,"Nouveau test" : on_click no%,nouveautest
  no% = no% + 1 : button no% : top no%,210 : left no%,830 : width no%,130
    caption no%,"Explorateur" : on_click no%,explorateur
end_sub

sub initialisations()
  no% = no% + 1 : no_TVO% = no%
  creertest()
end_sub

sub creertest()
  CreateTreeViewObject(no_TVO%,10,10,300,300)
  on_click no%,click
  CreateTreeViewRoot(TVO,"Racine")
  niveau1% = 1

  AddTreeViewChildByIndex(TVO,niveau1%,"aaaa")
  niveau2% = AddTreeViewChildByIndex

  AddTreeViewChildByIndex(TVO,niveau2%,"a1111111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"a2222222222")
  AddTreeViewChildByIndex(TVO,niveau1%,"bbbb")
  AddTreeViewChildByIndex(TVO,niveau1%,"cccc")
  niveau2% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau2%,"11111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"22222222")
  niveau3% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau3%,"2aaaaaaaaa")
  AddTreeViewChildByIndex(TVO,niveau3%,"2bbbbbbbbb")
  AddTreeViewChildByIndex(TVO,niveau2%,"33333333")
  AddTreeViewChildByIndex(TVO,niveau2%,"44444444")
  niveau3% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau3%,"4aaaaaaaaa")
  AddTreeViewChildByIndex(TVO,niveau3%,"4bbbbbbbbb")
  AddTreeViewChildByIndex(TVO,niveau1%,"dddd")
  niveau2% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau2%,"d11111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"d22222222")
end_sub







' positionnement des [+]
' niveau 1:  5...22 ==> 17
' niveau 2: 37...54 ==> 17
' niveau 3: 69...86 ==> 17
' niveau n:  a...b  n=int((x-5+32)/32)  a = (n-1)*32+5  b=a+17
click:
  TVO_index% = item_index(number_click) : ' mémoriser la ligne active
  x% = mouse_x_left_down(number_click)  : ' positionnement jorizontal du clic
  n% = int((x%-5+32)/32)                : ' multiple de 32 pixels avec offset de 5 pixels
  a% = (n%-1)*32 + 5                    : ' pixel de gauche pour [x]
  b% = a% + 16                          : ' pixel de droite pour [x]
  s$ = item_index$(number_click)        : ' la ligne cliquée
  CalculateLineLevel(s$)                : ' n1 = niveau d'arborescence
  CalculateTreeViewLevelVector(number_click,item_index(number_click),s$)
  TVO_n1% = TVO_levels%(0)              : ' le niveau d'arborescence
  s$ = item_index$(number_click)        : ' lire la ligne choisie
' message "click: ind="+str$(item_index(number_click))+"  x="+str$(x%)+"  a="+str$(a%)+"  b="+str$(b%)+"  n1="+str$(TVO_n1%)+"  s$="+s$
  flag% = 0                            : ' pas d'action par défaut
  if n%=TVO_n1%                        : ' si clic dans [x]:
    if mid$(s$,(n%-1)*4+2,1)="+" then flag% = 1  : ' action "déplier" si +
    if mid$(s$,(n%-1)*4+2,1)="-" then flag% = 2  : ' action "replier" si -
  end_if
 
'  dump("0")
  select flag%
    ' on a sélectionné une ligne
    case 0
      message "Ligne "+str$(item_index(number_click))+" niveau "+str$(TVO_n1%)
     
    ' on veut déplier une ligne ayant des lignes enfant
    case 1
'      message "déplier ligne "+str$(item_index(number_click))+" niveau "+str$(n%)
      UnfoldTreeViewNode(number_click,item_index(number_click),n%)
     
    ' on veut replier une ligne pour escamoter ses enfants
    case 2
'      message "replier ligne "+str$(item_index(number_click))+" niveau "+str$(n%)
      FoldTreeViewNode(number_click,item_index(number_click),n%)
  end_select
 
  return
 
creerfille:
  if TVO_index%=0
    message "Il fait choisir la ligne parent"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewChildByIndex(TVO,TVO_index%,"xxx "+str$(nadd%)+" xxx")
  return
 
creeravant:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewSisterByIndex(TVO,TVO_index%,0,"xxx "+str$(nadd%)+" xxx")
  return

creerapres:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewSisterByIndex(TVO,TVO_index%,1,"xxx "+str$(nadd%)+" xxx")
  return
 
supprimer:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  DeleteTreeViewNode(no_TVO%,item_index(no_TVO%))
  return
 
supprimerTVO:
  DeleteTreeViewObject(no_TVO%)
  return

nouveautest:
  DeleteTreeViewObject(no_TVO%)
  creertest()
  return
 
explorateur:
  montrer% = 0
  DeleteTreeViewObject(no_TVO%)
  CreateTreeViewObject(no_TVO%,10,10,800,300)
  on_click no%,click
  d$ = dir_current$
  f$ = f$
  i% = instr("\",f$)
  while i%>0
    f$ = mid$(f$,i%+1,len(f$))
    i% = instr("\",f$)
  end_while
  CreateTreeViewRoot(TVO,f$)
  d$ = d$ + "\"
  message "Attendre le message "+chr$(34)+"Chargement terminé."+chr$(34)
  niveau1% = 1
  nodir% = 1
  f$ = file_find_first$
  while f$<>"_"
    if dir_exists(d$+f$)=1
      AddTreeViewChildByIndex(TVO,niveau1%,f$+"²"+d$+"²")
      nodir% = 0
    else
      AddTreeViewChildByIndex(TVO,niveau1%,f$)
    end_if
    f$ = file_find_next$
  end_while
  file_find_close

  while nodir%=0
    nodir% = 1
    k% = 2
    while k%<=count(no_TVO%)
      s$ = item_read$(no_TVO%,k%)
      if right$(s$,1)="²"
        i% = instr(s$,"²")
        f$ = trim$(left$(s$,i%-1))
        d1$ = trim$(mid$(s$,i%+1,len(s$)))
        d1$ = left$(d1$,len(d1$)-1)+f$+"\"
        DeleteTreeViewNode(no_TVO%,k%)
        AddTreeViewSisterByIndex(TVO,k%,0,f$)
        dir_change d1$
        niveau2% = k%
        f1$ = file_find_first$
        while f1$<>"_"
          if dir_exists(d1$+f1$)=1
            AddTreeViewChildByIndex(TVO,niveau2%,f1$+"²"+d1$+"²")
            nodir% = 0
          else
            AddTreeViewChildByIndex(TVO,niveau2%,f1$)
          end_if
          k% = k% + 1
          f1$ = file_find_next$
        end_while
        file_find_close
       
      end_if
      k% = k% + 1
    end_while
  end_while
  message "Chargement terminé."
 
  return

 
 
sub UnfoldTreeViewNode(o,lig%,niv%)
  dim_local s$, s1$, i1%, i2%
  s$ = item_read$(o,lig%)
  s1$ = ""
  if n%>1 then s1$ = string$((niv%-1)*4," ")
  s$ = s1$ + "[-] "+mid$(s$,niv%*4+1,len(s$))
  item_delete o,lig%
  item_insert o,lig%,s$
  CalculateLineLevel(s$)
  CalculateTreeViewLevelVector(o,lig%,s$)
  FindTreeViewSource(o)
  i1% = lig%
  i2% = TVO_sources%(1)
  while i2%<count(o+1)
    i2% = i2% + 1
    s$ = item_read$(o+1,i2%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      i1% = i1% + 1
      item_insert o,i1%,s$
    else
      TVO_index% = 0
      exit_sub
    end_if
  end_while
  TVO_index% = 0
end_sub

sub FoldTreeViewNode(o,lig%,niv%)
  dim_local s$, s1$, i%
  s$ = item_read$(o,lig%)
  s1$ = ""
  if niv%>1 then s1$ = string$((niv%-1)*4," ")
  s$ = s1$ + "[+] "+mid$(s$,niv%*4+1,len(s$))
  item_delete o,lig%
  item_insert o,lig%,s$
  i% = lig%
  while i%<count(o)
    i% = i% + 1
    s$ = item_read$(o,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o,i%
      i% = i% - 1
    else
      TVO_index% = 0
      exit_sub
    end_if
    TVO_index% = 0
  end_while

end_sub

  ' determiner le niveau d'une ligne dans la section
sub CalculateLineLevel(s$)
  dim_local i%
  i% = 1
  while mid$(s$,i%,1)=" "
    i% = i% + 1
  end_while
  if mid$(s$,i%,1)<>"[" then i% = i% - 4
  i% = i% - 1
  TVO_n1% = i%/4 + 1
end_sub

' déterminer le vecteur des niveaux imbriqués
sub CalculateTreeViewLevelVector(o,ind%,r$)
  dim_local s$
  s$ = r$
  TVO_levels%(0) = 0
  i% = ind%
  CalculateLineLevel(s$)

  while TVO_n1%>1
    ldet% = 0
    n2% = TVO_n1%
    if n2%>1
      TVO_indexes%(TVO_levels%(0)+1) = i%
      while TVO_n1%>=n2%
        if TVO_n1%=n2% then ldet% = ldet% + 1
        i% = i% - 1
        s$ = item_read$(o,i%)
        CalculateLineLevel(s$)
      end_while
      TVO_levels%(0) = TVO_levels%(0) + 1
      TVO_levels%(TVO_levels%(0)) = ldet%
    end_if
  end_while
  TVO_levels%(0) = TVO_levels%(0) + 1
  TVO_levels%(TVO_levels%(0)) = 1
  TVO_indexes%(TVO_levels%(0)) = 1
end_sub

sub CreateTreeViewObject(no%,t%,l%,w%,h%)
  if (object_exists(no%)+object_exists(no%+1))>0
    message "TreeView - error 1"
    exit_sub
  end_if
  if variable("TVO")=0
    dim TVO, TVO_n1%, TVO_levels%(50), TVO_indexes%(50), TVO_sources%(50)
    dim TVO_index%
  end_if
 
  TVO = no%
  list TVO : top TVO,t% : left TVO,l%
    width TVO,w% : height TVO,h%
    font_name TVO,"Courier"
  if montrer%=0
    dlist TVO+1
  else
    list TVO+1 : top TVO+1,t% : left TVO+1,l%+w%+10
      width TVO+1,w% : height TVO+1,h%
      font_name TVO+1,"Courier"
  end_if
end_sub

sub DeleteTreeViewObject(n%)
  if variable("TVO")=1
    delete n%
    delete n%+1
    free TVO
    free TVO_n1%
    free TVO_levels%
    free TVO_indexes%
    free TVO_sources%
    free TVO_index%
  end_if
end_sub

sub CreateTreeViewRoot(o,txt$)
  if count(o)>0
    message "TreeView - error 2"
    exit_sub
  end_if
  item_add o,"    "+txt$
  item_add o+1,"    "+txt$
end_sub

sub AddTreeViewChildByIndex(o,ind%,txt$)
  if variable("AddTreeViewChildByIndex")=0 then dim AddTreeViewChildByIndex
  dim_local a$, index%, n2%
  if ind%>count(o)
    message "TreeView - error 3"
    exit_sub
  end_if
  i% = ind%
  s$ = item_read$(o,i%)
  CalculateLineLevel(s$)
  n2% = TVO_n1%
  CalculateTreeViewLevelVector(o,ind%,s$)
  FindTreeViewSource(o)
  FindTreeViewEndLevel(o,n2%)
  index% = TVO_indexes%(1)
  s$ = item_read$(o,index%)
  a$ = mid$(s$,(n2%-1)*4+1,3)
  CalculateLineLevel(s$)

  if a$="[+]"
    index% = TVO_sources%(1)
    if a$=sp3$ and (TVO_n1%=n2%)
      s$ = item_read$(o+1,index%)
      item_delete o+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o+1,index%+1,string$(n2%*4," ")+"    "+txt$
    AddTreeViewChildByIndex = index%+1
    TVO_index% = 0
  end_if
  if (a$=sp3$) or (a$="[-]")
    if (a$=sp3$) and (TVO_n1%=n2%)
      s$ = item_read$(o,index%)
      item_delete o,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o,index%+1,string$(n2%*4," ")+"    "+txt$
    AddTreeViewChildByIndex = index%+1
    index% = TVO_sources%(1)
    if a$=sp3$ and (TVO_n1%=n2%)
      s$ = item_read$(o+1,index%)
      item_delete o+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o+1,index%+1,string$(n2%*4," ")+"    "+txt$
  end_if
  TVO_index% = 0
end_sub

sub AddTreeViewSisterByIndex(o,ind%,off%,txt$)
  if variable("AddTreeViewSisterByIndex")=0 then dim AddTreeViewSisterByIndex
  dim_local a$, index%, n2%
  if ind%>count(o)
    message "TreeView - error 3"
    exit_sub
  end_if
  if (ind%=1) and (off%=0)
    message "TreeView - error 3"
    exit_sub
  end_if
  i% = ind%
  s$ = item_read$(o,i%)
  CalculateLineLevel(s$)
  n2% = TVO_n1%
  CalculateTreeViewLevelVector(o,ind%,s$)
  FindTreeViewSource(o)
  if off%=1 then FindTreeViewEndLevel(o,n2%)
  index% = TVO_indexes%(1)
  s$ = item_read$(o,index%)
  a$ = mid$(s$,(n2%-1)*4+1,3)
  CalculateLineLevel(s$)
  item_insert o,index%+off%,string$((n2%-1)*4," ")+"    "+txt$
  AddTreeViewSisterByIndex = index%+1
  index% = TVO_sources%(1)
  TVO_index% = 0
end_sub


sub FindTreeViewSource(o)
  dim_local k%, n%, s$
  sind% = 1
  s$ = item_read$(o+1,sind%)
  CalculateLineLevel(s$)
  for k%=TVO_levels%(0) to 1 step -1
    n% = TVO_levels%(k%)
    while (n%>1)
      sind% = sind% + 1
      if sind%>count(o+1)
        TVO_sources%(k%) = sind%
        exit_sub
      end_if
      s$ = item_read$(o+1,sind%)
      CalculateLineLevel(s$)
      if TVO_n1%=(TVO_levels%(0)+1-k%) then n% = n% - 1
    end_while
    TVO_sources%(k%) = sind%
    sind% = sind% + 1
  next k%
end_sub

sub FindTreeViewEndLevel(o,nx%)
  dim_local s$, k%, l%
  k% = TVO_sources%(1)
  while k%<count(o+1)
    k% = k% + 1
    l% = l% + 1
    s$ = item_read$(o+1,k%)
    CalculateLineLevel(s$)
    if TVO_n1%<=nx%
      TVO_sources%(1) = k% - 1
      if instr(item_read$(o,TVO_indexes%(1)),"[+]")=0
        TVO_levels%(1) = TVO_levels%(1) + l% - 1
        TVO_indexes%(1) = TVO_indexes%(1) + l% - 1
      end_if
      exit_sub
    end_if
  end_while
  TVO_sources%(1) = k%
  TVO_levels%(1) = TVO_levels%(1) + l%
  TVO_indexes%(1) = TVO_indexes%(1) + l%
end_sub

sub DeleteTreeViewNode(o,lig%)
  dim_local s$, s1$, i%, niv%
  s$ = item_read$(o,lig%)
  CalculateLineLevel(s$)
  niv% = TVO_n1%
  CalculateTreeViewLevelVector(o,lig%,s$)
  FindTreeViewSource(o)
  s1$ = ""
  i% = lig%
  while i%<count(o)
    i% = i% + 1
    s$ = item_read$(o,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o,i%
      i% = i% - 1
    else
      TVO_index% = 0
'      item_delete o,lig%
'      exit_sub
    end_if
    TVO_index% = 0
  end_while
  if lig%>1
    item_delete o,lig%
  else
    s$ = item_read$(o,lig%)
    s$ = string$(4," ")+mid$(s$,5,len(s$))
    item_delete o,lig%
    item_insert o,lig%,s$
  end_if

  i% = TVO_sources%(1)
  while i%<count(o+1)
    i% = i% + 1
    s$ = item_read$(o+1,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o+1,i%
      i% = i% - 1
    else
'      TVO_index% = 0
'      item_delete o+1,lig%
'      exit_sub
    end_if
'    TVO_index% = 0
  end_while
  lig% = TVO_sources%(1)
  if lig%>1
    item_delete o+1,lig%
  else
    s$ = item_read$(o+1,lig%)
    s$ = string$(4," ")+mid$(s$,5,len(s$))
    item_delete o+1,lig%
    item_insert o+1,lig%,s$
  end_if
end_sub

sub dump(t$)
  message t$+": niveaux: "+str$(TVO_levels%(0))+","+str$(TVO_levels%(1))+","+str$(TVO_levels%(2))+","+str$(TVO_levels%(3))+","+str$(TVO_levels%(4))
  message t$+": indices: "+str$(TVO_indexes%(0))+","+str$(TVO_indexes%(1))+","+str$(TVO_indexes%(2))+","+str$(TVO_indexes%(3))+","+str$(TVO_indexes%(4))
  message t$+": sources: "+str$(TVO_sources%(0))+","+str$(TVO_sources%(1))+","+str$(TVO_sources%(2))+","+str$(TVO_sources%(3))+","+str$(TVO_sources%(4))
end_sub

EDIT

Et comme c'est un peu long, j'ai ajouté des messages pour signaler début et fin du chargement de l'arborescence des dossiers...


Dernière édition par Klaus le Ven 7 Déc 2012 - 1:07, édité 1 fois
Revenir en haut Aller en bas
http://klauspanoramic.comxa.com/index.html
Nardo26

Nardo26


Nombre de messages : 2294
Age : 55
Localisation : Valence
Date d'inscription : 02/07/2010

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyVen 7 Déc 2012 - 0:39

Nickel Klaus ! Simulation d'un objet TreeView - Page 2 18

Une chose quand même à rajouter dans ta démo :
la suppression automatique TreeView avant de relancer un nouveau test (sinon 2 messages d'erreur... Wink )
Revenir en haut Aller en bas
http://nardo26.lescigales.org
Klaus

Klaus


Nombre de messages : 12301
Age : 75
Localisation : Ile de France
Date d'inscription : 29/12/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyVen 7 Déc 2012 - 1:08

OK. J'ai rajouté la ligne dans le source ci-dessus...
Revenir en haut Aller en bas
http://klauspanoramic.comxa.com/index.html
Jean Claude

Jean Claude


Nombre de messages : 5950
Age : 69
Localisation : 83 Var
Date d'inscription : 07/05/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyVen 7 Déc 2012 - 8:04

Super Boulot,

Je viens de tester le bouton "Explorateur" et en haut du list j'ai [-] mais pas le nom du dossier ("racine")...
Tous le reste s'affiche correctement

A+
Revenir en haut Aller en bas
Klaus

Klaus


Nombre de messages : 12301
Age : 75
Localisation : Ile de France
Date d'inscription : 29/12/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyVen 7 Déc 2012 - 9:31

A la ligne 196, tu remplaces d$ par f$. Voici:
Code:
' treeview.bas

labels()
constantes()
variables()
form0()
GUI()
initialisations()
end





sub labels()
  label click, creerfille, creeravant, creerapres, supprimer
  label supprimerTVO, nouveautest, explorateur
end_sub

sub constantes()
  dim chk%      : chk% = 0
  dim montrer%  : montrer% = 1
  dim sp3$      : sp3$ = string$(3," ")
end_sub

sub variables()
  dim no%, x%, n%, a%, b%, s$, i%, flag%, a1$, a2$, n2%, n3%, s1$
  dim nadd%
  dim no_TVO%
  dim ldet%, nsect%, lorg%, sind%, nindex%, niveau1%, niveau2%, niveau3%
  dim f$, d$, k%, d1$, f1$, nodir%
end_sub

sub form0()
  width 0,1000
  caption 0,"Test d'un objet TreeView
end_sub

sub GUI()
  no% = no% + 1 : button no% : top no%,10 : left no%,830 : width no%,130
    caption no%,"Créer noeud fille" : on_click no%,creerfille
  no% = no% + 1 : button no% : top no%,40 : left no%,830 : width no%,130
    caption no%,"Créer noeud soeur ainée" : on_click no%,creeravant
  no% = no% + 1 : button no% : top no%,70 : left no%,830 : width no%,130
    caption no%,"Créer noeud soeur cadette" : on_click no%,creerapres
  no% = no% + 1 : button no% : top no%,100 : left no%,830 : width no%,130
    caption no%,"Supprimer noeud" : on_click no%,supprimer

  no% = no% + 1 : button no% : top no%,150 : left no%,830 : width no%,130
    caption no%,"Supprimer TreeView" : on_click no%,supprimerTVO
  no% = no% + 1 : button no% : top no%,180 : left no%,830 : width no%,130
    caption no%,"Nouveau test" : on_click no%,nouveautest
  no% = no% + 1 : button no% : top no%,210 : left no%,830 : width no%,130
    caption no%,"Explorateur" : on_click no%,explorateur
end_sub

sub initialisations()
  no% = no% + 1 : no_TVO% = no%
  creertest()
end_sub

sub creertest()
  CreateTreeViewObject(no_TVO%,10,10,300,300)
  on_click no%,click
  CreateTreeViewRoot(TVO,"Racine")
  niveau1% = 1

  AddTreeViewChildByIndex(TVO,niveau1%,"aaaa")
  niveau2% = AddTreeViewChildByIndex

  AddTreeViewChildByIndex(TVO,niveau2%,"a1111111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"a2222222222")
  AddTreeViewChildByIndex(TVO,niveau1%,"bbbb")
  AddTreeViewChildByIndex(TVO,niveau1%,"cccc")
  niveau2% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau2%,"11111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"22222222")
  niveau3% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau3%,"2aaaaaaaaa")
  AddTreeViewChildByIndex(TVO,niveau3%,"2bbbbbbbbb")
  AddTreeViewChildByIndex(TVO,niveau2%,"33333333")
  AddTreeViewChildByIndex(TVO,niveau2%,"44444444")
  niveau3% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau3%,"4aaaaaaaaa")
  AddTreeViewChildByIndex(TVO,niveau3%,"4bbbbbbbbb")
  AddTreeViewChildByIndex(TVO,niveau1%,"dddd")
  niveau2% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau2%,"d11111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"d22222222")
end_sub







' positionnement des [+]
' niveau 1:  5...22 ==> 17
' niveau 2: 37...54 ==> 17
' niveau 3: 69...86 ==> 17
' niveau n:  a...b  n=int((x-5+32)/32)  a = (n-1)*32+5  b=a+17
click:
  TVO_index% = item_index(number_click) : ' mémoriser la ligne active
  x% = mouse_x_left_down(number_click)  : ' positionnement jorizontal du clic
  n% = int((x%-5+32)/32)                : ' multiple de 32 pixels avec offset de 5 pixels
  a% = (n%-1)*32 + 5                    : ' pixel de gauche pour [x]
  b% = a% + 16                          : ' pixel de droite pour [x]
  s$ = item_index$(number_click)        : ' la ligne cliquée
  CalculateLineLevel(s$)                : ' n1 = niveau d'arborescence
  CalculateTreeViewLevelVector(number_click,item_index(number_click),s$)
  TVO_n1% = TVO_levels%(0)              : ' le niveau d'arborescence
  s$ = item_index$(number_click)        : ' lire la ligne choisie
' message "click: ind="+str$(item_index(number_click))+"  x="+str$(x%)+"  a="+str$(a%)+"  b="+str$(b%)+"  n1="+str$(TVO_n1%)+"  s$="+s$
  flag% = 0                            : ' pas d'action par défaut
  if n%=TVO_n1%                        : ' si clic dans [x]:
    if mid$(s$,(n%-1)*4+2,1)="+" then flag% = 1  : ' action "déplier" si +
    if mid$(s$,(n%-1)*4+2,1)="-" then flag% = 2  : ' action "replier" si -
  end_if
 
'  dump("0")
  select flag%
    ' on a sélectionné une ligne
    case 0
      message "Ligne "+str$(item_index(number_click))+" niveau "+str$(TVO_n1%)
     
    ' on veut déplier une ligne ayant des lignes enfant
    case 1
'      message "déplier ligne "+str$(item_index(number_click))+" niveau "+str$(n%)
      UnfoldTreeViewNode(number_click,item_index(number_click),n%)
     
    ' on veut replier une ligne pour escamoter ses enfants
    case 2
'      message "replier ligne "+str$(item_index(number_click))+" niveau "+str$(n%)
      FoldTreeViewNode(number_click,item_index(number_click),n%)
  end_select
 
  return
 
creerfille:
  if TVO_index%=0
    message "Il fait choisir la ligne parent"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewChildByIndex(TVO,TVO_index%,"xxx "+str$(nadd%)+" xxx")
  return
 
creeravant:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewSisterByIndex(TVO,TVO_index%,0,"xxx "+str$(nadd%)+" xxx")
  return

creerapres:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewSisterByIndex(TVO,TVO_index%,1,"xxx "+str$(nadd%)+" xxx")
  return
 
supprimer:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  DeleteTreeViewNode(no_TVO%,item_index(no_TVO%))
  return
 
supprimerTVO:
  DeleteTreeViewObject(no_TVO%)
  return

nouveautest:
  DeleteTreeViewObject(no_TVO%)
  creertest()
  return
 
explorateur:
  montrer% = 0
  DeleteTreeViewObject(no_TVO%)
  CreateTreeViewObject(no_TVO%,10,10,800,300)
  on_click no%,click
  d$ = dir_current$
  f$ = f$
  i% = instr("\",f$)
  while i%>0
    f$ = mid$(f$,i%+1,len(f$))
    i% = instr("\",f$)
  end_while
  CreateTreeViewRoot(TVO,d$)
  d$ = d$ + "\"
  message "Attendre le message "+chr$(34)+"Chargement terminé."+chr$(34)
  niveau1% = 1
  nodir% = 1
  f$ = file_find_first$
  while f$<>"_"
    if dir_exists(d$+f$)=1
      AddTreeViewChildByIndex(TVO,niveau1%,f$+"²"+d$+"²")
      nodir% = 0
    else
      AddTreeViewChildByIndex(TVO,niveau1%,f$)
    end_if
    f$ = file_find_next$
  end_while
  file_find_close

  while nodir%=0
    nodir% = 1
    k% = 2
    while k%<=count(no_TVO%)
      s$ = item_read$(no_TVO%,k%)
      if right$(s$,1)="²"
        i% = instr(s$,"²")
        f$ = trim$(left$(s$,i%-1))
        d1$ = trim$(mid$(s$,i%+1,len(s$)))
        d1$ = left$(d1$,len(d1$)-1)+f$+"\"
        DeleteTreeViewNode(no_TVO%,k%)
        AddTreeViewSisterByIndex(TVO,k%,0,f$)
        dir_change d1$
        niveau2% = k%
        f1$ = file_find_first$
        while f1$<>"_"
          if dir_exists(d1$+f1$)=1
            AddTreeViewChildByIndex(TVO,niveau2%,f1$+"²"+d1$+"²")
            nodir% = 0
          else
            AddTreeViewChildByIndex(TVO,niveau2%,f1$)
          end_if
          k% = k% + 1
          f1$ = file_find_next$
        end_while
        file_find_close
       
      end_if
      k% = k% + 1
    end_while
  end_while
  message "Chargement terminé."
 
  return

 
 
sub UnfoldTreeViewNode(o,lig%,niv%)
  dim_local s$, s1$, i1%, i2%
  s$ = item_read$(o,lig%)
  s1$ = ""
  if n%>1 then s1$ = string$((niv%-1)*4," ")
  s$ = s1$ + "[-] "+mid$(s$,niv%*4+1,len(s$))
  item_delete o,lig%
  item_insert o,lig%,s$
  CalculateLineLevel(s$)
  CalculateTreeViewLevelVector(o,lig%,s$)
  FindTreeViewSource(o)
  i1% = lig%
  i2% = TVO_sources%(1)
  while i2%<count(o+1)
    i2% = i2% + 1
    s$ = item_read$(o+1,i2%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      i1% = i1% + 1
      item_insert o,i1%,s$
    else
      TVO_index% = 0
      exit_sub
    end_if
  end_while
  TVO_index% = 0
end_sub

sub FoldTreeViewNode(o,lig%,niv%)
  dim_local s$, s1$, i%
  s$ = item_read$(o,lig%)
  s1$ = ""
  if niv%>1 then s1$ = string$((niv%-1)*4," ")
  s$ = s1$ + "[+] "+mid$(s$,niv%*4+1,len(s$))
  item_delete o,lig%
  item_insert o,lig%,s$
  i% = lig%
  while i%<count(o)
    i% = i% + 1
    s$ = item_read$(o,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o,i%
      i% = i% - 1
    else
      TVO_index% = 0
      exit_sub
    end_if
    TVO_index% = 0
  end_while

end_sub

  ' determiner le niveau d'une ligne dans la section
sub CalculateLineLevel(s$)
  dim_local i%
  i% = 1
  while mid$(s$,i%,1)=" "
    i% = i% + 1
  end_while
  if mid$(s$,i%,1)<>"[" then i% = i% - 4
  i% = i% - 1
  TVO_n1% = i%/4 + 1
end_sub

' déterminer le vecteur des niveaux imbriqués
sub CalculateTreeViewLevelVector(o,ind%,r$)
  dim_local s$
  s$ = r$
  TVO_levels%(0) = 0
  i% = ind%
  CalculateLineLevel(s$)

  while TVO_n1%>1
    ldet% = 0
    n2% = TVO_n1%
    if n2%>1
      TVO_indexes%(TVO_levels%(0)+1) = i%
      while TVO_n1%>=n2%
        if TVO_n1%=n2% then ldet% = ldet% + 1
        i% = i% - 1
        s$ = item_read$(o,i%)
        CalculateLineLevel(s$)
      end_while
      TVO_levels%(0) = TVO_levels%(0) + 1
      TVO_levels%(TVO_levels%(0)) = ldet%
    end_if
  end_while
  TVO_levels%(0) = TVO_levels%(0) + 1
  TVO_levels%(TVO_levels%(0)) = 1
  TVO_indexes%(TVO_levels%(0)) = 1
end_sub

sub CreateTreeViewObject(no%,t%,l%,w%,h%)
  if (object_exists(no%)+object_exists(no%+1))>0
    message "TreeView - error 1"
    exit_sub
  end_if
  if variable("TVO")=0
    dim TVO, TVO_n1%, TVO_levels%(50), TVO_indexes%(50), TVO_sources%(50)
    dim TVO_index%
  end_if
 
  TVO = no%
  list TVO : top TVO,t% : left TVO,l%
    width TVO,w% : height TVO,h%
    font_name TVO,"Courier"
  if montrer%=0
    dlist TVO+1
  else
    list TVO+1 : top TVO+1,t% : left TVO+1,l%+w%+10
      width TVO+1,w% : height TVO+1,h%
      font_name TVO+1,"Courier"
  end_if
end_sub

sub DeleteTreeViewObject(n%)
  if variable("TVO")=1
    delete n%
    delete n%+1
    free TVO
    free TVO_n1%
    free TVO_levels%
    free TVO_indexes%
    free TVO_sources%
    free TVO_index%
  end_if
end_sub

sub CreateTreeViewRoot(o,txt$)
  if count(o)>0
    message "TreeView - error 2"
    exit_sub
  end_if
  item_add o,"    "+txt$
  item_add o+1,"    "+txt$
end_sub

sub AddTreeViewChildByIndex(o,ind%,txt$)
  if variable("AddTreeViewChildByIndex")=0 then dim AddTreeViewChildByIndex
  dim_local a$, index%, n2%
  if ind%>count(o)
    message "TreeView - error 3"
    exit_sub
  end_if
  i% = ind%
  s$ = item_read$(o,i%)
  CalculateLineLevel(s$)
  n2% = TVO_n1%
  CalculateTreeViewLevelVector(o,ind%,s$)
  FindTreeViewSource(o)
  FindTreeViewEndLevel(o,n2%)
  index% = TVO_indexes%(1)
  s$ = item_read$(o,index%)
  a$ = mid$(s$,(n2%-1)*4+1,3)
  CalculateLineLevel(s$)

  if a$="[+]"
    index% = TVO_sources%(1)
    if a$=sp3$ and (TVO_n1%=n2%)
      s$ = item_read$(o+1,index%)
      item_delete o+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o+1,index%+1,string$(n2%*4," ")+"    "+txt$
    AddTreeViewChildByIndex = index%+1
    TVO_index% = 0
  end_if
  if (a$=sp3$) or (a$="[-]")
    if (a$=sp3$) and (TVO_n1%=n2%)
      s$ = item_read$(o,index%)
      item_delete o,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o,index%+1,string$(n2%*4," ")+"    "+txt$
    AddTreeViewChildByIndex = index%+1
    index% = TVO_sources%(1)
    if a$=sp3$ and (TVO_n1%=n2%)
      s$ = item_read$(o+1,index%)
      item_delete o+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o+1,index%+1,string$(n2%*4," ")+"    "+txt$
  end_if
  TVO_index% = 0
end_sub

sub AddTreeViewSisterByIndex(o,ind%,off%,txt$)
  if variable("AddTreeViewSisterByIndex")=0 then dim AddTreeViewSisterByIndex
  dim_local a$, index%, n2%
  if ind%>count(o)
    message "TreeView - error 3"
    exit_sub
  end_if
  if (ind%=1) and (off%=0)
    message "TreeView - error 3"
    exit_sub
  end_if
  i% = ind%
  s$ = item_read$(o,i%)
  CalculateLineLevel(s$)
  n2% = TVO_n1%
  CalculateTreeViewLevelVector(o,ind%,s$)
  FindTreeViewSource(o)
  if off%=1 then FindTreeViewEndLevel(o,n2%)
  index% = TVO_indexes%(1)
  s$ = item_read$(o,index%)
  a$ = mid$(s$,(n2%-1)*4+1,3)
  CalculateLineLevel(s$)
  item_insert o,index%+off%,string$((n2%-1)*4," ")+"    "+txt$
  AddTreeViewSisterByIndex = index%+1
  index% = TVO_sources%(1)
  TVO_index% = 0
end_sub


sub FindTreeViewSource(o)
  dim_local k%, n%, s$
  sind% = 1
  s$ = item_read$(o+1,sind%)
  CalculateLineLevel(s$)
  for k%=TVO_levels%(0) to 1 step -1
    n% = TVO_levels%(k%)
    while (n%>1)
      sind% = sind% + 1
      if sind%>count(o+1)
        TVO_sources%(k%) = sind%
        exit_sub
      end_if
      s$ = item_read$(o+1,sind%)
      CalculateLineLevel(s$)
      if TVO_n1%=(TVO_levels%(0)+1-k%) then n% = n% - 1
    end_while
    TVO_sources%(k%) = sind%
    sind% = sind% + 1
  next k%
end_sub

sub FindTreeViewEndLevel(o,nx%)
  dim_local s$, k%, l%
  k% = TVO_sources%(1)
  while k%<count(o+1)
    k% = k% + 1
    l% = l% + 1
    s$ = item_read$(o+1,k%)
    CalculateLineLevel(s$)
    if TVO_n1%<=nx%
      TVO_sources%(1) = k% - 1
      if instr(item_read$(o,TVO_indexes%(1)),"[+]")=0
        TVO_levels%(1) = TVO_levels%(1) + l% - 1
        TVO_indexes%(1) = TVO_indexes%(1) + l% - 1
      end_if
      exit_sub
    end_if
  end_while
  TVO_sources%(1) = k%
  TVO_levels%(1) = TVO_levels%(1) + l%
  TVO_indexes%(1) = TVO_indexes%(1) + l%
end_sub

sub DeleteTreeViewNode(o,lig%)
  dim_local s$, s1$, i%, niv%
  s$ = item_read$(o,lig%)
  CalculateLineLevel(s$)
  niv% = TVO_n1%
  CalculateTreeViewLevelVector(o,lig%,s$)
  FindTreeViewSource(o)
  s1$ = ""
  i% = lig%
  while i%<count(o)
    i% = i% + 1
    s$ = item_read$(o,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o,i%
      i% = i% - 1
    else
      TVO_index% = 0
'      item_delete o,lig%
'      exit_sub
    end_if
    TVO_index% = 0
  end_while
  if lig%>1
    item_delete o,lig%
  else
    s$ = item_read$(o,lig%)
    s$ = string$(4," ")+mid$(s$,5,len(s$))
    item_delete o,lig%
    item_insert o,lig%,s$
  end_if

  i% = TVO_sources%(1)
  while i%<count(o+1)
    i% = i% + 1
    s$ = item_read$(o+1,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o+1,i%
      i% = i% - 1
    else
'      TVO_index% = 0
'      item_delete o+1,lig%
'      exit_sub
    end_if
'    TVO_index% = 0
  end_while
  lig% = TVO_sources%(1)
  if lig%>1
    item_delete o+1,lig%
  else
    s$ = item_read$(o+1,lig%)
    s$ = string$(4," ")+mid$(s$,5,len(s$))
    item_delete o+1,lig%
    item_insert o+1,lig%,s$
  end_if
end_sub

sub dump(t$)
  message t$+": niveaux: "+str$(TVO_levels%(0))+","+str$(TVO_levels%(1))+","+str$(TVO_levels%(2))+","+str$(TVO_levels%(3))+","+str$(TVO_levels%(4))
  message t$+": indices: "+str$(TVO_indexes%(0))+","+str$(TVO_indexes%(1))+","+str$(TVO_indexes%(2))+","+str$(TVO_indexes%(3))+","+str$(TVO_indexes%(4))
  message t$+": sources: "+str$(TVO_sources%(0))+","+str$(TVO_sources%(1))+","+str$(TVO_sources%(2))+","+str$(TVO_sources%(3))+","+str$(TVO_sources%(4))
end_sub
Revenir en haut Aller en bas
http://klauspanoramic.comxa.com/index.html
Jean Claude

Jean Claude


Nombre de messages : 5950
Age : 69
Localisation : 83 Var
Date d'inscription : 07/05/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyVen 7 Déc 2012 - 10:25

OK c'est bon.

J'ai eu la mauvaise idée de lancer le programme à partir du bureau... Au bout de 5 mn j'ai arrêté le programme par le gestionnaire des tâches (non pas qu'il était planté, mais il y a tellement de fichier.... Very Happy ).
J'ai tenté de le fermer par la crois rouge du FORM, que nenni, il faut attendre.
Pour pallier au problème j'ai ajouter un ON_CLOSE 0,fin

Si çà te plait...
Code:
' treeview.bas

labels()
constantes()
variables()
form0()
GUI()
initialisations()
end





sub labels()
  label fin, click, creerfille, creeravant, creerapres, supprimer
  label supprimerTVO, nouveautest, explorateur
end_sub

sub constantes()
  dim chk%      : chk% = 0
  dim montrer%  : montrer% = 1
  dim sp3$      : sp3$ = string$(3," ")
end_sub

sub variables()
  dim no%, x%, n%, a%, b%, s$, i%, flag%, a1$, a2$, n2%, n3%, s1$
  dim nadd%
  dim no_TVO%
  dim ldet%, nsect%, lorg%, sind%, nindex%, niveau1%, niveau2%, niveau3%
  dim f$, d$, k%, d1$, f1$, nodir%
end_sub

sub form0()
  width 0,1000
  caption 0,"Test d'un objet TreeView
  on_close 0,fin
end_sub

sub GUI()
  no% = no% + 1 : button no% : top no%,10 : left no%,830 : width no%,130
    caption no%,"Créer noeud fille" : on_click no%,creerfille
  no% = no% + 1 : button no% : top no%,40 : left no%,830 : width no%,130
    caption no%,"Créer noeud soeur ainée" : on_click no%,creeravant
  no% = no% + 1 : button no% : top no%,70 : left no%,830 : width no%,130
    caption no%,"Créer noeud soeur cadette" : on_click no%,creerapres
  no% = no% + 1 : button no% : top no%,100 : left no%,830 : width no%,130
    caption no%,"Supprimer noeud" : on_click no%,supprimer

  no% = no% + 1 : button no% : top no%,150 : left no%,830 : width no%,130
    caption no%,"Supprimer TreeView" : on_click no%,supprimerTVO
  no% = no% + 1 : button no% : top no%,180 : left no%,830 : width no%,130
    caption no%,"Nouveau test" : on_click no%,nouveautest
  no% = no% + 1 : button no% : top no%,210 : left no%,830 : width no%,130
    caption no%,"Explorateur" : on_click no%,explorateur
end_sub

sub initialisations()
  no% = no% + 1 : no_TVO% = no%
  creertest()
end_sub

sub creertest()
  CreateTreeViewObject(no_TVO%,10,10,300,300)
  on_click no%,click
  CreateTreeViewRoot(TVO,"Racine")
  niveau1% = 1

  AddTreeViewChildByIndex(TVO,niveau1%,"aaaa")
  niveau2% = AddTreeViewChildByIndex

  AddTreeViewChildByIndex(TVO,niveau2%,"a1111111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"a2222222222")
  AddTreeViewChildByIndex(TVO,niveau1%,"bbbb")
  AddTreeViewChildByIndex(TVO,niveau1%,"cccc")
  niveau2% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau2%,"11111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"22222222")
  niveau3% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau3%,"2aaaaaaaaa")
  AddTreeViewChildByIndex(TVO,niveau3%,"2bbbbbbbbb")
  AddTreeViewChildByIndex(TVO,niveau2%,"33333333")
  AddTreeViewChildByIndex(TVO,niveau2%,"44444444")
  niveau3% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau3%,"4aaaaaaaaa")
  AddTreeViewChildByIndex(TVO,niveau3%,"4bbbbbbbbb")
  AddTreeViewChildByIndex(TVO,niveau1%,"dddd")
  niveau2% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau2%,"d11111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"d22222222")
end_sub







' positionnement des [+]
' niveau 1:  5...22 ==> 17
' niveau 2: 37...54 ==> 17
' niveau 3: 69...86 ==> 17
' niveau n:  a...b  n=int((x-5+32)/32)  a = (n-1)*32+5  b=a+17
click:
  TVO_index% = item_index(number_click) : ' mémoriser la ligne active
  x% = mouse_x_left_down(number_click)  : ' positionnement jorizontal du clic
  n% = int((x%-5+32)/32)                : ' multiple de 32 pixels avec offset de 5 pixels
  a% = (n%-1)*32 + 5                    : ' pixel de gauche pour [x]
  b% = a% + 16                          : ' pixel de droite pour [x]
  s$ = item_index$(number_click)        : ' la ligne cliquée
  CalculateLineLevel(s$)                : ' n1 = niveau d'arborescence
  CalculateTreeViewLevelVector(number_click,item_index(number_click),s$)
  TVO_n1% = TVO_levels%(0)              : ' le niveau d'arborescence
  s$ = item_index$(number_click)        : ' lire la ligne choisie
' message "click: ind="+str$(item_index(number_click))+"  x="+str$(x%)+"  a="+str$(a%)+"  b="+str$(b%)+"  n1="+str$(TVO_n1%)+"  s$="+s$
  flag% = 0                            : ' pas d'action par défaut
  if n%=TVO_n1%                        : ' si clic dans [x]:
    if mid$(s$,(n%-1)*4+2,1)="+" then flag% = 1  : ' action "déplier" si +
    if mid$(s$,(n%-1)*4+2,1)="-" then flag% = 2  : ' action "replier" si -
  end_if

'  dump("0")
  select flag%
    ' on a sélectionné une ligne
    case 0
      message "Ligne "+str$(item_index(number_click))+" niveau "+str$(TVO_n1%)

    ' on veut déplier une ligne ayant des lignes enfant
    case 1
'      message "déplier ligne "+str$(item_index(number_click))+" niveau "+str$(n%)
      UnfoldTreeViewNode(number_click,item_index(number_click),n%)

    ' on veut replier une ligne pour escamoter ses enfants
    case 2
'      message "replier ligne "+str$(item_index(number_click))+" niveau "+str$(n%)
      FoldTreeViewNode(number_click,item_index(number_click),n%)
  end_select

  return

creerfille:
  if TVO_index%=0
    message "Il fait choisir la ligne parent"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewChildByIndex(TVO,TVO_index%,"xxx "+str$(nadd%)+" xxx")
  return

creeravant:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewSisterByIndex(TVO,TVO_index%,0,"xxx "+str$(nadd%)+" xxx")
  return

creerapres:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewSisterByIndex(TVO,TVO_index%,1,"xxx "+str$(nadd%)+" xxx")
  return

supprimer:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  DeleteTreeViewNode(no_TVO%,item_index(no_TVO%))
  return

supprimerTVO:
  DeleteTreeViewObject(no_TVO%)
  return

nouveautest:
  DeleteTreeViewObject(no_TVO%)
  creertest()
  return

explorateur:
  montrer% = 0
  DeleteTreeViewObject(no_TVO%)
  CreateTreeViewObject(no_TVO%,10,10,800,300)
  on_click no%,click
  d$ = dir_current$
  f$ = f$
  i% = instr("\",f$)
  while i%>0
    f$ = mid$(f$,i%+1,len(f$))
    i% = instr("\",f$)
  end_while
  CreateTreeViewRoot(TVO,d$)
  d$ = d$ + "\"
  message "Attendre le message "+chr$(34)+"Chargement terminé."+chr$(34)
  niveau1% = 1
  nodir% = 1
  f$ = file_find_first$
  while f$<>"_"
    if dir_exists(d$+f$)=1
      AddTreeViewChildByIndex(TVO,niveau1%,f$+"²"+d$+"²")
      nodir% = 0
    else
      AddTreeViewChildByIndex(TVO,niveau1%,f$)
    end_if
    f$ = file_find_next$
  end_while
  file_find_close

  while nodir%=0
    nodir% = 1
    k% = 2
    while k%<=count(no_TVO%)
      s$ = item_read$(no_TVO%,k%)
      if right$(s$,1)="²"
        i% = instr(s$,"²")
        f$ = trim$(left$(s$,i%-1))
        d1$ = trim$(mid$(s$,i%+1,len(s$)))
        d1$ = left$(d1$,len(d1$)-1)+f$+"\"
        DeleteTreeViewNode(no_TVO%,k%)
        AddTreeViewSisterByIndex(TVO,k%,0,f$)
        dir_change d1$
        niveau2% = k%
        f1$ = file_find_first$
        while f1$<>"_"
          if dir_exists(d1$+f1$)=1
            AddTreeViewChildByIndex(TVO,niveau2%,f1$+"²"+d1$+"²")
            nodir% = 0
          else
            AddTreeViewChildByIndex(TVO,niveau2%,f1$)
          end_if
          k% = k% + 1
          f1$ = file_find_next$
        end_while
        file_find_close

      end_if
      k% = k% + 1
    end_while
  end_while
  message "Chargement terminé."

  return



sub UnfoldTreeViewNode(o,lig%,niv%)
  dim_local s$, s1$, i1%, i2%
  s$ = item_read$(o,lig%)
  s1$ = ""
  if n%>1 then s1$ = string$((niv%-1)*4," ")
  s$ = s1$ + "[-] "+mid$(s$,niv%*4+1,len(s$))
  item_delete o,lig%
  item_insert o,lig%,s$
  CalculateLineLevel(s$)
  CalculateTreeViewLevelVector(o,lig%,s$)
  FindTreeViewSource(o)
  i1% = lig%
  i2% = TVO_sources%(1)
  while i2%<count(o+1)
    i2% = i2% + 1
    s$ = item_read$(o+1,i2%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      i1% = i1% + 1
      item_insert o,i1%,s$
    else
      TVO_index% = 0
      exit_sub
    end_if
  end_while
  TVO_index% = 0
end_sub

sub FoldTreeViewNode(o,lig%,niv%)
  dim_local s$, s1$, i%
  s$ = item_read$(o,lig%)
  s1$ = ""
  if niv%>1 then s1$ = string$((niv%-1)*4," ")
  s$ = s1$ + "[+] "+mid$(s$,niv%*4+1,len(s$))
  item_delete o,lig%
  item_insert o,lig%,s$
  i% = lig%
  while i%<count(o)
    i% = i% + 1
    s$ = item_read$(o,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o,i%
      i% = i% - 1
    else
      TVO_index% = 0
      exit_sub
    end_if
    TVO_index% = 0
  end_while

end_sub

  ' determiner le niveau d'une ligne dans la section
sub CalculateLineLevel(s$)
  dim_local i%
  i% = 1
  while mid$(s$,i%,1)=" "
    i% = i% + 1
  end_while
  if mid$(s$,i%,1)<>"[" then i% = i% - 4
  i% = i% - 1
  TVO_n1% = i%/4 + 1
end_sub

' déterminer le vecteur des niveaux imbriqués
sub CalculateTreeViewLevelVector(o,ind%,r$)
  dim_local s$
  s$ = r$
  TVO_levels%(0) = 0
  i% = ind%
  CalculateLineLevel(s$)

  while TVO_n1%>1
    ldet% = 0
    n2% = TVO_n1%
    if n2%>1
      TVO_indexes%(TVO_levels%(0)+1) = i%
      while TVO_n1%>=n2%
        if TVO_n1%=n2% then ldet% = ldet% + 1
        i% = i% - 1
        s$ = item_read$(o,i%)
        CalculateLineLevel(s$)
      end_while
      TVO_levels%(0) = TVO_levels%(0) + 1
      TVO_levels%(TVO_levels%(0)) = ldet%
    end_if
  end_while
  TVO_levels%(0) = TVO_levels%(0) + 1
  TVO_levels%(TVO_levels%(0)) = 1
  TVO_indexes%(TVO_levels%(0)) = 1
end_sub

sub CreateTreeViewObject(no%,t%,l%,w%,h%)
  if (object_exists(no%)+object_exists(no%+1))>0
    message "TreeView - error 1"
    exit_sub
  end_if
  if variable("TVO")=0
    dim TVO, TVO_n1%, TVO_levels%(50), TVO_indexes%(50), TVO_sources%(50)
    dim TVO_index%
  end_if

  TVO = no%
  list TVO : top TVO,t% : left TVO,l%
    width TVO,w% : height TVO,h%
    font_name TVO,"Courier"
  if montrer%=0
    dlist TVO+1
  else
    list TVO+1 : top TVO+1,t% : left TVO+1,l%+w%+10
      width TVO+1,w% : height TVO+1,h%
      font_name TVO+1,"Courier"
  end_if
end_sub

sub DeleteTreeViewObject(n%)
  if variable("TVO")=1
    delete n%
    delete n%+1
    free TVO
    free TVO_n1%
    free TVO_levels%
    free TVO_indexes%
    free TVO_sources%
    free TVO_index%
  end_if
end_sub

sub CreateTreeViewRoot(o,txt$)
  if count(o)>0
    message "TreeView - error 2"
    exit_sub
  end_if
  item_add o,"    "+txt$
  item_add o+1,"    "+txt$
end_sub

sub AddTreeViewChildByIndex(o,ind%,txt$)
  if variable("AddTreeViewChildByIndex")=0 then dim AddTreeViewChildByIndex
  dim_local a$, index%, n2%
  if ind%>count(o)
    message "TreeView - error 3"
    exit_sub
  end_if
  i% = ind%
  s$ = item_read$(o,i%)
  CalculateLineLevel(s$)
  n2% = TVO_n1%
  CalculateTreeViewLevelVector(o,ind%,s$)
  FindTreeViewSource(o)
  FindTreeViewEndLevel(o,n2%)
  index% = TVO_indexes%(1)
  s$ = item_read$(o,index%)
  a$ = mid$(s$,(n2%-1)*4+1,3)
  CalculateLineLevel(s$)

  if a$="[+]"
    index% = TVO_sources%(1)
    if a$=sp3$ and (TVO_n1%=n2%)
      s$ = item_read$(o+1,index%)
      item_delete o+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o+1,index%+1,string$(n2%*4," ")+"    "+txt$
    AddTreeViewChildByIndex = index%+1
    TVO_index% = 0
  end_if
  if (a$=sp3$) or (a$="[-]")
    if (a$=sp3$) and (TVO_n1%=n2%)
      s$ = item_read$(o,index%)
      item_delete o,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o,index%+1,string$(n2%*4," ")+"    "+txt$
    AddTreeViewChildByIndex = index%+1
    index% = TVO_sources%(1)
    if a$=sp3$ and (TVO_n1%=n2%)
      s$ = item_read$(o+1,index%)
      item_delete o+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert o+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert o+1,index%+1,string$(n2%*4," ")+"    "+txt$
  end_if
  TVO_index% = 0
end_sub

sub AddTreeViewSisterByIndex(o,ind%,off%,txt$)
  if variable("AddTreeViewSisterByIndex")=0 then dim AddTreeViewSisterByIndex
  dim_local a$, index%, n2%
  if ind%>count(o)
    message "TreeView - error 3"
    exit_sub
  end_if
  if (ind%=1) and (off%=0)
    message "TreeView - error 3"
    exit_sub
  end_if
  i% = ind%
  s$ = item_read$(o,i%)
  CalculateLineLevel(s$)
  n2% = TVO_n1%
  CalculateTreeViewLevelVector(o,ind%,s$)
  FindTreeViewSource(o)
  if off%=1 then FindTreeViewEndLevel(o,n2%)
  index% = TVO_indexes%(1)
  s$ = item_read$(o,index%)
  a$ = mid$(s$,(n2%-1)*4+1,3)
  CalculateLineLevel(s$)
  item_insert o,index%+off%,string$((n2%-1)*4," ")+"    "+txt$
  AddTreeViewSisterByIndex = index%+1
  index% = TVO_sources%(1)
  TVO_index% = 0
end_sub


sub FindTreeViewSource(o)
  dim_local k%, n%, s$
  sind% = 1
  s$ = item_read$(o+1,sind%)
  CalculateLineLevel(s$)
  for k%=TVO_levels%(0) to 1 step -1
    n% = TVO_levels%(k%)
    while (n%>1)
      sind% = sind% + 1
      if sind%>count(o+1)
        TVO_sources%(k%) = sind%
        exit_sub
      end_if
      s$ = item_read$(o+1,sind%)
      CalculateLineLevel(s$)
      if TVO_n1%=(TVO_levels%(0)+1-k%) then n% = n% - 1
    end_while
    TVO_sources%(k%) = sind%
    sind% = sind% + 1
  next k%
end_sub

sub FindTreeViewEndLevel(o,nx%)
  dim_local s$, k%, l%
  k% = TVO_sources%(1)
  while k%<count(o+1)
    k% = k% + 1
    l% = l% + 1
    s$ = item_read$(o+1,k%)
    CalculateLineLevel(s$)
    if TVO_n1%<=nx%
      TVO_sources%(1) = k% - 1
      if instr(item_read$(o,TVO_indexes%(1)),"[+]")=0
        TVO_levels%(1) = TVO_levels%(1) + l% - 1
        TVO_indexes%(1) = TVO_indexes%(1) + l% - 1
      end_if
      exit_sub
    end_if
  end_while
  TVO_sources%(1) = k%
  TVO_levels%(1) = TVO_levels%(1) + l%
  TVO_indexes%(1) = TVO_indexes%(1) + l%
end_sub

sub DeleteTreeViewNode(o,lig%)
  dim_local s$, s1$, i%, niv%
  s$ = item_read$(o,lig%)
  CalculateLineLevel(s$)
  niv% = TVO_n1%
  CalculateTreeViewLevelVector(o,lig%,s$)
  FindTreeViewSource(o)
  s1$ = ""
  i% = lig%
  while i%<count(o)
    i% = i% + 1
    s$ = item_read$(o,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o,i%
      i% = i% - 1
    else
      TVO_index% = 0
'      item_delete o,lig%
'      exit_sub
    end_if
    TVO_index% = 0
  end_while
  if lig%>1
    item_delete o,lig%
  else
    s$ = item_read$(o,lig%)
    s$ = string$(4," ")+mid$(s$,5,len(s$))
    item_delete o,lig%
    item_insert o,lig%,s$
  end_if

  i% = TVO_sources%(1)
  while i%<count(o+1)
    i% = i% + 1
    s$ = item_read$(o+1,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete o+1,i%
      i% = i% - 1
    else
'      TVO_index% = 0
'      item_delete o+1,lig%
'      exit_sub
    end_if
'    TVO_index% = 0
  end_while
  lig% = TVO_sources%(1)
  if lig%>1
    item_delete o+1,lig%
  else
    s$ = item_read$(o+1,lig%)
    s$ = string$(4," ")+mid$(s$,5,len(s$))
    item_delete o+1,lig%
    item_insert o+1,lig%,s$
  end_if
end_sub

sub dump(t$)
  message t$+": niveaux: "+str$(TVO_levels%(0))+","+str$(TVO_levels%(1))+","+str$(TVO_levels%(2))+","+str$(TVO_levels%(3))+","+str$(TVO_levels%(4))
  message t$+": indices: "+str$(TVO_indexes%(0))+","+str$(TVO_indexes%(1))+","+str$(TVO_indexes%(2))+","+str$(TVO_indexes%(3))+","+str$(TVO_indexes%(4))
  message t$+": sources: "+str$(TVO_sources%(0))+","+str$(TVO_sources%(1))+","+str$(TVO_sources%(2))+","+str$(TVO_sources%(3))+","+str$(TVO_sources%(4))
end_sub

fin:
terminate

A+
Revenir en haut Aller en bas
Klaus

Klaus


Nombre de messages : 12301
Age : 75
Localisation : Ile de France
Date d'inscription : 29/12/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptyVen 7 Déc 2012 - 11:05

Bonne idée, Jean-Claude. Je l'ai intégré.

En même temps, j'ai fini l'adaptation pour pouvoir l'utiliser par #INCLUDE. Il y a donc maintenant 2 modules:
- treeview.bas = programme de démo
' TVO_SUB.bas = module INCLUDE à placer dans le même dossier

treeview.bas:
Code:
' treeview.bas

labels()
constantes()
variables()
form0()
GUI()
initialisations()
end

fin:
  terminate




sub labels()
  label fin
  label creerfille, creeravant, creerapres, supprimer
  label supprimerTVO, nouveautest, explorateur
end_sub

sub constantes()
  ' la constante suivante détermine la visibilité de la liste interne.
  ' si cette constante est absente ou a la valeur 0, la liste interne est invisible.
  ' si cette constante est présente et a la valeur 1, la liste interne esr visible
  dim montrer%  : montrer% = 1
end_sub

sub variables()
  ' variables du programme de base
  dim no%, no_TVO%, nadd%
  ' variables pour constituer la démo initiale
  dim niveau1%, niveau2%, niveau3%
  ' variables pour le bouton "Répertoire"
  dim i%, s$, f$, d$, k%, d1$, f1$, nodir%
end_sub

sub form0()
  width 0,1000
  caption 0,"Test d'un objet TreeView
  on_close 0,fin
end_sub

sub GUI()
  no% = no% + 1 : button no% : top no%,10 : left no%,830 : width no%,130
    caption no%,"Créer noeud fille" : on_click no%,creerfille
  no% = no% + 1 : button no% : top no%,40 : left no%,830 : width no%,130
    caption no%,"Créer noeud soeur ainée" : on_click no%,creeravant
  no% = no% + 1 : button no% : top no%,70 : left no%,830 : width no%,130
    caption no%,"Créer noeud soeur cadette" : on_click no%,creerapres
  no% = no% + 1 : button no% : top no%,100 : left no%,830 : width no%,130
    caption no%,"Supprimer noeud" : on_click no%,supprimer

  no% = no% + 1 : button no% : top no%,150 : left no%,830 : width no%,130
    caption no%,"Supprimer TreeView" : on_click no%,supprimerTVO
  no% = no% + 1 : button no% : top no%,180 : left no%,830 : width no%,130
    caption no%,"Nouveau test" : on_click no%,nouveautest
  no% = no% + 1 : button no% : top no%,210 : left no%,830 : width no%,130
    caption no%,"Explorateur" : on_click no%,explorateur
end_sub

sub initialisations()
  no% = no% + 1 : no_TVO% = no%
  creertest()
end_sub

sub creertest()
  CreateTreeViewObject(no_TVO%,10,10,300,300)
  CreateTreeViewRoot(TVO,"Racine")
  niveau1% = 1

  AddTreeViewChildByIndex(TVO,niveau1%,"aaaa")
  niveau2% = AddTreeViewChildByIndex

  AddTreeViewChildByIndex(TVO,niveau2%,"a1111111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"a2222222222")
  AddTreeViewChildByIndex(TVO,niveau1%,"bbbb")
  AddTreeViewChildByIndex(TVO,niveau1%,"cccc")
  niveau2% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau2%,"11111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"22222222")
  niveau3% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau3%,"2aaaaaaaaa")
  AddTreeViewChildByIndex(TVO,niveau3%,"2bbbbbbbbb")
  AddTreeViewChildByIndex(TVO,niveau2%,"33333333")
  AddTreeViewChildByIndex(TVO,niveau2%,"44444444")
  niveau3% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau3%,"4aaaaaaaaa")
  AddTreeViewChildByIndex(TVO,niveau3%,"4bbbbbbbbb")
  AddTreeViewChildByIndex(TVO,niveau1%,"dddd")
  niveau2% = AddTreeViewChildByIndex
  AddTreeViewChildByIndex(TVO,niveau2%,"d11111111")
  AddTreeViewChildByIndex(TVO,niveau2%,"d22222222")
end_sub

creerfille:
  if TVO_index%=0
    message "Il faut choisir la ligne parent"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewChildByIndex(TVO,TVO_index%,"xxx "+str$(nadd%)+" xxx")
  return

creeravant:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewSisterByIndex(TVO,TVO_index%,0,"xxx "+str$(nadd%)+" xxx")
  return

creerapres:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  nadd% = nadd% + 1
  AddTreeViewSisterByIndex(TVO,TVO_index%,1,"xxx "+str$(nadd%)+" xxx")
  return

supprimer:
  if TVO_index%=0
    message "Il fait choisir la ligne cible"
    return
  end_if
  DeleteTreeViewNode(no_TVO%,item_index(no_TVO%))
  return

supprimerTVO:
  DeleteTreeViewObject(no_TVO%)
  return

nouveautest:
  DeleteTreeViewObject(no_TVO%)
  creertest()
  return

explorateur:
  montrer% = 0
  DeleteTreeViewObject(no_TVO%)
  CreateTreeViewObject(no_TVO%,10,10,800,300)
  d$ = dir_current$
  f$ = f$
  i% = instr("\",f$)
  while i%>0
    f$ = mid$(f$,i%+1,len(f$))
    i% = instr("\",f$)
  end_while
  CreateTreeViewRoot(TVO,d$)
  d$ = d$ + "\"
  message "Attendre le message "+chr$(34)+"Chargement terminé."+chr$(34)
  niveau1% = 1
  nodir% = 1
  f$ = file_find_first$
  while f$<>"_"
    if dir_exists(d$+f$)=1
      AddTreeViewChildByIndex(TVO,niveau1%,f$+"²"+d$+"²")
      nodir% = 0
    else
      AddTreeViewChildByIndex(TVO,niveau1%,f$)
    end_if
    f$ = file_find_next$
  end_while
  file_find_close

  while nodir%=0
    nodir% = 1
    k% = 2
    while k%<=count(no_TVO%)
      s$ = item_read$(no_TVO%,k%)
      if right$(s$,1)="²"
        i% = instr(s$,"²")
        f$ = trim$(left$(s$,i%-1))
        d1$ = trim$(mid$(s$,i%+1,len(s$)))
        d1$ = left$(d1$,len(d1$)-1)+f$+"\"
        DeleteTreeViewNode(no_TVO%,k%)
        AddTreeViewSisterByIndex(TVO,k%,0,f$)
        dir_change d1$
        niveau2% = k%
        f1$ = file_find_first$
        while f1$<>"_"
          if dir_exists(d1$+f1$)=1
            AddTreeViewChildByIndex(TVO,niveau2%,f1$+"²"+d1$+"²")
            nodir% = 0
          else
            AddTreeViewChildByIndex(TVO,niveau2%,f1$)
          end_if
          k% = k% + 1
          f1$ = file_find_next$
        end_while
        file_find_close

      end_if
      k% = k% + 1
    end_while
  end_while
  message "Chargement terminé."

  return

' ===== procédure obligatoire pour recevoir une sélection de ligne
sub user_select_line(lig%,niv%)
  ' lig% est le numéro de ligne cliquée (1,2,3,...)
  ' niv% est le niveau de la ligne (1,2,3,...)
  ' on récupère le texte de la ligne cliquée par
  ' s$ = item_read$(n,lig%)
  ' avec n étant le numéro de l'objet passé en premier paramètre à la procédure  message "Ligne "+str$(lig%)+" niveau "+str$(niv%)
end_sub

#INCLUDE "TVO_SUB.bas"

TVO_SUB.bas:
Code:
' TVO_SUB.bas

' Ce module implémente la gestion d'un objet TVO (Tree View Object).
' Il suffit d'inclure ce module par #INCLUDE "TVO_SUB.bas" à la fin du source
' pour pouvoir utiliser les fonctions.
'
' Un objet TVO est composé de deux objets Panoramic:
'  - un LIST contenant la représentation visible
'  - un DLIST contenant la représentation complète entièrement dépliée
' Une ligne est accédée par son numéro (indice). Sa position dans l'arborescence
' est déterminée par son "vecteur" qui est composé des valeurs suivantes:
'  - le nombre de niveaux (la racine est le niveau 1)
'  - pour chaque niveau, le numéro de la ligne dans ce niveau (1,2,...) de la LIST

message "TVO: ce module ne peut pas être exécuté directement !"
terminate

' ===== routine évènement ON_CLICK du TVO
'  positionnement des [+]
'  niveau 1:  5...22 ==> 17
'  niveau 2: 37...54 ==> 17
'  niveau 3: 69...86 ==> 17
'  niveau n:  a...b  n=int((x-5+32)/32)  a = (n-1)*32+5  b=a+17
TVO_click:
  TVO_index% = item_index(number_click)    : ' mémoriser la ligne active
  TVO_x% = mouse_x_left_down(number_click)  : ' positionnement jorizontal du clic
  TVO_n% = int((TVO_x%-5+32)/32)            : ' multiple de 32 pixels avec offset de 5 pixels
  TVO_s$ = item_index$(number_click)        : ' la ligne cliquée
  CalculateLineLevel(TVO_s$)                : ' n1 = niveau d'arborescence
  CalculateTreeViewLevelVector(number_click,item_index(number_click),TVO_s$)
  TVO_n1% = TVO_levels%(0)                  : ' le niveau d'arborescence
  TVO_s$ = item_index$(number_click)        : ' lire la ligne choisie
  TVO_flag% = 0                            : ' pas d'action par défaut
  if TVO_n%=TVO_n1%                        : ' si clic dans [x]:
    if mid$(TVO_s$,(TVO_n%-1)*4+2,1)="+" then TVO_flag% = 1  : ' action "déplier" si +
    if mid$(TVO_s$,(TVO_n%-1)*4+2,1)="-" then TVO_flag% = 2  : ' action "replier" si -
  end_if
  select TVO_flag%
    case 0 : ' on a sélectionné une ligne
      user_select_line(item_index(number_click),TVO_n1%)
    case 1 : ' on veut déplier une ligne ayant des lignes enfant
      UnfoldTreeViewNode(number_click,item_index(number_click),TVO_n%)
    case 2 : ' on veut replier une ligne pour escamoter ses enfants
      FoldTreeViewNode(number_click,item_index(number_click),TVO_n%)
  end_select
  return

' ===== déplier un noeud
'  on recopie les lignes nécessaires à partir la DLIST
sub UnfoldTreeViewNode(obj,lig%,niv%)
  dim_local s$, s1$, i1%, i2%, n%
  s$ = item_read$(obj,lig%)                    : ' relire le noeud
  s1$ = ""
  n% = niv%
  if n%>1 then s1$ = string$((niv%-1)*4," ")  : ' construire le préfixe
  s$ = s1$ + "[-] "+mid$(s$,niv%*4+1,len(s$))  : ' et combiner avec le noeud
  item_delete obj,lig%
  item_insert obj,lig%,s$                      : ' remplacer la ligne du noeud
  CalculateLineLevel(s$)                      : ' déterminer le niveau de la ligne
  CalculateTreeViewLevelVector(obj,lig%,s$)    : ' déterminer le vecteur
  FindTreeViewSource(obj)                      : ' déterminer les adresses dans la DLIST
  i1% = lig%
  i2% = TVO_sources%(1)
  while i2%<count(obj+1)                      : ' recopier tout ce qui est
    i2% = i2% + 1                              : '  en-dessous du noeud ciblé
    s$ = item_read$(obj+1,i2%)                  : ' lire une ligne
    CalculateLineLevel(s$)                      : ' déterminer son niveau
    if TVO_n1%>niv%                              : ' son niveau est plus grand ?
      i1% = i1% + 1                                : ' alors recopier cette ligne
      item_insert obj,i1%,s$
  else                                        : ' sinon
      TVO_index% = 0                              : ' désélectionner le noeud
      exit_sub                                    : ' et terminer
    end_if
  end_while
  TVO_index% = 0                              : ' désélectionnerle noeud
end_sub

' ===== replier un noeud
'  on supprime toutes les lignes suivant le noeud
'  ayant un niveau supérieur à celui du noeud visé
sub FoldTreeViewNode(obj,lig%,niv%)
  dim_local s$, s1$, i%
  s$ = item_read$(obj,lig%)                    : ' relire le noeud
  s1$ = ""
  if niv%>1 then s1$ = string$((niv%-1)*4," ") : ' construire le préfixe
  s$ = s1$ + "[+] "+mid$(s$,niv%*4+1,len(s$))  : ' et combiner avec le noeud
  item_delete obj,lig%
  item_insert obj,lig%,s$                      : ' remplacer la ligne du noeud
  i% = lig%
  while i%<count(obj)                          : ' supprimer tout ce qui est
    i% = i% + 1                                : '  en-dessous du noeud
    s$ = item_read$(obj,i%)                      : ' lire une ligne
    CalculateLineLevel(s$)                      : ' déterminer son niveau
    if TVO_n1%>niv%                              : ' son niveau est plus grand ?
      item_delete obj,i%                            : ' supprimer cette ligne
      i% = i% - 1
    else                                        : ' sinon
      TVO_index% = 0                              : ' désélectionner le noeud
      exit_sub                                    : ' et terminer
    end_if
    TVO_index% = 0                              : ' désélectionner le noeud
  end_while
end_sub

' ===== determiner le niveau d'une ligne dans la section
'  Le niveau d'une ligne est déterminé à partir de la position
'  du premier caractère non-blanc d'une ligne, donc à partir
'  de la longueur de son préfixe. Ce dernier est composé d'un
'  multiple de cases de 4 caractères, dont la dernière peut
'  contenir ma marque de noeud: "[+] " ou [-] "
sub CalculateLineLevel(s$)
  dim_local i%
  i% = 1
  while mid$(s$,i%,1)=" "                  : ' chercher le premier caractère
    i% = i% + 1                            : '  non-blanc
  end_while
  if mid$(s$,i%,1)<>"[" then i% = i% - 4  : ' ignorer une dernière case vide
  i% = i% - 1                              : ' i% est la longueur du préfixe
  TVO_n1% = i%/4 + 1                      : ' déterminer le niveau
end_sub

' ===== déterminer le vecteur des niveaux imbriqués
'  on remplit 2 tableaux:
'    TVO_indexes%()  liste des adresses des niveaux dans la LIST
'    TVO_levels%()  liste des positions des noeuds dans le niveau correspondant
sub CalculateTreeViewLevelVector(obj,ind%,r$)
  dim_local s$, n2%, i%, ldet%
  s$ = r$
  TVO_levels%(0) = 0                            : ' initialiser le nombre des niveaux
  i% = ind%
  CalculateLineLevel(s$)                        : ' calculer le niveau de la ligne ciblée

  while TVO_n1%>1                              : ' parcourir la LIST jusqu'à la racine
    ldet% = 0                                    : ' initialiser le compteur de ligne dans le niveau
    n2% = TVO_n1%                                : ' mémoriser le niveau de la ligne
    if n2%>1                                      : ' ce niveau est > 1 ?
      TVO_indexes%(TVO_levels%(0)+1) = i%          : ' mémoriser la position de la ligne
      while TVO_n1%>=n2%                            : ' analyser en arrière jusqu'au début du niveau
        if TVO_n1%=n2% then ldet% = ldet% + 1        : ' compter la ligne en cours
        i% = i% - 1                                  : ' reculer d'une ligne
        s$ = item_read$(obj,i%)                      : ' lire cette ligne
        CalculateLineLevel(s$)                        : ' et déterminer son niveau
      end_while
      TVO_levels%(0) = TVO_levels%(0) + 1          : ' compter le niveau
      TVO_levels%(TVO_levels%(0)) = ldet%          : ' mémoriser la position dans le niveau
    end_if
  end_while                                    : ' fin de boucle de recherche
  TVO_levels%(0) = TVO_levels%(0) + 1          : ' mémoriser le niveau racine
  TVO_levels%(TVO_levels%(0)) = 1
  TVO_indexes%(TVO_levels%(0)) = 1
end_sub

' ===== création initiale d'un objet TVO vide
'  Cette fonction crée la LIST et la DLIST.
'  Si une variable globale montrer% est définie, et si sa valeur est 1,
'  la DLIST sera remplacée par une LIST identique à la LIST de base
'  et placé à gauche de celle-ci, de sorte à visualiser l'image interne.
'  Cette possibilité est utile en phase de mise au point d'une application.
sub CreateTreeViewObject(no%,t%,l%,w%,h%)
  if (object_exists(no%)+object_exists(no%+1))>0
    message "TreeView - error 1"    : ' les numéros d'objet sont déjà utilisés
    exit_sub
  end_if                            : ' définir les variables et labels internes
  if variable("TVO")=0
    dim TVO, TVO_n1%, TVO_levels%(50), TVO_indexes%(50), TVO_sources%(50)
    dim TVO_index%, TVO_flag%, TVO_x%, TVO_n%, TVO_s$
    if label("TVO_click")=0 then label TVO_click
  end_if

  TVO = no%
  list TVO : top TVO,t% : left TVO,l%    : ' créer la LIST de la partie visible
    width TVO,w% : height TVO,h%
    font_name TVO,"Courier"
    on_click TVO,TVO_click
  if variable("montrer%")=1              : ' la variable montrer% existe ?
    if montrer%=0                          : ' sa valeur est 0 ?
      dlist TVO+1                            : ' DLIST de la représentatioin interne
    else                                    : ' sa valeur est <> 0 ?
      list TVO+1 : top TVO+1,t% : left TVO+1,l%+w%+10  : ' représentation interne visible
        width TVO+1,w% : height TVO+1,h%
        font_name TVO+1,"Courier"
    end_if
  else                                    : ' la variable montrer% n'existe pas ?
      dlist TVO+1                          : ' DLIST de la représentatioin interne
  end_if
end_sub

' ===== supprimer un objet TVO
'  Cette fonction supprime les deux objets Panoramic du TVO: LIST et DLIST,
'  ainsi que toutes les variables internes
sub DeleteTreeViewObject(n%)
  if variable("TVO")=1      : ' exécuter seulement si un objet TVO existe !
    delete n%
    delete n%+1
    free TVO
    free TVO_n1%
    free TVO_levels%
    free TVO_indexes%
    free TVO_sources%
    free TVO_index%
    free TVO_flag%
    free TVO_x%
    free TVO_n%
    free TVO_s$
  end_if
end_sub

' ===== créer la racine d'un objet TVO
sub CreateTreeViewRoot(obj,txt$)
  if count(obj)>0
    message "TreeView - error 2"  : ' la racine existe déjà
    exit_sub
  end_if
  item_add obj,"    "+txt$
  item_add obj+1,"    "+txt$
end_sub

' ===== attacher une ligne fille à une ligne existante
'  La ligne parent est ciblée par le paramètre ind%.
'  Si cette ligne est une ligne simple, elle sera transformée en ligne noeud
'  Si cette ligne est déjà un noeud, on cherchera la dernière ligne fille
'  de ce noeud (au niveau immédiatement supérieur à celui de la ligne parent)
'  et l'insertion aura lieu après cette ligne.
sub AddTreeViewChildByIndex(obj,ind%,txt$)
  if variable("AddTreeViewChildByIndex")=0 then dim AddTreeViewChildByIndex
  dim_local a$, index%, n2%, s$, s1$, i%
  dim_local sp3$      : sp3$ = string$(3," ")
  if (ind%>count(obj)) or (count(obj)=0)
    message "TreeView - error 3"
    exit_sub
  end_if
  i% = ind%
  s$ = item_read$(obj,i%)                  : ' lire la ligne parent
  CalculateLineLevel(s$)                    : ' déterminer son niveau
  n2% = TVO_n1%                            : ' et le mémoriser
  CalculateTreeViewLevelVector(obj,ind%,s$) : ' calculer le vecteur
  FindTreeViewSource(obj)                  : ' et déterminer les adresses dans la DLIST
  FindTreeViewEndLevel(obj,n2%)            : ' chercher la fin du niveau fille
  index% = TVO_indexes%(1)
  s$ = item_read$(obj,index%)              : ' relire la ligne parent
  a$ = mid$(s$,(n2%-1)*4+1,3)              : ' extraire son statut de noeud
  CalculateLineLevel(s$)

  if a$="[+]"                              : ' c'est un noeud replié ?
    index% = TVO_sources%(1)                  : ' alors insérer uniquement dans la DLIST
    if a$=sp3$ and (TVO_n1%=n2%)              : ' cas improbable...
      s$ = item_read$(obj+1,index%)
      item_delete obj+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert obj+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert obj+1,index%+1,string$(n2%*4," ")+"    "+txt$  : ' insérer dans la DLIST
    AddTreeViewChildByIndex = index%+1
    TVO_index% = 0
  end_if
  if (a$=sp3$) or (a$="[-]")                : ' c'est une ligne simple ou un noeud déplié ?
    if (a$=sp3$) and (TVO_n1%=n2%)            : ' c'est une ligne simple ?
      s$ = item_read$(obj,index%)              : ' alors transformer en noeud déplié
      item_delete obj,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert obj,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert obj,index%+1,string$(n2%*4," ")+"    "+txt$ : ' insérer dans la LIST
    AddTreeViewChildByIndex = index%+1
    index% = TVO_sources%(1)                  : ' traiter également la DLIST
    if a$=sp3$ and (TVO_n1%=n2%)                : ' c'est une ligne simple ?
      s$ = item_read$(obj+1,index%)              : ' alors transformer en noeud déplié
      item_delete obj+1,index%
      s1$ = ""
      if n2%>1 then s1$ = string$((n2%-1)*4," ")
      item_insert obj+1,index%,s1$+"[-] "+trim$(s$)
    end_if
    item_insert obj+1,index%+1,string$(n2%*4," ")+"    "+txt$  : ' insérer dans la DLIST
  end_if
  TVO_index% = 0
end_sub

' ===== insérer une ligne soeur
'  Cette fonction insère une ligne dans le même niveau que la ligne ciblée.
'  On appelle une soeur "ainée" une ligne qui se place au-dessus de la ligne ciblée.
'  On appelle une soeur "cadette" une ligne qui se place en-dessous de la ligne ciblée.
'  Le statut de la ligne ciblée n'est pas changé.
'  off%=0 ==> soeur ainée  off%=1 ==> soeur cadette
sub AddTreeViewSisterByIndex(obj,ind%,off%,txt$)
  if variable("AddTreeViewSisterByIndex")=0 then dim AddTreeViewSisterByIndex
  dim_local a$, s$, index%, n2%, i%
  if (ind%>count(obj)) or (count(obj)=0)
    message "TreeView - error 3"
    exit_sub
  end_if
  if ind%=1
    message "TreeView - error 3"
    exit_sub
  end_if
  i% = ind%
  s$ = item_read$(obj,i%)                      : ' lire la ligne ciblée
  CalculateLineLevel(s$)                      : ' calculer son niveau
  n2% = TVO_n1%                                : ' et le mémoriser
  CalculateTreeViewLevelVector(obj,ind%,s$)    : ' calculer son vecteur
  FindTreeViewSource(obj)                      : ' et les adresses dans la DLIST
  if off%=1 then FindTreeViewEndLevel(obj,n2%) : ' fille cadette: aller à la fin du niveau
  index% = TVO_indexes%(1)
  s$ = item_read$(obj,index%)                  : ' reprendre la ligne ciblée
  a$ = mid$(s$,(n2%-1)*4+1,3)
  CalculateLineLevel(s$)
  item_insert obj,index%+off%,string$((n2%-1)*4," ")+"    "+txt$ : ' insérer la ligne soeur
  AddTreeViewSisterByIndex = index%+1
  index% = TVO_sources%(1)
  TVO_index% = 0
end_sub

' ===== déterminer les adresses des sources par rapport au vecteur
'  Cette fonction utilise le vecteur basé sur la LIST pour déterminer
'  les adresses de chaque niveau dans la DLIST
sub FindTreeViewSource(obj)
  dim_local k%, n%, s$, sind%
  sind% = 1                              : ' commencer à la racine de la DLIST
  s$ = item_read$(obj+1,sind%)          : ' lire la racine '
  CalculateLineLevel(s$)                : ' déterminer son niveau (doit être 1)
  for k%=TVO_levels%(0) to 1 step -1    : ' parcourir le vecteur de la fin au début
    n% = TVO_levels%(k%)                  : ' prendre le numéro de ligne dans le niveau
    while (n%>1)                          : ' parcourir ce niveau jusqu'au nombre indiqué
      sind% = sind% + 1                      : ' pointer la ligne suivante
      if sind%>count(obj+1)                  : ' fin de liste ?
        TVO_sources%(k%) = sind%              : ' alors mémoriser cette adresse
        exit_sub                              : ' et terminer
      end_if
      s$ = item_read$(obj+1,sind%)          : ' lire cette ligne
      CalculateLineLevel(s$)                : ' et déterminer son niveau
      if TVO_n1%=(TVO_levels%(0)+1-k%) then n% = n% - 1 : ' décompter si c'est le bon niveau
    end_while                              : ' fin de lecture du niveau
    TVO_sources%(k%) = sind%              : ' mémoriser cette adresse
    sind% = sind% + 1                      : ' passer à la ligne suivante
  next k%                                : ' fin des niveaux
end_sub

' ===== chercher la fin d'un niveau dans les sources
'  Cette fonction cherche la fin du niveau de la ligne ciblée, à partir
'  des adresses des sources dans la DLIST établies par FindTreeViewSource()
sub FindTreeViewEndLevel(obj,nx%)
  dim_local s$, k%, l%
  k% = TVO_sources%(1)                      : ' prendre l'adresse de la lugne ciblée
  while k%<count(obj+1)                      : ' parcourir la suite de la DLIST
    k% = k% + 1                                : ' pointer la ligne suivante
    l% = l% + 1                                : ' compter le décalage
    s$ = item_read$(obj+1,k%)                  : ' lire la ligne
    CalculateLineLevel(s$)                    : ' et valculer son niveau
    if TVO_n1%<=nx%                            : ' on a quitté le niveau ?
      TVO_sources%(1) = k% - 1                  : ' alors mémoriser cette adresse
      if instr(item_read$(obj,TVO_indexes%(1)),"[+]")=0  : ' la ligne ciblée est un noeud replié ?
        TVO_levels%(1) = TVO_levels%(1) + l% - 1    : ' alors maj du vecteur
        TVO_indexes%(1) = TVO_indexes%(1) + l% - 1
      end_if
      exit_sub                                  : ' et quitter
    end_if
  end_while                                    : ' pas fin de niveau ? continuer à chercher...
  TVO_sources%(1) = k%                      : ' mémoriser cette adresse
  TVO_levels%(1) = TVO_levels%(1) + l%      : ' et mettre à jour le vecteur
  TVO_indexes%(1) = TVO_indexes%(1) + l%
end_sub

' ===== supprimer une ligne ou un noeud
'  Cette fonction supprime la ligne ciblée dans la LIST et la DLIST.
'  Si la ligne ciblée est un noeud, toutes les lignes appartenant à ce noeud sont supprimées.
sub DeleteTreeViewNode(obj,lig%)
  dim_local s$, s1$, i%, niv%
  if (lig%>count(obj)) or (count(obj)=0)
    message "TreeView - error 3"
    exit_sub
  end_if
  s$ = item_read$(obj,lig%)                  : ' lire la ligne
  CalculateLineLevel(s$)                    : ' et déterminer son niveau
  niv% = TVO_n1%                            : ' mémoriser ce niveau
  CalculateTreeViewLevelVector(obj,lig%,s$)  : ' déterminer le vecteur
  FindTreeViewSource(obj)                    : ' et calduler les adresses dans la DLIST
  s1$ = ""
  i% = lig%                                  : ' pointer la ligne
  while i%<count(obj)                        : ' parcourir la LIST
    i% = i% + 1                                : ' pointer la ligne suivante
    s$ = item_read$(obj,i%)                    : ' lire la ligne
    CalculateLineLevel(s$)                    : ' calculer son niveau
    if TVO_n1%>niv%                            : ' niveau > niveau mémorisé ?
      item_delete obj,i%                        : ' supprimer la ligne
      i% = i% - 1                                : ' ajuster le pointeur
    else                                      : ' sinon
      TVO_index% = 0                            : ' désélectionner la ligne
    end_if
    TVO_index% = 0                            : ' désélectionner la lign
  end_while
  if lig%>1                                  : ' ce n'est pas la racine ?
    item_delete obj,lig%                      : ' alors supprimer la ligne ciblée
  else                                      : ' c'est la racine
    s$ = item_read$(obj,lig%)                  : ' alors reconstruire une racine en ligne simple
    s$ = string$(4," ")+mid$(s$,5,len(s$))
    item_delete obj,lig%
    item_insert obj,lig%,s$
  end_if

  i% = TVO_sources%(1)                      : ' et faire la même chose dans la DLIST
  while i%<count(obj+1)                      : ' en premant l'adresse de la source comme départ !
    i% = i% + 1
    s$ = item_read$(obj+1,i%)
    CalculateLineLevel(s$)
    if TVO_n1%>niv%
      item_delete obj+1,i%
      i% = i% - 1
    end_if
  end_while
  lig% = TVO_sources%(1)
  if lig%>1
    item_delete obj+1,lig%
  else
    s$ = item_read$(obj+1,lig%)
    s$ = string$(4," ")+mid$(s$,5,len(s$))
    item_delete obj+1,lig%
    item_insert obj+1,lig%,s$
  end_if
end_sub

sub dump(t$)
  message t$+": niveaux: "+str$(TVO_levels%(0))+","+str$(TVO_levels%(1))+","+str$(TVO_levels%(2))+","+str$(TVO_levels%(3))+","+str$(TVO_levels%(4))
  message t$+": indices: "+str$(TVO_indexes%(0))+","+str$(TVO_indexes%(1))+","+str$(TVO_indexes%(2))+","+str$(TVO_indexes%(3))+","+str$(TVO_indexes%(4))
  message t$+": sources: "+str$(TVO_sources%(0))+","+str$(TVO_sources%(1))+","+str$(TVO_sources%(2))+","+str$(TVO_sources%(3))+","+str$(TVO_sources%(4))
end_sub

Le programme principal DOIT contenir une procédure définie comme suit:
Code:
sub user_select_line(lig%,niv%)
  ' lig% est le numéro de ligne cliquée (1,2,3,...)
  ' niv% est le niveau de la ligne (1,2,3,...)
  ' on récupère le texte de la ligne cliquée par
  ' s$ = item_read$(n,lig%)
  ' avec n étant le numéro de l'objet passé en premier paramètre à la procédure CreateTreeViewObject
end_sub


Dernière édition par Klaus le Sam 8 Déc 2012 - 14:57, édité 4 fois
Revenir en haut Aller en bas
http://klauspanoramic.comxa.com/index.html
Klaus

Klaus


Nombre de messages : 12301
Age : 75
Localisation : Ile de France
Date d'inscription : 29/12/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptySam 8 Déc 2012 - 10:30

J'ai renouvelé les codes de mon post d'hier. Certaines parties du programme de démo étaient restées dans TVO_SUB.bas. J'ai donc mis à jour treeview.bas et TVO_SUB.bas. Maintenant, la séparation est "propre".

J'ai commencé à commenter TVO_SUB.bas de façon détaillée. Ceci sera poursuivi.

EDIT

Suite des commentaires. Et toutes les occurrences de la variable iinterne o ont été remplacées par obj - c'est plus lisible comme ça. Il s'agit de paramètres formels des procédures internes à TVO_SUB.bas.
Revenir en haut Aller en bas
http://klauspanoramic.comxa.com/index.html
Klaus

Klaus


Nombre de messages : 12301
Age : 75
Localisation : Ile de France
Date d'inscription : 29/12/2009

Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 EmptySam 8 Déc 2012 - 12:35

C'est terminé. Le module TVO_SUB.bas est complètement documenté.

EDIT

Correction d'un bug: blocage d'insertion de ligne soeur aînée ou cadette pour la racine. Ceci n'a pas de sens.
Revenir en haut Aller en bas
http://klauspanoramic.comxa.com/index.html
Contenu sponsorisé





Simulation d'un objet TreeView - Page 2 Empty
MessageSujet: Re: Simulation d'un objet TreeView   Simulation d'un objet TreeView - Page 2 Empty

Revenir en haut Aller en bas
 
Simulation d'un objet TreeView
Revenir en haut 
Page 2 sur 2Aller à la page : Précédent  1, 2
 Sujets similaires
-
» TreeView en Panoramic
» Simulation de la fonction IIF
» Détermine si oui ou non un objet est PARENT d'un autre objet
» Où j'en suis ...
» Automate cellulaire : simulation de feu de forêt

Permission de ce forum:Vous ne pouvez pas répondre aux sujets dans ce forum
FORUM DE DISCUSSION SUR LE LANGAGE PANORAMIC :: PANORAMIC :: Vos sources, vos utilitaires à partager-
Sauter vers: