oia.c 23.5 KB
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
/*
 * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270  e X3270
 * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
 * aplicativos mainframe. Registro no INPI sob o nome G3270.
 *
 * Copyright (C) <2008> <Banco do Brasil S.A.>
 *
 * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
 * os termos da GPL v.2 - Licença Pública Geral  GNU,  conforme  publicado  pela
 * Free Software Foundation.
 *
 * Este programa é distribuído na expectativa de  ser  útil,  mas  SEM  QUALQUER
 * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou  de  ADEQUAÇÃO
 * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
 * obter mais detalhes.
 *
 * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
 * programa;  se  não, escreva para a Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA, 02111-1307, USA
 *
 * Este programa está nomeado como oia.c e possui - linhas de código.
 *
 * Contatos:
 *
 * perry.werneck@gmail.com	(Alexandre Perry de Souza Werneck)
 * erico.mendonca@gmail.com	(Erico Mascarenhas Mendonça)
 * licinio@bb.com.br		(Licínio Luis Branco)
 * kraucer@bb.com.br		(Kraucer Fernandes Mazuco)
 * macmiranda@bb.com.br		(Marco Aurélio Caldas Miranda)
 *
 */

/*
 * The status line is laid out thusly (M is maxCOLS):
 *
 *	0			"4" in a square
 *	1			"A" underlined
 *	2			solid box if connected, "?" in a box if not
 *	5...M-47	message area
 *	M-46...45	SSL Status
 *	M-			Meta indication ("M" or blank)
 *	M-42		Alt indication ("A" or blank)
 *	M-			Compose indication ("C" or blank)
 *	M-			Compose first character
 *	M-40		Caps indication ("A" or blank)
 *	M-39...38	Shift Status
 *	M-36		Typeahead indication ("T" or blank)
 *	M-			Alternate keymap indication ("K" or blank)
 *	M-			Reverse input mode indication ("R" or blank)
 *	M-34		Insert mode indication (Special symbol/"I" or blank)
 *	M-32		Script indication
 *	M-			Printer indication ("P" or blank)
 *	M-29...		LU Name
 *	M-16..15	commang timing spinner
 *	M-14		command timing (Clock symbol and m:ss, or blank)
 *	M-7...m		cursor position (rrr/ccc or blank)
 *
 */

#include <math.h>
#include "gui.h"
#include "fonts.h"
#include "oia.h"

#ifndef __APPLE__
	#define ENABLE_BM_PIXMAPS 1
#endif

/*---[ Prototipes ]---------------------------------------------------------------------------------------------*/

 static void oia_draw_cursor_position(cairo_t *cr, GdkGC *gc, GdkRectangle *r);
 static void oia_draw_lu_name(cairo_t *cr, GdkGC *gc, GdkRectangle *r);

 static void oia_draw_insert_state(cairo_t *cr, GdkGC *gc, GdkRectangle *r);
 static void oia_draw_typeahead_state(cairo_t *cr, GdkGC *gc, GdkRectangle *r);
 static void oia_draw_script_state(cairo_t *cr, GdkGC *gc, GdkRectangle *r);
 static void oia_undera_indicator(cairo_t *cr, GdkGC *gc, GdkRectangle *r);
 static void oia_connection_status(cairo_t *cr, GdkGC *gc, GdkRectangle *r);

 static void oia_draw_shift_state(cairo_t *cr, GdkGC *gc, GdkRectangle *r);
 static void oia_draw_alt_state(cairo_t *cr, GdkGC *gc, GdkRectangle *r);
 static void oia_draw_ssl_state(cairo_t *cr, GdkGC *gc, GdkRectangle *r);
 static void oia_draw_timer(cairo_t *cr, GdkGC *gc, GdkRectangle *r);

#if defined(HAVE_CAPS_STATE) || defined(DEBUG)
 static void oia_draw_caps_state(cairo_t *cr, GdkGC *gc, GdkRectangle *r);
#endif // HAVE_CAPS_STATE || DEBUG

 static void oia_four_in_a_square(cairo_t *cr, GdkGC *gc, GdkRectangle *r);

 static void oia_draw_spinner(cairo_t *cr, GdkGC *gc, GdkRectangle *r);

 static void oia_message_area(cairo_t *cr, GdkGC *gc, GdkRectangle *r);

// static void dunno(cairo_t *cr, GdkGC *gc, GdkRectangle *r);

 static void oia_clear_rect(cairo_t *cr, GdkRectangle *r);
 static void oia_show_text(cairo_t *cr, GdkRectangle *r, const gchar *text, enum TERMINAL_COLOR c);

/*---[ Statics ]------------------------------------------------------------------------------------------------*/

#ifdef ENABLE_BM_PIXMAPS
 static GdkPixmap * pixmap_oia[OIA_PIXMAP_COUNT] = { NULL, NULL};
#endif // ENABLE_BM_PIXMAPS

 #define OIAROW	(view.top+4+(terminal_font_info.spacing*view.rows))

 gboolean		  oia_flag[OIA_FLAG_USER];
 LIB3270_STATUS	  terminal_message_id = (LIB3270_STATUS) -1;

 SCRIPT_STATE	  oia_script_state = SCRIPT_STATE_NONE;

#if defined(HAVE_CAPS_STATE) || defined(DEBUG)
 gboolean		  oia_caps_state = FALSE;
#endif // HAVE_CAPS_STATE || DEBUG

 gboolean		  oia_script_blink = TRUE;

 gboolean		  oia_shift_state	= FALSE;
 gboolean		  oia_alt_state = FALSE;

 gint			  oia_timer = -1;
 gint			  oia_spinner_step = 0;

/*---[ Globals ]------------------------------------------------------------------------------------------------*/

 static const struct _oia_calls
 {
	void (*update)(cairo_t *cr, GdkGC *gc, GdkRectangle *r);
 } oia_call[OIA_ELEMENT_COUNT] =
 {
	{ oia_four_in_a_square		},	// "4" in a square
	{ oia_undera_indicator		},	// "A" underlined
	{ oia_connection_status		},	// solid box if connected, "?" in a box if not
	{ oia_message_area			},	// message area
	{ oia_draw_ssl_state		},	// SSL Status
	{ oia_draw_alt_state		},	// Alt indication ("A" or blank)
	{ oia_draw_shift_state		},	// Shift Status
	{ oia_draw_typeahead_state	},	// Typeahead indication ("T" or blank)
	{ oia_draw_insert_state		},	// Insert mode indication (Special symbol/"I" or blank)
	{ oia_draw_script_state		},	// Script indication ("S" or blank)
	{ oia_draw_lu_name				},	// LU Name
	{ oia_draw_timer			},	// command timing (Clock symbol and m:ss, or blank)
	{ oia_draw_spinner			},  // Spinner indicator
	{ oia_draw_cursor_position	},	// cursor position (rrr/ccc or blank)

#if defined(HAVE_CAPS_STATE) || defined(DEBUG)
	{ oia_draw_caps_state		},	// Caps indications ("A" or blank)
#endif // HAVE_CAPS_STATE || DEBUG

 };


/*---[ Implement ]----------------------------------------------------------------------------------------------*/

/*
 static void dunno(cairo_t *cr, GdkGC *gc, GdkRectangle *r)
 {
 }
*/

 void update_oia(void)
 {
 	if(valid_terminal_window())
	{
		cairo_t *cr	= get_terminal_cairo_context();
		GdkGC *gc = get_terminal_cached_gc();
		draw_oia(cr,gc);
		cairo_destroy(cr);
		gtk_widget_queue_draw_area(terminal,OIAROW,view.left,view.cols*fontWidth,terminal_font_info.spacing+2);
	}
 }

 void draw_oia(cairo_t *cr, GdkGC *gc)
 {
 	int	f;
 	int row   = OIAROW;
 	int width = (view.cols * terminal_font_info.width);

	cairo_set_3270_color(cr,TERMINAL_COLOR_OIA_BACKGROUND);
	cairo_rectangle(cr, view.left, row, width, terminal_font_info.height);
	cairo_fill(cr);

	width += view.left;
	for(f=0;f<OIA_ELEMENT_COUNT;f++)
	{
		GdkRectangle rect;

		memset(&rect,0,sizeof(rect));
		rect.y = row;
		rect.width = width;
		rect.height = terminal_font_info.height;
		oia_call[f].update(cr,gc,&rect);
	}

	// http://cairographics.org/FAQ/#sharp_lines
	cairo_set_3270_color(cr,TERMINAL_COLOR_OIA_SEPARATOR);
	cairo_rectangle(cr, view.left, row-2, (view.cols * fontWidth), 1);
	cairo_fill(cr);

 }

 void update_oia_element(OIA_ELEMENT el)
 {
 	if(valid_terminal_window())
 	{
		cairo_t *cr = get_terminal_cairo_context();
		GdkRectangle rect;

		memset(&rect,0,sizeof(rect));
		rect.y = OIAROW;
		rect.height = terminal_font_info.height;
		rect.width = view.left + (view.cols * fontWidth);

		oia_call[el].update(cr,get_terminal_cached_gc(),&rect);

		cairo_destroy(cr);

		if(rect.width > 0)
			gtk_widget_queue_draw_area(terminal,rect.x,rect.y,rect.width,rect.height);

 	}
 }

 static void oia_clear_rect(cairo_t *cr, GdkRectangle *r)
 {
	cairo_set_3270_color(cr,TERMINAL_COLOR_OIA_BACKGROUND);
	cairo_rectangle(cr, r->x, r->y, r->width,r->height);
	cairo_fill(cr);

#ifdef DEBUG
	cairo_set_source_rgb(cr, 0.0, 0.0, 0.4);
//	cairo_move_to(cr,r->x,r->y+r->height);
//	cairo_line_to(cr,r->x+r->width,r->y+r->height);
	cairo_rectangle(cr, r->x, r->y, r->width,r->height);
	cairo_stroke(cr);
#endif
 }

 static void oia_show_text(cairo_t *cr, GdkRectangle *r, const gchar *text, enum TERMINAL_COLOR c)
 {
	cairo_set_3270_color(cr,c);
	cairo_move_to(cr,r->x,r->y+fontAscent);
	cairo_show_text(cr,text);
 }

 static void oia_clear_icon(cairo_t *cr, GdkRectangle *r)
 {
	if(r->width > r->height)
	{
		r->x += (r->width - r->height)/2;
		r->width = r->height;
	}
	else if(r->height > r->width)
	{
		r->y += (r->height - r->width)/2;
		r->height = r->width;
	}

	oia_clear_rect(cr,r);

 }

 static gint draw_spinner(cairo_t *cr, GdkRectangle *r, gint step)
 {
	static const guint num_steps	= 10;

	gdouble dx = r->width/2;
	gdouble dy = r->height/2;
	gdouble radius = MIN (r->width / 2, r->height / 2);
	gdouble half = num_steps / 2;
	gint i;


	cairo_save(cr);
	cairo_set_operator(cr, CAIRO_OPERATOR_OVER);

	cairo_rectangle(cr, r->x, r->y, r->width, r->height);
	cairo_clip(cr);
	cairo_translate(cr, r->x, r->y);

	step %= num_steps;

	for (i = 0; i < num_steps; i++)
	{
		gint inset = 0.7 * radius;

		/* transparency is a function of time and intial value */
		gdouble t = (gdouble) ((i + num_steps - step) % num_steps) / num_steps;

		cairo_save(cr);

		cairo_set_source_rgba (cr,
							 color[TERMINAL_COLOR_OIA_SPINNER].red / 65535.,
							 color[TERMINAL_COLOR_OIA_SPINNER].green / 65535.,
							 color[TERMINAL_COLOR_OIA_SPINNER].blue / 65535.,
							 t);

		cairo_set_line_width (cr, 2.0);
		cairo_move_to (cr,
					 dx + (radius - inset) * cos (i * G_PI / half),
					 dy + (radius - inset) * sin (i * G_PI / half));
		cairo_line_to (cr,
					 dx + radius * cos (i * G_PI / half),
					 dy + radius * sin (i * G_PI / half));
		cairo_stroke (cr);

		cairo_restore (cr);
	}

	cairo_restore(cr);

 	return step;
 }

 static void draw_border(cairo_t *cr, GdkGC *gc, GdkRectangle *r, enum TERMINAL_COLOR clr)
 {
	cairo_set_3270_color(cr,clr);

	cairo_rectangle(cr, r->x, r->y, r->width,1);
	cairo_rectangle(cr, r->x, r->y+r->height, r->width,1);
	cairo_rectangle(cr, r->x, r->y, 1, r->height);
	cairo_rectangle(cr, r->x+r->width, r->y, 1, r->height+1);

	cairo_fill(cr);
 }

/*
 * Update calls.
 */

 static void oia_draw_alt_state(cairo_t *cr, GdkGC *gc, GdkRectangle *r)
 {
	// *	M-42	Alt Status

	r->x = (r->width - (42*terminal_font_info.width));
	r->width = terminal_font_info.width;

	oia_clear_rect(cr,r);

	if(oia_alt_state)
		oia_show_text(cr,r,"A",TERMINAL_COLOR_OIA_ALT_STATE);

 }

 static void oia_draw_shift_state(cairo_t *cr, GdkGC *gc, GdkRectangle *r)
 {
	// *	M-39...38	Shift Status

	r->x = (r->width - (39*terminal_font_info.width));
	r->width = terminal_font_info.width*2;

	oia_clear_icon(cr,r);

	r->x++;
	r->y++;
	r->width -= 2;
	r->height -= 2;

	if(r->width < 3 || r->height < 3)
		return;

	if(oia_shift_state)
	{
/*
		http://4umi.com/web/javascript/xbm.htm

		  00000000001
		  01234567890
		00     * ....
		01    * *....
		02   *   *...
		03  *     *..
		04 *       *.
		05****   ****
		06   *   *
		07   *   *
		08   *   *
		09   *   *
		10   *   *
		11   *   *
		12   *   *
		13   *   *
		14   *   *
		15   *****
*/

		// Scale image
		int b,x,y,w,h,l;

		if(r->height > r->width)
		{
			w = r->width;
			h = w*1.5;
		}
		else // width > height
		{
			h = r->height;
			w = h/1.5;
		}

		// Set image position
		x = r->x + ((r->width - w)/2);
		y = r->y + ((r->height - h)/2);
		l = (w/3);
		b = y+(w/1.5);

		cairo_set_3270_color(cr,TERMINAL_COLOR_OIA_SHIFT_STATE);

		cairo_move_to(cr,x+(w/2),y);
		cairo_line_to(cr,x+w,b);
		cairo_line_to(cr,(x+w)-l,b);
		cairo_line_to(cr,(x+w)-l,y+h);
		cairo_line_to(cr,x+l,y+h);
		cairo_line_to(cr,x+l,b);
		cairo_line_to(cr,x,b);
		cairo_close_path(cr);

		cairo_stroke(cr);

	}

 }

 static void oia_draw_typeahead_state(cairo_t *cr, GdkGC *gc, GdkRectangle *r)
 {
	r->x = (r->width - (36*terminal_font_info.width));
	r->width = terminal_font_info.width;

	oia_clear_rect(cr,r);

	if(oia_flag[OIA_FLAG_TYPEAHEAD])
		oia_show_text(cr,r,"T",TERMINAL_COLOR_OIA_TYPEAHEAD_STATE);
 }

#if defined(HAVE_CAPS_STATE) || defined(DEBUG)
 static void oia_draw_caps_state(cairo_t *cr, GdkGC *gc, GdkRectangle *r)
 {
	r->x = (r->width - (40*terminal_font_info.width));
	r->width = terminal_font_info.width;
	r->height = terminal_font_info.height;

	oia_clear_rect(cr,r);

#if GTK_CHECK_VERSION(2,16,0)
	// http://library.gnome.org/devel/gdk/stable/gdk-Keyboard-Handling.html#gdk-keymap-get-caps-lock-state
	oia_caps_state = gdk_keymap_get_caps_lock_state(gdk_keymap_get_default());
#endif

	if(oia_caps_state)
		oia_show_text(cr,r,"A",TERMINAL_COLOR_OIA_CAPS_STATE);

 }
#endif // HAVE_CAPS_STATE || DEBUG

#ifdef ENABLE_BM_PIXMAPS
 static GdkPixmap * oia_create_scaled_pixmap(GdkRectangle *r, GdkGC *gc, const unsigned char *data, gint width, gint height, enum TERMINAL_COLOR cl)
 {
 	GdkPixbuf *buf;
 	GdkPixbuf *pix;
 	GdkPixmap *ret;
	GdkPixmap *tmp = gdk_pixmap_create_from_data(	get_terminal_pixmap(),
													(const char *) data,width,height,
													gdk_drawable_get_depth(get_terminal_pixmap()),
													color+cl,
													color+TERMINAL_COLOR_OIA_BACKGROUND );

	buf = gdk_pixbuf_get_from_drawable(0, tmp, gdk_drawable_get_colormap(get_terminal_pixmap()),0, 0, 0, 0, width, height);

	gdk_pixmap_unref(tmp);

	pix = gdk_pixbuf_scale_simple(buf,r->width,r->height,GDK_INTERP_HYPER);

	ret = gdk_pixmap_new(get_terminal_pixmap(),r->width,r->height,gdk_drawable_get_depth(get_terminal_pixmap()));

	gdk_pixbuf_render_to_drawable(pix,GDK_DRAWABLE(ret),gc,0,0,0,0,-1,-1,GDK_RGB_DITHER_NORMAL,0,0);

    gdk_pixbuf_unref(buf);
    gdk_pixbuf_unref(pix);

    return ret;

 }
#endif //  ENABLE_BM_PIXMAPS

 static void oia_draw_ssl_state(cairo_t *cr, GdkGC *gc, GdkRectangle *r)
 {
/*
  00000000001111111
  01234567890123456
00        xxxxxxx
01       x       x
02      x  xxxxx  x
03      x x     x x
04      x x     x x
05      x x     x x
06      x x     x x
07      x x     x x
08      x x     xxx
09xxxxxxxxxxx
10xxxxxxxxxxx
11xxxxxxxxxxx
12xxxxxxxxxxx
13xxxxxxxxxxx
14xxxxxxxxxxx
15x         x
16xxxxxxxxxxx

  00000000001111111
  01234567890123456
00     xxxxxxx
01    x       x
02   x  xxxxx  x
03   x x     x x
04   x x     x x
05   x x     x x
06   x x     x x
07   x x     x x
08   x x     x x
09..xxxxxxxxxxxxx
10  xxxxxxxxxxxxx
11  xxxxxxxxxxxxx
12  xxxxxxxxxxxxx
13  xxxxxxxxxxxxx
14  xxxxxxxxxxxxx
15  x           x
16  xxxxxxxxxxxxx

*/

#ifdef ENABLE_BM_PIXMAPS

	// Pixmap version

	#include "locked.bm"
	#include "unlocked.bm"

	static const struct _imagedata
	{
		const unsigned char	*data;
		gint					width;
		gint					height;
	} imagedata[] =
	{
		{ locked_bits,		locked_width,	locked_height	},
		{ unlocked_bits,	unlocked_width,	unlocked_height	},

	};

	int idx = query_secure_connection(hSession) ? OIA_PIXMAP_LOCKED : OIA_PIXMAP_UNLOCKED;

	r->x = (r->width - (46*terminal_font_info.width))+1;
	r->y++;
	r->width = (terminal_font_info.width*2)-1;
	r->height--;

	oia_clear_icon(cr,r);

	if(!pixmap_oia[idx])
		pixmap_oia[idx] = oia_create_scaled_pixmap(r,gc,imagedata[idx].data,imagedata[idx].width,imagedata[idx].height,TERMINAL_COLOR_OIA_SSL_STATE);

	gdk_cairo_set_source_pixmap(cr, pixmap_oia[idx], r->x, r->y);
	gdk_cairo_rectangle(cr,r);
	cairo_fill(cr);

#else

	// Non Pixmap version

	r->x = (r->width - (46*terminal_font_info.width))+1;
	r->y++;
	r->width = (terminal_font_info.width*2)-1;
	r->height--;

	oia_clear_icon(cr,r);

	if(query_secure_connection(hSession))
	{
		cairo_set_3270_color(cr,TERMINAL_COLOR_OIA_SSL_STATE);
		cairo_arc(cr,r->x+(r->width/2),r->y+(r->height/2),r->width/(2.5),0,2*M_PI);
		cairo_fill(cr);
	}

#endif // ENABLE_BM_PIXMAPS

 }

 static void oia_draw_insert_state(cairo_t *cr, GdkGC *gc, GdkRectangle *r)
 {
	r->x = (r->width - (34*terminal_font_info.width));
	r->width = terminal_font_info.width;

	oia_clear_icon(cr,r);

	if(Toggled(INSERT))
	{
		double y = r->y+(r->height-2);

		cairo_set_3270_color(cr,TERMINAL_COLOR_OIA_INSERT_STATE);

		cairo_move_to(cr,r->x,y);
		cairo_rel_line_to(cr,r->width/2,-(r->height/1.7));
		cairo_line_to(cr,r->x+r->width,y);

		cairo_stroke(cr);
	}

/*
	oia_clear_rect(cr,r);

	if(Toggled(INSERT))
		oia_show_text(cr,r,"I",TERMINAL_COLOR_OIA_INDICATORS);
*/
 }

 static void oia_draw_script_state(cairo_t *cr, GdkGC *gc, GdkRectangle *r)
 {
	r->x = (r->width - (32*terminal_font_info.width));
	r->width = fontWidth;

	oia_clear_rect(cr,r);

	if(oia_script_state != SCRIPT_STATE_NONE && oia_script_blink)
		oia_show_text(cr,r,"S",TERMINAL_COLOR_OIA_SCRIPT_STATE);

 }

 static void oia_four_in_a_square(cairo_t *cr, GdkGC *gc, GdkRectangle *r)
 {
	//  0          "4" in a square
	r->x = view.left;
	r->width = terminal_font_info.width+2;

	cairo_set_3270_color(cr,TERMINAL_COLOR_OIA_BACKGROUND);
	cairo_rectangle(cr, r->x, r->y+1, r->width,r->height);
	cairo_fill(cr);

	draw_border(cr,gc,r,TERMINAL_COLOR_OIA_FOREGROUND);

	cairo_move_to(cr,r->x+2,r->y+fontAscent);
	cairo_show_text(cr,"4");
 }

 static void oia_undera_indicator(cairo_t *cr, GdkGC *gc, GdkRectangle *r)
 {
	//  1          "A" underlined
	r->x = (view.left+terminal_font_info.width+4);
	r->width = terminal_font_info.width;

	oia_clear_rect(cr,r);

	if(oia_flag[OIA_FLAG_UNDERA])
	{
		oia_show_text(cr,r,(IN_E) ? "B" : "A", TERMINAL_COLOR_OIA_UNDERA);

		cairo_move_to(cr,r->x,r->y+fontAscent+(fontDescent/2));
		cairo_rel_line_to(cr,terminal_font_info.width,0);
		cairo_stroke(cr);
	}

 }

 static void oia_connection_status(cairo_t *cr, GdkGC *gc, GdkRectangle *r)
 {
	// 2          solid box if connected, "?" in a box if not
	r->x = (view.left+(terminal_font_info.width*2)+6);
	r->width = terminal_font_info.width+2;

	cairo_set_3270_color(cr,TERMINAL_COLOR_OIA_BACKGROUND);
	cairo_rectangle(cr, r->x, r->y+1, r->width,r->height);
	cairo_fill(cr);

	cairo_set_3270_color(cr,TERMINAL_COLOR_OIA_FOREGROUND);

	if(IN_ANSI)
	{
		draw_border(cr,gc,r,TERMINAL_COLOR_OIA_FOREGROUND);
		cairo_move_to(cr,r->x+2,r->y+terminal_font_info.ascent);
		cairo_show_text(cr,"N");
	}
	else if(oia_flag[OIA_FLAG_BOXSOLID])
	{
		cairo_rectangle(cr, r->x, r->y, r->width,r->height+1);
		cairo_fill(cr);
	}
	else if(IN_SSCP)
	{
		draw_border(cr,gc,r,TERMINAL_COLOR_OIA_FOREGROUND);
		cairo_move_to(cr,r->x+2,r->y+terminal_font_info.ascent);
		cairo_show_text(cr,"S");
	}
	else
	{
		draw_border(cr,gc,r,TERMINAL_COLOR_OIA_FOREGROUND);
		cairo_move_to(cr,r->x+2,r->y+terminal_font_info.ascent);
		cairo_show_text(cr,"?");
	}

 }

 static void oia_draw_cursor_position(cairo_t *cr, GdkGC *gc, GdkRectangle *r)
 {
	// M-7..M		cursor position (rrr/ccc or blank)
	gint width = r->width;

	// For some reason the smallest size of "xos4 terminus bold" isn't drawing with the exact size;
	// that's the reason why I'm reserving a bigger space for this element
 	r->width = (terminal_font_info.width*9)+(terminal_font_info.width/2);
 	r->x = width - r->width;

	oia_clear_rect(cr,r);

	if(Toggled(CURSOR_POS))
	{
		gchar *text;
		cairo_text_extents_t s;

		text = g_strdup_printf("%03d/%03d",(cursor_position/screen->cols)+1,(cursor_position%screen->cols)+1);

		cairo_set_3270_color(cr,TERMINAL_COLOR_OIA_CURSOR);
		cairo_text_extents(cr,text,&s);

		cairo_move_to(cr,width-(s.width+2),r->y+fontAscent);
		cairo_show_text(cr,text);

		g_free(text);
	}

 }

 static void oia_draw_lu_name(cairo_t *cr, GdkGC *gc, GdkRectangle *r)
 {
 	const char *luname = get_connected_lu(hSession);

 	r->x = (r->width - (terminal_font_info.width*29));
 	r->width = terminal_font_info.width*11;

	oia_clear_rect(cr,r);

	if(luname)
		oia_show_text(cr, r,luname, TERMINAL_COLOR_OIA_LUNAME);
 }

 static void oia_draw_timer(cairo_t *cr, GdkGC *gc, GdkRectangle *r)
 {
 	r->x = (r->width - (terminal_font_info.width*15));
 	r->width = terminal_font_info.width*6;

	oia_clear_rect(cr,r);

	if(oia_timer >= 0)
	{
		cairo_text_extents_t s;
		double x;
		gchar *text = g_strdup_printf("%02d:%02d",oia_timer/60,oia_timer % 60);

		cairo_set_3270_color(cr,TERMINAL_COLOR_OIA_TIMER);

		cairo_text_extents(cr,text,&s);

		x = (r->width - s.width)/2;
		if(x > 0)
			x += r->x;
		else
			x = r->x;

		cairo_move_to(cr,x,r->y+fontAscent);
		cairo_show_text(cr,text);

		g_free(text);
	}

 }

 static void oia_draw_spinner(cairo_t *cr, GdkGC *gc, GdkRectangle *r)
 {
 	r->x = r->width - ((terminal_font_info.width*17)+(terminal_font_info.width/2));
	r->width = terminal_font_info.width*2;

	oia_clear_icon(cr,r);
	r->x++;
	r->y++;
	r->width--;
	r->height--;

	if(r->width < 2)
		return;

#ifdef DEBUG
	oia_spinner_step = draw_spinner(cr,r,oia_spinner_step);
#else
	if(oia_timer >= 0)
		oia_spinner_step = draw_spinner(cr,r,oia_spinner_step);
#endif

 }


 static void oia_message_area(cairo_t *cr, GdkGC *gc, GdkRectangle *r)
 {
	#ifdef DEBUG
		#define OIA_MESSAGE(x,c,y) { #x, c, y }
	#else
		#define OIA_MESSAGE(x,c,y) { c, y }
	#endif

	static const struct _message
	{
	#ifdef DEBUG
		const gchar	* dbg;
	#endif
		int				  color;
		const gchar	* string;
	} message[LIB3270_STATUS_USER] =
 	{
		OIA_MESSAGE(	LIB3270_STATUS_BLANK,
						TERMINAL_COLOR_OIA_STATUS_OK,
						"" ),

		OIA_MESSAGE(	LIB3270_STATUS_SYSWAIT,
						TERMINAL_COLOR_OIA_STATUS_OK,
						N_( "X System" ) ),

		OIA_MESSAGE(	LIB3270_STATUS_TWAIT,
						TERMINAL_COLOR_OIA_STATUS_OK,
						N_( "X Wait" ) ),

		OIA_MESSAGE(	LIB3270_STATUS_CONNECTED,
						TERMINAL_COLOR_OIA_STATUS_OK,
						NULL ),

		OIA_MESSAGE(	LIB3270_STATUS_DISCONNECTED,
						TERMINAL_COLOR_OIA_STATUS_INVALID,
						N_( "X Not Connected" ) ),

		OIA_MESSAGE(	LIB3270_STATUS_AWAITING_FIRST,
						TERMINAL_COLOR_OIA_STATUS_OK,
						N_( "X" ) ),

		OIA_MESSAGE(	LIB3270_STATUS_MINUS,
						TERMINAL_COLOR_OIA_STATUS_OK,
						N_( "X -f" ) ),

		OIA_MESSAGE(	LIB3270_STATUS_PROTECTED,
						TERMINAL_COLOR_OIA_STATUS_INVALID,
						N_( "X Protected" ) ),

		OIA_MESSAGE(	LIB3270_STATUS_NUMERIC,
						TERMINAL_COLOR_OIA_STATUS_INVALID,
						N_( "X Numeric" ) ),

		OIA_MESSAGE(	LIB3270_STATUS_OVERFLOW,
						TERMINAL_COLOR_OIA_STATUS_INVALID,
						N_( "X Overflow" ) ),

		OIA_MESSAGE(	LIB3270_STATUS_INHIBIT,
						TERMINAL_COLOR_OIA_STATUS_INVALID,
						N_( "X Inhibit" ) ),

		OIA_MESSAGE(	LIB3270_STATUS_KYBDLOCK,
						TERMINAL_COLOR_OIA_STATUS_INVALID,
						NULL ),

		OIA_MESSAGE(	LIB3270_STATUS_X,
						TERMINAL_COLOR_OIA_STATUS_INVALID,
						N_( "X" ) ),

		OIA_MESSAGE(	LIB3270_STATUS_RESOLVING,
						TERMINAL_COLOR_OIA_STATUS_WARNING,
						N_( "X Resolving" ) ),

		OIA_MESSAGE(	LIB3270_STATUS_CONNECTING,
						TERMINAL_COLOR_OIA_STATUS_WARNING,
						N_( "X Connecting" ) ),


	};

	static gint current_message_id = 0;

	const gchar *msg = NULL;

	if(message[terminal_message_id].string)
		current_message_id = terminal_message_id;

	msg = message[current_message_id].string;

	r->x = view.left + (terminal_font_info.width * 5);
	r->width = (r->width - (terminal_font_info.width * 47)) - r->x;

	if(r->width < 0)
		return;

	oia_clear_rect(cr,r);

#ifdef DEBUG
	if(!*msg)
		msg = message[terminal_message_id].dbg;
#endif

	if(*msg)
	{
		int x = r->x;
		msg = gettext(msg);

		cairo_set_3270_color(cr,message[terminal_message_id].color);

		if(*msg == 'X')
		{
			cairo_save(cr);

			cairo_move_to(cr,x+1,r->y+1);
			cairo_rel_line_to(cr,fontWidth,fontAscent);
			cairo_rel_move_to(cr,-fontWidth,0);
			cairo_rel_line_to(cr,fontWidth,-fontAscent);

			cairo_stroke(cr);
			x += fontWidth;
			msg++;

			cairo_restore(cr);
		}

		cairo_move_to(cr,x,r->y+fontAscent);
		cairo_show_text(cr,msg);

	}
 }

 static gboolean update_timer_spinner(gpointer dunno)
 {
	oia_spinner_step++;
	update_oia_element(OIA_ELEMENT_COMMAND_SPINNER);
	return (oia_timer >= 0);
 }

 void oia_set_timer(long seconds)
 {
 	if(seconds < 0)
 	{
 		oia_timer = -1;
 	}
 	else if(seconds == oia_timer)
 	{
 		return;
 	}
 	else
 	{
 		if(oia_timer < 0)
			g_timeout_add((guint) 150, (GSourceFunc) update_timer_spinner, 0);
		oia_timer = (seconds % 5940);
 	}
	update_oia_element(OIA_ELEMENT_COMMAND_SPINNER);
 	update_oia_element(OIA_ELEMENT_COMMAND_TIMER);
 }

 void oia_release_pixmaps(void)
 {
#ifdef ENABLE_BM_PIXMAPS
	 int f;

	for(f=0;f<OIA_PIXMAP_COUNT;f++)
	{
		if(pixmap_oia[f])
		{
			gdk_pixmap_unref(pixmap_oia[f]);
			pixmap_oia[f] = NULL;
		}
	}
#endif // ENABLE_BM_PIXMAPS
 }