forked from wxGlade/wxGlade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
1600 lines (1338 loc) · 68.1 KB
/
main.py
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
"""\
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
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.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
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:
print("DELETE PAGE; new widget:", edit_widget)
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:
focus_before.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
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 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
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)
self.notebook.RemovePage(0)
compat.DestroyLater(dummy_panel)
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()