Trap 'paste' in an editorgadget?

Just starting out? Need help? Post your questions and find answers here.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

I see no problem with your method at all PB. I just prefer mine as it only blocks #WM_PASTE in my target EditorGadget, letting other gadgets go on as usual with no code interruption. ;)

rsts now has at least 3 options to choose from and he can decide which one is best suited for his app. 8)

Code: Select all

Procedure ProtectEditor(editorGadget) 
  With editFormat.CHARFORMAT2 
    \cbSize = SizeOf(CHARFORMAT2) 
    \dwMask = #CFM_PROTECTED 
    \dwEffects = #CFE_PROTECTED 
  EndWith 
  SendMessage_(GadgetID(editorGadget), #EM_SETCHARFORMAT, #SCF_ALL, @editFormat) 
  SendMessage_(GadgetID(editorGadget), #EM_SETEVENTMASK, 0, #ENM_PROTECTED) 
EndProcedure 

Procedure WinCallback(hwnd, msg, wParam, lParam) 
  result = #PB_ProcessPureBasicEvents 
  Select msg 
    Case #WM_NOTIFY 
      *pNMHDR.NMHDR = lParam 
      Select *pNMHDR\code 
        Case #EN_PROTECTED 
          *enp.ENPROTECTED = lParam 
          If *enp\msg = #WM_PASTE 
            ;...Do your thing here and return nonzero to prevent paste 
            MessageRequester("Sorry", "Paste not allowed", #PB_MessageRequester_Ok | #MB_ICONERROR) 
            result = 1 
          Else 
            ;...Allow everything else 
            result = 0 
          EndIf 
      EndSelect 
  EndSelect 
  ProcedureReturn result 
EndProcedure 
        
If OpenWindow(0, 0, 0, 500, 425, "ProtectEditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0)) 
  SetWindowCallback(@WinCallback()) 
  EditorGadget (0, 10, 10, 480, 180) 
  EditorGadget (1, 10, 200, 480, 180) 
  StringGadget(2, 10, 390, 450, 25, "StringGadget")
  ProtectEditor(0) 
  For g = 0 To 1
    For i = 0 To 15 
      AddGadgetItem(g, i, "Line " + Str(i)) 
    Next i
  Next g 
  Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow 
EndIf 
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Little John
Addict
Addict
Posts: 4805
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Post by Little John »

Hi all,

I've read this interesting discussion. Now I've got a question out of curiosity:
When someone uses [Shift]+[Insert] instead of [Ctrl]+[V] for pasting, will the various solutions presented here still work?

Regards, Little John
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Sparkie's code will work without any alterations. All other solutions presented above will require a little modification.
I may look like a mule, but I'm not a complete ass.
Little John
Addict
Addict
Posts: 4805
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Post by Little John »

Thanks for the information.

Regards, Little John
Post Reply