cut/copy/paste enabling/disabling to a textarea gadget

Started by Juiceter, October 20, 2020, 14:20:45

Previous topic - Next topic

Juiceter


Import maxgui.drivers


SuperStrict

Local mywindow:tgadget=CreateWindow("pop up menu example",200,200,320,240)
Local mycanvas:tgadget=CreateCanvas(200,200,320,240,mywindow)
Local mypanel:tgadget=CreatePanel(0,0,300,200,mywindow,PANEL_ACTIVE)
Local filemenu:tgadget=CreateMenu("",0,mycanvas)

Local exitmenu:tgadget=CreateMenu("exit",1,filemenu)

CreateMenu "toggle exit menu",3,filemenu ; CreateMenu "open",2,filemenu


Repeat
WaitEvent()
Select EventID()
Case EVENT_WINDOWCLOSE End
Case EVENT_MOUSEDOWN If(EventSource()=mypanel And EventData()=2) PopupWindowMenu mywindow,filemenu
Case EVENT_MENUACTION
Select EventData()
Case 1 Notify "you clicked exit"
Print "you clicked exit"
Case 2 Notify "you clicked open"
Case 3
If MenuEnabled(exitmenu)
DisableMenu exitmenu
Else EnableMenu exitmenu
EndIf
End Select
End Select

'UpdateWindowMenu mywindow ' not needed
Forever



Just a quick question regarding the system text buffer on windows (the code example is provided so you roughly know what I'm on about, for the 3 options read cut copy paste instead).

Is there a way to tell that text within a textarea gadget has been selected so you can enable/disable cut/copy as appropriate? Or that the buffer isn't empty so you can paste into the gadget?

Doing it isn't the problem, disabling the appropriate menu options is!

Edit: the text buffer (of course also known as the clipboard ;-)).

Henri

Hi,

you can use EVENT_GADGETSELECT to catch the event and TextareSelLen to get if something is actually selected and what.

Something like..

Code (blitzmax) Select

SuperStrict

Import maxgui.drivers

Local mywindow:tgadget=CreateWindow("pop up menu example",200,200,320,240)
Local mycanvas:tgadget=CreateCanvas(200,200,320,240,mywindow)
Local mypanel:tgadget=CreatePanel(0,0,300,200,mywindow,PANEL_ACTIVE)
Local mytext:tgadget=CreateTextArea(0,0,ClientWidth(mypanel),ClientHeight(mypanel),mypanel)

Local filemenu:tgadget=CreateMenu("",0,mycanvas)
Local exitmenu:tgadget=CreateMenu("exit",1,filemenu)

CreateMenu "toggle exit menu",3,filemenu ; CreateMenu "open",2,filemenu

Repeat
WaitEvent()
Select EventID()
Case EVENT_WINDOWCLOSE
End
Case EVENT_MOUSEDOWN
If(EventSource()=mypanel And EventData()=2) Then PopupWindowMenu mywindow,filemenu
Case EVENT_MENUACTION
Select EventData()
Case 1 Notify "you clicked exit"
Print "you clicked exit"
Case 2 Notify "you clicked open"
Case 3
If MenuEnabled(exitmenu)
DisableMenu exitmenu
Else EnableMenu exitmenu
EndIf
End Select
Case EVENT_GADGETSELECT
Select EventSource()
Case mytext

DebugLog "EVENT_GADGETSELECT"

Local chars:Int = TextAreaSelLen(mytext)
If chars Then Print("Current selection is: " + TextAreaText(mytext,TextAreaCursor(mytext)) )
EndSelect
End Select

'UpdateWindowMenu mywindow ' not needed
Forever



-Henri
- Got 01100011 problems, but the bit ain't 00000001

Juiceter

#2
Thanks once again for pointing me in the right direction (you really are one of the blitz angels ;)). I'll give that a try. Is there any way to see if the clipboard is empty though?

https://www.blitzmax.org/docs/en/api/brl/brl.clipboard/

I'm just looking in here at the minute for info. but this seems to relate to a custom blitz clipboard.

Henri

If you are using NG then you can enjoy the native clipboard support already baked in. It uses the system API on each platform so no custom Blitz-stuff.

Example:

Code (blitzmax) Select

SuperStrict

Framework Brl.Clipboard
Import Brl.StandardIO
Import Brl.SystemDefault

' create a clipboard manager to access the system wide clipboard
Local clipboard:TClipboard = CreateClipboard()

' clipboard mode can be:
' LCB_CLIPBOARD = The primary (global) clipboard [default mode]
' LCB_PRIMARY = The (global) mouse selection clipboard.
' LCB_SECONDARY = The largely unused (global) secondary selection clipboard.
Local clipboardMode:Int = LCB_CLIPBOARD

'Uncomment this to clear clipboard
'ClearClipboard(clipboard)

'Add text to clipboard
If Confirm("Add some text to clipboard ? (Else the current content of clipboard is printed)") Then ClipboardSetText(clipboard, "This is my text..")

If ClipboardHasOwnership(clipboard, clipboardMode)
Print "clipboard content created by us: " + ClipboardText(clipboard)
Else
Print "clipboard content of another application: " + ClipboardText(clipboard)
EndIf]



-Henri
- Got 01100011 problems, but the bit ain't 00000001

Juiceter

#4
Hmm I'm not, and I realised that was an NG solution, but I was looking for pointers as to how to read what's in the clipboard. Thanks anyway H!

I seem to have found a way to do it with this piece of code:


Code (blitzmax) Select


SuperStrict

Import MaxGui.Drivers

AppTitle = "Clpboard Example"

Extern "Win32"
Function OpenClipboard:Int(hwnd:Byte Ptr = Null)
Function CloseClipboard()
Function EmptyClipboard()
'Function SetClipboardData(format:Int, hMem:Byte Ptr)
Function GetClipboardData:Byte Ptr(format:Int)
Function IsClipboardFormatAvailable:Int(format:Int)
Function GlobalAlloc:Byte Ptr(uflags:Int, bytes:Int)
End Extern


Function GetClipboardText:String()
If OpenClipboard(Null) And IsClipboardFormatAvailable(1)
Local str:String = String.FromCString(GetClipboardData(1))
CloseClipboard()
Return str
Else
Return Null
EndIf
End Function



Local S$=GetClipboardText()
Print Len S$


It tells you how many characters are in the clipboard. Cant remember where I found it but you may even have written it some time ago, or one of the other Blitz Gurus here.