Added New File action

This commit is contained in:
Yevgen Muntyan 2006-06-19 04:27:42 -05:00
parent d59436525b
commit 12b36b542a
2 changed files with 293 additions and 2 deletions

View File

@ -286,6 +286,146 @@ goto_current_doc_dir (MooFileSelector *filesel)
}
/****************************************************************************/
/* NewFile
*/
static GtkWidget *
create_new_file_dialog (GtkWidget *parent,
const char *start_text,
MooGladeXML **xml)
{
GtkWidget *dialog, *button;
GtkEntry *entry;
*xml = moo_glade_xml_new_empty ();
moo_glade_xml_map_class (*xml, "GtkEntry", MOO_TYPE_ENTRY);
moo_glade_xml_parse_memory (*xml, MOO_FILE_SELECTOR_GLADE_XML, -1,
"new_file_dialog");
dialog = moo_glade_xml_get_widget (*xml, "new_file_dialog");
g_return_val_if_fail (dialog != NULL, NULL);
moo_position_window (dialog, parent, FALSE, FALSE, 0, 0);
entry = moo_glade_xml_get_widget (*xml, "entry");
gtk_entry_set_text (entry, start_text);
moo_entry_clear_undo (MOO_ENTRY (entry));
gtk_widget_show_all (dialog);
gtk_widget_grab_focus (GTK_WIDGET (entry));
button = moo_glade_xml_get_widget (*xml, "ok_button");
moo_bind_bool_property (button, "sensitive", entry, "empty", TRUE);
return dialog;
}
static char *
new_file_dialog (GtkWidget *parent,
const char *dirname,
const char *start_name)
{
MooGladeXML *xml = NULL;
GtkWidget *dialog = NULL;
GtkEntry *entry = NULL;
char *fullname = NULL;
g_return_val_if_fail (dirname != NULL, NULL);
while (TRUE)
{
const char *text;
char *name;
char *err_text;
if (!dialog)
{
dialog = create_new_file_dialog (parent, start_name, &xml);
entry = moo_glade_xml_get_widget (xml, "entry");
}
if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_OK)
goto out;
text = gtk_entry_get_text (entry);
if (!text[0])
{
g_critical ("%s: ooops", G_STRLOC);
goto out;
}
/* XXX error checking, you know */
name = g_filename_from_utf8 (text, -1, NULL, NULL, NULL);
if (!name)
{
char *sec_text;
err_text = g_strdup_printf ("Can not create file '%s'", text);
sec_text = g_strdup_printf ("Could not convert '%s' to filename encoding.\n"
"Please consider simpler name, such as foo.blah "
"or blah.foo", text);
moo_error_dialog (dialog ? dialog : parent, err_text, sec_text);
g_free (err_text);
g_free (sec_text);
continue;
}
fullname = g_build_filename (dirname, name, NULL);
g_free (name);
if (!g_file_test (fullname, G_FILE_TEST_EXISTS))
goto out;
err_text = g_strdup_printf ("File '%s' already exists", text);
moo_error_dialog (dialog, err_text, NULL);
g_free (err_text);
g_free (fullname);
fullname = NULL;
}
out:
if (xml)
g_object_unref (xml);
if (dialog)
gtk_widget_destroy (dialog);
return fullname;
}
static void
file_selector_create_file (MooFileSelector *filesel)
{
char *path, *dir = NULL;
MooEdit *doc;
g_object_get (filesel, "current-directory", &dir, NULL);
if (!dir)
return;
path = new_file_dialog (GTK_WIDGET (filesel), dir, "Untitled");
if (!path)
return;
doc = moo_editor_new_file (moo_edit_window_get_editor (filesel->window),
filesel->window, GTK_WIDGET (filesel), path, NULL);
if (doc)
moo_edit_save (doc, NULL);
g_free (path);
}
/****************************************************************************/
/* Constructor
*/
static GObject *
moo_file_selector_constructor (GType type,
guint n_props,
@ -297,6 +437,7 @@ moo_file_selector_constructor (GType type,
MooFileSelector *filesel;
MooFileView *fileview;
GObject *object;
guint merge_id;
object = G_OBJECT_CLASS(_moo_file_selector_parent_class)->constructor (type, n_props, props);
filesel = MOO_FILE_SELECTOR (object);
@ -315,11 +456,25 @@ moo_file_selector_constructor (GType type,
"closure-object", filesel,
"closure-callback", goto_current_doc_dir,
NULL);
moo_action_group_add_action (_moo_file_view_get_actions (MOO_FILE_VIEW (fileview)),
"NewFile",
"label", "New File...",
"tooltip", "New File...",
"stock-id", GTK_STOCK_NEW,
"closure-object", filesel,
"closure-callback", file_selector_create_file,
NULL);
xml = _moo_file_view_get_ui_xml (MOO_FILE_VIEW (fileview));
moo_ui_xml_insert_markup (xml, moo_ui_xml_new_merge_id (xml),
merge_id = moo_ui_xml_new_merge_id (xml);
moo_ui_xml_insert_markup (xml, merge_id,
"MooFileView/Toolbar", -1,
"<item action=\"GoToCurrentDocDir\"/>");
moo_ui_xml_insert_markup_before (xml, merge_id,
"MooFileView/Menu",
"NewFolder",
"<item action=\"NewFile\"/>"
"<separator/>");
label = moo_pane_label_new (MOO_STOCK_FILE_SELECTOR,
NULL, NULL/*button*/, "File Selector",
@ -538,7 +693,8 @@ create_save_as_dialog (GtkWidget *parent,
*xml = moo_glade_xml_new_empty ();
moo_glade_xml_map_class (*xml, "GtkEntry", MOO_TYPE_ENTRY);
moo_glade_xml_parse_memory (*xml, MOO_FILE_SELECTOR_GLADE_XML, -1, NULL);
moo_glade_xml_parse_memory (*xml, MOO_FILE_SELECTOR_GLADE_XML, -1,
"save_untitled_dialog");
dialog = moo_glade_xml_get_widget (*xml, "save_untitled_dialog");
g_return_val_if_fail (dialog != NULL, NULL);

View File

@ -139,4 +139,139 @@
</child>
</widget>
<widget class="GtkDialog" id="new_file_dialog">
<property name="title" translatable="yes">Create File</property>
<property name="type">GTK_WINDOW_TOPLEVEL</property>
<property name="window_position">GTK_WIN_POS_MOUSE</property>
<property name="modal">False</property>
<property name="resizable">True</property>
<property name="destroy_with_parent">False</property>
<property name="decorated">True</property>
<property name="skip_taskbar_hint">False</property>
<property name="skip_pager_hint">False</property>
<property name="type_hint">GDK_WINDOW_TYPE_HINT_DIALOG</property>
<property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
<property name="focus_on_map">True</property>
<property name="urgency_hint">False</property>
<property name="has_separator">False</property>
<child internal-child="vbox">
<widget class="GtkVBox" id="vbox2">
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">0</property>
<child internal-child="action_area">
<widget class="GtkHButtonBox" id="hbuttonbox1">
<property name="visible">True</property>
<property name="layout_style">GTK_BUTTONBOX_END</property>
<child>
<widget class="GtkButton" id="button1">
<property name="visible">True</property>
<property name="can_default">True</property>
<property name="can_focus">True</property>
<property name="label">gtk-cancel</property>
<property name="use_stock">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<property name="response_id">-6</property>
</widget>
</child>
<child>
<widget class="GtkButton" id="ok_button">
<property name="visible">True</property>
<property name="can_default">True</property>
<property name="has_default">True</property>
<property name="can_focus">True</property>
<property name="label">gtk-ok</property>
<property name="use_stock">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<property name="response_id">-5</property>
</widget>
</child>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack_type">GTK_PACK_END</property>
</packing>
</child>
<child>
<widget class="GtkAlignment" id="alignment2">
<property name="visible">True</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xscale">1</property>
<property name="yscale">1</property>
<property name="top_padding">6</property>
<property name="bottom_padding">6</property>
<property name="left_padding">6</property>
<property name="right_padding">6</property>
<child>
<widget class="GtkVBox" id="vbox3">
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">6</property>
<child>
<widget class="GtkLabel" id="label">
<property name="label" translatable="yes">File name:</property>
<property name="use_underline">False</property>
<property name="use_markup">False</property>
<property name="justify">GTK_JUSTIFY_LEFT</property>
<property name="wrap">False</property>
<property name="selectable">False</property>
<property name="xalign">0</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
<property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
<property name="width_chars">-1</property>
<property name="single_line_mode">False</property>
<property name="angle">0</property>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
<child>
<widget class="GtkEntry" id="entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="editable">True</property>
<property name="visibility">True</property>
<property name="max_length">0</property>
<property name="text" translatable="yes">rgwrgwr</property>
<property name="has_frame">True</property>
<property name="invisible_char">*</property>
<property name="activates_default">True</property>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
</widget>
</child>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">True</property>
<property name="fill">True</property>
</packing>
</child>
</widget>
</child>
</widget>
</glade-interface>