Re: How to add to Edit Text default context menu
- Posted by ghaberek (admin) Nov 27, 2013
- 2101 views
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