File: main.py

package info (click to toggle)
wxglade 1%3A1.1.1%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,592 kB
  • sloc: python: 30,644; javascript: 740; makefile: 169; cpp: 99; perl: 90; lisp: 62; xml: 61; sh: 3
file content (1608 lines) | stat: -rw-r--r-- 70,302 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
"""\
Main wxGlade module: defines wxGladeFrame which contains the buttons to add
widgets and initializes all the stuff (tree, frame_property, etc.)

@copyright: 2002-2007 Alberto Griggio
@copyright: 2011-2016 Carsten Grohmann
@copyright: 2016-2021 Dietmar Schwertberger
@license: MIT (see LICENSE.txt) - THIS PROGRAM COMES WITH NO WARRANTY
"""

# import general python modules
import logging, os, os.path, sys, math, time, functools
import wx
from xml.sax import SAXParseException

# import project modules
import application
import common, config, compat, misc, history
import new_properties as np
import preferencesdialog, msgdialog, bugdialog, about
import log
import template
from tree import WidgetTree
from xml_parse import XmlWidgetBuilder, ProgressXmlWidgetBuilder, XmlParsingError



class FileDropTarget(wx.FileDropTarget):
    # file drop target that checks first whether the property panel is the target
    def __init__(self, parent):
        wx.FileDropTarget.__init__(self)
        self.parent = parent

    if config.debugging:
        def OnDragOver(self, x, y, defResult):
            x0,y0 = self.parent.GetClientAreaOrigin()
            screen_xy = self.parent.ClientToScreen( (x-x0,y-y0) )
            ctrl = wx.FindWindowAtPoint( screen_xy )
            print("DragOver", x0,y0, x-x0,y-y0, ctrl)
            return wx.FileDropTarget.OnDragOver(self, x,y, defResult)

    def OnDropFiles(self, x, y, filenames):
        if len(filenames) > 1:
            wx.MessageBox( _("Please only drop one file at a time"), "wxGlade", wx.ICON_ERROR )
            return False
        if not filenames or not os.path.exists(filenames[0]): return False
        # find control under drop point; this does also work if a dialog is open
        x0,y0 = self.parent.GetClientAreaOrigin()
        screen_xy = self.parent.ClientToScreen( (x-x0,y-y0) )
        ctrl = c = wx.FindWindowAtPoint( screen_xy )
        # go up the hierarchy and find a widget with an 'on_drop_files' method
        while c:
            if hasattr(c, "on_drop_files"):
                handled = c.on_drop_files(screen_xy, ctrl, filenames)
                if handled: return True
            if c is self.parent.property_panel: break
            if isinstance(c, wx.Dialog): return False  # when a dialog is open, don't open wxg or xrc files
            c = c.GetParent()

        # not handled by a control; try to open .wxg or .XRC file
        if not self.parent.ask_save(): return False

        path = filenames[0]
        if os.path.splitext(path)[1].upper() == ".XRC":
            self.parent.import_xrc(path, ask_save=False)
        else:
            self.parent._open_app(path)
        self.parent.cur_dir = os.path.dirname(path)
        return True


class wxGladePropertyPanel(wx.Panel):
    "Panel used to display the Properties of the various widgets"
    def __init__(self, parent):
        wx.Panel.__init__( self, parent, -1, name='PropertyPanel' )
        self.SetBackgroundColour( compat.wx_SystemSettings_GetColour(wx.SYS_COLOUR_BTNFACE) )

        self.current_widget = None        # instance currently being edited
        self.next_widget = None           # the next one, will only be edited after a small delay

        self.pagenames = None

        sizer = wx.BoxSizer(wx.VERTICAL)
        self.heading = wx.TextCtrl(self, style=wx.TE_READONLY)
        sizer.Add(self.heading, 0, wx.EXPAND, 0)
        self.notebook = wx.Notebook(self)
        self.notebook.Bind(wx.EVT_SIZE, self.on_notebook_size)

        sizer.Add(self.notebook, 1, wx.EXPAND, 0)

        # for GTK3: add a panel to determine page size
        self._dummy_page = p = wx.Panel(self.notebook)
        self.notebook.AddPage(p, "panel")
        self._notebook_decoration_size = None
        p.Bind(wx.EVT_SIZE, self.on_panel_size)
        self._edit_triggered = False  # for MSW to avoid multiple calls to set_widget

        self.SetSizer(sizer)
        self.Layout()

    def on_drop_files(self, screen_xy, ctrl, filenames):
        if not self.current_widget: return False
        for p_name in self.current_widget.PROPERTIES:
            if p_name[0].isupper(): continue
            prop = self.current_widget.properties.get(p_name)
            if not prop or not hasattr(prop, "on_drop_file"): continue
            if ( hasattr(prop, "label_ctrl") and prop.label_ctrl.ScreenRect.Contains( screen_xy ) and
                 prop.label_ctrl.IsShownOnScreen() ) or prop.has_control(ctrl):
                return prop.on_drop_file(filenames[0])
        return False

    ####################################################################################################################
    # new editor interface
    def set_widget(self, widget, force=False):
        if widget is self.current_widget and not force:
            # just update
            return
        self.next_widget = widget
        if self.current_widget:
            # this might not be executed if there was an error during creation of the property editors
            for editor in self.current_widget.properties.values():
                editor.destroy_editor()
            self.current_widget = None   # delete the reference
        wx.CallLater( 150, self.edit_properties, widget )

    def edit_properties(self, edit_widget):
        # this will be called with a delay
        if edit_widget is not self.next_widget:
            # wait for another call...
            return
        if self._notebook_decoration_size is None:
            # try again later
            wx.CallLater( 150, self.edit_properties, edit_widget )
            return

        self.current_widget = None
        self.create_editor(edit_widget)
        # this code might not be reached in case of an error
        self.current_widget = edit_widget
        if edit_widget:
            # XXX set status bar
            klass = edit_widget.get_prop_value("class", edit_widget.WX_CLASS)
            self.heading.SetValue( _('Properties - %s - <%s>:') % (klass, edit_widget.name) )
        else:
            self.heading.SetValue( _('Properties') )

    def create_editor(self, edit_widget):
        # fill the frame with a notebook of property editors

        if not self.notebook: return  # already deleted
        self.current_widget_class = edit_widget.__class__
        if wx.Platform != "__WXMSW__" :
            focus_before = self.FindFocus()
        self.notebook.Hide()

        # remember the notebook page to be selected
        if self._dummy_page or not self.pagenames:
            self._dummy_page = None
            select_page = None
        else:
            selection = self.notebook.GetSelection()
            select_page = self.pagenames[selection]  if selection!=-1  else None

        # clear notebook pages
        #self.notebook.DeleteAllPages()  # deletes also the windows on the pages
        while self.notebook.PageCount:
            self.notebook.DeletePage(self.notebook.PageCount-1)

        self.pagenames = pagenames = []
        self.sizers = []
        if not edit_widget: return
        current_page = current_sizer = current_pagename = None
        property_instance = None
        for prop in edit_widget.PROPERTIES:
            if prop[0].isupper():
                # end previous page
                if current_page is not None:
                    self.end_page(current_page, current_sizer, current_pagename)
                    current_page = None

                # start new page
                current_pagename = prop
                if prop=="Layout" and not edit_widget._has_layout:continue
                if prop=="Events" and edit_widget.events is None: continue

                current_page = self.start_page(prop)
                current_sizer = wx.BoxSizer(wx.VERTICAL)
                self.sizers.append(current_sizer)

                self.pagenames.append(prop)

                continue

            if current_pagename=="Layout" and not edit_widget._has_layout: continue

            # a property or None
            property_instance_ = edit_widget.properties.get(prop)
            if property_instance_ is not None:
                property_instance = property_instance_
                property_instance.create_editor(current_page, current_sizer)

        if current_page is not None:
            self.end_page(current_page, current_sizer, current_pagename)

        if select_page and select_page in pagenames:
            index = pagenames.index(select_page)
            self.notebook.SetSelection(index)
        else:
            self.notebook.SetSelection(0)

        self.notebook.Show()

        if wx.Platform != "__WXMSW__" and (focus_before is common.app_tree or focus_before is None):
            common.app_tree.SetFocus()

    def start_page(self, name):
        # create a ScrolledWindow and a Panel; with only ScrolledWindow, scrolling on gtk 3 does not work
        scrolled = wx.ScrolledWindow( self.notebook, name=name)
        panel = wx.Panel(scrolled, name="%s properties"%name)
        if wx.VERSION[0]<3:
            panel.SetBackgroundColour(scrolled.GetBackgroundColour())
        return panel

    def end_page(self, panel, sizer, header, select=False):
        sizer.AddSpacer(30)
        panel.SetAutoLayout(1)
        panel.SetSizer(sizer)
        sizer.Layout()
        sizer.Fit(panel)

        scrolled = panel.GetParent()
        self.notebook.AddPage(scrolled, _(header),select=select)
        self._set_page_size(scrolled)

    def _set_page_size(self, scrolled):
        # set ScrolledWindow and Panel to available size; enable scrolling, if required
        # gets available size for notebook pages
        if not self.pagenames: return  # in initialization
        ws, hs = self.notebook.GetSize()
        ws -= self._notebook_decoration_size[0]
        hs -= self._notebook_decoration_size[1]
        w_scrollbar = wx.SystemSettings.GetMetric(wx.SYS_VSCROLL_X)  # width a of a scrollbar

        panel = [w for w in list(scrolled.GetChildren()) if isinstance(w, wx.Panel)][0]
        szr = panel.GetSizer()
        if not szr: return
        wm, hm = szr.GetMinSize()
        if hs<hm:
            # best size is smaller than the available height -> enable scrolling
            scrolled.SetScrollbars(1, 5, 1, int(math.ceil(hm/5.0)))
            panel.SetSize( (ws-w_scrollbar, hm) )
        else:
            panel.SetSize( (ws, hs) )

    def on_notebook_size(self, event):
        # calculate available size for pages
        if self._notebook_decoration_size:
            for p in range( self.notebook.GetPageCount() ):
                self._set_page_size( self.notebook.GetPage(p) )
        if event: event.Skip()

    def on_panel_size(self, event):
        # when the dummy panel receives a size event, we know that things are ready to calculate the notebook pages size
        # calculate decoration size from the dummy panel that was added initially
        # this might be called several times during initialization
        # on Windows the sizes change, it's fine to take the last value; actually the correct value is required for GTK
        if event.GetSize() != (0,0):
            dummy_panel = self.notebook.GetPage(0)
            wp, hp = dummy_panel.GetSize()    # page/panel size
            wn, hn = self.notebook.GetSize()  # notebook size
            self._notebook_decoration_size = (wn-wp, hn-hp)
            if wx.Platform == '__WXMSW__' and not self._edit_triggered:
                # as the dummy is not deleted any more due to GTK, we need to display something
                wx.CallLater(150, self.set_widget, common.root)
        else:
            # Mac OS: inital event on creation
            event.Skip()


# don't use any more for application; causes crashes on Cent OS 7; still used when testing
class wxGladeArtProvider(wx.ArtProvider):
    def CreateBitmap(self, artid, client, size):
        if wx.Platform == '__WXGTK__' and artid == wx.ART_FOLDER:
            return wx.Bitmap(os.path.join(config.icons_path, 'closed_folder.png'))
        return wx.NullBitmap


class wxGladePalettePanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        common.palette = self # for building the buttons
        self.SetBackgroundColour( compat.wx_SystemSettings_GetColour(wx.SYS_COLOUR_BTNFACE) )

        # load the available code generators
        all_widgets = common.init_codegen()
        if not config.use_gui: return
        self.all_togglebuttons = []  # used by reset_togglebuttons

        # for keyboard navigation:
        self._id_to_coordinate = {}
        self._ids_by_row = []
        self._section_to_row = {}

        # build the palette for all_widgets
        sizer = wx.FlexGridSizer(0, 2, 0, 0)
        maxlen = max([len(all_widgets[sect]) for sect in all_widgets])  # the maximum number of buttons in a section
        for row, section in enumerate(all_widgets):
            self._section_to_row[section] = row
            self._ids_by_row.append([])
            if section:
                label = wx.StaticText(self, -1, "%s:" % section.replace('&', '&&'))
                sizer.Add( label, 1, wx.ALIGN_CENTER_VERTICAL | wx.LEFT, 2 )
            bsizer = wx.BoxSizer()
            for col, button in enumerate(all_widgets[section]):
                self._ids_by_row[-1].append(button.Id)
                self._id_to_coordinate[button.Id] = (row,col)
                bsizer.Add(button, flag=wx.ALL, border=1)
                if isinstance(button, wx.ToggleButton):
                    self.all_togglebuttons.append(button)
            sizer.Add(bsizer)
        self.SetSizer(sizer)
        # on platforms other than Windows, we'll set the ToggleButton background colour to indicate the selection
        if wx.Platform == "__WXMSW__":
            self._highlight_colour = None
        else:
            self._highlight_colour = wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT)
        self.Bind(wx.EVT_CHAR_HOOK, self.on_char)

    def reset_togglebuttons(self, keep=None):
        # un-toggle all buttons except keep
        for button in self.all_togglebuttons:
            if keep is not None and button is keep:
                if self._highlight_colour:
                    button.SetBackgroundColour(self._highlight_colour)
                continue
            if self._highlight_colour and button.GetBackgroundColour()==self._highlight_colour:
                button.SetBackgroundColour(wx.NullColour)
            if button.GetValue(): button.SetValue(False)

    def on_char(self, event):
        key = (event.GetKeyCode(), event.GetModifiers())    # modifiers: 1,2,4 for Alt, Ctrl, Shift
        if key[1]: return event.Skip()

        focused = self.FindFocus()
        if not focused or not focused.Id in self._id_to_coordinate:
            return event.Skip()

        row, col = self._id_to_coordinate[focused.Id]
        new_row = new_col = None
        if key[0]==wx.WXK_UP:
            if row>0: new_row = row-1
        elif key[0]==wx.WXK_DOWN:
            if row < len(self._ids_by_row)-1: new_row = row+1
        elif key[0]==wx.WXK_LEFT:
            if col>0: new_col = col-1
        elif key[0]==wx.WXK_RIGHT:
            if col < len(self._ids_by_row[row])-1: new_col = col+1
        elif key[0]==wx.WXK_HOME:
            new_col = 0
        elif key[0]==wx.WXK_END:
            new_col = len(self._ids_by_row[row])-1
        elif key[0]==wx.WXK_PAGEUP:
            new_row = 0
        elif key[0]==wx.WXK_PAGEDOWN:
            new_row = len(self._ids_by_row)-1
        elif (ord("A") <= key[0] <= ord("Z")) and chr(key[0]) in misc.palette_hotkeys:
            section = misc.palette_hotkeys[chr(key[0])]
            new_row = self._section_to_row[section]
            new_col = 0
        else:
            return event.Skip()

        if new_row is None and new_col is None:
            # limits hit
            wx.Bell()
        else:
            if new_col is None: new_col = min(col, len(self._ids_by_row[new_row])-1)
            if new_row is None: new_row = row
            focus = self.FindWindowById(self._ids_by_row[new_row][new_col])
            if focus: focus.SetFocus()

import shell_frame
class ShellFrame(shell_frame.ShellFrame):
    def on_btn_assign(self, event):
        # insert a variable assignment
        widget = misc.focused_widget
        if not widget:
            event.Skip()
            return
        path = widget.get_path()
        command = 'widget = common.root.find_widget_from_path("%s")\r\n'%path
        #self.shell.push(command) # or .write ?
        self.shell.write(command)


class wxGladeFrame(wx.Frame):
    "Main frame of wxGlade"
    def __init__(self):
        version = config.version
        pos, size, layout = self.init_layout_settings()
        wx.Frame.__init__(self, None, -1, "wxGlade v%s" % version, pos=pos, size=size,
                          style=wx.DEFAULT_FRAME_STYLE, name='MainFrame')

        common.main = self
        self._set_icon()
        self.create_menu()
        self.create_toolbar()

        style = wx.SP_3D | wx.SP_LIVE_UPDATE
        self.splitter1 = wx.SplitterWindow(self, style=style)
        self.splitter2 = wx.SplitterWindow(self.splitter1, style=style)
        self.palette = wxGladePalettePanel(self.splitter2)

        # create the property and the tree frame
        common.property_panel = self.property_panel = wxGladePropertyPanel(self.splitter2)
        common.root = app = application.Application()
        common.app_tree = self.tree = WidgetTree(self.splitter1, app)

        self.splitter1.SplitVertically(self.splitter2, self.tree)
        self.splitter2.SplitHorizontally(self.palette, self.property_panel)

        self.switch_layout(layout, initial=True)

        # last visited directory, used on GTK for wxFileDialog
        self.cur_dir = config.preferences.open_save_path

        # set a drop target for us...
        self._droptarget = FileDropTarget(self)
        self.SetDropTarget(self._droptarget)

        self.create_statusbar()  # create statusbar for display of messages

        self.Show()

        w, h = self.GetSize()
        if w>size[0] or h>size[1]:
            # can happen on e.g. Linux due to menu/tool/status bar
            new_size = ( 2*size[0] - w, 2*size[1] - h )
            self.SetSize(new_size)

        #misc.set_focused_widget(common.root)
        self.Bind(wx.EVT_CLOSE, self.on_close)

        # disable autosave checks during unittests
        if config.testing: return
        self.init_autosave()
        self.check_autosaved()

        self.Bind(wx.EVT_CHAR_HOOK, self.on_char_hook)
        if config.debugging:
            self.splitter1.Bind(wx.EVT_SPLITTER_SASH_POS_CHANGED, self.on_sash)
            self.splitter2.Bind(wx.EVT_SPLITTER_SASH_POS_CHANGED, self.on_sash)

    def on_sash(self, event):
        # XXX not yet used, but it could be used to re-format the palette panel
        layout = self.layout_settings["layout"]
        if layout==0:
            size = (self.splitter1.GetSashPosition(), self.splitter2.GetSashPosition())
        elif layout==1:
            size = (self.splitter2.GetSashPosition(), self.splitter1.GetSashPosition())
        elif layout==2:
            size = (self.GetClientSize()[0], self.splitter2.GetSashPosition())

    def on_char_hook(self, event):
        # bound to EVT_CHAR_HOOK
        focus = parent = self.FindFocus()
        grid = None  # will be set if a grid or a grid's child is focused
        window_type = None
        while parent:
            # go up and identify parent: Palette, Property or Tree
            if isinstance(parent, wx.grid.Grid):
                grid = parent
            if parent is self.palette:
                window_type = "palette"
            elif parent is self.tree:
                window_type = "tree"
            elif parent is self.property_panel:
                window_type = "properties"
            if window_type: break
            parent = parent.GetParent()

        # forward to specific controls / properties? (on wx 2.8 installing EVT_CHAR_HOOK on controls does not work)
        if window_type=="properties" and grid and grid.Name!="grid":
            # forward event to grid property?
            if misc.focused_widget.properties[grid.Name].on_char(event):
                return
        if window_type=="tree":
            if common.app_tree.on_char(event):
                return

        # global handler
        misc.handle_key_event(event, window_type)

    def set_widget(self, widget):
        # update redo/repeat tools and menus
        if not common.history: return
        redo_state = (common.history.can_undo, common.history.can_redo, common.history.can_repeat)
        if self._previous_redo_state == redo_state:
            return
        self._menu_undo.Enable(common.history.can_undo)
        self._menu_redo.Enable(common.history.can_redo)
        self._menu_repeat.Enable(common.history.can_repeat)
        if self._tool_redo:
            self._tool_undo.Enable(common.history.can_undo)
            self._tool_redo.Enable(common.history.can_redo)
            self._tool_repeat.Enable(common.history.can_repeat)
            self.toolbar.Realize()
        self._previous_redo_state = redo_state

    # menu and actions #################################################################################################
    def create_menu(self):
        self._previous_redo_state = None
        menu_bar = wx.MenuBar()

        compat.wx_ToolTip_SetDelay(1000)
        compat.wx_ToolTip_SetAutoPop(30000)

        append_menu_item = misc.append_menu_item

        # File menu
        file_menu = wx.Menu(style=wx.MENU_TEAROFF)

        NEW = append_menu_item(file_menu, -1, _("&New\tCtrl+N"), wx.ART_NEW)
        misc.bind_menu_item(self, NEW, self.new_app)

        item = append_menu_item(file_menu, -1, _("New from &Template...\tShift+Ctrl+N"))
        misc.bind_menu_item(self, item, self.new_app_from_template)

        OPEN = append_menu_item(file_menu, -1, _("&Open...\tCtrl+O"), wx.ART_FILE_OPEN)
        misc.bind_menu_item(self, OPEN, self.open_app)

        SAVE = append_menu_item(file_menu, -1, _("&Save\tCtrl+S"), wx.ART_FILE_SAVE)
        misc.bind_menu_item(self, SAVE, self.save_app)

        SAVE_AS = append_menu_item(file_menu, -1, _("Save As..."), wx.ART_FILE_SAVE_AS)
        misc.bind_menu_item(self, SAVE_AS, self.save_app_as)

        item = append_menu_item(file_menu, -1, _("Save As Template..."))
        misc.bind_menu_item(self, item, self.save_app_as_template)

        file_menu.AppendSeparator() # ----------------------------------------------------------------------------------

        GENERATE_CODE = append_menu_item(file_menu, -1, _("&Generate Code\tCtrl+G"), wx.ART_EXECUTABLE_FILE)
        misc.bind_menu_item(self, GENERATE_CODE, lambda: common.root.generate_code())

        file_menu.AppendSeparator() # ----------------------------------------------------------------------------------

        item = append_menu_item(file_menu, -1, _("&Import from XRC..."))
        misc.bind_menu_item(self, item, self.import_xrc)

        file_menu.AppendSeparator() # ----------------------------------------------------------------------------------

        EXIT = append_menu_item(file_menu, -1, _('E&xit\tCtrl+Q'), wx.ART_QUIT)
        misc.bind_menu_item(self, EXIT, self.Close)

        menu_bar.Append(file_menu, _("&File"))


        # Edit menu ====================================================================================================
        edit_menu = wx.Menu(style=wx.MENU_TEAROFF)

        # these menu items will be updated
        self._menu_undo = item = append_menu_item(edit_menu, -1, _('Un-do\tCtrl+Z'),
                                                  helpString="Un-do the last property modification on another widget")
        misc.bind_menu_item(self, item, lambda: common.history.undo(misc.focused_widget))

        self._menu_redo = item = append_menu_item(edit_menu, -1, _('Re-do\tCtrl+Y'),
                                                  helpString="Re-do the last property modification on another widget")
        misc.bind_menu_item(self, item, lambda: common.history.repeat(misc.focused_widget))

        self._menu_repeat = item = append_menu_item(edit_menu, -1, _('Repeat\tCtrl-R'),
          helpString="Repeat the last property modifications on another widget (multiple modifications, if applicable)")
        misc.bind_menu_item(self, item, lambda: common.history.repeat(misc.focused_widget))

        edit_menu.AppendSeparator() # ----------------------------------------------------------------------------------

        item = append_menu_item(edit_menu, -1, _('Template Manager...'))
        misc.bind_menu_item(self, item, self.manage_templates)

        item = append_menu_item(edit_menu, wx.ID_PREFERENCES, _('Preferences...'), "prefs.png")
        misc.bind_menu_item(self, item, self.edit_preferences)

        menu_bar.Append(edit_menu, _("&Edit"))

        # Windows menu: layout and focus ===============================================================================
        view_menu = wx.Menu(style=wx.MENU_TEAROFF)

        i = append_menu_item(view_menu, -1, _("Layout &1: Tree\tAlt-1"), "../layout1.png")
        misc.bind_menu_item(self, i, self.switch_layout, 0)
        
        i = append_menu_item(view_menu, -1, _("Layout &2: Properties\tAlt-2"), "../layout2.png")
        misc.bind_menu_item(self, i, self.switch_layout, 1)

        i = append_menu_item(view_menu, -1, _("Layout &3: Narrow\tAlt-3"), "../layout3.png")
        misc.bind_menu_item(self, i, self.switch_layout, 2)
        view_menu.AppendSeparator()

        i = append_menu_item(view_menu, -1, _("Focus &Tree\tF2"))
        misc.bind_menu_item(self, i, self.show_tree)

        i = append_menu_item(view_menu, -1, _("Focus &Properties\tF3"))
        misc.bind_menu_item(self, i, self.show_props_window )

        i = append_menu_item(view_menu, -1, _("Focus Pa&lette\tF4"))
        misc.bind_menu_item(self, i, self.show_palette )

        # submenu focus sections >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        view_props_menu = wx.Menu()
        # randomly select set of shortcuts to be displayed:
        if int(math.ceil(time.time())) % 2:
            shortcuts = ["F8", "F9", "F10", "F11", "F12"]
        else:
            shortcuts = ["Ctrl-M", "Ctrl-L", "Ctrl-W", "Ctrl-E", "Ctrl-D"]
        for sc, section in zip(shortcuts, ("Common", "Layout", "Widget", "Events", "Code")):
            i = append_menu_item(view_props_menu, -1, _("Focus &%s\t%s"%(section, sc)))
            misc.bind_menu_item(self, i, self.show_props_window, section)
        view_menu.AppendSubMenu(view_props_menu, _("Focus Properties &Section"))
        view_menu.AppendSeparator() # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

        i = append_menu_item(view_menu, -1, _("Show/Hide &Design\tF6"))
        misc.bind_menu_item(self, i, self.show_design_window)
        self._m_pin_design_window = i = append_menu_item(view_menu, -1, _("&Pin &Design\tCtrl-P"),  kind=wx.ITEM_CHECK)
        misc.bind_menu_item(self, i, self.pin_design_window)
        view_menu.AppendSeparator() # ----------------------------------------------------------------------------------

        item = append_menu_item(view_menu, wx.ID_REFRESH, _("&Refresh Preview\tF5"), "refresh.png")
        misc.bind_menu_item(self, item, self.preview)

        menu_bar.Append(view_menu, _("&Windows"))

        # Help menu ====================================================================================================
        help_menu = wx.Menu(style=wx.MENU_TEAROFF)

        MANUAL = append_menu_item(help_menu, -1, _('Manual\tF1'), wx.ART_HELP_BOOK)
        misc.bind_menu_item(self, MANUAL, self.show_manual)
        #item = append_menu_item(help_menu, -1, _('Tutorial'))
        #misc.bind_menu_item(self, item, self.show_tutorial)
        help_menu.AppendSeparator() # ----------------------------------------------------------------------------------

        i = append_menu_item(help_menu, -1, _('Mailing list'))
        misc.bind_menu_item(self, i, self.show_mailing_list)
        i = append_menu_item(help_menu, -1, _('Bug tracker'))
        misc.bind_menu_item(self, i, self.show_bug_tracker)
        i = append_menu_item(help_menu, -1, _('Releases'))
        misc.bind_menu_item(self, i, self.show_releases)
        help_menu.AppendSeparator() # ----------------------------------------------------------------------------------

        if config.debugging:
            i = append_menu_item(help_menu, -1, 'Shell\tF7')
            misc.bind_menu_item(self, i, self.create_shell_window)
            help_menu.AppendSeparator()

        item = append_menu_item(help_menu, wx.ID_ABOUT, _('About'), wx.ART_INFORMATION)
        misc.bind_menu_item(self, item, self.show_about_box)

        menu_bar.Append(help_menu, _('&Help'))

        self.SetMenuBar(menu_bar)
        # Mac tweaks...
        if wx.Platform == "__WXMAC__":
            if compat.IS_PHOENIX:
                wx.PyApp.SetMacAboutMenuItemId(wx.ID_ABOUT)
                wx.PyApp.SetMacPreferencesMenuItemId(wx.ID_PREFERENCES)
                wx.PyApp.SetMacExitMenuItemId(wx.ID_EXIT)
                wx.PyApp.SetMacHelpMenuTitleName(_('&Help'))
            else:
                wx.App_SetMacAboutMenuItemId(wx.ID_ABOUT)
                wx.App_SetMacPreferencesMenuItemId(wx.ID_PREFERENCES)
                wx.App_SetMacExitMenuItemId(wx.ID_EXIT)
                wx.App_SetMacHelpMenuTitleName(_('&Help'))

        # file history support
        num_entries = config.preferences.number_history
        self.file_history = wx.FileHistory(num_entries)
        self.file_history.UseMenu(file_menu)
        files = common.load_file_history()
        files.reverse()
        for path in files:
            self.file_history.AddFileToHistory(path.strip())

        self.Bind(wx.EVT_MENU_RANGE, self.open_from_history, id=wx.ID_FILE1, id2=wx.ID_FILE1+num_entries-1)

    def _add_label_tool(self, tb, size, id, label, bmp, itemtype, msg, msg_long=None):
        ADD = tb.AddLabelTool  if compat.IS_CLASSIC else  tb.AddTool
        if compat.IS_PHOENIX:
            method = getattr(tb, "AddTool")
        else:
            method = getattr(tb, "AddLabelTool")

        if isinstance(bmp, str) and not bmp.startswith("wxART_"):
            bmp = wx.Bitmap( os.path.join(config.icons_path, bmp) )
        else:
            # a wx.ART_... constant
            bmp = wx.ArtProvider.GetBitmap(bmp, wx.ART_TOOLBAR, size)
        return ADD(-1, _(label), bmp, wx.NullBitmap, itemtype, _(msg), _(msg_long or msg))

    def create_toolbar(self):
        # new, open, save, generate, add, delete, re-do,  Layout 1, 2, 3,  pin,    help
        #   insert slot/page?
        #   Layout: Alt + 1,2,3
        
        self.toolbar = tb = wx.ToolBar(self, -1)
        self.SetToolBar(tb)
        size = (21,21)
        add = functools.partial(self._add_label_tool, tb, size)
        t = add( wx.ID_NEW, "New", wx.ART_NEW, wx.ITEM_NORMAL, "Start a new file (Ctrl+N)")
        self.Bind(wx.EVT_TOOL, self.new_app, t)
        
        t = add( wx.ID_OPEN, "Open", wx.ART_FILE_OPEN, wx.ITEM_NORMAL, "Open an existing file (Ctrl+O)")
        self.Bind(wx.EVT_TOOL, self.open_app, t)
        
        t = add( wx.ID_SAVE, "Save", wx.ART_FILE_SAVE, wx.ITEM_NORMAL, "Save file (Ctrl+S)")
        self.Bind(wx.EVT_TOOL, self.save_app, t)

        if config.debugging and hasattr(wx, "ART_PLUS"):
            t = add( -1, "Add", wx.ART_PLUS, wx.ITEM_NORMAL, "Add widget (Ctrl+A)")
            t.Enable(False)

            # XXX switch between wx.ART_DELETE for filled slots and wx.ART_MINUS for empty slots
            t = add( -1, "Remove", wx.ART_MINUS, wx.ITEM_NORMAL, "Add widget (Ctrl+A)")
            t.Enable(False)

            tb.AddSeparator()

        self._tool_undo = t = add( wx.ID_UNDO, "Un-do", wx.ART_UNDO, wx.ITEM_NORMAL, "Un-do (Ctrl+Z)" )
        t.Enable(False)
        self.Bind(wx.EVT_TOOL, lambda evt: common.history.undo(misc.focused_widget), t)

        self._tool_redo = t = add( wx.ID_REDO, "Re-do", wx.ART_REDO, wx.ITEM_NORMAL, "Re-do (Ctrl+Y)" )
        t.Enable(False)
        self.Bind(wx.EVT_TOOL, lambda evt: common.history.redo(misc.focused_widget), t)

        self._tool_repeat = t = add( -1, "Repeat", wx.ART_REDO, wx.ITEM_NORMAL, "Repeat (Ctrl+R)" )
        t.Enable(False)
        self.Bind(wx.EVT_TOOL, lambda evt: common.history.repeat(misc.focused_widget), t)

        tb.AddSeparator()
        t = add(-1, "Generate Code", wx.ART_EXECUTABLE_FILE, wx.ITEM_NORMAL, "Generate Code (Ctrl+G)" )
        self.Bind(wx.EVT_TOOL, lambda event: common.root.generate_code(), t)
        tb.AddSeparator()
        
        t1 = add(-1, "Layout 1", "layout1.png", wx.ITEM_RADIO, "Switch layout: Tree", 
                                                               "Switch layout: Palette and Properties left, Tree right")
        self.Bind(wx.EVT_TOOL, lambda event: self.switch_layout(0), t1)
        t2 = add(-1, "Layout 2", "layout2.png", wx.ITEM_RADIO,"Switch layout: Properties",
                                                              "Switch layout: Palette and Tree top,  Properties bottom") 
        self.Bind(wx.EVT_TOOL, lambda event: self.switch_layout(1), t2)
        t3 = add(-1, "Layout 3", "layout3.png", wx.ITEM_RADIO, "Switch layout: narrow",
                                                     "Switch layout: Palette, Tree and Properties on top of each other")
        self.Bind(wx.EVT_TOOL, lambda event: self.switch_layout(2), t3)
        self._layout_tools = [t1,t2,t3]

        tb.AddSeparator()
        t = add(-1, "Pin Design Window", "pin_design.png", wx.ITEM_CHECK, "Pin Design Window",
                                                                          "Pin Design Window to stay on top")
        self.Bind(wx.EVT_TOOL, lambda event: self.pin_design_window(), t)
        self._t_pin_design_window = t

        tb.AddSeparator()

        t = add(wx.ID_HELP, "Help", wx.ART_HELP_BOOK, wx.ITEM_NORMAL, "Show manual (F1)")
        self.Bind(wx.EVT_TOOL, self.show_manual, t)

        self.toolbar.Realize()

    def init_autosave(self):
        # ALB 2004-10-15, autosave support...
        self.autosave_timer = None
        if not config.preferences.autosave: return
        self.autosave_timer = wx.Timer(self, -1)
        self.Bind(wx.EVT_TIMER, self.on_autosave_timer, self.autosave_timer)
        self.autosave_timer.Start( int(config.preferences.autosave_delay) * 1000 )

    def on_autosave_timer(self, event):
        res = common.autosave_current()
        if res == 2:
            self.user_message(_("Auto saving... done"))
        elif not res:
            self.autosave_timer.Stop()
            config.preferences.autosave = False
            logging.info(_('Disable autosave function permanently'))
            wx.MessageBox(
                _('The autosave function failed. It has been disabled\n'
                  'permanently due to this error. Use the preferences\n'
                  'dialog to re-enable this functionality.\n'
                  'The details have been written to the wxGlade log file\n\n'
                  'The log file is: %s' % config.log_file ),
                _('Autosave Failed'), wx.OK | wx.CENTRE | wx.ICON_ERROR )

    def check_autosaved(self):
        if not common.check_autosaved(None): return
        res = wx.MessageBox(
            _('There seems to be auto saved data from last wxGlade session: do you want to restore it?'),
            _('Auto save detected'), style=wx.ICON_QUESTION | wx.YES_NO)
        if res == wx.YES:
            filename = common.get_name_for_autosave()
            if self._open_app(filename, add_to_history=False):
                self.cur_dir = os.path.dirname(filename)
                common.root.saved = False
                common.root.filename = None
                self.user_message(_('Auto save loaded'))
        common.remove_autosaved()


    def edit_preferences(self):
        dialog = preferencesdialog.wxGladePreferences(config.preferences)
        if dialog.ShowModal() == wx.ID_OK:
            wx.MessageBox( _('Changes will take effect after wxGlade is restarted'),
                           _('Preferences saved'), wx.OK|wx.CENTRE|wx.ICON_INFORMATION )
            dialog.set_preferences()
        else:
            dialog.canceled()
        dialog.Destroy()

    def _get_toplevel(self):
        # return the toplevel for a preview or design window
        if misc.focused_widget and not isinstance(misc.focused_widget, application.Application):
            # a widget is selected, find the toplevel window for it
            return misc.focused_widget.toplevel_parent
        # find main toplevel window
        return common.root._get_top_window()

    def preview(self):
        """Generate preview of the current loaded project.
        
        A preview can be triggered by keyboard shortcut or by pressing the preview button.
        The preview can be triggered for all selected widgets.
        This doesn't mean that the widget is opened for editing."""
        toplevel = self._get_toplevel()
        if toplevel is not None:
            toplevel.preview(refresh=True)

    def show_palette(self):
        if self.IsIconized(): self.Iconize(False)
        self.palette.SetFocus()

    def show_tree(self):
        if self.IsIconized(): self.Iconize(False)
        common.app_tree.SetFocus()

    def show_props_window(self, section=None):
        # XXX implement: if a section is active already, then go to first property of the page
        if not self.property_panel.notebook: return
        if self.IsIconized(): self.Iconize(False)
        self.property_panel.pagenames
        if not section:
            self.property_panel.notebook.SetFocus()
        else:
            if not section in self.property_panel.pagenames:
                return
            i = self.property_panel.pagenames.index(section)
            if self.property_panel.notebook.GetSelection() != i:
                self.property_panel.notebook.ChangeSelection(i)
            else:
                self.property_panel.notebook.SetFocus()
                # try to set the focus if the widget has changed; this is not yet implemented for many property types
                widget = self.property_panel.current_widget
                if ( widget and (widget is not np.current_property.owner) and
                     section in widget.PROPERTIES ):
                    i_p = widget.PROPERTIES.index(section) + 1
                    if i_p<len(widget.PROPERTIES):
                        prop = widget.properties.get(widget.PROPERTIES[i_p])
                        if prop: prop.set_focus()
        self.Raise()

    def show_design_window(self):
        toplevel = self._get_toplevel()
        if not toplevel: return

        if toplevel.widget:
            focus = toplevel.widget.FindFocus()
            focused = focus and focus.GetTopLevelParent() is toplevel.widget.GetTopLevelParent()

        if toplevel.widget and toplevel.widget.IsShownOnScreen() and not focused:
            # just raise it
            if toplevel.widget.GetTopLevelParent().IsIconized():
                toplevel.widget.GetTopLevelParent().Iconize(False)
            toplevel.widget.GetTopLevelParent().Raise()
            return
        # open or close
        common.app_tree.show_toplevel(None, editor=toplevel)

    def pin_design_window(self):
        common.pin_design_window = not common.pin_design_window
        if common.pin_design_window != self._t_pin_design_window.IsToggled():
            self._t_pin_design_window.Toggle()
            self.toolbar.Realize()
        self._m_pin_design_window.Check(common.pin_design_window)

        toplevel = self._get_toplevel()
        if not toplevel or not toplevel.widget: return
        frame = toplevel.widget.GetTopLevelParent()
        if not isinstance(frame, wx.Frame): return
        style = frame.GetWindowStyle()
        if common.pin_design_window:
            frame.SetWindowStyle( style | wx.STAY_ON_TOP)
        elif style & wx.STAY_ON_TOP:
            frame.ToggleWindowStyle(wx.STAY_ON_TOP)
            if wx.Platform=='__WXMSW__':
                frame.Iconize(True)
                frame.Iconize(False)
            else:
                toplevel.widget.Raise()

    def create_shell_window(self):
        common.shell = ShellFrame(None)
        if misc.focused_widget: common.shell.txt_path.SetValue( misc.focused_widget.get_path() )
        common.shell.Show()

    # status bar for message display ###################################################################################
    def create_statusbar(self):
        self.CreateStatusBar(1)
        # ALB 2004-10-15  statusbar timer: delete user message after some time
        self.clear_sb_timer = wx.Timer(self, -1)
        self.Bind(wx.EVT_TIMER, self.on_clear_sb_timer, self.clear_sb_timer)

    def user_message(self, msg):
        # display a message, but clear it after a few seconds again
        sb = self.GetStatusBar()
        if sb:
            sb.SetStatusText(msg)
            if msg:
                self.clear_sb_timer.Start(5000, True)

    def on_clear_sb_timer(self, event):
        sb = self.GetStatusBar()
        if sb: sb.SetStatusText("")
    ####################################################################################################################

    def ask_save(self):
        """checks whether the current app has changed and needs to be saved:
        if so, prompts the user;
        returns False if the operation has been cancelled"""
        if not common.root.saved:
            ok = wx.MessageBox(_("Save changes to the current app?"),
                               _("Confirm"), wx.YES_NO|wx.CANCEL|wx.CENTRE|wx.ICON_QUESTION)
            if ok == wx.YES:
                self.save_app()
            return ok != wx.CANCEL
        return True

    def new_app(self, event=None):
        "creates a new wxGlade project"
        if not self.ask_save(): return
        common.root.clear()
        common.root.new()
        common.root.filename = None
        self.user_message("")
        misc.rebuild_tree(common.root)
        common.root.saved = True
        common.remove_autosaved()
        if common.history: common.history.reset()
        if config.preferences.autosave and self.autosave_timer is not None:
            self.autosave_timer.Start()

    def new_app_from_template(self):
        "creates a new wxGlade project from an existing template file"
        if not self.ask_save(): return
        infile = template.select_template()
        if infile:
            self._open_app(infile, add_to_history=False)
            common.root.template_data = None

    def open_app(self, event=None):
        """loads a wxGlade project from an xml file
        NOTE: this is very slow and needs optimisation efforts
        NOTE2: the note above should not be True anymore :) """
        if not self.ask_save():
            return
        default_path = os.path.dirname(common.root.filename or "") or self.cur_dir
        infile = wx.FileSelector(_("Open file"),
                                   wildcard="wxGlade files (*.wxg)|*.wxg|wxGlade Template files (*.wgt)|*.wgt|"
                                            "XML files (*.xml)|*.xml|All files|*",
                                   flags=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST, default_path=default_path)
        if not infile: return
        self._open(infile)

    def open_from_history(self, event):
        if not self.ask_save():
            return
        pos = event.GetId() - wx.ID_FILE1
        filename = self.file_history.GetHistoryFile(pos)
        if not os.path.exists(filename):
            wx.MessageBox( _("The file %s doesn't exist.") % filename,
                           _('Information'), style=wx.CENTER | wx.ICON_INFORMATION | wx.OK )
            self.file_history.RemoveFileFromHistory(pos)
            common.remove_autosaved(filename)
            return
        self._open(filename)

    def _open(self, filename):
        # called by open_app and open_from_history
        if common.check_autosaved(filename):
            res = wx.MessageBox( _('There seems to be auto saved data for this file: do you want to restore it?'),
                                 _('Auto save detected'), style=wx.ICON_QUESTION | wx.YES_NO )
            if res == wx.YES:
                common.restore_from_autosaved(filename)
            else:
                common.remove_autosaved(filename)
        else:
            common.remove_autosaved(filename)

        path = position = None
        if filename == common.root.filename:
            # if we are re-loading the file, store path and position
            if misc.focused_widget:
                path = misc.focused_widget.get_path()
                if misc.focused_widget.widget is not None and not misc.focused_widget.IS_ROOT:
                    toplevel = misc.get_toplevel_parent(misc.focused_widget.widget)
                    if toplevel: position = toplevel.GetPosition()

        self._open_app(filename)
        self.cur_dir = os.path.dirname(filename)
        if not path: return
        editor = common.root.find_widget_from_path(path)
        if not editor: return
        misc.set_focused_widget(editor)
        if editor is common.root: return
        editor.toplevel_parent.create()

        common.app_tree.ExpandAllChildren(editor.item)

        if not position or not editor.widget: return
        misc.get_toplevel_parent(editor.widget).SetPosition(position)

    def _open_app(self, filename, use_progress_dialog=True, add_to_history=True):
        "Load a new wxGlade project"
        error_msg = None
        infile = None

        start = time.time()

        common.root.clear()
        common.root.init()
        common.app_tree.DeleteChildren(common.root.item)
        common.app_tree.auto_expand = False  # disable auto-expansion of nodes

        try:
            try:
                logging.info( _('Read wxGlade project from file "%s"'), filename )
                input_file_version = None

                if not isinstance(filename, list):
                    common.root.filename = filename
                    # decoding will done automatically by SAX XML library
                    if compat.PYTHON2:
                        infile = open(filename)
                    else:
                        infile = open(filename, "r", encoding="UTF8")
                    if hasattr(infile, "seek"):
                        # try to read file version number from the first few lines
                        import re
                        version_re = re.compile(r"<!-- generated by wxGlade (\d+)\.(\d+)\.(\d+)(\S*)\s*")
                        for n in range(3):
                            match = version_re.match( infile.readline() )
                            if match:
                                major, minor, sub, extension = match.groups()
                                input_file_version = (int(major), int(minor), int(sub), extension)
                                break
                        infile.seek(0)

                else:
                    common.root.filename = None

                if use_progress_dialog and config.preferences.show_progress:
                    p = ProgressXmlWidgetBuilder(filename, input_file_version, input_file=infile)
                else:
                    p = XmlWidgetBuilder(filename, input_file_version)

                if infile is not None:
                    p.parse(infile)
                else:
                    p.parse_string(filename)
                    filename = None
            except (EnvironmentError, SAXParseException, XmlParsingError) as msg:
                if config.debugging: raise
                if infile is not None:
                    error_msg = _("Error loading file %s:\n%s") % (misc.wxstr(filename), misc.wxstr(msg))
                else:
                    error_msg = _("Error loading from a file-like object:\n%s") % misc.wxstr(msg)
            except Exception as inst:
                if config.debugging: raise
                if filename and not isinstance(filename, list):
                    fn = os.path.basename(filename).encode('ascii','replace')
                    msg = _('loading file "%s"') % fn
                else:
                    msg = _('loading from a file-like object')
                bugdialog.Show(msg, inst)
        finally:
            if infile and filename:
                infile.close()

            if error_msg:
                common.root.clear()
                common.root.new()
                common.root.saved = True
                if common.history: common.history.reset()
                common.app_tree.auto_expand = True  # re-enable auto-expansion of nodes

                wx.MessageBox(error_msg, _('Error'), wx.OK | wx.CENTRE | wx.ICON_ERROR)

                return False

        misc.rebuild_tree(common.root, freeze=True)

        common.app_tree.auto_expand = True  # re-enable auto-expansion of nodes

        common.app_tree.Expand(common.root.item)
        if common.root.is_template:
            logging.info(_("Template loaded"))
            common.root.template_data = template.Template(filename)
            common.root.filename = None

        end = time.time()
        logging.info(_('Loading time: %.5f'), end - start)

        common.root.saved = True
        if common.history: common.history.reset()
        #common.property_panel.Raise()

        if hasattr(self, 'file_history') and filename is not None and add_to_history and \
           (not common.root.is_template):
            self.file_history.AddFileToHistory(misc.wxstr(filename))

        if config.preferences.autosave and self.autosave_timer is not None:
            self.autosave_timer.Start()

        duration = end - start
        if filename:
            self.user_message( _("Loaded %s in %.2f seconds") % (misc.wxstr(os.path.basename(filename)), duration) )
        else:
            self.user_message( _("Loaded in %.2f seconds") % duration )

        return True

    def save_app(self, event=None):
        "saves a wxGlade project onto an xml file"
        np.flush_current_property()
        if not common.root.filename or common.root.is_template:
            self.save_app_as()
        else:
            # check whether we are saving a template
            ext = os.path.splitext(common.root.filename)[1].lower()
            if ext == ".wgt":
                common.root.is_template = True
            self._save_app(common.root.filename)

    def _save_app(self, filename):
        try:
            obuffer = []
            common.root.write(obuffer)
            common.save_file(filename, obuffer, 'wxg')
        except EnvironmentError as inst:
            if config.debugging: raise
            common.root.saved = False
            bugdialog.ShowEnvironmentError(_('Saving this project failed'), inst)
        except Exception as inst:
            if config.debugging: raise
            common.root.saved = False
            fn = os.path.basename(filename).encode('ascii', 'replace')
            bugdialog.Show(_('Save File "%s"') % fn, inst)
        else:
            common.root.saved = True
            common.remove_autosaved()
            if config.preferences.autosave and self.autosave_timer is not None:
                self.autosave_timer.Start()
            self.user_message( _("Saved %s") % os.path.basename(filename) )

    def save_app_as(self):
        "saves a wxGlade project onto an xml file chosen by the user"
        if common.root.filename:
            default_path, default_filename = os.path.split(common.root.filename)
        else:
            default_path, default_filename = self.cur_dir, "wxglade.wxg"
        fn = wx.FileSelector( _("Save project as..."),
                              wildcard="wxGlade files (*.wxg)|*.wxg|wxGlade Template files (*.wgt) |*.wgt|"
                              "XML files (*.xml)|*.xml|All files|*",
                              flags=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT,
                              default_path=default_path,
                              default_filename=default_filename)
        if not fn: return

        # check for file extension and add default extension if missing
        ext = os.path.splitext(fn)[1].lower()
        if not ext:
            fn = "%s.wxg" % fn

        common.root.filename = fn
        #remove the template flag so we can save the file.
        common.root.properties["is_template"].set(False)

        self.save_app()
        self.cur_dir = os.path.dirname(fn)
        self.file_history.AddFileToHistory(fn)

    def save_app_as_template(self):
        "save a wxGlade project as a template"
        data = getattr(common.root, 'template_data', None)
        outfile, data = template.save_template(data)
        if outfile:
            common.root.properties["is_template"].set(True)
            common.root.template_data = data
            self._save_app(outfile)

    def on_close(self, event):
        if not event.CanVeto():
            event.Skip
            return
        if self.ask_save():
            # close application
            # first, let's see if we have to save the geometry...
            prefs = config.preferences
            if prefs.remember_geometry:
                self._store_layout()
                prefs.set_dict("layout", self.layout_settings)
                prefs.changed = True
            common.root.clear()
            common.root.new()
            try:
                common.save_preferences()
            except Exception as e:
                wx.MessageBox( _('Error saving preferences:\n%s') % e,
                               _('Error'), wx.OK|wx.CENTRE|wx.ICON_ERROR )
            self.Destroy()
            common.remove_autosaved()
            wx.CallAfter(wx.GetApp().ExitMainLoop)
        elif event.CanVeto():
            event.Veto()

    def show_about_box(self):
        "show the about dialog;  see: about.wxGladeAboutBox"
        about_box = about.wxGladeAboutBox()
        about_box.ShowModal()
        about_box.Destroy()

    def show_manual(self, event=None):
        "Show the wxGlade user manual"
        self._show_html(config.manual_file)

    def show_bug_tracker(self):
        self._show_html("https://github.com/wxGlade/wxGlade/issues")
    def show_mailing_list(self):
        self._show_html("https://sourceforge.net/p/wxglade/mailman/wxglade-general/")
    def show_releases(self):
        self._show_html("https://github.com/wxGlade/wxGlade/releases")

    def _show_html(self, html_file):
        "Open browser and show an HTML documentation"

        if wx.Platform == "__WXMAC__":
            os.system(r'open -a Safari.app "%s"' % html_file)
        else:
            import webbrowser, threading
            # ALB 2004-08-15: why did this block the program????? (at least on linux - GTK)
            def go():
                webbrowser.open_new(html_file)
            t = threading.Thread(target=go)
            t.setDaemon(True)
            t.start()

    def show_and_raise(self):
        self.property_panel.Show()  # self.GetMenuBar().IsChecked(self.PROPS_ID))
        self.tree_panel.Show()      # self.GetMenuBar().IsChecked(self.TREE_ID))
        self.property_panel.Raise()
        self.tree_panel.Raise()
        self.Raise()

    def hide_all(self):
        self.tree_panel.Hide()
        self.property_panel.Hide()

    def import_xrc(self, infilename=None, ask_save=True):
        import xrc2wxg

        if ask_save and not self.ask_save():
            return

        if not infilename:
            infilename = wx.FileSelector( _("Import file"), wildcard="XRC files (*.xrc)" "|*.xrc|All files|*",
                                          flags=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST, default_path=self.cur_dir)
        if infilename:
            ibuffer = []
            try:
                xrc2wxg.convert(infilename, ibuffer)

                # Convert UTF-8 returned by xrc2wxg.convert() to Unicode
                tmp = b"".join(ibuffer).decode('UTF-8')
                ibuffer = ['%s\n'%line for line in tmp.split('\n')]

                self._open_app(ibuffer)
                common.root.saved = False
            except Exception as inst:
                fn = os.path.basename(infilename).encode('ascii', 'replace')
                bugdialog.Show(_('Import File "%s"') % fn, inst)

    def manage_templates(self):
        to_edit = template.manage_templates()
        if to_edit is not None and self.ask_save():
            # edit the template
            # TODO, you still need to save it manually...
            self._open_app(to_edit, add_to_history=False)
            wx.MessageBox( _("To save the changes to the template, edit the GUI as usual,\n"
                             "and then click File->Save As Template..."),
                           _("Information"), style=wx.OK|wx.ICON_INFORMATION )
    ####################################################################################################################
    # user interface helpers
    def _set_icon(self):
        icon = compat.wx_EmptyIcon()
        bmp = wx.Bitmap( os.path.join(config.icons_path, "icon.png") )
        icon.CopyFromBitmap(bmp)
        self.SetIcon(icon)
    def init_layout_settings(self):
        # either load from file or init with defaults
        display_area = wx.Display(0).ClientArea
        default_pos = display_area.TopLeft
        height = display_area.height
        width = 800
        default_size = (width,height)

        self.layout_settings = {}
        self.layout_settings["layout"] = 0
        self.layout_settings["sash_positions"] = [[400,     380       ],   # 0: palette and properties left; tree right
                                                  [height//2,400       ],   # 1: palette and tree top; properties bottom
                                                  [2*height//3,height//3] ]  # 2: all on top of each other
        self.layout_settings["widths"] = [width,500]  # for layouts 0/1 and 2
        self.layout_settings["height"] = height
        self.layout_settings["x"], self.layout_settings["y"] = default_pos

        if not config.preferences.remember_geometry:
            return default_pos, default_size, 0

        # read from preferences
        try:
            layout = config.preferences.get_int("layout", "layout")
            x = config.preferences.get_int("layout", "x")
            y = config.preferences.get_int("layout", "y")
            widths = [config.preferences.get_int("layout", "widths_l0"),
                      config.preferences.get_int("layout", "widths_l1")]
            width = widths[0]  if layout<2  else widths[1]
            height = config.preferences.get_int("layout", "height")

            sash_positions = [[config.preferences.get_int("layout", "sash_positions_l0_l0"),
                               config.preferences.get_int("layout", "sash_positions_l0_l1")],
                              [config.preferences.get_int("layout", "sash_positions_l1_l0"),
                               config.preferences.get_int("layout", "sash_positions_l1_l1")],
                              [config.preferences.get_int("layout", "sash_positions_l2_l0"),
                               config.preferences.get_int("layout", "sash_positions_l2_l1")]]
        except:
            return default_pos, default_size, 0
        if layout<0 or layout>2 or not self._check_geometry(x, y, width, height):
            return default_pos, default_size, 0
        self.layout_settings["height"] = height
        self.layout_settings["sash_positions"] = sash_positions
        self.layout_settings["widths"] = widths
        return (x,y), (widths[0],height), layout  # return widths[0] as 0 is the initial setting

    def switch_layout(self, new_layout, initial=False):
        if new_layout != self.layout_settings["layout"]:
            # set the splitters
            if not initial: self._store_layout()
            self.splitter2.Unsplit()
            self.splitter1.Unsplit()
            if new_layout==0:
                self.property_panel.Reparent(self.splitter2)
                self.palette.Reparent(self.splitter2)
                self.tree.Reparent(self.splitter1)
                self.splitter1.SplitVertically(self.splitter2, self.tree)
                self.splitter2.SplitHorizontally(self.palette, self.property_panel)
            elif new_layout==1:
                self.property_panel.Reparent(self.splitter1)
                self.palette.Reparent(self.splitter2)
                self.tree.Reparent(self.splitter2)
                self.splitter1.SplitHorizontally(self.splitter2, self.property_panel)
                self.splitter2.SplitVertically(self.palette, self.tree)
            elif new_layout==2:
                self.property_panel.Reparent(self.splitter1)
                self.palette.Reparent(self.splitter2)
                self.tree.Reparent(self.splitter2)
                self.splitter1.SplitHorizontally(self.splitter2, self.property_panel)
                self.splitter2.SplitHorizontally(self.palette, self.tree)
            if self.layout_settings["layout"] in (0,1) and new_layout==2:
                self.SetSize( (self.layout_settings["widths"][1], self.GetSize()[1]) )
            elif self.layout_settings["layout"]==2 and new_layout in (0,1):
                self.SetSize( (self.layout_settings["widths"][0], self.GetSize()[1]) )
            self.layout_settings["layout"] = new_layout

        # display in toolbar
        t = self._layout_tools[new_layout]
        if not t.IsToggled(): t.Toggle()
        self.toolbar.Realize()

        # set splitter sash positions
        if new_layout==0:
            self.splitter2.SetMinimumPaneSize(1)
            self.splitter2.SetSashGravity(0)
            self.splitter1.SetMinimumPaneSize(2)
            self.splitter1.SetSashGravity(0)
        elif new_layout==1:
            self.splitter2.SetMinimumPaneSize(1)
            self.splitter2.SetSashGravity(0)
            self.splitter1.SetMinimumPaneSize(2)
            self.splitter1.SetSashGravity(0.5)
        elif new_layout==2:
            self.splitter2.SetMinimumPaneSize(1)
            self.splitter2.SetSashGravity(0)
            self.splitter1.SetMinimumPaneSize(2)
            self.splitter1.SetSashGravity(0.5)
        positions = self.layout_settings["sash_positions"][new_layout]
        self.splitter1.SetSashPosition( positions[0] )
        self.splitter2.SetSashPosition( positions[1] )

    def _store_layout(self):
        # store position, size and splitter sash positions
        self.layout_settings["x"], self.layout_settings["y"] = self.GetPosition()

        layout = self.layout_settings["layout"]
        self.layout_settings["sash_positions"][layout] = [self.splitter1.GetSashPosition(),
                                                          self.splitter2.GetSashPosition()]
        width, height = self.GetSize()
        if layout in (0,1):
            self.layout_settings["widths"][0] = width
        else:
            self.layout_settings["widths"][1] = width
        self.layout_settings["height"] = height

    def _check_geometry(self, x,y,width,height):
        # check whether a significant part would be visible
        # also, at least a part of the top edge needs to be visible
        if width<150 or height<150: return False

        # check top line
        top_line = wx.Rect(x,y,width,1)
        min_visible = int(round(width/3))
        top_line_OK = False
        for d in range(wx.Display.GetCount()):
            display = wx.Display(d)
            client_area = display.ClientArea
            if not client_area.width or not client_area.height:
                # the display info is broken on some installations
                continue
            intersection = client_area.Intersect(top_line)
            if intersection.width>=min_visible:
                top_line_OK = True
                break

        if not top_line_OK: return False

        # check rectangle
        geometry = wx.Rect(x,y,width,height)

        for d in range(wx.Display.GetCount()):
            display = wx.Display(d)
            client_area = display.ClientArea
            if not client_area.width or not client_area.height:
                # the display info is broken on some installations
                continue
            intersection = client_area.Intersect(geometry)
            if intersection.width>150 and intersection.height>150 or geometry.width==-1 or geometry.height==-1:
                return True
        return False


class wxGlade(wx.App):
    """wxGlade application class
    
    _exception_orig: Reference to original implementation of logging.exception()"""

    def OnInit(self):
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__

        # replace text based exception handler by a graphical exception dialog
        sys.excepthook = self.graphical_exception_handler

        # use graphical implementation to show caught exceptions
        self._exception_orig = logging.exception  # Reference to original implementation of logging.exception()
        logging.exception = self.exception

        # needed for wx >= 2.3.4 to disable wxPyAssertionError exceptions
        if not config.debugging:
            self.SetAssertMode(0)

        common.init_preferences()

        self.locale = wx.Locale(wx.LANGUAGE_DEFAULT)  # avoid PyAssertionErrors
        #compat.wx_ArtProviderPush(wxGladeArtProvider())

        frame = wxGladeFrame()
        self.SetTopWindow(frame)
        self.SetExitOnFrameDelete(True)

        self.init_idle()

        if config.inform_screen_reader:
            message = ("It seems you have a screen reader software installed.\n"
                       "Please be aware that there are some options to improve wxGlade accessibility\n"
                       "with screen readers.\n"
                       "See menu Edit -> Preferences -> Accessibility.")
            wx.CallLater(1000, wx.MessageBox, message, "Accessibility Info")

        return True

    def OnExit(self):
        "Restore original exception handler and logging.exception() on exit"
        sys.excepthook = sys.__excepthook__
        logging.exception = self._exception_orig
        return 0

    def init_idle(self):
        if wx.Platform == "__WXMAC__" and compat.IS_CLASSIC:
            # it seems that EVT_IDLE makes wx.CallAfter stall from time to time, so we use a timer
            wx.CallLater(200, self.OnIdle)
        else:
            self.Bind(wx.EVT_IDLE, self.OnIdle)

    def OnIdle(self, event=None):
        "Idle tasks - currently show error messages only;  see: show_msgdialog()"
        try:
            self.show_msgdialog()
        finally:
            if wx.Platform == "__WXMAC__" and compat.IS_CLASSIC:
                wx.CallLater(200, self.OnIdle)

    def show_msgdialog(self):
        """
        Check for log messages and show them

        see: main.wxGlade.OnIdle(), log.getBufferAsList(), msgdialog.MessageDialog"""
        log_msg = log.getBufferAsString()
        if not log_msg:
            return

        # initialise message dialog
        msg_dialog = msgdialog.MessageDialog(None, -1, "")
        msg_dialog.msg_list.InsertColumn(0, "")

        # clear dialog and show new messages
        msg_dialog.msg_list.Freeze()
        msg_dialog.msg_list.DeleteAllItems()
        for line in log_msg.split('\n'):
            msg_dialog.msg_list.Append([line, ])
        msg_dialog.msg_list.SetColumnWidth(0, -1)
        msg_dialog.msg_list.Thaw()
        msg_dialog.ShowModal()
        msg_dialog.Destroy()

    def graphical_exception_handler(self, exc_type, exc_value, exc_tb):
        """Show detailed information about uncaught exceptions in bugdialog.BugReport.

        The shown exception will be logged to the log file in parallel.
        The exception information will be cleared after the bug dialog has closed.

        exc_type:  Type of the exception (normally a class object)
        exc_value: The "value" of the exception
        exc_tb:    Call stack of the exception

        see: bugdialog.BugReport(), bugdialog.Show()"""
        bugdialog.ShowEI(exc_type, exc_value, exc_tb)
        if compat.PYTHON2: sys.exc_clear()

    def exception(self, msg, *args, **kwargs):
        """Graphical replacement of logging.exception().

        All exception details logged with logging.exception() will be shown in bugdialog.BugReport.
        The shown exception will be logged to the log file ding.
        The exception information will be cleared after the bug dialog has closed.

        msg: Short description of the exception

        see: bugdialog.BugReport, bugdialog.ShowEI()"""
        if args:
            try:
                msg = msg % args
            except TypeError:
                log.exception_orig(_('Wrong format of a log message'))

        (exc_type, exc_value, exc_tb) = sys.exc_info()
        bugdialog.ShowEI(exc_type, exc_value, exc_tb, msg)
        if compat.PYTHON2: sys.exc_clear()



def main(filename=None):
    "if filename is not None, loads it"
    logging.info(_("Using wxPython %s"), config.wx_version)
    common.history = history.History()
    app = wxGlade()
    if filename is not None:
        win = app.GetTopWindow()
        if os.path.splitext(filename)[1].upper() == ".XRC":
            win.import_xrc(filename)
        else:
            win._open_app(filename, False)

            # mainly for debugging we want the first window to be opened already
            if filename and config.open_design_window and common.root.children:
                editor = common.root.children[0]
                misc.set_focused_widget(editor)
                editor.create()
                common.app_tree.ExpandAllChildren(editor.item)

        win.cur_dir = os.path.dirname(filename)
    #win = app.GetTopWindow()
    ##win.import_xrc(r"D:\Python\Sources35\wxglade\wxglade_dev\tests\casefiles\CalendarCtrl.xrc")
    #win.import_xrc(r"D:\Python\Sources35\wxglade\wxglade_dev\tests\casefiles\AllWidgets_30.xrc")

    app.MainLoop()