This file is indexed.

/usr/share/pyshared/PythonCard/tools/resourceEditor/modules/menuDialog.py is in python-pythoncard 0.8.2-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
"""
__version__ = "$Revision: 1.16 $"
__date__ = "$Date: 2004/08/22 19:11:35 $"
"""

from PythonCard import log, model, resource
import os
import wx


MENULIST_PADDING = '....'


def menuItemAttributes(menuItem):
    desc =  "                  {'type':'MenuItem',\n"
    desc += "                   'name':'%s',\n" % menuItem['name']
    # KEA 2002-05-16
    # work on string repr to get strings with mixed ' and " to work correctly
    if menuItem['shortcut'] == '':
        desc += """                   'label':%s,\n""" % repr(menuItem['label'])
    else:
        desc += """                   'label':%s,\n""" % repr(menuItem['label'] + '\t' + menuItem['shortcut'])
    try:
        if menuItem['command'] is not None:
            desc += "                   'command':'%s',\n" % menuItem['command']
    except:
        pass
    try:
        if not menuItem['enabled']:
            desc += "                   'enabled':0,\n"
    except:
        pass
    try:
        if menuItem['checkable']:
            desc += "                   'checkable':1,\n"
        if menuItem['checked']:
            desc += "                   'checked':1,\n"
    except:
        pass
    desc += "                  },\n"
    return desc

def menuAttributes(menu):
    desc =  "            {'type':'Menu',\n"
    desc += "              'name':'%s',\n" % menu['name']
    desc += """              'label':%s,\n""" % repr(menu['label'])
    desc += "              'items': [\n"
    
    return desc

def menuResourceFromList(menuList):
    #desc =  "    'menubar': {'type':'MenuBar',\n"
    desc =  "{'type':'MenuBar',\n"
    desc += "         'menus': [\n"

    inMenu = 0
    for m in menuList:
        if m['type'] == 'Menu':
            if inMenu:
                # close Menu
                desc += "               ]\n"
                desc += "             },\n"
            desc += menuAttributes(m)
            inMenu = 1
        else:
            desc += menuItemAttributes(m)
            

    # close Menu
    desc += "               ]\n"
    desc += "             },\n"
    # close MenuBar
    desc += "         ]\n"
    desc += "}\n"
    d = eval(desc)
    return resource.Resource(d)


class MenuDialog(model.CustomDialog):
    def __init__(self, aBg, rsrc):
        model.CustomDialog.__init__(self, aBg)
        
        self.parent = aBg
        
        # if some special setup is necessary, do it here
        if rsrc is not None:
            #aBg.printMenubar(rsrc)
            self.menuList = self.parseMenus(rsrc)
        else:
            self.menuList = []
        for m in self.menuList:
            if m['type'] == 'Menu':
                self.components.listMenus.append(m['label'])
            else:
                self.components.listMenus.append(MENULIST_PADDING + m['label'])
        #self.components.listMenus.items = self.parseMenus(rsrc)

        # Esc doesn't seem to work, it ends up closing the dialog
        # so need to work on that
        self.keyCodes = {wx.WXK_ESCAPE:'ESC',
                         wx.WXK_SPACE:'Space',
                         wx.WXK_DELETE:'Del',
                         wx.WXK_F1:'F1',
                         wx.WXK_F2:'F2',
                         wx.WXK_F3:'F3',
                         wx.WXK_F4:'F4',
                         wx.WXK_F5:'F5',
                         wx.WXK_F6:'F6',
                         wx.WXK_F7:'F7',
                         wx.WXK_F8:'F8',
                         wx.WXK_F9:'F9',
                         wx.WXK_F10:'F10',
                         wx.WXK_F11:'F11',
                         wx.WXK_F12:'F12',
                         }
        #self.components.fldName.text = rsrc.application.name
        #self.components.fldTitle.text = rsrc.application.title
        #self.components.fldPosition.text = str(rsrc.application.position)
        #self.components.fldSize.text = str(rsrc.application.size)
        #self.components.chkStatusBar.checked = rsrc.application.statusBar

    def buildMenu(self, name, label):
        m = {}
        m['type'] = 'Menu'
        m['name'] = name
        m['label'] = label
        return m

    def buildMenuItem(self, name, label, shortcut, command, enabled, checkable, checked):
        m = {}
        m['type'] = 'MenuItem'
        m['name'] = name
        m['label'] = label
        m['shortcut'] = shortcut
        m['command'] = command
        m['enabled'] = enabled
        m['checkable'] = checkable
        m['checked'] = checked
        return m

    def parseMenus(self, menubar):
        menuList = []
        for menu in menubar.menus:
            #menuList.append(menu.label)
            #print menu.type, menu.name, menu.label
            menuList.append(self.buildMenu(menu.name, menu.label))
            for menuItem in menu.items:
                itemParts = menuItem.label.split("\t")
                label = itemParts[0]
                try:
                    shortcut = itemParts[1]
                except:
                    shortcut = ''
                #menuList.append("....%s" % itemParts[0])
                #print menuItem.type, menuItem.name, itemParts, menuItem.command, menuItem.enabled, menuItem.checkable, menuItem.checked
                menuList.append(self.buildMenuItem(menuItem.name, label, shortcut, menuItem.command, menuItem.enabled, menuItem.checkable, menuItem.checked))
        return menuList

    def on_fldShortcut_keyDown(self, event):
        # this should handle the special key codes
        keyCode = event.keyCode
        if keyCode > 32 and keyCode < 127:
            keyStr = chr(keyCode).upper()
        elif keyCode in self.keyCodes:
            keyStr = self.keyCodes[keyCode]
        else:
            event.target.text = ''
            return

        if event.shiftDown:
            keyStr = 'Shift+' + keyStr
        if event.altDown:
            keyStr = 'Alt+' + keyStr
        if event.controlDown:
            keyStr = 'Ctrl+' + keyStr
        if len(keyStr) > 1:
            # don't allow just a number or letter
            # without a modifier
            # might also need a leading Ctrl or Alt
            event.target.text = keyStr

    def on_fldShortcut_keyPress(self, event):
        pass

    def on_fldName_loseFocus(self, event):
        sel = self.components.listMenus.selection
        try:
            self.menuList[sel]['name'] = event.target.text
            log.info(self.menuList[sel])
        except:
            pass

    def on_fldLabel_loseFocus(self, event):
        def normalize(label):
            name = label.replace("&", "").replace(".", "").replace(" ", "")
            return name

        sel = self.components.listMenus.selection
        try:
            label = event.target.text
            if self.menuList[sel]['type'] == 'Menu' and self.menuList[sel]['label'] == 'New Menu':
                oldname = self.menuList[sel]['name']
                if oldname == 'menuNewMenu':
                    name = 'menu'+normalize(label)
                    self.menuList[sel]['name'] = name
                    self.components.fldName.text = name
            elif self.menuList[sel]['type'] == 'MenuItem' and self.menuList[sel]['label'] == 'New Item':
                oldname = self.menuList[sel]['name']
                menuname = 'menuMenu'
                for i in range(sel+1):
                    if self.menuList[sel-i]['type'] == 'Menu':
                        menuname = self.menuList[sel-i]['name'] 
                        break
                if oldname == menuname+'NewItem':
                    name = menuname+normalize(label)
                    self.menuList[sel]['name'] = name
                    self.components.fldName.text = name
            self.menuList[sel]['label'] = label
            if self.menuList[sel]['type'] == 'MenuItem':
                label = MENULIST_PADDING + label
            self.components.listMenus.setString(sel, label)
            log.info(self.menuList[sel])
        except:
            pass

    def on_fldShortcut_loseFocus(self, event):
        sel = self.components.listMenus.selection
        try:
            if self.menuList[sel]['type'] == 'MenuItem':
                self.menuList[sel]['shortcut'] = event.target.text
            log.info(self.menuList[sel])
        except:
            pass

    def on_fldCommand_loseFocus(self, event):
        sel = self.components.listMenus.selection
        try:
            if self.menuList[sel]['type'] == 'MenuItem':
                if event.target.text == '':
                    self.menuList[sel]['command'] = None
                else:
                    self.menuList[sel]['command'] = event.target.text
            log.info(self.menuList[sel])
        except:
            pass

    def on_chkEnabled_mouseClick(self, event):
        sel = self.components.listMenus.selection
        try:
            if self.menuList[sel]['type'] == 'MenuItem':
                self.menuList[sel]['enabled'] = event.target.checked
            log.info(self.menuList[sel])
        except:
            pass

    def on_chkCheckable_mouseClick(self, event):
        sel = self.components.listMenus.selection
        try:
            if self.menuList[sel]['type'] == 'MenuItem':
                self.menuList[sel]['checkable'] = event.target.checked
            log.info(self.menuList[sel])
        except:
            pass

    def on_chkChecked_mouseClick(self, event):
        sel = self.components.listMenus.selection
        try:
            if self.menuList[sel]['type'] == 'MenuItem':
                self.menuList[sel]['checked'] = event.target.checked
            log.info(self.menuList[sel])
        except:
            pass

    def displayItemAttributes(self, sel):
        m = self.menuList[sel]
        self.components.fldListIndex.text = str(sel)
        self.components.fldName.text = m['name']
        self.components.fldLabel.text = m['label']
        if m['type'] == 'MenuItem':
            self.components.fldShortcut.text = m['shortcut']
            if m['command'] is None:
                self.components.fldCommand.text = ''
            else:
                self.components.fldCommand.text = m['command']
            self.components.chkEnabled.checked = m['enabled']
            self.components.chkCheckable.checked = m['checkable']
            self.components.chkChecked.checked = m['checked']
            
            self.components.stcShortcut.visible = 1
            self.components.stcCommand.visible = 1
            self.components.fldShortcut.visible = 1
            self.components.fldCommand.visible = 1
            self.components.chkEnabled.visible = 1
            self.components.chkCheckable.visible = 1
            self.components.chkChecked.visible = 1
        else:
            self.components.stcShortcut.visible = 0
            self.components.stcCommand.visible = 0
            self.components.fldShortcut.visible = 0
            self.components.fldCommand.visible = 0
            self.components.chkEnabled.visible = 0
            self.components.chkCheckable.visible = 0
            self.components.chkChecked.visible = 0

    def on_listMenus_select(self, event):
        self.displayItemAttributes(event.target.selection)

    def rebuildListMenus(self, sel=-1):
        self.components.listMenus.clear()
        for m in self.menuList:
            if m['type'] == 'Menu':
                self.components.listMenus.append(m['label'])
            else:
                self.components.listMenus.append(MENULIST_PADDING + m['label'])
        if sel != -1:
            self.components.listMenus.selection = sel
            
    def on_btnUp_mouseClick(self, event):
        sel = self.components.listMenus.selection
        # a selection of -1 means no selection
        # a selection of 0 is the first item in the list
        if sel > 0:
            temp = self.menuList[sel]
            self.menuList[sel] = self.menuList[sel - 1]
            self.menuList[sel - 1] = temp
            self.rebuildListMenus(sel - 1)

    def on_btnDown_mouseClick(self, event):
        sel = self.components.listMenus.selection
        if sel != -1 and sel < len(self.menuList) - 1:
            temp = self.menuList[sel]
            self.menuList[sel] = self.menuList[sel + 1]
            self.menuList[sel + 1] = temp
            self.rebuildListMenus(sel + 1)

    def on_btnDelete_mouseClick(self, event):
        sel = self.components.listMenus.selection
        if sel != -1:
            del self.menuList[sel]
            self.components.listMenus.delete(sel)
            if len(self.menuList) > 0:
                if sel > 0:
                    sel = sel - 1
                self.components.listMenus.selection = sel
                self.displayItemAttributes(sel)

    def on_btnNewMenu_mouseClick(self, event):
        sel = self.components.listMenus.selection
        self.menuList.append(" ") # extend list
        if sel == -1:
            sel = len(self.menuList) - 1
        else:
            self.menuList[sel+1:] = self.menuList[sel:-1]
            sel = sel+1
        self.menuList[sel] = self.buildMenu('menuNewMenu', 'New Menu')
        self.rebuildListMenus(sel)
        self.displayItemAttributes(sel)

    def on_btnNewMenuItem_mouseClick(self, event):
        sel = self.components.listMenus.selection
        self.menuList.append(" ") # extend list
        if sel == -1:
            sel = len(self.menuList) - 1
        else:
            self.menuList[sel+1:] = self.menuList[sel:-1]
            sel = sel+1
        name = 'menuMenu'
        for i in range(1, sel+1):
            if self.menuList[sel-i]['type'] == 'Menu':
                name = self.menuList[sel-i]['name'] 
                break
        self.menuList[sel] = self.buildMenuItem(name+'NewItem', 'New Item', '', None, 1, 0, 0)
        self.rebuildListMenus(sel)
        self.displayItemAttributes(sel)


def menuDialog(parent, rsrc):
    dlg = MenuDialog(parent, rsrc)
    result = dlg.showModal()
    if result.accepted:
        if len(dlg.menuList) == 0:
            result.menubar = None
        else:
            result.menubar = menuResourceFromList(dlg.menuList)
    dlg.destroy()
    return result