00001 #include <glade/glade.h> 00002 #include <gtk/gtk.h> 00003 #include "vapiString.h" 00004 #include "vapiAux.h" 00005 00006 #define NUMBER_OF_LINES_TO_RECORD 40 00007 static GtkWidget *txtMessages; 00008 static GtkWidget *txtErrors; 00009 static GMutex *MessageBufferMutex; 00010 typedef struct _vTempMessages 00011 { 00012 char *Message; 00013 int type; 00014 00015 } vTempMessages; 00016 00017 00018 gboolean vapiGtkWriteMessageIdle (vTempMessages * TempMessages); 00019 00020 void 00021 vapiGtkMessagesInit (GladeXML * xml) 00022 { 00023 txtMessages = glade_xml_get_widget (xml, "txtMessages"); 00024 txtErrors = glade_xml_get_widget (xml, "txtErrors"); 00025 if (!g_thread_supported ()) 00026 { 00027 g_thread_init (NULL); 00028 } 00029 MessageBufferMutex = g_mutex_new (); 00030 } 00031 00032 void 00033 vapiGtkWriteMessage (const char *Message, int type) 00034 { 00035 vTempMessages *TempMessages = malloc (sizeof (vTempMessages)); 00036 TempMessages->Message = v_printf ("%s", Message); 00037 TempMessages->type = type; 00038 g_idle_add ((GSourceFunc) & vapiGtkWriteMessageIdle, TempMessages); 00039 } 00040 00041 gboolean 00042 vapiGtkWriteMessageIdle (vTempMessages * TempMessages) 00043 { 00044 GtkTextBuffer *textBuffer; 00045 GtkWidget *textBox; 00046 GtkTextIter iter, iter2, iter3; 00047 int lines; 00048 g_mutex_lock (MessageBufferMutex); 00049 if (TempMessages->type == v_ERROR || TempMessages->type == v_ERROR_UI) 00050 { 00051 textBox = txtErrors; 00052 } 00053 else 00054 { 00055 textBox = txtMessages; 00056 } 00057 textBuffer = gtk_text_view_get_buffer ((GtkTextView *) textBox); 00058 lines = gtk_text_buffer_get_line_count (textBuffer); 00059 if (lines > NUMBER_OF_LINES_TO_RECORD) 00060 { 00061 gtk_text_buffer_get_start_iter (textBuffer, &iter); 00062 gtk_text_buffer_get_iter_at_line (textBuffer, &iter2, 00063 (lines - 00064 NUMBER_OF_LINES_TO_RECORD)); 00065 gtk_text_buffer_delete (textBuffer, &iter, &iter2); 00066 } 00067 00068 if (lines <= 1) 00069 { 00070 gtk_text_buffer_set_text (textBuffer, 00071 TempMessages->Message, -1); 00072 } 00073 else 00074 { 00075 gtk_text_buffer_get_end_iter (textBuffer, &iter3); 00076 gtk_text_buffer_insert (textBuffer, &iter3, 00077 TempMessages->Message, -1); 00078 } 00079 00080 free (TempMessages->Message); 00081 free (TempMessages); 00082 g_mutex_unlock (MessageBufferMutex); 00083 00084 return FALSE; 00085 }