Hi. I want to get selected item from GtkDropDown in task run from separate thread, but from what I saw during my debugging attempts it appears that my dropdown is not passed correctly.
How should I pass widgets to tasks so I can operate on them?
GtkWidget *receivedDataView;
GTask *task;
GCancellable *cancellable;
GObject *object;
GtkWidget *itemsDropdown;
int main (int argc, char *argv[]) {
//starting application
return startApp("exe.test", G_APPLICATION_DEFAULT_FLAGS, activateApp, argc, argv);
}
int startApp(char *id, GApplicationFlags flags, void (*activateFunc), int argc, char *argv[]) {
GtkApplication *app;
int stat;
app=gtk_application_new(id, flags);
g_signal_connect(app, "activate", G_CALLBACK(activateFunc), NULL);
stat=g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return stat;
}
static void activateApp (GApplication *app) {
...
//TASK
object=g_object_new(G_TYPE_OBJECT, NULL);
cancellable=g_cancellable_new();
task=g_task_new(object, cancellable, (void *)cb, itemsDropdown);
g_task_set_task_data(task, NULL, cbDestroy);
g_task_run_in_thread(task, (void *)cb);
g_object_unref(cancellable);
g_object_unref(object);
g_object_unref(task);
//TASK END
gtk_window_present(GTK_WINDOW(win));
}
void cb(GTask *task, gpointer source_object, gpointer itemsDropdown, GCancellable *cancellable) {
do {
static char buf[2]="1";
GtkWidget *dropdown=itemsDropdown;
char *selectedItemAsString=g_idle_add_once((void *)getStringFromDropdown, dropdown);
g_usleep(1);
static void *data[2];
data[0]=receivedDataView;
data[1]=selectedItemAsString; //if I put buf here the instruction below works fine
g_idle_add_once((void *)setText, data);
g_usleep(1);
}
while(!g_cancellable_is_cancelled(cancellable));
}
const char* getStringFromDropdown(GtkWidget *dropdown) {
GObject *itemFromDropdown=gtk_drop_down_get_selected_item(GTK_DROP_DOWN(dropdown));
const char *obtainedString=gtk_string_object_get_string(GTK_STRING_OBJECT(itemFromDropdown));
return obtainedString;
}
void setText(void* data[]) {
setTextInTextView(data[0], data[1]);
}
void setTextInTextView(GtkWidget *textView, char *text) {
GtkTextBuffer *infoBuffer=gtk_text_buffer_new(NULL);
gtk_text_buffer_set_text(GTK_TEXT_BUFFER(infoBuffer), text, strlen(text));
gtk_text_view_set_buffer(GTK_TEXT_VIEW(textView), infoBuffer);
}
void cbDestroy() {
//nothing to free(?)
}