summaryrefslogtreecommitdiff
path: root/src/frames.c
blob: 0c82a501ad675177f8ce18b084f5c0ebd4a92eb1 (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
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
/* Metacity window frame manager widget */

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

#include "frames.h"
#include "util.h"
#include "core.h"
#include "menu.h"

#define DEFAULT_INNER_BUTTON_BORDER 3

struct _MetaFrameProperties
{
  /* Size of left/right/bottom sides */
  int left_width;
  int right_width;
  int bottom_height;
  
  /* Border of blue title region */
  GtkBorder title_border;

  /* Border inside title region, around title */
  GtkBorder text_border;  
 
  /* padding on either side of spacer */
  int spacer_padding;

  /* Size of spacer */
  int spacer_width;
  int spacer_height;

  /* indent of buttons from edges of frame */
  int right_inset;
  int left_inset;
  
  /* Size of buttons */
  int button_width;
  int button_height;

  /* Space around buttons */
  GtkBorder button_border;

  /* Space inside button which is clickable but doesn't draw the
   * button icon
   */
  GtkBorder inner_button_border;
};

typedef struct _MetaFrameGeometry MetaFrameGeometry;

struct _MetaFrameGeometry
{
  int left_width;
  int right_width;
  int top_height;
  int bottom_height;

  int width;
  int height;
  
  GdkRectangle close_rect;
  GdkRectangle max_rect;
  GdkRectangle min_rect;
  GdkRectangle spacer_rect;
  GdkRectangle menu_rect;
  GdkRectangle title_rect;  
};

static void meta_frames_class_init (MetaFramesClass *klass);
static void meta_frames_init       (MetaFrames      *frames);
static void meta_frames_destroy    (GtkObject       *object);
static void meta_frames_finalize   (GObject         *object);
static void meta_frames_style_set  (GtkWidget       *widget,
                                    GtkStyle        *prev_style);

gboolean meta_frames_button_press_event      (GtkWidget           *widget,
                                              GdkEventButton      *event);
gboolean meta_frames_button_release_event    (GtkWidget           *widget,
                                              GdkEventButton      *event);
gboolean meta_frames_motion_notify_event     (GtkWidget           *widget,
                                              GdkEventMotion      *event);
gboolean meta_frames_destroy_event           (GtkWidget           *widget,
                                              GdkEventAny         *event);
gboolean meta_frames_expose_event            (GtkWidget           *widget,
                                              GdkEventExpose      *event);
gboolean meta_frames_key_press_event         (GtkWidget           *widget,
                                              GdkEventKey         *event);
gboolean meta_frames_key_release_event       (GtkWidget           *widget,
                                              GdkEventKey         *event);
gboolean meta_frames_enter_notify_event      (GtkWidget           *widget,
                                              GdkEventCrossing    *event);
gboolean meta_frames_leave_notify_event      (GtkWidget           *widget,
                                              GdkEventCrossing    *event);
gboolean meta_frames_configure_event         (GtkWidget           *widget,
                                              GdkEventConfigure   *event);
gboolean meta_frames_focus_in_event          (GtkWidget           *widget,
                                              GdkEventFocus       *event);
gboolean meta_frames_focus_out_event         (GtkWidget           *widget,
                                              GdkEventFocus       *event);
gboolean meta_frames_map_event               (GtkWidget           *widget,
                                              GdkEventAny         *event);
gboolean meta_frames_unmap_event             (GtkWidget           *widget,
                                              GdkEventAny         *event);
gboolean meta_frames_property_notify_event   (GtkWidget           *widget,
                                              GdkEventProperty    *event);
gboolean meta_frames_client_event            (GtkWidget           *widget,
                                              GdkEventClient      *event);
gboolean meta_frames_window_state_event      (GtkWidget           *widget,
                                              GdkEventWindowState *event);


static void meta_frames_calc_geometry (MetaFrames        *frames,
                                       MetaUIFrame         *frame,
                                       MetaFrameGeometry *fgeom);

static MetaUIFrame* meta_frames_lookup_window (MetaFrames *frames,
                                               Window      xwindow);

static void meta_frames_begin_grab (MetaFrames *frames,
                                    MetaUIFrame *frame,
                                    MetaFrameStatus status,
                                    int start_button,
                                    int start_root_x,
                                    int start_root_y,
                                    int start_window_x,
                                    int start_window_y,
                                    guint32 timestamp);
static void meta_frames_end_grab (MetaFrames *frames,
                                  guint32 timestamp);


static GdkRectangle*    control_rect (MetaFrameControl   control,
                                      MetaFrameGeometry *fgeom);
static MetaFrameControl get_control  (MetaFrames        *frames,
                                      MetaUIFrame       *frame,
                                      int                x,
                                      int                y);

enum
{
  LAST_SIGNAL
};

static GtkWidgetClass *parent_class = NULL;
static guint signals[LAST_SIGNAL];

GtkType
meta_frames_get_type (void)
{
  static GtkType frames_type = 0;

  if (!frames_type)
    {
      static const GtkTypeInfo frames_info =
      {
	"MetaFrames",
	sizeof (MetaFrames),
	sizeof (MetaFramesClass),
	(GtkClassInitFunc) meta_frames_class_init,
	(GtkObjectInitFunc) meta_frames_init,
	/* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
        (GtkClassInitFunc) NULL,
      };

      frames_type = gtk_type_unique (GTK_TYPE_WINDOW, &frames_info);
    }

  return frames_type;
}

#define BORDER_PROPERTY(name, blurb, docs)                                        \
  gtk_widget_class_install_style_property (widget_class,                           \
					   g_param_spec_boxed (name,               \
							       blurb,              \
							       docs,               \
							       GTK_TYPE_BORDER,    \
							       G_PARAM_READABLE))

#define INT_PROPERTY(name, default, blurb, docs)                               \
  gtk_widget_class_install_style_property (widget_class,                        \
					   g_param_spec_int (name,              \
                                                             blurb,             \
                                                             docs,              \
							     0,                 \
							     G_MAXINT,          \
                                                             default,           \
							     G_PARAM_READABLE))

static void
meta_frames_class_init (MetaFramesClass *class)
{
  GObjectClass   *gobject_class;
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;

  gobject_class = G_OBJECT_CLASS (class);
  object_class = (GtkObjectClass*) class;
  widget_class = (GtkWidgetClass*) class;

  parent_class = g_type_class_peek_parent (class);

  gobject_class->finalize = meta_frames_finalize;
  object_class->destroy = meta_frames_destroy;

  widget_class->style_set = meta_frames_style_set;

  widget_class->expose_event = meta_frames_expose_event;
  widget_class->unmap_event = meta_frames_unmap_event;
  widget_class->destroy_event = meta_frames_destroy_event;
  widget_class->button_press_event = meta_frames_button_press_event;
  widget_class->button_release_event = meta_frames_button_release_event;
  widget_class->motion_notify_event = meta_frames_motion_notify_event;
  
  INT_PROPERTY ("left_width", 6, _("Left edge"), _("Left window edge width"));
  INT_PROPERTY ("right_width", 6, _("Right edge"), _("Right window edge width"));
  INT_PROPERTY ("bottom_height", 7, _("Bottom edge"), _("Bottom window edge height"));
  
  BORDER_PROPERTY ("title_border", _("Title border"), _("Border around title area"));
  BORDER_PROPERTY ("text_border", _("Text border"), _("Border around window title text"));

  INT_PROPERTY ("spacer_padding", 3, _("Spacer padding"), _("Padding on either side of spacer"));
  INT_PROPERTY ("spacer_width", 2, _("Spacer width"), _("Width of spacer"));
  INT_PROPERTY ("spacer_height", 11, _("Spacer height"), _("Height of spacer"));

  /* same as right_width left_width by default */
  INT_PROPERTY ("right_inset", 6, _("Right inset"), _("Distance of buttons from right edge of frame"));
  INT_PROPERTY ("left_inset", 6, _("Left inset"), _("Distance of menu button from left edge of frame"));
  
  INT_PROPERTY ("button_width", 15, _("Button width"), _("Width of buttons"));
  INT_PROPERTY ("button_height", 15, _("Button height"), _("Height of buttons"));

  BORDER_PROPERTY ("button_border", _("Button border"), _("Border around buttons"));
  BORDER_PROPERTY ("inner_button_border", _("Inner button border"), _("Border around the icon inside buttons"));
}

static gint
unsigned_long_equal (gconstpointer v1,
                     gconstpointer v2)
{
  return *((const gulong*) v1) == *((const gulong*) v2);
}

static guint
unsigned_long_hash (gconstpointer v)
{
  gulong val = * (const gulong *) v;

  /* I'm not sure this works so well. */
#if G_SIZEOF_LONG > 4
  return (guint) (val ^ (val >> 32));
#else
  return val;
#endif
}

static void
meta_frames_init (MetaFrames *frames)
{
  GTK_WINDOW (frames)->type = GTK_WINDOW_POPUP;

  frames->props = g_new0 (MetaFrameProperties, 1);

  frames->frames = g_hash_table_new (unsigned_long_hash, unsigned_long_equal);

  frames->grab_status = META_FRAME_STATUS_NORMAL;
}

static void
listify_func (gpointer key, gpointer value, gpointer data)
{
  GSList **listp;

  listp = data;
  *listp = g_slist_prepend (*listp, value);
}

static void
meta_frames_destroy (GtkObject *object)
{
  GSList *winlist;
  GSList *tmp;
  MetaFrames *frames;
  
  frames = META_FRAMES (object);
  
  winlist = NULL;
  g_hash_table_foreach (frames->frames,
                        listify_func,
                        &winlist);

  /* Unmanage all frames */
  tmp = winlist;
  while (tmp != NULL)
    {
      MetaUIFrame *frame;

      frame = tmp->data;

      meta_frames_unmanage_window (frames, frame->xwindow);
      
      tmp = tmp->next;
    }
  g_slist_free (winlist);

  GTK_OBJECT_CLASS (parent_class)->destroy (object);
}

static void
meta_frames_finalize (GObject *object)
{
  MetaFrames *frames;
  
  frames = META_FRAMES (object);

  g_assert (g_hash_table_size (frames->frames) == 0);
  g_hash_table_destroy (frames->frames);
  
  g_free (frames->props);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
queue_recalc_func (gpointer key, gpointer value, gpointer data)
{
  MetaUIFrame *frame;
  MetaFrames *frames;

  frames = META_FRAMES (data);
  frame = value;

  /* If a resize occurs it will cause a redraw, but the
   * resize may not actually be needed so we always redraw
   * in case of color change.
   */
  gtk_style_set_background (GTK_WIDGET (frames)->style,
                            frame->window, GTK_STATE_NORMAL);
  gdk_window_invalidate_rect (frame->window, NULL, FALSE);
  meta_core_queue_frame_resize (gdk_display,
                                frame->xwindow);
  if (frame->layout)
    {
      /* recreate layout */
      char *text;
      
      text = g_strdup (pango_layout_get_text (frame->layout));

      g_object_unref (G_OBJECT (frame->layout));
      
      frame->layout = gtk_widget_create_pango_layout (GTK_WIDGET (frames),
                                                      text);

      g_free (text);
    }
}

static void
meta_frames_style_set  (GtkWidget *widget,
                        GtkStyle  *prev_style)
{
  MetaFrames *frames;
  /* left, right, top, bottom */
  static GtkBorder default_title_border = { 3, 4, 4, 3 };
  static GtkBorder default_text_border = { 2, 2, 2, 2 };
  static GtkBorder default_button_border = { 1, 1, 1, 1 };
  static GtkBorder default_inner_button_border = {
    DEFAULT_INNER_BUTTON_BORDER,
    DEFAULT_INNER_BUTTON_BORDER,
    DEFAULT_INNER_BUTTON_BORDER,
    DEFAULT_INNER_BUTTON_BORDER
  };
  GtkBorder *title_border;
  GtkBorder *text_border;
  GtkBorder *button_border;
  GtkBorder *inner_button_border;
  MetaFrameProperties props;

  frames = META_FRAMES (widget);
  
  gtk_widget_style_get (widget,
                        "left_width",
                        &props.left_width,
                        "right_width",
                        &props.right_width,
                        "bottom_height",
                        &props.bottom_height,
                        "title_border",
                        &title_border,
                        "text_border",
                        &text_border,
                        "spacer_padding",
                        &props.spacer_padding,
                        "spacer_width",
                        &props.spacer_width,
                        "spacer_height",
                        &props.spacer_height,
                        "right_inset",
                        &props.right_inset,
                        "left_inset",
                        &props.left_inset,
                        "button_width",
                        &props.button_width,
                        "button_height",
                        &props.button_height,
                        "button_border",
                        &button_border,
                        "inner_button_border",
                        &inner_button_border,
                        NULL);
  
  if (title_border)
    props.title_border = *title_border;
  else
    props.title_border = default_title_border;

  g_free (title_border);
  
  if (text_border)
    props.text_border = *text_border;
  else
    props.text_border = default_text_border;

  g_free (text_border);

  if (button_border)
    props.button_border = *button_border;
  else
    props.button_border = default_button_border;

  g_free (button_border);

  if (inner_button_border)
    props.inner_button_border = *inner_button_border;
  else
    props.inner_button_border = default_inner_button_border;

  g_free (inner_button_border);

  *(frames->props) = props;

  {
    PangoFontMetrics metrics;
    PangoFont *font;
    PangoLanguage *lang;

    font = pango_context_load_font (gtk_widget_get_pango_context (widget),
                                    widget->style->font_desc);
    lang = pango_context_get_language (gtk_widget_get_pango_context (widget));
    pango_font_get_metrics (font, lang, &metrics);
    
    g_object_unref (G_OBJECT (font));
    
    frames->text_height = PANGO_PIXELS (metrics.ascent + metrics.descent);
  }

  /* Queue a draw/resize on all frames */
  g_hash_table_foreach (frames->frames,
                        queue_recalc_func, frames);
}

static void
meta_frames_calc_geometry (MetaFrames        *frames,
                           MetaUIFrame         *frame,
                           MetaFrameGeometry *fgeom)
{
  int x;
  int button_y;
  int title_right_edge;
  MetaFrameProperties props;
  int buttons_height, title_height, spacer_height;
  int width, height;
  MetaFrameFlags flags;
  
  props = *(frames->props);

  meta_core_get_frame_size (gdk_display, frame->xwindow,
                            &width, &height);
  
  flags = meta_core_get_frame_flags (gdk_display, frame->xwindow);

  fgeom->width = width;
  fgeom->height = height;
  
  buttons_height = props.button_height +
    props.button_border.top + props.button_border.bottom;
  title_height = frames->text_height +
    props.text_border.top + props.text_border.bottom +
    props.title_border.top + props.title_border.bottom;
  spacer_height = props.spacer_height;
  
  fgeom->top_height = MAX (buttons_height, title_height);
  fgeom->top_height = MAX (fgeom->top_height, spacer_height);

  fgeom->left_width = props.left_width;
  fgeom->right_width = props.right_width;

  if (flags & META_FRAME_SHADED)
    fgeom->bottom_height = 0;
  else
    fgeom->bottom_height = props.bottom_height;

  x = width - props.right_inset;

  /* center buttons */
  button_y = (fgeom->top_height -
              (props.button_height + props.button_border.top + props.button_border.bottom)) / 2 + props.button_border.top;

  if ((flags & META_FRAME_ALLOWS_DELETE) &&
      x >= 0)
    {
      fgeom->close_rect.x = x - props.button_border.right - props.button_width;
      fgeom->close_rect.y = button_y;
      fgeom->close_rect.width = props.button_width;
      fgeom->close_rect.height = props.button_height;

      x = fgeom->close_rect.x - props.button_border.left;
    }
  else
    {
      fgeom->close_rect.x = 0;
      fgeom->close_rect.y = 0;
      fgeom->close_rect.width = 0;
      fgeom->close_rect.height = 0;
    }

  if ((flags & META_FRAME_ALLOWS_MAXIMIZE) &&
      x >= 0)
    {
      fgeom->max_rect.x = x - props.button_border.right - props.button_width;
      fgeom->max_rect.y = button_y;
      fgeom->max_rect.width = props.button_width;
      fgeom->max_rect.height = props.button_height;

      x = fgeom->max_rect.x - props.button_border.left;
    }
  else
    {
      fgeom->max_rect.x = 0;
      fgeom->max_rect.y = 0;
      fgeom->max_rect.width = 0;
      fgeom->max_rect.height = 0;
    }
  
  if ((flags & META_FRAME_ALLOWS_MINIMIZE) &&
      x >= 0)
    {
      fgeom->min_rect.x = x - props.button_border.right - props.button_width;
      fgeom->min_rect.y = button_y;
      fgeom->min_rect.width = props.button_width;
      fgeom->min_rect.height = props.button_height;

      x = fgeom->min_rect.x - props.button_border.left;
    }
  else
    {
      fgeom->min_rect.x = 0;
      fgeom->min_rect.y = 0;
      fgeom->min_rect.width = 0;
      fgeom->min_rect.height = 0;
    }

  if ((fgeom->close_rect.width > 0 ||
       fgeom->max_rect.width > 0 ||
       fgeom->min_rect.width > 0) &&
      x >= 0)
    {
      fgeom->spacer_rect.x = x - props.spacer_padding - props.spacer_width;
      fgeom->spacer_rect.y = (fgeom->top_height - props.spacer_height) / 2;
      fgeom->spacer_rect.width = props.spacer_width;
      fgeom->spacer_rect.height = props.spacer_height;

      x = fgeom->spacer_rect.x - props.spacer_padding;
    }
  else
    {
      fgeom->spacer_rect.x = 0;
      fgeom->spacer_rect.y = 0;
      fgeom->spacer_rect.width = 0;
      fgeom->spacer_rect.height = 0;
    }

  title_right_edge = x - props.title_border.right;
  
  /* Now x changes to be position from the left */
  x = props.left_inset;
  
  if (flags & META_FRAME_ALLOWS_MENU)
    {
      fgeom->menu_rect.x = x + props.button_border.left;
      fgeom->menu_rect.y = button_y;
      fgeom->menu_rect.width = props.button_width;
      fgeom->menu_rect.height = props.button_height;

      x = fgeom->menu_rect.x + fgeom->menu_rect.width + props.button_border.right;
    }
  else
    {
      fgeom->menu_rect.x = 0;
      fgeom->menu_rect.y = 0;
      fgeom->menu_rect.width = 0;
      fgeom->menu_rect.height = 0;
    }

  /* If menu overlaps close button, then the menu wins since it
   * lets you perform any operation including close
   */
  if (fgeom->close_rect.width > 0 &&
      fgeom->close_rect.x < (fgeom->menu_rect.x + fgeom->menu_rect.height))
    {
      fgeom->close_rect.width = 0;
      fgeom->close_rect.height = 0;
    }

  /* Check for maximize overlap */
  if (fgeom->max_rect.width > 0 &&
      fgeom->max_rect.x < (fgeom->menu_rect.x + fgeom->menu_rect.height))
    {
      fgeom->max_rect.width = 0;
      fgeom->max_rect.height = 0;
    }
  
  /* Check for minimize overlap */
  if (fgeom->min_rect.width > 0 &&
      fgeom->min_rect.x < (fgeom->menu_rect.x + fgeom->menu_rect.height))
    {
      fgeom->min_rect.width = 0;
      fgeom->min_rect.height = 0;
    }

  /* Check for spacer overlap */
  if (fgeom->spacer_rect.width > 0 &&
      fgeom->spacer_rect.x < (fgeom->menu_rect.x + fgeom->menu_rect.height))
    {
      fgeom->spacer_rect.width = 0;
      fgeom->spacer_rect.height = 0;
    }
  
  /* We always fill as much vertical space as possible with title rect,
   * rather than centering it like the buttons and spacer
   */
  fgeom->title_rect.x = x + props.title_border.left;
  fgeom->title_rect.y = props.title_border.top;
  fgeom->title_rect.width = title_right_edge - fgeom->title_rect.x;
  fgeom->title_rect.height = fgeom->top_height - props.title_border.top - props.title_border.bottom;

  /* Nuke title if it won't fit */
  if (fgeom->title_rect.width < 0 ||
      fgeom->title_rect.height < 0)
    {
      fgeom->title_rect.width = 0;
      fgeom->title_rect.height = 0;
    }
}

MetaFrames*
meta_frames_new (void)
{
  return g_object_new (META_TYPE_FRAMES, NULL);
}

void
meta_frames_manage_window (MetaFrames *frames,
                           Window      xwindow)
{
  MetaUIFrame *frame;

  frame = g_new (MetaUIFrame, 1);
  
  frame->window = gdk_window_foreign_new (xwindow);

  if (frame->window == NULL)
    {
      g_free (frame);
      meta_bug ("Frame 0x%lx doesn't exist\n", xwindow);
      return;
    }
  
  gdk_window_set_user_data (frame->window, frames);

#if 0
  /* Add events in frame.c */
  gdk_window_set_events (frame->window,
                         GDK_EXPOSURE_MASK |
                         GDK_POINTER_MOTION_MASK |
                         GDK_POINTER_MOTION_HINT_MASK |
                         GDK_BUTTON_PRESS_MASK |
                         GDK_BUTTON_RELEASE_MASK |
                         GDK_STRUCTURE_MASK);
  
  /* This shouldn't be required if we don't select for button
   * press in frame.c?
   */
  XGrabButton (gdk_display, AnyButton, AnyModifier,
               xwindow, False,
               ButtonPressMask | ButtonReleaseMask |    
               PointerMotionMask | PointerMotionHintMask,
               GrabModeAsync, GrabModeAsync,
               False, None);

  XFlush (gdk_display);
#endif
  
  frame->xwindow = xwindow;
  frame->layout = NULL;
  
  g_hash_table_insert (frames->frames, &frame->xwindow, frame);
}

void
meta_frames_unmanage_window (MetaFrames *frames,
                             Window      xwindow)
{
  MetaUIFrame *frame;

  frame = g_hash_table_lookup (frames->frames, &xwindow);

  if (frame)
    {
      gdk_window_set_user_data (frame->window, NULL);
      
      if (frames->grab_frame == frame)
        meta_frames_end_grab (frames, GDK_CURRENT_TIME);
      
      g_hash_table_remove (frames->frames, &frame->xwindow);

      g_object_unref (G_OBJECT (frame->window));

      if (frame->layout)
        g_object_unref (G_OBJECT (frame->layout));
      
      g_free (frame);
    }
  else
    meta_warning ("Frame 0x%lx not managed, can't unmanage\n", xwindow);
}

static MetaUIFrame*
meta_frames_lookup_window (MetaFrames *frames,
                           Window      xwindow)
{
  MetaUIFrame *frame;

  frame = g_hash_table_lookup (frames->frames, &xwindow);

  return frame;
}

void
meta_frames_get_geometry (MetaFrames *frames,
                          Window xwindow,
                          int *top_height, int *bottom_height,
                          int *left_width, int *right_width)
{
  MetaFrameGeometry fgeom;

  MetaUIFrame *frame;

  frame = meta_frames_lookup_window (frames, xwindow);

  if (frame == NULL)
    meta_bug ("No such frame 0x%lx\n", xwindow);

  meta_frames_calc_geometry (frames, frame, &fgeom);

  if (top_height)
    *top_height = fgeom.top_height;
  if (bottom_height)
    *bottom_height = fgeom.bottom_height;
  if (left_width)
    *left_width = fgeom.left_width;
  if (right_width)
    *right_width = fgeom.right_width;
}

void
meta_frames_reset_bg (MetaFrames *frames,
                      Window  xwindow)
{
  GtkWidget *widget;
  MetaUIFrame *frame;
  
  widget = GTK_WIDGET (frames);

  frame = meta_frames_lookup_window (frames, xwindow);
  
  gtk_style_set_background (widget->style, frame->window, GTK_STATE_NORMAL);
}

void
meta_frames_queue_draw (MetaFrames *frames,
                        Window      xwindow)
{
  GtkWidget *widget;
  MetaUIFrame *frame;
  
  widget = GTK_WIDGET (frames);

  frame = meta_frames_lookup_window (frames, xwindow);

  gdk_window_invalidate_rect (frame->window, NULL, FALSE);
}

void
meta_frames_set_title (MetaFrames *frames,
                       Window      xwindow,
                       const char *title)
{
  GtkWidget *widget;
  MetaUIFrame *frame;
  
  widget = GTK_WIDGET (frames);

  frame = meta_frames_lookup_window (frames, xwindow);
  
  if (frame->layout == NULL)
    frame->layout = gtk_widget_create_pango_layout (widget,
                                                    title);
  else
    pango_layout_set_text (frame->layout, title, -1);
  
  gdk_window_invalidate_rect (frame->window, NULL, FALSE);
}

static void
meta_frames_begin_grab (MetaFrames *frames,
                        MetaUIFrame *frame,
                        MetaFrameStatus status,
                        int start_button,
                        int start_root_x,
                        int start_root_y,
                        int start_window_x,
                        int start_window_y,
                        guint32 timestamp)
{
  g_return_if_fail (frames->grab_frame == NULL);

  /* This grab isn't needed I don't think */
  if (gdk_pointer_grab (frame->window,
                        FALSE,
                        GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
                        GDK_POINTER_MOTION_HINT_MASK | GDK_POINTER_MOTION_MASK,
                        NULL,
                        NULL,
                        timestamp) == GDK_GRAB_SUCCESS)
    {
      frames->grab_frame = frame;
      frames->grab_status = status;
      frames->start_button = start_button;
      frames->start_root_x = start_root_x;
      frames->start_root_y = start_root_y;
      frames->start_window_x = start_window_x;
      frames->start_window_y = start_window_y;
    }
}

static void
meta_frames_end_grab (MetaFrames *frames,
                      guint32     timestamp)
{
  if (frames->grab_frame)
    {      
      frames->grab_frame = NULL;
      frames->grab_status = META_FRAME_STATUS_NORMAL;
      gdk_pointer_ungrab (timestamp);
    }
}

static void
frame_query_root_pointer (MetaUIFrame *frame,
                          int *x, int *y)
{
  Window root_return, child_return;
  int root_x_return, root_y_return;
  int win_x_return, win_y_return;
  unsigned int mask_return;

  XQueryPointer (gdk_display,
                 frame->xwindow,
                 &root_return,
                 &child_return,
                 &root_x_return,
                 &root_y_return,
                 &win_x_return,
                 &win_y_return,
                 &mask_return);

  if (x)
    *x = root_x_return;
  if (y)
    *y = root_y_return;
}

static void
update_move (MetaFrames  *frames,
             MetaUIFrame *frame,
             int          x,
             int          y)
{
  int dx, dy;
  
  dx = x - frames->start_root_x;
  dy = y - frames->start_root_y;

  meta_core_user_move (gdk_display,
                       frame->xwindow,
                       frames->start_window_x + dx,
                       frames->start_window_y + dy);
}

static void
update_resize_se (MetaFrames *frames,
                  MetaUIFrame *frame,
                  int x, int y)
{
  int dx, dy;
  
  dx = x - frames->start_root_x;
  dy = y - frames->start_root_y;

  meta_core_user_resize (gdk_display,
                         frame->xwindow,
                         frames->start_window_x + dx,
                         frames->start_window_y + dy);
}

static void
redraw_control (MetaFrames *frames,
                MetaUIFrame *frame,
                MetaFrameControl control)
{
  MetaFrameGeometry fgeom;
  GdkRectangle *rect;
  
  meta_frames_calc_geometry (frames, frame, &fgeom);

  rect = control_rect (control, &fgeom);

  gdk_window_invalidate_rect (frame->window, rect, FALSE);
}

static gboolean
point_in_control (MetaFrames *frames,
                  MetaUIFrame *frame,
                  MetaFrameControl control,
                  int x, int y)
{
  return control == get_control (frames, frame, x, y);
}

gboolean
meta_frames_button_press_event (GtkWidget      *widget,
                                GdkEventButton *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;
  MetaFrameControl control;
  
  frames = META_FRAMES (widget);
  
  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;
  
  control = get_control (frames, frame, event->x, event->y);

  if (control == META_FRAME_CONTROL_TITLE &&
      event->button == 1 &&
      event->type == GDK_2BUTTON_PRESS)
    {
      MetaFrameFlags flags;
      
      flags = meta_core_get_frame_flags (gdk_display, frame->xwindow);

      if (flags & META_FRAME_ALLOWS_SHADE)
        {
          if (flags & META_FRAME_SHADED)
            meta_core_unshade (gdk_display,
                               frame->xwindow);
          else
            meta_core_shade (gdk_display,
                             frame->xwindow);
        }

      return TRUE;
    }

  if (frames->grab_frame != NULL)
    return FALSE; /* already up to something */
  
  if (event->button == 1)
    meta_core_user_raise (gdk_display, frame->xwindow);

  if (event->button == 1 &&
      (control == META_FRAME_CONTROL_MAXIMIZE ||
       control == META_FRAME_CONTROL_MINIMIZE ||
       control == META_FRAME_CONTROL_DELETE ||
       control == META_FRAME_CONTROL_MENU))
    {
      MetaFrameStatus status = META_FRAME_STATUS_NORMAL;

      switch (control)
        {
        case META_FRAME_CONTROL_MINIMIZE:
          status = META_FRAME_STATUS_CLICKING_MINIMIZE;
          break;
        case META_FRAME_CONTROL_MAXIMIZE:
          status = META_FRAME_STATUS_CLICKING_MAXIMIZE;
          break;
        case META_FRAME_CONTROL_DELETE:
          status = META_FRAME_STATUS_CLICKING_DELETE;
          break;
        case META_FRAME_CONTROL_MENU:
          status = META_FRAME_STATUS_CLICKING_MENU;
          break;
        default:
          g_assert_not_reached ();
          break;
        }

      g_assert (status != META_FRAME_STATUS_NORMAL);

      meta_frames_begin_grab (frames, frame,
                              status,
                              event->button,
                              0, 0, 0, 0, /* not needed */
                              event->time);

      redraw_control (frames, frame, control);

      if (status == META_FRAME_STATUS_CLICKING_MENU)
        {
          meta_core_show_window_menu (gdk_display,
                                      frame->xwindow,
                                      event->x_root,
                                      event->y_root,
                                      event->button,
                                      event->time);
        }
    }
  else if (control == META_FRAME_CONTROL_RESIZE_SE &&
           event->button == 1)
    {
      MetaFrameFlags flags;
      
      flags = meta_core_get_frame_flags (gdk_display, frame->xwindow);

      if (flags & META_FRAME_ALLOWS_RESIZE)
        {
          int w, h;
          
          meta_core_get_size (gdk_display,
                              frame->xwindow,
                              &w, &h);
          
          meta_frames_begin_grab (frames, frame,
                                  META_FRAME_STATUS_RESIZING_SE,
                                  event->button,
                                  event->x_root,
                                  event->y_root,
                                  w, h,
                                  event->time);
        }
    }
  else if (((control == META_FRAME_CONTROL_TITLE ||
             control == META_FRAME_CONTROL_NONE) &&
            event->button == 1) ||
           event->button == 2)
    {
      MetaFrameFlags flags;
      
      flags = meta_core_get_frame_flags (gdk_display, frame->xwindow);
      
      if (flags & META_FRAME_ALLOWS_MOVE)
        {
          int x, y;
          
          meta_core_get_position (gdk_display,
                                  frame->xwindow,
                                  &x, &y);
          
          meta_frames_begin_grab (frames, frame,
                                  META_FRAME_STATUS_MOVING,
                                  event->button,
                                  event->x_root,
                                  event->y_root,
                                  x, y,
                                  event->time);
        }
    }
  
  return TRUE;
}

void
meta_frames_notify_menu_hide (MetaFrames *frames)
{
  if (frames->grab_status == META_FRAME_STATUS_CLICKING_MENU)
    {
      redraw_control (frames, frames->grab_frame,
                      META_FRAME_CONTROL_MENU);
      meta_frames_end_grab (frames, GDK_CURRENT_TIME);
    }
}

gboolean
meta_frames_button_release_event    (GtkWidget           *widget,
                                     GdkEventButton      *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;

  frames = META_FRAMES (widget);
  
  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;

  if (frames->grab_frame == frame &&
      frames->start_button == event->button)
    {
      MetaFrameStatus status;

      status = frames->grab_status;
      
      meta_frames_end_grab (frames, event->time);

      switch (status)
        {
        case META_FRAME_STATUS_MOVING:
          update_move (frames, frame, event->x_root, event->y_root);
          break;

        case META_FRAME_STATUS_RESIZING_SE:
          update_resize_se (frames, frame, event->x_root, event->y_root);
          break;

        case META_FRAME_STATUS_CLICKING_MINIMIZE:
          if (point_in_control (frames, frame,
                                META_FRAME_CONTROL_MINIMIZE,
                                event->x, event->y))
            meta_core_minimize (gdk_display, frame->xwindow);

          redraw_control (frames, frame,
                          META_FRAME_CONTROL_MINIMIZE);
          break;

        case META_FRAME_STATUS_CLICKING_MAXIMIZE:
          if (point_in_control (frames, frame,
                                META_FRAME_CONTROL_MAXIMIZE,
                                event->x, event->y))
            {
              if (meta_core_get_frame_flags (gdk_display, frame->xwindow) &
                  META_FRAME_MAXIMIZED)
                meta_core_unmaximize (gdk_display, frame->xwindow);
              else
                meta_core_maximize (gdk_display, frame->xwindow);
            }
          redraw_control (frames, frame,
                          META_FRAME_CONTROL_MAXIMIZE);
          break;

        case META_FRAME_STATUS_CLICKING_DELETE:
          if (point_in_control (frames, frame,
                                META_FRAME_CONTROL_DELETE,
                                event->x, event->y))
            meta_core_delete (gdk_display, frame->xwindow, event->time);
          redraw_control (frames, frame,
                          META_FRAME_CONTROL_DELETE);
          break;
          
        case META_FRAME_STATUS_CLICKING_MENU:
          redraw_control (frames, frame,
                          META_FRAME_CONTROL_MENU);
          break;
          
        case META_FRAME_STATUS_NORMAL:
          break;
        }
    }
  
  return TRUE;
}

gboolean
meta_frames_motion_notify_event     (GtkWidget           *widget,
                                     GdkEventMotion      *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;
  
  frames = META_FRAMES (widget);

  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;

  switch (frames->grab_status)
    {
    case META_FRAME_STATUS_MOVING:
      {
        int x, y;
        frame_query_root_pointer (frame, &x, &y);
        update_move (frames, frame, x, y);
      }
      break;
    case META_FRAME_STATUS_RESIZING_SE:
      {
        int x, y;
        frame_query_root_pointer (frame, &x, &y);
        update_resize_se (frames, frame, x, y);
      }
      break;

    case META_FRAME_STATUS_CLICKING_MENU:
    case META_FRAME_STATUS_CLICKING_DELETE:
    case META_FRAME_STATUS_CLICKING_MINIMIZE:
    case META_FRAME_STATUS_CLICKING_MAXIMIZE:
    case META_FRAME_STATUS_NORMAL:
      break;
    }
      
  return TRUE;
}

gboolean
meta_frames_destroy_event           (GtkWidget           *widget,
                                     GdkEventAny         *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;

  frames = META_FRAMES (widget);

  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;

  if (frames->grab_frame == frame)
    meta_frames_end_grab (frames, GDK_CURRENT_TIME);
  
  return TRUE;
}

static void
draw_control (MetaFrames  *frames,
              GdkDrawable *drawable,
              GdkGC       *override,
              MetaFrameControl control,
              int x, int y, int width, int height)
{
  GtkWidget *widget;
  GdkGCValues vals;
  GdkGC *gc;

  widget = GTK_WIDGET (frames);

#define THICK_LINE_WIDTH 3

  gc = override ? override : widget->style->fg_gc[GTK_STATE_NORMAL];
  
  switch (control)
    {
    case META_FRAME_CONTROL_DELETE:
      {
        gdk_draw_line (drawable,
                       gc,
                       x, y, x + width - 1, y + height - 1);
        
        gdk_draw_line (drawable,
                       gc,
                       x, y + height - 1, x + width - 1, y);
      }
      break;

    case META_FRAME_CONTROL_MAXIMIZE:
      {
        gdk_draw_rectangle (drawable,
                            gc,
                            FALSE,
                            x, y, width - 1, height - 1);
        
        vals.line_width = THICK_LINE_WIDTH;
        gdk_gc_set_values (gc,
                           &vals,
                           GDK_GC_LINE_WIDTH);
        
        gdk_draw_line (drawable,
                       gc,
                       x, y + 1, x + width, y + 1);
        
        vals.line_width = 0;
        gdk_gc_set_values (gc,
                           &vals,
                           GDK_GC_LINE_WIDTH);
      }
      break;

    case META_FRAME_CONTROL_MINIMIZE:
      {
              
      vals.line_width = THICK_LINE_WIDTH;
      gdk_gc_set_values (gc,
                         &vals,
                         GDK_GC_LINE_WIDTH);

      gdk_draw_line (drawable,
                     gc,
                     x,         y + height - THICK_LINE_WIDTH + 1,
                     x + width, y + height - THICK_LINE_WIDTH + 1);
      
      vals.line_width = 0;
      gdk_gc_set_values (gc,
                         &vals,
                         GDK_GC_LINE_WIDTH);
      }
      break;
      
    default:
      break;
    }
#undef THICK_LINE_WIDTH
}

void
meta_frames_get_pixmap_for_control (MetaFrames      *frames,
                                    MetaFrameControl control,
                                    GdkPixmap      **pixmapp,
                                    GdkBitmap      **maskp)
{
  int w, h;
  GdkPixmap *pix;
  GdkBitmap *mask;
  GtkWidget *widget;
  GdkGC *mgc;
  GdkColor color;
  
  widget = GTK_WIDGET (frames);
  
  gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &w, &h);

  w -= DEFAULT_INNER_BUTTON_BORDER * 2;
  h -= DEFAULT_INNER_BUTTON_BORDER * 2;

  /* avoid crashing on bizarre icon sizes */
  if (w < 1)
    w = 1;
  if (h < 1)
    h = 1;
  
  pix = gdk_pixmap_new (NULL, w, h, gtk_widget_get_visual (widget)->depth);
  mask = gdk_pixmap_new (NULL, w, h, 1);

  mgc = gdk_gc_new (mask);

  color.pixel = 0;
  gdk_gc_set_foreground (mgc, &color);
  gdk_draw_rectangle (mask, mgc, TRUE, 0, 0, -1, -1);

  color.pixel = 1;
  gdk_gc_set_foreground (mgc, &color);
  draw_control (frames, mask, mgc, control, 0, 0, w, h);

  gdk_gc_unref (mgc);

  draw_control (frames, pix, NULL, control, 0, 0, w, h);

  *pixmapp = pix;
  *maskp = mask;
}

static void
draw_control_bg (MetaFrames         *frames,
                 MetaUIFrame        *frame,
                 MetaFrameControl    control,
                 MetaFrameGeometry  *fgeom)
{
  GdkRectangle *rect;
  GtkWidget *widget;
  gboolean draw = FALSE;
  
  widget = GTK_WIDGET (frames);

  if (frame == frames->grab_frame)
    {
      switch (frames->grab_status)
        {
        case META_FRAME_STATUS_CLICKING_MENU:
          draw = control == META_FRAME_CONTROL_MENU;
          break;
        case META_FRAME_STATUS_CLICKING_DELETE:
          draw = control == META_FRAME_CONTROL_DELETE;
          break;
        case META_FRAME_STATUS_CLICKING_MAXIMIZE:
          draw = control == META_FRAME_CONTROL_MAXIMIZE;
          break;      
        case META_FRAME_STATUS_CLICKING_MINIMIZE:
          draw = control == META_FRAME_CONTROL_MINIMIZE;
          break;
        default:
          break;
        }
    }

  if (draw)
    {        
      rect = control_rect (control, fgeom);
      
      if (rect == NULL)
        return;
      
      gtk_paint_box (widget->style, frame->window,
                     GTK_STATE_ACTIVE,
                     GTK_SHADOW_IN, NULL,
                     widget, "button",
                     rect->x, rect->y, rect->width, rect->height);
    }
}

gboolean
meta_frames_expose_event            (GtkWidget           *widget,
                                     GdkEventExpose      *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;
  MetaFrameGeometry fgeom;
  MetaFrameFlags flags;
  int width, height;
  GtkBorder inner;
  
  frames = META_FRAMES (widget);
  
  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;

  meta_frames_calc_geometry (frames, frame, &fgeom);
  flags = meta_core_get_frame_flags (gdk_display, frame->xwindow);
  meta_core_get_frame_size (gdk_display, frame->xwindow, &width, &height);
  
  /* Black line around outside to give definition */
  gdk_draw_rectangle (frame->window,
                      widget->style->black_gc,
                      FALSE,
                      0, 0, width - 1, height - 1);

  /* Light GC on top/left edges */
  gdk_draw_line (frame->window,
                 widget->style->light_gc[GTK_STATE_NORMAL],
                 1, 1,
                 1, height - 2);
  gdk_draw_line (frame->window,
                 widget->style->light_gc[GTK_STATE_NORMAL],
                 1, 1,
                 width - 2, 1);
  /* Dark on bottom/right */
  gdk_draw_line (frame->window,
                 widget->style->dark_gc[GTK_STATE_NORMAL],
                 width - 2, 1,
                 width - 2, height - 2);
  gdk_draw_line (frame->window,
                 widget->style->dark_gc[GTK_STATE_NORMAL],
                 1, height - 2,
                 width - 2, height - 2);

  if (flags & META_FRAME_HAS_FOCUS)
    {
      /* Black line around inside while we have focus */

      gdk_draw_rectangle (frame->window,
                          widget->style->black_gc,
                          FALSE,
                          fgeom.left_width - 1,
                          fgeom.top_height - 1,
                          width - fgeom.right_width - fgeom.left_width + 1,
                          height - fgeom.bottom_height - fgeom.top_height + 1);
    }

  if (event->area.y < fgeom.top_height &&
      fgeom.title_rect.width > 0 && fgeom.title_rect.height > 0)
    {
      GdkRectangle clip;
      GdkGC *layout_gc;
      
      clip = fgeom.title_rect;
      clip.x += frames->props->text_border.left;
      clip.width -= frames->props->text_border.left +
        frames->props->text_border.right;

      layout_gc = widget->style->text_gc[GTK_STATE_NORMAL];
      if (flags & META_FRAME_HAS_FOCUS)
        {
          layout_gc = widget->style->text_gc[GTK_STATE_SELECTED];

          /* Draw blue background */
          gdk_draw_rectangle (frame->window,
                              widget->style->base_gc[GTK_STATE_SELECTED],
                              TRUE,
                              fgeom.title_rect.x,
                              fgeom.title_rect.y,
                              fgeom.title_rect.width,
                              fgeom.title_rect.height);
        }

      if (frame->layout)
        {
          gdk_gc_set_clip_rectangle (layout_gc, &clip);
          gdk_draw_layout (frame->window,
                           layout_gc,
                           fgeom.title_rect.x + frames->props->text_border.left,
                           fgeom.title_rect.y + frames->props->text_border.top,
                           frame->layout);
          gdk_gc_set_clip_rectangle (layout_gc, NULL);
        }
    }

  inner = frames->props->inner_button_border;
  
  if (fgeom.close_rect.width > 0 && fgeom.close_rect.height > 0)
    {
      draw_control_bg (frames, frame, META_FRAME_CONTROL_DELETE, &fgeom);

      draw_control (frames, frame->window,
                    NULL,
                    META_FRAME_CONTROL_DELETE,
                    fgeom.close_rect.x + inner.left,
                    fgeom.close_rect.y + inner.top,
                    fgeom.close_rect.width - inner.right - inner.left,
                    fgeom.close_rect.height - inner.bottom - inner.top);
    }

  if (fgeom.max_rect.width > 0 && fgeom.max_rect.height > 0)
    {
      draw_control_bg (frames, frame, META_FRAME_CONTROL_MAXIMIZE, &fgeom);
      
      draw_control (frames, frame->window,                    
                    NULL,
                    META_FRAME_CONTROL_MAXIMIZE,
                    fgeom.max_rect.x + inner.left,
                    fgeom.max_rect.y + inner.top,
                    fgeom.max_rect.width - inner.left - inner.right,
                    fgeom.max_rect.height - inner.top - inner.bottom);
    }

  if (fgeom.min_rect.width > 0 && fgeom.min_rect.height > 0)
    {
      draw_control_bg (frames, frame, META_FRAME_CONTROL_MINIMIZE, &fgeom);

      draw_control (frames, frame->window,
                    NULL,
                    META_FRAME_CONTROL_MINIMIZE,
                    fgeom.min_rect.x + inner.left,
                    fgeom.min_rect.y + inner.top,
                    fgeom.min_rect.width - inner.left - inner.right,
                    fgeom.min_rect.height - inner.top - inner.bottom);
    }
  
  if (fgeom.spacer_rect.width > 0 && fgeom.spacer_rect.height > 0)
    {
      gtk_paint_vline (widget->style,
                       frame->window,
                       GTK_STATE_NORMAL,
                       &event->area,
                       widget,
                       "metacity_frame_spacer",
                       fgeom.spacer_rect.y,
                       fgeom.spacer_rect.y + fgeom.spacer_rect.height,
                       fgeom.spacer_rect.x + fgeom.spacer_rect.width / 2);
    }

  if (fgeom.menu_rect.width > 0 && fgeom.menu_rect.height > 0)
    {
      int x, y;
#define ARROW_WIDTH 7
#define ARROW_HEIGHT 5
      
      draw_control_bg (frames, frame, META_FRAME_CONTROL_MENU, &fgeom);
      
      x = fgeom.menu_rect.x;
      y = fgeom.menu_rect.y;
      x += (fgeom.menu_rect.width - ARROW_WIDTH) / 2;
      y += (fgeom.menu_rect.height - ARROW_HEIGHT) / 2;

      gtk_paint_arrow (widget->style,
                       frame->window,
                       GTK_STATE_NORMAL,
                       GTK_SHADOW_OUT,
                       &event->area,
                       widget,
                       "metacity_menu_button",
                       GTK_ARROW_DOWN,
                       TRUE,
                       x, y, ARROW_WIDTH, ARROW_HEIGHT);
    }
  
  return TRUE;
}

gboolean
meta_frames_key_press_event         (GtkWidget           *widget,
                                     GdkEventKey         *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;

  frames = META_FRAMES (widget);

  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;

  return TRUE;
}

gboolean
meta_frames_key_release_event       (GtkWidget           *widget,
                                     GdkEventKey         *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;

  frames = META_FRAMES (widget);

  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;

  return TRUE;
}

gboolean
meta_frames_enter_notify_event      (GtkWidget           *widget,
                                     GdkEventCrossing    *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;

  frames = META_FRAMES (widget);

  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;

  return TRUE;
}

gboolean
meta_frames_leave_notify_event      (GtkWidget           *widget,
                                     GdkEventCrossing    *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;

  frames = META_FRAMES (widget);

  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;

  return TRUE;
}

gboolean
meta_frames_configure_event         (GtkWidget           *widget,
                                     GdkEventConfigure   *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;

  frames = META_FRAMES (widget);

  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;

  return TRUE;
}

gboolean
meta_frames_focus_in_event          (GtkWidget           *widget,
                                     GdkEventFocus       *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;

  frames = META_FRAMES (widget);

  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;

  return TRUE;
}

gboolean
meta_frames_focus_out_event         (GtkWidget           *widget,
                                     GdkEventFocus       *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;

  frames = META_FRAMES (widget);

  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;

  return TRUE;
}

gboolean
meta_frames_map_event               (GtkWidget           *widget,
                                     GdkEventAny         *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;

  frames = META_FRAMES (widget);

  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;

  return TRUE;
}

gboolean
meta_frames_unmap_event             (GtkWidget           *widget,
                                     GdkEventAny         *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;

  frames = META_FRAMES (widget);

  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;

  if (frames->grab_frame == frame)
    meta_frames_end_grab (frames, GDK_CURRENT_TIME);
  
  return TRUE;
}

gboolean
meta_frames_property_notify_event   (GtkWidget           *widget,
                                     GdkEventProperty    *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;

  frames = META_FRAMES (widget);

  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;

  return TRUE;
}

gboolean
meta_frames_client_event            (GtkWidget           *widget,
                                     GdkEventClient      *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;

  frames = META_FRAMES (widget);

  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;

  return TRUE;
}

gboolean
meta_frames_window_state_event      (GtkWidget           *widget,
                                     GdkEventWindowState *event)
{
  MetaUIFrame *frame;
  MetaFrames *frames;

  frames = META_FRAMES (widget);

  frame = meta_frames_lookup_window (frames, GDK_WINDOW_XID (event->window));
  if (frame == NULL)
    return FALSE;

  return TRUE;
}




static GdkRectangle*
control_rect (MetaFrameControl control,
              MetaFrameGeometry *fgeom)
{
  GdkRectangle *rect;
  
  rect = NULL;
  switch (control)
    {
    case META_FRAME_CONTROL_TITLE:
      rect = &fgeom->title_rect;
      break;
    case META_FRAME_CONTROL_DELETE:
      rect = &fgeom->close_rect;
      break;
    case META_FRAME_CONTROL_MENU:
      rect = &fgeom->menu_rect;
      break;
    case META_FRAME_CONTROL_MINIMIZE:
      rect = &fgeom->min_rect;
      break;
    case META_FRAME_CONTROL_MAXIMIZE:
      rect = &fgeom->max_rect;
      break;
    case META_FRAME_CONTROL_RESIZE_SE:
      break;
    case META_FRAME_CONTROL_RESIZE_S:
      break;
    case META_FRAME_CONTROL_RESIZE_SW:
      break;
    case META_FRAME_CONTROL_RESIZE_N:
      break;
    case META_FRAME_CONTROL_RESIZE_NE:
      break;
    case META_FRAME_CONTROL_RESIZE_NW:
      break;
    case META_FRAME_CONTROL_RESIZE_W:
      break;
    case META_FRAME_CONTROL_RESIZE_E:
      break;
    case META_FRAME_CONTROL_NONE:
      break;
    }

  return rect;
}

#define POINT_IN_RECT(xcoord, ycoord, rect) \
 ((xcoord) >= (rect).x &&                   \
  (xcoord) <  ((rect).x + (rect).width) &&  \
  (ycoord) >= (rect).y &&                   \
  (ycoord) <  ((rect).y + (rect).height))

#define RESIZE_EXTENDS 10
static MetaFrameControl
get_control (MetaFrames *frames,
             MetaUIFrame *frame,
             int x, int y)
{
  MetaFrameGeometry fgeom;
  
  meta_frames_calc_geometry (frames, frame, &fgeom);
  
  if (POINT_IN_RECT (x, y, fgeom.close_rect))
    return META_FRAME_CONTROL_DELETE;

  if (POINT_IN_RECT (x, y, fgeom.min_rect))
    return META_FRAME_CONTROL_MINIMIZE;

  if (POINT_IN_RECT (x, y, fgeom.max_rect))
    return META_FRAME_CONTROL_MAXIMIZE;

  if (POINT_IN_RECT (x, y, fgeom.menu_rect))
    return META_FRAME_CONTROL_MENU;
  
  if (POINT_IN_RECT (x, y, fgeom.title_rect))
    return META_FRAME_CONTROL_TITLE;
  
  if (y > (fgeom.height - fgeom.bottom_height - RESIZE_EXTENDS) &&
      x > (fgeom.width - fgeom.right_width - RESIZE_EXTENDS))
    return META_FRAME_CONTROL_RESIZE_SE;
  
  return META_FRAME_CONTROL_NONE;
}


















#if 0

static void
frame_query_root_pointer (MetaUIFrame *frame,
                          int *x, int *y)
{
  Window root_return, child_return;
  int root_x_return, root_y_return;
  int win_x_return, win_y_return;
  unsigned int mask_return;

  XQueryPointer (frame->window->display->xdisplay,
                 frame->xwindow,
                 &root_return,
                 &child_return,
                 &root_x_return,
                 &root_y_return,
                 &win_x_return,
                 &win_y_return,
                 &mask_return);

  if (x)
    *x = root_x_return;
  if (y)
    *y = root_y_return;
}

static void
show_tip_now (MetaUIFrame *frame)
{
  const char *tiptext;

  tiptext = NULL;
  switch (frame->current_control)
    {
    case META_FRAME_CONTROL_TITLE:
      break;
    case META_FRAME_CONTROL_DELETE:
      tiptext = _("Close Window");
      break;
    case META_FRAME_CONTROL_MENU:
      tiptext = _("Menu");
      break;
    case META_FRAME_CONTROL_ICONIFY:
      tiptext = _("Minimize Window");
      break;
    case META_FRAME_CONTROL_MAXIMIZE:
      tiptext = _("Maximize Window");
      break;
    case META_FRAME_CONTROL_RESIZE_SE:
      break;
    case META_FRAME_CONTROL_RESIZE_S:
      break;
    case META_FRAME_CONTROL_RESIZE_SW:
      break;
    case META_FRAME_CONTROL_RESIZE_N:
      break;
    case META_FRAME_CONTROL_RESIZE_NE:
      break;
    case META_FRAME_CONTROL_RESIZE_NW:
      break;
    case META_FRAME_CONTROL_RESIZE_W:
      break;
    case META_FRAME_CONTROL_RESIZE_E:
      break;
    case META_FRAME_CONTROL_NONE:
      break;
    }

  if (tiptext)
    {
      int x, y, width, height;      
      MetaFrameInfo info;

      meta_frame_init_info (frame, &info);
      frame->window->screen->engine->get_control_rect (&info,
                                                       frame->current_control,
                                                       &x, &y, &width, &height,
                                                       frame->theme_data);

      /* Display tip a couple pixels below control */
      meta_screen_show_tip (frame->window->screen,
                            frame->rect.x + x,
                            frame->rect.y + y + height + 2,
                            tiptext);
    }
}

static gboolean
tip_timeout_func (gpointer data)
{
  MetaUIFrame *frame;

  frame = data;

  show_tip_now (frame);

  return FALSE;
}

#define TIP_DELAY 250
static void
queue_tip (MetaUIFrame *frame)
{
  if (frame->tooltip_timeout)
    g_source_remove (frame->tooltip_timeout);

  frame->tooltip_timeout = g_timeout_add (250,
                                          tip_timeout_func,
                                          frame);  
}

static void
clear_tip (MetaUIFrame *frame)
{
  if (frame->tooltip_timeout)
    {
      g_source_remove (frame->tooltip_timeout);
      frame->tooltip_timeout = 0;
    }
  meta_screen_hide_tip (frame->window->screen);
}

static MetaFrameControl
frame_get_control (MetaUIFrame *frame,
                   int x, int y)
{
  MetaFrameInfo info;

  if (x < 0 || y < 0 ||
      x > frame->rect.width || y > frame->rect.height)
    return META_FRAME_CONTROL_NONE;
  
  meta_frame_init_info (frame, &info);
  
  return frame->window->screen->engine->get_control (&info,
                                                     x, y,
                                                     frame->theme_data);
}

static void
update_move (MetaUIFrame *frame,
             int        x,
             int        y)
{
  int dx, dy;
  
  dx = x - frame->grab->start_root_x;
  dy = y - frame->grab->start_root_y;

  frame->window->user_has_moved = TRUE;
  meta_window_move (frame->window,
                    frame->grab->start_window_x + dx,
                    frame->grab->start_window_y + dy);
}

static void
update_resize_se (MetaUIFrame *frame,
                  int x, int y)
{
  int dx, dy;
  
  dx = x - frame->grab->start_root_x;
  dy = y - frame->grab->start_root_y;

  frame->window->user_has_resized = TRUE;
  meta_window_resize (frame->window,
                      frame->grab->start_window_x + dx,
                      frame->grab->start_window_y + dy);
}

static void
update_current_control (MetaUIFrame *frame,
                        int x_root, int y_root)
{
  MetaFrameControl old;

  if (frame->grab)
    return;
  
  old = frame->current_control;

  frame->current_control = frame_get_control (frame,
                                              x_root - frame->rect.x,
                                              y_root - frame->rect.y);

  if (old != frame->current_control)
    {
      meta_frame_queue_draw (frame);

      if (frame->current_control == META_FRAME_CONTROL_NONE)
        clear_tip (frame);
      else
        queue_tip (frame);
    }
}

static void
grab_action (MetaUIFrame      *frame,
             MetaFrameAction action,
             Time            time)
{
  meta_verbose ("Grabbing action %d\n", action);
  
  frame->grab = g_new0 (MetaFrameActionGrab, 1);
  
  if (XGrabPointer (frame->window->display->xdisplay,
                    frame->xwindow,
                    False,
                    ButtonPressMask | ButtonReleaseMask |
                    PointerMotionMask | PointerMotionHintMask,
                    GrabModeAsync, GrabModeAsync,
                    None,
                    None,
                    time) != GrabSuccess)
    meta_warning ("Grab for frame action failed\n");

  frame->grab->action = action;

  /* display ACTIVE state */
  meta_frame_queue_draw (frame);

  clear_tip (frame);
}

static void
ungrab_action (MetaUIFrame      *frame,
               Time            time)
{
  int x, y;

  meta_verbose ("Ungrabbing action %d\n", frame->grab->action);
  
  XUngrabPointer (frame->window->display->xdisplay,
                  time);
  
  g_free (frame->grab);
  frame->grab = NULL;
  
  frame_query_root_pointer (frame, &x, &y);
  update_current_control (frame, x, y);

  /* undisplay ACTIVE state */
  meta_frame_queue_draw (frame);

  queue_tip (frame);
}

static void
get_menu_items (MetaUIFrame *frame,
                MetaFrameInfo *info,
                MetaMessageWindowMenuOps *ops,
                MetaMessageWindowMenuOps *insensitive)
{
  *ops = 0;
  *insensitive = 0;
  
  if (info->flags & META_FRAME_CONTROL_MAXIMIZE)
    {
      if (frame->window->maximized)
        *ops |= META_MESSAGE_MENU_UNMAXIMIZE;
      else
        *ops |= META_MESSAGE_MENU_MAXIMIZE;
    }

  if (frame->window->shaded)
    *ops |= META_MESSAGE_MENU_UNSHADE;
  else
    *ops |= META_MESSAGE_MENU_SHADE;

  if (frame->window->on_all_workspaces)
    *ops |= META_MESSAGE_MENU_UNSTICK;
  else
    *ops |= META_MESSAGE_MENU_STICK;
  
  *ops |= (META_MESSAGE_MENU_DELETE | META_MESSAGE_MENU_WORKSPACES | META_MESSAGE_MENU_MINIMIZE);

  if (!(info->flags & META_FRAME_CONTROL_ICONIFY))
    *insensitive |= META_MESSAGE_MENU_MINIMIZE;
  
  if (!(info->flags & META_FRAME_CONTROL_DELETE))
    *insensitive |= META_MESSAGE_MENU_DELETE;
}

gboolean
meta_frame_event (MetaUIFrame *frame,
                  XEvent    *event)
{
  switch (event->type)
    {
    case KeyPress:
      break;
    case KeyRelease:
      break;
    case ButtonPress:
      /* you can use button 2 to move a window without raising it */
      if (event->xbutton.button == 1)
        meta_window_raise (frame->window);
      
      update_current_control (frame,
                              event->xbutton.x_root,
                              event->xbutton.y_root);
      
      if (frame->grab == NULL)
        {
          MetaFrameControl control;
          control = frame->current_control;

          if (control == META_FRAME_CONTROL_TITLE &&
              event->xbutton.button == 1 &&
              meta_display_is_double_click (frame->window->display))
            {
              meta_verbose ("Double click on title\n");

              /* FIXME this catches double click that starts elsewhere
               * with the second click on title, maybe no one will
               * ever notice
               */

              if (frame->window->shaded)
                meta_window_unshade (frame->window);
              else
                meta_window_shade (frame->window);
            }
          else if (((control == META_FRAME_CONTROL_TITLE ||
                     control == META_FRAME_CONTROL_NONE) &&
                    event->xbutton.button == 1) ||
                   event->xbutton.button == 2)
            {
              meta_verbose ("Begin move on %s\n",
                            frame->window->desc);
              grab_action (frame, META_FRAME_ACTION_MOVING,
                           event->xbutton.time);
              frame->grab->start_root_x = event->xbutton.x_root;
              frame->grab->start_root_y = event->xbutton.y_root;
              /* pos of client in root coords */
              frame->grab->start_window_x =
                frame->rect.x + frame->window->rect.x;
              frame->grab->start_window_y =
                frame->rect.y + frame->window->rect.y;
              frame->grab->start_button = event->xbutton.button; 
            }
          else if (control == META_FRAME_CONTROL_DELETE &&
                   event->xbutton.button == 1)
            {
              meta_verbose ("Close control clicked on %s\n",
                            frame->window->desc);
              grab_action (frame, META_FRAME_ACTION_DELETING,
                           event->xbutton.time);
              frame->grab->start_button = event->xbutton.button;
            }
          else if (control == META_FRAME_CONTROL_MAXIMIZE &&
                   event->xbutton.button == 1)
            {
              meta_verbose ("Maximize control clicked on %s\n",
                            frame->window->desc);
              grab_action (frame, META_FRAME_ACTION_TOGGLING_MAXIMIZE,
                           event->xbutton.time);
              frame->grab->start_button = event->xbutton.button;
            }
          else if (control == META_FRAME_CONTROL_RESIZE_SE &&
                   event->xbutton.button == 1)
            {
              meta_verbose ("Resize control clicked on %s\n",
                            frame->window->desc);
              grab_action (frame, META_FRAME_ACTION_RESIZING_SE,
                           event->xbutton.time);
              frame->grab->start_root_x = event->xbutton.x_root;
              frame->grab->start_root_y = event->xbutton.y_root;
              frame->grab->start_window_x = frame->window->rect.width;
              frame->grab->start_window_y = frame->window->rect.height;
              frame->grab->start_button = event->xbutton.button;
            }
          else if (control == META_FRAME_CONTROL_MENU &&
                   event->xbutton.button == 1)
            {
              int x, y, width, height;      
              MetaFrameInfo info;
              MetaMessageWindowMenuOps ops;
              MetaMessageWindowMenuOps insensitive;
              
              meta_verbose ("Menu control clicked on %s\n",
                            frame->window->desc);
              
              meta_frame_init_info (frame, &info);
              frame->window->screen->engine->get_control_rect (&info,
                                                               META_FRAME_CONTROL_MENU,
                                                               &x, &y, &width, &height,
                                                               frame->theme_data);

              /* Let the menu get a grab. The user could release button
               * before the menu gets the grab, in which case the
               * menu gets somewhat confused, but it's not that
               * disastrous.
               */
              XUngrabPointer (frame->window->display->xdisplay,
                              event->xbutton.time);

              get_menu_items (frame, &info, &ops, &insensitive);
              
              meta_ui_slave_show_window_menu (frame->window->screen->uislave,
                                              frame->window,
                                              frame->rect.x + x,
                                              frame->rect.y + y + height,
                                              event->xbutton.button,
                                              ops, insensitive,
                                              event->xbutton.time);      
            }
        }
      break;
    case ButtonRelease:
      if (frame->grab)
        meta_debug_spew ("Here! grab %p action %d buttons %d %d\n",
                         frame->grab, frame->grab->action, frame->grab->start_button, event->xbutton.button);
      if (frame->grab &&
          event->xbutton.button == frame->grab->start_button)
        {
          switch (frame->grab->action)
            {
            case META_FRAME_ACTION_MOVING:
              update_move (frame, event->xbutton.x_root, event->xbutton.y_root);
              ungrab_action (frame, event->xbutton.time);
              update_current_control (frame,
                                      event->xbutton.x_root, event->xbutton.y_root);
              break;
              
            case META_FRAME_ACTION_RESIZING_SE:
              update_resize_se (frame, event->xbutton.x_root, event->xbutton.y_root);
              ungrab_action (frame, event->xbutton.time);
              update_current_control (frame,
                                      event->xbutton.x_root, event->xbutton.y_root);
              break;

            case META_FRAME_ACTION_DELETING:
              /* Must ungrab before getting "real" control position */
              ungrab_action (frame, event->xbutton.time);
              update_current_control (frame,
                                      event->xbutton.x_root,
                                      event->xbutton.y_root);
              /* delete if we're still over the button */
              if (frame->current_control == META_FRAME_CONTROL_DELETE)
                meta_window_delete (frame->window, event->xbutton.time);
              break;
            case META_FRAME_ACTION_TOGGLING_MAXIMIZE:
              /* Must ungrab before getting "real" control position */
              ungrab_action (frame, event->xbutton.time);
              update_current_control (frame,
                                      event->xbutton.x_root,
                                      event->xbutton.y_root);
              /* delete if we're still over the button */
              if (frame->current_control == META_FRAME_CONTROL_MAXIMIZE)
                {
                  if (frame->window->maximized)
                    meta_window_unmaximize (frame->window);
                  else
                    meta_window_maximize (frame->window);
                }
              break;
            default:
              meta_warning ("Unhandled action in button release\n");
              break;
            }
        }
      break;
    case MotionNotify:
      {
        int x, y;

        frame_query_root_pointer (frame, &x, &y);
        if (frame->grab)
          {
            switch (frame->grab->action)
              {
              case META_FRAME_ACTION_MOVING:
                update_move (frame, x, y);
                break;
                
              case META_FRAME_ACTION_RESIZING_SE:
                update_resize_se (frame, x, y);
                break;
                
              case META_FRAME_ACTION_NONE:
                
                break;
              default:
                break;
              }
          }
        else
          {
            update_current_control (frame, x, y);
          }
        }
      break;
    case EnterNotify:
      /* We handle it here if a decorated window
       * is involved, otherwise we handle it in display.c
       */
      /* do this even if window->has_focus to avoid races */
      meta_window_focus (frame->window,
                         event->xcrossing.time);
      break;
    case LeaveNotify:
      update_current_control (frame, -1, -1);
      break;
    case FocusIn:
      break;
    case FocusOut:
      break;
    case KeymapNotify:
      break;
    case Expose:
      {
        gboolean title_was_exposed = frame->title_exposed;
        meta_frame_queue_draw (frame);
        if (!title_was_exposed &&
            event->xexpose.y > frame->child_y)
          frame->title_exposed = FALSE;
      }
      break;
    case GraphicsExpose:
      break;
    case NoExpose:
      break;
    case VisibilityNotify:
      break;
    case CreateNotify:
      break;
    case DestroyNotify:
      {
        MetaDisplay *display;
        
        meta_warning ("Unexpected destruction of frame 0x%lx, not sure if this should silently fail or be considered a bug\n", frame->xwindow);
        display = frame->window->display;
        meta_error_trap_push (display);
        meta_window_destroy_frame (frame->window);
        meta_error_trap_pop (display);
        return TRUE;
      }
      break;
    case UnmapNotify:
      if (frame->grab)
        ungrab_action (frame, CurrentTime);
      break;
    case MapNotify:
      if (frame->grab)
        ungrab_action (frame, CurrentTime);
      break;
    case MapRequest:
      break;
    case ReparentNotify:
      break;
    case ConfigureNotify:
      break;
    case ConfigureRequest:
      {
        /* This is a request from the UISlave, or else a client
         * that's completely out of line. We call
         * meta_window_move_resize() using this information.
         */

      }
      break;
    case GravityNotify:
      break;
    case ResizeRequest:
      break;
    case CirculateNotify:
      break;
    case CirculateRequest:
      break;
    case PropertyNotify:
      break;
    case SelectionClear:
      break;
    case SelectionRequest:
      break;
    case SelectionNotify:
      break;
    case ColormapNotify:
      break;
    case ClientMessage:
      break;
    case MappingNotify:
      break;
    default:
      break;
    }

  return FALSE;
}
#endif