I want to make a small editor. And my goal is to autocomplete the string, just like in the PB environment. Please, see if this can be done better ? I want to calculate the font size (height) in pixels and take into account the size of the comboboxgadget to position it correctly when displayed.
Thank you in advance.
Code: Select all
#EFontName = "lucida console"
#EFontSize = 14
#EFontStyle = #PB_Font_Bold
#MainWindow = 0
#Editor = 1
#ComboBox = 2
Global cbWnd.i, eWnd.i
Procedure GetFontHeight(text.s, FontID)
Protected dc = GetDC_(0)
Protected sz.Size
SelectObject_(dc, FontID)
GetTextExtentPoint32_(dc, text, Len(text), sz)
ReleaseDC_(0, dc)
ProcedureReturn sz\cy
EndProcedure
Procedure WndProc(hWnd, uMsg, wParam, lParam)
If lParam = cbWnd
If uMsg = #WM_COMMAND
Select wParam >> 16 & $FFFF
Case #CBN_CLOSEUP
HideGadget(#ComboBox, #True)
insert$ = GetGadgetText(#ComboBox)
SendMessage_(eWnd, #EM_REPLACESEL, 0, insert$)
SetActiveGadget(#Editor)
Case #CBN_SELCHANGE
If cbState
ProcedureReturn 0
EndIf
EndSelect
EndIf
EndIf
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
OpenWindow(#MainWindow, 0, 0, 800, 600, "EG-test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
eWnd = EditorGadget(#Editor, 10, 10, WindowWidth(0)-20, 150, #PB_Editor_WordWrap)
cbWnd = ComboBoxGadget(#ComboBox, 10, 10, 150, 25)
HideGadget(#ComboBox, #True)
h = SendMessage_(cbWnd, #CB_GETITEMHEIGHT, 0, 0)
h = h / 2
userfont = FontID(LoadFont(#PB_Any, #EFontName, #EFontSize, #EFontStyle))
SetGadgetFont(#Editor, userfont)
SetGadgetFont(#ComboBox, userfont)
For a = 1 To 5
AddGadgetItem(#ComboBox, -1, "str " + Str(a))
Next
SetGadgetState(#ComboBox, 3)
AddGadgetItem(#Editor, 0, "string #1")
AddGadgetItem(#Editor, 1, "string #2")
SetActiveGadget(#Editor)
FontHeight = GetFontHeight("Ag", userfont)
HeightCOR = FontHeight + h
SetWindowCallback(@WndProc())
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_CloseWindow
Quit = 1
Case #WM_CHAR
If GetActiveGadget() = #Editor
key = EventwParam()
If key = 32
GetCaretPos_(p.POINT)
ResizeGadget(#ComboBox, p\x, p\y + HeightCOR, #PB_Ignore, #PB_Ignore)
HideGadget(#ComboBox, #False)
SetActiveGadget(#ComboBox)
SendMessage_(cbWnd, #CB_SHOWDROPDOWN, 1, 0)
EndIf
EndIf
EndSelect
Until Quit = 1