functions which autoselects the text in a StringGadget on moving the
mouse cursor over the StringGadget and which deselects the text on
the cursor leaving the Gadget. I have tested it to work on these OSes:
- Windows XP SP3 (32 Bit)
- Windows 7 (32 Bit)
- Windows 7 (64 Bit with PB x64)
- MacOS X 10.6.8
- andLinux/Kubuntu 9.04 (32 Bit)
- Kubuntu 10.04 (32 Bit)
- Ubuntu/Unity 11.04 (32 Bit)
Code: Select all
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_MacOS
ImportC ""
GetEventClass(Event.L)
HIViewNewTrackingArea(HIViewRef.L, Shape.L, TrackingAreaID.Q, *TrackingAreaRef)
SetControlData(ControlRef.L, ControlPartCode.L, TagName.L, BufferSize.L, *Buffer)
EndImport
#kControlEditTextPart = 5
#kControlEditTextSelectionTag = 'sele'
#kEventClassControl = 'cntl'
#kEventControlTrackingAreaEntered = 23
#kEventControlTrackingAreaExited = 24
Structure ControlEditTextSelectionRec
SelStart.W
SelEnd.W
EndStructure
Structure EventTypeSpec
EventClass.L
EventKind.L
EndStructure
Dim EventTypes.EventTypeSpec(1)
ProcedureC EventHandler(*NextEventHandler, Event.L, UserData.L)
Protected TextSelection.ControlEditTextSelectionRec
Select GetEventKind_(Event)
Case #kEventControlTrackingAreaEntered
TextSelection\SelStart = 0
TextSelection\SelEnd = Len(GetGadgetText(0))
SetActiveGadget(0)
Case #kEventControlTrackingAreaExited
TextSelection\SelStart = 0
TextSelection\SelEnd = 0
EndSelect
SetControlData(GadgetID(0), #kControlEditTextPart, #kControlEditTextSelectionTag, SizeOf(ControlEditTextSelectionRec), @TextSelection)
If *NextEventHandler
CallNextEventHandler_(*NextEventHandler, Event)
EndIf
EndProcedure
CompilerCase #PB_OS_Linux
Enumeration
#EnterGadgetEvent
#LeaveGadgetEvent
EndEnumeration
ProcedureC EventHandler(*Widget.GtkWidget, *Event.GdkEventMotion, UserData)
Select UserData
Case #EnterGadgetEvent
gtk_editable_select_region_(GadgetID(0), 0, Len(GetGadgetText(0)))
Case #LeaveGadgetEvent
gtk_editable_select_region_(GadgetID(0), 0, 0)
EndSelect
EndProcedure
CompilerCase #PB_OS_Windows
Procedure IsMouseOverGadget(GadgetID)
GetWindowRect_(GadgetID(GadgetID), GadgetRect.RECT)
ProcedureReturn PtInRect_(@GadgetRect, DesktopMouseY() << 32 | DesktopMouseX())
EndProcedure
CompilerEndSelect
OpenWindow(0, 0, 0, 200, 100, "Autoselect text", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(0, 50, 40, 100, 20, "Test")
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_MacOS
EventTypes(0)\EventClass = #kEventClassControl
Eventtypes(0)\EventKind = #kEventControlTrackingAreaEntered
EventTypes(1)\EventClass = #kEventClassControl
Eventtypes(1)\EventKind = #kEventControlTrackingAreaExited
HIViewNewTrackingArea(GadgetID(0), 0, 0, 0)
EventHandlerUPP = NewEventHandlerUPP_(@EventHandler())
InstallEventHandler_(GetControlEventTarget_(GadgetID(0)), EventHandlerUPP, 2, @EventTypes(), @EventHandler(), 0)
CompilerCase #PB_OS_Linux
gtk_widget_add_events_(GadgetID(0), #GDK_ENTER_NOTIFY_MASK | #GDK_LEAVE_NOTIFY_MASK)
g_signal_connect_data_(GadgetID(0), "enter-notify-event", @EventHandler(), #EnterGadgetEvent, 0, 0)
g_signal_connect_data_(GadgetID(0), "leave-notify-event", @EventHandler(), #LeaveGadgetEvent, 0, 0)
CompilerEndSelect
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
Case #WM_MOUSEMOVE
If IsMouseOverGadget(0)
If IsTextSelected = #False
SetActiveGadget(0)
SendMessage_(GadgetID(0), #EM_SETSEL, 0, -1)
IsTextSelected = #True
EndIf
EndIf
Case #WM_MOUSELEAVE
SendMessage_(GadgetID(0), #EM_SETSEL, 0, 0)
IsTextSelected = #False
CompilerEndIf
EndSelect
ForEver