Page 1 of 1

Standard Cut, Copy, Paste Menu Items

Posted: Tue Oct 18, 2011 10:20 pm
by Slyvnr
Don't see a Board Sub-Forum for Standardized Code Snippets so figure Tip's n Trick's is the best place.

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")
For those of you who like to use IF-EndIF Statements then this code goes in your Main Program Event Checking Loop.

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
For those who prefer Select Case statements, then you would use this.

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
GetActiveGadget() returns the gadget that currently has Keyboard Focus. If there is no gadget that has Keyboard Focus then GetActiveGadget returns -1. I prefer to check for an active gadget within each Cut/Copy/Paste simply because if I do the check first, there will almost always be an active gadget. Which means I will always be checking to see if there has been a Cut/Copy/Paste call made in every loop.

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