A search in this forum would have shown you already a cross-platform solution for Linux, Windows and MacOS X (supporting even both Cocoa and Carbon framework) that demonstrates how to select a specific line in the EditorGadget. I have customized that example even further so that a double click in the upper ListIconGadget (or selecting a line and pressing the Find button) will search the text in the lower EditorGadget, select that line and scroll it into view.marcoagpinto wrote:Now there is only the Linux version missing :p
I have tested the example below successfully with these operating systems and both PB 5.11 and PB 5.20 Beta 15:
- andLinux/Kubuntu 9.04 x86
- Ubuntu 12.04 x64 with Unity and KDE
- Windows XP SP3 x86
- Windows 7 SP1 x64
- MacOS X 10.6.8 (Snow Leopard) x86 and x64
- MacOS X 10.8.4 (Mountain Lion) x86 and x64
Code: Select all
EnableExplicit
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_MacOS
CompilerIf #PB_Compiler_Version <= 461 Or (#PB_Compiler_Version >= 500 And Subsystem("Carbon"))
ImportC ""
GetControlProperty(ControlRef.L, PropertyCreator.L, PropertyTag.L, BufferSize.L, *ActualSize, *PropertyBuffer)
TXNSetSelection(TXNObject.L, StartOffset.L, EndOffset.L)
TXNShowSelection(TXNObject.L, ShowEnd.L)
EndImport
CompilerEndIf
CompilerCase #PB_OS_Linux
ImportC ""
gtk_text_view_scroll_to_iter(*TextView.GtkTextView, *Iter.GtkTextIter, WithinMargin.D, UseAlign.I, xAlign.D, yAlign.D)
EndImport
CompilerEndSelect
Enumeration
#ListIcon
#Button
#Editor
EndEnumeration
Procedure SelectEditorLine(EditorID.I, LineNumber.I)
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux
Protected EndOfLine.GtkTextIter
Protected StartOfLine.GtkTextIter
Protected *TextBuffer.GtkTextBuffer
*TextBuffer = gtk_text_view_get_buffer_(GadgetID(EditorID))
gtk_text_buffer_get_iter_at_line_(*TextBuffer, @StartOfLine, LineNumber)
gtk_text_view_scroll_to_iter(GadgetID(EditorID), StartOfLine, 0.0, #False, 0.0, 0.0)
EndOfLine = StartOfLine
gtk_text_iter_forward_to_line_end_(EndOfLine)
gtk_text_buffer_select_range_(*TextBuffer, StartOfLine, EndOfLine)
CompilerCase #PB_OS_MacOS
CompilerIf #PB_Compiler_Version <= 461 Or (#PB_Compiler_Version >= 500 And Subsystem("Carbon"))
Protected EndOfLine.L
Protected i.L
Protected StartOfLine.L
Protected TXNObject.L
If LineNumber > 0
For i = 0 To LineNumber - 1
StartOfLine + Len(GetGadgetItemText(EditorID, i)) + 1
Next i
EndIf
If GetControlProperty(GadgetID(EditorID), $50555245, $54584F42, 4, 0, @TXNObject) = 0
EndOfLine = StartOfLine + Len(GetGadgetItemText(EditorID, LineNumber))
TXNSetSelection(TXNObject, StartOfLine, EndOfLine)
TXNShowSelection(TXNObject, #True)
EndIf
CompilerElse
Protected i.I
Protected Range.NSRange
If LineNumber > 0
For i = 0 To LineNumber - 1
Range\location + Len(GetGadgetItemText(EditorID, i)) + 1
Next i
EndIf
Range\length = Len(GetGadgetItemText(EditorID, LineNumber))
CocoaMessage(0, GadgetID(EditorID), "setSelectedRange:@", @Range)
CocoaMessage(0, GadgetID(EditorID), "scrollRangeToVisible:@", @Range)
CompilerEndIf
CompilerCase #PB_OS_Windows
Protected LineStart.I
LineStart = SendMessage_(GadgetID(#Editor), #EM_LINEINDEX, LineNumber, 0)
SendMessage_(GadgetID(#Editor), #EM_SETSEL, LineStart, LineStart + Len(GetGadgetItemText(#Editor, LineNumber)))
CompilerEndSelect
EndProcedure
Procedure FindText(EditorID, Text.S)
Protected LinesTotal.I
Protected LineNumber.I
Protected LineStart.I
LinesTotal = CountGadgetItems(#ListIcon)
If LinesTotal > 0
For LineNumber = 0 To LinesTotal - 1
If FindString(GetGadgetItemText(#Editor, LineNumber), Text) > 0
SelectEditorLine(EditorID, LineNumber)
Break
EndIf
Next LineNumber
EndIf
EndProcedure
Define i.I
OpenWindow(0, 100, 100, 261, 230, "Find clicked text in Editor")
ListIconGadget(#ListIcon, 10, 10, WindowWidth(0) - 20, 90, "Column 1", 110, #PB_ListIcon_FullRowSelect)
AddGadgetColumn(#ListIcon, 1, "Column 2", GadgetWidth(#ListIcon) - GetGadgetItemAttribute(#ListIcon, 0, #PB_ListIcon_ColumnWidth, 0) - 24)
ButtonGadget(#Button, 35, GadgetY(#ListIcon) + GadgetHeight(#ListIcon) + 10, 190, 25, "Find selected text in Editor")
EditorGadget(#Editor, 10, GadgetY(#Button) + GadgetHeight(#Button) + 10, WindowWidth(0) - 20, WindowHeight(0) - GadgetY(#Button) - GadgetHeight(#Button) - 20, #PB_Editor_ReadOnly)
For i= 1 To 20
AddGadgetItem(#ListIcon, -1, "Row " + Str(i))
Next
For i = 1 To 20
AddGadgetItem(#Editor, -1, "Row " + Str(i) + " in EditorGadget")
Next i
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
Select EventGadget()
Case #ListIcon
If EventType() = #PB_EventType_LeftDoubleClick
FindText(#Editor, StringField(GetGadgetItemText(#ListIcon, GetGadgetState(#ListIcon)), 1, ","))
EndIf
Case #Button
If GetGadgetState(#ListIcon) >= 0
FindText(#Editor, StringField(GetGadgetItemText(#ListIcon, GetGadgetState(#ListIcon)), 1, ","))
EndIf
SetActiveGadget(#ListIcon)
EndSelect
EndSelect
ForEver