1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| #include <stdlib.h>
#include <gtk/gtk.h>
#include "include.h"
#include <pthread.h>
#include <stdio.h>
static char received[250];
static GtkWidget *pLabel;
static reply message;
static char pseudo[20];
static GtkWidget *pWindow;
static gchar* sUtf8;
void redraw(GtkWidget * widget)
{
gtk_widget_queue_draw(GTK_WIDGET(widget));
}
void writing (char *pseudo_sender, char *msg_sender)
{
strcat(received,pseudo_sender);
strcat(received," : ");
strcat(received,msg_sender);
strcat(received,"\n");
sUtf8 = g_locale_to_utf8(received, -1, NULL, NULL, NULL);
// gtk_label_set_label(pLabel,sUtf8);
g_free(sUtf8);
}
void Sending (GtkWidget *pWidget, gpointer *data)
{
GtkWidget *pDialog;
GtkWidget *pTextView;
GtkTextBuffer* pTextBuffer;
GtkTextIter iStart;
GtkTextIter iEnd;
gchar* sBuffer;
pthread_t thread2;
pTextView = GTK_WIDGET(data);
pTextBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pTextView));
gtk_text_buffer_get_start_iter(pTextBuffer, &iStart);
gtk_text_buffer_get_end_iter(pTextBuffer, &iEnd);
sBuffer = gtk_text_buffer_get_text(pTextBuffer, &iStart, &iEnd, TRUE);
strcpy(message.pseudo,pseudo);
strcpy(message.msg,sBuffer);
if (pthread_create(&thread2, NULL, client, &message))
fprintf(stderr, "error creating a new thread (client) \n");
strcat(received,message.pseudo);
strcat(received," : ");
strcat(received,sBuffer);
strcat(received,"\n");
sUtf8 = g_locale_to_utf8(received, -1, NULL, NULL, NULL);
// gtk_label_set_label(pLabel,sUtf8);
g_free(sUtf8);
g_free(sBuffer);
pthread_detach(thread2);
}
int main(int argc, char* argv[])
{
// Partie chat normal
pthread_t thread1;
int i;
char txt[250];
if(argc!=2){
printf("Usage: %s <nickname>\n", argv[0]);
return(-1);
}
strcpy(pseudo,argv[1]);
if (pthread_create(&thread1, NULL, server, NULL/*(void *)(buf)*/)) {
fprintf(stderr, "error creating a new thread (server) \n");
return (-1);
}
// Partie GTK
GtkWidget* pBox;
GtkWidget* pBox1;
GtkWidget* pBox2;
GtkWidget* pTextView;
GtkWidget* pButton;
GtkWidget *scrollbar;
GtkWidget *scrollbar2;
GtkWidget* pWindow;
GtkWidget* hbuttonbox;
gtk_init(&argc, &argv);
pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(pWindow), 320, 200);
gtk_window_set_title(GTK_WINDOW(pWindow), "OBMSN");
g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(gtk_main_quit), NULL);
pBox = gtk_vbox_new(FALSE, 5);
pBox1 = gtk_vbox_new(FALSE, 5);
pBox2 = gtk_vbox_new(FALSE, 5);
gtk_container_add(GTK_CONTAINER(pWindow),pBox);
scrollbar = gtk_scrolled_window_new(NULL, NULL);
scrollbar2 = gtk_scrolled_window_new(NULL, NULL);
gtk_box_pack_start(GTK_BOX(pBox), pBox1, TRUE, TRUE, 5);
gtk_box_pack_start(GTK_BOX(pBox), pBox2, TRUE, TRUE, 5);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW(scrollbar),pBox1);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW(scrollbar2),pBox2);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar2), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
sUtf8 = g_locale_to_utf8(received, -1, NULL, NULL, NULL);
pLabel = gtk_label_new(sUtf8);
g_free(sUtf8);
gtk_label_set_text(GTK_LABEL(pLabel), received);
gtk_box_pack_start(GTK_BOX(pBox1),pLabel,FALSE,FALSE,5);
pTextView = gtk_text_view_new();
gtk_container_add(GTK_CONTAINER(pBox2),pTextView);
pButton=gtk_button_new_with_label("Send");
hbuttonbox = gtk_hbutton_box_new();
gtk_box_pack_start(GTK_BOX(hbuttonbox),pButton,FALSE,FALSE,0);
gtk_container_add(GTK_CONTAINER (pBox), hbuttonbox);
g_signal_connect(G_OBJECT(pButton), "clicked", G_CALLBACK(Sending), (gpointer) pTextView);
gtk_widget_show_all(pWindow);
gtk_main();
return EXIT_SUCCESS;
pthread_detach(thread1);
pthread_exit(NULL);
return 0;
} |
Partager