If you are implementing a menu system and have the standard Cut, Copy, Paste functions here is simple code you could utilize.
Be sure to enumerate the #MenuItem_Cut, #MenuItem_Copy and #MenuItem_Paste constants with your other enumerations.
In your Menu-setup code
Code: Select all
MenuTitle("Edit")
MenuItem( #MenuItem_Cut, "Cut Ctrl-X")
MenuItem( #MenuItem_Copy, "&Copy Ctrl-C")
MenuItem(#MenuItem_Paste, "&Paste Ctrl-V")
Code: Select all
;Menu Item Cut
If MenuItem=#MenuItem_Cut
If GetActiveGadget() and GadgetType(GetActiveGadget())=#PB_GadgetType_String
SetClipboardText(GetGadgetText(GetActiveGadget()))
SetGadgetText(GetActiveGadget(),"")
EndIf
EndIf
;- Menu Item Copy
If MenuItem=#MenuItem_Copy
If GetActiveGadget() and GadgetType(GetActiveGadget())=#PB_GadgetType_String
SetClipboardText(GetGadgetText(GetActiveGadget()))
EndIf
EndIf
;- Menu Item Paste
If MenuItem=#MenuItem_Paste
If GetActiveGadget() and GadgetType(GetActiveGadget())=#PB_GadgetType_String
SetGadgetText(GetActiveGadget(),GetClipboardText())
EndIf
EndIf
Code: Select all
Select MenuItem
Case #MenuItem_Cut
IF GetActiveGadget() and GadgetType(GetActiveGadget())=#PB_GadgetType_String
SetClipboardText(GetGadgetText(GetActiveGadget()))
SetGadgetText(GetActiveGadget(),"")
EndIF
Case #MenuItem_Copy
IF GetActiveGadget() and GadgetType(GetActiveGadget())=#PB_GadgetType_String
SetClipboardText(GetGadgetText(GetActiveGadget()))
EndIf
Case #MenuItem_Paste
IF GetActiveGadget() and GadgetType(GetActiveGadget())=#PB_GadgetType_String
SetGadgetText(GetActiveGadget(),GetClipboardText())
EndIf
EndSelect
Whereas by checking inside each of the Cut/Copy/Paste routines, I only verify there is an active gadget to operate on before actually trying to do anything.
I think it ends up being a half dozen of one, half dozen of the other as far as processor cycles utilized. I think in the long-run it probably evens out. You could just as easily move the "If GetActiveGadget() and GadgetType(GetActiveGadget())=#PB_GadgetType_String" outside and check that one time only and then see if any of the MenuItem's have been selected.
Also, be aware that you do not need to code anything for Ctrl-X, Ctrl-C, Ctrl-V as these keys under Windows have been setup automatically for Cut/Copy/Paste.
Anyway, just thought I would add this to the code base for standardization so people don't have to re-code something that has already been done and everyone needs to have access to.
Take care
Slyvnr