File: gui_Left.py

package info (click to toggle)
fontypython 0.4.6-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,152 kB
  • ctags: 559
  • sloc: python: 3,619; makefile: 10
file content (271 lines) | stat: -rw-r--r-- 9,088 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
##	Fonty Python Copyright (C) 2006, 2007, 2008, 2009 Donn.C.Ingle
##	Contact: donn.ingle@gmail.com - I hope this email lasts.
##
##	This file is part of Fonty Python.
##	Fonty Python is free software: you can redistribute it and/or modify
##	it under the terms of the GNU General Public License as published by
##	the Free Software Foundation, either version 3 of the License, or
##	(at your option) any later version.
##
##	Fonty Python is distributed in the hope that it will be useful,
##	but WITHOUT ANY WARRANTY; without even the implied warranty of
##	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##	GNU General Public License for more details.
##
##	You should have received a copy of the GNU General Public License
##	along with Fonty Python.  If not, see <http://www.gnu.org/licenses/>.

import wx, os

## June 25th 2016
## Remarking these two lines because they are causing a segfault:
##  ../src/common/stdpbase.cpp(62): assert "traits" failed in Get(): 
##  create wxApp before calling this
##  Segmentation fault (core dumped)
##
##  I do not know how to test or fix this, hence simply removing it.
##  AFAICT, stock buttons will be in the system language.
##
## Setup wxPython to access translations : enables the stock buttons.
##langid = wx.LANGUAGE_DEFAULT # Picks this up from $LANG
##mylocale = wx.Locale( langid )


from pubsub import *
from wxgui import ps

from gui_PogChooser import *

import fpsys # Global objects
import fontyfilter
import fontybugs


class DirControl(wx.GenericDirCtrl) :
	"""
	The Directory tree view. Note: Directory names are all UNICODE!
	"""
	def __init__(self, parent):
		if fpsys.state.viewpattern == "F": 
			startdir = fpsys.state.viewobject.path
		else: 
			##Let's get it from the config object
			lastdir = fpsys.config.lastdir
			
			if os.path.exists(lastdir):
				startdir = lastdir
			else:
				startdir = os.environ['HOME']
		wx.GenericDirCtrl.__init__(self, parent, -1, dir = startdir, style=wx.DIRCTRL_DIR_ONLY)

		# create the image list:
		isz = (16,16)
		il = wx.ImageList(isz[0], isz[1])

		# Add images to list. You need to keep the same order in order for
		# this to work!

		# closed folder:
		il.Add( wx.Bitmap( fpsys.mythingsdir + "/icon_closed_folder.png",wx.BITMAP_TYPE_PNG) )

		# open folder:
		il.Add(wx.Bitmap(fpsys.mythingsdir + "/icon_open_folder.png",wx.BITMAP_TYPE_PNG))

		# root of filesystem (linux):
		il.Add(wx.Bitmap(fpsys.mythingsdir + "/icon_root.png",wx.BITMAP_TYPE_PNG))

		# drive letter (windows):
		il.Add(wx.Bitmap(fpsys.mythingsdir + "/icon_drive.png",wx.BITMAP_TYPE_PNG))

		# cdrom drive:
		il.Add(wx.Bitmap(fpsys.mythingsdir + "/icon_cdrom.png",wx.BITMAP_TYPE_PNG))

		# removable drive on win98:
		il.Add(wx.Bitmap(fpsys.mythingsdir + "/icon_drive.png",wx.BITMAP_TYPE_PNG))

		# removable drive (floppy, flash, etc) Does not seem to work on Kubuntu Jaunty (2009)
		il.Add(wx.Bitmap(fpsys.mythingsdir + "/icon_drive.png",wx.BITMAP_TYPE_PNG))

		# assign image list:
		self.il = il
		self.GetTreeCtrl().SetImageList(il)

		## NOTE: The click event is bound in the Notebook.

class NoteBook(wx.Notebook):
	"""
	Used in the left part of the splitter in mainframe.
	Has two tabs - Folders and Pogs
	THIS IS THE VIEW or SOURCE of fonts.	
	"""
	def __init__(self, parent):
		wx.Notebook.__init__(self, parent, style=wx.NB_BOTTOM)
		self.imlist = wx.ImageList(16, 16)
		
		pan1 = wx.Panel(self)

		## THE DIR CONTROL
		self.dircontrol = DirControl(pan1) 

		## The Recurse check-box
		self.recurseFolders = wx.CheckBox(pan1, -1, _("Include sub-folders."))
		self.recurseFolders.SetValue( fpsys.config.recurseFolders )
		self.Bind(wx.EVT_CHECKBOX, self.__onDirCtrlClick, self.recurseFolders) #click on check box same as click on folder item.

		## Add them to a sizer
		box = wx.BoxSizer(wx.VERTICAL) 
		box.Add( self.dircontrol,1, wx.EXPAND ) 
		box.Add( self.recurseFolders,0,wx.EXPAND )
		pan1.SetSizer(box) 
		box.Layout() 

		self.pogindexselected = 0
	
		## The SOURCE POG control
		pan2 = wx.Panel(self) 
		page = 0
		s = None
		if fpsys.state.viewpattern  == "P": 
			s = fpsys.state.viewobject.name
			if s == "EMPTY": s= None #Very first run, the view will be an EMPTY object.
			page = 1
		self.ctrlPogSource = PogChooser(pan2, whoami="SOURCEPOG", select = s)

		
		ps.sub(source_pog_has_been_selected, self.OnViewPogClick) ##DND: class NoteBook
		ps.sub(select_no_view_pog, self.SelectNoView) ##DND: class NoteBook
		ps.sub( add_pog_item_to_source, self.AddItem ) #DND: class NoteBook
		ps.sub( remove_pog_item_from_source, self.RemoveItem ) #DND: class NoteBook

		# Get a ref to the dircontrol.
		self.tree = self.dircontrol.GetTreeCtrl()
		
		## Dud tree events, causing bad behaviour:
		## EVT_LIST_ITEM_SELECTED
		## EVT_LEFT_UP
		
		## Bind to another event solve the problem of EVT_LEFT_UP firing when the little
		## open-branch/tree arrow was pressed.
		## 5.3.2009 Michael Hoeft
		self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.__onDirCtrlClick) 	

		## Had a context menu, but not using it.
		#self.tree.Bind(wx.EVT_CONTEXT_MENU, self.OnContextMenu)

		box2 = wx.BoxSizer(wx.HORIZONTAL) 
		box2.Add(self.ctrlPogSource,1,wx.EXPAND) 
		pan2.SetSizer(box2) 
		box2.Layout() 
		
		self.AddPage(pan1, _("Folders"))
		self.AddPage(pan2, _("Pogs")) 
		
		source_pog_icon = self.imlist.Add( wx.Bitmap(fpsys.mythingsdir + "/icon_source_pog_16x16.png",wx.BITMAP_TYPE_PNG) )

		target_pog_icon = self.imlist.Add( wx.Bitmap(fpsys.mythingsdir + "/icon_source_folder_16x16.png",wx.BITMAP_TYPE_PNG) )
		
		self.AssignImageList(self.imlist)
		self.SetPageImage(1, source_pog_icon)
		self.SetPageImage(0, target_pog_icon)
		
		self.SetSelection(page)
	
		self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.onPageChanged) # Bind page changed event

		## If the app is started with a Folder as the Source, then
		## check if we must recurse. If so, fake a click to kick that off.
		if fpsys.state.viewpattern  == "F":
			if self.recurseFolders.GetValue():
				self.__onDirCtrlClick(None) # Fake an event
		

	def onPageChanged(self, e):
		self.ctrlPogSource.ClearLastIndex()
		if self.GetSelection() == 0: # The dircontrol
			## I want to force the dir control to clear the selection.
			## Reason: When you return to this control (from Pog page), the selection
			## from last visit is still there. Clicking on it again does NOT UPDATE
			## the font view. This is wierd. So, clearing the selection makes this moot.
			self.tree.UnselectAll() # Found this method in the wxpython book.

	#def OnContextMenu(self, event):
	#	# only do this part the first time so the events are only bound once
	#	if not hasattr(self, "popupID1"):
	#		self.popupID1 = wx.NewId()
	#		self.popupID2 = wx.NewId()
	#
	#		self.Bind(wx.EVT_MENU, self.OnPopupOne, id=self.popupID1)
	#		self.Bind(wx.EVT_MENU, self.OnPopupTwo, id=self.popupID2)

	#	# make a menu
	#	menu = wx.Menu()
		# Show how to put an icon in the menu
		#item = wx.MenuItem(menu, self.popupID1,"One")
		#bmp = images.getSmilesBitmap()
		#item.SetBitmap(bmp)
		#menu.AppendItem(item)
		# add some other items
	#	menu.Append(self.popupID1, _("Add fonts in this folder to a Pog.") )
	#	menu.Append(self.popupID2, _("Add fonts in this folder and sub-folders to a Pog.") )

		# Popup the menu.  If an item is selected then its handler
		# will be called before PopupMenu returns.
	#	self.PopupMenu(menu)
	#	menu.Destroy()

	#def OnPopupOne(self, event):
	#	print "\n"

	#def OnPopupTwo(self, event):
	#	print "Popup one\n"

	def __onDirCtrlClick(self, e):
		wx.BeginBusyCursor() #Thanks to Suzuki Alex on the wxpython list!
		p = self.dircontrol.GetPath()
		try:
			fpsys.instantiateViewFolder(p,self.recurseFolders.GetValue() )
			fpsys.config.lastdir = p
		except fontybugs.FolderHasNoFonts, e:
			pass # update_font_view handles this with a std message.
		
		ps.pub(reset_to_page_one)# reset before updating!		  
		ps.pub(update_font_view)
		
		wx.EndBusyCursor()
		wx.CallAfter( self.SetFocus )

	def OnViewPogClick(self, args):
		"""
		args[0] is pogname, args[1] is pognochange
		"""
		## Check pognochange, it means this is the same pog as last time.
		if args[1]: return 
		
		## instantiateViewPog calls pog.genList which bubbles:
		## PogInvalid
		## BUT - this error only makes sense from the
		## cli pov. By the time the gui is running, that
		## pog has been renamed .badpog and therefore 
		## won't even appear in the list. So, don't bother
		## catching it.
		fpsys.instantiateViewPog(args[0])

		if fpsys.state.samepogs: #forbid same pogs selection
			ps.pub(clear_targetpog_selection)
		else:
			ps.pub(reset_to_page_one)
		ps.pub(update_font_view)
	
	def AddItem(self, pogname):
		self.ctrlPogSource.AddItem(pogname[0]) #[0] bit is because pogname is a tuple from pubsub.

	def RemoveItem(self, pogname):
		self.ctrlPogSource.RemoveItem(pogname[0])
		
	def SelectNoView(self):
		## Purpose: To select no viewobject and clear view pog list selections
		## Called when a TARGET item is clicked AND samepogs it True
		wx.BeginBusyCursor()
		self.ctrlPogSource.ClearSelection()
		fpsys.SetViewPogToEmpty()
		wx.EndBusyCursor()