Skip to content

Commit

Permalink
workbench: added "Create file here..."/"Create directory here..."
Browse files Browse the repository at this point in the history
The user can now select from two new context menu items to either
create a new file or a new directory on the current selected position
in the file tree. See #660.
  • Loading branch information
lpaulsen93 committed Jan 21, 2018
1 parent e4f7265 commit 839819f
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 9 deletions.
8 changes: 8 additions & 0 deletions workbench/README
Expand Up @@ -133,6 +133,14 @@ These are the available items:
Remove file from the Workbench or project bookmarks. It is only available
if you right clicked on a bookmark.

**Create file here...**
Select this item to create a new file at the current selected position
in the file tree. Available since version 1.02 of the workbench plugin.

**Create directory here...**
Select this item to create a new directory at the current selected position
in the file tree. Available since version 1.02 of the workbench plugin.

Known issues
============

Expand Down
71 changes: 71 additions & 0 deletions workbench/src/dialogs.c
Expand Up @@ -28,6 +28,77 @@

extern GeanyPlugin *geany_plugin;


/** Shows the dialog "Create new file".
*
* The dialog lets the user create a new file (filter *).
*
* @param path The current folder
* @return The filename
*
**/
gchar *dialogs_create_new_file(const gchar *path)
{
gchar *filename = NULL;
GtkWidget *dialog;

dialog = gtk_file_chooser_dialog_new(_("Create new file"),
GTK_WINDOW(wb_globals.geany_plugin->geany_data->main_widgets->window), GTK_FILE_CHOOSER_ACTION_SAVE,
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("C_reate"), GTK_RESPONSE_ACCEPT, NULL);
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);

if (path != NULL)
{
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), path);
}

if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
{
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
}

gtk_widget_destroy(dialog);

return filename;
}


/** Shows the dialog "Create new directory".
*
* The dialog lets the user create a new directory.
*
* @param path The current folder
* @return The filename
*
**/
gchar *dialogs_create_new_directory(const gchar *path)
{
gchar *filename = NULL;
GtkWidget *dialog;

dialog = gtk_file_chooser_dialog_new(_("Create new directory"),
GTK_WINDOW(wb_globals.geany_plugin->geany_data->main_widgets->window), GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER,
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("C_reate"), GTK_RESPONSE_ACCEPT, NULL);
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);

if (path != NULL)
{
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), path);
}

if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
{
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
}

gtk_widget_destroy(dialog);

return filename;
}


/** Shows the dialog "Create new workbench".
*
* The dialog lets the user create a new workbench file (filter *.geanywb).
Expand Down
2 changes: 2 additions & 0 deletions workbench/src/dialogs.h
Expand Up @@ -19,6 +19,8 @@
#ifndef __WB_DIALOGS_H__
#define __WB_DIALOGS_H__

gchar *dialogs_create_new_file(const gchar *path);
gchar *dialogs_create_new_directory(const gchar *path);
gchar *dialogs_create_new_workbench(void);
gchar *dialogs_open_workbench(void);
gchar *dialogs_add_project(void);
Expand Down
2 changes: 1 addition & 1 deletion workbench/src/plugin_main.c
Expand Up @@ -128,7 +128,7 @@ void geany_load_module(GeanyPlugin *plugin)
/* Set metadata */
plugin->info->name = _("Workbench");
plugin->info->description = _("Manage and customize multiple projects.");
plugin->info->version = "1.01";
plugin->info->version = "1.02";
plugin->info->author = "LarsGit223";

/* Set functions */
Expand Down
114 changes: 114 additions & 0 deletions workbench/src/popup_menu.c
Expand Up @@ -19,9 +19,12 @@
/*
* Code for the popup menu.
*/
#include <errno.h>
#include <sys/time.h>
#include <gdk/gdkkeysyms.h>
#include <string.h>
#include <glib.h>
#include <glib/gstdio.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
Expand Down Expand Up @@ -57,6 +60,8 @@ static struct
GtkWidget *add_wb_bookmark;
GtkWidget *add_prj_bookmark;
GtkWidget *remove_bookmark;
GtkWidget *new_file;
GtkWidget *new_directory;
} s_popup_menu;


Expand Down Expand Up @@ -91,6 +96,8 @@ void popup_menu_show(POPUP_CONTEXT context, GdkEventButton *event)
gtk_widget_set_sensitive (s_popup_menu.remove_bookmark, FALSE);
gtk_widget_set_sensitive (s_popup_menu.subdir_open_all, FALSE);
gtk_widget_set_sensitive (s_popup_menu.subdir_close_all, FALSE);
gtk_widget_set_sensitive (s_popup_menu.new_file, FALSE);
gtk_widget_set_sensitive (s_popup_menu.new_directory, FALSE);
break;
case POPUP_CONTEXT_DIRECTORY:
gtk_widget_set_sensitive (s_popup_menu.add_project, TRUE);
Expand All @@ -110,6 +117,8 @@ void popup_menu_show(POPUP_CONTEXT context, GdkEventButton *event)
gtk_widget_set_sensitive (s_popup_menu.remove_bookmark, FALSE);
gtk_widget_set_sensitive (s_popup_menu.subdir_open_all, FALSE);
gtk_widget_set_sensitive (s_popup_menu.subdir_close_all, FALSE);
gtk_widget_set_sensitive (s_popup_menu.new_file, TRUE);
gtk_widget_set_sensitive (s_popup_menu.new_directory, TRUE);
break;
case POPUP_CONTEXT_SUB_DIRECTORY:
gtk_widget_set_sensitive (s_popup_menu.add_project, TRUE);
Expand All @@ -129,6 +138,8 @@ void popup_menu_show(POPUP_CONTEXT context, GdkEventButton *event)
gtk_widget_set_sensitive (s_popup_menu.remove_bookmark, FALSE);
gtk_widget_set_sensitive (s_popup_menu.subdir_open_all, TRUE);
gtk_widget_set_sensitive (s_popup_menu.subdir_close_all, TRUE);
gtk_widget_set_sensitive (s_popup_menu.new_file, TRUE);
gtk_widget_set_sensitive (s_popup_menu.new_directory, TRUE);
break;
case POPUP_CONTEXT_FILE:
gtk_widget_set_sensitive (s_popup_menu.add_project, TRUE);
Expand All @@ -148,6 +159,8 @@ void popup_menu_show(POPUP_CONTEXT context, GdkEventButton *event)
gtk_widget_set_sensitive (s_popup_menu.remove_bookmark, FALSE);
gtk_widget_set_sensitive (s_popup_menu.subdir_open_all, TRUE);
gtk_widget_set_sensitive (s_popup_menu.subdir_close_all, TRUE);
gtk_widget_set_sensitive (s_popup_menu.new_file, TRUE);
gtk_widget_set_sensitive (s_popup_menu.new_directory, TRUE);
break;
case POPUP_CONTEXT_BACKGROUND:
gtk_widget_set_sensitive (s_popup_menu.add_project, TRUE);
Expand All @@ -167,6 +180,8 @@ void popup_menu_show(POPUP_CONTEXT context, GdkEventButton *event)
gtk_widget_set_sensitive (s_popup_menu.remove_bookmark, FALSE);
gtk_widget_set_sensitive (s_popup_menu.subdir_open_all, FALSE);
gtk_widget_set_sensitive (s_popup_menu.subdir_close_all, FALSE);
gtk_widget_set_sensitive (s_popup_menu.new_file, FALSE);
gtk_widget_set_sensitive (s_popup_menu.new_directory, FALSE);
break;
case POPUP_CONTEXT_WB_BOOKMARK:
gtk_widget_set_sensitive (s_popup_menu.add_project, TRUE);
Expand All @@ -186,6 +201,8 @@ void popup_menu_show(POPUP_CONTEXT context, GdkEventButton *event)
gtk_widget_set_sensitive (s_popup_menu.remove_bookmark, TRUE);
gtk_widget_set_sensitive (s_popup_menu.subdir_open_all, FALSE);
gtk_widget_set_sensitive (s_popup_menu.subdir_close_all, FALSE);
gtk_widget_set_sensitive (s_popup_menu.new_file, FALSE);
gtk_widget_set_sensitive (s_popup_menu.new_directory, FALSE);
break;
case POPUP_CONTEXT_PRJ_BOOKMARK:
gtk_widget_set_sensitive (s_popup_menu.add_project, TRUE);
Expand All @@ -205,6 +222,8 @@ void popup_menu_show(POPUP_CONTEXT context, GdkEventButton *event)
gtk_widget_set_sensitive (s_popup_menu.remove_bookmark, TRUE);
gtk_widget_set_sensitive (s_popup_menu.subdir_open_all, FALSE);
gtk_widget_set_sensitive (s_popup_menu.subdir_close_all, FALSE);
gtk_widget_set_sensitive (s_popup_menu.new_file, FALSE);
gtk_widget_set_sensitive (s_popup_menu.new_directory, FALSE);
break;
}
gtk_menu_popup(GTK_MENU(s_popup_menu.widget), NULL, NULL, NULL, NULL,
Expand Down Expand Up @@ -506,6 +525,85 @@ static void popup_menu_on_subdir_close_all (G_GNUC_UNUSED GtkMenuItem *menuitem,
}


/* Handle popup menu item "Create new file here..." */
static void popup_menu_on_new_file(G_GNUC_UNUSED GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
{
gchar *filename, *path = NULL, *abs_path = NULL;
SIDEBAR_CONTEXT context;

if (sidebar_file_view_get_selected_context(&context))
{
if (context.subdir != NULL)
{
path = context.subdir;
abs_path = g_strdup(path);
}
else
{
path = wb_project_dir_get_base_dir(context.directory);
abs_path = get_combined_path(wb_project_get_filename(context.project), path);
}
}

filename = dialogs_create_new_file(abs_path);
if (filename == NULL)
{
return;
}
else if (!g_file_test (filename, G_FILE_TEST_EXISTS))
{
FILE *new_file;
new_file = g_fopen (filename, "w");
if (new_file == NULL)
{
dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Could not create new file \"%s\":\n\n%s"), filename, strerror(errno));
}
else
{
fclose(new_file);
wb_project_dir_rescan(context.project, context.directory);
sidebar_update(SIDEBAR_CONTEXT_DIRECTORY_RESCANNED, &context);
document_open_file(filename, FALSE, NULL, NULL);
}
}

g_free(abs_path);
g_free(filename);
}


/* Handle popup menu item "Create new directory here..." */
static void popup_menu_on_new_directory(G_GNUC_UNUSED GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
{
gchar *filename, *path = NULL, *abs_path = NULL;
SIDEBAR_CONTEXT context;

if (sidebar_file_view_get_selected_context(&context))
{
if (context.subdir != NULL)
{
path = context.subdir;
abs_path = g_strdup(path);
}
else
{
path = wb_project_dir_get_base_dir(context.directory);
abs_path = get_combined_path(wb_project_get_filename(context.project), path);
}
}

filename = dialogs_create_new_directory(abs_path);
if (filename != NULL)
{
wb_project_dir_rescan(context.project, context.directory);
sidebar_update(SIDEBAR_CONTEXT_DIRECTORY_RESCANNED, &context);
}

g_free(abs_path);
g_free(filename);
}


/** Setup/Initialize the popup menu.
*
**/
Expand Down Expand Up @@ -650,4 +748,20 @@ void popup_menu_init(void)
gtk_container_add(GTK_CONTAINER(s_popup_menu.widget), item);
g_signal_connect(item, "activate", G_CALLBACK(popup_menu_on_remove_from_bookmarks), NULL);
s_popup_menu.remove_bookmark = item;

item = gtk_separator_menu_item_new();
gtk_widget_show(item);
gtk_container_add(GTK_CONTAINER(s_popup_menu.widget), item);

item = gtk_menu_item_new_with_mnemonic(_("_Create file here..."));
gtk_widget_show(item);
gtk_container_add(GTK_CONTAINER(s_popup_menu.widget), item);
g_signal_connect(item, "activate", G_CALLBACK(popup_menu_on_new_file), NULL);
s_popup_menu.new_file = item;

item = gtk_menu_item_new_with_mnemonic(_("_Create directory here..."));
gtk_widget_show(item);
gtk_container_add(GTK_CONTAINER(s_popup_menu.widget), item);
g_signal_connect(item, "activate", G_CALLBACK(popup_menu_on_new_directory), NULL);
s_popup_menu.new_directory = item;
}

0 comments on commit 839819f

Please sign in to comment.