diff options
-rw-r--r-- | data.c | 7 | ||||
-rw-r--r-- | gtk.c | 133 | ||||
-rw-r--r-- | gui.h | 1 |
3 files changed, 53 insertions, 88 deletions
@@ -19,7 +19,8 @@ typedef struct _roster_item_t { const char* name; const char* jid; subscription_t sub; - void* widget; // GUI widget as generic void* + void* widget; // the roster item in the contact list view + void* page_widget; // the chat page corresponding to this roster item struct _roster_item_t* next; } roster_item_t; @@ -59,7 +60,7 @@ void add_roster_item(const char* jid, const char* subscription, const char* name } // notify GUI - void* widget = gui_add_roster_item(jid, subscription, name); - item->widget = widget; // adds pointer to GUI widget to roster_item_t + item->widget = gui_add_roster_item(jid, subscription, name); + item->page_widget = gui_add_chat(); } @@ -6,17 +6,6 @@ #include "limox.h" -struct chat { - GtkWidget* chat_layout_box; // the layout box of the chat page - GtkWidget* chat_content_box; // the content box of the chat page - GtkWidget* text_entry; // where new messages are typed - struct chat* next; // entry to support the linked list -}; - - -// linked list of chats -static struct chat* chats = NULL; - // the GTK application is available as global variable static GtkApplication* app; @@ -69,77 +58,22 @@ static void disconnect_cb(void) { limox_disconnect(); } -static void to_chat(GtkWidget* layout_box) { - gtk_stack_set_visible_child(GTK_STACK(stack), layout_box); -} - -static void to_roster(void) { - gtk_stack_set_visible_child(GTK_STACK(stack), roster_layout_box); -} - -void send_message(struct chat* chat) { - - // get recipient and message text - const char* recipient = "dummy@example.com"; // TODO reimplement this - const char* text = gtk_editable_get_text(GTK_EDITABLE(chat->text_entry)); - - // execute dummy XMPP send TODO - printf("Sending to %s:\n> %s\n", recipient, text); - - // add message content to the chat - GtkWidget* message = gtk_label_new(text); - gtk_box_append(GTK_BOX(chat->chat_content_box), message); - - // clear text input - GtkEntryBuffer* empty_buffer = gtk_entry_buffer_new("", 0); - gtk_entry_set_buffer(GTK_ENTRY(chat->text_entry), empty_buffer); -} - -void add_chat(char* jid) { - - // create chat struct and initialize - struct chat* chat = malloc(sizeof(struct chat)); - chat->chat_layout_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 20); - chat->chat_content_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5); - chat->text_entry = gtk_entry_new(); - chat->next = NULL; +void send_message() { - // add chat page to stack - gtk_stack_add_child(GTK_STACK(stack), chat->chat_layout_box); + //// get recipient and message text + //const char* recipient = "dummy@example.com"; // TODO reimplement this + //const char* text = gtk_editable_get_text(GTK_EDITABLE(chat->text_entry)); - // add a button to go back to roster - GtkWidget* back = gtk_button_new_with_label("back"); - gtk_box_append(GTK_BOX(chat->chat_layout_box), back); - g_signal_connect_swapped(back, "clicked", G_CALLBACK(to_roster), NULL); - - // add a scrolled window to chat page - GtkWidget* scrolled = gtk_scrolled_window_new(); - gtk_widget_set_vexpand(scrolled, TRUE); - gtk_box_append(GTK_BOX(chat->chat_layout_box), scrolled); - gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scrolled), - chat->chat_content_box); + //// execute dummy XMPP send TODO + //printf("Sending to %s:\n> %s\n", recipient, text); - // add text entry and send button to chat page - gtk_box_append(GTK_BOX(chat->chat_layout_box), chat->text_entry); - GtkWidget* send_button = gtk_button_new_with_label("send"); - gtk_box_append(GTK_BOX(chat->chat_layout_box), send_button); - g_signal_connect_swapped(send_button, "clicked", G_CALLBACK(send_message), - chat); - - // append chat to linked list of chats - struct chat* current = chats; - while (true) { - if (current == NULL) { - chats = chat; - break; - } else if (current->next == NULL) { - current->next = chat; - break; - } else { - current = current->next; - } - } + //// add message content to the chat + //GtkWidget* message = gtk_label_new(text); + //gtk_box_append(GTK_BOX(chat->chat_content_box), message); + //// clear text input + //GtkEntryBuffer* empty_buffer = gtk_entry_buffer_new("", 0); + //gtk_entry_set_buffer(GTK_ENTRY(chat->text_entry), empty_buffer); } void add_incoming_text_message(char* sender_jid, char* content) { @@ -206,13 +140,6 @@ static void build_static_widgets(void) { roster_content_box); gtk_box_append(GTK_BOX(roster_layout_box), roster_scrolled); - // TODO just for debugging - for (int i=0; i<20; i++) { - add_incoming_text_message("contact-2@example.com", "Just a test."); - // message = gtk_label_new("Test message"); - // gtk_box_append(GTK_BOX(chat->chat_content_box), message); - } - } static void activate(void) { @@ -251,6 +178,42 @@ void gui_run(void) { } +void* gui_add_chat(void) { + + // create chat page (GtkBox) and add it to the stack + GtkWidget* chat_layout_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 20); + gtk_stack_add_child(GTK_STACK(stack), chat_layout_box); + + // create and add back button + GtkWidget* back = gtk_button_new_with_label("back"); + gtk_box_append(GTK_BOX(chat_layout_box), back); + //g_signal_connect_swapped(back, "clicked", G_CALLBACK(to_roster), NULL); TODO + + // create and add scrolled window + GtkWidget* scrolled = gtk_scrolled_window_new(); + gtk_widget_set_vexpand(scrolled, TRUE); + gtk_box_append(GTK_BOX(chat_layout_box), scrolled); + + // create and add content box for dynamic message widgets + GtkWidget* chat_content_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5); + gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scrolled), + chat_content_box); + + // create and add text input field + GtkWidget* text_entry = gtk_entry_new(); + gtk_box_append(GTK_BOX(chat_layout_box), text_entry); + + // create and add send button + GtkWidget* send_button = gtk_button_new_with_label("send"); + gtk_box_append(GTK_BOX(chat_layout_box), send_button); + //g_signal_connect_swapped(send_button, "clicked", G_CALLBACK(send_message), + // chat); // TODO + + // return chat page as void pointer for future reference + return (void*)chat_layout_box; + +} + void gui_add_message(const char* sender_jid, const char* content) { printf("Received from %s:\n%s\n", sender_jid, content); @@ -6,5 +6,6 @@ void gui_connected(char* jid, char* password); void gui_disconnected(void); void gui_suspended(void); void gui_resumed(void); +void* gui_add_chat(void); void gui_add_message(const char* sender_jid, const char* content); void* gui_add_roster_item(const char* jid, const char* sub, const char* name); |