Hi
I want a simple clipboard text-receiver.
If I copy something in any program, my program here should receive it and shows it in the textview.
But i get here an endless-loop. My solution is (not here in this example), to
compare the new text with the old text, and only hold the new text, if it is
different. But I am not happy with that.
I want, that the system calls my clipboard-function (clipboard_callback), when
somewhere text is copied and then only inform my program, if a new text is
copied !
In clipboard_callback I have to call again gtk_clipboard_wait_for_text or
gtk_clipboard_request_text. Otherwise the System calls my function only one
time and I had to switch my programm off and on again.
But my program should receive text as long as it is activated.
Here my example:
#include <stdbool.h>
#include <gtk/gtk.h>
/*
Structure:
GtkWindow
GtkFixed
GtkButton
GtkScrolledWindow
GtkTextView
What i want:
I want a simple clipboard text-receiver.
If I copy something in any program, my program here should receive it and shows it in the textview.
But i get here an endless-loop. My solution is (not here in this example), to compare the new text with
the old text, and only hold the new text, if it is different. But I am not happy with that.
I want, that the system calls my clipboard-function (clipboard_callback), when somewhere text is copied
and then only inform my program, is a new text is copied !
In clipboard_callback I have to call again gtk_clipboard_wait_for_text or gtk_clipboard_request_text.
Otherwise the System calls my function only one time and I had to switch my programm off and on again.
But my program should receive text as long as it is activated.
I tried gtk_clipboard_wait_for_text instead of gtk_clipboard_request_text : Same result
*/
GtkWidget *Window,*fixed,*button_start,
*TV, // Textview: It receives the Clipboard-Content
*SW, // ScrolledWindow for the Textview
*label_counting, // Shows, whether there is that unwanted endless-loop. It never should counting
*label_infos; // Descriptiontext for the counting-label
GdkDisplay *display;
GtkTextBuffer *TVB;
GtkClipboard * Clipboard;
bool Startbutton_clicked;
unsigned long count;
bool callback__button_start(void);
void clipboard_callback(GtkClipboard *gClipboardP,const gchar *P,gpointer data);
int main(void)
{
gtk_init(NULL,NULL);
Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(Window),800,400);
g_signal_connect(Window,"delete-event",(GCallback)gtk_main_quit,NULL);
g_signal_connect(Window,"destroy",(GCallback)gtk_main_quit,NULL);
fixed = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(Window),fixed);
label_infos = gtk_label_new("");
gtk_widget_set_margin_start(label_infos,255);
gtk_widget_set_margin_top(label_infos,10);
gtk_container_add(GTK_CONTAINER(fixed),label_infos);
label_counting = gtk_label_new("");
gtk_widget_set_margin_start(label_counting,255);
gtk_widget_set_margin_top(label_counting,30);
gtk_container_add(GTK_CONTAINER(fixed),label_counting);
button_start = gtk_button_new_with_label("ON");
gtk_container_add(GTK_CONTAINER(fixed),button_start);
gtk_widget_set_margin_start(button_start,10);
gtk_widget_set_margin_top(button_start,10);
g_signal_connect((GtkWidget *)button_start,"clicked",(GCallback)callback__button_start,NULL);
SW = gtk_scrolled_window_new(NULL,NULL);
gtk_container_add(GTK_CONTAINER(fixed),SW);
gtk_widget_set_size_request(GTK_WIDGET(SW), 200, 200);
gtk_widget_set_margin_start(SW,5);
gtk_widget_set_margin_top(SW,100);
TV = gtk_text_view_new();
gtk_container_add(GTK_CONTAINER(SW),TV);
TVB = gtk_text_view_get_buffer((GtkTextView*)TV);
gtk_text_buffer_set_text (TVB,"empty",-1);
display = gdk_display_get_default(); // For my experiments for gtk_clipboard_get_for_display
// and gtk_clipboard_get_default instead of gtk_clipboard_get
// But the problem still exists.
Clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);//GDK_SELECTION_TYPE_STRING GDK_SELECTION_CLIPBOARD
// I tried GDK_SELECTION_TYPE_STRING) and GDK_SELECTION_TYPE_STRING
//Clipboard = gtk_clipboard_get_for_display(display,
// GDK_SELECTION_TYPE_STRING);//GDK_SELECTION_TYPE_STRING
//Clipboard = gtk_clipboard_get_default(display);
Startbutton_clicked = false;
gtk_widget_show_all(Window);
gtk_main();
}
bool callback__button_start(void)
{
if(Startbutton_clicked == true)
{
Startbutton_clicked = false;
gtk_label_set_text ((GtkLabel*)label_infos,"");
gtk_label_set_text ((GtkLabel*)label_counting,"");
gtk_button_set_label((GtkButton*)button_start,"ON");
}
else
{
Startbutton_clicked = true;
count = 0;
gtk_button_set_label((GtkButton*)button_start,"Off");
gtk_label_set_text ((GtkLabel*)label_infos,"counting numbers here shows the unwanted loop:");
// gtk_clipboard_wait_for_text (Clipboard);
gtk_clipboard_request_text(Clipboard,(GtkClipboardTextReceivedFunc)clipboard_callback,NULL);
}
return true;
}
void clipboard_callback(GtkClipboard *gClipboardP,const gchar *P,gpointer data)
{
char countstr[10];
if(strlen(P) > 0)
{
if(Startbutton_clicked == true)
{
//printf("Test: %s\n",(char*)P);
count ++;
sprintf(countstr,"%ld",count);
gtk_label_set_text ((GtkLabel*)label_counting,countstr);
gtk_text_buffer_set_text (TVB,P,-1);
// Experiment : But it does not solve the problem
//Clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);//GDK_SELECTION_TYPE_STRING GDK_SELECTION_CLIPBOARD
// gtk_clipboard_clear(gClipboardP); // Experiment : But it does not solve the problem
//gtk_clipboard_wait_for_text(Clipboard);
gtk_clipboard_request_text(gClipboardP,(GtkClipboardTextReceivedFunc)clipboard_callback,NULL);
}
}
}