
'10:22 01/09/2013
'15:07 12/05/2017
'14:14 12/03/2018
'13:07 18/04/2018
'18:11 26/08/2018
'09:45 03/06/2019
'13:28 19/07/2019
'10:44 28/09/2022
'00:10 14/06/2023


'itr: tools: icons resources,hyperlinks
  #compact

 '$filename   "Oxide.exe"
  'uses     RTL32
  uses     OxideUtil

 'o2_mode 0   'read/return null terminated ascii strings
  o2_mode 9   'ascii string (1: byte characters) (8: o2 string)

  string about="OxIde for OxygenBasic 2017" +cr+
  "Using Oxygen.dll, version "+o2_version()+cr+
  "http://www.oxygenbasic.org"+cr+
  "Author: Charles E V Pegge"

  MainWindow 480,480,WS_OVERLAPPEDWINDOW

  function HelpMessageBox()
  =========================
  string HelpInfo="

  FUNCTION KEYS

  F1            *Language Help
  shift F1      *OxIde Help
  ctrl F1       *About
  F3            **Find next
  shift F3      *Find prev
  ctrl F3       *capture keyword
  F4            **Find/Replace next
  shift F4      *Find/Replace prev
  ctrl F4       *capture keyword
  Alt-F4        *Save and Quit
  F5            *Exec direct 32 bit
  shift F5      *Exec direct 64 bit
  ctrl  F5      *Compile 32bit binary
  ctrl shift F5 *Compile 64bit binary
  F7            *DOS console
  ctrl F7       *Find Files by Keyword (FindEd)
  F8            *Load File
  F9            *Save File
  Shift F9      *Save As ..

  OTHER KEYS

  ^A     *Select all
  ^C     *Copy
  ^E     *Exec Program
  ^X     *Cut
  ^V     *Paste
  ^F     *Find (edit box)
  ^R     *Replace (edit box)
  ^O     *Open File
  ^S     *Save File
  ^T     *TimeStamp
  ^Z     *Undo

  ^G     *Goto line number (edit box)
  ^Home  *Goto Top
  ^End   *Goto bottom

  "
  HelpInfo = replace HelpInfo,"*",tab
  MessageBox 0,HelpInfo,"Oxide Help",MB_ICONINFORMATION
  end function

  'MENU ITEMS
  '
  %IDM_FILE_NEW               3000
  %IDM_FILE_OPEN              3001
  %IDM_FILE_SAVE              3002
  %IDM_FILE_SAVE_AS           3003
  %IDM_DOS                    3004
  %IDM_SHOW_PROJ              3005
  %IDM_APP_EXIT               3009
  '
  %IDM_EDIT_UNDO              3010
  %IDM_EDIT_CUT               3011
  %IDM_EDIT_COPY              3012
  %IDM_EDIT_PASTE             3013
  %IDM_EDIT_FIND              3014
  %IDM_EDIT_REPLACE           3015
  %IDM_EDIT_REPLACE_IN_BLOCK  3016
  %IDM_EDIT_SELECT_ALL        3017
  %IDM_EDIT_TIME              3018
  '
  %IDM_EXECUTE32              3020
  %IDM_EXECUTE64              3027
  %IDM_COMPILE                3021
  %IDM_COMPILE_RTL32          3022
  %IDM_COMPILE_RTL64          3023
  %IDM_COMPILE_EXE            3024
  %IDM_COMPILE_DLL            3025
  %IDM_COMPILE_TO_DIR         3026

  %IDM_APP_HELP_OXYGEN        3030
  %IDM_APP_HELP_OXIDE         3031
  %IDM_APP_ABOUT              3032
  %IDM_APP_LIST               3033
  %IDM_APP_RECORDOF           3034
  %IDM_APP_SHOW               3035
  '
  %IDM_APP_CONS0              3040
  %IDM_APP_CONS1              3041
  %IDM_APP_FINDED             3042
  '
  sub SetMenuTicks()
  ==================
  int a,b,c,d
  if cdll
    a=MF_UNCHECKED
    b=MF_CHECKED
  else
    a=MF_CHECKED
    b=MF_UNCHECKED
  end if
  if csd then d=MF_CHECKED
  if projp then c=MF_CHECKED
  CheckMenuItem hMenu,IDM_COMPILE_EXE, a
  CheckMenuItem hMenu,IDM_COMPILE_DLL, b
  CheckMenuItem hMenu,IDM_COMPILE_TO_DIR, d
  CheckMenuItem hMenu,IDM_SHOW_PROJ,   c
  end sub
    '
  sub BuildMenus()
  '===============
  indexbase 0
  def apm AppendMenu hMenuPopup,
  def aps AppendMenu hMenuPopup,MF_STRING,
  '
  hMenu = CreateMenu
  hMenuPopup = CreateMenu
  aps IDM_FILE_NEW,     "&New"
  aps IDM_FILE_OPEN,    "&Open" tab "ctrl-o  F8"
  aps IDM_FILE_SAVE,    "&Save" tab "ctrl-s  F9"
  aps IDM_FILE_SAVE_AS, "Save &As..." tab "shift-F9"
  apm MF_SEPARATOR, 0,                null
  aps IDM_SHOW_PROJ,    "Show Project Panel"
  aps IDM_APP_CONS0,    "System Console" tab "F7"
  aps IDM_APP_CONS1,    "OxyConsole" tab "shift-F7"
  aps IDM_APP_FINDED,   "Peroxide" tab "Ctrl-F7"
  apm MF_SEPARATOR, 0,  null
  aps IDM_APP_EXIT,     "E&xit" tab "Alt-F4"
  AppendMenu hMenu,      MF_POPUP,     hMenuPopup,       "&File"
  '
  hMenuPopup = CreateMenu
  aps IDM_EDIT_UNDO,      "&Undo" tab "ctrl-z"
  apm MF_SEPARATOR, 0,                null
  aps IDM_EDIT_CUT,       "Cu&t" tab "ctrl-x"
  aps IDM_EDIT_COPY,      "&Copy" tab "ctrl-c"
  aps IDM_EDIT_PASTE,     "&Paste" tab "ctrl-v"
  aps IDM_EDIT_FIND,      "&Find" tab "ctrl-f"
  aps IDM_EDIT_REPLACE,   "&Replace" tab "ctrl-r"
  aps IDM_EDIT_REPLACE_IN_BLOCK, "Replace in block" tab
  aps IDM_EDIT_SELECT_ALL,"Select &All" tab "ctrl-a"
  aps IDM_EDIT_TIME,      "Date/Time Stamp" tab "ctrl-t"
  AppendMenu hMenu,      MF_POPUP,     hMenuPopup,       "&Edit"
  '
  hMenuPopup = CreateMenu
  aps IDM_EXECUTE32,       "Run 32bit directly" tab "ctrl-e  F5"
  aps IDM_EXECUTE64,       "Run 64bit directly" tab "shift-F5"
  aps IDM_COMPILE_RTL32, "Compile and Run Binary32" tab "ctrl-F5"
  aps IDM_COMPILE_RTL64, "Compile and Run Binary64" tab "ctrl-shift-F5"
  apm MF_SEPARATOR, 0,   null
  aps IDM_COMPILE_EXE,   "Compile EXE" tab
  aps IDM_COMPILE_DLL,   "Compile DLL" tab
  aps IDM_COMPILE_TO_DIR,"File to source folder" tab
  AppendMenu hMenu,      MF_POPUP,     hMenuPopup,        "&Compile"
  '
  if sprj then
    int a,b,fm,i,le
    string ca
    b=1
    do
      a=instr(b,sprj,"$command ")
      if a=0 then exit do
      if fm=0 then
        hMenuPopup = CreateMenu
        fm=1
      end if
      b=a
      a=instr b,sprj,"["
      b+=9
      ca=mid(sprj,b,a-b)
      ca=rtrim ca
      ProjCommand[i]=ExtractBr(sprj,a,b,le)
      aps i+3100,ca
      i++
    end do
    b=1
    do
      a=instr(b,sprj,"<[")
      if a=0 then exit do
      b=instr(a,sprj,"]>")
      if b=0 then exit do
      ca=mid(sprj,a,b-a+2)
      ProjCommand[i]=ca
      aps i+3100,mid(sprj,a+2,b-a-2)
      i++
    end do
    if fm then 
      AppendMenu hMenu,      MF_POPUP,     hMenuPopup,        "&Project"
    end if
  end if
  '
  hMenuPopup = CreateMenu
  aps IDM_APP_HELP_OXYGEN,"Oxygen Help" tab "F1"
  aps IDM_APP_HELP_OXIDE, "OxIde Help" tab "shift-F1"
  aps IDM_APP_ABOUT,      "About" tab "ctrl-F1"
  apm MF_SEPARATOR, 0,    null
  aps IDM_APP_LIST,       "List Symbol List" tab "F2"
  aps IDM_APP_RECORDOF,   "Record of" tab "shift-F2"
  aps IDM_APP_SHOW,       "Show Encoding" tab "ctrl-F2"
  AppendMenu hMenu,      MF_POPUP,     hMenuPopup,         "&Help"
  end sub 'BuildMenus

  sub MainMenus(sys hwnd)
  =======================
    if hmenu then DestroyMenu hMenu
    BuildMenus
    SetMenu hwnd, hMenu
    SetMenuTicks
  end sub

  sub UpdateProjMenu()
  ====================
  indexbase 0
  sprj=GetText hchw[5]
  if sprj then
    MainMenus hwndMain
  end if
  end sub

  sub UpdateProject(string*fprj)
  ==============================
  UpdateProjMenu
  UpdateProjFile fprj
  end sub


  function WndProc(sys hwnd,uMsg,wParam,lParam) as sys callback
  =============================================================
  '
  '
  indexbase 0
  '
  static string  fb,fi,fnt,fo,dir
  static string  s,t,f,wr
  static char    buf[256]
  static RECT    rc
  static sys     a,b,c,d,h,i,v,w
  static sys     hw
  static int     sx,sy,px,py,lx,ly,nx,ny,id,idmenu,style,le,lw
  static sys     hfont[4],ps
  ' 
  select umsg 
  '
  case WM_CREATE
  ==============

    sci= LoadLibrary "lib/SciLexer.dll"
    if not sci then
      print "Unable to load Scintilla: SciLexer.dll"
    end if

    GetCurrentDirectory 256, buf
    o2dir=AppExePath(1)
    s=getfile o2dir+"\Oxide.cfg"
    o2Args=UnQuote CommandLineArgs
    if o2args then
      dir=ExtractDirName o2args
    else
      dir=buf
    end if
    '
    if o2args
      if not instr lcase(o2args),"oxide.o2bas" then
        if mid(o2args,-7,7)=".o2proj" then
          fprj=o2args
          dir=SetDirUsingPath fprj
          projp=1 : iu=1
        else
          s=""
          fi=o2args
        end if
      end if
    end if
    '
    if s
      dir=ExtractData s,  "$dir "
      fi=ExtractData s,   "$file "
      ps=ExtractData s,   "$pos "
      sx=ExtractData s,   "$sizeX "
      sy=ExtractData s,   "$sizeY "
      fnt=ExtractData s,  "$font "
      fnz=ExtractData s,  "$fontsize "
    else
      sx=800
      sy=600
      fnt="Lucida Console" ' Courier
      fnz=12
      dir=o2dir
    end if
    GetWindowRect(hwnd,@rc)
    MoveWindow(hwnd,rc.left,rc.top,sx,sy,1)
    '
    'if fi="" then fi=o2dir+"\t.o2bas" : dir=o2dir
    '
    if fprj then
      dir=SetDirUsingPath fprj
      projp=1 : iu=1
      fi=""
      s=""
    else
      s=GetOxyFile fi
      le=len s
      if le=0 then fi=""
    end if
    '
    SetCurrentDirectory dir
    idmenu=0
    id=256
    '
    int Editstyle=WS_CHILD | WS_BORDER | WS_VISIBLE
    '
    int FullEditStyle= EditStyle |
    ES_MULTILINE   | ES_WANTRETURN  | ES_NOHIDESEL |
    ES_AUTOHSCROLL | ES_AUTOVSCROLL | WS_HSCROLL   | WS_VSCROLL
    '
    macro EditW(i,style)
    hchw(i)=CreateWindowEx(0,"edit",null,style,0,0,0,0,hwnd,id+i,hinst,null)
    end macro
    '
    EditW 1,Editstyle      'FIND
    EditW 2,EditStyle      'REPLACE
    EditW 3,EditStyle      'LINE
    EditW 4,EditStyle      'SYMBOL SEARCH
    EditW 5,FullEditStyle  'PROJECT PANEL
    nchw=6
    EditW 6, FullEditStyle 'INFO PANEL
    '
    '
    'USE SCINTILLA
    if sci then
      hchw(0) = CreateWindowEx(0,"Scintilla",null,EditStyle,0,0,0,0,hwnd,id+0,hinst,null)
     'hchw(5) = CreateWindowEx(0,"Scintilla",null,EditStyle,0,0,0,0,hwnd,id+5,hinst,null)
     'hchw(6) = CreateWindowEx(0,"Scintilla",null,EditStyle,0,0,0,0,hwnd,id+6,hinst,null)
      hsci=hchw[0]
      % SCLEX_OBASIC        40
      '
      def sm SendMessage hsci,
      '
     'http://www.scintilla.org/ScintillaDoc.html
     'http://search.cpan.org/dist/Wx-Scintilla/lib/Wx/Scintilla/Constant.pm#Lexical_states_for_SCLEX_CPP
     'http://www.garybeene.com/code/gbsnippets_gbs_00663.htm
      '
      '----Roland's Customised Scintilla Lexer 3.61----
      '
      % SCLEX_OBASIC        40
      '
      def sm SendMessage hsci,
      '
     '3  cpp
     '8  vb ***
     '40 lout
     '51 powerbasic
     '75 freebasic
      sm SCI_SETLEXER, 200,0    'Oxygenbasic lexer
      include "oxykeywords.inc"
      sm SCI_SETKEYWORDS, 0, strptr OxyKeywords
      '
     'sm SCI_STYLESETFORE, 32,0x999999
     'sm SCI_STYLESETBACK,32,0xFFFFFF  'white
     'sm SCI_STYLESETBACK,32,0x000000  'black
     'sm SCI_STYLECLEARALL, 0, 1
      for i=0 to 31
        sm SCI_STYLESETFONT, i, fnt 'Courier / Lucida Console
        sm SCI_STYLESETSIZE, i, fnz '10..14 point size range
      next
      '
      sm SCI_SETMARGINTYPEN,  0, SC_MARGIN_NUMBER
      sm SCI_SETMARGINWIDTHN, 0, 30
      sm SCI_SETCARETLINEBACK,0xffeeee,0
      sm SCI_SETCARETFORE,    0xff4444,0
      sm SCI_SETCARETLINEVISIBLE,1,0
      '
      def sn SendMessage hsci, SCI_STYLESETFORE,
      '
      'setting RGB colors
      sn 0,  0xff0000  '
      sn 1,  0x207800  'line comment
      sn 2,  0x006000  'numbers
      sn 3,  0xd00000  'keywords
      sn 4,  0x008800  'string
      sn 5,  0xc00080  '#names
      sn 6,  0x000088  'symbols
      sn 7,  0x002000  '

      sn 23,  0x207800 'block comment
      sn 24,  0x207800 'commentline //
      '
      SetText hsci,s

      '
    else 'NOT SCINTILLA
      '
      EditW 0,FullEditStyle
      hFont[1]=SetFont(hchw[0],1,fnz,fnz-3,400,fnt)
      SetText hw,s
      SendMessage hw,EM_SETSEL,ps,ps
      SendMessage hw,0xc5,0,0 'EM_LIMITTEXT = &HC5 limit for XP.. 7fffffff (before: XP 7fff)
    end if
    hw=hchw[0]
    SetWindowText hwnd,ClipName(fi)
    '
    if fprj then
      sprj=GetOxyFile  fprj
      SetText hchw[5],sprj
    end if
    '
    MainMenus hwnd
    '
    hFont[2]=SetFont(hchw[1],6,13,9,400,"Lucida console")

    SetFocus hWnd
    SetFocus hw
    '
    fo=fi

  case WM_MOUSEMOVE
  ===================
  mposx=loword wparam
  mposy=hiword wparam

  case WM_COMMAND
  ===============
    sys id,co,jd,mo
    id=wparam and 0xffff
    jd=id
    co=wparam >>16
    if GetAsyncKeyState(VK_SHIFT)   then mo=1
    if GetAsyncKeyState(VK_CONTROL) then mo or=2
    '
    select id
    '
    case IDM_FILE_NEW        : jd=1010 : mo=0
    case IDM_FILE_OPEN       : jd=1008 : mo=1
    case IDM_FILE_SAVE       : jd=1009 : mo=0
    case IDM_FILE_SAVE_AS    : jd=1009 : mo=1
    case IDM_SHOW_PROJ       : projp=1-projp : iu=1
    case IDM_APP_EXIT        : SendMessage hwnd,WM_CLOSE,0,0
    '
    case IDM_EDIT_UNDO       : sendm hsci,EM_UNDO,SCI_UNDO
    case IDM_EDIT_CUT        : sendm hsci,0,SCI_CUT
    case IDM_EDIT_COPY       : sendm hsci,0,SCI_COPY
    case IDM_EDIT_PASTE      : sendm hsci,0,SCI_PASTE
    case IDM_EDIT_SELECT_ALL : jd=2065 :
    case IDM_EDIT_TIME       : jd=2084 :
    case IDM_EDIT_FIND       : jd=1003 : mo=2
    case IDM_EDIT_REPLACE    : jd=1004 : mo=2
    case %IDM_EDIT_REPLACE_IN_BLOCK : jd=1020
    '
    case IDM_APP_CONS0       : jd=1007 : mo=0
    case IDM_APP_CONS1       : jd=1007 : mo=1
    case IDM_APP_FINDED      : jd=1007 : mo=2
    '
    case IDM_EXECUTE32       : jd=1005 : mo=0
    case IDM_EXECUTE64       : jd=1005 : mo=1
    case IDM_COMPILE_RTL32   : jd=1005 : mo=2
    case IDM_COMPILE_RTL64   : jd=1005 : mo=3
    case IDM_COMPILE_EXE     : cdll=0  : SetMenuTicks
    case IDM_COMPILE_DLL     : cdll=1  : SetMenuTicks
    case IDM_COMPILE_TO_DIR  : csd=1-csd : SetMenuTicks
    '
    case IDM_APP_HELP_OXYGEN : jd=1001 : mo=0
    case IDM_APP_HELP_OXIDE  : jd=1001 : mo=1
    case IDM_APP_ABOUT       : jd=1001 : mo=2
    case IDM_APP_LIST        : jd=1002 : mo=0
    case IDM_APP_RECORDOF    : jd=1002 : mo=1
    case IDM_APP_SHOW        : jd=1002 : mo=2
    '
    case 3100 to 3199 ' project commands
      s=GetText hchw[0]
      a=MayPutFile fi,s,"Performing Project Command"
      if a=IDCANCEL then exit function
      a=id-3100
      s=ProjCommand[a]
      if asc(s)<>60 then
        if not s then
          DOS ""
        else
          PutOxyFile "_.bat", s
          DOS "_.bat"
        end if
      else
        SwitchFile s,fi,fo
        SetFocus hw
      end if
      '----
    end select
    '
    'int dis=MF_DISABLED or MF_GRAYED
    'CheckMenuItem hMenu,IDM_FILE_NEW, MF_CHECKED
    'EnableMenuItem hMenu,IDM_FILE_OPEN,dis
    '
    Commands:
    ==========

    select jd
    case 0
      '
    case 1001 'F1
      if mo=1
        s=GetText hchw[0]
        HelpMessageBox
      elseif mo=2
        MessageBox 0,about,"OxIde",MB_ICONINFORMATION
      else
        scope
        sys hh
        int st,en,le,lw
        sys hh=GetFocus
        s=GetText hh
        le=len s
        SendMessage hh,EM_GETSEL,@st,@en
        st++ : en++
        string wd=ltrim ExtractWord(s,st,en,lw)
        '
        if not len wd then
          cm="hh.exe "+o2dir+"\inf\oxygenBasic_Manual.chm"
        else
          wd=filenameHTM(wd)
          cm="hh.exe "+o2dir+"\inf\OxygenBasic_Manual.chm::/"+wd
          'cm="hh.exe "+o2dir+"\inf\OxygenBasic_Manual.chm::/wordlink.htm#"+wd
        end if
        Exec cm
        end scope
      end if
   case 1002 'F2
      '#RECORDOF / #SHOW / #LIST
       CompilerInfo mo
   case 1003 'F3
      FindReplace hw,hchw[1], 0, mo
      SetWindowText hWnd, "Find"
    case 1004 'F4
      FindReplace hw,hchw[1], 0, mo,hchw[2]
      SetWindowText hWnd, "Find & Replace"
    case 1005 'F5
      scope
      string fp,fx,dsdll,cmsg, m, md
      int    isdll=cdll 'MENU SET DLL FLAG
      if fi=""
        'SendMessage hwnd,WM_COMMAND,1009,0 'save
        fi=o2dir+"\_.o2bas"
      end if
      if fi
        s=GetText hw
        if len(s) then
          MayPutFile fi,s 'SAVE CURRENT FILE
          f=fi
        end if
        a=1
        if fprj then
          sprj=GetText hchw[5]
          fx=OxyPath ExtractData sprj,"$binary "
          wr=lcase ExtractDotName fi
          if instr(" .bas .o2bas ",wr)=0 then
            fp=Oxypath ExtractData sprj,"$main "
            if not SameName(fp,fi) then
              a=0
              s=GetFile fp 'USE MAIN SOURCE
              f=fp
            end if
          end if
        end if
        if len(s) then
          m="!"
          fb=o2dir+"\_"
          string sdll=lcase(mid(fx,-4,4))
          if fx="" then
            if csd then
              fx=oxypath rtrimdotname fi 'COMPILE INTO SOURCE FOLDER
            else
              fx=fb 'DEFAULT COMPILE INTO OXY ROOT FOLDER
            end if
          elseif sdll=".exe"
            isdll=0 'OVERRIDE MENU SETTING
            fx=rtrimdotname fx
          elseif sdll=".dll"
            fx=rtrimdotname fx
            isdll=1 'OVERRIDE MENU SETTING
          end if
          '
          'do not use if RTLs are already included
          string cmo=""
          if mo and 2 'CTRL
            'TO CREATE INDEPENDENT BINARY
            '
            if isdll then
              m=fx+".dll"
              'md="$ DLL"+cr+"$filename "+qu+m+qu+cr
              'md=qu+m+qu
              cmo+="-d "
            else
              m=fx+".exe"
              'md="$filename "+qu+m+qu+cr
              'md=qu+m+qu
              cmo+="-r "
            end if
            '
            if mo and 1 'SHIFT
              t="RTL64" 'CTRL-SHIFT-F5
              m+=" (64bit)"
              cmo+="-64 "
            else 'NOT SHIFT
              t="RTL32" 'CTRL-F5
              m+=" (32bit)"
              cmo+="-32 "
            end if
            '
            's=md+"uses "+t+cr+"uses "+f+cr
            cmsg="Created independent " m
            m=""
          end if 'CTRL

          'SELECT COMPILER
          if mo=1 'SHIFT ONLY
            s="\co2m64.exe" 'CO2M64 64BIT COMPILING
          else '
            s="\co2.exe" 'CO2 COMPILING
            's="\gxo2.exe" 'GXO2 COMPILING
          endif
          'RUN COMPILER AS A SEPARATE PROCESS
          'Exec qu+o2dir+s+qu+" "+qu+OxyPath(f)+qu
          's=qu+o2dir+s+qu+" "+cmo+qu+OxyPath(f)+qu
          'Exec(s)
          QuExec( o2dir+s, cmo, OxyPath(f) )
        end if
      end if
      end scope
    case 1007 'F7
      if mo=1 then
        SetCurrentDirectory o2dir
        Exec "co2.exe tools/OxCommand.o2bas"
        SetCurrentDirectory dir
      elseif mo=2 then
        SetCurrentDirectory o2dir
        Exec "co2.exe tools/Peroxide.o2bas"
        SetCurrentDirectory dir
      else
        DOS ""
      end if
    case 1010 'NEW FILE
      s=GetText hw
      a=MayPutFile fo,s,"Creating New File"
      if a=IDCANCEL then exit function
      fi=""
      fo=""
      SetText hw,""
      SetWindowText hwnd,"New Code"
    case 1008 'F8 LOAD FILE
      s=GetText hw
      a=MayPutFile fo,s,"Loading Another File"
      if a=IDCANCEL then exit function
      f=GetFileName fi,0
      if f then
        fi=f
        fo=f
        LoadNewFile fi
      end if
      SetWindowText hwnd,"Loaded " ClipName(f)
    case 1009 'F9 SAVE FILE
      RecordLineNumber fo
      UpdateProject fprj
      f=fo
      if f="" or mo=1 'NEW FILE / SAVE AS
        f=GetFileName fo,1
        if f then
          if instr(f,".")=0 then f+=".o2bas"
        end if
      end if
      if f then
        fo=f
        fi=f
        s=GetText hw
        PutOxyFile fo,s
        SetWindowText hWnd, "Saved "+ClipName(f)
      end if
    case 1020 'FIND REPLACE IN BLOCK
      FindReplaceInBlock hchw[0],hchw[1],hchw[2]
    case 1103 'LEFT DOUBLE CLICK
      if lparam=hchw[5]
        SwitchFile "",fi,fo,1
      end if
      SetFocus hw
    case 2227 'Esc
      SetFocus hw
    case 2065 'Ctrl A
      SelectAll hw
    case 2070 'Ctrl F
      'SendMessage hWnd, WM_COMMAND,1003,0 'FIND
       SelectAll hchw[1]
       SetFocus hchw[1]
    case 2082 'Ctrl R
      'SendMessage hWnd, WM_COMMAND,1004,0 'FIND/REPLACE
      SelectAll hchw[2]
      SetFocus hchw[2]
    case 2071 'Ctrl G
      FindLineNumber hw,hchw[3]
      SelectAll hchw[3]
    case 2069 'Ctrl E
      jd=1005 : mo=0 : goto Commands 'EXECUTE
    case 2079 'Ctrl O
      SendMessage hWnd, WM_COMMAND,1008,0 'OPEN 'LOAD
    case 2083 'Ctrl S
      SendMessage hWnd, WM_COMMAND,1009,0 'SAVE
    case 2084 'Ctrl T
      h=GetFocus()
      s=GetText h
      SendMessage h,EM_GETSEL,@a,@b
      wr=TimeStr()
      s=left(s,a)+left(wr,19)+mid(s,b+1) 'TIMESTAMP
      SetText h,s
      a=a+len wr : b=a
      SendMessage h,EM_SETSEL,a,b    
    case 2013 'VK_ENTER
      h=GetFocus()
      select h
      case hchw[1]
        FindReplace hw,h,0, mo
        SetFocus h
      case hchw[2]
        FindReplace hw,h,0, mo,hchw[2]
        SetFocus h
      case hchw[3]
        ReadGotoLine hsci,h
        SetFocus hsci
      case hchw[4]
        iw=1
        iu=1
        s=GetText h
        if s then CompilerInfo 0,s else iw=0
      end select
    end select

  case WM_ERASEBKGND
  ==================
    return 0

  case WM_DROPFILES
  =================
    'DRAG AND DROP
    scope
    int a,b,lo,w,v,x,y
    GetClientWHXY hWnd,w,v,x,y
    string fl = GetDropFiles(wparam)
    DragFinish(wparam)
    lo=len o2dir
    '
    NextItem:
    '
    f=rtrim GetRestOfLine fl,b
    if left(f,lo)=o2dir then f="$"+mid(f,lo+1)
    '
    if mid(f,-7,7)=".o2proj" then
      fprj=f
      s=GetOxyFile fprj
      projp=1 : iu=1
      SetText hchw[5],s
      SetFocus hwnd
      dir=SetDirUsingPath fprj
      UpdateProjMenu
    elseif (x<w)and(projp=1) then
      'get sprj and append f
      string fn=ExtractFileName f
      sprj=GetText hchw[5]
      a=instr 1,sprj,"$files"
      if not a then
        fprj=rtrimdotname(f)+".o2proj"
        sprj=GetOxyFile fprj 'TRY TO GET EXISTING PROJ FILE
        a=instr 1,sprj,"$files"
        if a then 'VALID PROJECT
          SetText hchw[5],sprj
          exit function
        else 'CREATE A NEW PROJECT
          sprj="$project"+cr+
          "$files"+cr
          a=len(sprj)-1
          sprj+=cr+"$main ["+f+"]"+cr+cr+
          "$binary ["+RtrimDotName(f)+"]"+cr+cr+
          "$command "+"Dir Example"+cr+"["+cr+"dir"+cr+"pause"+cr+"]"+cr+cr
        end if
      end if
      a=instr(a,sprj,cr)
      sprj=left(sprj,a+1)+"<["+fn+"]>"+tab+"00001 "+" ["+f+"]"+cr+
      mid sprj,a+2
      SetText hchw[5],sprj
      if b<len(fl) then goto NextItem
      UpdateProject fprj
    else
      s=GetText hchw(0)
      a=MayPutFile fo,s,"Switching To Another File"
      if a=IDCANCEL then exit function
      fi=f
      fo=f
      dir=SetDirUsingPath fi
      LoadNewFile fi
      SetWindowText hwnd,"Loaded " ClipName(f)
      SetFocus hwnd
      SetFocus hw
      if y<40
        SendMessage hWnd,WM_COMMAND,1005,0 'Sim F5 Compile & Exec
      end if
    end if
    end scope
    '
   case WM_SIZE
  ============
    scope
    GetWindowRect(hwnd,@rc)
    sx=rc.right-rc.left
    sy=rc.bottom-rc.top
    GetClientRect(hwnd, @rc)
    w=rc.right*.25
    v=rc.bottom
    int u1=w, u2=w*3, u3=w*1.5
    if projp=0 then
      u1=0
      u2+=w
    end if
    if iw then 'INSERT INFO PANEL
      u2=u2-w*1.5
      MoveWindow hchw[6] , u1+u2 , 20 , u3 , v-20 , TRUE 'INFO PANEL
    else
      MoveWindow hchw[6] , 0 , 0 , 0 , 0 , TRUE 'INFO PANEL
    end if
    MoveWindow hchw[0] , u1     , 20 , u2    , v-20 , TRUE 'MAIN PANEL
    MoveWindow hchw[1] , 0      , 0  , w     , 20   , TRUE 'FINDER PANEL
    MoveWindow hchw[2] , w      , 0  , w     , 20   , TRUE 'REPLACER PANEL
    MoveWindow hchw[3] , w*2    , 0  , w     , 20   , TRUE 'LINE NUMBER PANEL
    MoveWindow hchw[4] , w*3    , 0  , w     , 20   , TRUE 'UNUSED PANEL
    MoveWindow hchw[5] , 0      , 20 , u1    , v-20 , TRUE 'PROJECT (LEFT) PANEL
    '
    if ps<>0 and hsci<>0 
      a=SendMessage hsci,SCI_LINEFROMPOSITION,ps,0
      SendMessage hsci,SCI_GOTOLINE,a+20,0
      SendMessage hsci,SCI_GOTOPOS,ps,0
      ps=0
    end if
    end scope

  case WM_CLOSE
  =============
    GetCurrentDirectory 256, buf
    s=GetText hw
    UpdateProjFile fprj
    a=MayPutFile fo,s,"Closing Oxide"
    if a=IDCANCEL then SetFocus hw : exit function
    if fprj then
      fo=""
      buf=""
      a=0
    else
      GetCurrentDirectory 256, buf
      SendMessage hw,EM_GETSEL,@a,@a
    end if
    s="$dir ["+buf+"]"+cr+
      "$file ["+fo+"]"+cr+
      "$pos [" a "]"+cr+
      "$sizeX [" sx "]" +cr+
      "$sizeY [" sy "]" +cr+
      "$font [" fnt "]"  +cr+
      "$fontsize [" fnz "]"  +cr
    putfile o2dir+"\oxide.cfg",s
    SendMessage hwnd,WM_DESTROY,0,0

  case WM_DESTROY
  ===============

    DeleteObject hfont[1]
    DeleteObject hfont[2]
    PostQuitMessage(0)

  case else
  =========
    return DefWindowProc(hwnd,uMsg,wParam,lParam)
    '
  end select
  if iu
    a=iu : iu=0 'RECURSION PROTECTION
    select a
    case 1
      'https://msdn.microsoft.com/en-us/library/windows/desktop/ms632646(v=vs.85).aspx
      SendMessage hwnd,WM_SIZE,0,0 'size params not read in this proc
    SetMenuTicks
    case 2 'SWITCH FILES
    end select
  end if
  '
  end function

