summaryrefslogtreecommitdiff
path: root/src/xsane-front-gtk.c
blob: 3695db85740ab1b5f70307bb6c42030c37442bd7 (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
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
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
/* xsane -- a graphical (X11, gtk) scanner-oriented SANE frontend

   xsane-front-gtk.c

   Oliver Rauch <Oliver.Rauch@rauch-domain.de>
   Copyright (C) 1998-2005 Oliver Rauch
   This file is part of the XSANE package.

   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., 675 Mass Ave, Cambridge, MA 02139, USA.  */ 

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

#include "xsane.h"
#include "xsane-back-gtk.h"
#include "xsane-preferences.h"
#include "xsane-preview.h"
#include "xsane-save.h"
#include "xsane-gamma.h"
#include "xsane-setup.h"
#include <md5.h>

#ifdef HAVE_LIBPNG
#ifdef HAVE_LIBZ
#include <png.h>
#include <zlib.h>
#endif
#endif

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

/* forward declarations: */

int xsane_parse_options(char *options, char *argv[]);
void xsane_get_bounds(const SANE_Option_Descriptor *opt, double *minp, double *maxp);
double xsane_find_best_resolution(int well_known_option, double dpi);
int xsane_set_resolution(int well_known_option, double resolution);
void xsane_set_all_resolutions(void);
void xsane_define_maximum_output_size();
void xsane_close_dialog_callback(GtkWidget *widget, gpointer data);
void xsane_authorization_button_callback(GtkWidget *widget, gpointer data);
gint xsane_authorization_callback(SANE_String_Const resource,
                                  SANE_Char username[SANE_MAX_USERNAME_LEN],
                                  SANE_Char password[SANE_MAX_PASSWORD_LEN]);
void xsane_progress_cancel(GtkWidget *widget, gpointer data);
void xsane_progress_new(char *bar_text, char *info, GtkSignalFunc callback, int *cancel_data_pointer);
void xsane_progress_update(gfloat newval);
void xsane_progress_clear();
GtkWidget *xsane_vendor_pixmap_new(GdkWindow *window, GtkWidget *parent);
GtkWidget *xsane_toggle_button_new_with_pixmap(GdkWindow *window, GtkWidget *parent, const char *xpm_d[], const char *desc,
                                         int *state, void *xsane_toggle_button_callback);
GtkWidget *xsane_button_new_with_pixmap(GdkWindow *window, GtkWidget *parent, const char *xpm_d[], const char *desc,
                                        void *xsane_button_callback, gpointer data);
void xsane_option_menu_new(GtkWidget *parent, char *str_list[], const char *val, int option_number, const char *desc,
                           void *option_menu_callback, SANE_Int settable, const gchar *widget_name);
void xsane_option_menu_new_with_pixmap(GdkWindow *window, GtkBox *parent, const char *xpm_d[], const char *desc,
                                       char *str_list[], const char *val,
                                       GtkObject **data, int option,
                                       void *option_menu_callback, SANE_Int settable, const gchar *widget_name);
void xsane_range_new(GtkBox *parent, char *labeltext, const char *desc,
                     float min, float max, float quant, float page_step,
                     int digits, double *val, GtkObject **data, void *xsane_range_callback, SANE_Int settable);
void xsane_range_new_with_pixmap(GdkWindow *window, GtkBox *parent, const char *xpm_d[], const char *desc,
                                 float min, float max, float quant, float page_step, int digits,
                                 double *val, GtkObject **data, int option, void *xsane_range_callback, SANE_Int settable);
static void xsane_outputfilename_changed_callback(GtkWidget *widget, gpointer data);
void xsane_set_filename(gchar *filename);
void xsane_separator_new(GtkWidget *xsane_parent, int dist);
void xsane_vseparator_new(GtkWidget *xsane_parent, int dist);
GtkWidget *xsane_info_table_text_new(GtkWidget *table, gchar *text, int row, int colomn);
GtkWidget *xsane_info_text_new(GtkWidget *parent, gchar *text);
void xsane_refresh_dialog(void);
void xsane_update_param(void *arg);
void xsane_define_output_filename(void);
int xsane_identify_output_format(char *filename, char *filetype, char **ext);

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

int xsane_parse_options(char *options, char *argv[])
{
 int optpos = 0;
 int bufpos = 0;
 int arg    = 0;
 char buf[256];
 
  DBG(DBG_proc, "xsane_parse_options\n");
 
  while (options[optpos] != 0)
  {
    switch(options[optpos])
    {
      case ' ':
        buf[bufpos] = 0;
        argv[arg++] = strdup(buf);
        bufpos = 0;
        optpos++;
       break;
 
      case '\"':
        optpos++; /* skip " */
        while ((options[optpos] != 0) && (options[optpos] != '\"'))
        {
          buf[bufpos++] = options[optpos++];
        }
        optpos++; /* skip " */
       break;
 
      default:
        buf[bufpos++] = options[optpos++];
       break;
    }
  }
  buf[bufpos] = 0;
  argv[arg++] = strdup(buf);

 return arg;
}

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

void xsane_get_bounds(const SANE_Option_Descriptor *opt, double *minp, double *maxp)
{
 double min, max;
 int i;

  DBG(DBG_proc, "xsane_get_bounds\n");

  min = -INF;
  max =  INF;
  switch (opt->constraint_type)
  {
    case SANE_CONSTRAINT_RANGE:
      min = opt->constraint.range->min;
      max = opt->constraint.range->max;
     break;

    case SANE_CONSTRAINT_WORD_LIST:
      min =  INF;
      max = -INF;

      for (i = 1; i <= opt->constraint.word_list[0]; ++i)
      {
        if (opt->constraint.word_list[i] < min)
        {
          min = opt->constraint.word_list[i];
        }
        if (opt->constraint.word_list[i] > max)
        {
          max = opt->constraint.word_list[i];
        }
      }
     break;

    default:
     break;
  }

  if (opt->type == SANE_TYPE_FIXED)
  {
    if (min > -INF && min < INF)
    {
      min = SANE_UNFIX (min);
    }
    if (max > -INF && max < INF)
    {
      max = SANE_UNFIX (max);
    }
  }
  *minp = min;
  *maxp = max;
}                                                        

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

double xsane_find_best_resolution(int well_known_option, double dpi)
{
 const SANE_Option_Descriptor *opt;
 double bestdpi;

  DBG(DBG_proc, "xsane_find_best_resolution\n");

  opt = xsane_get_option_descriptor(xsane.dev, well_known_option);

  if (!opt)
  {
    return -1.0; /* option does not exits */
  }

  if (opt->constraint_type == SANE_CONSTRAINT_RANGE)
  {
   double quant=0;
   double min=0;
   double max=0;

    switch (opt->type)
    {
      case SANE_TYPE_INT:
        min   = opt->constraint.range->min;
        max   = opt->constraint.range->max;
        quant = opt->constraint.range->quant;
      break;

      case SANE_TYPE_FIXED:
        min   = SANE_UNFIX(opt->constraint.range->min);
        max   = SANE_UNFIX(opt->constraint.range->max);
        quant = SANE_UNFIX(opt->constraint.range->quant);
      break;

      default:
        DBG(DBG_error, "find_best_resolution: %s %d\n", ERR_UNKNOWN_TYPE, opt->type);
    }

    bestdpi = dpi;

    if (quant != 0) /* make sure selected value fits into quantisation */
    {
     int factor;
     double diff;

      factor = (int) (dpi - min) / quant;

      diff = dpi - min - factor * quant;
      bestdpi = min + factor * quant;

      if (diff >quant/2.0)
      {
        bestdpi += quant;
      }
    }

    if (bestdpi < min)
    {
      bestdpi = min;
    }

    if (bestdpi > max)
    {
      bestdpi = max;
    }
  }
  else if (opt->constraint_type == SANE_CONSTRAINT_WORD_LIST)
  {
   SANE_Word diff;
   SANE_Word val;
   int items;
   int i;

    items = opt->constraint.word_list[0];

    bestdpi = opt->constraint.word_list[1];
    if (opt->type == SANE_TYPE_FIXED)
    {
      bestdpi = SANE_UNFIX(bestdpi);
    }

    diff = abs(bestdpi - dpi);

    for (i=1; i<=items; i++)
    {
      val = opt->constraint.word_list[i];
      if (opt->type == SANE_TYPE_FIXED)
      {
        val = SANE_UNFIX(val);
      }

      if (abs(val - dpi) < diff)
      {
        diff = abs(val - dpi);
        bestdpi = val;
      }
    }
  }
  else
  {
    DBG(DBG_error, "find_best_resolution: %s %d\n", ERR_UNKNOWN_CONSTRAINT_TYPE, opt->constraint_type);
    return -1; /* error */
  }

  return bestdpi;
}

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

int xsane_set_resolution(int well_known_option, double resolution)
{
 const SANE_Option_Descriptor *opt;
 double bestdpi;
 SANE_Word dpi;

  DBG(DBG_proc, "xsane_set_resolution\n");

  opt = xsane_get_option_descriptor(xsane.dev, well_known_option);

  if (!opt)
  {
    return -1; /* option does not exits */
  }

  if (!SANE_OPTION_IS_ACTIVE(opt->cap))
  {
    return -1; /* option is not active */
  }

  bestdpi = xsane_find_best_resolution(well_known_option, resolution);

  if (bestdpi < 0)
  {
     DBG(DBG_error, "set_resolution: %s\n", ERR_FAILED_SET_RESOLUTION);
    return -1;
  }

  switch (opt->type)
  {
    case SANE_TYPE_INT:
      dpi = bestdpi;
    break;

    case SANE_TYPE_FIXED:
      dpi = SANE_FIX(bestdpi);
    break;

    default:
     DBG(DBG_error, "set_resolution: %s %d\n", ERR_UNKNOWN_TYPE, opt->type);
    return 1; /* error */
  }

  /* it makes problems to call xsane_back_gtk_set_option. This would allow a */
  /* panel_rebuild that can mess up a lot at this place*/
  xsane_control_option(xsane.dev, well_known_option, SANE_ACTION_SET_VALUE, &dpi, 0);

 return 0; /* everything is ok */
}

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

void xsane_set_all_resolutions(void)
{
 int printer_resolution;

  DBG(DBG_proc, "xsane_set_all_resolutions\n");

  if (xsane_set_resolution(xsane.well_known.dpi_y, xsane.resolution_y)) /* set y resolution if possible */
  {
    xsane_set_resolution(xsane.well_known.dpi, xsane.resolution); /* set common resolution if necessary */
    xsane.resolution_x = xsane.resolution;
    xsane.resolution_y = xsane.resolution;
  }
  else
  {
    xsane_set_resolution(xsane.well_known.dpi_x, xsane.resolution_x); /* set x resolution if possible */
  }

  switch (xsane.param.format)
  {
    case SANE_FRAME_GRAY:
      if (xsane.param.depth == 1)
      {
        printer_resolution = preferences.printer[preferences.printernr]->lineart_resolution;
      }
      else
      {
        printer_resolution = preferences.printer[preferences.printernr]->grayscale_resolution;
      }
     break;

    case SANE_FRAME_RGB:
    case SANE_FRAME_RED:
    case SANE_FRAME_GREEN:
    case SANE_FRAME_BLUE:
    default:
      printer_resolution = preferences.printer[preferences.printernr]->color_resolution;
     break;
  }

  xsane.zoom   = xsane.resolution   / printer_resolution;
  xsane.zoom_x = xsane.resolution_x / printer_resolution;
  xsane.zoom_y = xsane.resolution_y / printer_resolution; 
}

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

void xsane_define_maximum_output_size()
{
 const SANE_Option_Descriptor *opt;

  DBG(DBG_proc, "xsane_define_maximum_output_size\n");

  opt = xsane_get_option_descriptor(xsane.dev, xsane.well_known.coord[0]);

  if ( (opt) && (opt->unit== SANE_UNIT_MM) )
  {
    switch(xsane.xsane_mode)
    {
      case XSANE_SAVE:

        xsane_define_output_filename();
        xsane.xsane_output_format = xsane_identify_output_format(xsane.output_filename, preferences.filetype, 0);

        preview_set_maximum_output_size(xsane.preview, INF, INF, 0);
       break;

      case XSANE_VIEWER:
        preview_set_maximum_output_size(xsane.preview, INF, INF, 0);
       break;

      case XSANE_COPY:
        if (preferences.paper_orientation >= 8) /* rotate: landscape */
        {
          preview_set_maximum_output_size(xsane.preview,
                                          preferences.printer[preferences.printernr]->height / xsane.zoom_y,
                                          preferences.printer[preferences.printernr]->width  / xsane.zoom_x,
                                          preferences.paper_orientation);
        }
        else /* do not rotate: portrait */
        {
          preview_set_maximum_output_size(xsane.preview,
                                          preferences.printer[preferences.printernr]->width  / xsane.zoom_x,
                                          preferences.printer[preferences.printernr]->height / xsane.zoom_y,
                                          preferences.paper_orientation);
        }
       break;

      case XSANE_FAX:
        preview_set_maximum_output_size(xsane.preview, preferences.fax_width, preferences.fax_height, 0);
       break;

      case XSANE_MAIL:
        preview_set_maximum_output_size(xsane.preview, INF, INF, 0);
       break;

      default:
        preview_set_maximum_output_size(xsane.preview, INF, INF, 0);
    }
  }
  else
  {
    preview_set_maximum_output_size(xsane.preview, INF, INF, 0);
  }
}

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

void xsane_close_dialog_callback(GtkWidget *widget, gpointer data)
{
 GtkWidget *dialog_widget = data;

  DBG(DBG_proc, "xsane_close_dialog_callback\n");

  gtk_widget_destroy(dialog_widget);
}

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

int authorization_flag;

void xsane_authorization_button_callback(GtkWidget *widget, gpointer data)
{
  DBG(DBG_proc, "xsane_authorization_button_callback\n");

  authorization_flag = (long) data;
}

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

gint xsane_authorization_callback(SANE_String_Const resource,
                                  SANE_Char username[SANE_MAX_USERNAME_LEN],
                                  SANE_Char password[SANE_MAX_PASSWORD_LEN])
{
 GtkWidget *authorize_dialog, *vbox, *hbox, *button, *label;
 GtkWidget *username_widget, *password_widget;
 char buf[SANE_MAX_PASSWORD_LEN+SANE_MAX_USERNAME_LEN+128];
 const gchar *input;
 char *resource_string;
 int len;
 int resource_len;
 int secure_password_transmission = 0;
 char password_filename[PATH_MAX];
 struct stat password_stat;
 FILE *password_file;
 unsigned char md5digest[16];
 int query_user = 1;

  DBG(DBG_proc, "xsane_authorization_callback\n");

  if (strstr(resource, "$MD5$") != NULL) /* secure password authorisation */
  {
    DBG(DBG_info, "xsane_authorization_callback: secure (MD5) password transmission requested\n");
    secure_password_transmission = 1;
    resource_len = strstr(resource, "$MD5$") - resource;
  }
  else
  {
    DBG(DBG_info, "xsane_authorization_callback: insecure password transmission requested\n");
    resource_len = strlen(resource);
  }
  resource_string = alloca(resource_len+1);
  snprintf(resource_string, resource_len+1, "%s", resource);

  xsane_back_gtk_make_path(sizeof(password_filename), password_filename, NULL, NULL, "pass", NULL, NULL, XSANE_PATH_LOCAL_SANE);

  /* if password transmission is secure and file ~/.sane/pass exists and it's permissions are x00 then
     try to read username and pasword for resource from that file */
  if ((stat(password_filename, &password_stat) == 0) && (secure_password_transmission))
  {
    if ((password_stat.st_mode & 63) != 0) /* 63 = 0x077 */
    {
      snprintf(buf, sizeof(buf), ERR_PASSWORD_FILE_INSECURE, password_filename);
      xsane_back_gtk_error(buf, TRUE);
    }
    else /* ok, password file has secure permissions, we can use it */
    {
      password_file = fopen(password_filename, "r");

      if (password_file)
      {
        DBG(DBG_info, "xsane authorization: opened %s as password file\n", password_filename);
        /* file format: "username:password:resource" */
        while (fgets(buf, sizeof(buf), password_file))
        {
         char *stored_username;
         char *stored_password;
         char *stored_resource;
         char *marker;

          marker = strrchr(buf, '\n');
          if (marker)
          {
            *marker = 0; /* remove \n at end of read line */
          }

          marker = strrchr(buf, '\r');
          if (marker)
          {
            *marker = 0; /* remove \r at end of read line (eg for windows file) */
          }

          marker = strchr(buf, ':');
          if (marker)
          {
            stored_username = buf;
            *marker = 0; /* set \0 to end of stored_username */
            stored_password = marker + 1;

            marker = strchr(stored_password, ':');
            if (marker)
            {
              *marker = 0; /* set \0 to end of stored_password */
              stored_resource = marker + 1;

              if (strcmp(stored_resource, resource_string) == 0) /* password file resource equals requested resource */
              {
                strcpy(username, stored_username);
                strcpy(password, stored_password);
                query_user = 0;
              }
            }
          }
        }

        fclose(password_file);
      }
      else
      {
        DBG(DBG_info, "xsane authorization: could not open existing password file %s\n", password_filename);
      }
    }
  }
  else
  {
    DBG(DBG_info, "xsane authorization: password file %s does not exist\n", password_filename);
  }

  if (query_user)
  {
    authorize_dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(authorize_dialog), GTK_WIN_POS_CENTER);
    gtk_window_set_resizable(GTK_WINDOW(authorize_dialog), FALSE);
    g_signal_connect(GTK_OBJECT(authorize_dialog), "delete_event", GTK_SIGNAL_FUNC(xsane_authorization_button_callback), (void *) -1); /* -1 = cancel */
    snprintf(buf, sizeof(buf), "%s %s", xsane.prog_name, WINDOW_AUTHORIZE);
    gtk_window_set_title(GTK_WINDOW(authorize_dialog), buf);
    xsane_set_window_icon(authorize_dialog, 0);

    vbox = gtk_vbox_new(/* not homogeneous */ FALSE, 10); /* y-space between all box items */
    gtk_container_add(GTK_CONTAINER(authorize_dialog), vbox);
    gtk_widget_show(vbox);

    /* print resourece string */
    snprintf(buf, sizeof(buf), "\n\n%s %s", TEXT_AUTHORIZATION_REQ, resource_string);
    label = gtk_label_new(buf);
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0); /* y-space around authorization text */
    gtk_widget_show(label);

    /* print securety of password transmission */
    if (secure_password_transmission)
    {
      snprintf(buf, sizeof(buf), "%s\n", TEXT_AUTHORIZATION_SECURE);
    }
    else
    {
      snprintf(buf, sizeof(buf), "%s\n", TEXT_AUTHORIZATION_INSECURE);
    }
    label = gtk_label_new(buf);
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0); /* y-space around authorization text */
    gtk_widget_show(label);

    /* ask for username */
    hbox = gtk_hbox_new(FALSE, 10); /* x-space between label and input filed */
    gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); /* y-space around inner items */

    label = gtk_label_new(TEXT_USERNAME);
    gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 10); /* x-space around label */
    gtk_widget_show(label);

    username_widget = gtk_entry_new_with_max_length(SANE_MAX_USERNAME_LEN-1);
    gtk_widget_set_size_request(username_widget, 250, -1);
    gtk_entry_set_text(GTK_ENTRY(username_widget), "");
    gtk_box_pack_end(GTK_BOX(hbox), username_widget, FALSE, FALSE, 10); /* x-space around input filed */
    gtk_widget_show(username_widget);
    gtk_widget_show(hbox);


    /* ask for password */
    hbox = gtk_hbox_new(FALSE, 10); /* x-space between label and input filed */
    gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); /* y-space around inner items */

    label = gtk_label_new(TEXT_PASSWORD);
    gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 10); /* x-space around label */
    gtk_widget_show(label);

    password_widget = gtk_entry_new_with_max_length(SANE_MAX_PASSWORD_LEN-1);
    gtk_entry_set_visibility(GTK_ENTRY(password_widget), FALSE); /* make entered text invisible */
    gtk_widget_set_size_request(password_widget, 250, -1);
    gtk_entry_set_text(GTK_ENTRY(password_widget), "");
    gtk_box_pack_end(GTK_BOX(hbox), password_widget, FALSE, FALSE, 10); /* x-space around input filed */
    gtk_widget_show(password_widget);
    gtk_widget_show(hbox);

    /* buttons */
    hbox = gtk_hbox_new(TRUE, 10); /* x-space between buttons */
    gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 10);  /* y-space around buttons */

#ifdef HAVE_GTK2
    button = gtk_button_new_from_stock(GTK_STOCK_OK);
#else
    button = gtk_button_new_with_label(BUTTON_OK);
#endif
    GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
    g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(xsane_authorization_button_callback), (void *) 1);
    gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 10); /* x-space around OK-button */
    gtk_widget_grab_default(button);
    gtk_widget_show(button);

#ifdef HAVE_GTK2
    button = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
#else
    button = gtk_button_new_with_label(BUTTON_CANCEL);
#endif
    g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(xsane_authorization_button_callback), (void *) -1);
    gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 10); /* x-space around cancel-button */
    gtk_widget_show(button);

    gtk_widget_show(hbox);

    gtk_widget_show(authorize_dialog);


    username[0]=0;
    password[0]=0;

    authorization_flag = 0;

    /* wait for ok or cancel */
    while (authorization_flag == 0)
    {
      gtk_main_iteration();
    }

    if (authorization_flag == 1) /* 1=ok, -1=cancel */
    {
      input = gtk_entry_get_text(GTK_ENTRY(username_widget));
      len = strlen(input);
      memcpy(username, input, len);
      username[len] = 0;

      input = gtk_entry_get_text(GTK_ENTRY(password_widget));
      len = strlen(input);
      memcpy(password, input, len);
      password[len] = 0;
    }
    gtk_widget_destroy(authorize_dialog);
  }

  if (secure_password_transmission)
  {
    DBG(DBG_info, "xsane authorization: calculating md5digest of password\n");

    snprintf(buf, sizeof(buf), "%s%s", (strstr(resource, "$MD5$")) + 5, password); /* random string from backend + password */
    md5_buffer(buf, strlen(buf), md5digest); /* calculate md5digest */
#if 0 /* makes problems with WIN32 */
    memset(password, 0, SANE_MAX_PASSWORD_LEN); /* clear password */
#endif

    sprintf(password, "$MD5$%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            md5digest[0],  md5digest[1],  md5digest[2],  md5digest[3],
            md5digest[4],  md5digest[5],  md5digest[6],  md5digest[7],
            md5digest[8],  md5digest[9],  md5digest[10], md5digest[11],
            md5digest[12], md5digest[13], md5digest[14], md5digest[15]);
  }         

  return TRUE;
}

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

void xsane_progress_cancel(GtkWidget *widget, gpointer data)
{
 void *cancel_data_pointer;
 GtkFunction callback = (GtkFunction) data;

  DBG(DBG_proc, "xsane_progress_cancel\n");

  cancel_data_pointer = gtk_object_get_data(GTK_OBJECT(widget), "progress-cancel-data-pointer");

  (callback)(cancel_data_pointer);
}

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

void xsane_progress_new(char *bar_text, char *info, GtkSignalFunc callback, int *cancel_data_pointer)
{
  DBG(DBG_proc, "xsane_progress_new\n");

  gtk_label_set(GTK_LABEL(xsane.info_label), info);
  gtk_progress_set_format_string(GTK_PROGRESS(xsane.progress_bar), bar_text); 
  gtk_progress_bar_update(GTK_PROGRESS_BAR(xsane.progress_bar), 0.0);
  gtk_widget_set_sensitive(GTK_WIDGET(xsane.cancel_button), TRUE);
  gtk_object_set_data(GTK_OBJECT(xsane.cancel_button), "progress-cancel-data-pointer", cancel_data_pointer);
  g_signal_connect(GTK_OBJECT(xsane.cancel_button), "clicked", (GtkSignalFunc) xsane_progress_cancel, callback);
  xsane.cancel_callback = callback;
}

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

void xsane_progress_clear()
{
  DBG(DBG_proc, "xsane_progress_clear\n");

  /* do not call xsane_update_param() here because it overrites the good scanning parameters with bad guessed ones */
  gtk_progress_set_format_string(GTK_PROGRESS(xsane.progress_bar), ""); 
  gtk_progress_bar_update(GTK_PROGRESS_BAR(xsane.progress_bar), 0.0);
  gtk_widget_set_sensitive(GTK_WIDGET(xsane.cancel_button), FALSE);

  if (xsane.cancel_callback)
  {
    g_signal_handlers_disconnect_by_func(GTK_OBJECT(xsane.cancel_button), (GtkSignalFunc) xsane_progress_cancel, xsane.cancel_callback);
    xsane.cancel_callback = 0;
  }
}

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

void xsane_progress_update(gfloat newval)
{
  DBG(DBG_proc, "xsane_progress_update\n");

  if (newval < 0.0)
  {
    newval = 0.0;
  }

  if (newval > 1.0)
  {
    newval = 1.0;
  }

  gtk_progress_bar_update(GTK_PROGRESS_BAR(xsane.progress_bar), newval);
}

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

/* add the scanner vendorīs logo if available */
/* when the logo is not available use the xsane logo */
GtkWidget *xsane_vendor_pixmap_new(GdkWindow *window, GtkWidget *parent)
{
 char filename[PATH_MAX];
 GtkWidget *hbox, *vbox;
 GtkWidget *pixmapwidget = NULL;
 GdkBitmap *mask = NULL;
 GdkPixmap *pixmap = NULL;

  if (xsane.devlist[xsane.selected_dev]->vendor)
  {
    xsane_back_gtk_make_path(sizeof(filename), filename, "xsane", NULL, NULL, xsane.devlist[xsane.selected_dev]->vendor, "-logo.xpm", XSANE_PATH_SYSTEM);
    pixmap = gdk_pixmap_create_from_xpm(window, &mask, xsane.bg_trans, filename);
  }

  if (!pixmap) /* vendor logo not available, use backend logo */
  {
    xsane_back_gtk_make_path(sizeof(filename), filename, "xsane", NULL, NULL, xsane.backend, "-logo.xpm", XSANE_PATH_SYSTEM);
    pixmap = gdk_pixmap_create_from_xpm(window, &mask, xsane.bg_trans, filename);
  }

  if (!pixmap) /* backend logo not available, use xsane logo */
  {
    xsane_back_gtk_make_path(sizeof(filename), filename, "xsane", NULL, NULL, "sane-xsane", "-logo.xpm", XSANE_PATH_SYSTEM);
    pixmap = gdk_pixmap_create_from_xpm(window, &mask, xsane.bg_trans, filename);
  }

  if (pixmap) /* ok, we have a pixmap, so letīs show it */
  {
    vbox = gtk_vbox_new(FALSE, 0);
    gtk_box_pack_end(GTK_BOX(parent), vbox, FALSE, FALSE, 0);
    gtk_widget_show(vbox);   

    hbox = gtk_hbox_new(TRUE /* homogeneous */ , 2);
    gtk_container_add(GTK_CONTAINER(vbox), hbox);
    gtk_container_set_border_width(GTK_CONTAINER(hbox), 0);
    gtk_widget_show(hbox);

    pixmapwidget = gtk_image_new_from_pixmap(pixmap, mask); /* now add the pixmap */
    gtk_box_pack_start(GTK_BOX(hbox), pixmapwidget, FALSE /* expand */, FALSE /* fill */, 2);
    gdk_drawable_unref(pixmap);
    gtk_widget_show(pixmapwidget);
  }

 return pixmapwidget;
}

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

GtkWidget *xsane_toggle_button_new_with_pixmap(GdkWindow *window, GtkWidget *parent, const char *xpm_d[], const char *desc,
                                         int *state, void *xsane_toggle_button_callback)
{
 GtkWidget *button;
 GtkWidget *pixmapwidget;
 GdkBitmap *mask;
 GdkPixmap *pixmap;

  DBG(DBG_proc, "xsane_toggle_button_new_with_pixmap\n");

  button = gtk_toggle_button_new();
  xsane_back_gtk_set_tooltip(xsane.tooltips, button, desc);

  pixmap = gdk_pixmap_create_from_xpm_d(window, &mask, xsane.bg_trans, (gchar **) xpm_d);
  pixmapwidget = gtk_image_new_from_pixmap(pixmap, mask);
  gtk_container_add(GTK_CONTAINER(button), pixmapwidget);
  gtk_widget_show(pixmapwidget);
  gdk_drawable_unref(pixmap);

  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), *state);
  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_toggle_button_callback, (GtkObject *) state);
  gtk_box_pack_start(GTK_BOX(parent), button, FALSE, FALSE, 0);
  gtk_widget_show(button);

 return button;
}

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

GtkWidget *xsane_button_new_with_pixmap(GdkWindow *window, GtkWidget *parent, const char *xpm_d[], const char *desc,
                                  void *xsane_button_callback, gpointer data)
{
 GtkWidget *button;
 GtkWidget *pixmapwidget;
 GdkBitmap *mask;
 GdkPixmap *pixmap;

  DBG(DBG_proc, "xsane_button_new_with_pixmap\n");

  button = gtk_button_new();
  xsane_back_gtk_set_tooltip(xsane.tooltips, button, desc);

  pixmap = gdk_pixmap_create_from_xpm_d(window, &mask, xsane.bg_trans, (gchar **) xpm_d);
  pixmapwidget = gtk_image_new_from_pixmap(pixmap, mask);
  gtk_container_add(GTK_CONTAINER(button), pixmapwidget);
  gtk_widget_show(pixmapwidget);
  gdk_drawable_unref(pixmap);

  if (xsane_button_callback)
  {
    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_button_callback, data);
  }
  gtk_box_pack_start(GTK_BOX(parent), button, FALSE, FALSE, 0);
  gtk_widget_show(button);

  return(button);
}

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

static int xsane_option_menu_lookup(MenuItem menu_items[], const char *string)
{
 int i;

  DBG(DBG_proc, "xsane_option_menu_lookup\n");

  for (i = 0; (menu_items[i].label) && strcmp(menu_items[i].label, string); ++i)
  {
  }

 return i;
}

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

static void xsane_option_menu_callback(GtkWidget *widget, gpointer data)
{
 MenuItem *menu_item = data;
 DialogElement *elem = menu_item->elem;
 const SANE_Option_Descriptor *opt;
 int opt_num;
 double dval;
 SANE_Word val;
 void *valp = &val;

  DBG(DBG_proc, "xsane_option_menu_callback\n");

  opt_num = elem - xsane.element;
  opt = xsane_get_option_descriptor(xsane.dev, opt_num);
  switch (opt->type)
  {
    case SANE_TYPE_INT:
      sscanf(menu_item->label, "%d", &val);
     break;

    case SANE_TYPE_FIXED:
      sscanf(menu_item->label, "%lg", &dval);
      val = SANE_FIX(dval);
     break;

    case SANE_TYPE_STRING:
      valp = menu_item->label;
     break;

    default:
      DBG(DBG_error, "xsane_option_menu_callback: %s %d\n", ERR_UNKNOWN_TYPE, opt->type);
     break;
  }
  xsane_back_gtk_set_option(opt_num, valp, SANE_ACTION_SET_VALUE);
}

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

void xsane_option_menu_new(GtkWidget *parent, char *str_list[], const char *val, int option_number, const char *desc,
                           void *option_menu_callback, SANE_Int settable, const gchar *widget_name)
{
 GtkWidget *option_menu, *menu, *item;
 MenuItem *menu_items;
 DialogElement *elem;
 int i, num_items;

  DBG(DBG_proc, "xsane_option_menu_new\n");

  elem = xsane.element + option_number;

  for (num_items = 0; str_list[num_items]; ++num_items)
  {
  }

  menu_items = malloc((num_items + 1) * sizeof(menu_items[0]));

  menu = gtk_menu_new();
  if (widget_name)
  {
    gtk_widget_set_name(menu, widget_name);
  }

  for (i = 0; i < num_items; ++i)
  {
    item = gtk_menu_item_new_with_label(_BGT(str_list[i]));
    gtk_container_add(GTK_CONTAINER(menu), item);

    if (option_menu_callback)
    {
      g_signal_connect(GTK_OBJECT(item), "activate", (GtkSignalFunc) option_menu_callback, menu_items + i);
    }
    else
    {
      g_signal_connect(GTK_OBJECT(item), "activate", (GtkSignalFunc) xsane_option_menu_callback, menu_items + i);
    }

    gtk_widget_show(item);

    menu_items[i].label = str_list[i];
    menu_items[i].elem  = elem;
    menu_items[i].index = i;
  }

  /* add empty element as end of list marker */
  menu_items[i].label = NULL;
  menu_items[i].elem  = NULL;
  menu_items[i].index = 0;

  option_menu = gtk_option_menu_new();
  xsane_back_gtk_set_tooltip(xsane.tooltips, option_menu, desc);
  gtk_box_pack_end(GTK_BOX(parent), option_menu, FALSE, FALSE, 2);
  gtk_option_menu_set_menu(GTK_OPTION_MENU(option_menu), menu);
  gtk_option_menu_set_history(GTK_OPTION_MENU(option_menu), xsane_option_menu_lookup(menu_items, val));

  gtk_widget_show(option_menu);

  gtk_widget_set_sensitive(option_menu, settable);

  elem->widget  = option_menu;
  elem->menu_size = num_items;
  elem->menu = menu_items;
}

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

void xsane_option_menu_new_with_pixmap(GdkWindow *window, GtkBox *parent, const char *xpm_d[], const char *desc,
                                       char *str_list[], const char *val,
                                       GtkObject **data, int option,
                                       void *option_menu_callback, SANE_Int settable, const gchar *widget_name)
{
 GtkWidget *hbox;
 GtkWidget *pixmapwidget;
 GdkBitmap *mask;
 GdkPixmap *pixmap;

  DBG(DBG_proc, "xsane_option_menu_new_with_pixmap\n");

  hbox = gtk_hbox_new(FALSE, 5);
  gtk_box_pack_start(parent, hbox, FALSE, FALSE, 0);

  pixmap = gdk_pixmap_create_from_xpm_d(window, &mask, xsane.bg_trans, (gchar **) xpm_d);
  pixmapwidget = gtk_image_new_from_pixmap(pixmap, mask);
  gtk_box_pack_start(GTK_BOX(hbox), pixmapwidget, FALSE, FALSE, 2);
  gtk_widget_show(pixmapwidget);

  xsane_option_menu_new(hbox, str_list, val, option, desc, option_menu_callback, settable, widget_name);
  gtk_widget_show(hbox);
}

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

static void xsane_range_display_value_right_callback(GtkAdjustment *adjust, gpointer data)
{
 gchar buf[256];
 int digits = (int) data;
 GtkLabel *label;

  snprintf(buf, sizeof(buf), "%1.*f", digits, adjust->value);
  label = (GtkLabel *) gtk_object_get_data(GTK_OBJECT(adjust), "value-label"); 
  gtk_label_set_text(label, buf);
}

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

void xsane_range_new(GtkBox *parent, char *labeltext, const char *desc,
                     float min, float max, float quant, float page_step,
                     int digits, double *val, GtkObject **data, void *xsane_range_callback, SANE_Int settable)
{
 GtkWidget *hbox;
 GtkWidget *label;
 GtkWidget *slider = NULL;
 GtkWidget *spinbutton;
 GtkWidget *value_label;

  DBG(DBG_proc, "xsane_range_new\n");

  hbox = gtk_hbox_new(FALSE, 5);
  gtk_box_pack_start(parent, hbox, FALSE, FALSE, 2);

  label = gtk_label_new(labeltext);
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 1);

  *data = gtk_adjustment_new(*val, min, max, quant, page_step, (max-min) * 1e-30);
  /* 1e-30 => hscrollbar has an unwanted side effect: the maximum is not the maximum */
  /* of the given range, it is reduced by the page_size, so it has to be very small */

  /* value label */
  if (preferences.show_range_mode & 8)
  {
    value_label = gtk_label_new("");
    gtk_widget_set_size_request(value_label, 35, -1);
    gtk_box_pack_end(GTK_BOX(hbox), value_label, FALSE, FALSE, 1);
    
    g_signal_connect(*data, "value_changed", (GtkSignalFunc) xsane_range_display_value_right_callback, (void *) digits);
    gtk_object_set_data(GTK_OBJECT(*data), "value-label", value_label); 
    g_signal_emit_by_name(GTK_OBJECT(*data), "value_changed"); /* update value */
    gtk_widget_show(value_label);
    gtk_widget_set_sensitive(value_label, settable);  
  }

  /* spinbutton */
  if (preferences.show_range_mode & 4)
  {
    spinbutton = gtk_spin_button_new(GTK_ADJUSTMENT(*data), 0, digits);
    if (preferences.show_range_mode & 3) /* slider also visible */
    {
      gtk_widget_set_size_request(spinbutton, 60, -1);
    }
    else /* slider not visible */
    {
      gtk_widget_set_size_request(spinbutton, 100, -1);
    }
    xsane_back_gtk_set_tooltip(xsane.tooltips, spinbutton, desc);
    gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(spinbutton), FALSE);
    gtk_box_pack_end(GTK_BOX(hbox), spinbutton, FALSE, FALSE, 5); /* make spinbutton not sizeable */
    gtk_widget_show(spinbutton);
    gtk_widget_set_sensitive(spinbutton, settable);  
  }

  /* slider */
  if (preferences.show_range_mode & 3)
  {
    if (preferences.show_range_mode & 1) /* bit 0 (val 1) : scale */
    {
      slider = gtk_hscale_new(GTK_ADJUSTMENT(*data));
      gtk_scale_set_draw_value(GTK_SCALE(slider), FALSE);
      gtk_scale_set_digits(GTK_SCALE(slider), digits);
    }
    else /* bit 1 (val 2) : scrollbar */
    {
      slider = gtk_hscrollbar_new(GTK_ADJUSTMENT(*data));
    }
    xsane_back_gtk_set_tooltip(xsane.tooltips, slider, desc);
    gtk_widget_set_size_request(slider, 180, -1);
    /* GTK_UPDATE_CONTINUOUS, GTK_UPDATE_DISCONTINUOUS, GTK_UPDATE_DELAYED */
    gtk_range_set_update_policy(GTK_RANGE(slider), preferences.gtk_update_policy);
    gtk_box_pack_end(GTK_BOX(hbox), slider, FALSE, FALSE, 5); /* make slider not sizeable */
    gtk_widget_show(slider);
    gtk_widget_set_sensitive(slider, settable);  
  }

  if (xsane_range_callback)
  {
    g_signal_connect(*data, "value_changed", (GtkSignalFunc) xsane_range_callback, val);
  }

  gtk_widget_show(label);
  gtk_widget_show(hbox);
}

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

void xsane_range_new_with_pixmap(GdkWindow *window, GtkBox *parent, const char *xpm_d[], const char *desc,
                                 float min, float max, float quant, float page_step,
                                 int digits, double *val, GtkObject **data, int option, void *xsane_range_callback, SANE_Int settable)
{
 GtkWidget *hbox;
 GtkWidget *slider = NULL;
 GtkWidget *spinbutton;
 GtkWidget *pixmapwidget;
 GdkBitmap *mask;
 GdkPixmap *pixmap;
 GtkWidget *value_label;

  DBG(DBG_proc, "xsane_slider_new_with_pixmap\n");

  hbox = gtk_hbox_new(FALSE, 5);
  gtk_box_pack_start(parent, hbox, FALSE, FALSE, 1);

  pixmap = gdk_pixmap_create_from_xpm_d(window, &mask, xsane.bg_trans, (gchar **) xpm_d);
  pixmapwidget = gtk_image_new_from_pixmap(pixmap, mask);
  gtk_box_pack_start(GTK_BOX(hbox), pixmapwidget, FALSE, FALSE, 2);
  gtk_widget_show(pixmapwidget);
  gdk_drawable_unref(pixmap);

  *data = gtk_adjustment_new(*val, min, max, quant, page_step, (max-min) * 1e-30);
  /* 1e-30 => hscrollbar has an unwanted side effect: the maximum is not the maximum */
  /* of the given range, it is reduced by the page_size, so it has to be very small */

  /* value label */
  if (preferences.show_range_mode & 8)
  {
    value_label = gtk_label_new("");
    gtk_widget_set_size_request(value_label, 35, -1);
    gtk_box_pack_end(GTK_BOX(hbox), value_label, FALSE, FALSE, 1);
    
    g_signal_connect(*data, "value_changed", (GtkSignalFunc) xsane_range_display_value_right_callback, (void *) digits);
    gtk_object_set_data(GTK_OBJECT(*data), "value-label", value_label); 
    g_signal_emit_by_name(GTK_OBJECT(*data), "value_changed"); /* update value */
    gtk_widget_show(value_label);
    gtk_widget_set_sensitive(value_label, settable);  
  }

  /* spinbutton */
  if (preferences.show_range_mode & 4)
  {
    spinbutton = gtk_spin_button_new(GTK_ADJUSTMENT(*data), 0, digits);
    gtk_widget_set_size_request(spinbutton, 60, -1);
    xsane_back_gtk_set_tooltip(xsane.tooltips, spinbutton, desc);
    gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(spinbutton), FALSE);
    if (preferences.show_range_mode & 3) /* slider also visible */
    {
      gtk_box_pack_end(GTK_BOX(hbox), spinbutton, FALSE, FALSE, 5); /* make spinbutton not sizeable */
    }
    else /* slider not visible */
    {
      gtk_box_pack_end(GTK_BOX(hbox), spinbutton, TRUE, TRUE, 5); /* make spinbutton sizeable */
    }
    gtk_widget_show(spinbutton);
    gtk_widget_set_sensitive(spinbutton, settable);  
  }

  /* slider */
  if (preferences.show_range_mode & 3)
  {
    if (preferences.show_range_mode & 1) /* bit 0 (val 1) : scale */
    {
      slider = gtk_hscale_new(GTK_ADJUSTMENT(*data));
      gtk_scale_set_draw_value(GTK_SCALE(slider), FALSE);
      gtk_scale_set_digits(GTK_SCALE(slider), digits);
    }
    else /* bit 1 (val 2) : scrollbar */
    {
      slider = gtk_hscrollbar_new(GTK_ADJUSTMENT(*data));
    }
    xsane_back_gtk_set_tooltip(xsane.tooltips, slider, desc);
    gtk_widget_set_size_request(slider, 170, -1);
    /* GTK_UPDATE_CONTINUOUS, GTK_UPDATE_DISCONTINUOUS, GTK_UPDATE_DELAYED */
    gtk_range_set_update_policy(GTK_RANGE(slider), preferences.gtk_update_policy);
    gtk_box_pack_end(GTK_BOX(hbox), slider, TRUE, TRUE, 5); /* make slider sizeable */
    gtk_widget_show(slider);
    gtk_widget_set_sensitive(slider, settable);  
  }

  if (xsane_range_callback)
  {
    g_signal_connect(*data, "value_changed", (GtkSignalFunc) xsane_range_callback, val);
  }

  gtk_widget_show(hbox);

  if (option)
  {
   DialogElement *elem;

    elem=xsane.element + option;
    elem->data    = *data;
    elem->widget  = slider;
  }
}

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

static void xsane_browse_filename_callback(GtkWidget *widget, gpointer data)
{
 char filename[1024];
 char windowname[256];

  DBG(DBG_proc, "xsane_browse_filename_callback\n");

  xsane_set_sensitivity(FALSE);

  if (preferences.filename) /* make sure a correct filename is defined */
  {
    strncpy(filename, preferences.filename, sizeof(filename));
    filename[sizeof(filename) - 1] = '\0';
  }
  else /* no filename given, take standard filename */
  {
    strcpy(filename, OUT_FILENAME);
  }

  snprintf(windowname, sizeof(windowname), "%s %s %s", xsane.prog_name, WINDOW_OUTPUT_FILENAME, xsane.device_text);

  umask((mode_t) preferences.directory_umask); /* define new file permissions */    
  xsane_back_gtk_get_filename(windowname, filename, sizeof(filename), filename, &preferences.filetype, TRUE, TRUE, FALSE, TRUE);
  umask(XSANE_DEFAULT_UMASK); /* define new file permissions */    

  if (preferences.filename)
  {
    free((void *) preferences.filename);
  }

  preferences.filename = strdup(filename);

  xsane_set_sensitivity(TRUE);

  xsane_back_gtk_filetype_menu_set_history(xsane.filetype_option_menu, preferences.filetype);

  /* correct length of filename counter if it is shorter than minimum length */
  xsane_update_counter_in_filename(&preferences.filename, FALSE, 0, preferences.filename_counter_len);

  xsane_set_filename(preferences.filename);

  xsane_define_maximum_output_size(); /* is necessary in postscript mode */
}

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

void xsane_set_filename(gchar *filename)
{
  g_signal_handlers_block_by_func(GTK_OBJECT(xsane.outputfilename_entry), (GtkSignalFunc) xsane_outputfilename_changed_callback, NULL);
  gtk_entry_set_text(GTK_ENTRY(xsane.outputfilename_entry), (char *) filename); /* update filename in entry */
  gtk_entry_set_position(GTK_ENTRY(xsane.outputfilename_entry), strlen(filename)); /* set cursor to right position of filename */

  g_signal_handlers_unblock_by_func(GTK_OBJECT(xsane.outputfilename_entry), (GtkSignalFunc) xsane_outputfilename_changed_callback, NULL);
}

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

static void xsane_filename_counter_step_callback(GtkWidget *widget, gpointer data)
{
  DBG(DBG_proc, "xsane_filename_counter_step_callback\n");
 
  preferences.filename_counter_step = (int) data;
}

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

static void xsane_filetype_callback(GtkWidget *widget, gpointer data)
{
 char *new_filetype = (char *) data;
 char buffer[PATH_MAX];
 char *filename;

  DBG(DBG_proc, "xsane_filetype_callback\n");

  filename = preferences.filename;

  if ((new_filetype) && (*new_filetype)) /* filetype exists and is not empty (by ext) */
  {
   char *extension;

    extension = strrchr(preferences.filename, '.');

    if ((extension) && (extension != preferences.filename))
    {
      if ( (!strcasecmp(extension, ".pnm"))  || (!strcasecmp(extension, ".raw"))
        || (!strcasecmp(extension, ".png"))  || (!strcasecmp(extension, ".ps"))
        || (!strcasecmp(extension, ".rgba"))
        || (!strcasecmp(extension, ".tiff")) || (!strcasecmp(extension, ".tif"))
        || (!strcasecmp(extension, ".text")) || (!strcasecmp(extension, ".txt"))
        || (!strcasecmp(extension, ".jpg"))  || (!strcasecmp(extension, ".jpeg"))
         ) /* remove filetype extension */
      {
        *extension = 0; /* remove extension */
      }
    }
    snprintf(buffer, sizeof(buffer), "%s%s", filename, new_filetype);
    free(filename);
    preferences.filename = strdup(buffer);
  }

  if (preferences.filetype)
  {
    free(preferences.filetype);
    preferences.filetype = NULL;
  }

  if (new_filetype)
  {
    preferences.filetype = strdup(new_filetype);
  }

  /* correct length of filename counter if it is shorter than minimum length */
  xsane_update_counter_in_filename(&preferences.filename, FALSE, 0, preferences.filename_counter_len);
  xsane_set_filename(preferences.filename);
  xsane_define_maximum_output_size(); /* is necessary in postscript mode */
}

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

static void xsane_outputfilename_changed_callback(GtkWidget *widget, gpointer data)
{
  DBG(DBG_proc, "xsane_outputfilename_changed_callback\n");

  if (preferences.filename)
  {
    free((void *) preferences.filename);
  }
  preferences.filename = strdup(gtk_entry_get_text(GTK_ENTRY(widget)));

  xsane_define_maximum_output_size(); /* is necessary in postscript mode */
}

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

void xsane_outputfilename_new(GtkWidget *vbox)
{
 GtkWidget *hbox;
 GtkWidget *text;
 GtkWidget *button;
 GtkWidget *xsane_filename_counter_step_option_menu;
 GtkWidget *xsane_filename_counter_step_menu;
 GtkWidget *xsane_filename_counter_step_item;
 GtkWidget *xsane_label;
 gchar buf[200];
 int i,j;
 int select_item = 0;

  DBG(DBG_proc, "xsane_outputfilename_new\n");

  /* first line: disk icon, filename box */

  hbox = gtk_hbox_new(FALSE, 2);
  gtk_container_set_border_width(GTK_CONTAINER(hbox), 2);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);

  button = xsane_button_new_with_pixmap(xsane.xsane_window->window, hbox, file_xpm, DESC_BROWSE_FILENAME,
                                        (GtkSignalFunc) xsane_browse_filename_callback, NULL);
  gtk_widget_add_accelerator(button, "clicked", xsane.accelerator_group, GDK_B, GDK_CONTROL_MASK, DEF_GTK_ACCEL_LOCKED);

  text = gtk_entry_new();
  gtk_widget_set_size_request(text, 80, -1); /* set minimum size */
  xsane_back_gtk_set_tooltip(xsane.tooltips, text, DESC_FILENAME);
  gtk_entry_set_max_length(GTK_ENTRY(text), 255);
  gtk_entry_set_text(GTK_ENTRY(text), (char *) preferences.filename);
  gtk_entry_set_position(GTK_ENTRY(text), strlen(preferences.filename)); /* set cursor to right position of filename */

  gtk_box_pack_start(GTK_BOX(hbox), text, TRUE, TRUE, 4);
  g_signal_connect(GTK_OBJECT(text), "changed", (GtkSignalFunc) xsane_outputfilename_changed_callback, NULL);

  xsane.outputfilename_entry = text;

  gtk_widget_show(text);
  gtk_widget_show(hbox);


  /* second line: Step, Type */

  hbox = gtk_hbox_new(FALSE, 2);
  gtk_container_set_border_width(GTK_CONTAINER(hbox), 2);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);

  /* filename counter step */
 
  xsane_label = gtk_label_new(TEXT_FILENAME_COUNTER_STEP);
  gtk_box_pack_start(GTK_BOX(hbox), xsane_label, FALSE, FALSE, 2);
  gtk_widget_show(xsane_label);
 
  xsane_filename_counter_step_option_menu = gtk_option_menu_new();
  xsane_back_gtk_set_tooltip(xsane.tooltips, xsane_filename_counter_step_option_menu, DESC_FILENAME_COUNTER_STEP);
  gtk_box_pack_start(GTK_BOX(hbox), xsane_filename_counter_step_option_menu, FALSE, FALSE, 2);
  gtk_widget_show(xsane_filename_counter_step_option_menu);
  gtk_widget_show(hbox);
 
  xsane_filename_counter_step_menu = gtk_menu_new();

  select_item = 0;
  j = -2;
  for (i=0; i < 5; i++)
  {
    snprintf(buf, sizeof(buf), "%+d", j);
    xsane_filename_counter_step_item = gtk_menu_item_new_with_label(buf);
    gtk_container_add(GTK_CONTAINER(xsane_filename_counter_step_menu), xsane_filename_counter_step_item);
    g_signal_connect(GTK_OBJECT(xsane_filename_counter_step_item), "activate", (GtkSignalFunc) xsane_filename_counter_step_callback, (void *) j);
    gtk_widget_show(xsane_filename_counter_step_item);
    if (preferences.filename_counter_step == j++)
    {
      select_item = i;
    }
  }
 
  gtk_option_menu_set_menu(GTK_OPTION_MENU(xsane_filename_counter_step_option_menu), xsane_filename_counter_step_menu);
  gtk_option_menu_set_history(GTK_OPTION_MENU(xsane_filename_counter_step_option_menu), select_item);

  xsane.filetype_option_menu = xsane_back_gtk_filetype_menu_new(preferences.filetype, (GtkSignalFunc) xsane_filetype_callback);
  gtk_box_pack_end(GTK_BOX(hbox), xsane.filetype_option_menu, FALSE, FALSE, 2);
  gtk_widget_show(xsane.filetype_option_menu);

  xsane_label = gtk_label_new(TEXT_FILETYPE); /* opposite order because of box_pack_end */
  gtk_box_pack_end(GTK_BOX(hbox), xsane_label, FALSE, FALSE, 2);
  gtk_widget_show(xsane_label);

  gtk_widget_show(text);
  gtk_widget_show(hbox);
}

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

void xsane_separator_new(GtkWidget *xsane_parent, int dist)
{
 GtkWidget *xsane_separator;

  DBG(DBG_proc, "xsane_separator_new\n");

  xsane_separator = gtk_hseparator_new();
  gtk_box_pack_start(GTK_BOX(xsane_parent), xsane_separator, FALSE, FALSE, dist);
  gtk_widget_show(xsane_separator);
}

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

void xsane_vseparator_new(GtkWidget *xsane_parent, int dist)
{
 GtkWidget *xsane_vseparator;

  DBG(DBG_proc, "xsane_vseparator_new\n");

  xsane_vseparator = gtk_vseparator_new();
  gtk_box_pack_start(GTK_BOX(xsane_parent), xsane_vseparator, FALSE, FALSE, dist);
  gtk_widget_show(xsane_vseparator);

}

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

GtkWidget *xsane_info_table_text_new(GtkWidget *table, gchar *text, int row, int colomn)
{
 GtkWidget *hbox, *label;

  DBG(DBG_proc, "xsane_info_table_text_new\n");

  hbox = gtk_hbox_new(/* homogeneous */ FALSE, 0);
  gtk_table_attach_defaults(GTK_TABLE(table), hbox, row, row+1, colomn, colomn+1);
  gtk_widget_show(hbox);

  label = gtk_label_new(text);
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 10);
  gtk_widget_show(label);

 return label;
}
/* ---------------------------------------------------------------------------------------------------------------------- */
#if 0
GtkWidget *xsane_info_text_new(GtkWidget *parent, gchar *text)
{
 GtkWidget *hbox, *label;

  DBG(DBG_proc, "xsane_info_text_new\n");

  hbox = gtk_hbox_new(/* homogeneous */ FALSE, 0);
  gtk_box_pack_start(GTK_BOX(parent), hbox, TRUE, TRUE, 5);
  gtk_widget_show(hbox);

  label = gtk_label_new(text);
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 10);
  gtk_widget_show(label);

 return label;
}
#endif
/* ---------------------------------------------------------------------------------------------------------------------- */

void xsane_refresh_dialog(void)
{
  DBG(DBG_proc, "xsane_refresh_dialog\n");

  xsane_back_gtk_refresh_dialog();
}                    

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

/* Update the info line with the latest size information and update histogram.  */
void xsane_update_param(void *arg)
{
 gchar buf[200];
 const char *unit;

  DBG(DBG_proc, "xsane_update_param\n");

  if (!xsane.info_label)
  {
    return;
  }

  if (xsane.block_update_param) /* if we change more than one value, we only want to update all once */
  {
    return;
  }

  if (xsane.preview)
  {
    preview_update_surface(xsane.preview, 0);
  }

  if (sane_get_parameters(xsane.dev, &xsane.param) == SANE_STATUS_GOOD)
  {
   float size = (float) xsane.param.bytes_per_line * (float) xsane.param.lines;
   int depth = xsane.param.depth;

    if ( (depth == 16) && (preferences.reduce_16bit_to_8bit) )
    {
      depth = 8;
      size /= 2;
    }

    unit = "B";

    if (xsane.param.format >= SANE_FRAME_RED && xsane.param.format <= SANE_FRAME_BLUE)
    {
      size *= 3.0;
      depth *= 3;
    }
    else if (xsane.param.format == SANE_FRAME_RGB)
    {
      depth *= 3;
    }
#ifdef SUPPORT_RGBA
    else if (xsane.param.format == SANE_FRAME_RGBA)
    {
      depth *= 4;
    }
#endif

    if (size >= 1024.0 * 1024.0)
    {
      size /= 1024.0 * 1024.0;
      unit = "MB";
    }
    else if (size >= 1024.0)
    {
      size /= 1024.0;
      unit = "KB";
    }
    snprintf(buf, sizeof(buf), "%d*%d*%d (%1.1f %s)", xsane.param.pixels_per_line, xsane.param.lines, depth, size, unit);

    if (xsane.param.format == SANE_FRAME_GRAY)
    {
      xsane.xsane_colors = 1;
    }
#ifdef SUPPORT_RGBA
    else if (xsane.param.format == SANE_FRAME_RGBA)
    {
      xsane.xsane_colors = 4;
    }
#endif
    else /* RGB */
    {
      xsane.xsane_colors = 3;
    }
  }
  else 
  {
    snprintf(buf, sizeof(buf), TEXT_INVALID_PARAMS);
  }

  gtk_label_set(GTK_LABEL(xsane.info_label), buf);


  if (xsane.preview->surface_unit == SANE_UNIT_MM)
  {
   double dx, dy;

    unit = xsane_back_gtk_unit_string(xsane.preview->surface_unit);

    dx = fabs(xsane.preview->selection.coordinate[2] - xsane.preview->selection.coordinate[0]) / preferences.length_unit;
    dy = fabs(xsane.preview->selection.coordinate[3] - xsane.preview->selection.coordinate[1]) / preferences.length_unit;

    snprintf(buf, sizeof(buf), "%1.2f %s x %1.2f %s", dx, unit, dy, unit);
    gtk_progress_set_format_string(GTK_PROGRESS(xsane.progress_bar), buf);
  }

  xsane_update_histogram(TRUE /* update raw */);

  preview_display_valid(xsane.preview);
}

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

void xsane_define_output_filename(void)
{
  DBG(DBG_proc, "xsane_define_output_filename\n");

  if (xsane.output_filename)
  {
    free(xsane.output_filename);
    xsane.output_filename = 0;
  }

  if (!xsane.force_filename)
  {
    xsane.output_filename = strdup(preferences.filename);
  }
  else
  {
    xsane.output_filename = strdup(xsane.external_filename);
  }
}

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

int xsane_identify_output_format(char *filename, char *filetype, char **ext)
{
 char *extension;
 int output_format=-1;

  DBG(DBG_proc, "xsane_identify_output_format\n");

  if ((filetype) && (*filetype))
  {
    extension = filetype+1; /* go to filetype, skip leading dot */
  }
  else
  {
    extension = strrchr(filename, '.');
    if (extension)
    {
      extension++; /* skip "." */
    }
  }

  output_format = XSANE_UNKNOWN;

  if (extension)
  {
    if ( (!strcasecmp(extension, "pnm")) || (!strcasecmp(extension, "ppm")) ||
         (!strcasecmp(extension, "pgm")) || (!strcasecmp(extension, "pbm")) )
    {
      if ((xsane.param.depth == 16) && (!preferences.reduce_16bit_to_8bit) )
      {
        output_format = XSANE_PNM16;
      }
      else
      {
        output_format = XSANE_PNM;
      }
    }
    else if ( (!strcasecmp(extension, "txt")) || (!strcasecmp(extension, "text")) )
    {
      output_format = XSANE_TEXT;
    }
#ifdef HAVE_LIBPNG
#ifdef HAVE_LIBZ
    else if (!strcasecmp(extension, "png"))
    {
      output_format = XSANE_PNG;
    }
#endif
#endif
#ifdef HAVE_LIBJPEG
    else if ( (!strcasecmp(extension, "jpg")) || (!strcasecmp(extension, "jpeg")) )
    {
      output_format = XSANE_JPEG;
    }
#endif
    else if (!strcasecmp(extension, "ps"))
    {
      output_format = XSANE_PS;
    }
#ifdef HAVE_LIBTIFF
    else if ( (!strcasecmp(extension, "tif")) || (!strcasecmp(extension, "tiff")) )
    {
      output_format = XSANE_TIFF;
    }
#endif
#ifdef SUPPORT_RGBA
    else if (!strcasecmp(extension, "rgba"))
    {
      output_format = XSANE_RGBA;
    }
#endif
  }

  if (ext)
  {
    if (extension)
    {
      *ext = strdup(extension);
    }
    else
    {
      *ext = 0;
    }
  }

 return output_format;
}

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

void xsane_change_working_directory(void)
{
 char filename[PATH_MAX];
 char windowname[256];

  DBG(DBG_proc, "xsane_change_working_directory\n");

  xsane_set_sensitivity(FALSE);

  sprintf(windowname, "%s %s %s", xsane.prog_name, WINDOW_CHANGE_WORKING_DIR, xsane.device_text);
  if (getcwd(filename, sizeof(filename)))
  {
    xsane_back_gtk_get_filename(windowname, filename, sizeof(filename), filename, NULL, TRUE, FALSE, TRUE, FALSE);
    if (chdir(filename))
    {
     char buf[256];

      snprintf(buf, sizeof(buf), "%s %s (%s).", ERR_CHANGE_WORKING_DIR, filename, strerror(errno));
      xsane_back_gtk_error(buf, TRUE);
      xsane_set_sensitivity(TRUE);
      return;
    }
    else
    {
      if (preferences.working_directory)
      {
        free(preferences.working_directory);
      }
      preferences.working_directory = strdup(filename);
    }
  }

  xsane_set_sensitivity(TRUE);
}

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

static int eula_accept_flag;
static GtkWidget *eula_dialog = NULL;

static gboolean xsane_eula_delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
  eula_accept_flag = (int) data;

  DBG(DBG_proc ,"xsane_eula_delete_event(%d)\n", eula_accept_flag);
  eula_dialog = NULL;

 return FALSE; /* continue with original delete even routine */
}

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

static void xsane_eula_button_callback(GtkWidget *widget, gpointer data)
{
  eula_accept_flag = (int) data;

  DBG(DBG_proc ,"xsane_eula_button_callback(%d)\n", eula_accept_flag);

  gtk_widget_destroy(eula_dialog);
  eula_dialog = NULL;
}

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

int xsane_display_eula(int ask_for_accept)
/* returns FALSE if accepted, TRUE if not accepted */
{
 GtkWidget *vbox, *hbox, *button, *label, *frame;
 GtkAccelGroup *accelerator_group;
 char buf[1024];
 char filename[PATH_MAX];
 FILE *infile;

  DBG(DBG_proc, "xsane_display_eula(%d)\n", ask_for_accept);

  if (eula_dialog) /* make sure the dialog is only opend once */
  {
    return 0;
  }

  eula_dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_size_request(eula_dialog, 550, 580);
  gtk_window_set_position(GTK_WINDOW(eula_dialog), GTK_WIN_POS_CENTER);
  gtk_window_set_resizable(GTK_WINDOW(eula_dialog), TRUE);
  g_signal_connect(GTK_OBJECT(eula_dialog), "delete_event", GTK_SIGNAL_FUNC(xsane_eula_delete_event), (void *) -1); /* -1 = cancel */
  snprintf(buf, sizeof(buf), "%s %s", xsane.prog_name, WINDOW_EULA);
  gtk_window_set_title(GTK_WINDOW(eula_dialog), buf);
  accelerator_group = gtk_accel_group_new();
  gtk_window_add_accel_group(GTK_WINDOW(eula_dialog), accelerator_group);

#if 0
  xsane_set_window_icon(eula_dialog, 0);
#endif

  /* create a frame */
  frame = gtk_frame_new(NULL);
  gtk_container_set_border_width(GTK_CONTAINER(frame), 10);
  gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_ETCHED_IN);
  gtk_container_add(GTK_CONTAINER(eula_dialog), frame);
  gtk_widget_show(frame);

  vbox = gtk_vbox_new(FALSE, 5);
  gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
  gtk_container_add(GTK_CONTAINER(frame), vbox);
  gtk_widget_show(vbox); 
#if 1
  /* this is normally done directly after gtk_window_set_title() */
  /* but gtk crashes when we would do that and select a text with the mouse */
  xsane_set_window_icon(eula_dialog, 0);
#endif

  /* display XSane copyright message */
  snprintf(buf, sizeof(buf), "XSane %s %s\n"
                             "%s %s\n"
                             "\n"
                             "%s\n"
                             "%s %s\n"
                             "%s %s\n",
                             TEXT_VERSION, XSANE_VERSION,
                             XSANE_COPYRIGHT_SIGN, XSANE_COPYRIGHT_TXT,
                             TEXT_EULA,
                             TEXT_HOMEPAGE, XSANE_HOMEPAGE,
                             TEXT_EMAIL, XSANE_EMAIL);

  label = gtk_label_new(buf);
  gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
  gtk_widget_show(label);

  /* add hbox with text and scrollbar to display the eula text */
  hbox = gtk_hbox_new(FALSE, 2);
  gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0); 
  gtk_widget_show(hbox);

#ifdef HAVE_GTK_TEXT_VIEW_H
  {
   GtkWidget *scrolled_window, *text_view;
   GtkTextBuffer *text_buffer;

    /* create a scrolled window to get a vertical scrollbar */
    scrolled_window = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
    gtk_container_add(GTK_CONTAINER(hbox), scrolled_window);
    gtk_widget_show(scrolled_window);
 
    /* create the gtk_text_view widget */
    text_view = gtk_text_view_new();
    gtk_text_view_set_editable(GTK_TEXT_VIEW(text_view), FALSE);
    gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_view), GTK_WRAP_WORD);
    gtk_container_add(GTK_CONTAINER(scrolled_window), text_view);
    gtk_widget_show(text_view); 

    /* get the text_buffer widget and insert the text from file */
    text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));

    xsane_back_gtk_make_path(sizeof(filename), filename, "xsane", 0, "xsane-eula", 0, ".txt", XSANE_PATH_SYSTEM);
    infile = fopen(filename, "r");
 
    if (infile)
    {
      char buffer[4096];
      int nchars;
   
      while (!feof(infile))
      {
        nchars = fread(buffer, 1, 4096, infile);
        gtk_text_buffer_insert_at_cursor(text_buffer, buffer, nchars);
      }
 
      fclose(infile);
    } 
    else
    {
      DBG(DBG_error0, "ERROR: eula text not found. Looks like xsane is not installed correct.\n");
     return TRUE;
    }

  }
#else /* we do not have gtk_text_view, so we use gtk_text */
  {
   GtkWidget *text, *vscrollbar;

    /* Create the gtk_text widget */
    text = gtk_text_new(NULL, NULL);
    gtk_text_set_editable(GTK_TEXT(text), FALSE); /* text is not editable */
    gtk_text_set_word_wrap(GTK_TEXT(text), TRUE); /* wrap complete words */
    gtk_box_pack_start(GTK_BOX(hbox), text, TRUE, TRUE, 0); 
    gtk_widget_show(text);
 
    /* Add a vertical scrollbar to the GtkText widget */
    vscrollbar = gtk_vscrollbar_new(GTK_TEXT(text)->vadj);
    gtk_box_pack_start(GTK_BOX(hbox), vscrollbar, FALSE, FALSE, 0); 
    gtk_widget_show(vscrollbar);

    /* Freeze the text widget, ready for multiple updates */ 
    gtk_text_freeze(GTK_TEXT(text)); 

    /* Load the file text.c into the text window */
    xsane_back_gtk_make_path(sizeof(filename), filename, "xsane", 0, "xsane-eula", 0, ".txt", XSANE_PATH_SYSTEM);
    infile = fopen(filename, "r");
 
    if (infile)
    {
      char buffer[4096];
      int nchars;
   
      while (!feof(infile))
      {
        nchars = fread(buffer, 1, 4096, infile);
        gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL, buffer, nchars);
      }
 
      fclose(infile);
    } 
    else
    {
      DBG(DBG_error0, "ERROR: eula text not found. Looks like xsane is not installed correct.\n");
     return TRUE;
    }

    /* Thaw the text widget, allowing the updates to become visible */
    gtk_text_thaw(GTK_TEXT(text)); 
  }
#endif



  hbox = gtk_hbox_new(FALSE, 2);
  gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); 

  if (ask_for_accept) /* show accept + not accept buttons */
  {
    button = gtk_button_new_with_label(BUTTON_ACCEPT);
    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_eula_button_callback, (void *) 0 /* accept */);
    gtk_container_add(GTK_CONTAINER(hbox), button);
    gtk_widget_show(button);

    button = gtk_button_new_with_label(BUTTON_NOT_ACCEPT);
    gtk_widget_add_accelerator(button, "clicked", accelerator_group, GDK_Escape, 0, DEF_GTK_ACCEL_LOCKED);
    GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_eula_button_callback, (void *) 1 /* not accept */);
    gtk_container_add(GTK_CONTAINER(hbox), button);
    gtk_widget_grab_default(button);
    gtk_widget_show(button);
  }
  else /* show close button */
  {
#ifdef HAVE_GTK2
    button = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
#else
    button = gtk_button_new_with_label(BUTTON_CLOSE);
#endif
    gtk_widget_add_accelerator(button, "clicked", accelerator_group, GDK_Escape, 0, DEF_GTK_ACCEL_LOCKED);
    GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_eula_button_callback, (void *) 0 /* ok = accept */);
    gtk_container_add(GTK_CONTAINER(hbox), button);
    gtk_widget_grab_default(button);
    gtk_widget_show(button);
  }

  gtk_widget_show(hbox);
  gtk_widget_show(vbox);
  gtk_widget_show(eula_dialog);

  if (ask_for_accept == 0) /* do not ask for accept */
  {
    return 0;
  }
  
  eula_accept_flag = -255;

  while(eula_accept_flag == -255)
  {
    gtk_main_iteration(); /* allow gtk to react to user action */
  }

  while (gtk_events_pending())
  {
    gtk_main_iteration();
  }
 
 return eula_accept_flag;
}

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

static GtkWidget *license_dialog = NULL;

static void xsane_close_license_dialog_callback(GtkWidget *widget, gpointer data)
{
  DBG(DBG_proc ,"xsane_close_license_dialog_callback\n");

  gtk_widget_destroy(license_dialog);
  license_dialog = NULL;
}

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

static gboolean xsane_license_dialog_delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
  DBG(DBG_proc ,"xsane_license_dialog_delete_event\n");

  license_dialog = NULL;

 return FALSE; /* continue with original delete even routine */
}

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

void xsane_display_gpl(void)
{
 GtkWidget *vbox, *hbox, *button, *label, *frame;
 GtkAccelGroup *accelerator_group;
 char buf[1024];
 char filename[PATH_MAX];
 FILE *infile;

  if (license_dialog) /* make sure the dialog is only opend once */
  {
    return;
  }

  license_dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_size_request(license_dialog, 550, 580);
  gtk_window_set_position(GTK_WINDOW(license_dialog), GTK_WIN_POS_CENTER);
  gtk_window_set_resizable(GTK_WINDOW(license_dialog), TRUE);
  g_signal_connect(GTK_OBJECT(license_dialog), "delete_event", GTK_SIGNAL_FUNC(xsane_license_dialog_delete_event), NULL);
  snprintf(buf, sizeof(buf), "%s: %s", xsane.prog_name, WINDOW_GPL);
  gtk_window_set_title(GTK_WINDOW(license_dialog), buf);
  accelerator_group = gtk_accel_group_new();
  gtk_window_add_accel_group(GTK_WINDOW(license_dialog), accelerator_group);

#if 0
  xsane_set_window_icon(license_dialog, 0);
#endif

  /* create a frame */
  frame = gtk_frame_new(NULL);
  gtk_container_set_border_width(GTK_CONTAINER(frame), 10);
  gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_ETCHED_IN);
  gtk_container_add(GTK_CONTAINER(license_dialog), frame);
  gtk_widget_show(frame);

  vbox = gtk_vbox_new(FALSE, 5);
  gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
  gtk_container_add(GTK_CONTAINER(frame), vbox);
  gtk_widget_show(vbox); 
#if 1
  /* this is normally done directly after gtk_window_set_title() */
  /* but gtk crashes when we would do that and select a text with the mouse */
  xsane_set_window_icon(license_dialog, 0);
#endif

  /* display XSane copyright message */
  snprintf(buf, sizeof(buf), "XSane %s %s\n"
                             "%s %s\n"
                             "\n"
                             "%s\n"
                             "%s %s\n"
                             "%s %s\n",
                             TEXT_VERSION, XSANE_VERSION,
                             XSANE_COPYRIGHT_SIGN, XSANE_COPYRIGHT_TXT,
                             TEXT_GPL,
                             TEXT_HOMEPAGE, XSANE_HOMEPAGE,
                             TEXT_EMAIL, XSANE_EMAIL);

  label = gtk_label_new(buf);
  gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
  gtk_widget_show(label);

  /* add hbox with text and scrollbar to display the license text */
  hbox = gtk_hbox_new(FALSE, 2);
  gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0); 
  gtk_widget_show(hbox);

#ifdef HAVE_GTK_TEXT_VIEW_H
  {
   GtkWidget *scrolled_window, *text_view;
   GtkTextBuffer *text_buffer;

    /* create a scrolled window to get a vertical scrollbar */
    scrolled_window = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
    gtk_container_add(GTK_CONTAINER(hbox), scrolled_window);
    gtk_widget_show(scrolled_window);
 
    /* create the gtk_text_view widget */
    text_view = gtk_text_view_new();
    gtk_text_view_set_editable(GTK_TEXT_VIEW(text_view), FALSE);
    gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_view), GTK_WRAP_WORD);
    gtk_container_add(GTK_CONTAINER(scrolled_window), text_view);
    gtk_widget_show(text_view); 

    /* get the text_buffer widget and insert the text from file */
    text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));

    xsane_back_gtk_make_path(sizeof(filename), filename, "xsane", 0, "xsane-gpl", 0, ".txt", XSANE_PATH_SYSTEM);
    infile = fopen(filename, "r");
 
    if (infile)
    {
      char buffer[4096];
      int nchars;
   
      while (!feof(infile))
      {
        nchars = fread(buffer, 1, 4096, infile);
        gtk_text_buffer_insert_at_cursor(text_buffer, buffer, nchars);
      }
 
      fclose(infile);
    } 
    else
    {
      DBG(DBG_error0, "ERROR: license text not found. Looks like xsane is not installed correct.\n");
     return;
    }

  }
#else /* we do not have gtk_text_view, so we use gtk_text */
  {
   GtkWidget *text, *vscrollbar;

    /* Create the gtk_text widget */
    text = gtk_text_new(NULL, NULL);
    gtk_text_set_editable(GTK_TEXT(text), FALSE); /* text is not editable */
    gtk_text_set_word_wrap(GTK_TEXT(text), TRUE); /* wrap complete words */
    gtk_box_pack_start(GTK_BOX(hbox), text, TRUE, TRUE, 0); 
    gtk_widget_show(text);
 
    /* Add a vertical scrollbar to the GtkText widget */
    vscrollbar = gtk_vscrollbar_new(GTK_TEXT(text)->vadj);
    gtk_box_pack_start(GTK_BOX(hbox), vscrollbar, FALSE, FALSE, 0); 
    gtk_widget_show(vscrollbar);

    /* Freeze the text widget, ready for multiple updates */ 
    gtk_text_freeze(GTK_TEXT(text)); 

    /* Load the file text.c into the text window */
    xsane_back_gtk_make_path(sizeof(filename), filename, "xsane", 0, "xsane-gpl", 0, ".txt", XSANE_PATH_SYSTEM);
    infile = fopen(filename, "r");
 
    if (infile)
    {
      char buffer[4096];
      int nchars;
   
      while (!feof(infile))
      {
        nchars = fread(buffer, 1, 4096, infile);
        gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL, buffer, nchars);
      }
 
      fclose(infile);
    } 
    else
    {
      DBG(DBG_error0, "ERROR: license text not found. Looks like xsane is not installed correct.\n");
     return;
    }

    /* Thaw the text widget, allowing the updates to become visible */
    gtk_text_thaw(GTK_TEXT(text)); 
  }
#endif



  hbox = gtk_hbox_new(FALSE, 2);
  gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); 

#ifdef HAVE_GTK2
  button = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
#else
  button = gtk_button_new_with_label(BUTTON_CLOSE);
#endif
  gtk_widget_add_accelerator(button, "clicked", accelerator_group, GDK_Escape, 0, DEF_GTK_ACCEL_LOCKED);
  GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
  g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_close_license_dialog_callback, (void *) 0 /* ok = accept */);
  gtk_container_add(GTK_CONTAINER(hbox), button);
  gtk_widget_grab_default(button);
  gtk_widget_show(button);

  gtk_widget_show(hbox);
  gtk_widget_show(vbox);
  gtk_widget_show(license_dialog);
}

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

void xsane_window_get_position(GtkWidget *gtk_window, gint *x, gint *y)
{
#ifdef USE_GTK2_WINDOW_GET_POSITION
  gtk_window_get_position(GTK_WINDOW(gtk_window), x, y);
#else
  if (xsane.get_deskrelative_origin)
  {
    DBG(DBG_proc, "xsane_window_get_position(deskrelative)\n");
    gdk_window_get_deskrelative_origin(gtk_window->window, x, y);
  }
  else
  {
    DBG(DBG_proc, "xsane_window_get_position(root)\n");
    gdk_window_get_root_origin(gtk_window->window, x, y);
  }
#endif
}   

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

void xsane_widget_test_uposition(GtkWidget *gtk_window)
{
#ifndef USE_GTK2_WINDOW_GET_POSITION
 gint x, y, x_orig, y_orig;

  DBG(DBG_proc, "xsane_widget_test_uposition\n");

  gtk_widget_realize(gtk_window);

  while (!GTK_WIDGET_REALIZED(gtk_window) || (gtk_events_pending()))
  {
    gtk_main_iteration();
  }

  xsane_window_get_position(gtk_window, &x, &y);
  xsane_window_get_position(gtk_window, &x, &y);

  DBG(DBG_info, "xsane_widget_test_uposition: original position = %d, %d\n", x, y);

  x_orig = x;
  y_orig = y;
  gtk_window_move(GTK_WINDOW(gtk_window), x, y);

  xsane_window_get_position(gtk_window, &x, &y);
  DBG(DBG_info, "xsane_widget_test_uposition: new position = %d, %d\n", x, y);

  if ( (x != x_orig) || (y != y_orig) )
  {
    DBG(DBG_proc, "xsane_widget_test_uposition: using deskrelative function\n");
    xsane.get_deskrelative_origin = 1;
  }
  else
  {
    DBG(DBG_proc, "xsane_widget_test_uposition: using root function\n");
  }
#endif
}

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

static int xsane_front_gtk_getname_button;
                                                                                                                                 
static void xsane_front_gtk_getname_button_callback(GtkWidget *widget, gpointer data)
{
  DBG(DBG_proc, "xsane_front_gtk_getname_button_callback\n");
                                                                                                                                 
  xsane_front_gtk_getname_button = (int) data;
}
                                                                                                                                 
/* ---------------------------------------------------------------------------------------------------------------------- */
                                                                                                                                 
int xsane_front_gtk_getname_dialog(const char *dialog_title, const char *desc_text, char *oldname, char **newname)
{
 GtkWidget *getname_dialog;
 GtkWidget *text;
 GtkWidget *button;
 GtkWidget *vbox, *hbox;
 GtkAccelGroup *accelerator_group;
 char buf[256];
                                                                                                                                 
  DBG(DBG_proc, "xsane_getname_dialog, oldname = %s\n", oldname);
                                                                                                                                 
  getname_dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  xsane_set_window_icon(getname_dialog, 0);
                                                                                                                                 
  /* set getname dialog */
  gtk_window_set_position(GTK_WINDOW(getname_dialog), GTK_WIN_POS_CENTER);
  gtk_window_set_resizable(GTK_WINDOW(getname_dialog), FALSE);
  snprintf(buf, sizeof(buf), "%s %s", xsane.prog_name, dialog_title);
  gtk_window_set_title(GTK_WINDOW(getname_dialog), buf);
  g_signal_connect(GTK_OBJECT(getname_dialog), "delete_event", (GtkSignalFunc) xsane_front_gtk_getname_button_callback, (void *) -1);
  gtk_widget_show(getname_dialog);
                                                                                                                                 
  /* set the main vbox */
  vbox = gtk_vbox_new(FALSE, 0);
  gtk_container_set_border_width(GTK_CONTAINER(vbox), 0);
  gtk_container_add(GTK_CONTAINER(getname_dialog), vbox);
  gtk_widget_show(vbox);
                                                                                                                                 
  /* set the main hbox */
  hbox = gtk_hbox_new(FALSE, 0);
  xsane_separator_new(vbox, 2);
  gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
  gtk_container_set_border_width(GTK_CONTAINER(hbox), 5);
  gtk_widget_show(hbox);
                                                                                                                                 
  text = gtk_entry_new_with_max_length(64);
  xsane_back_gtk_set_tooltip(xsane.tooltips, text, desc_text);
  gtk_entry_set_text(GTK_ENTRY(text), oldname);
  gtk_widget_set_size_request(text, 300, -1);
  gtk_box_pack_start(GTK_BOX(vbox), text, TRUE, TRUE, 4);
  gtk_widget_show(text);
                                                                                                                                 
  accelerator_group = gtk_accel_group_new();
  gtk_window_add_accel_group(GTK_WINDOW(getname_dialog), accelerator_group);
                                                                                                                                 
#ifdef HAVE_GTK2
  button = gtk_button_new_from_stock(GTK_STOCK_OK);
#else
  button = gtk_button_new_with_label(BUTTON_OK);
#endif
  g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_front_gtk_getname_button_callback, (void *) 1);
  GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
  gtk_widget_show(button);
                                                                                                                                 
#ifdef HAVE_GTK2
  button = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
#else
  button = gtk_button_new_with_label(BUTTON_CANCEL);
#endif
  g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_front_gtk_getname_button_callback, (void *) -1);
  gtk_widget_add_accelerator(button, "clicked", accelerator_group, GDK_Escape, 0, DEF_GTK_ACCEL_LOCKED); /* ESC */
  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
  gtk_widget_show(button);
                                                                                                                                 
  xsane_front_gtk_getname_button = 0;
                                                                                                                                 
  while (xsane_front_gtk_getname_button == 0)
  {
    while (gtk_events_pending())
    {
      DBG(DBG_info, "xsane_getname_dialog: calling gtk_main_iteration\n");
      gtk_main_iteration();
    }
  }
                                                                                                                                 
  *newname = strdup(gtk_entry_get_text(GTK_ENTRY(text)));
                                                                                                                                 
  gtk_widget_destroy(getname_dialog);
                                                                                                                                 
  xsane_set_sensitivity(TRUE);
                                                                                                                                 
  if (xsane_front_gtk_getname_button == 1) /* OK button has been pressed */
  {
    DBG(DBG_info, "renaming %s to %s\n", oldname, *newname);
   return 0; /* OK */
  }
                                                                                                                                 
 return 1; /* Escape */
}
                                                                                                                                 
/* ---------------------------------------------------------------------------------------------------------------------- */
#ifdef XSANE_ACTIVATE_MAIL
void xsane_front_gtk_mail_project_update_lockfile_status()
{
 FILE *lockfile;
 char filename[PATH_MAX];

  snprintf(filename, sizeof(filename), "%s/lockfile", preferences.mail_project);
  lockfile = fopen(filename, "wb");

  if (lockfile)
  {
    fprintf(lockfile, "%s\n", xsane.mail_status); /* first line is status of mail */
    fprintf(lockfile, "%3d\n", (int) (xsane.mail_progress_val * 100));
  }

  fclose(lockfile);
}
#endif
/* ---------------------------------------------------------------------------------------------------------------------- */