summaryrefslogtreecommitdiff
path: root/app/wlib/gtklib/gtkmisc.c
blob: acc123a513c37947123272c9a738aa28e7cf9e11 (plain)
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
/*
 * $Header: /home/dmarkle/xtrkcad-fork-cvs/xtrkcad/app/wlib/gtklib/gtkmisc.c,v 1.15 2009-10-03 04:49:01 dspagnol Exp $
 */

/*  XTrkCad - Model Railroad CAD
 *  Copyright (C) 2005 Dave Bullis
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <locale.h>

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>

#include "gtkint.h"
#include "i18n.h"

wWin_p gtkMainW;

long debugWindow = 0;

char wAppName[256];
char wConfigName[ 256 ];

#define FOUR		(4)
#ifndef GTK1
#define MENUH		(24)
#else
#define MENUH		(24)
#endif
#define LABEL_OFFSET	(3)
		
const char * wNames[] = {
		"MAIN",
		"POPUP",
		"BUTT",
		"CANCEL",
		"POPUP",
		"TEXT",
		"INTEGER",
		"FLOAT",
		"LIST",
		"DROPLIST",
		"COMBOLIST",
		"RADIO",
		"TOGGLE",
		"DRAW",
		"MENU"
		"MULTITEXT",
		"MESSAGE",
		"LINES",
		"MENUITEM",
		"BOX"
		};

static struct timeval startTime;

static wBool_t reverseIcon =
#if defined(linux)
				FALSE;
#else
				TRUE;
#endif

char gtkAccelChar;


/*
 *****************************************************************************
 *
 * Internal Utility functions
 *
 *****************************************************************************
 */

unsigned char gtkBitrotate(
		char v )
{
	unsigned char r = 0;
	int i;
	static unsigned char bits[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
	for (i=0;i<8;i++)
		if (v & bits[i])
			r |= bits[7-i];
	return r;
}

GdkPixmap* gtkMakeIcon(
		GtkWidget * widget,
		wIcon_p ip,
		GdkBitmap ** mask )
{
	GdkPixmap * pixmap;
	char ** pixmapData;
	char * oldline1;
	static char newline1[] = " \tc None s None";
	char line0[40];
	char line2[40];
	int row,col,wb;
	long rgb;
	const char * bits;
	GdkColor *transparent;
	
	transparent = &gtk_widget_get_style( gtkMainW->gtkwin )->bg[GTK_WIDGET_STATE( gtkMainW->gtkwin )];
	
	if ( ip->gtkIconType == gtkIcon_pixmap ) {
		pixmap = gdk_pixmap_create_from_xpm_d( gtkMainW->gtkwin->window, mask, transparent, (char**)ip->bits );
	}
	else {
		wb = (ip->w+7)/8;
		pixmapData = (char**)malloc( (3+ip->h) * sizeof *pixmapData );
		pixmapData[0] = line0;
		rgb = wDrawGetRGB(ip->color);
		sprintf( line0, " %d %d 2 1", ip->w, ip->h );
		sprintf( line2, "# c #%2.2lx%2.2lx%2.2lx", (rgb>>16)&0xFF, (rgb>>8)&0xFF, rgb&0xFF );
		pixmapData[1] = ". c None s None";
		pixmapData[2] = line2;
		bits = ip->bits;
		for (row = 0; row<ip->h; row++ ) {
			pixmapData[row+3] = (char*)malloc( (ip->w+1) * sizeof **pixmapData );
			for (col = 0; col<ip->w; col++ ) {
				if ( bits[ row*wb+(col>>3) ] & (1<<(col&07)) ) {
					pixmapData[row+3][col] = '#';
				} else {
					pixmapData[row+3][col] = '.';
				}
			}
			pixmapData[row+3][ip->w] = 0;
		}
		pixmap = gdk_pixmap_create_from_xpm_d( gtkMainW->gtkwin->window, mask, transparent, pixmapData );
		for (row = 0; row<ip->h; row++ ) {
			free( pixmapData[row+3] );
		}
	}
	return pixmap;
}


int gtkAddLabel( wControl_p b, const char * labelStr )
{
	GtkRequisition requisition;
	if (labelStr == NULL)
		return 0;
	b->label = gtk_label_new(gtkConvertInput(labelStr));
	gtk_widget_size_request( b->label, &requisition );
	gtk_container_add( GTK_CONTAINER(b->parent->widget), b->label );
#ifndef GTK1
	gtk_fixed_move( GTK_FIXED(b->parent->widget), b->label, b->realX-requisition.width-8, b->realY+LABEL_OFFSET );
#else
	gtk_widget_set_uposition( b->label, b->realX-requisition.width-8, b->realY+LABEL_OFFSET );
#endif
	gtk_widget_show( b->label );
	return requisition.width+8;
}


void * gtkAlloc(
		wWin_p parent,
		wType_e type,
		wPos_t origX,
		wPos_t origY,
		const char * labelStr,
		int size,
		void * data )
{
	wControl_p w = (wControl_p)malloc( size );
	char * cp;
	memset( w, 0, size );
	if (w == NULL)
		abort();
	w->type = type;
	w->parent = parent;
	w->origX = origX;
	w->origY = origY;
	gtkAccelChar = 0;
	if (labelStr) {
		cp = (char*)malloc(strlen(labelStr)+1);
		w->labelStr = cp;
		for ( ; *labelStr; labelStr++ )
			if ( *labelStr != '&' )
				*cp++ = *labelStr;
			else {
/*				*cp++ = '_';
				gtkAccelChar = labelStr[1]; */
			}	
		*cp = 0;
	}
	w->doneProc = NULL;
	w->data = data;
	return w;
}


void gtkComputePos(
		wControl_p b )
{
	wWin_p w = b->parent;

	if (b->origX >= 0) 
		b->realX = b->origX;
	else
		b->realX = w->lastX + (-b->origX) - 1;
	if (b->origY >= 0) 
		b->realY = b->origY + FOUR + ((w->option&F_MENUBAR)?MENUH:0);
	else
		b->realY = w->lastY + (-b->origY) - 1;
}


void gtkControlGetSize(
		wControl_p b )
{
	GtkRequisition requisition;
	gtk_widget_size_request( b->widget, &requisition );
	b->w = requisition.width;
	b->h = requisition.height;
}
	

void gtkAddButton(
		wControl_p b )
{
	wWin_p win = b->parent;
	wBool_t resize = FALSE;
	if (win->first == NULL) {
		win->first = b;
	} else {
		win->last->next = b;
	}
	win->last = b;
	b->next = NULL;
	b->parent = win;
	win->lastX = b->realX + b->w;
	win->lastY = b->realY + b->h;
	if (win->option&F_AUTOSIZE) {
		if (win->lastX > win->realX) {
			win->realX = win->lastX;
			if (win->w != (win->realX + win->origX)) {
				resize = TRUE;
				win->w = (win->realX + win->origX);
			}
		}
		if (win->lastY > win->realY) {
			win->realY = win->lastY;
			if (win->h != (win->realY + win->origY)) {
			resize = TRUE;
				win->h = (win->realY + win->origY);
			}
		}
		if (win->shown) {
			if ( resize ) {
#ifndef GTK1
				gtk_widget_set_size_request( win->gtkwin, win->w, win->h );
				gtk_widget_set_size_request( win->widget, win->w, win->h );
#else
				gtk_widget_set_usize( win->gtkwin, win->w, win->h );
				gtk_widget_set_usize( win->widget, win->w, win->h );
#endif
			}
		}
	}
}


void gtkSetReadonly( wControl_p b, wBool_t ro )
{
	if (ro)
		b->option |= BO_READONLY;
	else
		b->option &= ~BO_READONLY;
}


wControl_p gtkGetControlFromPos(
		wWin_p win,
		wPos_t x,
		wPos_t y )
{
	wControl_p b;
	wPos_t xx, yy;
	for (b=win->first; b != NULL; b = b->next) {
		if ( b->widget && GTK_WIDGET_VISIBLE(b->widget) ) {
			xx = b->realX;
			yy = b->realY;
			if ( xx <= x && x < xx+b->w &&
				 yy <= y && y < yy+b->h ) {
				return b;
			}
		}
	}
	return NULL;
}

/* \brief Convert label string from Windows mnemonic to GTK
 *
 * The first occurence of '&' in the passed string is changed to '_'
 *
 * \param label the string to convert
 * \return pointer to modified string, has to be free'd after usage
 *
 */
static 
char * gtkChgMnemonic( char *label )
{
	char *ptr;
	char *cp;
	
	cp = strdup( label );
	
	ptr = strchr( cp, '&' );
	if( ptr )
		*ptr = '_';
		
	return( cp );	
}


/*
 *****************************************************************************
 *
 * Exported Utility Functions 
 *
 *****************************************************************************
 */

EXPORT void wBeep(
		void )
/*
Beep!
*/
{
	gdk_display_beep(gdk_display_get_default());
}

typedef struct {
		GtkWidget * win;
		GtkWidget * label;
		GtkWidget * butt[3];
		} notice_win;
static notice_win noticeW;
static long noticeValue;

static void doNotice(
		GtkWidget * widget,
		long value )
{
	noticeValue = value;
	gtk_widget_destroy( noticeW.win );
	gtkDoModal( NULL, FALSE );
}

/**
 * Show a notification window with a yes/no reply and an icon.
 *
 * \param type IN type of message: Information, Warning, Error
 * \param msg  IN message to display
 * \param yes  IN text for accept button
 * \param no   IN text for cancel button
 * \return    True when accept was selected, false otherwise
 */

int wNoticeEx( int type, 
       		const char * msg,
       		const char * yes,
       		const char * no )
{
	
	int res;
	unsigned flag;
	char *headline;
	GtkWidget *dialog;
	GtkWindow *parent = GTK_WINDOW_TOPLEVEL;

	switch( type ) {
		case NT_INFORMATION:
			flag = GTK_MESSAGE_INFO;
			headline = _("Information");
			break;
		case NT_WARNING:
			flag = GTK_MESSAGE_WARNING;
			headline = _("Warning");
			break;
		case NT_ERROR:
			flag = GTK_MESSAGE_ERROR;
			headline = _("Error");
			break;
	}
	
	if( gtkMainW )
		parent = GTK_WINDOW( gtkMainW->gtkwin);
	
	dialog = gtk_message_dialog_new( parent,  
					 GTK_DIALOG_DESTROY_WITH_PARENT,
					 flag,
      					 ((no==NULL)?GTK_BUTTONS_OK:GTK_BUTTONS_YES_NO),
					 "%s", msg );
	gtk_window_set_title( GTK_WINDOW(dialog), headline );
	  
	res = gtk_dialog_run( GTK_DIALOG(dialog));
	gtk_widget_destroy( dialog );
      
	return res == GTK_RESPONSE_OK  || res == GTK_RESPONSE_YES; 
}


EXPORT int wNotice(
		const char * msg,		/* Message */
		const char * yes,		/* First button label */
		const char * no )		/* Second label (or 'NULL') */
/*
Popup up a notice box with one or two buttons.
When this notice box is displayed the application is paused and
will not response to other actions.

Pushing the first button returns 'TRUE'.
Pushing the second button (if present) returns 'FALSE'.
*/
{
	return wNotice3( msg, yes, no, NULL );
}

/** \brief Popup a notice box with three buttons.
 *
 * Popup up a notice box with three buttons.
 * When this notice box is displayed the application is paused and
 * will not response to other actions.
 *
 * Pushing the first button returns 1
 * Pushing the second button returns 0
 * Pushing the third button returns -1
 *
 * \param msg Text to display in message box
 * \param yes First button label
 * \param no  Second label (or 'NULL')
 * \param cancel Third button label (or 'NULL') 
 *
 * \returns 1, 0 or -1
 */
 
EXPORT int wNotice3(
		const char * msg,		/* Message */
		const char * affirmative,		/* First button label */
		const char * cancel,		/* Second label (or 'NULL') */
		const char * alternate )
{
	notice_win *nw;
	GtkWidget * vbox;
	GtkWidget * hbox;
	GtkWidget * hbox1;
	GtkWidget * image;
	nw = &noticeW;
	
	char *aff = NULL;
	char *can = NULL;
	char *alt = NULL;
	
#ifndef GTK1
		nw->win = gtk_window_new( GTK_WINDOW_TOPLEVEL );
		/*gtk_window_set_decorated( GTK_WINDOW(nw->win), FALSE );*/
#else
		nw->win = gtk_window_new( GTK_WINDOW_DIALOG );
#endif
	gtk_window_position( GTK_WINDOW(nw->win), GTK_WIN_POS_CENTER );
	gtk_container_set_border_width (GTK_CONTAINER (nw->win), 0);
	gtk_window_set_resizable (GTK_WINDOW (nw->win), FALSE);
	gtk_window_set_modal (GTK_WINDOW (nw->win), TRUE);
	gtk_window_set_type_hint (GTK_WINDOW (nw->win), GDK_WINDOW_TYPE_HINT_DIALOG);
		
	vbox = gtk_vbox_new( FALSE, 12 );
	gtk_widget_show( vbox );
	gtk_container_add( GTK_CONTAINER(nw->win), vbox );
	gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
		
	hbox = gtk_hbox_new( FALSE, 12 );
	gtk_box_pack_start( GTK_BOX(vbox), hbox, TRUE, TRUE, 0 );
	gtk_widget_show(hbox);

 	image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_DIALOG);
  gtk_widget_show (image);
  gtk_box_pack_start (GTK_BOX (hbox), image, TRUE, TRUE, 0);
  gtk_misc_set_alignment (GTK_MISC (image), 0, 0);

	/* create the text label, allow GTK to wrap and allow for markup (for future enhancements) */
	nw->label = gtk_label_new(msg);
	gtk_widget_show( nw->label );
	gtk_box_pack_end (GTK_BOX (hbox), nw->label, TRUE, TRUE, 0);
	gtk_label_set_use_markup (GTK_LABEL (nw->label), FALSE);
	gtk_label_set_line_wrap (GTK_LABEL (nw->label), TRUE);
	gtk_misc_set_alignment (GTK_MISC (nw->label), 0, 0);

	/* this hbox will include the button bar */
	hbox1 = gtk_hbox_new (TRUE, 0);
	gtk_widget_show (hbox1);
  gtk_box_pack_start (GTK_BOX (vbox), hbox1, FALSE, TRUE, 0);

	/* add the respective buttons */
	aff = gtkChgMnemonic( (char *) affirmative);
	nw->butt[ 0 ] = gtk_button_new_with_mnemonic (aff);
	gtk_widget_show (nw->butt[ 0 ]);
	gtk_box_pack_end (GTK_BOX (hbox1), nw->butt[ 0 ], TRUE, TRUE, 0);
 	gtk_container_set_border_width (GTK_CONTAINER (nw->butt[ 0 ]), 3);
	gtk_signal_connect( GTK_OBJECT(nw->butt[0]), "clicked", GTK_SIGNAL_FUNC(doNotice), (void*)1 );
  GTK_WIDGET_SET_FLAGS (nw->butt[ 0 ], GTK_CAN_DEFAULT);

	if( cancel ) {
		can = gtkChgMnemonic( (char *) cancel);
		nw->butt[ 1 ] = gtk_button_new_with_mnemonic (can);
 		gtk_widget_show (nw->butt[ 1 ]);
  	gtk_box_pack_end (GTK_BOX (hbox1), nw->butt[ 1 ], TRUE, TRUE, 0);
	  gtk_container_set_border_width (GTK_CONTAINER (nw->butt[ 1 ]), 3);
		gtk_signal_connect( GTK_OBJECT(nw->butt[1]), "clicked", GTK_SIGNAL_FUNC(doNotice), (void*)0 );
	  GTK_WIDGET_SET_FLAGS (nw->butt[ 1 ], GTK_CAN_DEFAULT);

		if( alternate ) {
			alt = gtkChgMnemonic( (char *) alternate);
			nw->butt[ 2 ] = gtk_button_new_with_mnemonic (alt);
			gtk_widget_show (nw->butt[ 2 ]);
			gtk_box_pack_start (GTK_BOX (hbox1), nw->butt[ 2 ], TRUE, TRUE, 0);
			gtk_container_set_border_width (GTK_CONTAINER (nw->butt[ 2 ]), 3);
			gtk_signal_connect( GTK_OBJECT(nw->butt[2]), "clicked", GTK_SIGNAL_FUNC(doNotice), (void*)-1 );
		  GTK_WIDGET_SET_FLAGS (nw->butt[ 2 ], GTK_CAN_DEFAULT);
 		}
	}	

  gtk_widget_grab_default (nw->butt[ 0 ]);
  gtk_widget_grab_focus (nw->butt[ 0 ]);

	gtk_widget_show( nw->win );

	if ( gtkMainW ) {
		gtk_window_set_transient_for( GTK_WINDOW(nw->win), GTK_WINDOW( gtkMainW->gtkwin) );
/*		gdk_window_set_group( nw->win->window, gtkMainW->gtkwin->window ); */
	}
	gtkDoModal( NULL, TRUE );
	
	if( aff )
		free( aff );
		
	if( can )
		free( can );
		
	if( alt )
		free( alt );		
		
	return noticeValue;
}


EXPORT void wFlush(
		void )
/*
Flushs all commands to the Window.
*/
{
	while ( gtk_events_pending() )
		gtk_main_iteration();

	gdk_display_sync(gdk_display_get_default());
}


void wWinTop( wWin_p win )
{
}


void wSetCursor( wCursor_t cursor )
{
}


const char * wMemStats( void )
{
#ifdef LATER
	static char msg[80];
	struct mstats stats;
	stats = mstats();
	sprintf( msg, "Total %d, used %d(%d), free %d(%d)",
				stats.bytes_total,
				stats.bytes_used, stats.chunks_used,
				stats.bytes_free, stats.chunks_free );
	return msg;
#else
	return "No stats available";
#endif
}


wBool_t wCheckExecutable( void )
{
	return TRUE;
}


void wGetDisplaySize( wPos_t * w, wPos_t * h )
{

	*w = gdk_screen_width();
	*h = gdk_screen_height();
}


wIcon_p wIconCreateBitMap( wPos_t w, wPos_t h, const char * bits, wDrawColor color )
{
	wIcon_p ip;
	ip = (wIcon_p)malloc( sizeof *ip );
	ip->gtkIconType = gtkIcon_bitmap;
	ip->w = w;
	ip->h = h;
	ip->color = color;
	ip->bits = bits;
	return ip;
}

wIcon_p wIconCreatePixMap( char *pm[] )
{
	wIcon_p ip;
	ip = (wIcon_p)malloc( sizeof *ip );
	ip->gtkIconType = gtkIcon_pixmap;
	ip->w = 0;
	ip->h = 0;
	ip->color = 0;
	ip->bits = pm;
	return ip;
}


void wIconSetColor( wIcon_p ip, wDrawColor color )
{
	ip->color = color;
}

void wConvertToCharSet( char * buffPtr, int buffMax )
{
}


void wConvertFromCharSet( char * buffPtr, int buffMax )
{
}

static dynArr_t conversionBuffer_da;
#define convesionBuffer(N) DYNARR_N( char, conversionBuffer_da, N )

char * gtkConvertInput( const char * inString )
{
#ifndef GTK1
	const char * cp;
	char * cq;
	int extCharCnt, inCharCnt;

	/* Already UTF-8 encoded? */
	if (g_utf8_validate(inString, -1, NULL))
		/* Yes, do not double-convert */
		return (char*)inString;
#ifdef VERBOSE
	fprintf(stderr, "gtkConvertInput(%s): Invalid UTF-8, converting...\n", inString);
#endif

	for ( cp=inString, extCharCnt=0; *cp; cp++ ) {
		if ( ((*cp)&0x80) != 0 )
			extCharCnt++;
	}
	inCharCnt = cp-inString;
	if ( extCharCnt == 0 )
		return (char*)inString;
	DYNARR_SET( char, conversionBuffer_da, inCharCnt+extCharCnt+1 );
	for ( cp=inString, cq=(char*)conversionBuffer_da.ptr; *cp; cp++ ) {
		if ( ((*cp)&0x80) != 0 ) {
			*cq++ = 0xC0+(((*cp)&0xC0)>>6);
			*cq++ = 0x80+((*cp)&0x3F);
		} else {
			*cq++ = *cp;
		}
	}
	*cq = 0;
	return (char*)conversionBuffer_da.ptr;
#else
	return (char*)inString;
#endif
}


char * gtkConvertOutput( const char * inString )
{
#ifndef GTK1
	const char * cp;
	char * cq;
	int extCharCnt, inCharCnt;
	for ( cp=inString, extCharCnt=0; *cp; cp++ ) {
		if ( ((*cp)&0xC0) == 0x80 )
			extCharCnt++;
	}
	inCharCnt = cp-inString;
	if ( extCharCnt == 0 )
		return (char*)inString;
	DYNARR_SET( char, conversionBuffer_da, inCharCnt+1 );
	for ( cp=inString, cq=(char*)conversionBuffer_da.ptr; *cp; cp++ ) {
		if ( ((*cp)&0x80) != 0 ) {
			*cq++ = 0xC0+(((*cp)&0xC0)>>6);
			*cq++ = 0x80+((*cp)&0x3F);
		} else {
			*cq++ = *cp;
		}
	}
	*cq = 0;
	return (char*)conversionBuffer_da.ptr;
#else
	return (char*)inString;
#endif
}

/*-----------------------------------------------------------------*/

typedef struct accelData_t {
		wAccelKey_e key;
		int modifier;
		wAccelKeyCallBack_p action;
		void * data;
		} accelData_t;
static dynArr_t accelData_da;
#define accelData(N) DYNARR_N( accelData_t, accelData_da, N )


static guint accelKeyMap[] = {
			0,			/* wAccelKey_None, */
			GDK_Delete,		/* wAccelKey_Del, */
			GDK_Insert,		/* wAccelKey_Ins, */
			GDK_Home,		/* wAccelKey_Home, */
			GDK_End,		/* wAccelKey_End, */
			GDK_Page_Up,	/* wAccelKey_Pgup, */
			GDK_Page_Down,	/* wAccelKey_Pgdn, */
			GDK_Up,		/* wAccelKey_Up, */
			GDK_Down,		/* wAccelKey_Down, */
			GDK_Right,		/* wAccelKey_Right, */
			GDK_Left,		/* wAccelKey_Left, */
			GDK_BackSpace,	/* wAccelKey_Back, */
			GDK_F1,		/* wAccelKey_F1, */
			GDK_F2,		/* wAccelKey_F2, */
			GDK_F3,		/* wAccelKey_F3, */
			GDK_F4,		/* wAccelKey_F4, */
			GDK_F5,		/* wAccelKey_F5, */
			GDK_F6,		/* wAccelKey_F6, */
			GDK_F7,		/* wAccelKey_F7, */
			GDK_F8,		/* wAccelKey_F8, */
			GDK_F9,		/* wAccelKey_F9, */
			GDK_F10,		/* wAccelKey_F10, */
			GDK_F11,		/* wAccelKey_F11, */
			GDK_F12		/* wAccelKey_F12, */
		};


EXPORT void wAttachAccelKey(
		wAccelKey_e key,
		int modifier,
		wAccelKeyCallBack_p action,
		void * data )
{
	accelData_t * ad;
	if ( key < 1 || key > wAccelKey_F12 ) {
		fprintf( stderr, "wAttachAccelKey(%d) out of range\n", (int)key );
		return;
	}
	DYNARR_APPEND( accelData_t, accelData_da, 10 );
	ad = &accelData(accelData_da.cnt-1);
	ad->key = key;
	ad->modifier = modifier;
	ad->action = action;
	ad->data = data;
}


EXPORT struct accelData_t * gtkFindAccelKey(
		GdkEventKey * event )
{
	accelData_t * ad;
	int modifier = 0;
	if ( ( event->state & GDK_SHIFT_MASK ) )
		modifier |= WKEY_SHIFT;
	if ( ( event->state & GDK_CONTROL_MASK ) )
		modifier |= WKEY_CTRL;
	if ( ( event->state & GDK_MOD1_MASK ) )
		modifier |= WKEY_ALT;
	for ( ad=&accelData(0); ad<&accelData(accelData_da.cnt); ad++ )
		if ( event->keyval == accelKeyMap[ad->key] &&
			 modifier == ad->modifier )
			return ad;
	return NULL;
}


EXPORT wBool_t gtkHandleAccelKey(
		GdkEventKey *event )
{
	accelData_t * ad = gtkFindAccelKey( event );
	if ( ad ) {
		ad->action( ad->key, ad->data );
		return TRUE;
	}
	return FALSE;
}

/*
 *****************************************************************************
 *
 * Timer Functions 
 *
 *****************************************************************************
 */

static wBool_t gtkPaused = FALSE;
static int alarmTimer = 0;

static gint doAlarm(
		gpointer data )
{
	wAlarmCallBack_p func = (wAlarmCallBack_p)data;
	if (alarmTimer)
		gtk_timeout_remove( alarmTimer );
	func();
	alarmTimer = 0;
	return 0;
}


EXPORT void wAlarm(
		long count,
		wAlarmCallBack_p func )		/* milliseconds */
/*
Alarm for <count> milliseconds.
*/
{
	gtkPaused = TRUE;
	if (alarmTimer)
		gtk_timeout_remove( alarmTimer );
	alarmTimer = gtk_timeout_add( count, doAlarm, (void *) (GtkFunction)func );
}


static wControl_p triggerControl = NULL;
static setTriggerCallback_p triggerFunc = NULL;

static void doTrigger( void )
{
	if (triggerControl && triggerFunc) {
		triggerFunc( triggerControl );
		triggerFunc = NULL;
		triggerControl = NULL;
	}
}

void gtkSetTrigger(
		wControl_p b,
		setTriggerCallback_p trigger )
{
	triggerControl = b;
	triggerFunc = trigger;
	wAlarm( 500, doTrigger );
}


EXPORT void wPause(
		long count )		/* milliseconds */
/*
Pause for <count> milliseconds.
*/
{
	struct timeval timeout;
	sigset_t signal_mask;
	sigset_t oldsignal_mask;
	
	gdk_display_sync(gdk_display_get_default());
	
	timeout.tv_sec = count/1000;
	timeout.tv_usec = (count%1000)*1000;
	
	sigemptyset( &signal_mask );
	sigaddset( &signal_mask, SIGIO );
	sigaddset( &signal_mask, SIGALRM );	
	sigprocmask( SIG_BLOCK, &signal_mask, &oldsignal_mask );
	
	if (select( 0, NULL, NULL, NULL, &timeout ) == -1) {
		perror("wPause:select");
	}
	sigprocmask( SIG_BLOCK, &oldsignal_mask, NULL );
}


unsigned long wGetTimer( void )
{
	struct timeval tv;
	struct timezone tz;
	int rc;
	rc = gettimeofday( &tv, &tz );
	return (tv.tv_sec-startTime.tv_sec+1) * 1000 + tv.tv_usec /1000;
}



/**
 * Add control to circular list of synonymous controls. Synonymous controls are kept in sync by 
 * calling wControlLinkedActive for one member of the list 
 *
 * \param b1 IN  first control
 * \param b2 IN  second control
 * \return    none
 */
 
EXPORT void wControlLinkedSet( wControl_p b1, wControl_p b2 )
{
	
	b2->synonym = b1->synonym;
	if( b2->synonym == NULL )
		b2->synonym = b1;
		
	b1->synonym = b2;
} 	

/**
 * Activate/deactivate a group of synonymous controls.
 *
 * \param b IN  control
 * \param active IN  state
 * \return    none
 */
 

EXPORT void wControlLinkedActive( wControl_p b, int active )
{
	wControl_p savePtr = b;

	if( savePtr->type == B_MENUITEM )
		wMenuPushEnable( (wMenuPush_p)savePtr, active );
	else 	
		wControlActive( savePtr, active );		

	savePtr = savePtr->synonym;

	while( savePtr && savePtr != b ) {	

		if( savePtr->type == B_MENUITEM )
			wMenuPushEnable( (wMenuPush_p)savePtr, active );
		else 	
			wControlActive( savePtr, active );		

		savePtr = savePtr->synonym;
	}	
}

/*
 *****************************************************************************
 *
 * Control Utilities
 *
 *****************************************************************************
 */

EXPORT void wControlShow(
		wControl_p b,		/* Control */
		wBool_t show )		/* Command */
/*
Cause the control <b> to be displayed or hidden.
Used to hide control (such as a list) while it is being updated.
*/
{
	if ( b->type == B_LINES ) {
		gtkLineShow( (wLine_p)b, show );
		return;
	}
	if (b->widget == 0) abort();
	if (show) {
		gtk_widget_show( b->widget );
		if (b->label)
			gtk_widget_show( b->label );
	} else {
		gtk_widget_hide( b->widget );
		if (b->label)
			gtk_widget_hide( b->label );
	}
}

EXPORT void wControlActive(
		wControl_p b,		/* Control */
		int active )		/* Command */
/*
Cause the control <b> to be marked active or inactive.
Inactive controls donot respond to actions.
*/
{
	if (b->widget == 0) abort();
	gtk_widget_set_sensitive( GTK_WIDGET(b->widget), active );
}


EXPORT wPos_t wLabelWidth(
		const char * label )		/* Label */
/*
Returns the width of <label>.
This is used for computing window layout.
Typically the width to the longest label is computed and used as
the X-position for <controls>.
*/
{
	GtkWidget * widget;
	GtkRequisition requisition;
	widget = gtk_label_new( gtkConvertInput(label) );
	gtk_widget_size_request( widget, &requisition );
	gtk_widget_destroy( widget );
	return requisition.width+8;
}


EXPORT wPos_t wControlGetWidth(
		wControl_p b)		/* Control */
{
	return b->w;
}


EXPORT wPos_t wControlGetHeight(
		wControl_p b)		/* Control */
{
	return b->h;
}


EXPORT wPos_t wControlGetPosX(
		wControl_p b)		/* Control */
{
	return b->realX;
}


EXPORT wPos_t wControlGetPosY(
		wControl_p b)		/* Control */
{
	return b->realY - FOUR - ((b->parent->option&F_MENUBAR)?MENUH:0);
}


EXPORT void wControlSetPos(
		wControl_p b,		/* Control */
		wPos_t x,		/* X-position */
		wPos_t y )		/* Y-position */
{
	b->realX = x;
	b->realY = y + FOUR + ((b->parent->option&F_MENUBAR)?MENUH:0);
#ifndef GTK1
	if (b->widget)
		gtk_fixed_move( GTK_FIXED(b->parent->widget), b->widget, b->realX, b->realY );
	if (b->label)
		gtk_fixed_move( GTK_FIXED(b->parent->widget), b->label, b->realX-b->labelW, b->realY+LABEL_OFFSET );
#else
	if (b->widget)
		gtk_widget_set_uposition( b->widget, b->realX, b->realY );
	if (b->label)
		gtk_widget_set_uposition( b->label, b->realX-b->labelW, b->realY+LABEL_OFFSET );
#endif
}


EXPORT void wControlSetLabel(
		wControl_p b,
		const char * labelStr )
{
	GtkRequisition requisition;
	if (b->label) {
		gtk_label_set( GTK_LABEL(b->label), gtkConvertInput(labelStr) );
		gtk_widget_size_request( b->label, &requisition );
		b->labelW = requisition.width+8;
#ifndef GTK1
		gtk_fixed_move( GTK_FIXED(b->parent->widget), b->label, b->realX-b->labelW, b->realY+LABEL_OFFSET );
#else
		gtk_widget_set_uposition( b->label, b->realX-b->labelW, b->realY+LABEL_OFFSET );
#endif
	} else {
		b->labelW = gtkAddLabel( b, labelStr );
	}
}

EXPORT void wControlSetContext(
		wControl_p b,
		void * context )
{
	b->data = context;
}


EXPORT void wControlSetFocus(
		wControl_p b )
{
}


static int gtkControlHiliteWidth = 3;
EXPORT void wControlHilite(
		wControl_p b,
		wBool_t hilite )
{
	int off = gtkControlHiliteWidth/2+1;
	if ( b->parent->gc == NULL ) {
		b->parent->gc = gdk_gc_new( b->parent->gtkwin->window );
		gdk_gc_copy( b->parent->gc, b->parent->gtkwin->style->base_gc[GTK_STATE_NORMAL] );
		b->parent->gc_linewidth = 0;
		gdk_gc_set_line_attributes( b->parent->gc, b->parent->gc_linewidth, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER );
	}
	if ( b->widget == NULL )
		return;
	if ( ! GTK_WIDGET_VISIBLE( b->widget ) )
		return;
	if ( ! GTK_WIDGET_VISIBLE( b->parent->widget ) )
		return;
	gdk_gc_set_foreground( b->parent->gc, gtkGetColor( wDrawColorBlack, FALSE ) );
	gdk_gc_set_function( b->parent->gc, GDK_XOR );
	gdk_gc_set_line_attributes( b->parent->gc, gtkControlHiliteWidth, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER );
	gdk_draw_line( b->parent->widget->window, b->parent->gc,
		b->realX - gtkControlHiliteWidth,
		b->realY - off,
		b->realX + b->w + gtkControlHiliteWidth,
		b->realY - off );
	gdk_draw_line( b->parent->widget->window, b->parent->gc,
		b->realX - gtkControlHiliteWidth,
		b->realY + b->h + off - 1,
		b->realX + b->w + gtkControlHiliteWidth,
		b->realY + b->h + off - 1 );
	gdk_draw_line( b->parent->widget->window, b->parent->gc,
		b->realX - off,
		b->realY,
		b->realX - off,
		b->realY + b->h );
	gdk_draw_line( b->parent->widget->window, b->parent->gc,
		b->realX + b->w + off - 1,
		b->realY,
		b->realX + b->w + off - 1,
		b->realY + b->h );
}

/*
 *******************************************************************************
 *
 * Main
 *
 *******************************************************************************
 */

#ifdef GTK
static wBool_t wAbortOnErrors = FALSE;
#endif

int do_rgb_init = 1;

int main( int argc, char *argv[] )
{
	wWin_p win;
	wControl_p b;
	const char *ld, *hp;
	static char buff[BUFSIZ];

	if ( getenv( "GTKLIB_NOLOCALE" ) == 0 )
		setlocale( LC_ALL, "en_US" );
	gtk_init( &argc, &argv );
	gettimeofday( &startTime, NULL );

	if ( getenv( "XVLIB_REVERSEICON" ) != 0 )
		reverseIcon = !reverseIcon;

	if ( do_rgb_init )
		gdk_rgb_init();	/* before we try to draw */

	if ((win=wMain( argc, argv )) == (wWin_p)0)
		exit(1);

	ld = wGetAppLibDir();
	if (ld != NULL) {
		sprintf( buff, "HELPPATH=/usr/lib/help:%s:", ld );
		if ( (hp = getenv("HELPPATH")) != NULL )
			strcat( buff, hp );
		putenv( buff );
	}

	if (!win->shown)
		wWinShow( win, TRUE );
	for (b=win->first; b != NULL; b = b->next) {
		if (b->repaintProc)
			b->repaintProc( b );
	}
	gtk_main();
	exit(0);
}