aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormitchell <70453897+667e-11@users.noreply.github.com>2009-02-14 23:29:50 -0500
committermitchell <70453897+667e-11@users.noreply.github.com>2009-02-14 23:29:50 -0500
commita90bc9a983f98fed2d4f9e386a6018042e2b3611 (patch)
tree4ba2503bfa6f54a5f9233f74ed41acb643170341 /src
parent24bc238e3d80a5a7eaf74a6199e3e71754e12f04 (diff)
downloadtextadept-a90bc9a983f98fed2d4f9e386a6018042e2b3611.tar.gz
textadept-a90bc9a983f98fed2d4f9e386a6018042e2b3611.zip
Refactored Project Manager code.
Diffstat (limited to 'src')
-rw-r--r--src/lua_interface.c128
-rw-r--r--src/textadept.c103
-rw-r--r--src/textadept.h11
3 files changed, 116 insertions, 126 deletions
diff --git a/src/lua_interface.c b/src/lua_interface.c
index 86b3150f..7a98451b 100644
--- a/src/lua_interface.c
+++ b/src/lua_interface.c
@@ -770,36 +770,51 @@ void l_ta_popup_context_menu(GdkEventButton *event) {
// Project Manager
/**
- * Requests contents for the Project Manager.
- * @param entry_text The text in the Project Manager Entry. If NULL, the full
- * path table is at the top of the Lua stack.
- * @param expanding Flag indicating whether or not a treenode is being expanded.
- * If true, the tree is walked up from the node to top creating a full path
- * table at the stack top to be used essentially as entry_text.
- * @see l_pm_get_full_path
+ * Creates and pushes a Lua table of parent nodes for the given Project Manager
+ * treeview path.
+ * The first table item is the PM Entry text, the next items are parents of the
+ * given node in descending order, and the last item is the given node itself.
+ * @param path The GtkTreePath of the node. If NULL, only the PM Entry text is
+ * contained in the resulting table.
*/
-bool l_pm_get_contents_for(const char *entry_text, bool expanding) {
- if (!l_ista2function("pm", "get_contents_for")) return false;
- if (entry_text) {
- lua_newtable(lua);
- lua_pushstring(lua, entry_text);
- lua_rawseti(lua, -2, 1);
- } else l_insert(lua, -1); // shift full_path down
- lua_pushboolean(lua, expanding);
- return l_call_function(2, 1, true);
+void l_pushpathtable(GtkTreePath *path) {
+ lua_newtable(lua);
+ lua_pushstring(lua, gtk_entry_get_text(GTK_ENTRY(pm_entry)));
+ lua_rawseti(lua, -2, 1);
+ if (!path) return;
+ GtkTreeIter iter;
+ while (gtk_tree_path_get_depth(path) > 0) {
+ char *item = 0;
+ gtk_tree_model_get_iter(GTK_TREE_MODEL(pm_store), &iter, path);
+ gtk_tree_model_get(GTK_TREE_MODEL(pm_store), &iter, 1, &item, -1);
+ lua_pushstring(lua, item);
+ lua_rawseti(lua, -2, gtk_tree_path_get_depth(path) + 1);
+ g_free(item);
+ gtk_tree_path_up(path);
+ }
}
/**
- * Populates the Project Manager pane with the contents of a Lua table at the
- * stack top.
- * @param initial_iter The initial GtkTreeIter. If not NULL, it is a treenode
- * being expanded and the contents will be added to that expanding node.
- * @see l_pm_get_contents_for
+ * Requests and adds contents to the Project Manager view.
+ * @param initial_iter An initial GtkTreeIter. If NULL, contents will be added
+ * to the treeview root. Otherwise they will be added to this parent node.
*/
-void l_pm_populate(GtkTreeIter *initial_iter) {
+void l_pm_view_fill(GtkTreeIter *initial_iter) {
+ if (!l_ista2function("pm", "get_contents_for")) return;
+ if (initial_iter) {
+ GtkTreePath *path =
+ gtk_tree_model_get_path(GTK_TREE_MODEL(pm_store), initial_iter);
+ l_pushpathtable(path);
+ gtk_tree_path_free(path);
+ } else l_pushpathtable(NULL);
+ lua_pushboolean(lua, initial_iter != NULL);
+ l_call_function(2, 1, true);
+ if (!lua_istable(lua, -1)) {
+ lua_pop(lua, 1); // non-table return
+ return warn("pm.get_contents_for: return not a table.");
+ }
+
GtkTreeIter iter, child;
- if (!lua_istable(lua, -1))
- return warn("pm.get_contents_for return not a table.");
if (!initial_iter) gtk_tree_store_clear(pm_store);
lua_pushnil(lua);
while (lua_next(lua, -2)) {
@@ -829,76 +844,45 @@ void l_pm_populate(GtkTreeIter *initial_iter) {
}
/**
- * For a Project Manager given node, get the full path to that node.
- * It leaves a full path table at the top of the Lua stack.
- * @param path The GtkTreePath of the node.
- */
-void l_pm_get_full_path(GtkTreePath *path) {
- lua_newtable(lua);
- lua_pushstring(lua, gtk_entry_get_text(GTK_ENTRY(pm_entry)));
- lua_rawseti(lua, -2, 1);
- if (!path) return;
- GtkTreeIter iter;
- char *filename;
- while (gtk_tree_path_get_depth(path) > 0) {
- gtk_tree_model_get_iter(GTK_TREE_MODEL(pm_store), &iter, path);
- gtk_tree_model_get(GTK_TREE_MODEL(pm_store), &iter, 1, &filename, -1);
- lua_pushstring(lua, filename);
- lua_rawseti(lua, -2, gtk_tree_path_get_depth(path) + 1);
- g_free(filename);
- gtk_tree_path_up(path);
- }
-}
-
-/**
- * Requests and pops up a context menu for the Project Manager.
+ * Requests and pops up a context menu for a selected Project Manager item.
+ * @param path The GtkTreePath of the item.
* @param event The mouse button event.
* @param callback The GCallback associated with each menu item.
*/
-void l_pm_popup_context_menu(GdkEventButton *event, GCallback callback) {
+void l_pm_popup_context_menu(GtkTreePath *path, GdkEventButton *event,
+ GCallback callback) {
if (!l_ista2function("pm", "get_context_menu")) return;
- GtkTreeIter iter;
- GtkTreePath *path = 0;
- GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_view));
- if (gtk_tree_selection_get_selected(sel, NULL, &iter))
- path = gtk_tree_model_get_path(GTK_TREE_MODEL(pm_store), &iter);
- l_pm_get_full_path(path);
- if (path) gtk_tree_path_free(path);
- if (lua_objlen(lua, -1) == 0) {
- lua_pop(lua, 2); // function and full_path
- return;
- }
- if (l_call_function(1, 1, true) && lua_istable(lua, -1)) {
+ l_pushpathtable(path);
+ l_call_function(1, 1, true);
+ if (lua_istable(lua, -1)) {
GtkWidget *menu = l_create_gtkmenu(lua, callback, false);
- lua_pop(lua, 1); // returned table
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 warn("pm.get_context_menu return was not a table.");
+ } else warn("pm.get_context_menu: return was not a table.");
+ lua_pop(lua, 1); // returned value
}
/**
- * Performs an action for an activated item in the Project Manager.
- * The full path table for the item is at the top of the Lua stack.
+ * Performs an action for the selected Project Manager item.
+ * @param path The GtkTreePath of the item.
*/
-void l_pm_perform_action() {
+void l_pm_perform_action(GtkTreePath *path) {
if (!l_ista2function("pm", "perform_action")) return;
- l_insert(lua, -1); // shift full_path down
+ l_pushpathtable(path);
l_call_function(1);
}
/**
- * Performs a selected menu action from an item's context menu in the Project
- * Manager.
- * The full path table for the item is at the top of the Lua stack.
+ * Performs a selected menu action from a Project Manager item's context menu.
+ * @param path The GtkTreePath of the item.
* @param menu_id The numeric ID for the menu item.
*/
-void l_pm_perform_menu_action(int menu_id) {
+void l_pm_perform_menu_action(GtkTreePath *path, int menu_id) {
if (!l_ista2function("pm", "perform_menu_action")) return;
- l_insert(lua, -1); // shift full_path down
lua_pushnumber(lua, menu_id);
- l_insert(lua, -1); // shift full_path down
+ l_pushpathtable(path);
l_call_function(2);
}
diff --git a/src/textadept.c b/src/textadept.c
index b159deb6..a53cc48f 100644
--- a/src/textadept.c
+++ b/src/textadept.c
@@ -38,15 +38,15 @@ static int pm_search_equal_func(GtkTreeModel *model, int col, const char *key,
GtkTreeIter *iter, gpointer);
static int pm_sort_iter_compare_func(GtkTreeModel *model, GtkTreeIter *a,
GtkTreeIter *b, gpointer);
-static void pm_entry_activated(GtkWidget *widget, gpointer);
-static void pm_entry_changed(GtkComboBoxEntry *widget, gpointer);
+static void pm_entry_activated(GtkWidget *, gpointer);
+static void pm_entry_changed(GtkComboBoxEntry *, gpointer);
static gbool pm_keypress(GtkWidget *, GdkEventKey *event, gpointer);
-static void pm_row_expanded(GtkTreeView *, GtkTreeIter *iter,
- GtkTreePath *path, gpointer);
+static void pm_row_expanded(GtkTreeView *, GtkTreeIter *iter, GtkTreePath *,
+ gpointer);
static void pm_row_collapsed(GtkTreeView *, GtkTreeIter *iter, GtkTreePath *,
gpointer);
-static void pm_row_activated(GtkTreeView *, GtkTreePath *, GtkTreeViewColumn *,
- gpointer);
+static void pm_row_activated(GtkTreeView *, GtkTreePath *path,
+ GtkTreeViewColumn *, gpointer);
static gbool pm_button_press(GtkTreeView *, GdkEventButton *event, gpointer);
static gbool pm_popup_menu(GtkWidget *, gpointer);
static void pm_menu_activate(GtkWidget *, gpointer menu_id);
@@ -683,21 +683,20 @@ static int pm_sort_iter_compare_func(GtkTreeModel *model, GtkTreeIter *a,
/**
* Signal for the activation of the Project Manager entry.
- * Requests contents for the treeview.
- * @see l_pm_get_contents_for
+ * Requests contents for the Project Manager.
+ * @see l_pm_view_fill
*/
-static void pm_entry_activated(GtkWidget *widget, gpointer) {
- const char *entry_text = gtk_entry_get_text(GTK_ENTRY(widget));
- if (l_pm_get_contents_for(entry_text, false)) l_pm_populate(NULL);
+static void pm_entry_activated(GtkWidget *, gpointer) {
+ l_pm_view_fill(NULL);
}
/**
* Signal for a change of the text in the Project Manager entry.
- * Calls pm_entry_activated to populate the treeview.
- * @see pm_entry_activated
+ * Requests contents for the Project Manager.
+ * @see l_pm_view_fill
*/
-static void pm_entry_changed(GtkComboBoxEntry *widget, gpointer) {
- pm_entry_activated(gtk_bin_get_child(GTK_BIN(widget)), NULL);
+static void pm_entry_changed(GtkComboBoxEntry *, gpointer) {
+ l_pm_view_fill(NULL);
}
/**
@@ -717,28 +716,27 @@ static gbool pm_keypress(GtkWidget *, GdkEventKey *event, gpointer) {
/**
* Signal for a Project Manager parent expansion.
* Requests contents for a Project Manager parent node being opened.
- * Since parents have a dummy child by default just to indicate they are indeed
- * parents, that dummy child is removed now.
- * @see l_pm_get_contents_for
+ * Since a parent is given a dummy child by default in order to indicate that
+ * it is a parent, that dummy child is removed.
+ * @see l_pm_view_fill
*/
-static void pm_row_expanded(GtkTreeView *, GtkTreeIter *iter,
- GtkTreePath *path, gpointer) {
- l_pm_get_full_path(path);
- if (l_pm_get_contents_for(NULL, true)) l_pm_populate(iter);
+static void pm_row_expanded(GtkTreeView *, GtkTreeIter *iter, GtkTreePath *,
+ gpointer) {
+ l_pm_view_fill(iter);
GtkTreeIter child;
- char *filename;
+ char *item;
gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(pm_store), &child, iter, 0);
- gtk_tree_model_get(GTK_TREE_MODEL(pm_store), &child, 1, &filename, -1);
- if (strcmp(reinterpret_cast<const char*>(filename), "\0dummy") == 0)
+ gtk_tree_model_get(GTK_TREE_MODEL(pm_store), &child, 1, &item, -1);
+ if (strcmp(reinterpret_cast<const char*>(item), "\0dummy") == 0)
gtk_tree_store_remove(pm_store, &child);
- g_free(filename);
+ g_free(item);
}
/**
* Signal for a Project Manager parent collapse.
* Removes all Project Manager children from a parent node being closed.
- * It does add a dummy child by default to indicate the parent is indeed a
- * parent. It will be removed when the parent is opened.
+ * Re-adds a dummy child to indicate this parent is still a parent. It will be
+ * removed when the parent is re-opened.
*/
static void pm_row_collapsed(GtkTreeView *, GtkTreeIter *iter, GtkTreePath *,
gpointer) {
@@ -757,33 +755,42 @@ static void pm_row_collapsed(GtkTreeView *, GtkTreeIter *iter, GtkTreePath *,
* collapsed. If the node is not a parent at all, a Lua action is performed.
* @see l_pm_perform_action
*/
-static void pm_row_activated(GtkTreeView *, GtkTreePath *, GtkTreeViewColumn *,
- gpointer) {
+static void pm_row_activated(GtkTreeView *, GtkTreePath *path,
+ GtkTreeViewColumn *, gpointer) {
GtkTreeIter iter;
- GtkTreePath *path;
- GtkTreeViewColumn *column;
- gtk_tree_view_get_cursor(GTK_TREE_VIEW(pm_view), &path, &column);
gtk_tree_model_get_iter(GTK_TREE_MODEL(pm_store), &iter, path);
- if (gtk_tree_model_iter_has_child(GTK_TREE_MODEL(pm_store), &iter))
+ if (gtk_tree_model_iter_has_child(GTK_TREE_MODEL(pm_store), &iter)) {
if (gtk_tree_view_row_expanded(GTK_TREE_VIEW(pm_view), path))
gtk_tree_view_collapse_row(GTK_TREE_VIEW(pm_view), path);
else
gtk_tree_view_expand_row(GTK_TREE_VIEW(pm_view), path, FALSE);
- else {
- l_pm_get_full_path(path);
- l_pm_perform_action();
- }
- gtk_tree_path_free(path);
+ } else l_pm_perform_action(path);
+}
+
+/**
+ * Helper function to return the path of the selected Project Manager view item
+ * (if any).
+ * The returned GtkTreePath must be freed if it is not NULL.
+ */
+static GtkTreePath *pm_view_get_selection_path() {
+ GtkTreeIter iter;
+ GtkTreePath *path = 0;
+ GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_view));
+ if (gtk_tree_selection_get_selected(sel, NULL, &iter))
+ path = gtk_tree_model_get_path(GTK_TREE_MODEL(pm_store), &iter);
+ return path;
}
/**
* Signal for a Project Manager mouse click.
- * If it is a right-click, popup a context menu for the selected node.
+ * If it is a right-click, popup a context menu for the selected item.
* @see l_pm_popup_context_menu
*/
static gbool pm_button_press(GtkTreeView *, GdkEventButton *event, gpointer) {
if (event->type != GDK_BUTTON_PRESS || event->button != 3) return FALSE;
- l_pm_popup_context_menu(event, G_CALLBACK(pm_menu_activate));
+ GtkTreePath *path = pm_view_get_selection_path();
+ l_pm_popup_context_menu(path, event, G_CALLBACK(pm_menu_activate));
+ if (path) gtk_tree_path_free(path);
return TRUE;
}
@@ -793,22 +800,22 @@ static gbool pm_button_press(GtkTreeView *, GdkEventButton *event, gpointer) {
* @see l_pm_popup_context_menu
*/
static gbool pm_popup_menu(GtkWidget *, gpointer) {
- l_pm_popup_context_menu(NULL, G_CALLBACK(pm_menu_activate));
+ GtkTreePath *path = pm_view_get_selection_path();
+ l_pm_popup_context_menu(path, NULL, G_CALLBACK(pm_menu_activate));
+ if (path) gtk_tree_path_free(path);
return TRUE;
}
/**
* Signal for a selected Project Manager menu item.
- * Performs a Lua action for a selected Project Manager menu item.
+ * Performs a Lua action for a selected menu item.
* @param menu_id The numeric ID for the menu item.
* @see l_pm_perform_menu_action
*/
static void pm_menu_activate(GtkWidget *, gpointer menu_id) {
- GtkTreePath *path;
- GtkTreeViewColumn *column;
- gtk_tree_view_get_cursor(GTK_TREE_VIEW(pm_view), &path, &column);
- l_pm_get_full_path(path);
- l_pm_perform_menu_action(GPOINTER_TO_INT(menu_id));
+ GtkTreePath *path = pm_view_get_selection_path();
+ l_pm_perform_menu_action(path, GPOINTER_TO_INT(menu_id));
+ if (path) gtk_tree_path_free(path);
}
// Find/Replace
diff --git a/src/textadept.h b/src/textadept.h
index dce0db3a..771d90dd 100644
--- a/src/textadept.h
+++ b/src/textadept.h
@@ -85,12 +85,11 @@ bool l_handle_keypress(int keyval, bool shift, bool control, bool alt);
void l_handle_scnnotification(SCNotification *n);
void l_ta_popup_context_menu(GdkEventButton *event);
-bool l_pm_get_contents_for(const char *entry_text, bool expanding);
-void l_pm_populate(GtkTreeIter *initial_iter);
-void l_pm_get_full_path(GtkTreePath *path);
-void l_pm_perform_action();
-void l_pm_popup_context_menu(GdkEventButton *event, GCallback callback);
-void l_pm_perform_menu_action(int menu_id);
+void l_pm_view_fill(GtkTreeIter *initial_iter);
+void l_pm_perform_action(GtkTreePath *path);
+void l_pm_popup_context_menu(GtkTreePath *path, GdkEventButton *event,
+ GCallback callback);
+void l_pm_perform_menu_action(GtkTreePath *path, int menu_id);
void l_find(const char *ftext, bool next);
void l_find_replace(const char *rtext);