From 8ea898e863c8c62fc0230d1c76d58bc477a16d14 Mon Sep 17 00:00:00 2001 From: mitchell <70453897+667e-11@users.noreply.github.com> Date: Sat, 14 Feb 2009 15:14:37 -0500 Subject: Added textadept.context_menu field for a custom popup context menu. --- src/lua_interface.c | 20 ++++++++++++++++++++ src/textadept.c | 14 ++++++++++++++ src/textadept.h | 1 + 3 files changed, 35 insertions(+) (limited to 'src') diff --git a/src/lua_interface.c b/src/lua_interface.c index b6739c15..369fd90d 100644 --- a/src/lua_interface.c +++ b/src/lua_interface.c @@ -747,6 +747,26 @@ void l_handle_scnnotification(SCNotification *n) { l_call_function(1); } +/** + * Requests and pops up a context menu for the Scintilla view. + * @param event The mouse button event. + */ +void l_ta_popup_context_menu(GdkEventButton *event) { + lua_getglobal(lua, "textadept"); + if (lua_istable(lua, -1)) { + lua_getfield(lua, -1, "context_menu"); + if (lua_isuserdata(lua, -1)) { + GtkWidget *menu = l_togtkwidget(lua, -1); + gtk_widget_show_all(menu); + gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, + event ? event->button : 0, + gdk_event_get_time(reinterpret_cast(event))); + } else if (!lua_isnil(lua, -1)) + warn("textadept.context_menu is not a gtkmenu."); + lua_pop(lua, 1); // textadept.context_menu + } else lua_pop(lua, 1); +} + /** * Executes a given command string as Lua code. * @param command Lua code to execute. diff --git a/src/textadept.c b/src/textadept.c index ee2f4da6..2b0a17d0 100644 --- a/src/textadept.c +++ b/src/textadept.c @@ -20,6 +20,7 @@ GtkWidget *window, *focused_editor, *menubar, *statusbar, *docstatusbar; static void t_notification(GtkWidget*, gint, gpointer lParam, gpointer); static void t_command(GtkWidget *editor, gint wParam, gpointer, gpointer); static gbool t_keypress(GtkWidget*, GdkEventKey *event, gpointer); +static gbool t_buttonpress(GtkWidget*, GdkEventButton *event, gpointer); static gbool w_focus(GtkWidget*, GdkEventFocus *, gpointer); static gbool w_keypress(GtkWidget*, GdkEventKey *event, gpointer); static gbool w_exit(GtkWidget*, GdkEventAny*, gpointer); @@ -243,7 +244,9 @@ void create_ui() { GtkWidget *new_scintilla_window(sptr_t buffer_id) { GtkWidget *editor = scintilla_new(); gtk_widget_set_size_request(editor, 1, 1); // minimum size + SS(SCINTILLA(editor), SCI_USEPOPUP, 0, 0); signal(editor, "key_press_event", t_keypress); + signal(editor, "button_press_event", t_buttonpress); signal(editor, "command", t_command); signal(editor, SCINTILLA_NOTIFY, t_notification); l_add_scintilla_window(editor); @@ -490,6 +493,17 @@ static gbool t_keypress(GtkWidget*, GdkEventKey *event, gpointer) { return l_handle_keypress(event->keyval, shift, control, alt) ? TRUE : FALSE; } +/** + * Signal for a Scintilla mouse click. + * If it is a right-click, popup a context menu. + * @see l_ta_popup_context_menu + */ +static gbool t_buttonpress(GtkWidget*, GdkEventButton *event, gpointer) { + if (event->type != GDK_BUTTON_PRESS || event->button != 3) return FALSE; + l_ta_popup_context_menu(event); + return TRUE; +} + /** * Signal for a Textadept window focus change. */ diff --git a/src/textadept.h b/src/textadept.h index 303c3ddf..f017ad82 100644 --- a/src/textadept.h +++ b/src/textadept.h @@ -88,6 +88,7 @@ bool l_handle_event(const char *e, const char *arg=NULL); bool l_handle_keypress(int keyval, bool shift, bool control, bool alt); void l_handle_scnnotification(SCNotification *n); void l_ta_command(const char *command); +void l_ta_popup_context_menu(GdkEventButton *event); bool l_cec_get_completions_for(const char *entry_text); void l_cec_populate(GtkListStore *store); -- cgit v1.2.3