Paste to StringGadget

Just starting out? Need help? Post your questions and find answers here.
GenRabbit
Enthusiast
Enthusiast
Posts: 151
Joined: Wed Dec 31, 2014 5:41 pm

Paste to StringGadget

Post by GenRabbit »

Hi
I have a problem, I try to intercept a Paste into a stringgadget, so to check that the stuff to be pasted does not include stuff not allowed(Numbers)

The intercept is happening inside and callback. So far so good.

The Question is, what command am I to intercept?

Tried #WM_PASTE, nothing happened, Tried #WM_KEYDOWN, I believe. This gave me what special key (CTRL, SHIFT etc) but not V.

Adding a debug, it seems like CTRL+V and CTRL+C both give med 0x0B0(176).
wombats
Enthusiast
Enthusiast
Posts: 716
Joined: Thu Dec 29, 2011 5:03 pm

Re: Paste to StringGadget

Post by wombats »

This code is modified from that in a post by skywalk.

This seems to work:

Code: Select all

EnableExplicit
Global.i evWW, SG_CBn
Procedure.i SG_CB(hW, msg, wP, lP)
  Protected.i ri, txt.s
  Select msg
    Case #WM_PASTE
      txt = GetClipboardText()
      If Len(txt) > 5 ; Allow the paste
        ri = CallWindowProc_(SG_CBn, hW, msg, wP, lP)
      Else
        ; ...paste denied.
      EndIf
  Default
    ri = CallWindowProc_(SG_CBn, hW, msg, wP, lP)
  EndSelect
  ProcedureReturn ri
EndProcedure

If OpenWindow(0, 0, 0, 230, 120, "EVENTTYPES EXAMPLE...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(1, 10, 10, 150, 100, "TEST")
  SG_CBn = SetWindowLongPtr_(GadgetID(1), #GWL_WNDPROC, @SG_CB())
  Repeat
    evWW = WaitWindowEvent()
  Until evWW = #PB_Event_CloseWindow
EndIf
GenRabbit
Enthusiast
Enthusiast
Posts: 151
Joined: Wed Dec 31, 2014 5:41 pm

Re: Paste to StringGadget

Post by GenRabbit »

Rewrote your version to be more like mine.
And it still work

Code: Select all

EnableExplicit
Global.i evWW

Import "Comctl32.lib" 
	; use the PureBasic Syntax (Windows API procedures using trailing underscore) 
	; 
	SetWindowSubclass_(hWnd.i, *fnSubclass, uIdSubclass.i, dwRefData.i)  As "SetWindowSubclass" 
	GetWindowSubclass_(hWnd.i, *fnSubclass, uIdSubclass.i, dwRefData.i) As "GetWindowSubclass"
	RemoveWindowSubclass_(hWnd.i, *fnSubclass, uIdSubclass.i)          As "RemoveWindowSubclass"
	DefSubclassProc_(hWnd.i, uMsg.i, wParam.i, lParam.i)                   As "DefSubclassProc" 
EndImport 

Procedure.i SG_CB(hWnd.i, uMsg.i, wParam.i, lParam.i, uIdSubclass.i, dwRefData.i)
  Protected.i ri, txt.s
  Select umsg
    Case #WM_PASTE
      txt = GetClipboardText()
      If Len(txt) > 5 ; Allow the paste
        ri = DefSubclassProc_(hwnd.i, umsg, wParam, lParam)
      Else
        ; ...paste denied.
      EndIf
  Default
    ri = DefSubclassProc_(hwnd.i, umsg, wParam, lParam)
  EndSelect
  ProcedureReturn ri
EndProcedure

If OpenWindow(0, 0, 0, 230, 120, "EVENTTYPES EXAMPLE...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(1, 10, 10, 150, 100, "TEST")
  ;SG_CBn = SetWindowLongPtr_(GadgetID(1), #GWL_WNDPROC, @SG_CB())
  SetWindowSubclass_(GadgetID(1), @SG_CB(), 100, 0)
  Repeat
    evWW = WaitWindowEvent()
  Until evWW = #PB_Event_CloseWindow
EndIf
In my code the callback starts like this;

Code: Select all

	Procedure NewSubclassWindow(hWnd.i, uMsg.i, wParam.i, lParam.i, uIdSubclass.i, dwRefData.i)
		Protected.i wp, lp, lt, ln, ct
		Protected.s sTemp
		Protected.i MenuItemID
		Select uMsg 
		Case #WM_NCDESTROY                                                         :Debug("WM_NCDESTROY") 
		  	RemoveWindowSubclass_(hWnd, @NewSubclassWindow(), uIdSubclass)  
		Case #WM_PASTE	
		  	Debug "Working Now!"
		  	
		  	
		  	
		Case #WM_CHAR  
Are there commands run before paste that can destroy it like, #WM_CHAR? Like if it returns a ProcedureReturn 0?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Paste to StringGadget

Post by RASHAD »

Using KeyboardShortcut()

Code: Select all


If OpenWindow(0, 0, 0, 230, 120, "EVENTTYPES EXAMPLE...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(1, 10, 10, 150, 100, "TEST")
  AddKeyboardShortcut(0,#PB_Shortcut_Control|#PB_Shortcut_V,10)
  AddKeyboardShortcut(0,#PB_Shortcut_Control|#PB_Shortcut_C,20)
  #SOC=SizeOf(Character)
  Repeat
    Select WaitWindowEvent(1)
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #PB_Event_Menu
        Select EventMenu()
          Case 10
            text$ = GetClipboardText()            
            *old.Character=@text$
            *new.Character=*old
            While *old\c
              If *old\c>47 And *old\c<58 
                *new\c=*old\c
                *new+#SOC
                Debug "Digits prohibited"
              EndIf
              *old+#SOC
            Wend
            *new\c=0
            
          Case 20
            Debug "Copy prohibited"
        EndSelect
    EndSelect
  Until Quit = 1
EndIf
Egypt my love
GenRabbit
Enthusiast
Enthusiast
Posts: 151
Joined: Wed Dec 31, 2014 5:41 pm

Re: Paste to StringGadget

Post by GenRabbit »

Code: Select all

		Select uMsg 
		Case #WM_NCDESTROY
		  	RemoveWindowSubclass_(hWnd, @NewSubclassWindow(), uIdSubclass)  
		Case #WM_PASTE	
		  	Debug "Working Now!"
		  	
		  	
		  	
		Case #WM_CHAR  
		  	Debug "Char: " + FormatNumber(wparam,0,"","") ; #WM_PASTE gives value 22
			SendMessage_(hwnd,#EM_GETSEL, @wp,@lp)
			lt = lp - wp
			Select wParam
				Case #ascUpperCaseA To #ascUpperCaseZ, #ascLowerCaseA To #ascLowerCaseZ
					Select uIdSubclass
						Case #GadgetIndexDayName
							ln = Len(GetGadgetText(#TxtDayName))
							If (ln - lt) >= #LettersDayName
								ProcedureReturn 0
							EndIf
							If Not FirstTypeDayName
								SetGadgetText(#TxtDayName,"")						
								FirstTypeDayName = 1
							EndIf
							
						Case #GadgetIndexMonthName
							ln = Len(GetGadgetText(#TxtMonthName))
							If (ln - lt) >= #LettersMonthName
								ProcedureReturn 0
							EndIf
							If Not FirstTypeMonthName
								SetGadgetText(#TxtMonthName,"")						
								FirstTypeMonthName = 1
							EndIf
					EndSelect
					
					
				Case #VK_BACK
					ProcedureReturn DefSubclassProc_(hWnd, uMsg, wParam, lParam)	
				Default	
					ProcedureReturn 0     <--- Kills #WM_PASTE
				EndSelect
this is the code I use inside my callback. The last ProcedureReturn 0 kills #WM_PASTE. CTRL+V gives wParam returns VK_IME_ON.
Is there a way to check if CTRL is pushed inside #WM_CHAR?
GenRabbit
Enthusiast
Enthusiast
Posts: 151
Joined: Wed Dec 31, 2014 5:41 pm

Re: Paste to StringGadget

Post by GenRabbit »

Code: Select all

		Case #WM_CHAR  
			If GetAsyncKeyState_(#VK_LCONTROL) & $8000 Or GetAsyncKeyState_(#VK_RCONTROL) & $8000
				ProcedureReturn DefSubclassProc_(hWnd, uMsg, wParam, lParam)
			EndIf
Added this to the beginning, seems to have done the trick. :)
Post Reply