#!/usr/bin/wxbasic
'
' Example of a small Desktop-Aplett-like program
'
' replace the example-element further down (clock) 
' with your own desklett-element
'


// start-position of Window
dim  maxix = 350
dim  maxiy = 0

// size of Window
dim sizex = 40
dim sizey = 20

// some helpfull stuff
dim startdir = wxGetWorkingdirectory()
dim os=left(lcase(wxGetOsDescription()),3)


dim slash = "/"

if os = "win" then
  slash = "\\"
end if

// create a filename for configuration
if !wxdirExists(wxGetHomedir() & slash & ".wxb-deskletts") then
  wxMkDir(wxGetHomedir() & slash & ".wxb-deskletts")
end if

configname = replace(startdir, "/" , "#")
configname = replace(configname, "\\" , "#")
configname = replace(configname, ":" , "#")
configname = replace(configname, " " , "#")

//  read in the Window-Position
configfilename = wxGetHomedir() & slash & ".wxb-deskletts" & slash & configname & "_pos"
if wxfileexists(configfilename) then
   freeNum= freeFile()
     open configfilename For Input As #freeNum
        Line Input #freeNum, maxix
        Line Input #freeNum, maxiy
     close #freeNum
end if
'wxmessagebox(configfilename)
dim frame
dim framename = "wxb-Desklett"
dim timer
dim theelement

// Windows and Linux need different methods
// to display borderless windows
dim border

// our elemts need individual IDs
dim cid = wxID_Highest
dim optionmenue = new wxMenu()
dim optionmenueid1 = cid
optionmenue.append( optionmenueid1, "exit" , "exit" )


//----------------------------------
// define your global variables here
//----------------------------------
// USERCODE start


dim bitmap1

dim menuedir
dim mHelp = {}
dim myCommands = {}

 dim ProgID = 10000
dim htmlfile
dim html

// USERCODE end
//----------------------------------


window_create( )
sub window_create( )

 
  ' -- create the Window --
  dim linuxborder = command(3)
  if os = "win" then
   border = 1
  else
   if linuxborder = "-border" then
    border = 0
   else
    border = 2
   end if
  end if
  if border = 1 then
   frame = new wxFrame( Nothing, -1, framename, wxPoint(maxix, maxiy), wxSize(1, 1) , wxSUNKEN_BORDER|wxSTAY_ON_TOP)
  end if
  if border = 0 then
   frame = new wxFrame( Nothing, -1, framename, wxPoint(maxix, maxiy), wxSize(1, 1) )
 //wxSIMPLE_BORDER
  end if
  if border = 2 then
    frame = new wxFrame( Nothing, -1, framename, wxPoint(maxix, maxiy), wxSize(1, 1), wxFRAME_TOOL_WINDOW)
  end if
 

'  dummybutton = new wxButton(frame, -1, "")
'  dummybutton.show(0)
'  frame.setbackgroundcolor(dummybutton.getbackgroundcolor())



  cid += 1
  timer = new wxTimer( frame , cid ) 
  Connect( frame, cid, wxEVT_TIMER, "ShowMouse" )
  Connect( frame, optionmenueid1, wxEVT_COMMAND_MENU_SELECTED, "optionmenu_clicked" )

//----------------------------------------------------
// USERCODE start
//----------------------------------------------------
// an example-element (clock), replace it with something else
// it uses the sub show_time()


fsizer=new wxBoxsizer(wxVERTICAL)


Dim myBMP = New wxEmptyBitmap( 32, 32 )
 myBMP.LoadFile( "puppy.gif", wxBITMAP_TYPE_ANY )
       myx=myBMP.GetWidth()
       myy=myBMP.GetHeight()


mb = new wxBitmapButton( frame, -1, myBMP, wxPoint( -1, -1 ), wxSize(myx, myy), wxNO_3D )
fsizer.addwindow(mb)
connect(mb , wxEVT_COMMAND_BUTTON_CLICKED, "main_button")


frame.setsizer(fsizer)
frame.fit()
frame.layout()


 mHelp[0] = new wxMenu()
'startdir = wxgetworkingdirectory()
 menuedir = wxgethomedir() 

 read_menu_from_rc(startdir & "/menuerc")
 read_menu_from_rc(menuedir & "/.myownmenuerc")
 read_menu_from_rc(menuedir & "/.myownkdemenuerc") 
 read_menu_from_rc(menuedir & "/.jwmrc")

//----------------------------------------------------
// USERCODE end
//----------------------------------------------------

 ' frame.Show(True)



  // now handle inbuilt events (movement, popup-menue)
   mb.settooltip("Middle mousebutton: move - Right mousebutton: options")
   connect(mb,wxEVT_MIDDLE_UP,"UnSetPress")
   connect(mb,wxEVT_MIDDLE_DOWN,"SetPress")
   connect(mb,wxEVT_RIGHT_DOWN,"optionmenu_show")



end sub

sub 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

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


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

counter=0

for each zeile in menuline

  if instr(zeile,"<Menu")>0 then
    mHelp[counter+1] = new wxMenu()
    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 )
'print mIcon
'     myBMP = New wxEmptyBitmap( 32, 32 )
'     myBMP.LoadFile( picturedir2 & "/" & mIcon, wxBITMAP_TYPE_XPM )


'myitem = new wxmenuitem( mHelp[counter] , ProgID , mText , mText , 0,mHelp[counter+1]  )

'myitem = new wxmenuitem( mHelp[counter] , ProgID , mText , mText , 0 )

myitem = new wxmenuitem( mHelp[counter]  , ProgID , mText , mText , 0 ,mHelp[counter+1]  )

'myitem.setbitmap(myBMP)
'myitem.setbitmaps(myBMP , myBMP)
mHelp[counter].Appenditem( myitem )


   ' mHelp[counter].AppendMenu(0, mText, mHelp[counter+1])
    counter += 1
  end if

  if instr(zeile,"<Separator")>0 then
    myitem = new wxmenuitem( mHelp[counter] , wxITEM_SEPARATOR , mText , mText , 0)
    mHelp[counter].Appenditem( myitem )
  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 )


    if left(mIcon,1) = "/" then
       rindex = rinstr( mIcon , "/")
       picloaddir = left( mIcon , rindex-1)
       mIcon = right( mIcon , len(mIcon)-rindex)
'print picloaddir
'print mIcon
    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 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

'print mIcon

myitem = new wxmenuitem( mHelp[counter] , ProgID , mText , mText , wxITEM_NORMAL)
myitem.setbitmap(myBMP)
mHelp[counter].Appenditem( myitem )


   '  mHelp[counter].Append(ProgID, mText , 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
  end if

next

end sub



'------------ this part is for the movement -------

 // move main - window

 sub ShowMouse ( event ) // Update position in realtime 
  if down = 0 then :return :end if
  mousePoint = wxGetMousePosition() 
  maxix = mousePoint.GetX() - sizex/2
  maxiy = mousePoint.GetY() - sizey/2
  frame.Move( maxix , maxiy )  
 end sub 


 sub SetPress( event )
  down=1 : timer.Start( 20 ) 
 end sub
 sub UnSetPress( event )
  down=0 : timer.stop()
 end sub

'------------end of movement-routines ---------------

sub optionmenu_show( event )
   frame.PopupMenuXY(optionmenue,10,19)
end sub

sub optionmenu_clicked( event )
 dim theid = event.getID()
 if theid = optionmenueid1 then
   onQuit(1)
 end if

end sub

sub onQuit( event )
'print maxix & "  " & maxiy
   // save current position and exit
   if val(maxix) <0 then:maxix=0:end if
   if val(maxiy) <0 then:maxiy=0:end if

   freeNum= freeFile()
     open configfilename For Output As #freeNum
        print #freeNum , maxix
        print #freeNum , maxiy
     close #freeNum
  end
end sub


//----------------------------------
// USERCODE (subs)
//----------------------------------
// put your own subroutines here






sub main_button( event )
 frame.PopupMenuXY(mHelp[0],10,20)
end sub

sub menu_clicked( event )

 ' print myCommands[event.GetID()-10000]

  mycommand = myCommands[event.GetID()-10000]

  if myCommand = "MENUERESTART" then

    wxExecute(startdir & "/../menue")
    onQuit( 1 )
  end if

  if myCommand = "MENUEHELP" then
    htmlfile = startdir & "/menue.htm" 
    htmlhelp( "Menue-Help" , htmlfile )
    return
  end if



  shell(myCommand & "&")

end sub



sub htmlhelp( title , htmlfile )
'  dim htmlframe = new wxFrame(Nothing , -1 , "Help" , wxpoint(100 , 100) , wxsize(700, 500))
'  dim htmlhelper = new wxHTMLWindow(htmlframe , -1)
'  htmlhelper.loadpage( filename )
'  htmlframe.show(1)

htmlframe = new wxDialog(Nothing,-1,title, wxPoint(-1, -1), wxSize(700, 500),wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
htmlsizer = new wxBoxSizer(wxVERTICAL)
buttonsizer = new wxBoxSizer(wxHORIZONTAL)
htmlsizer.addSizer(buttonsizer)
htmlframe.setsizer(htmlsizer)
b1 = new wxButton(htmlframe, -1 , "<", wxPoint(-1,-1))
connect(b1,wxEVT_COMMAND_BUTTON_CLICKED,"button_b1")
buttonsizer.AddWindow( b1,1,wxEXPAND)
htmlframe.setbackgroundcolor(b1.getbackgroundcolor())
b2 = new wxButton(htmlframe, -1 , ">", wxPoint(-1,-1))
connect(b2,wxEVT_COMMAND_BUTTON_CLICKED,"button_b2")
buttonsizer.AddWindow( b2,1,wxEXPAND)
b4 = new wxButton(htmlframe, -1 , "reload", wxPoint(-1,-1))
connect(b4,wxEVT_COMMAND_BUTTON_CLICKED,"button_b4")
buttonsizer.AddWindow( b4,1,wxEXPAND)
b3 = new wxButton(htmlframe, -1 , "mozilla", wxPoint(-1,-1))
connect(b3,wxEVT_COMMAND_BUTTON_CLICKED,"button_b3")
buttonsizer.AddWindow( b3,1,wxEXPAND)
html = new wxHtmlWindow(htmlframe)
 htmlsizer.AddWindow( html,1,wxEXPAND|wxALL,0)
 htmlsizer.layout()
if wxfileexists(htmlfile) then
 html.loadpage(htmlfile)
else
 html.setpage("file not found error")
end if
'htmlframe.Showmodal()
htmlframe.Show(1)
end sub

sub button_b1( event )
 html.historyback()
end sub
sub button_b2( event )
  html.historyforward()
end sub
sub button_b4( event )
  html.loadpage(htmlfile)
end sub
sub button_b3( event )
  wxexecute("mozilla " & htmlfile)
end sub
