aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/.textadept.lua3
-rw-r--r--core/ext/menu.lua12
-rw-r--r--src/lua_interface.c20
-rw-r--r--src/textadept.c14
-rw-r--r--src/textadept.h1
5 files changed, 49 insertions, 1 deletions
diff --git a/core/.textadept.lua b/core/.textadept.lua
index 3a0442fa..d46f6331 100644
--- a/core/.textadept.lua
+++ b/core/.textadept.lua
@@ -15,6 +15,7 @@ module('textadept')
-- @field focused_doc_pointer The pointer to the document associated with the
-- buffer of the currently focused view. (Used internally; read-only)
-- @field menubar A table of GTK menus defining a menubar (write-only).
+-- @field context_menu A GTK menu defining the editor's context menu.
-- @field clipboard_text The text on the clipboard (read-only).
-- @field statusbar_text The text displayed by the statusbar (write-only).
-- @field docstatusbar_text The text displayed by the doc statusbar
@@ -22,7 +23,7 @@ module('textadept')
-- @field size The size of the Textadept window ({ width, height}).
textadept = {
title = nil, focused_doc_pointer = nil, clipboard_text = nil, menubar = nil,
- statusbar_text = nil, docstatusbar_text = nil, size = nil
+ context_menu = nil, statusbar_text = nil, docstatusbar_text = nil, size = nil
}
---
diff --git a/core/ext/menu.lua b/core/ext/menu.lua
index 1a2b06ed..3b3ec7ef 100644
--- a/core/ext/menu.lua
+++ b/core/ext/menu.lua
@@ -585,3 +585,15 @@ t.events.add_handler('menu_clicked',
end
end
end)
+
+t.context_menu = gtkmenu {
+ { l.MENU_EDIT_UNDO, ID.UNDO },
+ { l.MENU_EDIT_REDO, ID.REDO },
+ { SEPARATOR, ID.SEPARATOR },
+ { l.MENU_EDIT_CUT, ID.CUT },
+ { l.MENU_EDIT_COPY, ID.COPY },
+ { l.MENU_EDIT_PASTE, ID.PASTE },
+ { l.MENU_EDIT_DELETE, ID.DELETE },
+ { SEPARATOR, ID.SEPARATOR },
+ { l.MENU_EDIT_SELECT_ALL, ID.SELECT_ALL }
+}
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
@@ -748,6 +748,26 @@ void l_handle_scnnotification(SCNotification *n) {
}
/**
+ * 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<GdkEvent*>(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);
@@ -491,6 +494,17 @@ static gbool t_keypress(GtkWidget*, GdkEventKey *event, gpointer) {
}
/**
+ * 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.
*/
static gbool w_focus(GtkWidget*, GdkEventFocus*, gpointer) {
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);