SetWindowLong_ not disabling Ctrl+V in EditorGadget?

Just starting out? Need help? Post your questions and find answers here.
hattonpa
New User
New User
Posts: 7
Joined: Tue Feb 24, 2004 10:13 pm
Location: Tampa Bay

SetWindowLong_ not disabling Ctrl+V in EditorGadget?

Post by hattonpa »

Hi,

I've only been using PureBasic for a few weeks and have been loving it. I have wanted to learn more about the Windows API for a long time, and it's been an unexpected bonus to find so much Win API help in the PB forums, in addition to PB help.

My question...
I've tried to implement a modified version of Sparkie's example from his post at: viewtopic.php?p=59741#59741 But, it's not working for me using an EditorGadget, instead of a StringGadget.

Is this because a different API call should be used, or is it a problem with the EditorGadget? BTW, I have checked out Sparkie's other recommendation posted just prior to the other one at the link above. That will work for me as well, but I would really like to use the Win API call.

Since I'm only barely scratching the surface of learning Win API calls, I would really appreciate some help. Here's my usage of Sparkie's code, where I've inserted an EditorGadget.

Code: Select all

Enumeration 
  #Window_0 
EndEnumeration 

Enumeration 
  #Edit_0 
  #String_0
  #String_1 
EndEnumeration 

Global OldCallback 

Procedure.l MyWindowCallBack(hwnd, msg, wParam, lParam) 
  
  Select msg 
    Case #WM_CONTEXTMENU 
      ;If hwnd = GadgetID(#String_0)
      If hwnd = GadgetID(#Edit_0) 
        msg = #WM_KEYDOWN 
        wParam = #VK_ESCAPE 
        lParam = 0 
      EndIf 
    Case #WM_PASTE 
      ;If hwnd = GadgetID(#String_0) :  ProcedureReturn : EndIf 
      If hwnd = GadgetID(#Edit_0) :  ProcedureReturn : EndIf 
    Case #WM_COPY
      ;If hwnd = GadgetID(#String_0) :  ProcedureReturn : EndIf 
      If hwnd = GadgetID(#Edit_0) :  ProcedureReturn : EndIf 
    Case #WM_CUT
      ;If hwnd = GadgetID(#String_0) :  ProcedureReturn : EndIf
      If hwnd = GadgetID(#Edit_0) :  ProcedureReturn : EndIf 
  EndSelect 
  
  ProcedureReturn CallWindowProc_(OldCallback, hwnd, msg, wParam, lParam) 
EndProcedure 

If OpenWindow(#Window_0, 0, 0, 300, 100,  #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_TitleBar , "Disable cut, copy, paste") 
  If CreateGadgetList(WindowID())
    ;StringGadget(#String_0, 50, 20, 200, 20, "Cut, Copy, Paste DISABLED")
    EditorGadget(#Edit_0, 50, 20, 200, 20) 
    StringGadget(#String_1, 50, 50, 200, 20, "Cut, Copy, Paste ENABLED")
    ;ActivateGadget(#String_0)
    ActivateGadget(#Edit_0)
    ;OldCallback = SetWindowLong_(GadgetID(#String_0), #GWL_WNDPROC, @MyWindowCallBack())
    OldCallback = SetWindowLong_(GadgetID(#Edit_0), #GWL_WNDPROC, @MyWindowCallBack()) 
  EndIf 
EndIf 

Quit = #False 

Repeat 
  event = WaitWindowEvent() 
  
  Select event 
    
    Case #PB_EventCloseWindow 
      Quit = #True 
      
  EndSelect 
Until Quit 
End
Paul H.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

See if this works a little better than that old method I was using.

Code: Select all

Enumeration 
  #Window_0 
EndEnumeration 
Enumeration 
  #Edit_0 
  #Edit_1 
EndEnumeration 
If OpenWindow(#Window_0, 0, 0, 300, 150,  #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_TitleBar , "Disable cut, copy, paste") And CreateGadgetList(WindowID()) 
  EditorGadget(#Edit_0, 10, 10, 280, 50)
  AddGadgetItem(#Edit_0, -1, "Ctrl+V paste disabled")
  EditorGadget(#Edit_1, 10, 60, 280, 50) 
  AddGadgetItem(#Edit_1, -1, "Ctrl+V paste enabled")
  AddKeyboardShortcut(#Window_0, #PB_Shortcut_Control | #PB_Shortcut_V, 1) 
  AddKeyboardShortcut(#Window_0, #PB_Shortcut_Control | #PB_Shortcut_C, 2) 
  AddKeyboardShortcut(#Window_0, #PB_Shortcut_Control | #PB_Shortcut_X, 3) 
  AddKeyboardShortcut(#Window_0, #PB_Shortcut_Shift | #PB_Shortcut_Insert, 4) 
  Repeat 
    event = WaitWindowEvent() 
    If event = #PB_EventGadget
      currentGadget = EventGadgetID()
    EndIf
    Select event
      Case #PB_EventMenu
        Select EventMenuID()
          ; --> Catch Ctrl+V
          Case 1
            Select currentGadget
              Case #Edit_0
                MessageRequester("Sorry", "No paste allowed")
                ActivateGadget(#Edit_0)
              Case #Edit_1
                SendMessage_(GadgetID(#Edit_1), #WM_PASTE, 0, 0)
            EndSelect
          ; --> Catch Ctrl+C
          Case 2
            Select currentGadget
              Case #Edit_0
                MessageRequester("Sorry", "No copy allowed")
                ActivateGadget(#Edit_0)
              Case #Edit_1
                SendMessage_(GadgetID(#Edit_1), #WM_COPY, 0, 0)
            EndSelect
          ; --> Catch Ctrl+X
          Case 3
            Select currentGadget
              Case #Edit_0
                MessageRequester("Sorry", "No cut allowed")
                ActivateGadget(#Edit_0)
              Case #Edit_1
                SendMessage_(GadgetID(#Edit_1), #WM_CUT, 0, 0)
            EndSelect
          ; --> Catch Shift+Insert
          Case 4
            Select currentGadget
              Case #Edit_0
                MessageRequester("Sorry", "No paste allowed")
                ActivateGadget(#Edit_0)
              Case #Edit_1
                SendMessage_(GadgetID(#Edit_1), #WM_PASTE, 0, 0)
            EndSelect
        EndSelect
    EndSelect
  Until event = #PB_Event_CloseWindow 
EndIf
End
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
hattonpa
New User
New User
Posts: 7
Joined: Tue Feb 24, 2004 10:13 pm
Location: Tampa Bay

RE: SetWindowLong_ not disabling Ctrl+V in EditorGadget?

Post by hattonpa »

Thanks, Sparkie. This code will work great for me.

Any observations on why, after using SetWindowLong_, Ctrl+V pasting is not disabled with an EditorGadget, while it is disabled with a StringGadget? :?:
Paul H.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

The EditorGadget is a bit more complex than the StringGadget. From what I've gathered, the EditorGadget (Rich Edit) does not respond to the #WM_PASTE message. Therefore, you need to respond the #WM_KEYDOWN message and filter the input from there. Here's that version of code that will catch Ctrl+V (paste) and disable it in the EditorGadget.

Code: Select all

Enumeration 
  #Window_0 
EndEnumeration 
Enumeration 
  #Edit_0 
  #String_0 
  #String_1 
EndEnumeration 
Global OldCallback 
Procedure.l MyEditorCallBack(hwnd, msg, wParam, lparam) 
  Select msg 
    Case #WM_KEYDOWN
      Select wParam
        Case #PB_Shortcut_V
          If GetAsyncKeyState_(#VK_Control) <> 0
             ProcedureReturn 0
          EndIf 
      EndSelect
  EndSelect 
  ProcedureReturn CallWindowProc_(OldCallback, hwnd, msg, wParam, lparam) 
EndProcedure 
If OpenWindow(#Window_0, 0, 0, 300, 100,  #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_TitleBar , "Disable cut, copy, paste") And CreateGadgetList(WindowID()) 
  EditorGadget(#Edit_0, 50, 20, 200, 20) 
  AddGadgetItem(#Edit_0, -1, "Paste Disabled")
  StringGadget(#String_1, 50, 50, 200, 20, "Paste ENABLED") 
  ActivateGadget(#Edit_0) 
  OldCallback = SetWindowLong_(GadgetID(#Edit_0), #GWL_WNDPROC, @MyEditorCallBack()) 
  Repeat 
    event = WaitWindowEvent() 
  Until event = #PB_EventCloseWindow 
EndIf
End
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
hattonpa
New User
New User
Posts: 7
Joined: Tue Feb 24, 2004 10:13 pm
Location: Tampa Bay

Post by hattonpa »

Thanks again, Sparkie.
Paul H.
Post Reply