1. How to add to Edit Text default context menu

I am writing a program with edit fields using win32lib version "0.70.20" Windows has a right click context menu used on EditText fields with (Undo, Cut, Copy, Paste, Delete, Select all). I would like to keep that functionality intact, and add more items. How can this be done? Thank you in advance.

new topic     » topic index » view message » categorize

2. Re: How to add to Edit Text default context menu

penpal0andrew said...

I am writing a program with edit fields using win32lib version "0.70.20" Windows has a right click context menu used on EditText fields with (Undo, Cut, Copy, Paste, Delete, Select all). I would like to keep that functionality intact, and add more items. How can this be done? Thank you in advance.

Hi

i do not use win32lib regulary, so i wrote a quick and dirty solution with tinewg, hopefully this gives you enough hints for a solution with win32lib.
there are maybe better solutions, but i'am sure you have to subclass your editcontrol (maybe handling WM_CONTEXTMENU)

I think the code is not to complicated and small enough.
MSDN is your friend ;)
Andreas

include tinewg.exw 
include std/dll.e 
 
-- create your own popup  
 
atom popup=CreatePopupMenu() 
AppendTextItem(popup,101,"Copy") 
AppendTextItem(popup,102,"Paste") 
AppendTextItem(popup,103,"Cut") 
AppendTextItem(popup,104,"Delete") 
AppendTextItem(popup,105,"Select All") 
AppendSeparator(popup) 
AppendTextItem(popup,106,"CloseApp") 
 
-- Create a Window and a EditControl 
 
Window("test") 
SetClientRect(WinHwnd,400,200) 
constant edit=Control(MultiEdit,"",50,0,350,200) 
 
-- SubClass the EditControl and handle the WM_RBUTTONDOWN yourself  
 
function EditWndProc(atom hwnd,atom msg,atom wParam,atom lParam) 
sequence rect 
    if equal(msg,WM_RBUTTONDOWN) then 
		rect=GetWindowRect(edit) 
		TrackPopupMenu(popup,rect[1]+LoWord(lParam),rect[2]+HiWord(lParam),WinHwnd) 
	return 0 -- WM_RBUTTONDOWN never reaches the original EditWindowProc  
		   -- so the default menu never appears 
    end if 
    return  CallWindowProc(OldWndProc,hwnd,msg,wParam,lParam) 
end function 
 
constant OldWndProc=GetWindowLong(edit,GWL_WNDPROC) 
SetWindowLong(edit,GWL_WNDPROC,call_back(routine_id("EditWndProc"))) 
 
-- Your handler for your menu 
 
procedure menuhandler() 
	switch EventItem do 
		case 101 then  
			SendMessage(edit,WM_COPY,0,0) 
		case 102 then 
			SendMessage(edit,WM_PASTE,0,0) 
		case 103 then 
			SendMessage(edit,WM_CUT,0,0) 
		case 104 then 
			SendMessage(edit,WM_CLEAR,0,0) 
		case 105 then 
			SendMessage(edit,EM_SETSEL,0,-1) 
		case 106 then 
			CloseApp() 
		-- and so on ... 
	end switch 
end procedure 
SetHandler(WinHwnd,Menu,routine_id("menuhandler")) 
 
WinMain() 
 
 
new topic     » goto parent     » topic index » view message » categorize

3. Re: How to add to Edit Text default context menu

Wow that is a great program. I like your library better than win32lib. I should try to port my program. But before that, one would need to completely replace all of the functionality of the context menu. I found this website - http://www.c-sharpcorner.com/UploadFile/deepak.sharma00/how-to-customize-default-contextmenu-of-a-textbox-control-in/ Specifically, menu items need to be disabled if they are not currently appropriate. Thanks. If I port my program and run into problems, we will probably be in touch again. Also, isn't it funny that the 3 of us share the same name - me (Andrew / Andy), you (Andreas), and Andrea whose project you based yours on.

new topic     » goto parent     » topic index » view message » categorize

4. Re: How to add to Edit Text default context menu

penpal0andrew said...

Wow that is a great program. I like your library better than win32lib. I should try to port my program. But before that, one would need to completely replace all of the functionality of the context menu. I found this website - http://www.c-sharpcorner.com/UploadFile/deepak.sharma00/how-to-customize-default-contextmenu-of-a-textbox-control-in/ Specifically, menu items need to be disabled if they are not currently appropriate. Thanks. If I port my program and run into problems, we will probably be in touch again. Also, isn't it funny that the 3 of us share the same name - me (Andrew / Andy), you (Andreas), and Andrea whose project you based yours on.

Hi

I think win32lib has much more to offer for bigger projects.
What do you mean with 'completly replace the functionality of the context menu'? That's a real big task.
I added some of the functionality (based on the picture in your link) to the program (only using functions allready in tinewg). 'Paste' is still not handled
and there is no 'getColorDialog' like in win32lib.

But maybe it helps you to go on.

Andreas

include tinewg.exw 
include std/dll.e 
atom txtcol=CL_BLACK 
atom backcol=CL_WHITE 
sequence colors={CL_BLACK,CL_WHITE,CL_YELLOW,CL_BLUE,CL_RED} 
sequence colorstring={"Black","White","Yellow","Blue","Red"} 
 
-- create your own popup  
 
atom poptxt=CreatePopupMenu() 
AppendTextItem(poptxt,201,colorstring) 
 
atom popback=CreatePopupMenu() 
AppendTextItem(popback,301,colorstring) 
 
atom popup=CreatePopupMenu() 
AppendTextItem(popup,100,"Undo") 
AppendSeparator(popup) 
AppendTextItem(popup,101,"Copy") 
AppendTextItem(popup,102,"Paste") 
AppendTextItem(popup,103,"Cut") 
AppendTextItem(popup,104,"Delete") 
AppendTextItem(popup,105,"Select All") 
AppendSeparator(popup) 
AppendTextItem(popup,106,"Font") 
AppendPopup(popup,poptxt,"TextColor") 
AppendPopup(popup,popback,"BackColor") 
AppendSeparator(popup) 
AppendTextItem(popup,109,"CloseApp") 
 
 
Window("test") 
SetClientRect(WinHwnd,400,200) 
constant edit=Control(MultiEdit,"",50,0,350,200) 
SetColor(edit,txtcol,backcol) 
Activate(edit) 
-- SubClass the EditControl and handle the WM_RBUTTONDOWN yourself  
 
function EditWndProc(atom hwnd,atom msg,atom wParam,atom lParam) 
sequence rect,selec,newselec 
atom lengthselec 
    if equal(msg,WM_CONTEXTMENU) then 
		rect=GetWindowRect(edit) 
		if SendMessage(edit,EM_CANUNDO,0,0) then 
			EnableItem(popup,100,True) 
		else 
			EnableItem(popup,100,False) 
		end if 
		selec=GetSelection(edit) 
		lengthselec=selec[2]-selec[1] 
		if lengthselec then 
			EnableItem(popup,{101,103,104},True) 
		else 
			EnableItem(popup,{101,103,104},False) 
		end if 
		SetSelection(edit,-1,-1) 
		newselec=GetSelection(edit) 
		if newselec[2]>0 then 
			EnableItem(popup,105,True) 
		else 
			EnableItem(popup,105,False) 
		end if 
		SetSelection(edit,selec[1],selec[2]) 
		TrackPopupMenu(popup,LoWord(lParam),HiWord(lParam),WinHwnd) 
	return 0 -- WM_RBUTTONDOWN never reaches the original EditWindowProc  
		   -- so the default menu never appears 
    end if 
    return  CallWindowProc(OldWndProc,hwnd,msg,wParam,lParam) 
end function 
 
constant OldWndProc=GetWindowLong(edit,GWL_WNDPROC) 
SetWindowLong(edit,GWL_WNDPROC,call_back(routine_id("EditWndProc"))) 
 
-- Your handler for your menu 
 
procedure menuhandler() 
sequence f 
atom textfont 
	switch EventItem do 
		case 100 then 
			SendMessage(edit,EM_UNDO,0,0) 
		case 101 then  
			SendMessage(edit,WM_COPY,0,0) 
		case 102 then 
			SendMessage(edit,WM_PASTE,0,0) 
		case 103 then 
			SendMessage(edit,WM_CUT,0,0) 
		case 104 then 
			SendMessage(edit,WM_CLEAR,0,0) 
		case 105 then 
			SendMessage(edit,EM_SETSEL,0,-1) 
		case 106 then 
			f=ChooseFont() 
			if sequence(f[1]) then  
				textfont=NewFont(f[1],f[2],f[3],f[4],f[5]) 
				SetFont(edit,textfont)  
			end if 
		case 109 then 
			CloseApp() 
		case 201,202,203,204,205 then 
			EnableItem(popback,{301,302,303,304,305},True) 
			txtcol=colors[EventItem-200] 
			EnableItem(popback,EventItem-200+300,False) 
			SetColor(edit,txtcol,backcol) 
		case 301,302,303,304,305 then 
			EnableItem(poptxt,{201,202,203,204,205},True) 
			backcol=colors[EventItem-300] 
			EnableItem(poptxt,EventItem-300+200,False) 
			SetColor(edit,txtcol,backcol) 
		-- and so on ... 
	end switch 
end procedure 
SetHandler(WinHwnd,Menu,routine_id("menuhandler")) 
 
WinMain() 
 
 
new topic     » goto parent     » topic index » view message » categorize

5. Re: How to add to Edit Text default context menu

andi49 said...

Hi

I think win32lib has much more to offer for bigger projects.
What do you mean with 'completly replace the functionality of the context menu'? That's a real big task.
I added some of the functionality (based on the picture in your link) to the program (only using functions allready in tinewg). 'Paste' is still not handled
and there is no 'getColorDialog' like in win32lib.

But maybe it helps you to go on.

Andreas

This is amazing. If this library can call the Windows font dialog, why can't it call the Windows color dialog? The disabling of Paste requires the ability to interrogate the Windows clipboard. That is a minor issue, since if there is nothing to paste, nothing will get pasted. Actually, I really like your library, and I was hoping that we could work together to expand it, if I were to port my program to your library, and found things were missing. If you are interested, let me know. If you would like the current version of my program, let me know. It is too large to paste in a forum.

new topic     » goto parent     » topic index » view message » categorize

6. Re: How to add to Edit Text default context menu

penpal0andrew said...

[...] If you are interested, let me know. If you would like the current version of my program, let me know. It is too large to paste in a forum.

Hi

Yes, i think this can be interressting. Please, drop me a line, you will find a valid email adress in tinewg.exw.
(but keep in mind i'am not a professional programer and only program in my sparetime)

Andreas

new topic     » goto parent     » topic index » view message » categorize

7. Re: How to add to Edit Text default context menu

Here is an example of something I've done a few times. You can trap the WM_CONTEXTMENU message using w32HEvent and then display your own Popup menu instead. It's really quite straight-forward. From there you can use whatever menu you'd like. In this example, I've added the control key shortcuts and an "About" menu option.

-- w32Text.ew 
-- Here are several extra Win32Lib EditText functions 
-- 
-- can_clear() 
-- can_copy() 
-- can_cut() 
-- can_paste() 
-- can_select() 
-- can_undo() 
-- get_selection() 
-- select_all() 
-- 
 
include Win32Lib.ew 
 
public function get_selection( integer id ) 
     
    atom result = sendMessage( id, EM_GETSEL, 0, 0 ) 
     
    integer index = w32lo_word( result ) 
    integer range = w32hi_word( result ) 
     
    return {index,range} 
end function 
 
public function can_undo( integer id ) 
    return sendMessage( id, EM_CANUNDO, 0, 0 ) 
end function 
 
public function can_cut( integer id ) 
    sequence sel = get_selection( id ) 
    return (sel[2] - sel[1]) > 0 
end function 
 
public function can_copy( integer id ) 
    sequence sel = get_selection( id ) 
    return (sel[2] - sel[1]) > 0 
end function 
 
public function can_paste( integer id ) 
    return w32Func( xIsClipboardFormatAvailable, {CF_TEXT} ) 
end function 
 
public function can_clear( integer id ) 
    sequence sel = get_selection( id ) 
    return (sel[2] - sel[1]) > 0 
end function 
 
public function can_select( integer id ) 
    sequence text = getText( id ) 
    return length( text ) > 0 
end function 
 
public procedure select_all( integer id ) 
    setIndex( id, {1,0} ) 
end procedure 
-- context.exw 
-- Custom context menu example 
 
-- WM_CONTEXTMENU message 
-- http://msdn.microsoft.com/en-us/library/windows/desktop/ms647592%28v=vs.85%29.aspx 
 
include Win32Lib.ew 
include w32Text.ew 
without warning 
 
constant WM_CONTEXTMENU = #007B 
 
constant 
    Main            = create( Window, "Context", 0, Default, Default, 640, 480, 0 ), 
    Text            = create( MleText, "", Main, 0, 0, 0, 0, 0 ), 
$ 
 
constant 
    Context_Menu    = create( Popup, "", 0, 0, 0, 0, 0, 0 ), 
    Context_Undo    = create( MenuItem, "&Undo\tCtrl+Z", Context_Menu, 0, 0, 0, 0, 0 ), 
    Context_Sep1    = create( MenuItem, "-", Context_Menu, 0, 0, 0, 0, 0 ), 
    Context_Cut     = create( MenuItem, "Cu&t\tCtrl+X", Context_Menu, 0, 0, 0, 0, 0 ), 
    Context_Copy    = create( MenuItem, "&Copy\tCtrl+C", Context_Menu, 0, 0, 0, 0, 0 ), 
    Context_Paste   = create( MenuItem, "&Paste\tCtrl+V", Context_Menu, 0, 0, 0, 0, 0 ), 
    Context_Delete  = create( MenuItem, "Delete\tDel", Context_Menu, 0, 0, 0, 0, 0 ), 
    Context_Sep2    = create( MenuItem, "-", Context_Menu, 0, 0, 0, 0, 0 ), 
    Context_Select  = create( MenuItem, "Select &All\tCtrl+A", Context_Menu, 0, 0, 0, 0, 0 ), 
    Context_Sep3    = create( MenuItem, "-", Context_Menu, 0, 0, 0, 0, 0 ), 
    Context_About   = create( MenuItem, "About...", Context_Menu, 0, 0, 0, 0, 0 ), 
$ 
 
setFont( Text, "Courier New", 10, Normal ) 
removeStyle( Text, WS_BORDER ) -- remove the border 
 
procedure Main_OnResize( integer self, integer event, sequence params ) 
     
    -- resize the text control to fit 
    setRect( Text, {w32Edge,0}, {w32Edge,0}, {w32Edge,0}, {w32Edge,0}, w32True ) 
     
end procedure 
setHandler( Main, w32HResize, routine_id("Main_OnResize") ) 
 
procedure Text_OnEvent( integer self, integer event, sequence params ) 
     
    atom winmsg = params[1] 
    atom wparam = params[2] 
    atom lparam = params[3] 
     
    if winmsg = WM_CONTEXTMENU then 
        -- process the context menu message 
         
        -- get the context menu location 
        integer x = w32lo_word( lparam ) 
        integer y = w32hi_word( lparam ) 
         
        -- update the context menu options 
        setEnable( Context_Undo,   can_undo(self)   ) 
        setEnable( Context_Cut,    can_cut(self)    ) 
        setEnable( Context_Copy,   can_copy(self)   ) 
        setEnable( Context_Paste,  can_paste(self)  ) 
        setEnable( Context_Delete, can_clear(self)  ) 
        setEnable( Context_Select, can_select(self) ) 
         
        -- translate the coordinates 
        sequence posn = ScreenToClient( Main, x, y ) 
         
        -- show the menu 
        popup( Context_Menu, posn[1], posn[2] ) 
         
        -- return true to show we processed this message 
        returnValue( w32True ) 
         
    end if 
     
end procedure 
setHandler( Text, w32HEvent, routine_id("Text_OnEvent") ) 
 
procedure Text_OnKeyDown( integer self, integer event, sequence params ) 
     
    integer keyCode = params[1] 
    integer shift = params[2] 
     
    if and_bits( shift, ControlMask ) = ControlMask and keyCode = 'A' then -- Ctrl+A 
         
        select_all( Text ) 
         
        -- return true to show we processed this message 
        returnValue( w32True ) 
         
    end if 
     
end procedure 
setHandler( Text, w32HKeyDown, routine_id("Text_OnKeyDown") ) 
 
procedure Undo_OnClick( integer self, integer event, sequence params ) 
     
    undo( Text ) 
     
end procedure 
setHandler( Context_Undo, w32HClick, routine_id("Undo_OnClick") ) 
 
procedure Cut_OnClick( integer self, integer event, sequence params ) 
     
    cut( Text ) 
     
end procedure 
setHandler( Context_Cut, w32HClick, routine_id("Cut_OnClick") ) 
 
procedure Copy_OnClick( integer self, integer event, sequence params ) 
     
    copy( Text ) 
     
end procedure 
setHandler( Context_Copy, w32HClick, routine_id("Copy_OnClick") ) 
 
procedure Paste_OnClick( integer self, integer event, sequence params ) 
     
    paste( Text ) 
     
end procedure 
setHandler( Context_Paste, w32HClick, routine_id("Paste_OnClick") ) 
 
procedure Delete_OnClick( integer self, integer event, sequence params ) 
     
    clear( Text ) 
     
end procedure 
setHandler( Context_Delete, w32HClick, routine_id("Delete_OnClick") ) 
 
procedure Select_OnClick( integer self, integer event, sequence params ) 
     
    select_all( Text ) 
     
end procedure 
setHandler( Context_Select, w32HClick, routine_id("Select_OnClick") ) 
 
procedure About_OnClick( integer self, integer event, sequence params ) 
     
    sequence about_text = "Context menu example\n" 
        & "\n" 
        & "by Gregory Haberek\n" 
        & "<ghaberek@gmail.com>" 
     
    message_box( about_text, "About", MB_ICONINFORMATION ) 
     
end procedure 
setHandler( Context_About, w32HClick, routine_id("About_OnClick") ) 
 
WinMain( Main, Normal ) 

-Greg

new topic     » goto parent     » topic index » view message » categorize

8. Re: How to add to Edit Text default context menu

ghaberek said...

Here is an example of something I've done a few times. You can trap the WM_CONTEXTMENU message using w32HEvent and then display your own Popup menu instead. It's really quite straight-forward. From there you can use whatever menu you'd like. In this example, I've added the control key shortcuts and an "About" menu option.

-Greg

It's a kind of fun, two libs, different code, same bug ;) Try to Press the Menu-Key or Shift-F10

WM_CONTEXTMENU returns -1 -1 as position in lParam if it is started from the Keyboard
Andreas

new topic     » goto parent     » topic index » view message » categorize

9. Re: How to add to Edit Text default context menu

andi49 said...

It's a kind of fun, two libs, different code, same bug ;) Try to Press the Menu-Key or Shift-F10

WM_CONTEXTMENU returns -1 -1 as position in lParam if it is started from the Keyboard

Not a bug. This is by design. See the Microsoft developer docs ...

Microsoft said...

WM_CONTEXTMENU

If the context menu is generated from the keyboard, then the x- and y-coordinates are –1 and the application should display the context menu at the location of the current selection rather than at (xPos, yPos).

new topic     » goto parent     » topic index » view message » categorize

10. Re: How to add to Edit Text default context menu

DerekParnell said...

Not a bug. This is by design. See the Microsoft developer docs ...

Hallo

I have to excuse my bad English.

What i meant was not 'there is a bug in the Win32Api'.
I meant there is a bug in the program (at least in my one) not handling this circumstance.

Andreas

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu