Mercurial > ~mikael > mcabber > hg
comparison mcabber/screen.c @ 0:b3b2332715fb
Tailorization of /trunk
Import of the upstream sources from
Repository: file:///tmp/svn-mcabber
Module: /trunk
Revision: 15
author | tailor@frmp8452 |
---|---|
date | Thu, 30 Jun 2005 21:39:31 +0000 |
parents | |
children | da1fb510081e |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b3b2332715fb |
---|---|
1 #include <stdio.h> | |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 #include <ncurses.h> | |
5 #include <panel.h> | |
6 #include <time.h> | |
7 #include <ctype.h> | |
8 | |
9 #include "screen.h" | |
10 #include "utils.h" | |
11 #include "buddies.h" | |
12 #include "parsecfg.h" | |
13 #include "lang.h" | |
14 #include "server.h" | |
15 | |
16 #include "list.h" | |
17 | |
18 /* Definicion de tipos */ | |
19 #define window_entry(n) list_entry(n, window_entry_t, list) | |
20 | |
21 typedef struct _window_entry_t { | |
22 WINDOW *win; | |
23 PANEL *panel; | |
24 char *name; | |
25 int nlines; | |
26 char **texto; | |
27 struct list_head list; | |
28 } window_entry_t; | |
29 | |
30 LIST_HEAD(window_list); | |
31 | |
32 /* Variables globales a SCREEN.C */ | |
33 WINDOW *rosterWnd, *chatWnd; | |
34 PANEL *rosterPanel, *chatPanel; | |
35 int maxY, maxX; | |
36 window_entry_t *ventanaActual; | |
37 | |
38 | |
39 /* Funciones */ | |
40 | |
41 int scr_WindowHeight(WINDOW * win) | |
42 { | |
43 int x, y; | |
44 getmaxyx(win, y, x); | |
45 return x; | |
46 } | |
47 | |
48 void | |
49 scr_draw_box(WINDOW * win, int y, int x, int height, int width, int Color, | |
50 chtype box, chtype border) | |
51 { | |
52 int i, j; | |
53 | |
54 wattrset(win, COLOR_PAIR(Color)); | |
55 for (i = 0; i < height; i++) { | |
56 wmove(win, y + i, x); | |
57 for (j = 0; j < width; j++) | |
58 if (!i && !j) | |
59 waddch(win, border | ACS_ULCORNER); | |
60 else if (i == height - 1 && !j) | |
61 waddch(win, border | ACS_LLCORNER); | |
62 else if (!i && j == width - 1) | |
63 waddch(win, box | ACS_URCORNER); | |
64 else if (i == height - 1 && j == width - 1) | |
65 waddch(win, box | ACS_LRCORNER); | |
66 else if (!i) | |
67 waddch(win, border | ACS_HLINE); | |
68 else if (i == height - 1) | |
69 waddch(win, box | ACS_HLINE); | |
70 else if (!j) | |
71 waddch(win, border | ACS_VLINE); | |
72 else if (j == width - 1) | |
73 waddch(win, box | ACS_VLINE); | |
74 else | |
75 waddch(win, box | ' '); | |
76 } | |
77 } | |
78 | |
79 int FindColor(char *name) | |
80 { | |
81 if (!strcmp(name, "default")) | |
82 return -1; | |
83 if (!strcmp(name, "black")) | |
84 return COLOR_BLACK; | |
85 if (!strcmp(name, "red")) | |
86 return COLOR_RED; | |
87 if (!strcmp(name, "green")) | |
88 return COLOR_GREEN; | |
89 if (!strcmp(name, "yellow")) | |
90 return COLOR_YELLOW; | |
91 if (!strcmp(name, "blue")) | |
92 return COLOR_BLUE; | |
93 if (!strcmp(name, "magenta")) | |
94 return COLOR_MAGENTA; | |
95 if (!strcmp(name, "cyan")) | |
96 return COLOR_CYAN; | |
97 if (!strcmp(name, "white")) | |
98 return COLOR_WHITE; | |
99 | |
100 return -1; | |
101 } | |
102 | |
103 void ParseColors(void) | |
104 { | |
105 char *colors[11] = { | |
106 "", "", | |
107 "borderlines", | |
108 "jidonlineselected", | |
109 "jidonline", | |
110 "jidofflineselected", | |
111 "jidoffline", | |
112 "text", | |
113 NULL | |
114 }; | |
115 | |
116 char *tmp = malloc(1024); | |
117 char *color1; | |
118 char *background = cfg_read("color_background"); | |
119 char *backselected = cfg_read("color_backselected"); | |
120 int i = 0; | |
121 | |
122 while (colors[i]) { | |
123 sprintf(tmp, "color_%s", colors[i]); | |
124 color1 = cfg_read(tmp); | |
125 | |
126 switch (i + 1) { | |
127 case 1: | |
128 init_pair(1, COLOR_BLACK, COLOR_WHITE); | |
129 break; | |
130 case 2: | |
131 init_pair(2, COLOR_WHITE, COLOR_BLACK); | |
132 break; | |
133 case 3: | |
134 init_pair(3, FindColor(color1), FindColor(background)); | |
135 break; | |
136 case 4: | |
137 init_pair(4, FindColor(color1), FindColor(backselected)); | |
138 break; | |
139 case 5: | |
140 init_pair(5, FindColor(color1), FindColor(background)); | |
141 break; | |
142 case 6: | |
143 init_pair(6, FindColor(color1), FindColor(backselected)); | |
144 break; | |
145 case 7: | |
146 init_pair(7, FindColor(color1), FindColor(background)); | |
147 break; | |
148 case 8: | |
149 init_pair(8, FindColor(color1), FindColor(background)); | |
150 break; | |
151 } | |
152 i++; | |
153 } | |
154 } | |
155 | |
156 | |
157 window_entry_t *scr_CreatePanel(char *title, int x, int y, int lines, | |
158 int cols) | |
159 { | |
160 window_entry_t *tmp = calloc(1, sizeof(window_entry_t)); | |
161 | |
162 tmp->win = newwin(lines, cols, y, x); | |
163 tmp->panel = new_panel(tmp->win); | |
164 tmp->name = (char *) calloc(1, 1024); | |
165 strncpy(tmp->name, title, 1024); | |
166 scr_draw_box(tmp->win, 0, 0, lines, cols, COLOR_GENERAL, 0, 0); | |
167 mvwprintw(tmp->win, 0, (cols - (2 + strlen(title))) / 2, " %s ", title); | |
168 | |
169 list_add_tail(&tmp->list, &window_list); | |
170 update_panels(); | |
171 | |
172 return tmp; | |
173 } | |
174 | |
175 | |
176 void | |
177 scr_CreatePopup(char *title, char *texto, int corte, int type, | |
178 char *returnstring) | |
179 { | |
180 WINDOW *popupWin; | |
181 PANEL *popupPanel; | |
182 | |
183 int lineas = 0; | |
184 int cols = 0; | |
185 | |
186 char **submsgs; | |
187 int n = 0; | |
188 int i; | |
189 | |
190 char *instr = (char *) calloc(1, 1024); | |
191 | |
192 /* fprintf(stderr, "\r\n%d", lineas); */ | |
193 | |
194 submsgs = ut_SplitMessage(texto, &n, corte); | |
195 | |
196 switch (type) { | |
197 case 1: | |
198 case 0: | |
199 lineas = n + 4; | |
200 break; | |
201 } | |
202 | |
203 cols = corte + 3; | |
204 popupWin = newwin(lineas, cols, (maxY - lineas) / 2, (maxX - cols) / 2); | |
205 popupPanel = new_panel(popupWin); | |
206 | |
207 /*ATENCION!!! Colorear el popup ?? | |
208 / box (popupWin, 0, 0); */ | |
209 scr_draw_box(popupWin, 0, 0, lineas, cols, COLOR_POPUP, 0, 0); | |
210 mvwprintw(popupWin, 0, (cols - (2 + strlen(title))) / 2, " %s ", title); | |
211 | |
212 for (i = 0; i < n; i++) | |
213 mvwprintw(popupWin, i + 1, 2, "%s", submsgs[i]); | |
214 | |
215 | |
216 for (i = 0; i < n; i++) | |
217 free(submsgs[i]); | |
218 free(submsgs); | |
219 | |
220 switch (type) { | |
221 case 0: | |
222 mvwprintw(popupWin, n + 2, | |
223 (cols - (2 + strlen(i18n("Press any key")))) / 2, | |
224 i18n("Press any key")); | |
225 update_panels(); | |
226 doupdate(); | |
227 getch(); | |
228 break; | |
229 case 1: | |
230 { | |
231 char ch; | |
232 int scroll = 0; | |
233 int input_x = 0; | |
234 | |
235 wmove(popupWin, 3, 1); | |
236 wrefresh(popupWin); | |
237 keypad(popupWin, TRUE); | |
238 while ((ch = getch()) != '\n') { | |
239 switch (ch) { | |
240 case 0x09: | |
241 case KEY_UP: | |
242 case KEY_DOWN: | |
243 break; | |
244 case KEY_RIGHT: | |
245 case KEY_LEFT: | |
246 break; | |
247 case KEY_BACKSPACE: | |
248 case 127: | |
249 if (input_x || scroll) { | |
250 /* wattrset (popupWin, 0); */ | |
251 if (!input_x) { | |
252 scroll = scroll < cols - 3 ? 0 : scroll - (cols - 3); | |
253 wmove(popupWin, 3, 1); | |
254 for (i = 0; i < cols; i++) | |
255 waddch | |
256 (popupWin, | |
257 instr | |
258 [scroll | |
259 + input_x + i] ? instr[scroll + input_x + i] : ' '); | |
260 input_x = strlen(instr) - scroll; | |
261 } else | |
262 input_x--; | |
263 instr[scroll + input_x] = '\0'; | |
264 mvwaddch(popupWin, 3, input_x + 1, ' '); | |
265 wmove(popupWin, 3, input_x + 1); | |
266 wrefresh(popupWin); | |
267 } | |
268 default: | |
269 if ( /*ch<0x100 && */ isprint(ch) || ch == 'ñ' | |
270 || ch == 'Ñ') { | |
271 if (scroll + input_x < 1024) { | |
272 instr[scroll + input_x] = ch; | |
273 instr[scroll + input_x + 1] = '\0'; | |
274 if (input_x == cols - 3) { | |
275 scroll++; | |
276 wmove(popupWin, 3, 1); | |
277 for (i = 0; i < cols - 3; i++) | |
278 waddch(popupWin, instr[scroll + i]); | |
279 } else { | |
280 wmove(popupWin, 3, 1 + input_x++); | |
281 waddch(popupWin, ch); | |
282 } | |
283 wrefresh(popupWin); | |
284 } else { | |
285 flash(); | |
286 } | |
287 } | |
288 } | |
289 } | |
290 } | |
291 if (returnstring != NULL) | |
292 strcpy(returnstring, instr); | |
293 break; | |
294 } | |
295 | |
296 del_panel(popupPanel); | |
297 delwin(popupWin); | |
298 update_panels(); | |
299 doupdate(); | |
300 free(instr); | |
301 keypad(rosterWnd, TRUE); | |
302 } | |
303 | |
304 | |
305 | |
306 void scr_RoolWindow(void) | |
307 { | |
308 } | |
309 | |
310 window_entry_t *scr_SearchWindow(char *nombreVentana) | |
311 { | |
312 struct list_head *pos, *n; | |
313 window_entry_t *search_entry = NULL; | |
314 | |
315 list_for_each_safe(pos, n, &window_list) { | |
316 search_entry = window_entry(pos); | |
317 if (search_entry->name) { | |
318 if (!strcasecmp(search_entry->name, nombreVentana)) { | |
319 return search_entry; | |
320 } | |
321 } | |
322 } | |
323 return NULL; | |
324 } | |
325 | |
326 void scr_ShowWindow(char *nombreVentana) | |
327 { | |
328 int n, width, i; | |
329 window_entry_t *tmp = scr_SearchWindow(nombreVentana); | |
330 if (tmp != NULL) { | |
331 top_panel(tmp->panel); | |
332 width = scr_WindowHeight(tmp->win); | |
333 for (n = 0; n < tmp->nlines; n++) { | |
334 mvwprintw(tmp->win, n + 1, 1, ""); | |
335 for (i = 0; i < width - 2; i++) | |
336 waddch(tmp->win, ' '); | |
337 mvwprintw(tmp->win, n + 1, 1, "%s", tmp->texto[n]); | |
338 } | |
339 move(maxY - 2, maxX - 1); | |
340 update_panels(); | |
341 doupdate(); | |
342 } | |
343 } | |
344 | |
345 void scr_ShowBuddyWindow(void) | |
346 { | |
347 buddy_entry_t *tmp = bud_SelectedInfo(); | |
348 if (tmp->jid != NULL) | |
349 scr_ShowWindow(tmp->jid); | |
350 } | |
351 | |
352 | |
353 void scr_WriteInWindow(char *nombreVentana, char *texto, int TimeStamp) | |
354 { | |
355 time_t ahora; | |
356 int n; | |
357 int i; | |
358 int width; | |
359 window_entry_t *tmp; | |
360 | |
361 tmp = scr_SearchWindow(nombreVentana); | |
362 if (tmp == NULL) { | |
363 tmp = scr_CreatePanel(nombreVentana, 20, 0, maxY-1, maxX - 20); | |
364 tmp->texto = (char **) calloc(maxY * 3, sizeof(char *)); | |
365 for (n = 0; n < (maxY-1) * 3; n++) | |
366 tmp->texto[n] = (char *) calloc(1, 1024); | |
367 | |
368 if (TimeStamp) { | |
369 ahora = time(NULL); | |
370 strftime(tmp->texto[tmp->nlines], 1024, "[%H:%M] ", | |
371 localtime(&ahora)); | |
372 strcat(tmp->texto[tmp->nlines], texto); | |
373 } else { | |
374 sprintf(tmp->texto[tmp->nlines], " %s", texto); | |
375 } | |
376 tmp->nlines++; | |
377 } else { | |
378 if (tmp->nlines < maxY - 3) { | |
379 if (TimeStamp) { | |
380 ahora = time(NULL); | |
381 strftime(tmp->texto[tmp->nlines], 1024, | |
382 "[%H:%M] ", localtime(&ahora)); | |
383 strcat(tmp->texto[tmp->nlines], texto); | |
384 } else { | |
385 sprintf(tmp->texto[tmp->nlines], " %s", texto); | |
386 } | |
387 tmp->nlines++; | |
388 } else { | |
389 for (n = 0; n < tmp->nlines; n++) { | |
390 memset(tmp->texto[n], 0, 1024); | |
391 strncpy(tmp->texto[n], tmp->texto[n + 1], 1024); | |
392 } | |
393 if (TimeStamp) { | |
394 ahora = time(NULL); | |
395 strftime(tmp->texto[tmp->nlines - 1], 1024, | |
396 "[%H:%M] ", localtime(&ahora)); | |
397 strcat(tmp->texto[tmp->nlines - 1], texto); | |
398 } else { | |
399 sprintf(tmp->texto[tmp->nlines - 1], " %s", texto); | |
400 } | |
401 } | |
402 } | |
403 | |
404 top_panel(tmp->panel); | |
405 width = scr_WindowHeight(tmp->win); | |
406 for (n = 0; n < tmp->nlines; n++) { | |
407 mvwprintw(tmp->win, n + 1, 1, ""); | |
408 for (i = 0; i < width - 2; i++) | |
409 waddch(tmp->win, ' '); | |
410 mvwprintw(tmp->win, n + 1, 1, "%s", tmp->texto[n]); | |
411 } | |
412 | |
413 update_panels(); | |
414 doupdate(); | |
415 } | |
416 | |
417 void scr_InitCurses(void) | |
418 { | |
419 initscr(); | |
420 noecho(); | |
421 raw(); | |
422 start_color(); | |
423 use_default_colors(); | |
424 | |
425 ParseColors(); | |
426 | |
427 getmaxyx(stdscr, maxY, maxX); | |
428 | |
429 return; | |
430 } | |
431 | |
432 void scr_DrawMainWindow(void) | |
433 { | |
434 /* Dibujamos los paneles principales */ | |
435 rosterWnd = newwin(maxY-1, 20, 0, 0); | |
436 rosterPanel = new_panel(rosterWnd); | |
437 scr_draw_box(rosterWnd, 0, 0, maxY-1, 20, COLOR_GENERAL, 0, 0); | |
438 mvwprintw(rosterWnd, 0, (20 - strlen(i18n("Roster"))) / 2, | |
439 i18n("Roster")); | |
440 | |
441 chatWnd = newwin(maxY-1, maxX - 20, 0, 20); | |
442 chatPanel = new_panel(chatWnd); | |
443 scr_draw_box(chatWnd, 0, 0, maxY-1, maxX - 20, COLOR_GENERAL, 0, 0); | |
444 mvwprintw(chatWnd, 0, | |
445 ((maxX - 20) - strlen(i18n("Status Window"))) / 2, | |
446 i18n("Status Window")); | |
447 | |
448 bud_DrawRoster(rosterWnd); | |
449 | |
450 update_panels(); | |
451 doupdate(); | |
452 return; | |
453 } | |
454 | |
455 void scr_TerminateCurses(void) | |
456 { | |
457 clear(); | |
458 refresh(); | |
459 endwin(); | |
460 return; | |
461 } | |
462 | |
463 void scr_WriteIncomingMessage(char *jidfrom, char *text) | |
464 { | |
465 char **submsgs; | |
466 int n, i; | |
467 char *buffer = (char *) malloc(5 + strlen(text)); | |
468 | |
469 sprintf(buffer, "<<< %s", text); | |
470 | |
471 submsgs = | |
472 ut_SplitMessage(buffer, &n, maxX - scr_WindowHeight(rosterWnd) - 20); | |
473 | |
474 for (i = 0; i < n; i++) { | |
475 if (i == 0) | |
476 scr_WriteInWindow(jidfrom, submsgs[i], TRUE); | |
477 else | |
478 scr_WriteInWindow(jidfrom, submsgs[i], FALSE); | |
479 } | |
480 | |
481 for (i = 0; i < n; i++) | |
482 free(submsgs[i]); | |
483 | |
484 free(submsgs); | |
485 free(buffer); | |
486 | |
487 } | |
488 | |
489 void scr_WriteMessage(int sock) | |
490 { | |
491 char **submsgs; | |
492 int n, i; | |
493 char *buffer = (char *) calloc(1, 1024); | |
494 char *buffer2 = (char *) calloc(1, 1024); | |
495 buddy_entry_t *tmp = bud_SelectedInfo(); | |
496 | |
497 scr_ShowWindow(tmp->jid); | |
498 | |
499 ut_CenterMessage(i18n("write your message here"), 60, buffer2); | |
500 | |
501 scr_CreatePopup(tmp->jid, buffer2, 60, 1, buffer); | |
502 | |
503 if (strlen(buffer)) { | |
504 sprintf(buffer2, ">>> %s", buffer); | |
505 | |
506 submsgs = | |
507 ut_SplitMessage(buffer2, &n, | |
508 maxX - scr_WindowHeight(rosterWnd) - 20); | |
509 for (i = 0; i < n; i++) { | |
510 if (i == 0) | |
511 scr_WriteInWindow(tmp->jid, submsgs[i], TRUE); | |
512 else | |
513 scr_WriteInWindow(tmp->jid, submsgs[i], FALSE); | |
514 } | |
515 | |
516 for (i = 0; i < n; i++) | |
517 free(submsgs[i]); | |
518 free(submsgs); | |
519 | |
520 move(maxY - 2, maxX - 1); | |
521 refresh(); | |
522 sprintf(buffer2, "%s@%s/%s", cfg_read("username"), | |
523 cfg_read("server"), cfg_read("resource")); | |
524 srv_sendtext(sock, tmp->jid, buffer, buffer2); | |
525 } | |
526 free(buffer); | |
527 free(buffer2); | |
528 } | |
529 | |
530 int scr_Getch(void) | |
531 { | |
532 int ch; | |
533 keypad(rosterWnd, TRUE); | |
534 ch = wgetch(rosterWnd); | |
535 return ch; | |
536 } | |
537 | |
538 WINDOW *scr_GetRosterWindow(void) | |
539 { | |
540 return rosterWnd; | |
541 } | |
542 | |
543 WINDOW *scr_GetStatusWindow(void) | |
544 { | |
545 return chatWnd; | |
546 } |