#!/usr/bin/wxbasic

dim startdir = wxGetworkingDirectory()
Dim me_frame = new wxFrame(nothing,-1,"wxB-Menue-Editor  --- please wait, loading Icons ---",wxPoint(100,100),wxsize(740,560))

me_currentdir = "/usr/bin"
slash = "/"

debug = 0

lw=0
if debug = 1 then
   lw = 100
end if

dim me_mainsizer = new wxBoxsizer(wxHORIZONTAL)
dim me_allsizer = new wxBoxsizer(wxVERTICAL)
me_frame.setsizer(me_mainsizer)
me_mainsizer.addsizer(me_allsizer, 1 , wxEXPAND|wxALL,4)

me_hs1 = new wxBoxsizer(wxHORIZONTAL)
me_allsizer.addsizer(me_hs1, 1 , wxEXPAND|wxALL,1)

me_lb = new wxListbox(me_frame , -1, wxPoint(-1,-1),wxsize(500,300))
me_hs1.addwindow( me_lb, 1, wxEXPAND )

 connect(me_lb,wxEVT_COMMAND_LISTBOX_SELECTED,"lb_selected")

me_lb2 = new wxListbox(me_frame , -1, wxPoint(-1,-1),wxsize(lw,300))
me_hs1.addwindow( me_lb2, 0, wxEXPAND )

me_lb3 = new wxListbox(me_frame , -1, wxPoint(-1,-1),wxsize(lw,300))
me_hs1.addwindow( me_lb3, 0, wxEXPAND )

me_lb4 = new wxListbox(me_frame , -1, wxPoint(-1,-1),wxsize(lw,300))
me_hs1.addwindow( me_lb4, 0, wxEXPAND )

me_lb5 = new wxListbox(me_frame , -1, wxPoint(-1,-1),wxsize(lw,300))
me_hs1.addwindow( me_lb5, 0, wxEXPAND )

if debug = 0 then

  me_lb2.show(0)
  me_lb3.show(0)
  me_lb4.show(0)
  me_lb5.show(0)
end if

//----option-buttons

me_hbsizer1 = new wxBoxsizer(wxHORIZONTAL)
me_allsizer.addsizer(me_hbsizer1, 0 , wxEXPAND|wxALL,1)


dim me_obu0a = new wxButton(me_frame, -1, "up" , wxPoint(-1,-1),wxSize(-1,-1))
me_hbsizer1.addwindow( me_obu0a, 1  , wxEXPAND )
connect( me_obu0a , -1 , wxEVT_COMMAND_BUTTON_CLICKED , "obu_up" )

dim me_obu0b = new wxButton(me_frame, -1, "down" , wxPoint(-1,-1),wxSize(-1,-1))
me_hbsizer1.addwindow( me_obu0b, 1 , wxEXPAND)
connect( me_obu0b , -1 , wxEVT_COMMAND_BUTTON_CLICKED , "obu_down" )



dim me_obu1 = new wxButton(me_frame, -1, "delete" , wxPoint(-1,-1),wxSize(-1,-1))
me_hbsizer1.addwindow( me_obu1, 1, wxEXPAND )
connect( me_obu1 , -1 , wxEVT_COMMAND_BUTTON_CLICKED , "obu_delete" )

dim me_obu2 = new wxButton(me_frame, -1, ">> menue" , wxPoint(-1,-1),wxSize(-1,-1))
me_hbsizer1.addwindow( me_obu2, 1, wxEXPAND )
connect( me_obu2 , -1 , wxEVT_COMMAND_BUTTON_CLICKED , "obu_newsubmenue" )

dim me_obu3 = new wxButton(me_frame, -1, "<< menue" , wxPoint(-1,-1),wxSize(-1,-1))
me_hbsizer1.addwindow( me_obu3, 1, wxEXPAND )
connect( me_obu3 , -1 , wxEVT_COMMAND_BUTTON_CLICKED , "obu_closesubmenue" )


dim me_obu4 = new wxButton(me_frame, -1, "program" , wxPoint(-1,-1),wxSize(-1,-1))
me_hbsizer1.addwindow( me_obu4, 1, wxEXPAND )
connect( me_obu4 , -1 , wxEVT_COMMAND_BUTTON_CLICKED , "obu_addprogram" )


dim me_obu5 = new wxButton(me_frame, -1, "seperator" , wxPoint(-1,-1),wxSize(-1,-1))
me_hbsizer1.addwindow( me_obu5, 1, wxEXPAND )
connect( me_obu5 , -1 , wxEVT_COMMAND_BUTTON_CLICKED , "obu_seperator" )
//----

me_hs2 = new wxBoxsizer(wxHORIZONTAL)
me_allsizer.addsizer(me_hs2, 0 , wxEXPAND|wxALL,1)

myBMP = new wxEmptybitmap(32 ,32)
myBMP.loadfile( startdir & "/empty.gif" , wxBITMAP_TYPE_GIF )
dim me_bmpbu1 = new wxBitmapButton(me_frame, -1, myBMP , wxPoint(-1,-1),wxSize(-1,-1))
me_hs2.addwindow( me_bmpbu1, 0, wxEXPAND )

me_vs1 = new wxBoxsizer(wxVERTICAL)
me_hs2.addsizer(me_vs1, 1 , wxEXPAND|wxALL,1)

dim me_tc1 = new wxTextCtrl(me_frame , -1 , "" , wxPoint(-1,-1),wxSize(-1,-1))
me_vs1.addwindow( me_tc1, 0, wxEXPAND )
connect( me_tc1 , -1 , wxEVT_KEY_UP , "me_tc1_updated" )


me_hs3 = new wxBoxsizer(wxHORIZONTAL)
me_vs1.addsizer(me_hs3, 0 , wxEXPAND|wxALL,0)

dim me_tc2 = new wxTextCtrl(me_frame , -1 , "" , wxPoint(-1,-1),wxSize(-1,-1))
me_hs3.addwindow( me_tc2, 1, wxEXPAND )
connect( me_tc2 , -1 , wxEVT_KEY_UP , "me_tc2_updated" )

dim me_busearch = new wxButton(me_frame, -1, "search" , wxPoint(-1,-1),wxSize(-1,-1))
me_hs3.addwindow( me_busearch, 0, wxEXPAND )
connect( me_busearch , -1 , wxEVT_COMMAND_BUTTON_CLICKED , "me_bu_search" )

dim me_bu1 = new wxButton(me_frame, -1, "save" , wxPoint(-1,-1),wxSize(-1,-1))
me_allsizer.addwindow( me_bu1, 0, wxEXPAND )
connect( me_bu1 , -1 , wxEVT_COMMAND_BUTTON_CLICKED , "save_menue" )


dim picturedir = "/usr/share/mini-icons"
dim picturedir2 = "/usr/local/X11/pixmaps"
dim picturedir3 = "/usr/share/icons"

//--- iconlist
dim me_iconsizer = new wxBoxsizer(wxVERTICAL)
me_mainsizer.addsizer(me_iconsizer, 0 , wxEXPAND|wxALL,4)


'ic= 0
'myBMP = new wxEmptybitmap(32 ,32)
'myBMP.loadfile( startdir & "/puppy.gif" , wxBITMAP_TYPE_GIF )
'dim me_bmpbu[ic] = new wxBitmapButton(me_frame, 20000+ic, myBMP , wxPoint(-1,-1),wxSize(-1,-1))
'me_iconsizer.addwindow( me_bmpbu[ic], 0, wxEXPAND )
'

'me_frame.fit()
me_frame.show(1)

include "iconlist.wxb"

dim intro
dim myCommands ={}
dim ProgID=10000

 menuedir = wxgethomedir() 
 me_read_menu_from_rc(menuedir & "/.myownmenuerc")


me_frame.settitle( "wxB-Menue-Editor" )

//--------------------------------
sub me_bu_search( event )  

    mychoiceindex = me_lb.GetSelection()

    mycommand = me_lb2.getString( mychoiceindex )
    if mycommand = "m°" OR mycommand = "s°" OR mycommand = "c°" OR mychoiceindex = -1 then
       return
    end if

    me_wildcard ="All files|*"
    me_fileDialog = new wxFileDialog( Nothing, "","","", me_wildcard ) 
    me_fileDialog.SetMessage("Open File") 
    me_fileDialog.SetStyle( wxOPEN|wxCHANGE_DIR  ) 
    me_fileDialog.SetWildcard( me_wildcard ) 
    me_fileDialog.SetPath( me_currentdir & slash)  

    If me_fileDialog.ShowModal() = wxID_OK Then  
      me_currentdir = me_fileDialog.GetDirectory()
      me_name =me_fileDialog.GetDirectory() & "/" & me_fileDialog.GetFilename() 
      me_tc2.setvalue( me_name )
      me_tc2_updated( 1 )
    end if 


end sub

//--------------------------------
sub obu_up( event )


  mychoiceindex = me_lb.GetSelection()
  if mychoiceindex = 0 then
    return
  end if

  myamount = me_lb.getcount() 
  me_lb.append( me_lb.getString( myamount-1 ) )
  me_lb2.append( me_lb2.getString( myamount-1 ) )
  me_lb3.append( me_lb3.getString( myamount-1 ) )
  me_lb4.append( me_lb4.getString( myamount-1 ) )
  me_lb5.append( me_lb5.getString( myamount-1 ) )

  for i = myamount-1 to mychoiceindex step -1

    me_lb.setstring( i, me_lb.getString( i-1 ) )
    me_lb2.setstring( i, me_lb2.getString( i-1 ) )
    me_lb3.setstring( i, me_lb3.getString( i-1 ) )
    me_lb4.setstring( i, me_lb4.getString( i-1 ) )
    me_lb5.setstring( i, me_lb5.getString( i-1 ) )
   
  next
  

  me_lb.setstring( mychoiceindex-1, me_lb.getString( mychoiceindex +1 ) )
  me_lb2.setstring( mychoiceindex-1, me_lb2.getString( mychoiceindex +1 ) )  
  me_lb3.setstring( mychoiceindex-1, me_lb3.getString( mychoiceindex +1 ) )
  me_lb4.setstring( mychoiceindex-1, me_lb4.getString( mychoiceindex +1 ) )
  me_lb5.setstring( mychoiceindex-1, me_lb5.getString( mychoiceindex +1 ) )

  me_lb.delete( mychoiceindex +1 )
  me_lb2.delete( mychoiceindex +1 )
  me_lb3.delete( mychoiceindex +1 )
  me_lb4.delete( mychoiceindex +1 )
  me_lb5.delete( mychoiceindex +1 )

recalc_mainlist()

  me_lb.setselection( mychoiceindex-1 , 0 )
  me_lb.setselection( mychoiceindex-1 , 1 )
  lb_selected( dummy )

end sub

//--------------------------------
sub obu_down( event )

  mychoiceindex = me_lb.GetSelection()
  myamount = me_lb.getcount() 

  if mychoiceindex = myamount-1 then
    return
  end if

  me_lb.append( me_lb.getString( myamount-1 ) )
  me_lb2.append( me_lb2.getString( myamount-1 ) )
  me_lb3.append( me_lb3.getString( myamount-1 ) )
  me_lb4.append( me_lb4.getString( myamount-1 ) )
  me_lb5.append( me_lb5.getString( myamount-1 ) )

  for i = myamount-1 to mychoiceindex+1 step-1

    me_lb.setstring( i, me_lb.getString( i-1 ) )
    me_lb2.setstring( i, me_lb2.getString( i-1 ) )
    me_lb3.setstring( i, me_lb3.getString( i-1 ) )
    me_lb4.setstring( i, me_lb4.getString( i-1 ) )
    me_lb5.setstring( i, me_lb5.getString( i-1 ) )
   
  next


  me_lb.setstring( mychoiceindex, me_lb.getString( mychoiceindex +2 ) )
  me_lb2.setstring( mychoiceindex, me_lb2.getString( mychoiceindex +2 ) )  
  me_lb3.setstring( mychoiceindex, me_lb3.getString( mychoiceindex +2 ) )
  me_lb4.setstring( mychoiceindex, me_lb4.getString( mychoiceindex +2 ) )
  me_lb5.setstring( mychoiceindex, me_lb5.getString( mychoiceindex +2 ) )

  me_lb.delete( mychoiceindex +2 )
  me_lb2.delete( mychoiceindex +2 )
  me_lb3.delete( mychoiceindex +2 )
  me_lb4.delete( mychoiceindex +2 )
  me_lb5.delete( mychoiceindex +2 )

recalc_mainlist()

  me_lb.setselection( mychoiceindex+1 , 0 )
  me_lb.setselection( mychoiceindex+1 , 1 )
  lb_selected( dummy )

end sub


//--------------------------------
sub obu_delete( event )
  mychoiceindex = me_lb.GetSelection()
  me_lb.delete( mychoiceindex )
  me_lb2.delete( mychoiceindex )
  me_lb3.delete( mychoiceindex )
  me_lb4.delete( mychoiceindex )
  me_lb5.delete( mychoiceindex )

  if me_lb.getcount() = mychoiceindex then
    mychoiceindex = me_lb.getcount()-1
  end if

  recalc_mainlist()

  me_lb.setselection( mychoiceindex , 0 )
  me_lb.setselection( mychoiceindex , 1 )
  lb_selected( dummy )
end sub

//--------------------------------
sub obu_seperator( event )


  mychoiceindex = me_lb.GetSelection()
  myamount = me_lb.getcount() 
  me_lb.append( me_lb.getString( myamount-1 ) )
  me_lb2.append( me_lb2.getString( myamount-1 ) )
  me_lb3.append( me_lb3.getString( myamount-1 ) )
  me_lb4.append( me_lb4.getString( myamount-1 ) )
  me_lb5.append( me_lb5.getString( myamount-1 ) )

  for i = myamount-1 to mychoiceindex+1 step -1

    me_lb.setstring( i, me_lb.getString( i-1 ) )
    me_lb2.setstring( i, me_lb2.getString( i-1 ) )
    me_lb3.setstring( i, me_lb3.getString( i-1 ) )
    me_lb4.setstring( i, me_lb4.getString( i-1 ) )
    me_lb5.setstring( i, me_lb5.getString( i-1 ) )
   
  next
  

  me_lb.setstring( mychoiceindex+1,  "-----------------" )
  me_lb2.setstring( mychoiceindex+1, "s°" )  
  me_lb3.setstring( mychoiceindex+1, "°" )
  me_lb4.setstring( mychoiceindex+1, "-1" )
  me_lb5.setstring( mychoiceindex+1,  "-----------------" )

recalc_mainlist()

  me_lb.setselection( mychoiceindex+1 , 0 )
  me_lb.setselection( mychoiceindex+1 , 1 )
  lb_selected( dummy )

end sub



//--------------------------------
sub obu_addprogram( event )


  mychoiceindex = me_lb.GetSelection()
  myamount = me_lb.getcount() 
  me_lb.append( me_lb.getString( myamount-1 ) )
  me_lb2.append( me_lb2.getString( myamount-1 ) )
  me_lb3.append( me_lb3.getString( myamount-1 ) )
  me_lb4.append( me_lb4.getString( myamount-1 ) )
  me_lb5.append( me_lb5.getString( myamount-1 ) )

  for i = myamount-1 to mychoiceindex+1 step -1

    me_lb.setstring( i, me_lb.getString( i-1 ) )
    me_lb2.setstring( i, me_lb2.getString( i-1 ) )
    me_lb3.setstring( i, me_lb3.getString( i-1 ) )
    me_lb4.setstring( i, me_lb4.getString( i-1 ) )
    me_lb5.setstring( i, me_lb5.getString( i-1 ) )
   
  next
  

  me_lb.setstring( mychoiceindex+1,  "newProgram" )
  me_lb2.setstring( mychoiceindex+1, "runme" )  
  me_lb3.setstring( mychoiceindex+1, "Executable.xpm" )
  me_lb4.setstring( mychoiceindex+1, "-1" )
  me_lb5.setstring( mychoiceindex+1,  "newProgram" )

recalc_mainlist()

  me_lb.setselection( mychoiceindex+1 , 0 )
  me_lb.setselection( mychoiceindex+1 , 1 )
  lb_selected( dummy )

end sub


//--------------------------------
sub obu_closesubmenue( event )


  mychoiceindex = me_lb.GetSelection()
  myamount = me_lb.getcount() 
  me_lb.append( me_lb.getString( myamount-1 ) )
  me_lb2.append( me_lb2.getString( myamount-1 ) )
  me_lb3.append( me_lb3.getString( myamount-1 ) )
  me_lb4.append( me_lb4.getString( myamount-1 ) )
  me_lb5.append( me_lb5.getString( myamount-1 ) )

  for i = myamount-1 to mychoiceindex+1 step -1

    me_lb.setstring( i, me_lb.getString( i-1 ) )
    me_lb2.setstring( i, me_lb2.getString( i-1 ) )
    me_lb3.setstring( i, me_lb3.getString( i-1 ) )
    me_lb4.setstring( i, me_lb4.getString( i-1 ) )
    me_lb5.setstring( i, me_lb5.getString( i-1 ) )
   
  next
  

  me_lb.setstring( mychoiceindex+1,  "<" )
  me_lb2.setstring( mychoiceindex+1, "c°" )  
  me_lb3.setstring( mychoiceindex+1, "°" )
  me_lb4.setstring( mychoiceindex+1, "-1" )
  me_lb5.setstring( mychoiceindex+1,  "" )

recalc_mainlist()

  me_lb.setselection( mychoiceindex+1 , 0 )
  me_lb.setselection( mychoiceindex+1 , 1 )
  lb_selected( dummy )

end sub

//--------------------------------
sub obu_newsubmenue( event )

  mychoiceindex = me_lb.GetSelection()
  myamount = me_lb.getcount() 
  me_lb.append( me_lb.getString( myamount-1 ) )
  me_lb2.append( me_lb2.getString( myamount-1 ) )
  me_lb3.append( me_lb3.getString( myamount-1 ) )
  me_lb4.append( me_lb4.getString( myamount-1 ) )
  me_lb5.append( me_lb5.getString( myamount-1 ) )

  for i = myamount-1 to mychoiceindex+1 step -1

    me_lb.setstring( i, me_lb.getString( i-1 ) )
    me_lb2.setstring( i, me_lb2.getString( i-1 ) )
    me_lb3.setstring( i, me_lb3.getString( i-1 ) )
    me_lb4.setstring( i, me_lb4.getString( i-1 ) )
    me_lb5.setstring( i, me_lb5.getString( i-1 ) )
   
  next
  

  me_lb.setstring( mychoiceindex+1,  ">submenue" )
  me_lb2.setstring( mychoiceindex+1, "m°" )  
  me_lb3.setstring( mychoiceindex+1, "°" )
  me_lb4.setstring( mychoiceindex+1, "-1")
  me_lb5.setstring( mychoiceindex+1,  "submenue" )

recalc_mainlist()

  me_lb.setselection( mychoiceindex+1 , 0 )
  me_lb.setselection( mychoiceindex+1 , 1 )
  lb_selected( dummy )
end sub

//--------------------
//--------------------
sub recalc_mainlist()

counter = -1
for n=0 to me_lb.getcount() 

  mytype = me_lb2.getString( n )
  if mytype = "m°" then
    counter += 1
  end if

  mytitle = me_lb5.getString( n )
  mycommand = me_lb2.getString( n )

     intro = ""
     for i=0 to (counter)*3
        intro &= " "
     next
     if mycommand = "m°" then
        intro &= ">>"
     else
       if mycommand = "c°" then
          intro &= "<<"
       else
          intro &= "    "
       end if
     end if

  me_lb.setstring( n,  intro & mytitle )
  me_lb4.setstring( n,  counter+1 )
  if mytype = "c°" then
    counter -= 1
  end if



next

end sub


//--------------------------------
sub me_tc1_updated( event )
  mychoiceindex = me_lb.GetSelection()
  mycommand = me_lb2.getString( mychoiceindex )

  newval = me_tc1.getvalue()

     intro = ""
     mydepth = val(me_lb4.getString( mychoiceindex ))-1
     for i=0 to (mydepth)*3
        intro &= " "
     next
     if mycommand = "m°" then
        intro &= ">>"
     else
       if mycommand = "c°" then
          intro &= "<<"
       else
          intro &= "    "
       end if
     end if
  me_lb.setstring( mychoiceindex  , intro & newval )
  me_lb5.setstring( mychoiceindex  , newval )
end sub
//--------------------------------
sub me_tc2_updated( event )
  mychoiceindex = me_lb.GetSelection()
  newval = me_tc2.getvalue()
  if newval != "" then
     me_lb2.setstring( mychoiceindex  , newval )
  end if
end sub

//--------------------------------
sub set_icon( event )

  mychoiceindex = me_lb.GetSelection()

  mycommand = me_lb2.getString( mychoiceindex )
  if mycommand = "m°" OR mycommand = "s°" OR mycommand = "c°" OR mychoiceindex = -1 then
    return
  end if

   dim lines = ifs_listcontrol1.GetItemCount() 
   thefile = ""
   for i=0 to lines+1 
     thecurrentcheck = ifs_listcontrol1.GetNextItem(i-1,-1,wxLIST_STATE_SELECTED) 
     if i = thecurrentcheck then 
       thefile =  ifs_listcontrol1.GetItemText(i) 
     end if
   next
'  wxmessagebox(thefile)


     mIcon = thefile

if ifs_currentdir != "" then
   picloaddir = ifs_currentdir
else

     picloaddir = picturedir2
     if wxFileexists(picturedir & "/" & mIcon) then
        picloaddir = picturedir
     end if
     if wxFileexists(picturedir3 & "/" & mIcon) then
        picloaddir = picturedir3
     end if
     if left (mIcon , 8) = "MENUEDIR" then
        mIcon = replace(mIcon, "MENUEDIR/" , "")
        picloaddir = startdir
     end if
end if

     myBMP = New wxEmptyBitmap( 32, 32 )

     if wxfileexists( picloaddir & "/" & mIcon ) then

     if lcase(right(mIcon , 3)) = "xpm" then 
         myBMP.LoadFile( picloaddir & "/" & mIcon, wxBITMAP_TYPE_XPM )
     else
        if lcase(right(mIcon , 3)) = "png" then 
           myBMP.LoadFile( picloaddir & "/" & mIcon, wxBITMAP_TYPE_PNG )
        else
           myBMP.LoadFile( picloaddir & "/" & mIcon, wxBITMAP_TYPE_ANY )
        end if
     end if

     else
        myBMP.loadfile( startdir & "/empty.gif" , wxBITMAP_TYPE_GIF )
     end if
     me_bmpbu1.setbitmaplabel( myBMP )

  me_lb3.setstring( mychoiceindex, picloaddir & "/" & mIcon )

end sub

//--------------------------------
sub lb_selected( event )
  mychoiceindex = me_lb.GetSelection()
  mycommand = me_lb2.getString( mychoiceindex )
  mychoice = me_lb5.getString( mychoiceindex )


  me_tc1.setvalue( mychoice  )


  me_tc1.seteditable(true)
  me_tc2.seteditable(true)


  if mycommand = "s°" OR mycommand = "c°" then
    me_tc1.seteditable(false)
  end if
  if mycommand = "m°" OR mycommand = "s°" OR mycommand = "c°" then
    mycommand = ""
    me_tc2.seteditable(false)
  end if


  me_tc2.setvalue( mycommand  )

    
     mIcon = me_lb3.getString( mychoiceindex )

   if left( mIcon , 1 ) = "/" then
     lindex = rinstr( mIcon , "/")
     picloaddir = left( mIcon , lindex-1 )
     mIcon = right ( mIcon , len(mIcon)-lindex)
     
   else
     picloaddir = picturedir2
     if wxFileexists(picturedir & "/" & mIcon) then
        picloaddir = picturedir
     end if
     if wxFileexists(picturedir3 & "/" & mIcon) then
        picloaddir = picturedir3
     end if
     if left (mIcon , 8) = "MENUEDIR" then
        mIcon = replace(mIcon, "MENUEDIR/" , "")
        picloaddir = startdir
     end if
   end if

     myBMP = New wxEmptyBitmap( 32, 32 )
    
     if wxfileexists( picloaddir & "/" & mIcon ) then

     if lcase(right(mIcon , 3)) = "xpm" then 
         myBMP.LoadFile( picloaddir & "/" & mIcon, wxBITMAP_TYPE_XPM )
     else
        if lcase(right(mIcon , 3)) = "png" then 
           myBMP.LoadFile( picloaddir & "/" & mIcon, wxBITMAP_TYPE_PNG )
        else
           myBMP.LoadFile( picloaddir & "/" & mIcon, wxBITMAP_TYPE_ANY )
        end if
     end if

     else
        myBMP.loadfile( startdir & "/empty.gif" , wxBITMAP_TYPE_GIF )
     end if
     me_bmpbu1.setbitmaplabel( myBMP )
end sub

//--------------------------------
sub me_read_menu_from_rc(rcfile)

menuline={}
exist=1
try

Open rcfile For Input As #1
catch
exist=0
'wxmessagebox("ERROR: " & rcfile & " not found.")
return
end try

if exist then
counter=0

myline = ""
while left(myline , 9) != "<RootMenu" AND Not Eof( 1 )
  Line Input #1, myline
wend

zeile = ""
while left(zeile , 11) != "</RootMenu>" AND  Not Eof( 1 )
Line Input #1, zeile
if zeile<>Nothing then

  if left(zeile, 4) != "<!--" then
  if left(zeile, 10) != "</RootMenu" then
     menuline[counter] =ltrim(zeile) 'führende Leerzeichen löschen, um leichter arbeiten zu können
  end if
  end if
end if

counter+=1
End while
Close #1
end if

'----------------

counter=0

for each zeile in menuline

  if instr(zeile,"<Menu")>0 then

    lindex = instr( zeile , "\"")
    mText=right( zeile , len(zeile)-lindex)
    lindex = instr( mText , "\"")
    mText=left( mText , lindex-1)

     lindex = instr( zeile , "\" icon")
     mIcon = right( zeile , len(zeile)-lindex-7 )
     lindex = instr(  mIcon , "\">")
     mIcon = left( mIcon , lindex-1 )

    me_lb.append(mText)
    me_lb2.append("m°")
    me_lb3.append("°")
    me_lb4.append(counter)
    me_lb5.append(mText)
    counter += 1
  end if

  if instr(zeile,"<Separator")>0 then

     me_lb.append("-----------------")
     me_lb2.append("s°")
     me_lb3.append("°")
     me_lb4.append(counter)
     me_lb5.append("-----------------")
  end if

  if instr(zeile,"<Program")>0 then
     mCommand = replace(zeile , "</Program>" , "")
     lindex = instr( zeile , "\">")
     mCommand=right( mCommand , len(mCommand)-lindex-1)

     mText = right(zeile, len(zeile)-16)
     lindex = instr( mText , "\" icon")
     mText = left( mText , lindex-1 )

     lindex = instr( zeile , "\" icon")
     mIcon = right( zeile , len(zeile)-lindex-7 )
     lindex = instr(  mIcon , "\">")
     mIcon = left( mIcon , lindex-1 )

     picloaddir = picturedir2
     if wxFileexists(picturedir & "/" & mIcon) then
        picloaddir = picturedir
     end if
     if wxFileexists(picturedir3 & "/" & mIcon) then
        picloaddir = picturedir3
     end if
     if left (mIcon , 8) = "MENUEDIR" then
        mIcon = replace(mIcon, "MENUEDIR/" , "")
        picloaddir = startdir
     end if
     myBMP = New wxEmptyBitmap( 32, 32 )

     if lcase(right(mIcon , 3)) = "xpm" then 
         myBMP.LoadFile( picloaddir & "/" & mIcon, wxBITMAP_TYPE_XPM )
     else
        if lcase(right(mIcon , 3)) = "png" then 
           myBMP.LoadFile( picloaddir & "/" & mIcon, wxBITMAP_TYPE_PNG )
        else
           myBMP.LoadFile( picloaddir & "/" & mIcon, wxBITMAP_TYPE_ANY )
        end if
     end if

     me_lb.append(mText)
     me_lb2.append(mCommand)
     me_lb3.append(mIcon)
     me_lb4.append(counter)
     me_lb5.append(mText)
       myCommands[ProgID-10000] = mCommand
'       Connect( frame, ProgID, wxEVT_COMMAND_MENU_SELECTED, "menu_clicked" )
 

     ProgID += 1
  end if


  if instr(zeile,"</Menu")>0 then

    counter -= 1

     me_lb.append("")
     me_lb2.append("c°")
     me_lb3.append("°")
     me_lb4.append(counter)
     me_lb5.append("")

  end if

next
recalc_mainlist()
end sub


sub save_menue( event )

   //-- integrity check

counter = 0
for n=0 to me_lb.getcount() -1

  mytype = me_lb2.getString( n )
  if mytype = "m°" then
    counter += 1
  end if
  if mytype = "c°" then
    counter -= 1
    if counter <0 then
       wxMessagebox("Integrity Error, correct the submenues first - aborting!")
       return
    end if
  end if

next
    if counter !=0 then
       wxMessagebox("Integrity Error, correct the submenues first - aborting!")
       return
    end if

  //--save to disk

Open menuedir & "/.myownmenuerc" For output As #1
print #1 , "<?xml version=\"1.0\"?>"
print #1 ,  ""

print #1 ,  "<RootMenu>"

for n=0 to me_lb.getcount() -1

  mytitle = me_lb5.getString( n )
  mycommand = me_lb2.getString( n )
  myicon = me_lb3.getString( n )

  mytype = me_lb2.getString( n )

  done = 0
  if mytype = "m°" then
    print #1 ,  "<Menu label=\""  & mytitle & "\" icon=\"nothing.xpm\">"
    done = 1
  end if

  if mytype = "c°" then
    print #1 ,  "</Menu>"
    done = 1
  end if

  if mytype = "s°" then
    print #1 ,  "<Separator/>"
    done = 1
  end if

  if done = 0 then
     print #1 ,  "<Program label=\"" & mytitle & "\" icon=\"" & myicon & "\">" & mycommand & "</Program>"
  end if

next

print #1 ,  "</RootMenu>"
close #1

wxmessagebox("File was saved!")

end sub
