in the nearby future. This would give iup users access to a popular editing component
that comes with things like folding, syntax colouring, regular expression matching etc... etc....
The nearby future is... now :) This example is an almost exact replica of the C demo
written by the iup developer. It shows you how to create a scintilla control, select a lexer
(responsible for the all important folding and other syntax related things), change syntax colouring
and how to add some text to the control. Scintilla comes with a freebasic lexer but the visual
basic lexer included with scintilla seems to do a better job at folding/colourizing fb code.
I have chosen some wonderful colours for you to enjoy (liven up the demo a bit). I used
--> green (comments)
--> pink (operators)
--> turquoise(keywords1)
--> lime green (keywords2)
--> purple (strings)
A picture to illustrate
Colours can be changed easily (colors are set using the standard rgb notation).
For more info on using iup scintilla: http://www.tecgraf.puc-rio.br/iup/
Info on scintilla can be found at the scintilla site http://www.scintilla.org/
To compile/run the example you need the iup binaries. You can get those from sourceforge.net
http://sourceforge.net/projects/iup/fil ... s/Dynamic/
Choose the file called iup-3.8_win32_dll8_lib.zip
You will also need the msvcr80.dll runtime library for the demo to work (it's an iup dependency).
That should be sitting on the hdd of your pc somewhere (if not in syswow64 then somewhere
else). If you cannot find msvcr80.dll then you can download a package that contains it from
http://www.sarmardar.nl/FreeBASIC/iup_dll.zip.
Be sure NOT to use any of the other files in the zip file (iup-3.8_win32_dll8_lib.zip contains
everything you need except for msvcr80.dll).
Linux users could try and download binaries for their distro from
http://sourceforge.net/projects/iup/fil ... Libraries/
I do not think the example will compile/run on linux but perhaps I am being
overly negative here (on linux iup acts as a 'wrapper' around gtk2.x. gtk2.x
works well on linux so there should be no reason for the demo not to work on linux).
You do not need import libraries on either windows or linux as I have used
DyLibLoad, DyLibSymbol and DyLibFree (the dynamic link libraries ((the ones
ending with .dll (win) or .so (lin)) are all you need).
Code: Select all
#include "crt/stdlib.bi"
#include "crt/stdio.bi"
#include "crt/string.bi"
#ifndef NIL
#define NIL cast(any ptr,0)
#endif
#define IUP_IGNORE -1
#define IUP_DEFAULT -2
#define IUP_CLOSE -3
#define IUP_CONTINUE -4
type Ihandle as any
type Icallback as function cdecl (byval as Ihandle ptr) as integer
dim shared IupOpen as function cdecl (byval as integer ptr, byval as zstring ptr ptr ptr) as integer
dim shared IupSetCallback as function cdecl (byval as ihandle ptr, byval as zstring ptr, byval as icallback) as icallback
dim shared IupMainLoop as sub cdecl ()
dim shared IupClose as sub cdecl ()
dim shared IupGetAttributeId as function cdecl (byval as Ihandle ptr, byval as zstring ptr, byval as integer) as zstring ptr
dim shared IupGetAttribute as function cdecl (byval as Ihandle ptr, byval as zstring ptr) as zstring ptr
dim shared IupSetAttribute as sub cdecl (byval as Ihandle ptr, byval as zstring ptr, byval as zstring ptr)
dim shared IupSetfAttribute as sub cdecl (byval as Ihandle ptr, byval as zstring ptr, byval as zstring ptr, ...)
dim shared IupShow as function cdecl (byval as Ihandle ptr) as integer
dim shared IupScintillaOpen as sub cdecl ()
dim shared IupScintilla as function cdecl () as Ihandle ptr
dim shared IupDialog as function cdecl (byval as Ihandle ptr) as ihandle ptr
dim shared IupVbox as function cdecl (byval as Ihandle ptr, ...) as ihandle ptr
dim shared sampleCode as string * 1708 = _
!"enum FB_TKCLASS\n" +_
!" FB_TKCLASS_IDENTIFIER\n" +_
!" FB_TKCLASS_KEYWORD\n" +_
!" FB_TKCLASS_QUIRKWD\n" +_
!" FB_TKCLASS_NUMLITERAL\n" +_
!" FB_TKCLASS_STRLITERAL\n" +_
!" FB_TKCLASS_OPERATOR\n" +_
!" FB_TKCLASS_DELIMITER\n" +_
!" FB_TKCLASS_UNKNOWN\n" +_
!"end enum\n" +_
!"\n" +_
!"\'\':::::\n" +_
!"sub symbSetDefType _\n" +_
!" ( _\n" +_
!" byval ichar as integer, _\n" +_
!" byval echar as integer, _\n" +_
!" byval dtype as integer _\n" +_
!" )\n" +_
!"\n" +_
!" dim as integer i = any\n" +_
!"\n" +_
!" if( ichar < asc(\"A\") ) then\n" +_
!" ichar = asc(\"A\")\n" +_
!" elseif( ichar > asc(\"_\") ) then\n" +_
!" ichar = asc(\"_\")\n" +_
!" end if\n" +_
!"\n" +_
!" if( echar < asc(\"A\") ) then\n" +_
!" echar = asc(\"A\")\n" +_
!" elseif( echar > asc(\"_\") ) then\n" +_
!" echar = asc(\"_\")\n" +_
!" end if\n" +_
!"\n" +_
!" if( ichar > echar ) then\n" +_
!" swap ichar, echar\n" +_
!" end if\n" +_
!"\n" +_
!" for i = ichar to echar\n" +_
!" deftypeTB(i - asc(\"A\")) = dtype\n" +_
!" next\n" +_
!"\n" +_
!"end sub\n" +_
!"\n" +_
!"\'\' Recalculate the length, to be used after the symbol's type was set/changed\n" +_
!"sub symbRecalcLen( byval sym as FBSYMBOL ptr )\n" +_
!" if( sym->class = FB_SYMBCLASS_PARAM ) then\n" +_
!" sym->lgt = symbCalcParamLen( sym->typ, sym->subtype, sym->param.mode )\n" +_
!" else\n" +_
!" sym->lgt = symbCalcLen( sym->typ, sym->subtype )\n" +_
!" end if\n" +_
!"end sub\n" +_
!"\n" +_
!"dim shared s as string\n" +_
!"s = \"hello world\"\n" +_
!"type simple_type\n" +_
!" dim field1 as integer\n" +_
!" dim field2 as integer\n" +_
!"end type\n"
function marginclick_cb cdecl(byval self as Ihandle ptr, byval margin as integer, byval line_ as integer, byval status as zstring ptr) as integer
printf(!"MARGINCLICK_CB(Margin: %d, Line: %d, Status:%s)\n", margin, line_, status)
printf(!"Fold Level = %s\n", IupGetAttributeId(self, "FOLDLEVEL", line_))
IupSetfAttribute(self, "FOLDTOGGLE", "%d", line_)
return IUP_DEFAULT
end function
function hotspotclick_cb cdecl(byval self as Ihandle ptr, byval pos_ as integer, byval line_ as integer, byval col as integer, byval status as zstring ptr) as integer
dim text as zstring ptr = IupGetAttributeId(self, "LINE", line_)
printf(!"HOTSPOTCLICK_CB (Pos: %d, Line: %d, Col: %d, Status:%s)\n", pos_, line_, col, status)
printf(!" line text = %s\n", text)
return IUP_DEFAULT
end function
function button_cb cdecl(byval self as Ihandle ptr, byval button as integer, byval pressed as integer, byval x as integer, byval y as integer, byval status as zstring ptr) as integer
printf(!"BUTTON_CB = button: %d, pressed: %d, x: %d, y: %d, status: %s\n", button, pressed, x, y, status)
return IUP_DEFAULT
end function
function motion_cb cdecl(byval self as Ihandle ptr, byval x as integer, byval y as integer, byval status as zstring ptr) as integer
printf(!"MOTION_CB = x: %d, y: %d, status: %s\n", x, y, status)
return IUP_DEFAULT
end function
function caret_cb cdecl (byval self as Ihandle ptr, byval lin as integer, byval col as integer, byval pos_ as integer) as integer
printf(!"CARET_CB = lin: %d, col: %d, pos_: %d\n", lin, col, pos_)
return IUP_DEFAULT
end function
function valuechanged_cb cdecl(byval self as Ihandle ptr) as integer
printf(!"VALUECHANGED_CB\n")
return IUP_DEFAULT
end function
function action_cb cdecl (byval self as Ihandle ptr, byval insert as integer, byval pos_ as integer, byval length as integer, byval text as zstring ptr) as integer
printf(!"ACTION = insert: %d, pos_: %d, lenght:%d, text: %s\n", insert, pos_, length, text)
return IUP_IGNORE
end function
sub set_attribs (byval sci as Ihandle ptr)
IupSetAttribute(sci, "CLEARALL", "")
IupSetAttribute(sci, "LEXERLANGUAGE", "vb")
IupSetAttribute(sci, "KEYWORDS1", "sub function declare dim end type union as ptr enum "_
"if elseif then select case default break goto return for while do continue sizeof typeof new delete property namespace operator this cast using andalso orelse xor or and not cdecl byval byref ")
IupSetAttribute(sci,"KEYWORDS2","integer long ulong ulongint longint string zstring wstring short ushort ubyte double single unsigned signed")
IupSetAttribute(sci, "STYLEFONT32", "Consolas")
IupSetAttribute(sci, "STYLEFONTSIZE32", "11")
IupSetAttribute(sci, "STYLECLEARALL", "Yes") /' sets all styles to have the same attributes as 32 '/
IupSetAttribute(sci, "STYLEFGCOLOR1", "0 128 0") '' 1-BASIC comment
IupSetAttribute(sci, "STYLEFGCOLOR2", "0 128 0") '' 2-BASIC comment line
IupSetAttribute(sci, "STYLEFGCOLOR4", "139 0 139") '' 4-String
IupSetAttribute(sci, "STYLEFGCOLOR5", "0 128 128") ''
IupSetAttribute(sci, "STYLEFGCOLOR6", "255 20 147") ''6-Operators
IupSetAttribute(sci, "STYLEFGCOLOR7", "0 0 0") '' 7-Character
IupSetAttribute(sci, "STYLEFGCOLOR9", "205 149 12") '' 9-Preprocessor block
IupSetAttribute(sci, "STYLEFGCOLOR10", "0 197 205") '' 10-Keywords1
IupSetAttribute(sci, "STYLEFGCOLOR11", "0 255 0") '' 11-Keywords2
IupSetAttribute(sci, "STYLEBOLD10", "YES")
IupSetAttribute(sci, "STYLEHOTSPOT4", "YES")
IupSetAttribute(sci, "STYLEHOTSPOT11", "YES")
IupSetAttribute(sci, "INSERT0", sampleCode)
IupSetAttribute(sci, "MARGINWIDTH0", "50")
if (1) then
IupSetAttribute(sci, "PROPERTY", "fold=1")
IupSetAttribute(sci, "PROPERTY", "fold.compact=0")
IupSetAttribute(sci, "PROPERTY", "fold.comment=1")
IupSetAttribute(sci, "PROPERTY", "fold.preprocessor=1")
IupSetAttribute(sci, "MARGINWIDTH1", "20")
IupSetAttribute(sci, "MARGINTYPE1", "SYMBOL")
IupSetAttribute(sci, "MARGINMASKFOLDERS1", "Yes")
IupSetAttribute(sci, "MARKERDEFINE", "FOLDER=PLUS")
IupSetAttribute(sci, "MARKERDEFINE", "FOLDEROPEN=MINUS")
IupSetAttribute(sci, "MARKERDEFINE", "FOLDEREND=EMPTY")
IupSetAttribute(sci, "MARKERDEFINE", "FOLDERMIDTAIL=EMPTY")
IupSetAttribute(sci, "MARKERDEFINE", "FOLDEROPENMID=EMPTY")
IupSetAttribute(sci, "MARKERDEFINE", "FOLDERSUB=EMPTY")
IupSetAttribute(sci, "MARKERDEFINE", "FOLDERTAIL=EMPTY")
IupSetAttribute(sci, "FOLDFLAGS", "LINEAFTER_CONTRACTED")
IupSetAttribute(sci, "MARGINSENSITIVE1", "YES")
end if
printf(!"Number of chars in this text: %s\n", IupGetAttribute(sci, "COUNT"))
printf(!"Number of lines in this text: %s\n", IupGetAttribute(sci, "LINECOUNT"))
printf(!"%s\n", IupGetAttribute(sci, "LINEVALUE"))
end sub
sub ScintillaTest()
dim as Ihandle ptr dlg
dim as Ihandle ptr sci
IupScintillaOpen()
'' Creates an instance of the Scintilla control
sci = IupScintilla()
'' IupSetAttribute(sci, "VISIBLECOLUMNS", "80")
'' IupSetAttribute(sci, "VISIBLELINES", "40")
''IupSetAttribute(sci, "SCROLLBAR", "NO")
'' IupSetAttribute(sci, "BORDER", "NO")
IupSetAttribute(sci, "EXPAND", "Yes")
IupSetCallback(sci, "MARGINCLICK_CB", cast(icallback,@marginclick_cb))
IupSetCallback(sci, "HOTSPOTCLICK_CB", cast(icallback,@hotspotclick_cb))
'' IupSetCallback(sci, "BUTTON_CB", @button_cb)
'' IupSetCallback(sci, "MOTION_CB", @motion_cb)
IupSetCallback(sci, "CARET_CB", cast(icallback,@caret_cb))
IupSetCallback(sci, "VALUECHANGED_CB", cast(icallback,@valuechanged_cb))
IupSetCallback(sci, "ACTION", cast(icallback,@action_cb))
'' Creates a dialog containing the control
dlg = IupDialog(IupVbox(sci, NIL))
IupSetAttribute(dlg, "TITLE", "IupScintilla")
IupSetAttribute(dlg, "RASTERSIZE", "700x500")
IupSetAttribute(dlg, "MARGIN", "10x10")
'' Shows dialog
IupShow(dlg)
IupSetAttribute(dlg, "RASTERSIZE", NIL)
set_attribs(sci)
end sub
sub mymain()
''first set up the two dynamic link libraries
var iup = DyLibLoad("iup.dll")
if (iup = NIL) then
print "error loading iup"
return
end if
var scintilla = DyLibLoad("iup_scintilla.dll")
if (scintilla = NIL) then
print "error loading scintilla"
dylibfree(iup)
return
end if
IupOpen = DyLibSymbol(iup,"IupOpen")
IupSetCallback = DyLibSymbol(iup,"IupSetCallback")
IupMainLoop = DyLibSymbol(iup,"IupMainLoop")
IupClose = DyLibSymbol(iup,"IupClose")
IupSetAttribute = DyLibSymbol(iup,"IupSetAttribute")
IupShow = DyLibSymbol(iup,"IupShow")
IupScintilla = DyLibSymbol(scintilla,"IupScintilla")
IupScintillaOpen = DyLibSymbol(scintilla,"IupScintillaOpen")
IupSetCallback = DyLibSymbol(iup,"IupSetCallback")
IupSetfAttribute = DyLibSymbol(iup,"IupSetfAttribute")
IupDialog = DyLibSymbol(iup,"IupDialog")
IupGetAttributeId = DyLibSymbol(iup,"IupGetAttributeId")
IupGetAttribute = DyLibSymbol(iup,"IupGetAttribute")
IupVbox = DyLibSymbol(iup,"IupVbox")
IupOpen(NIL,NIL)
ScintillaTest()
IupMainLoop()
IupClose()
DyLibFree(iup)
DyLibFree(scintilla)
return
end sub
mymain()