comparison src/logwindow.c @ 682:54980ddfe1ce

Memorize text to display in the log window when the window is not yet created. On log window display, the memorized text is then recalled and displayed.
author zas_
date Sun, 18 May 2008 11:39:58 +0000
parents ac0ede6a53cf
children fece9ff5c624
comparison
equal deleted inserted replaced
681:ac0ede6a53cf 682:54980ddfe1ce
15 15
16 #include "window.h" 16 #include "window.h"
17 17
18 #include <gdk/gdkkeysyms.h> 18 #include <gdk/gdkkeysyms.h>
19 19
20 #define TRIM_LINES 25
21
22 20
23 typedef struct _LogWindow LogWindow; 21 typedef struct _LogWindow LogWindow;
24 22
25 struct _LogWindow 23 struct _LogWindow
26 { 24 {
164 buffer = gtk_text_view_get_buffer(text); 162 buffer = gtk_text_view_get_buffer(text);
165 mark = gtk_text_buffer_get_mark(buffer, "end"); 163 mark = gtk_text_buffer_get_mark(buffer, "end");
166 gtk_text_view_scroll_mark_onscreen(text, mark); 164 gtk_text_view_scroll_mark_onscreen(text, mark);
167 165
168 gtk_window_present(GTK_WINDOW(logwin->window)); 166 gtk_window_present(GTK_WINDOW(logwin->window));
167
168 log_window_append("", LOG_NORMAL); // to flush memorized lines
169 } 169 }
170 170
171 171
172 void log_window_new(void) 172 void log_window_new(void)
173 { 173 {
181 } 181 }
182 182
183 log_window_show(logwindow); 183 log_window_show(logwindow);
184 } 184 }
185 185
186 typedef struct _LogMsg LogMsg;
187
188 struct _LogMsg {
189 gchar *text;
190 LogType type;
191 };
192
186 void log_window_append(const gchar *str, LogType type) 193 void log_window_append(const gchar *str, LogType type)
187 { 194 {
188 GtkTextView *text; 195 GtkTextView *text;
189 GtkTextBuffer *buffer; 196 GtkTextBuffer *buffer;
190 GtkTextIter iter; 197 GtkTextIter iter;
191 gint line_limit = 1000; //FIXME: option 198 gint line_limit = 1000; //FIXME: option
192 gchar *str_utf8; 199 gchar *str_utf8;
193 200 static GList *memory = NULL;
194 if (logwindow == NULL) return; 201
202 if (logwindow == NULL && *str)
203 {
204 LogMsg *msg = g_new(LogMsg, 1);
205
206 msg->text = g_strdup(str);
207 msg->type = type;
208
209 memory = g_list_prepend(memory, msg);
210
211 while (g_list_length(memory) >= line_limit)
212 {
213 GList *work = g_list_last(memory);
214 LogMsg *oldest_msg = work->data;
215
216 g_free(oldest_msg->text);
217 memory = g_list_delete_link(memory, work);
218 }
219
220 return;
221 }
195 222
196 text = GTK_TEXT_VIEW(logwindow->text); 223 text = GTK_TEXT_VIEW(logwindow->text);
197 buffer = gtk_text_view_get_buffer(text); 224 buffer = gtk_text_view_get_buffer(text);
198 225
199 if (line_limit > 0 && logwindow->lines >= line_limit) 226 if (line_limit > 0 && logwindow->lines >= line_limit)
200 { 227 {
201 GtkTextIter start, end; 228 GtkTextIter start, end;
202 229
203 gtk_text_buffer_get_start_iter(buffer, &start); 230 gtk_text_buffer_get_start_iter(buffer, &start);
204 end = start; 231 end = start;
205 gtk_text_iter_forward_lines(&end, TRIM_LINES); 232 gtk_text_iter_forward_lines(&end, logwindow->lines - line_limit);
206 gtk_text_buffer_delete(buffer, &start, &end); 233 gtk_text_buffer_delete(buffer, &start, &end);
207 logwindow->lines = gtk_text_buffer_get_line_count(buffer);
208 } 234 }
209 235
210 gtk_text_buffer_get_end_iter(buffer, &iter); 236 gtk_text_buffer_get_end_iter(buffer, &iter);
211 237
212 str_utf8 = utf8_validate_or_convert((gchar *)str); 238 {
213 gtk_text_buffer_insert_with_tags_by_name 239 GList *work = g_list_last(memory);
214 (buffer, &iter, str_utf8, -1, logdefs[type].tag, NULL); 240
241 while (work)
242 {
243 GList *prev;
244 LogMsg *oldest_msg = work->data;
245
246 str_utf8 = utf8_validate_or_convert((gchar *)oldest_msg->text);
247 gtk_text_buffer_insert_with_tags_by_name
248 (buffer, &iter, str_utf8, -1, logdefs[oldest_msg->type].tag, NULL);
249
250 prev = work->prev;
251 memory = g_list_delete_link(memory, work);
252 work = prev;
253 }
254 }
255
256 if (*str)
257 {
258 str_utf8 = utf8_validate_or_convert((gchar *)str);
259 gtk_text_buffer_insert_with_tags_by_name
260 (buffer, &iter, str_utf8, -1, logdefs[type].tag, NULL);
261 }
215 262
216 if (GTK_WIDGET_VISIBLE(text)) 263 if (GTK_WIDGET_VISIBLE(text))
217 { 264 {
218 GtkTextMark *mark; 265 GtkTextMark *mark;
219 266
220 mark = gtk_text_buffer_get_mark(buffer, "end"); 267 mark = gtk_text_buffer_get_mark(buffer, "end");
221 gtk_text_view_scroll_mark_onscreen(text, mark); 268 gtk_text_view_scroll_mark_onscreen(text, mark);
222 } 269 }
223 270
224 logwindow->lines++; 271 logwindow->lines = gtk_text_buffer_get_line_count(buffer);
225 } 272 }