PDA

View Full Version : maybe this is a cool idea



martin
19-05-2009, 08:11
Script idea:

User types in a ThinBasic keyword and then the user can choose from a list filled with filenames of sample-scripts (from C:\thinBasic\SampleScripts) that features this keyword. Click on filename and it opens the script.

I can make it by myself if i have some time or maybe this is a nice addition for the thinbasic tutorial

ErosOlmi
19-05-2009, 14:36
Idea is not bad.
Problem is time (as usual)

I would like to add also a "Tips & Tricks" area where to cover single specific tricks.

Michael Hartlef
19-05-2009, 19:39
This is similar to FIND IN FILES, something I wish thinAir would support.

martin
03-06-2009, 16:44
I created this script this afternoon. A nice tool if you want to learn more about a ThinBasic Keyword. I hope it's useful to other people too.


uses "ui","ll","file","os"

begin const
%btnSearch = 1
%textbox1 = 2
%listbox1 = 3
%label1 = 4
end const

dim Msg,wParam,lParam,tel AS LONG
dim WindowMain as dword
dim Keyword,tmpStr as string

function tbmain()
DIALOG New 0, "ThinBasic Keyword Finder by Martin Verlaan",-1,-1, 300, 320, %WS_CLIPCHILDREN or %WS_OVERLAPPEDWINDOW, 0 To WindowMain
DIALOG SHOW MODELESS WindowMain

Do while IsWindow(WindowMain)
Msg = GetMessage(WindowMain, wParam, lParam)
SELECT CASE Msg
CASE %WM_INITDIALOG
CONTROL ADD listbox, WindowMain, %listbox1,, 1,1,297, 280
CONTROL ADD BUTTON, WindowMain, %btnSearch, "Search", 212, 280, 40, 12
CONTROL ADD TEXTBOX, WindowMain, %textbox1, "", 10,280, 200, 12
CONTROL ADD LABEL, WindowMain, %label1, "Enter a keyword, click on the Search button and then double click on a file to view the script", 10, 295, 200, 20

CONTROL SET RESIZE WindowMain,%listbox1, 1,1,1,1
CONTROL SET RESIZE WindowMain,%btnSearch, 0,1,0,1
CONTROL SET RESIZE WindowMain,%textbox1, 0,1,0,1

CONTROL SET FOCUS WindowMain, %textbox1
CASE %WM_SIZING
DIALOG REDRAW WindowMain
CASE %WM_COMMAND
SELECT CASE lowrd(wParam)
case %Listbox1
select case HIwrd(wParam)
case %LBN_DBLCLK
LISTBOX GET TEXT WindowMain, %Listbox1 TO tmpStr
OS_Shell(app_path & "thinAir\thinair.exe " & app_path & "SampleScripts\" & tmpStr ,%OS_WNDSTYLE_NORMAL,%OS_SHELL_ASYNC)
end select
case %btnSearch
CONTROL GET TEXT WindowMain,%textbox1 TO Keyword
CONTROL DISABLE WindowMain, %btnSearch

LISTBOX RESET WindowMain, %listbox1

ListDir(app_path)
GetFiles(app_path)

DIALOG REDRAW WindowMain
CONTROL ENABLE WindowMain, %btnSearch
end select
CASE %WM_SYSCOMMAND
SELECT CASE wParam
CASE %SC_CLOSE
EXIT DO
END SELECT
END SELECT
Loop

DIALOG END WindowMain
end function

FUNCTION ListDir(ldir)
LOCAL Count,pList,nItems AS LONG
local xitem as string

pList = DIR_List(ldir, "*.*", %FILE_ADDPATH or %FILE_SUBDIR)
nItems = LL_Count(pList)

IF nItems > 0 THEN
FOR Count = 1 TO nItems
xItem = LL_GetItem(pList, Count)
GetFiles(xItem)
ListDir(xItem)
NEXT Count
END IF

LL_Free(pList)
END FUNCTION

FUNCTION GetFiles(ldir)
LOCAL Count,pList,nItems AS LONG
LOCAL xpath as string

pList = DIR_List(ldir, "*.tbasic", %FILE_NORMAL OR %FILE_ADDPATH)
nItems = LL_Count(pList)

IF nItems > 0 THEN
FOR Count = 1 TO nItems
xpath=LL_GetItem(pList, Count)
if instr(lcase$(file_load(xpath)),lcase$(keyword))>0 then
LISTBOX ADD WindowMain, %listbox1, replace$(xpath, app_path & "SampleScripts","")
incr tel
end if
NEXT Count
END IF

LL_Free(pList)
END FUNCTION

Michael Hartlef
03-06-2009, 17:27
Thanks Martin,

Ill test this tonight.

ErosOlmi
03-06-2009, 17:37
Seems a great start.

I would just make the following 2 changes.

Change CONTROL SET RESIZE in the following way:


CONTROL SET RESIZE WindowMain,%listbox1 , 1,1,1,1
CONTROL SET RESIZE WindowMain,%btnSearch , 1,0,0,1
CONTROL SET RESIZE WindowMain,%textbox1 , 1,0,0,1
CONTROL SET RESIZE WindowMain,%label1 , 1,0,0,1


add a DOEVENTS in ListDir inside the FOR/NEXT like:


FOR Count = 1 TO nItems
xItem = LL_GetItem(pList, Count)
doevents
GetFiles(xItem)
ListDir(xItem)
NEXT Count

You should see a better application reaction to events. LISTBOX will be populated while still searching.

Thanks for the present
Ciao
Eros

Petr Schreiber
03-06-2009, 17:59
Martin,

that is very nice script!

I tried to modify it to use CALLBACKs ( lower CPU usage, saves some parsing ), used arrays instead of linked lists and the search field is now combobox with predefined module names.



USES "ui", "file", "os"

BEGIN CONST
%bSearch = %IDOK
%cbWord
%lbResults
%lblHelp
END CONST

DIM hDlg AS DWORD
FUNCTION TBMain( )

DIALOG NEW 0, "ThinBasic Keyword Finder by Martin Verlaan", -1, -1, 300, 320, %WS_CLIPCHILDREN OR %WS_OVERLAPPEDWINDOW, 0 TO hDlg

CONTROL ADD LISTBOX, hDlg, %lbResults,, 1, 1, 297, 280
CONTROL ADD BUTTON, hDlg, %bSearch , "Search", 230, 280, 60, 12

DIM Modules() AS STRING
DIM n AS LONG = DIR_ListArray( Modules, APP_PATH+"LIB", "thinbasic_*.DLL", %FILE_NORMAL )

CONTROL ADD COMBOBOX, hDlg, %cbWord , , 10, 280, 210, 120, %CBS_DROPDOWN Or %CBS_SORT Or %WS_TABSTOP
FOR i AS LONG = 1 TO n
Modules(i) = lcase$(Modules(i))
Modules(i) = LTRIM$(Modules(i), "thinbasic_")
Modules(i) = RTRIM$(Modules(i), ".dll")
Modules(i) = $DQ+Modules(i)+$DQ
COMBOBOX ADD hDlg, %cbWord, Modules(i)
NEXT

CONTROL ADD LABEL, hDlg, %lblHelp , "Enter a keyword, click on the Search button and then double click on a file to view the script", 10, 295, 200, 20

CONTROL SET RESIZE hDlg, %lbResults, 1, 1, 1, 1
CONTROL SET RESIZE hDlg, %bSearch , 0, 1, 0, 1
CONTROL SET RESIZE hDlg, %cbWord , 1, 1, 0, 1
CONTROL SET RESIZE hDlg, %lblHelp , 1, 0, 0, 1

CONTROL SET FOCUS hDlg, %cbWord
DIALOG SHOW MODAL hDlg, call dlgProc

END FUNCTION

CALLBACK FUNCTION dlgProc() AS LONG
STATIC Keyword, tmpStr AS STRING

SELECT CASE cbMsg
CASE %WM_COMMAND
SELECT CASE cbCtl
CASE %lbResults
IF cbCtlMsg = %LBN_DBLCLK THEN
LISTBOX GET TEXT cbHndl, %lbResults TO tmpStr
OS_SHELL( APP_PATH & "thinAir\thinair.exe " & APP_PATH & tmpStr, %OS_WNDSTYLE_NORMAL, %OS_SHELL_ASYNC )
END IF

CASE %bSearch
CONTROL GET TEXT cbHndl, %cbWord TO Keyword
CONTROL DISABLE cbHndl, %bSearch

LISTBOX RESET cbHndl, %lbResults

ListDir( APP_PATH, Keyword )


DIALOG REDRAW cbHndl
CONTROL ENABLE cbHndl, %bSearch
END SELECT
END SELECT
END FUNCTION

FUNCTION ListDir( ldir as string, keyword as string )
LOCAL Count, pList, nItems AS LONG
LOCAL xitem AS STRING
LOCAL directory( ) AS STRING

nItems = DIR_LISTARRAY( directory, ldir, "*.*", %FILE_ADDPATH OR %FILE_SUBDIR )

FOR Count = 1 TO nItems
xItem = directory( count )
GetFiles( xItem, keyword )
ListDir( xItem, keyword )
doevents
NEXT

END FUNCTION

FUNCTION GetFiles( ldir as string, keyword as string )

LOCAL Count, pList, nItems AS LONG
LOCAL xpath AS STRING
LOCAL script( ) AS STRING
LOCAL filesOk AS string
LOCAL filesOkCount AS LONG

nItems = DIR_LISTARRAY( script, ldir, "*.tbasic", %FILE_ADDPATH OR %FILE_NORMAL )

IF nItems > 0 THEN
FOR Count = 1 TO nItems
xpath = script( count )
IF INSTR( LCASE$( FILE_LOAD( xpath )), LCASE$( keyword )) > 0 THEN
filesOk += REMOVE$( xpath, APP_PATH )+"|"
END IF
NEXT
filesOk = rtrim$(filesOk, "|")
if len(filesOk) then
listbox add hDlg, %lbResults, filesOk using "|"
end if

END IF

END FUNCTION


Thank you!,
Petr

martin
03-06-2009, 19:29
Thanks Eros and Petr for your tips and modifications. This is were I'm learning from!