From f068316e0f35bac1e4ae1617053285968ce19660 Mon Sep 17 00:00:00 2001 From: Florian Zeitz Date: Wed, 26 Dec 2012 02:10:10 +0100 Subject: [PATCH] Add helper to g_idle_add() blocks --- src/gui/gtk/JubGtkChatUI.m | 64 +++++++++++++------------------------- src/gui/gtk/JubGtkHelper.h | 1 + src/gui/gtk/JubGtkHelper.m | 18 +++++++++++ src/gui/gtk/Makefile | 3 +- 4 files changed, 43 insertions(+), 43 deletions(-) create mode 100644 src/gui/gtk/JubGtkHelper.h create mode 100644 src/gui/gtk/JubGtkHelper.m diff --git a/src/gui/gtk/JubGtkChatUI.m b/src/gui/gtk/JubGtkChatUI.m index b1fc930..4a45bcf 100644 --- a/src/gui/gtk/JubGtkChatUI.m +++ b/src/gui/gtk/JubGtkChatUI.m @@ -1,38 +1,5 @@ #import "JubGtkChatUI.h" - -struct add_text_params { - OFString *name; - OFString *text; - GtkTextBuffer *buffer; - BOOL first; -}; - -static gboolean add_text(gpointer data) -{ - GtkTextIter endIter; - struct add_text_params *params = data; - - if (OF_LIKELY(!params->first)) - gtk_text_buffer_insert_at_cursor(params->buffer, "\n", 1); - - gtk_text_buffer_get_end_iter(params->buffer, &endIter); - gtk_text_buffer_insert_with_tags_by_name(params->buffer, &endIter, - [params->name UTF8String], [params->name UTF8StringLength], "bold", - NULL); - gtk_text_buffer_get_end_iter(params->buffer, &endIter); - gtk_text_buffer_insert_with_tags_by_name(params->buffer, &endIter, - ": ", 2, "bold", NULL); - - gtk_text_buffer_insert_at_cursor(params->buffer, - [params->text UTF8String], [params->text UTF8StringLength]); - - [params->name release]; - [params->text release]; - g_object_unref(params->buffer); - free(params); - - return FALSE; -} +#import "JubGtkHelper.h" struct call_send_block_params { jub_send_block_t block; @@ -129,7 +96,6 @@ static gboolean init(gpointer data) self = [super init]; @try { - closeBlock = [closeBlock_ copy]; sendBlock = [sendBlock_ copy]; bufferEmpty = YES; @@ -159,14 +125,28 @@ static gboolean init(gpointer data) } - (void)addMessage: (OFString*)text - sender: (OFString*)sender; + sender: (OFString*)sender { - struct add_text_params *params = malloc(sizeof(*params)); - params->name = [sender retain]; - params->text = [text retain]; - params->buffer = g_object_ref(chat_buffer); - params->first = bufferEmpty; - g_idle_add(add_text, params); + BOOL first = bufferEmpty; if (OF_UNLIKELY(bufferEmpty)) bufferEmpty = NO; + + g_object_ref(chat_buffer); + g_idle_add_block(^{ + GtkTextIter endIter; + if (OF_LIKELY(!first)) + gtk_text_buffer_insert_at_cursor(chat_buffer, "\n", 1); + + gtk_text_buffer_get_end_iter(chat_buffer, &endIter); + gtk_text_buffer_insert_with_tags_by_name(chat_buffer, &endIter, + [sender UTF8String], [sender UTF8StringLength], "bold", + NULL); + gtk_text_buffer_get_end_iter(chat_buffer, &endIter); + gtk_text_buffer_insert_with_tags_by_name(chat_buffer, &endIter, + ": ", 2, "bold", NULL); + + gtk_text_buffer_insert_at_cursor(chat_buffer, [text UTF8String], + [text UTF8StringLength]); + g_object_unref(chat_buffer); + }); } @end diff --git a/src/gui/gtk/JubGtkHelper.h b/src/gui/gtk/JubGtkHelper.h new file mode 100644 index 0000000..1168620 --- /dev/null +++ b/src/gui/gtk/JubGtkHelper.h @@ -0,0 +1 @@ +void g_idle_add_block(void (^block)(void)); diff --git a/src/gui/gtk/JubGtkHelper.m b/src/gui/gtk/JubGtkHelper.m new file mode 100644 index 0000000..f282673 --- /dev/null +++ b/src/gui/gtk/JubGtkHelper.m @@ -0,0 +1,18 @@ +#import +#include + +#import "JubGtkHelper.h" + +static gboolean call_block(gpointer data) +{ + void (^block)(void) = data; + block(); + [block release]; + + return FALSE; +} + +void g_idle_add_block(void (^block)(void)) +{ + g_idle_add(call_block, [block copy]); +} diff --git a/src/gui/gtk/Makefile b/src/gui/gtk/Makefile index 275ebf9..c505896 100644 --- a/src/gui/gtk/Makefile +++ b/src/gui/gtk/Makefile @@ -1,7 +1,8 @@ STATIC_LIB_NOINST = gtk.a SRCS = JubGtkUI.m \ JubGtkChatUI.m \ - JubGtkRosterUI.m + JubGtkRosterUI.m \ + JubGtkHelper.m include ../../../buildsys.mk -- 2.39.5