FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Error en TreeItem ( tree inside xBrowse)
Posts: 1088
Joined: Fri Oct 07, 2005 03:33 PM
Error en TreeItem ( tree inside xBrowse)
Posted: Mon May 09, 2011 02:35 AM
Hello,

I have some problems with TreeItem class, I am working with tree inside xbrowse but I have problems deleting an element, when it isn't at end all work ok, but when I try to delete the last element there are an error in this part of code ( ttreeitem line 252)

Code (fw): Select all Collapse
   if ::oNext == nil .or. ::oNext:nLevel < ::nLevel
      oTree:oLast       := ::oPrev
   else
      ::oNext:oPrev     := ::oPrev
   endif


it is complety logical but the error is about oLast data, and it isn't defined in the class ttreeitem, this is defined in tlinklist class

The same problem for the first element and logic is the same

This is a simple adapted sample

Code (fw): Select all Collapse
#include "FiveWin.Ch"
#include "xbrowse.ch"

STATIC aData

//----------------------------------------------------------------------------//
#xtranslate bmp( <c> ) => ".\" + <c> + ".bmp"
//----------------------------------------------------------------------------//

function Main()

   CreateTestData()
   TreeBrowse()

return (0)

//----------------------------------------------------------------------------//
static function TreeBrowse

   local oDlg, oBrw, oFont, nn, oItem
   local aTot  := { 0, 0 }
   local aColors  := {  { CLR_WHITE,   CLR_GREEN         }, ;
                        { CLR_BLACK,   RGB(255,255,180)  }, ;
                        { CLR_BLACK,   CLR_WHITE         }  }

   DEFINE FONT oFont NAME "TAHOMA" SIZE 0,-12
   DEFINE DIALOG oDlg SIZE 600,500 PIXEL ;
      TITLE "Xbrowse Tree" FONT oFont

   @ 20,10 XBROWSE oBrw SIZE -10,-10 PIXEL OF oDlg ;
      CELL LINES NOBORDER FOOTERS FASTEDIT;
      ON DBLCLICK MSGINFO( oBrw:oTreeItem:nLevel ) 

   oBrw:SetTree( MakeTree( aTot ), { bmp( "open" ), bmp( "close" ), bmp( "go" ) } )

   ADD TO oBrw  PICTURE "999,999,999" HEADER "Sales"  DATA oBrw:oTreeItem:Cargo[ 1 ]
   ADD TO oBrw  PICTURE "999,999,999" HEADER "Margin" DATA oBrw:oTreeItem:Cargo[ 2 ]

   //oBrw:aCols[3]:bEditWhen   := {|| oBrw:oTreeItem:Cargo[3] == 2  }
   oBrw:aCols[3]:nEditType   := EDIT_GET
   oBrw:aCols[3]:bOnPostEdit := {|o,x| oBrw:oTreeItem:Cargo[2] := x }
   oBrw:aCols[3]:lAutoSave   := .T.

   oBrw:CreateFromCode()
   oBrw:OpenAll()

   oBrw:bPastEof := {|| oItem := oBrw:oLast   ,;
                        IF( oBrw:oTreeItem:Cargo[2] = 99,; 
            ( oItem := oItem:Add( 'AAAAA' ),;
              oItem:Cargo := {1,2,99} ), ) ,;
            oBrw:refresh() }

   @ 1, 40 BUTTON "ADD" OF oDlg SIZE 30,10 PIXEL ACTION ( IF( oBrw:oTreeItem:oTree != NIL,;
                                                              oItem := oBrw:oTreeItem:oLast, ;
                                  oItem := oBrw:oLast ), ;
                                  oItem := oItem:Add( 'AAAAA' ),;
                                  oItem:Cargo := {1,2,99},;
                                  oBrw:refresh() )
                                                        
   @ 1, 80 BUTTON 'DEL' OF oDlg SIZE 30,10 PIXEL ACTION  ( msginfo( oBrw:oTreeItem:classname() ) ,;
                                                           IF(  oBrw:oTreeItem:oTree != NIL, ;
                                                                oBrw:oTreeItem:delete( oBrw:oTreeItem:oTree ),;
                                    oBrw:oTreeItem:delete( oBrw:oTreeItem ) ) ,;
                               oBrw:refresh() )
   
   ACTIVATE DIALOG oDlg CENTERED
   RELEASE FONT oFont

return nil

//----------------------------------------------------------------------------//
static function MakeTree( aTot )

   local oTree
   local cRegion, cCountry
   local oRegion, oCountry, i

   TREE oTree
   FOR i := 1 TO 10      
      TREEITEM oRegion PROMPT aData[i,1] CARGO { aData[i,1], 2, i }      
   NEXT
   ENDTREE

return oTree

//----------------------------------------------------------------------------//
static function createTestData()

   LOCAL REGION,COUNTRY,CITY
   local i,n,m

   aData := {}
   for i := 1 to 10
      AADD( aData, { 'DATO ' + STR(i), STR(I), STR(i), i, i } )
   next i

return nil

//----------------------------------------------------------------------------//


some idea?

regards

Marcelo
Posts: 1088
Joined: Fri Oct 07, 2005 03:33 PM
Re: Error en TreeItem ( tree inside xBrowse)
Posted: Tue May 10, 2011 01:30 AM
Hello,

possible solution:

linklist.prg

Code (fw): Select all Collapse
METHOD Add( cPrompt, nLevel, hBmpOpen, hBmpClose, lOpened, bAction, uCargo ) CLASS TLinkList

   local oItem

   DEFAULT lOpened := .f., nLevel := If( ::oFirst != nil, ::oFirst:nLevel,)

   oItem := TTreeItem():New( cPrompt, nLevel, hBmpOpen, hBmpClose, bAction, uCargo, SELF )  // MVG + SELF

   if ::oFirst == nil
      ::oFirst = oItem
      ::oLast  = oItem
   else
      ::oLast:SetNext( oItem )
      oItem:oPrev   = ::oLast
      ::oLast       = oItem
   endif

   oItem:lOpened = lOpened

return oItem


treeitem.prg

Code (fw): Select all Collapse
......
DATA   oParent
........

METHOD New( cPrompt, nLevel, hBmpOpen, hBmpClose, bAction, uCargo, oParent) CLASS TTreeItem  // MVG + oParent
........
::oParent   = oParent
........

METHOD Delete( oRoot ) CLASS TTreeItem

   local oParent  := ::Parent()
   local oTree    := If( oParent == nil, oRoot, oParent:oTree )

   if ( ::oPrev == nil .or. ::oNext == nil ) .and. oRoot == nil
      return .f.
   endif

   if ( ::oPrev == nil .or. ::oPrev:nLevel < ::nLevel ) .and. ;
      ( ::oNext == nil .or. ::oNext:nLevel < ::nLevel )
      // only one item in the parent's tree
      if oParent == nil
         //oRoot:oFirst := oRoot:oLast := nil     // - MVG
     ::oParent:oFirst := oRoot:oLast := nil   // + MVG
         return .t.
      else
         oParent:Close()
         oParent:oTree  := nil
         return .t.
      endif
   endif

   if ::oPrev == nil .or. ::oPrev:nLevel < ::nLevel
      ::oNext:oPrev     := oParent
      //oTree:oFirst      := ::oNext  // - MVG
      ::oParent:oFirst := ::oNext     // + MVG 
   else
      ::oPrev:SetNext( ::oNext )
   endif

   if ::oNext == nil .or. ::oNext:nLevel < ::nLevel
      //oTree:oLast       := ::oPrev  // - MVG 
      ::oParent:oLast := ::oPrev      // + MVG
   else
      ::oNext:oPrev     := ::oPrev
   endif

   if ::oTree != nil
      ::oTree:oLast:oNext  := nil
   endif

return .t.

........


METHOD Add( cPrompt ) CLASS TTreeItem  // cPrompt can be oItem aso

   local oItem := TTreeItem():New( cPrompt, ::nLevel )
   local oParent  := ::Parent()

   if ValType( cPrompt ) == 'O'
      if cPrompt:IsKindOf( 'TTREEITEM' )
         oItem          := cPrompt
         oItem:nLevel   := ::nLevel
         if oItem:oTree != nil
            oItem:oTree:SetLevel( ::nLevel + 1 )
         endif
      else
         return nil
      endif
   else
      oItem := TTreeItem():New( cPrompt, ::nLevel )
      oItem:oParent := ::oParent   // + MVG
   endif

   oItem:oPrev   = Self
   oItem:SetNext( ::oNext )
   ::SetNext( oItem )

   if oItem:oNext != nil
      if oItem:oNext:nLevel == ::nLevel
         oItem:oNext:oPrev       = oItem
      elseif oItem:oNext:nLevel < ::nLevel
         oParent:oTree:oLast     = oItem
      endif
   endif

return oItem


this changes needs test, I hope fivetech can do it and we will have feedback or maybe I am wrong

regards

Marcelo
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Error en TreeItem ( tree inside xBrowse)
Posted: Tue May 10, 2011 08:35 AM

Works well in the recent versions of FWH.

Regards



G. N. Rao.

Hyderabad, India
Posts: 1088
Joined: Fri Oct 07, 2005 03:33 PM
Re: Error en TreeItem ( tree inside xBrowse)
Posted: Tue May 10, 2011 06:18 PM

Mr. Rao,

thanks for de feedback, there are other things to solve, I hope I can do that soon

But how can I put an empty tree inside xbrowse, what I want is start with empty tree and populate it, some idea?

regards

Marcelo

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Error en TreeItem ( tree inside xBrowse)
Posted: Wed May 11, 2011 04:34 AM

As of now, it is not possible to browse empty trees.
Hope we can have this feature in the next version.

Regards



G. N. Rao.

Hyderabad, India
Posts: 1088
Joined: Fri Oct 07, 2005 03:33 PM
Re: Error en TreeItem ( tree inside xBrowse)
Posted: Wed May 11, 2011 04:28 PM

Thanks Mr. Rao,

I will wait for it

regards

Marcelo

Continue the discussion