Ooops
November 29, 2020, 07:27:02 PM

Author Topic: cut/copy/paste enabling/disabling to a textarea gadget  (Read 318 times)

Offline Juiceter

  • Jr. Member
  • **
  • Posts: 65
cut/copy/paste enabling/disabling to a textarea gadget
« on: October 20, 2020, 02:20:45 PM »
Code: [Select]
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 ;-)).

Offline Henri

  • Sr. Member
  • ****
  • Posts: 265
Re: cut/copy/paste enabling/disabling to a textarea gadget
« Reply #1 on: October 21, 2020, 08:54:36 AM »
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
  1. SuperStrict
  2.  
  3. Import maxgui.drivers
  4.  
  5. Local mywindow:tgadget=CreateWindow("pop up menu example",200,200,320,240)
  6. Local mycanvas:tgadget=CreateCanvas(200,200,320,240,mywindow)
  7. Local mypanel:tgadget=CreatePanel(0,0,300,200,mywindow,PANEL_ACTIVE)
  8. Local mytext:tgadget=CreateTextArea(0,0,ClientWidth(mypanel),ClientHeight(mypanel),mypanel)
  9.  
  10. Local filemenu:tgadget=CreateMenu("",0,mycanvas)
  11. Local exitmenu:tgadget=CreateMenu("exit",1,filemenu)
  12.  
  13. CreateMenu "toggle exit menu",3,filemenu ; CreateMenu "open",2,filemenu
  14.  
  15. Repeat
  16.         WaitEvent()
  17.         Select EventID()
  18.                 Case EVENT_WINDOWCLOSE
  19.                         End
  20.                 Case EVENT_MOUSEDOWN
  21.                         If(EventSource()=mypanel And EventData()=2) Then PopupWindowMenu mywindow,filemenu
  22.                 Case EVENT_MENUACTION
  23.                         Select EventData()
  24.                                 Case 1  Notify "you clicked exit"
  25.                                         Print "you clicked exit"
  26.                                 Case 2  Notify "you clicked open"
  27.                                 Case 3
  28.                                         If MenuEnabled(exitmenu)
  29.                                                 DisableMenu exitmenu
  30.                                         Else EnableMenu exitmenu
  31.                                         EndIf  
  32.                         End Select
  33.                 Case EVENT_GADGETSELECT
  34.                         Select EventSource()
  35.                                 Case mytext
  36.                                
  37.                                         DebugLog "EVENT_GADGETSELECT"
  38.                                
  39.                                         Local chars:Int = TextAreaSelLen(mytext)
  40.                                         If chars Then Print("Current selection is: " + TextAreaText(mytext,TextAreaCursor(mytext)) )
  41.                         EndSelect
  42.         End Select
  43.        
  44.         'UpdateWindowMenu mywindow ' not needed
  45. Forever
  46.  


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

Offline Juiceter

  • Jr. Member
  • **
  • Posts: 65
Re: cut/copy/paste enabling/disabling to a textarea gadget
« Reply #2 on: October 21, 2020, 09:02:00 PM »
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.

Offline Henri

  • Sr. Member
  • ****
  • Posts: 265
Re: cut/copy/paste enabling/disabling to a textarea gadget
« Reply #3 on: October 22, 2020, 08:09:30 AM »
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
  1. SuperStrict
  2.  
  3. Framework Brl.Clipboard
  4. Import Brl.StandardIO
  5. Import Brl.SystemDefault
  6.  
  7. ' create a clipboard manager to access the system wide clipboard
  8. Local clipboard:TClipboard = CreateClipboard()
  9.  
  10. ' clipboard mode can be:
  11. ' LCB_CLIPBOARD = The primary (global) clipboard [default mode]
  12. ' LCB_PRIMARY = The (global) mouse selection clipboard.
  13. ' LCB_SECONDARY = The largely unused (global) secondary selection clipboard.
  14. Local clipboardMode:Int = LCB_CLIPBOARD
  15.  
  16. 'Uncomment this to clear clipboard
  17. 'ClearClipboard(clipboard)
  18.  
  19. 'Add text to clipboard
  20. If Confirm("Add some text to clipboard ? (Else the current content of clipboard is printed)") Then ClipboardSetText(clipboard, "This is my text..")
  21.  
  22. If ClipboardHasOwnership(clipboard, clipboardMode)
  23.         Print "clipboard content created by us: " + ClipboardText(clipboard)
  24. Else
  25.         Print "clipboard content of another application: " + ClipboardText(clipboard)
  26. EndIf]
  27.  


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

Offline Juiceter

  • Jr. Member
  • **
  • Posts: 65
Re: cut/copy/paste enabling/disabling to a textarea gadget
« Reply #4 on: October 22, 2020, 11:27:18 PM »
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
  1.  
  2. SuperStrict
  3.  
  4. Import MaxGui.Drivers
  5.  
  6. AppTitle = "Clpboard Example"
  7.  
  8. Extern "Win32"
  9.         Function OpenClipboard:Int(hwnd:Byte Ptr = Null)
  10.         Function CloseClipboard()
  11.         Function EmptyClipboard()
  12.         'Function SetClipboardData(format:Int, hMem:Byte Ptr)
  13.         Function GetClipboardData:Byte Ptr(format:Int)
  14.         Function IsClipboardFormatAvailable:Int(format:Int)
  15.         Function GlobalAlloc:Byte Ptr(uflags:Int, bytes:Int)
  16. End Extern
  17.  
  18.  
  19. Function GetClipboardText:String()
  20.         If OpenClipboard(Null) And IsClipboardFormatAvailable(1)
  21.                 Local str:String = String.FromCString(GetClipboardData(1))
  22.                 CloseClipboard()
  23.                 Return str
  24.         Else
  25.                 Return Null
  26.         EndIf
  27. End Function
  28.  
  29.  
  30.  
  31. Local S$=GetClipboardText()
  32. Print Len S$
  33.  

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.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal