]> git.babelmonkeys.de Git - jubjub.git/commitdiff
Add helper to g_idle_add() blocks
authorFlorian Zeitz <florob@babelmonkeys.de>
Wed, 26 Dec 2012 01:10:10 +0000 (02:10 +0100)
committerFlorian Zeitz <florob@babelmonkeys.de>
Wed, 26 Dec 2012 01:10:10 +0000 (02:10 +0100)
src/gui/gtk/JubGtkChatUI.m
src/gui/gtk/JubGtkHelper.h [new file with mode: 0644]
src/gui/gtk/JubGtkHelper.m [new file with mode: 0644]
src/gui/gtk/Makefile

index b1fc9301c722d68349dd2f75c61b418b888ef2c1..4a45bcf447016ec5dd48d12f5510d37a56200fb0 100644 (file)
@@ -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 (file)
index 0000000..1168620
--- /dev/null
@@ -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 (file)
index 0000000..f282673
--- /dev/null
@@ -0,0 +1,18 @@
+#import <ObjFW/OFBlock.h>
+#include <gtk/gtk.h>
+
+#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]);
+}
index 275ebf9cb731fa6cbb9343ced311347fa3bc1d4d..c50589634a3f9c09780db1688e8999b2f4f1ab88 100644 (file)
@@ -1,7 +1,8 @@
 STATIC_LIB_NOINST = gtk.a
 SRCS = JubGtkUI.m      \
        JubGtkChatUI.m   \
-       JubGtkRosterUI.m
+       JubGtkRosterUI.m \
+       JubGtkHelper.m
 
 include ../../../buildsys.mk