GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Sep 02, 2014 6:47 am

All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: [SOLVED]Textview drag and drop
PostPosted: Fri Jan 04, 2008 4:38 pm 
Offline
Familiar Face

Joined: Tue Dec 11, 2007 11:03 pm
Posts: 11
Location: Burleson, TX
Hi!

I am having a problem with a textview drag and drop operation.
What's happening is that when I drag a list of files on my textview widget the filenames are getting put in the widget and I am trying to override this behavior.

Code:
void
on_textview1_drag_data_received(GtkWidget * widget,
            GdkDragContext * drag_context,
            gint x,
            gint y,
            GtkSelectionData * data,
            guint info, guint time, gpointer user_data)
{
   if (data == NULL)
      return;
      
   g_message("Got Drag-Data-Received");   
         
   build_html(widget, data->data);   
   
   gtk_drag_finish(drag_context, TRUE, FALSE, time);   
}
///////////////////////////////////////////////////
void build_html(GtkWidget *w, guchar *data)
{
   GtkTextView *textview1 = GTK_TEXT_VIEW(w);
   GtkTextBuffer *output_buffer = gtk_text_view_get_buffer(textview1);
   GtkTextIter iter_start;
   GtkTextIter iter_stop;
   gchar *input_ptr = NULL;
   gchar *temp_ptr = NULL;
   gchar *output_ptr = NULL;
   gint i;
   gchar **vec = NULL;
   GList *file_list = NULL;
   GString *html_string = NULL;
   struct tm *tm_ptr;
   time_t now;
         
   input_ptr = (gchar *)data;      
      
   vec = g_strsplit(input_ptr, "\n", -1);
   if (vec == NULL)
      return;
      
   for (i = 0; vec[i]; i++)
   {
      temp_ptr = vec[i];
      
      output_ptr = g_strrstr(temp_ptr, "/");
      
      if (output_ptr != NULL)
      {
         g_strstrip(output_ptr);
               
         file_list = g_list_append(file_list, output_ptr);
      }
   }
   
   now = time(NULL);
   tm_ptr = localtime(&now);
   
   html_string = g_string_new("<HTML>\n");
   g_string_append(html_string, "<CENTER>\n");
   g_string_append(html_string, "<TITLE>");
   g_string_append(html_string, "Recent Pictures (");
   g_string_append_printf(html_string, "%2.2d/%2.2d/%4.4d",
      tm_ptr->tm_mon+1, tm_ptr->tm_mday, (tm_ptr->tm_year + 1900));
   g_string_append(html_string, ")</TITLE>\n");
   g_string_append(html_string, "<HEADER><H2>Recent Pictures (");
   g_string_append_printf(html_string, "%2.2d/%2.2d/%4.4d",
      tm_ptr->tm_mon+1, tm_ptr->tm_mday, (tm_ptr->tm_year + 1900));
   g_string_append_printf(html_string, ")</H2></HEADER>\n");
   g_string_append_printf(html_string, "</CENTER>\n");
   g_string_append_printf(html_string, "<p><hr><br>\n");
   g_string_append_printf(html_string,
      "<BODY text=\"#FFFFFF\" bgcolor=\"#000000\" vlink=\"#FFFF00\" link=\"#FFFF00\" alink=\"#FF0000\">\n");
   g_string_append(html_string, "<CENTER>\n");
   
   g_list_foreach(file_list, build_html_file_output, html_string);   
   
   g_string_append(html_string, "</CENTER>\n");
   g_string_append(html_string, "</p>\n");
   g_string_append(html_string, "</BODY>\n");
   g_string_append(html_string, "</HTML>\n");
   
   //g_message(html_string->str);
   gtk_text_buffer_set_text(output_buffer, html_string->str, html_string->len);
   gtk_text_view_set_buffer(textview1, output_buffer);
   
   g_string_free(html_string, TRUE);
}



My output in the textview widget is:
<HTML>
<CENTER>
<TITLE>Recent Pictures (01/04/2008)</TITLE>
<HEADER><H2>Recent Pictures (01/04/2008)</H2></HEADER>
</CENTER>
<p><hr><br>
<BODY text="#FFFFFF" bgcolor="#000000" vlink="#FFFF00" link="#FFFF00" alink="#FF0000">
<CENTER>
<IMG SRC="./RecentPix/IMG_0123.JPG" alt="/IMG_0123.JPG">
<br><hr>
</CENTER>
</p>
</BODY>
</HTML>
file:///home/dave/src/htmlBuilder/src/IMG_0123.JPG

I am trying to get rid of the file:///home/dave/src/htmlBuilder/src/IMG_0123.JPG.c
which is the default behavior on the textview.

Any help would be appreciated :)


Last edited by rdo on Sat Jan 05, 2008 6:24 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 04, 2008 5:31 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
I would start with the GTK+ 2.0 Tutorial's Drag-and-drop section: http://www.gtk.org/tutorial/c1901.html

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 04, 2008 9:32 pm 
Offline
Familiar Face

Joined: Tue Dec 11, 2007 11:03 pm
Posts: 11
Location: Burleson, TX
Well, I still don't see the problem... I think there must be another event being fired...

If I change this:
Code:
GtkTextBuffer *output_buffer = gtk_text_view_get_buffer(textview1);


to this:
Code:
GtkTextBuffer *output_buffer = gtk_text_buffer_new(NULL);


the "file:///home/dave/src/htmlBuilder/src/IMG_0123.JPG" gets inserted at the top of the textview widget then my HTML gets rendered.

I might try making html_string a global var and write it on an expose event but I really don't want to make it global.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 04, 2008 10:01 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
What exactly are you trying to achieve? When a user drags and drops an image file onto your textview, what do you want to happen?

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 04, 2008 10:13 pm 
Offline
Familiar Face

Joined: Tue Dec 11, 2007 11:03 pm
Posts: 11
Location: Burleson, TX
I want to generate a simple HTML page using the files that were dropped on the widget; then I will save the HTML and put it on my web server.

The HTML is being generated but the path to the file is being appended/pre-pended in the textview widget.

Previously, I was just dropping the files into a textview widget and displaying the generated HTML in another textview widget... but I don't like the way that works... I want to drop the files onto the textwidget that will render the output.

Is pre-pended even a word? :)

Edit: Just FYI, I only dropped one file on the textview widget in the sample output I displayed... If I drop more than one, each file is displayed with the <img source...> html but then listed at the end of the text as file:///...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 05, 2008 12:09 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Wow, I've been tinkering around a bit and I honestly don't know. The textview has it's own handler for "drag-data-received" and I'm not sure how to squash it. I'm gonna post this to the gtk-list as it's peeked my curiosity...

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 05, 2008 3:50 pm 
Offline
Familiar Face

Joined: Tue Dec 11, 2007 11:03 pm
Posts: 11
Location: Burleson, TX
Hi, I think I found a solution...

If I set the drag_data_received() callback to happen AFTER the default handler then it appears to output the text correctly; but I need to clean up my code and do some testing.. I kinda kludged it up trying different things. :)

In glade-2 you can set the "AFTER" button on the callback or in code you can use the function g_signal_connect_after().

Anyway, let me do some testing and I will update this to solved if it works out.
Thanks for your help!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 05, 2008 4:06 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Yes, the AFTER will make your handler appear AFTER the default handler, however, the default handler will still in fact run and you'll have to replace the text it inserts. Not the most elegant. What you can do is add:

Code:
g_signal_stop_emission_by_name (widget, "drag-data-received");


To the end of your callback for the "drag-data-received" callback. That will prevent the textview's default handler from being called at all. I've tested to confirm. It works.

Here's the reply I got from Mathias on gtk-app-devel-list (I can't believe I didn't think of it yesterday...)

Quote:
GtkTextView just installs a default handler. Those are called after all
custom signal handlers installed with g_signal_connect() (and before any
signal handler installed with g_signal_connect_after()).

Therefore you can connect your own signal handler, which stops signal
emission:

static void
drag_data_received_cb (GtkWidget *widget)
{
g_signal_stop_emission_by_name (widget, "drag-data-received");
}



Note: When looking at apps like Gedit, they derive their own classes from TextView and simply don't call their parent's handler.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 05, 2008 6:21 pm 
Offline
Familiar Face

Joined: Tue Dec 11, 2007 11:03 pm
Posts: 11
Location: Burleson, TX
Awesome! Thanks for you help!

I am awaiting approval from www.sf.net and will put the code on there if anyone else is interested. The project name is htmlbuilder.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 2:04 am 
Offline
Familiar Face

Joined: Tue Dec 11, 2007 11:03 pm
Posts: 11
Location: Burleson, TX
Ok, if anyone is interested the code is on sourceforge.

http://htmlbuilder.sf.net will get you to it.

Thanks for your help Micah!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group