File: callbacks.c

package info (click to toggle)
xmorph 1%3A20011220
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,468 kB
  • ctags: 1,534
  • sloc: ansic: 16,401; sh: 2,651; makefile: 556; tcl: 516
file content (1756 lines) | stat: -rw-r--r-- 41,507 bytes parent folder | download
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
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include "stdio.h"
#include "strings.h"
#include "string.h"

#include <gtk/gtk.h>

#ifdef USE_IMLIB
#include <gdk_imlib.h>
#else
#include <gdk-pixbuf/gdk-pixbuf.h>
#endif

#include <gdk/gdk.h>

#include "gtktopdata.h"

#include "gtk_subimagesel.h"

#include "main.h"

#include "callbacks.h"
#include "interface.h"
#include "support.h"
//#include "pixmaps.h"
#include "mesh-gtk.h"
#include "utils.h"
#include "gtk-extra.h"
#include "../libmorph/relax.h"

/* we will implement UNDO by copying this structure and its substructures */
gtkmorph_status_t  settings,
  *sp = &settings;


char * theunimplemented_text="sorry this functions is as yet unimplemented";
#define unimplemented_text  (_(theunimplemented_text))





/* some code was taken from the gtk-tutorial , the "scribble" example */


/*******************************************************************
******************************************************************

                    main window 

**********************************************************************
**********************************************************************

*/



void
on_morph_help_activate                 (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  show_warning( _("\
To morph:\n\
  1) load an image in each `input image',\n\
  2) edit the meshes by dragging the points\n\
  3) set the `blending factors' and `mesh factors' as desired\n\
  3) and hit `do morph'\n\
Tips:\n\
  0) as soon as you load the images, set all factors to equal values and\n\
    do a morph: if the images do not superimpose well, you may try to select\n\
    a subimage in each image so that they superimpose better\n\
  1) try to keep the mesh lines as linear as possible:\n\
   add new lines (with right mouse button) if this helps!\n\
  2) to have a better morph, for each image, set the `morph factors' to\n\
    a maximum, hit `do warp' and try to adjust the mesh until this warp\n\
    looks ok right\n\
  3) if you are fighting with small details, then you should increase\n\
    the resulting image size until you have fixed things\n\
")  );
}


void
on_warp_help_activate                  (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  show_warning( _("\
It is not difficult to use this program for warping: just\n\
  1) load an image in the `input image 1',\n\
  2) edit the meshes by dragging the points (and use the menu that you get\n\
       by the right mouse button)\n\
  3) and hit `do warp'\n\
")  );
}


void
on_generic_help_activate               (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  show_warning( _("\
Sorry, there is no real help at the moment; but here are a few clues:\n\
0) at startup the program is set for ``warping'': read the ``warp help''\n\
1) to morph, you need to have 2 or more `input images' :\n\
 use `add an image' (it is in the `file' menu); read the ``morph help''\n\
2) if you keep the mouse still on a menu voice or on a button\n\
  for a moment, you may read the help tips.\n\
3) when the mouse pointer is in on the mesh grid, by hitting\n\
  the right button, you get an useful menu.\n\
4)  if you are `preserving aspect ratio' (see `Settings'),\n\
 when you change the resulting image size, always keep it square:\n\
  the meshes do not scale well otherwise\n\
Nonetheless, it is not difficult to use this program:\n\
read the two short helps below.")  );
}


//void
//on_logtext_realize                     (GtkWidget       *widget,
//                                        gpointer         user_data)
//{
//
//}



/*
  main menus callbacks 

 */


void
on_load_image_set_activate             (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  show_warning( unimplemented_text); //show_fs(menuitem);
}


void
on_load_mesh_set_activate              (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{ 
  show_warning( unimplemented_text); //show_fs(menuitem);
}



void
on_save_image_set_activate             (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  show_warning( unimplemented_text); //show_fs(menuitem);
}


void
on_preferencesmenu_activate            (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  show_warning( unimplemented_text); //show_fs(menuitem);
}


void
on_save_meshes_set_activate            (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  show_warning( unimplemented_text); //show_fs(menuitem);  
}


void
on_save_morphed_activate               (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  show_warning( unimplemented_text); //show_fs(menuitem);
}


void
on_add_an_image_activate               (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
    int lp;
    for(lp=1 ; lp <= MAX_WINS;  lp ++)
      if(sp->im_widget[lp] == NULL)
	{
	  /* we set this before, 
	     otherwise the "factor_normalize" goes berseker */
	  sp->max_wins++;

	  create_and_show_image_win(lp);	
	  setup_handlebox_factors();
	  return; 
	}
}



void 
on_quit1_activate                      (GtkMenuItem     *menuitem,
					gpointer         user_data) 
{ 
  //FIXME we should handle this more gracefully
  gtk_main_quit();
} 




void
on_view_images1_activate               (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  int lp;
  for(lp=1 ; lp <= MAX_WINS+1 ; lp ++)
    {
      if(sp->im_widget[lp] != NULL)
	gtk_widget_show (sp->im_widget[lp]);
    }
}

void
redraw_spins();


double
tot_mixing_factors()
{
  double  totdiss;
  int lp;
  totdiss=0;
  for ( lp =1; lp <= MAX_WINS; lp++)
    if(sp->im_widget[lp] != NULL)
      totdiss += sp->mf.im_dissolve_factor[lp];
  // FIXME this is not shown
  sp->mf.im_dissolve_factor[MAX_WINS+1]=totdiss; 
  
  return totdiss;
}

double
tot_mixing_factors_nice()
{
  double totdiss=tot_mixing_factors();
  int lp;
  if ( ABS(totdiss) < 0.001)
    {
      show_warning( _("\
to blend the images, the sum of the all `image mixing factors' must be nonzero\
\nI have put default values for you"));
      
      for ( lp =1; lp <= MAX_WINS; lp++)
	if(sp->im_widget[lp] != NULL) {
	  sp->mf.im_dissolve_factor[lp]=1.0/(double)sp->max_wins;
	  //gtk_widget_get_data_top(sp->im_widget[lp],"imagenum")
	}
      totdiss =1;
      redraw_spins();
    }
  return totdiss;
}


/********************************
does the actual morphing 
********************************/

void
on_morph_images1_activate              (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  int lp;
  //double  totdiss=tot_mixing_factors_nice();

  /* geometry of the resulting image */
  int w=sp->resulting_width, h=sp->resulting_height;
  long size = w * h ;

  guint8 * r , *b, *g;

  if( sp->max_wins<=1)
    {
      show_warning( _("\
to morph, you must have at least two input images"));
      return;
    }
  
 if( settings_get_value("automatic mesh interpolation"))
   on_interpolate_meshes1_activate (NULL,NULL);

  //meshAlloc(dstmesh, nx , ny );

/*    totdiss=0; */
/*    for ( lp =1; lp <= MAX_WINS; lp++) */
/*      if(sp->im_widget[lp] != NULL) */
/*        totdiss += sp->mf.im_dissolve_factor[lp]; */

 
  // FIXME this is not shown
  //sp->mf.im_dissolve_factor[MAX_WINS+1]=totdiss;

  //we clear the old image
 //gdk_pixbuf_clear(sp-> im_pixbufwarped[MAX_WINS+1]);

  r = g_malloc( size );
  g = g_malloc( size );
  b = g_malloc( size );
  
  for ( lp =1; lp <= MAX_WINS; lp++)
    if(sp->im_widget[lp] != NULL)
      {
	if( ABS(sp->mf.im_dissolve_factor[lp]) < 0.001 )      
	  g_message("NOT morphing image %d to inbetween\n", lp);
	else
	  {
	    g_message("morphing image %d to inbetween\n", lp);
	    do_warp_an_image(lp, r , g, b);
	    /* dissolve in the total */
	    //g_message("writing pixels for %d\n",lp);
	    //{
	      
	      //GdkPixbuf *pb = sp-> im_pixbufwarped[MAX_WINS+1];
	      //FIXME this doesnt work if there are negative coefficents 
	      //rrrgggbbb_add_to_pixbuf(pb,    r,g,b,
	      //		      sp->mf.im_dissolve_factor[lp] / totdiss
	      //			      );	
	      //we clear the old warped image
	      //gdk_pixbuf_clear(sp-> im_pixbufwarped[lp]);	      
	      //rrrgggbbb_add_to_pixbuf(sp-> im_pixbufwarped[lp],    r,g,b,
	      //			      1.0
	      //			      );
		
	    //}
/* 	    gdk_pixbuf_render_to_drawable   */
/* 	      (sp->im_pixbufwarped[lp], */
/* 	       sp->im_pixwarped[lp], */
/* 	       sp->im_widget[lp]->style->black_gc, //GdkGC *gc, */
/* 	       0, //int src_x, */
/* 	       0, //int src_y, */
/* 	       0, //int dest_x, */
/* 	       0, //int dest_y, */
/* 	       w,h, //width, height, */
/* 	       GDK_RGB_DITHER_NORMAL,//GdkRgbDither dither, */
/* 	       0, //int x_dither, */
/* 	       0 ); //int y_dither); */
	  }
      }

  g_free(r);   g_free(g);   g_free(b); 

  on_do_mixing_clicked(NULL,NULL);
  
/*   gdk_pixbuf_render_to_drawable   */
/*     (sp->im_pixbufwarped[MAX_WINS+1], */
/*      sp->im_pixwarped[MAX_WINS+1], */
/*      sp->im_widget[MAX_WINS+1]->style->black_gc, //GdkGC *gc, */
/*      0, //int src_x, */
/*      0, //int src_y, */
/*      0, //int dest_x, */
/*      0, //int dest_y, */
/*      w,h, //width, height, */
/*      GDK_RGB_DITHER_NORMAL,//GdkRgbDither dither, */
/*      0, //int x_dither, */
/*      0 ); //int y_dither); */

  set_editshow(MAX_WINS+1, settings_get_value( "show warp after warp") );
  gtk_widget_draw (sp->im_widget[MAX_WINS+1] , NULL);
}






/***********************************************************
 *
 *************************** file selection  ************
 *
 **********************************************************

 the following hook is set 
 so that the same fileselection dialog will be used for many
 different purposes

*/

/* see utils.h */

fileselection_hook_t fileselection_hook=NULL;

void
on_ok_button1_realize                  (GtkWidget       *widget,
                                        gpointer         user_data)
{
//  GtkWidget       *widget_f=  gtk_widget_get_toplevel (widget);
// doesnt work
//  gtk_container_add (GTK_CONTAINER (widget_f), 
//		     menu_image_num_g);

//  gtk_menu_item_set_submenu(widget,create_menu_image_num_g );
}



void
on_ok_button1_clicked         (GtkButton       *button,
			       gpointer         user_data)
{  
  char *file=
    gtk_file_selection_get_filename
    (GTK_FILE_SELECTION (imageselection1_g));


  g_assert(fileselection1_for_image_num_g > 0);

  if( //fileselection_hook=load_image_from_file
     fileselection_hook  (fileselection1_for_image_num_g,
			  file))
    {

      gtk_widget_hide(GTK_WIDGET(imageselection1_g));
      gtk_widget_show( sp-> im_widget[fileselection1_for_image_num_g]);  

      // lets be nasty
      fileselection1_for_image_num_g=-2;
      fileselection_hook=NULL;
    }

}
























/********************************************************************
 *
 * input image window, drawingarea
 *

 note: many callbacks are shared with the "resulting image window"

 ******************************************************************
**/




/***************************************************************

 *		drawing area callbacks

 ***********************************************************
*/ 



void
on_drawingarea_realize                 (GtkWidget       *widget,
				     gpointer         user_data)
{
  int i=
    GPOINTER_TO_UINT(gtk_widget_get_data_top(widget,"imagenum")); 
  /* the drawing area widget will be accessible tru this */
  gtk_widget_set_data_top(widget, "drawingarea widget", widget);

  sp->im_drawingarea_widget[i] = widget;

  drawingarea_configure  (i);
}



/* save also the viewport widget address, so we can resize it easily 
   
   actually "glade" does the same thing, and I didnt know it
*/

void
on_viewport3_realize                   (GtkWidget       *widget,
                                        gpointer         user_data)
{
  gtk_widget_set_data_top(widget, "viewport widget", widget);
}



gboolean
on_drawingarea_configure_event         (GtkWidget       *widget,
                                        GdkEventConfigure *event,
                                        gpointer         user_data)
{
  //int i=
  // GPOINTER_TO_UINT(gtk_widget_get_data_top(widget,"imagenum")); 
  // this loops
  //g_message(" configure drawingarea %d",i);
  //drawingarea_configure(i);
  return TRUE;
}




gboolean
on_expose_event                        (GtkWidget       *widget,
                                        GdkEventExpose  *event,
                                        gpointer         user_data)
{
  int i=
    GPOINTER_TO_UINT(gtk_widget_get_data_top(widget,"imagenum")); 
  GdkPixmap *pixmap = *(which_pixmap_is_visible(i));// get_pixmap_addr(widget);

  g_assert(i > 0);
  g_assert(pixmap != NULL);

  /* the gtk+ manual (see "drawing area" section)
     says we have to:*/
  gdk_gc_set_clip_rectangle (widget->style->fg_gc[widget->state],
			     &event->area);

  // this one flashes too much
  //gdk_window_clear_area (widget->window,
  //                        event->area.x, event->area.y,
  //                        event->area.width, event->area.height);

  /* then we draw...*/ 
  gdk_draw_pixmap(widget->window,
		  widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
		  pixmap,
		  event->area.x, event->area.y,
		  event->area.x, event->area.y,
		  event->area.width, event->area.height);
 if( sp->im_editshow[i]==EDITSHOW_EYES)
   {
     gdk_subimagesel_draw( widget->window, &(sp->subimasel[i]), mpl_gc);
   }
 else {
   //if(sp->im_editshow[i]!=EDITSHOW_SHOW ) 
   if ( image_settings_get_value("view original mesh",i))
       gdk_draw_mesh(widget->window,
		     mpl_gc ,
		     mps_gc ,
		     &(sp->im_mesh[i]),// MeshT *mesh, 
		     NULL, //&(sp->subimage[i]),
		     sp->resulting_height,
		     sp->resulting_width
		     );
       
   //if(sp->im_editshow[i]==EDITSHOW_SHOWMESHES && i < MAX_WINS+1)
   if ( image_settings_get_value("view warped mesh",i))
   gdk_draw_mesh(widget->window,
		 widget->style->white_gc ,
		 mpr_gc,
		 &(sp->im_mesh[MAX_WINS+1]),// MeshT *mesh,
		 NULL,//&(sp->subimage[i]),
		 sp->resulting_height,
		 sp->resulting_width
		 );       
 }

  /* sfter that, we  */
  gdk_gc_set_clip_rectangle (widget->style->fg_gc[widget->state],
			     NULL);
  return TRUE;
}



/* gboolean */
/* on_configure_event                     (GtkWidget       *widget, */
/*                                         GdkEventConfigure *event, */
/*                                         gpointer         user_data) */
/* { */
/* //  GdkPixmap *pixmap = get_pixmap_addr(widget); */
/*   //g_assert(pixmap != NULL); */

/*   return FALSE; */

/* } */


























 

/********************************************************************
 *
 * input image window, top bar buttons
 *

 note: many callbacks are shared with the "resulting image window"

 ******************************************************************
**/



/***********************************************************************
  load/save interface
*/




void check_fs()
{
  /*gtk_widget_destroy(imageselection1_g);*/
  if (! GTK_IS_WIDGET (imageselection1_g))
    {
      g_warning("lost image selection widget\n");
      imageselection1_g = GTK_FILE_SELECTION(create_imageselection1() );
    }
}

/***  show file selector 
      
      if title==NULL, make up a title
      
      adds to the title the window number      

      sets the variable fileselection1_for_image_num_g
*/

void show_fs(void *widget,
	     char *title /* fileselection title */
	     ) 
{
  char s[200];
  check_fs();

  fileselection1_for_image_num_g= 
    GPOINTER_TO_UINT(gtk_widget_get_data_top(widget,"imagenum")); 

  g_assert(fileselection1_for_image_num_g > 0);

  sprintf(s,"%s %d",
	   ((title==NULL)?
	    gtk_widget_get_name(GTK_WIDGET(widget)) :
	    title)    ,
	  fileselection1_for_image_num_g);
 
  gtk_window_set_title(GTK_WINDOW (imageselection1_g), s);  

  gtk_widget_show( GTK_WIDGET(imageselection1_g));
}




void
on_loadimage_clicked                   (GtkButton       *button,
                                        gpointer         user_data)
{
  
  int i = GPOINTER_TO_UINT(gtk_widget_get_data_top(GTK_WIDGET(button),"imagenum")); 

  fileselection_hook=load_image_from_file;
       
  if (sp->im_filename[i] != NULL)
    gtk_file_selection_set_filename ((imageselection1_g),
				     sp->im_filename[i] );
  else
    gtk_file_selection_set_filename ((imageselection1_g), 
				     g_strdup_printf("%s%d.png",
						     _("image"),i));    
  show_fs(button, _("load image") );  
}

/***** 
       returns TRUE if it was already known
***/
static gboolean
set_fs_mesh_filename(int i)
{
  if ( sp->im_mesh_filename[i] != NULL)
    {	
      gtk_file_selection_set_filename(imageselection1_g,
				      sp->im_mesh_filename[i]);
      return TRUE;
    }
  else
    {
      char *file= sp-> im_filename[i];
      
      if(file != NULL) { 
	char *s = g_strdup_printf("%s.mesh",file);
	gtk_file_selection_set_filename(imageselection1_g,  s);
	g_free(s);
      }
      return FALSE;
  }
}

void
on_loadmesh_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{
  int i =
    GPOINTER_TO_UINT(gtk_widget_get_data_top(GTK_WIDGET(button),"imagenum")); 

  fileselection_hook=load_mesh_from_file;

  set_fs_mesh_filename(i);

  show_fs(button, _( "load mesh") ); 
}


void
on_savemesh_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

  int i = GPOINTER_TO_UINT(gtk_widget_get_data_top(GTK_WIDGET(button),"imagenum")); 

  fileselection_hook=save_mesh_to_file;
  
  set_fs_mesh_filename(i);

  //FIXME auto save doesnt work
  //if( set_fs_mesh_filename(i) == FALSE|| button->state & GDK_SHIFT_MASK)
  show_fs(button, _( "save mesh n.") ); 
  //  else
  /* directly simulate as if OK was hit */
  //on_ok_button1_clicked   (NULL,NULL); 
}



void
on_save_image_clicked                  (GtkButton       *button,
                                        gpointer         user_data)
{
  int i = GPOINTER_TO_UINT(gtk_widget_get_data_top(GTK_WIDGET(button),"imagenum")); 


  GdkPixbuf * pb=*(which_pixbuf_is_visible(i));

  if( pb ==NULL)
    {
      show_warning(_("\
internal error: the image doesnt exist!"));
      return ;
    }

  if( gdk_pixbuf_get_n_channels(pb) != 3)
    {
      show_warning
	(g_strdup_printf
	 (  " cant save :-( the image has %d channels", 
	    gdk_pixbuf_get_n_channels(pb)  ));
      return ;
    }
 

  fileselection_hook=save_image_to_file;

  {
    char *file= sp-> im_filename[i];

    if(file != NULL)
      { 
	char *s = g_strdup_printf("%s.ppm",file);
	      gtk_file_selection_set_filename
	      (GTK_FILE_SELECTION(imageselection1_g), 
	       s);
	    g_free(s);
      }
  }

  show_fs(button, _( "save image (only PPM format) n.") ); 
}















void
on_settings_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{  
  int i=
    GPOINTER_TO_UINT(gtk_widget_get_data_top(GTK_WIDGET(button),"imagenum")); 
  GtkWidget* m=sp->im_menu_settings[i];
  show_warning( unimplemented_text);
  return ;
  gtk_menu_popup (GTK_MENU(m),//GtkMenu *menu,
		  NULL,//GtkWidget *parent_menu_shell,
		  NULL,//GtkWidget *parent_menu_item,
		  NULL,//GtkMenuPositionFunc func,
		  NULL,//gpointer data,
		  1,//guint button,
		  0);//guint32 activate_time);
}






void
on_do_warp_clicked                     (GtkButton       *button,
                                        gpointer         user_data)
{
  int i=
    GPOINTER_TO_UINT(gtk_widget_get_data_top(GTK_WIDGET(button),"imagenum")); 
 
  /* geometry of the resulting image */
  int w=sp->resulting_width, h=sp->resulting_height;
  long size = w * h ;

  guint8 * r , *b, *g;
  
  r = g_malloc( size );
  g  = g_malloc( size );
  b = g_malloc( size ) ;  
  
  do_warp_an_image(i,r,g,b);
  g_free(r);   g_free(g);   g_free(b); 
}


/*** this routine behaves correclt even if some image factors 
     are negative; the "do_morph" routine didnt
*/
void
on_do_mixing_clicked                   (GtkButton       *button,
                                        gpointer         user_data)
{
  int lp;
  double  totdiss=tot_mixing_factors_nice();

  int     
    w=sp->resulting_width, h=sp->resulting_height;    
  
  if( sp->max_wins<=1)
    {
      show_warning( _("\
to mix images, you must have at least two input images"));
      return;
    }

  {
    GdkPixbuf *dpb = sp-> im_warped_pixbuf[MAX_WINS+1];
    if(dpb==NULL)
      create_pixbuf(MAX_WINS+1, PIXWARPED);
  }
  {

    GdkPixbuf *dpb = sp-> im_warped_pixbuf[MAX_WINS+1];
    guchar  *ddata = gdk_pixbuf_get_pixels(dpb);	
    
    int drowstride= gdk_pixbuf_get_rowstride (dpb) ;
    
    int i,j , dch= gdk_pixbuf_get_n_channels(dpb);

    double val;
    
    long dp=0; /* data position */
    
    /* we precompute some data */
    double fact[MAX_WINS];
    guint8 *data[MAX_WINS];
    int effective_max_wins=0;
    for ( lp =1; lp <= MAX_WINS; lp++) 
      {
	if(sp->im_widget[lp] != NULL &&	  
	   ABS(sp->mf.im_dissolve_factor[lp]) >= 0.001 )	    
	  {  	     	     	     
	    fact[lp] = sp->mf.im_dissolve_factor[lp] / totdiss;
	    data[lp] =	gdk_pixbuf_get_pixels(sp-> im_warped_pixbuf[lp]);
	    g_assert( data[lp] != NULL);

	    effective_max_wins=lp;
	  }
	else
	  data[lp]=NULL;
      }

    //we clear the old image
    gdk_pixbuf_clear(dpb);

    for( j=0; j<  h ; j++)	      
      {
	dp= drowstride * j;
	/* this interpolates also the alpha value, if any  */
	for( i=0; i< w  * dch ; i++)
	  {
	    val = 0 ;   
	    for ( lp =effective_max_wins; lp >= 1; lp--) {
	      if (data[lp] != NULL)
		{
		  val +=  fact[lp] * (double) (data[lp])[dp + i]  ;
		}}
	    /* if teh image is not 8 bit */ 
	    ddata[dp + i] = CLAMP(val, 0, 255);
	  }
      }
  }
  
  render_pixmap(MAX_WINS+1, PIXWARPED);
 
  set_editshow(MAX_WINS+1,  settings_get_value( "show warp after warp")); 
  gtk_widget_draw (sp->im_widget[MAX_WINS+1] , NULL);  
}





void
on_interpolate_meshes1_activate        (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  
  int lp,
    nx=sp-> im_mesh[1].nx ,
    ny=sp-> im_mesh[1].ny ;
  double totwarp;

 if( sp->max_wins<=1)
    {
      show_warning( _("\
to interpolate meshes, you must have at least two input images"));
      return;
    }


 //g_message("creating mesh in between...\n");

  totwarp=0;
  for ( lp =1; lp <= MAX_WINS; lp++)
    if( sp->im_widget[lp] != NULL)
      totwarp += sp->mf.im_warp_factor[lp];

  if ( ABS(totwarp)< 0.0001) {
    show_warning( _("\
to interpolate the meshes, the sum of the all `mesh factors' should be nonzero\n\
I have set some default values for you") );
    for ( lp =1; lp <= MAX_WINS; lp++)
      if( sp->im_widget[lp] != NULL)
	sp->mf.im_warp_factor[lp]=1.0/ (double)sp->max_wins;
    totwarp =1;
    redraw_spins();
  }
  // FIXME this is not shown
  sp->mf.im_warp_factor[MAX_WINS+1]=totwarp;

    {
      int xi, yi;  
      double vx,vy;
      
      for(yi=0; yi < ny; yi++) {
	for(xi=0; xi < nx; xi++) {
	  vx=vy=0;
	  for ( lp =1; lp <= MAX_WINS; lp++)
	    if( sp->im_widget[lp] != NULL)
	      {
		vx += meshGetx( &(sp->im_mesh[lp]), xi, yi)
		  * sp->mf.im_warp_factor[lp];
		vy += meshGety( &(sp->im_mesh[lp]), xi, yi)
		  * sp->mf.im_warp_factor[lp];	      
	      }
	  //printf("%d %d %f %f\n",  xi, yi, vx, vy);
	  meshSetNoundo(dstmesh,  xi, yi, vx /totwarp, vy /totwarp);
	}}
      gtk_widget_draw (sp->im_widget[MAX_WINS+1] , NULL);
    }

     
}





/*****************************************************************************

 *               edit/view option menu

 ****************************************************************************/





void
on_optionmenu_editview_released        (GtkButton       *button,
                                        gpointer         user_data)
{
  /* this signal is (almost) never emitted
  printf("the choice is %s \n",
	 gtk_widget_get_name(gtk_menu_get_active (GTK_MENU(user_data)))); 
   */
}


void
on_optionmenu_editview_clicked         (GtkButton       *button,
                                        gpointer         user_data)
{
  /* this signal is never emitted
     printf("the click choice is %s \n",
     gtk_widget_get_name(gtk_menu_get_active (GTK_MENU(user_data)))); 
  */
}


void
on_optionmenu_editview_pressed         (GtkButton       *button,
                                        gpointer         user_data)
{
  /* this signal is never emitted
  g_message("at %s %d PRESSED" ,__FILE__ , __LINE__);
  */
}



/* since the 3 above do not work... this comes from the glade FAQ */
void
on_optionmenu_editview__selected (GtkMenuShell *menu_shell,
                    gpointer data)
{
  GtkWidget *active_item;
  gint item_index;

  /* the following fails: the menu_shell has a different window
    int i=
    GPOINTER_TO_UINT(gtk_widget_get_data_top(GTK_WIDGET(data),
					     "imagenum")); 
  */
  int i=
    GPOINTER_TO_UINT(data);
  g_assert(i>0);

  active_item = gtk_menu_get_active (GTK_MENU (menu_shell));
  item_index = g_list_index (menu_shell->children, active_item);

  /* thanks GOD this works!!
    g_print ("In on_option_selected active: %i\n", item_index);*/
  sp->im_editshow[i]=item_index;

  editview_callback(i);

  gtk_widget_draw (sp->im_widget[i] , NULL);
}















/*
**********************************************************************

************** resulting image,  spinbuttons      ********************

**********************************************************************
*/


void
on_resulting_apply_clicked             (GtkButton       *button,
                                        gpointer         user_data)
{

  int lp;

  sp->resulting_width =  sp->resulting_width_sp;
  sp->resulting_height = sp->resulting_height_sp;


  for( lp =1; lp <= MAX_WINS+1; lp++)
   if ( sp->im_widget[lp] ) 
     {       
       destroy_image_win_data(lp);
       alloc_image_win_data(lp);
       
       meshScale( &(sp->im_mesh[lp]),
		  sp->resulting_width, sp->resulting_height);

       reload_and_scale_image(lp);

       drawingarea_configure(lp);
       gtk_widget_draw( sp-> im_widget[lp], NULL);  
     }
}


void
on_spinbutton_reswidth_draw            (GtkWidget       *widget,
                                        GdkRectangle    *area,
                                        gpointer         user_data)
{
  int i=
    GPOINTER_TO_UINT(gtk_widget_get_data_top(widget,
					     "imagenum")); 
  g_assert(i>0);  
  gtk_spin_button_set_value (GTK_SPIN_BUTTON(widget),
			     sp->resulting_width_sp );
}


void
on_spinbutton_resheight_draw           (GtkWidget       *widget,
                                        GdkRectangle    *area,
                                        gpointer         user_data)
{
 int i=
    GPOINTER_TO_UINT(gtk_widget_get_data_top(widget,
					     "imagenum")); 
  g_assert(i>0);  
  gtk_spin_button_set_value (GTK_SPIN_BUTTON(widget),
			     sp->resulting_height_sp );
}


void
on_spinbutton_reswidth_changed         (GtkEditable     *editable,
                                        gpointer         user_data)
{
  GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
  sp->resulting_width_sp =gtk_spin_button_get_value_as_float (spin);
}


void
on_spinbutton_resheight_changed        (GtkEditable     *editable,
                                        gpointer         user_data)
{
  GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
  sp->resulting_height_sp =gtk_spin_button_get_value_as_float (spin);
}





/***********************************************************************
**********************************************************************

image windows callbacks


 note: many callbacks are shared with the "resulting image window"

**********************************************************************
*/


gboolean
on_image_win_1_delete_event            (GtkWidget       *widget,
                                        GdkEvent        *event,
                                        gpointer         user_data)
{  
  int i=
    GPOINTER_TO_UINT(gtk_widget_get_data_top(widget,"imagenum")); 

  gtk_widget_hide(widget);

  sp->mf.im_warp_factor[i]=0;
  sp->mf.im_dissolve_factor[i]=0;
  
  return TRUE;
}

/***************************************************************

 *		drawing area mesh editing

 ***********************************************************
*/ 





/******************************* sometimes when we change the labels..*/
int all_images_need_redraw=0;

gboolean
on_button_press_event                  (GtkWidget       *widget,
                                        GdkEventButton  *event,
                                        gpointer         user_data)
{

  int i=
    GPOINTER_TO_UINT(gtk_widget_get_data_top(widget,"imagenum")); 
  //GdkPixmap *pixmap = sp->im_pixmap_subimage[i]; //get_pixmap_addr(widget);
  //g_assert(pixmap != NULL);

  g_assert(i > 0);


  if( ( i == MAX_WINS+1) && sp->max_wins>1 &&
      settings_get_value("automatic mesh interpolation")) 
    { 
      show_warning("you cant edit this mesh - it is automatically generated as an interpolation\nof the input images meshes .\n(but if you really want to edit, unset 'automatic mesh interpolation' ");
      return FALSE;     
    }


  /* if the menu says "view", do not edit */
  if( (sp->im_editshow[i] == EDITSHOW_SHOWMESHES && i <= MAX_WINS) )
    {
      show_warning("\
you are currently viewing the warped version of the image\n\
you may not edit this mesh (which refers to the loaded image).\n\
To edit the mesh, select `edit mesh' in the menu (at top center).\n\
I have done it now for you"); 
      set_editshow(i, EDITSHOW_EDIT); 
      return FALSE; 
    }

  if (sp->im_editshow[i] == EDITSHOW_SHOW )
    {
      show_warning("\
To edit the mesh, select the appropriate voice in the menu (at top center).\n\
I have done it now for you "); 
      set_editshow(i, EDITSHOW_EDIT);
      return FALSE;
    }
  if(sp->im_editshow[i]==EDITSHOW_EYES  )     
    gdk_subimagesel_button_press_event (widget,event,user_data);  
  else
    {
      int mi,mj, action;
      gdk_mesh_button_press_event  (widget,event,
				    user_data, &(sp->im_mesh[i]),
				    &mi,&mj, &action);    
    }
  return TRUE;
}


gboolean
on_motion_notify_event                 (GtkWidget       *widget,
                                        GdkEventMotion  *event,
                                        gpointer         user_data)
{

  {
    int i=
      GPOINTER_TO_UINT(gtk_widget_get_data_top(widget,"imagenum")); 
    //GdkPixmap *pixmap = sp->im_pixmap_subimage[i]; 
    // get_pixmap_addr(widget);
    //g_assert(pixmap != NULL);
    g_assert(i > 0);

    if( ( i == MAX_WINS+1) && sp->max_wins>1 &&
	settings_get_value("automatic mesh interpolation"))
      return FALSE;

    /* if the menu says "view", do not edit */
    if( (sp->im_editshow[i] == EDITSHOW_SHOWMESHES && i <= MAX_WINS) )
      return FALSE;
    if(sp->im_editshow[i]==EDITSHOW_SHOW )
      return FALSE;
  
    if(sp->im_editshow[i]==EDITSHOW_EYES )
      {
	//g_warning("subimasel");
	gdk_subimagesel_motion_notify_event (widget,event,user_data);	
      }
    else
      {
	int mi,mj;
	//g_warning("mesh");
	if(gdk_mesh_motion_notify_event   (widget,event,
					   user_data, &(sp->im_mesh[i]),
					   &mi,&mj))
	  {
	    //if ( 0==meshGetLabel(&(sp->im_mesh[i]), mi, mj))
	    if(settings_get_value("mesh auto sync")) {
	      int lp=MAX_WINS+1; 
	      for(; lp>=0; lp--) 
		if(sp->im_widget[lp] != NULL) {
		  //meshSetLabel(&(sp->im_mesh[lp]), mi, mj, -1);
		  /*gtk_widget_draw (sp->im_widget[lp] , NULL); */
		  all_images_need_redraw++;		
		  flash_point(sp->im_drawingarea_widget[lp]->window,
			      meshGetx(&sp->im_mesh[lp],mi,mj),
			      meshGety(&sp->im_mesh[lp],mi,mj)    );
		  
		}
	    }
	  }
      }
  }
  return TRUE;
}


gboolean
on_drawingarea_button_release_event    (GtkWidget       *widget,
                                        GdkEventButton  *event,
                                        gpointer         user_data)
{

  int i=
    GPOINTER_TO_UINT(gtk_widget_get_data_top(widget,"imagenum")); 

  g_assert(i > 0);

  if( ( i == MAX_WINS+1) && sp->max_wins>1 &&
      settings_get_value("automatic mesh interpolation"))
    return FALSE;

  /* if the menu says "view", do not edit */
  if( (sp->im_editshow[i] == EDITSHOW_SHOWMESHES && i <= MAX_WINS) )
    return FALSE;
  if(sp->im_editshow[i]==EDITSHOW_SHOW )
    return FALSE;

  if(//event->state & GDK_BUTTON1_MASK && 
     sp->im_editshow[i]==EDITSHOW_EYES )
    {
      return TRUE;
    }
  else
    { 
      int mi,mj;
      gdk_mesh_button_release_event    ( widget,event,
					 user_data, &(sp->im_mesh[i]),
					 &mi,&mj);      
      
      if( settings_get_value("automatic mesh interpolation")&& sp->max_wins>1)
	{
	  on_interpolate_meshes1_activate(NULL,NULL);
	  gtk_widget_draw (sp->im_drawingarea_widget[MAX_WINS+1] , NULL);
	}
    }

  if ( all_images_need_redraw) {
    int lp=MAX_WINS; for(; lp>=0; lp--) 
      if(sp->im_widget[lp] != NULL) {	 	
	gtk_widget_draw (sp->im_drawingarea_widget[lp] , NULL);	
      }
    all_images_need_redraw=0;
  }
  else
    gtk_widget_draw (widget , NULL);  
  return TRUE;
}




/***************************************************************************
**************************************************************************

                  handlebox of ranges 

              ****************************

  used to enter the numerical values for im_warp_factor
  and  im_dissolve_factor

**********************************************************************
 */



void
on_handlebox2_show                     (GtkWidget       *widget,
                                        gpointer         user_data)
{
  int i=
    GPOINTER_TO_UINT(gtk_widget_get_data_top(widget,"imagenum")); 

  g_assert(i > 0);
  //FIXME this doesnt work so we use a different window in glade
  if ( i == MAX_WINS+1)
    gtk_widget_hide(widget);
}


void im_factor_normalize(double *p, int i)
{
  double t=0,tt=0;
  int lp;
  
  if((p==sp->mf.im_warp_factor && 
      0== settings_get_value("mesh factors sum to 1"))
     || 
     (p==sp->mf.im_dissolve_factor && 
      0== settings_get_value("image factors sum to 1")))
    return;

  for (lp=1 ; lp<=MAX_WINS ; lp++)
    if(sp->im_widget[lp] != NULL ) {
      tt += p[lp];
      if(i!=lp) t+= p[lp];
    }

  for (lp=1 ; lp<=MAX_WINS ; lp++)
    if(sp->im_widget[lp] != NULL ) {
      if(i!=lp) {
	if(sp->max_wins==2) 
	  p[lp] = 1-p[i];
	else {
	  if( ABS(t) > 0.01)
	    p[lp] = p[lp] * (1 - p[i]) / t;
	  else
	    p[lp] = (1 - p[i]) / ((double) sp->max_wins-1.0) ;
	}
      }
    }
  redraw_spins();
}



void
redraw_spins()
{
  int lp;
  GtkWidget *w;
  GtkSpinButton *s;
  for (lp=1 ; lp<=MAX_WINS ; lp++)
    if( (w=sp->im_widget[lp]) != NULL ) {
      s=GTK_SPIN_BUTTON(lookup_widget(w,"spinbutton_mesh"));
      gtk_spin_button_set_value (s,sp->mf.im_warp_factor[lp]);
      s=GTK_SPIN_BUTTON(lookup_widget(w,"spinbutton_image"));
      gtk_spin_button_set_value (s,sp->mf.im_dissolve_factor[lp]);
   }
}



void
on_spinbutton_mesh_changed             (GtkEditable     *editable,
                                        gpointer         user_data)
{
  GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
  GtkRange *range=GTK_RANGE(user_data);
  double val;
  int i=GPOINTER_TO_UINT(gtk_widget_get_data_top(GTK_WIDGET(editable)
						 ,"imagenum"));

  //g_message ( "spin mesh %0.*f\n", spin->digits,
  //      gtk_spin_button_get_value_as_float (spin));
  
  sp->mf.im_warp_factor[i]=val=gtk_spin_button_get_value_as_float (spin);
  im_factor_normalize(sp->mf.im_warp_factor,i);
  
  range->adjustment->value=val;
  gtk_signal_emit_by_name (GTK_OBJECT (range->adjustment), "changed");

  if( settings_get_value("automatic mesh interpolation")&& sp->max_wins>1)
    on_interpolate_meshes1_activate        (NULL,NULL);
    
}


void
on_spinbutton_image_changed            (GtkEditable     *editable,
                                        gpointer         user_data)
{
  GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
  GtkRange *range=GTK_RANGE(user_data);
  double val;
  int i=GPOINTER_TO_UINT(gtk_widget_get_data_top(GTK_WIDGET(editable),
						 "imagenum"));
    
  //g_message ( "spin image %0.*f\n", spin->digits,
  //	      gtk_spin_button_get_value_as_float (spin));
  
  sp->mf.im_dissolve_factor[i]=val=gtk_spin_button_get_value_as_float (spin);
  im_factor_normalize(sp->mf.im_dissolve_factor, i);

  range->adjustment->value=val;
  gtk_signal_emit_by_name (GTK_OBJECT (range->adjustment), "changed");
}


gboolean
on_hscale_mesh_button_release_event    (GtkWidget       *widget,
                                        GdkEventButton  *event,
                                        gpointer         user_data)
{
  GtkRange *range=GTK_RANGE(widget);
  GtkSpinButton *spin=GTK_SPIN_BUTTON(user_data);

  gtk_spin_button_set_value (spin, range->adjustment->value);

  return FALSE;
}

gboolean
on_hscale_image_button_release_event    (GtkWidget       *widget,
                                        GdkEventButton  *event,
                                        gpointer         user_data)
{
  GtkRange *range=GTK_RANGE(widget);
  GtkSpinButton *spin=GTK_SPIN_BUTTON(user_data);

  gtk_spin_button_set_value (spin, range->adjustment->value);
  return FALSE;
}






















/****************************************************************************/









void
on_settings1_activate                  (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{

}




void
on_warped_im_in_other_win1_activate    (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  //extern int on_warped_im_in_other_win_val;
  //on_warped_im_in_other_win_val=GTK_CHECK_MENU_ITEM(menuitem)->active;
  abort();
}



gboolean
on_window_warped_delete_event          (GtkWidget       *widget,
                                        GdkEvent        *event,
                                        gpointer         user_data)
{
  int i=
    GPOINTER_TO_UINT(gtk_widget_get_data_top(widget,"imagenum")); 
  g_assert(i > 0);
  g_assert(sp->im_warped_widget[i] == widget);
  //gtk_widget_unref(sp->im_warped_widget[i]);
  sp->im_warped_widget[i]=NULL;
  return FALSE;
}


gboolean
on_drawingarea_warped_expose_event     (GtkWidget       *widget,
                                        GdkEventExpose  *event,
                                        gpointer         user_data)
{
  //  int w=sp->resulting_width, h=sp->resulting_height;
  int i=
    GPOINTER_TO_UINT(gtk_widget_get_data_top(widget,"imagenum")); 
  GdkPixmap *pixmap = sp->im_warped_pixmap[i];

  g_assert(i > 0);
  g_assert(pixmap != NULL);
  gdk_gc_set_clip_rectangle (widget->style->fg_gc[widget->state],
			     &event->area);
  g_assert( pixmap != NULL);
  gdk_draw_pixmap(widget->window,
		  widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
		  pixmap,
		  event->area.x, event->area.y,
		  event->area.x, event->area.y,
		  event->area.width, event->area.height);

 
  return TRUE;
}



gboolean
on_drawingarea_warped_configure_event  (GtkWidget       *widget,
                                        GdkEventConfigure *event,
                                        gpointer         user_data)
{

  int i=
    GPOINTER_TO_UINT(gtk_widget_get_data_top(widget,"imagenum")); 
  //GdkPixmap *pixmap = sp->im_pixmap_subimage[i];
  g_assert(i>0);
  
  return FALSE;
}














/*********************************************************************
**********************************************************************

 * warning dialog hook
 FIXME: I think this is not the right way to do it

**********************************************************************
**********************************************************************
**/


/* the text of the warning */
char dialogwarning_text[1001]="";

 
void
on_labelwarning_show                   (GtkWidget       *widget,
                                        gpointer         user_data)
{
  gtk_label_set_text              (GTK_LABEL(widget),
				   dialogwarning_text);
}

void
on_labelwarning_realize                (GtkWidget       *widget,
                                        gpointer         user_data)
{
  gtk_label_set_text              (GTK_LABEL(widget),
				   dialogwarning_text);
}





//GtkWidget *menu_image_num_g=NULL;

void show_warning(const char *str)
{
  strncpy(dialogwarning_text,str,1000);
  if(settings_get_value("no warnings")==0) {
    dialogwarning_g= create_dialogwarning();
    gtk_widget_show(dialogwarning_g);
  }
  else
    gdk_beep();
}

void
on_why_the_beep_1_activate             (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
 if(*dialogwarning_text) {
   dialogwarning_g= create_dialogwarning();
   gtk_widget_show(dialogwarning_g);
 }
 *dialogwarning_text=0;
}