spot_img
HomeEducation0115: GTK GIO Utility Flags - Opening Information Receive US

0115: GTK GIO Utility Flags – Opening Information Receive US

The subsequent ApplicationFlag, HANDLES_OPEN, offers us a mechanism for opening recordsdata from the command line. Such issues are comparatively simple anyway, however maybe we’ll discover a bonus or two through the use of what GIO has to supply. Let’s dig in and discover out, we could?

We’ll do that in two steps. Firstly, we’ll have a look at the fundamentals—grabbing the file names from the command line—and secondly, we’ll add simply sufficient code to open every file in its personal window.

Importing the GIO File Abstraction


Demonstration the place a number of file names are given on the command line.
Current example terminal output

Prime: no file names given on the command line. Backside: two file names given.

The GIO assemble that helps us deal with recordsdata is known as gio.FileIF. It’s not likely an interface, however a sort-of wrapper standing in for a C-language abstraction—GFile—which represents the important statistics of a file. For our functions, we don’t must know so much about this to make use of it, so we’ll skip the small print. For now, simply know we want this import assertion to make these things work:

MyApplication Modifications

Elevate the Flag

As we’ve carried out earlier than, we declare the suitable flag within the MyApplication class. And whereas we’re at it, let’s change the applying id as nicely so it matches our present instance:

ApplicationFlags flags = ApplicationFlags.HANDLES_OPEN;
string id = "com.gtkdcoding.app.app_05_open_flag";

Hook up the Callback

The initializer technique wants somewhat one thing, too:

Mainly, simply hook up the sign. Observe that we don’t want the HANDLES_COMMAND_LINE flag to make this work, despite the fact that that may look like the case (it did to me at first).

Messing with the activate() Technique

It is a fairly small change from our final demo. There, we handed in an array containing the scale of the window we have been about to open. This time, we forego that in favour of passing in no matter arguments the consumer sorts on the command line. For functions of demonstration, we hope these arguments can be legitimate file names so we don’t have to jot down strive/catch statements. However be at liberty so as to add these if you need.

Anyway, activate() now appears like this:

void activate(string filename)

	writeln("activate known as");
	AppWindow appWindow = new AppWindow(this, filename);

 // activate()

We’ve disbursed with the dimensions argument—an array of integers—and changed it with filename—a string—which is the title of the file we’ll be opening in a window occasion.

This technique is known as as soon as for every file title offered on the command line.

Modifications to the onActivate() Technique

Right here’s one other small change:

void onActivate(GioApplication app) // non-advanced syntax

	AppWindow appWindow = new AppWindow(this, null);
	writeln("triggered onActivate...");
	writeln("tApplication ID: ", getApplicationId());
	
 // onActivate()

The rationale now we have one other instantiation of AppWindow right here is to take care of the chance that the consumer offers no arguments. Observe that—relying on circumstances—both activate() or onActivate() is known as, however not each. Right here’s the low-down:

  • activate() is known as as soon as for every file title handed in, whether or not these file names are legitimate or not, and
  • onActivate() is known as if no arguments by any means are given.

Observe: If a number of non-valid file names are given, activate() continues to be known as a number of instances and a number of home windows are opened. Somewhat error checking is not going to go amiss right here, however I’ll depart that to your creativeness and ability.

A New Callback – onOpen()

It’s comparatively trivial, so right here’s all the technique:

void onOpen(FileIF[] recordsdata, string trace, GioApplication app)

	writeln("triggered onOpen...");
		
	foreach(file; recordsdata)
	
		string title = file.getPath();
		writeln("file title: ", title);
		activate(title);
	
		
 // onOpen()

This straightforward technique steps via an array of FileIF objects, grabs the complete path of every, then calls activate() for every one… as talked about above.

The second argument—string trace—permits for various modes when opening a file (edit, view, and so forth.) and it’s instructed that until now we have a particular want for any such factor, we must always simply depart or not it’s. So we are going to.

Let’s transfer on to the following step the place we really load recordsdata into these home windows…

Loading Textual content Information


Current example output

Two file names given on the command line.
Current example terminal output

Two file names given on the command line. (click on for enlarged view)

We’ve seen the GTK code to perform this earlier than—the fine details that slap a TextView right into a Window—in order that half, I’ll skip. If you need a refresher, take a fast have a look at weblog put up #0069.

The one factor we haven’t coated is the D code that opens and reads the file. Right here’s what that appears like:

if(filename)

	file = File(filename, "r");
			
	whereas (!file.eof())
	
		string line = file.readln();
		content material ~= line;
	
			
	textBuffer.setText(content material);
	file.shut();		

Fairly simple. We begin by ensuring the filename variable incorporates a string, then dip into D’s stdio library to open the file in learn mode.

The whereas loop reads the file one line at a time and concatenates it to the content material variable.

As soon as that’s carried out, content material is dumped into the TextBuffer and we shut the file.

Conclusion

So, now we’ve learn file names from the command line and opened them, every in their very own window. Subsequent time, we’ll have a look at command line switches.

‘Til then, let’s all do our greatest to remain sane. These attempting instances are a problem—like getting down to design a rubbish collector in your first day with a brand new language whereas concurrently attempting to work out the plot of Misplaced—however we are able to survive and flourish if all of us maintain our heads… and our distance.

Be secure.

Did we miss a tidbit of data that might make this put up much more informative? Let’s speak about it within the feedback.

You can too subscribe through RSS so you will not miss something. Thanks very a lot for dropping by.

© Copyright 2022 Ron Tarrant

#GTK #GIO #Utility #Flags #Opening #Information

RELATED ARTICLES
Continue to the category

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -spot_img

Most Popular

Recent Comments