www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - problem extracting data from GtkSourceView using Gtkd

reply Chris Bare <chris bareflix.com> writes:
I would have posted this in the Gtkd forum, but it has been down 
for a while.

I'm porting a GTK2/C program to Gtkd. I'm trying to read the data 
from a GtkSourceView, but when I try to get the bounds, it's 
always zero.

Here's the c version that works:
	GtkSourceBuffer *bf;
	GtkTextIter start, end;
	int len;

	bf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w.mainTxt));
	gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER(bf), &start, &end);
	data = gtk_text_buffer_get_text (GTK_TEXT_BUFFER(bf), &start, 
&end, FALSE);

Here's what I tried in D:
	SourceBuffer bf;
	auto start = new TextIter();
	auto end = new TextIter();

	bf = this.mainTxt.getBuffer ();
	bf.getBounds (start, end);
	string str = bf.getText (start, end, false);

getBounds is defined as: getBounds(out TextIter start, out 
TextIter end)

Is there something I'm doing wrong?
Any suggestions on what else to try?
Jan 14
next sibling parent Neia Neutuladh <neia ikeran.org> writes:
On Mon, 14 Jan 2019 22:52:48 +0000, Chris Bare wrote:
 	auto start = new TextIter();
 	auto end = new TextIter();
You shouldn't need to new these. `out` means that the function is going to overwrite the variables. Other than that, I'm not sure.
Jan 14
prev sibling parent reply Mike Wey <mike-wey example.com> writes:
On 14-01-2019 23:52, Chris Bare wrote:
 I would have posted this in the Gtkd forum, but it has been down for a 
 while.
 
 I'm porting a GTK2/C program to Gtkd. I'm trying to read the data from a 
 GtkSourceView, but when I try to get the bounds, it's always zero.
 
 Here's the c version that works:
      GtkSourceBuffer *bf;
      GtkTextIter start, end;
      int len;
 
      bf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w.mainTxt));
      gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER(bf), &start, &end);
      data = gtk_text_buffer_get_text (GTK_TEXT_BUFFER(bf), &start, &end, 
 FALSE);
 
 Here's what I tried in D:
      SourceBuffer bf;
      auto start = new TextIter();
      auto end = new TextIter();
 
      bf = this.mainTxt.getBuffer ();
      bf.getBounds (start, end);
      string str = bf.getText (start, end, false);
 
 getBounds is defined as: getBounds(out TextIter start, out TextIter end)
 
 Is there something I'm doing wrong?
 Any suggestions on what else to try?
Your code looks correct, and when copying it into the GtkD sourceview demo str does indeed hold the text displayed in the GtkSourceview. Do you have a more complete example we could look at? -- Mike Wey
Jan 15
parent reply Chris Bare <chris bareflix.com> writes:
Weird, the code does work in my program during startup, but when 
I call the same function from Application.onShutdown it gets the 
0 results.

Are the widgets destroyed before onShutdown?

Here's a stripped down version my Application subclass:


int
main (string[] args)
{
	auto application = new SpiralApp();
	application.run(args);
	return(0);
}

class SpiralApp: Application
{
	MainWin win;

	this ()
	{
		super("com.bareflix.spiral", ApplicationFlags.NON_UNIQUE);

		addOnActivate(&onActivate);
         addOnStartup(&onStartup);
		addOnShutdown(&onShutdown);
	}

	void onActivate(GioApplication application)
	{
		win = new MainWin();
		this.addWindow (win.spiral);
		win.spiral.showAll();

		notepadList = new NotepadList (dir);
		activePad = notepadList.front ();
		activePad.activate ();	// this sets the text in the 
GtkSourceView
		win.getMainTxt ();	// this retrieves the text correctly
	}

	void onShutdown(GioApplication application)
	{
		// this returns a blank string. I don't do anything in the UI, 
just
		// start it up and then exit with the window manager
		win.getMainTxt ();
		notepadList.write ();
		activePad.save ();
		info ("on shutdown here");
	}
}

Any ideas?
Jan 16
next sibling parent Mike Wey <mike-wey example.com> writes:
On 17-01-2019 00:31, Chris Bare wrote:
 
 Are the widgets destroyed before onShutdown?
The onShutdown callback is run after the GTK main loop terminates, so most objects would be finalized. -- Mike Wey
Jan 17
prev sibling parent Chris Bare <chris bareflix.com> writes:
I think I finally figured it out.
I think the GTKapplication shutdown signal is called after the 
window has been destroyed.
If I attach a handler to the window's destroy signal, then I am 
able to get the data from the sourceView.
Jan 17