#include once "windows.bi"
#define PLAT_WIN 1
#include "scintilla.bi"
#include "scilexer.bi"
DyLibLoad("SciLexer.dll")

#DEFINE RGBX(r,g,b) RGBA(b,g,r,0)

const black = RGBX(0,0,0)
const white = RGBX(&hff,&hff,&hff)
const red = RGBX(&hFF, 0, 0)
const offWhite = RGBX(&hFF, &hFB, &hF0)
const darkGreen = RGBX(0, &h80, 0)
const darkBlue = RGBX(0, 0, &h80)
const lightBlue = RGBX(&hA6, &hCA, &hF0)

declare function WinMain( byval hInstance as HINSTANCE, byval hPrevInstance as HINSTANCE, byval iCmdShow as integer ) as integer

end WinMain( GetModuleHandle( null ), null, SW_NORMAL )

Dim Shared As HWND hwndScintilla

Sub SetAStyle(style As Integer, fore As COLORREF = NULL, back As COLORREF = white, size As Integer = -1)
	SendMessage(hwndScintilla,SCI_STYLESETFORE, style, fore)
	SendMessage(hwndScintilla,SCI_STYLESETBACK, style, back)
	if size >= 1 then SendMessage(hwndScintilla,SCI_STYLESETSIZE, style, size)
End Sub

function WndProc ( byval hWnd as HWND, byval wMsg as UINT, byval wParam as WPARAM, byval lParam as LPARAM ) as LRESULT
    
    function = 0
    
    select case( wMsg )
    
        case WM_CREATE
            
            hwndScintilla = CreateWindowEx(0,"Scintilla","", WS_CHILD Or WS_VISIBLE Or WS_TABSTOP Or WS_CLIPCHILDREN,0,0,700,500,hWnd,NULL, GetModuleHandle(NULL),NULL)
                                    
            SendMessage(hwndScintilla,SCI_SETLEXER, SCLEX_LUA, NULL)
            SendMessage(hwndScintilla,SCI_SETSTYLEBITS, 7, NULL)
            
            SendMessage(hwndScintilla,SCI_SETMARGINTYPEN,1,SC_MARGIN_NUMBER )
            SendMessage(hwndScintilla,SCI_SETMARGINWIDTHN,1,48)
            
            SendMessage(hwndScintilla,SCI_SETKEYWORDS,0,Cast(LPARAM,@"and break do else elseif end false for function if in local nil not or repeat return then true until while"))
            SendMessage(hwndScintilla,SCI_SETKEYWORDS,1,Cast(LPARAM,@"_VERSION assert collectgarbage dofile error gcinfo loadfile loadstring print tonumber tostring type unpack _ALERT _ERRORMESSAGE _INPUT _PROMPT _OUTPUT _STDERR _STDIN _STDOUT call dostring foreach foreachi getn globals newtype rawget rawset require sort tinsert tremove _G getfenv getmetatable ipairs loadlib next pairs pcall rawegal rawget rawset require setfenv setmetatable xpcall string table math coroutine io os debug"))
            SendMessage(hwndScintilla,SCI_SETKEYWORDS,2,Cast(LPARAM,@"abs acos asin atan atan2 ceil cos deg exp floor format frexp gsub ldexp log log10 max min mod rad random randomseed sin sqrt strbyte strchar strfind strlen strlower strrep strsub strupper tan string.byte string.char string.dump string.find string.len string.lower string.rep string.sub string.upper string.format string.gfind string.gsub table.concat table.foreach table.foreachi table.getn table.sort table.insert table.remove table.setn math.abs math.acos math.asin math.atan math.atan2 math.ceil math.cos math.deg math.exp math.floor math.frexp math.ldexp math.log math.log10 math.max math.min math.mod math.pi math.rad math.random math.randomseed math.sin math.sqrt math.tan"))
            SendMessage(hwndScintilla,SCI_SETKEYWORDS,3,Cast(LPARAM,@"openfile closefile readfrom writeto appendto remove rename flush seek tmpfile tmpname read write clock date difftime execute exit getenv setlocale time coroutine.create coroutine.resume coroutine.status coroutine.wrap coroutine.yield io.close io.flush io.input io.lines io.open io.output io.read io.tmpfile io.type io.write io.stdin io.stdout io.stderr os.clock os.date os.difftime os.execute os.exit os.getenv os.remove os.rename os.setlocale os.time os.tmpname"))
            SendMessage(hwndScintilla,SCI_SETKEYWORDS,4,Cast(LPARAM,@"AddBody3D AddBody CircularVelocity SetInterval CircularOrbit SetScale SetHistoryLog"))

            SetAStyle(STYLE_DEFAULT, black, white, 12)
            SendMessage(hwndScintilla,SCI_STYLESETFONT, STYLE_DEFAULT, Cast(LPARAM,@"Courier New"))
            SendMessage(hwndScintilla,SCI_STYLECLEARALL, NULL, NULL)	'// Copies global style to all others
            
            SetAStyle(STYLE_LINENUMBER,white,RGBX(200,200,200),12)

            SetAStyle(SCE_LUA_STRING, RGBX(255,0,0))
            
            SetAStyle(SCE_LUA_OPERATOR, RGBX(0,128,192))
            SendMessage(hwndScintilla,SCI_STYLESETBOLD, SCE_LUA_OPERATOR, 1)
            
            SendMessage(hwndScintilla,SCI_STYLESETITALIC, SCE_LUA_COMMENT, 1)
            SendMessage(hwndScintilla,SCI_STYLESETFORE, SCE_LUA_COMMENT, Cast(LPARAM,RGBX(100,100,100)))
            SendMessage(hwndScintilla,SCI_STYLESETBACK, SCE_LUA_COMMENT, Cast(LPARAM,&HFFEFD0))
            SendMessage(hwndScintilla,SCI_STYLESETEOLFILLED, SCE_LUA_COMMENT, 1)
            
            SendMessage(hwndScintilla,SCI_STYLESETITALIC, SCE_LUA_COMMENTLINE, 1)
            SendMessage(hwndScintilla,SCI_STYLESETFORE, SCE_LUA_COMMENTLINE, Cast(LPARAM,RGBX(100,100,100)))
            SendMessage(hwndScintilla,SCI_STYLESETBACK, SCE_LUA_COMMENTLINE, Cast(LPARAM,&HFFEFD0))
            SendMessage(hwndScintilla,SCI_STYLESETEOLFILLED, SCE_LUA_COMMENTLINE, 1)
            
            SetAStyle(SCE_LUA_WORD, RGBX(0,0,255))
            SetAStyle(SCE_LUA_WORD5, RGBX(0,128,0))
            SendMessage(hwndScintilla,SCI_STYLESETBOLD, SCE_LUA_WORD, 1)
            
            SendMessage(hwndScintilla,SCI_STYLESETBOLD, SCE_LUA_WORD2, 1)
            SendMessage(hwndScintilla,SCI_STYLESETBOLD, SCE_LUA_WORD3, 1)
            SendMessage(hwndScintilla,SCI_STYLESETBOLD, SCE_LUA_WORD4, 1)
            SendMessage(hwndScintilla,SCI_STYLESETBOLD, SCE_LUA_WORD5, 1)
            
            SetAStyle(SCE_LUA_NUMBER, RGBX(0,0,255))
            
            Dim As Integer f = FREEFILE
            OPEN "script.lua" FOR BINARY AS #f
            Dim As String text = Space(LOF(f))
            GET #f, , text
            CLOSE #f

            SendMessage(hwndScintilla,SCI_SETTEXT,NULL,Cast(LPARAM,strptr(text)))
            SendMessage(hwndScintilla,SCI_COLOURISE,0,-1)
        
		case WM_SIZE
            MoveWindow(hwndScintilla,0,0,loword(lparam),hiword(lparam),TRUE)
    	case WM_DESTROY
            PostQuitMessage( 0 )
            exit function
    end select
    
    function = DefWindowProc( hWnd, wMsg, wParam, lParam )    
    
end function

'':::::
function WinMain ( byval hInstance as HINSTANCE, _
                   byval hPrevInstance as HINSTANCE, _
                   byval iCmdShow as integer ) as integer    
     
    dim wMsg as MSG
    dim wcls as WNDCLASS     
    dim hWnd as HWND
     
    function = 0
     
    with wcls
    	.style         = CS_HREDRAW or CS_VREDRAW
    	.lpfnWndProc   = @WndProc
    	.cbClsExtra    = 0
    	.cbWndExtra    = 0
    	.hInstance     = hInstance
    	.hIcon         = LoadIcon( NULL, IDI_APPLICATION )
    	.hCursor       = LoadCursor( NULL, IDC_ARROW )
    	.hbrBackground = GetStockObject( WHITE_BRUSH )
    	.lpszMenuName  = NULL
    	.lpszClassName = @"ScintillaDemo"
    end with
          
    if( RegisterClass( @wcls ) = FALSE ) then
       MessageBox( null, "Failed to register wcls", "Error", MB_ICONERROR )
       exit function
    end if
    
    hWnd = CreateWindowEx( 0, _
    			 		   @"ScintillaDemo", _
                           "Scintilla Demo", _
                           WS_OVERLAPPEDWINDOW, _
                           CW_USEDEFAULT, _
                           CW_USEDEFAULT, _
                           CW_USEDEFAULT, _
                           CW_USEDEFAULT, _
                           NULL, _
                           NULL, _
                           hInstance, _
                           NULL )
                          

    ShowWindow( hWnd, iCmdShow )
    UpdateWindow( hWnd )
     
    while( GetMessage( @wMsg, NULL, 0, 0 ) <> FALSE )    
        TranslateMessage( @wMsg )
        DispatchMessage( @wMsg )
    wend
    
    function = wMsg.wParam

end function
