0039: Dialogs V - Adding SaveAs, Filename in the Titlebar

Since we covered a lot of this stuff last time when we talked about adding a Save MenuItem, I’ll just cover the differences…

Dialog for a FileSaveAs Item

Results of this example:
Current example output
Current example output
Current example terminal output
Current example terminal output (click for enlarged view)

Of course, when you compare the FileSaveItem to the FileSaveAsItem, most of the differences consist of adding “as…” or “As” to function names, the class name, and the string used to name the MenuItem.

But there is one bigger change in the callback:

void doSomething(MenuItem mi)
		int response;
		FileChooserAction action = FileChooserAction.SAVE;

		filename = _filenameEntry.getText();

		FileChooserDialog dialog = new FileChooserDialog("Save a File", _parentWindow, action, null, null);
		response = dialog.run();

		if(response == ResponseType.OK)
			filename = dialog.getFilename();



	} // doSomething()

In the FileSaveItem, there’s a long if/else block which doesn’t show up here. Nothing in the else block is needed, so it’s just gone, but everything that was in the if block is now executed unconditionally. The only real differences are in the statements. As mentioned, saveFile() is now saveAsFile(), etc.

Except for this line:


And that brings us to our second topic…

Filename Reflection in the Titlebar

To examine this thoroughly, let’s start back at the TestRigWindow class. In the initialization section this:

string title = Save Dialog Example;

is now:

string title = “”;

And that’s because the initial window title is now set elsewhere, in the TextEntry class:

class TextEntry : Entry
	string defaultFilename = "Untitled";
	Window _parentWindow;

As before, the defaultFilename is set to “Untitled”, but since we have that reference to the window (_parentWindow), we use it in the TextEntry’s constructor:

this(Window parentWindow)
	_parentWindow = parentWindow;
} // this()

So if you decide to change the default file name, it’s set in both the TextEntry and the titlebar with this one string.

We do refer to it once more, though, in changeFilename():

	void changeFilename(Entry e)
		if(getText() == null)
			writeln("The filename is an empty string. Resetting to default: Untitled.");
			writeln("Filename has changed to: ", getText());


	} // changeFilename()

} // class TextEntry

And thus we keep the titlebar in sync with the TextEntry.

The Rest of It

The other changes between this and the previous example show up in the FileMenu class:

class FileMenu : Menu
	FileSaveItem fileSaveItem;
	FileSaveAsItem fileSaveAsItem;
	// arg: an array of items
	this(Window parentWindow, TextEntry filenameEntry)
		fileSaveItem = new FileSaveItem(parentWindow, filenameEntry);

		fileSaveAsItem = new FileSaveAsItem(parentWindow, filenameEntry);
	} // this()
} // class FileMenu

And those changes are simply declaring and instantiating the FileSaveAsItem and then appending it to FileMenu.


And there you have it. Over the last three blog posts, we’ve covered the basics of:

  • opening a single file,
  • opening multiple files,
  • saving a file, and
  • saving an already-named file under a new name.

Until next time, make the gods of coding smile upon you and your work.

Comments? Questions? Observations?

Did we miss a tidbit of information that would make this post even more informative? Let's talk about it in the comments.

You can also subscribe via RSS so you won't miss anything. Thank you very much for dropping by.

© Copyright 2023 Ron Tarrant