|
| 1 | + |
| 2 | +# this is a python object for helping in extending the menus |
| 3 | +# it is sort of based on the gnc-menu-extensions but does not use |
| 4 | +# the separate gnc-plugin-menu-additions plugin - it is simply |
| 5 | +# part of a python plugin module |
| 6 | +# it is a generic class in that could be used with many different |
| 7 | +# python plugins where the menu list is not predefined but is |
| 8 | +# determined at runtime |
| 9 | + |
| 10 | +import sys |
| 11 | + |
| 12 | +import gtk |
| 13 | + |
| 14 | +import gobject |
| 15 | + |
| 16 | + |
| 17 | +import pdb |
| 18 | + |
| 19 | + |
| 20 | +import gnucash_log |
| 21 | +from gnucash_log import ENTER |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +class PythonMenuAdditions(gobject.GObject): |
| 26 | + |
| 27 | + def __init__ (self, group_name, menu_list): |
| 28 | + |
| 29 | + #pdb.set_trace() |
| 30 | + |
| 31 | + super(PythonMenuAdditions,self).__init__() |
| 32 | + |
| 33 | + # the group_name MUST be different for each python plugin |
| 34 | + |
| 35 | + self.group_name = group_name |
| 36 | + self.menu_list = menu_list |
| 37 | + |
| 38 | + self.saved_windows = {} |
| 39 | + |
| 40 | + |
| 41 | + def add_to_window (self, window, window_type): |
| 42 | + |
| 43 | + #pdb.set_trace() |
| 44 | + |
| 45 | + #if hash(window) in self.saved_windows: |
| 46 | + # # what to do!! |
| 47 | + # print "add called more than once same window!!" |
| 48 | + # pdb.set_trace() |
| 49 | + # print "add called more than once same window!!" |
| 50 | + |
| 51 | + per_window = PythonPerWindowMenuAdditions(window, self) |
| 52 | + |
| 53 | + per_window.add_to_window(window, window_type) |
| 54 | + |
| 55 | + def remove_from_window (self, window, window_type): |
| 56 | + |
| 57 | + pdb.set_trace() |
| 58 | + |
| 59 | + #if hash(window) in self.saved_windows: |
| 60 | + |
| 61 | + per_window.remove_from_window(window, window_type) |
| 62 | + |
| 63 | + |
| 64 | + # can these be done once - or do they need to be done in the per window?? |
| 65 | + # I think so - they are only required to be unique within a given menu |
| 66 | + # the lookup is by the menu path |
| 67 | + |
| 68 | + def do_preassigned_accel (self, info, table): |
| 69 | + |
| 70 | + if info.assigned: |
| 71 | + return |
| 72 | + |
| 73 | + ## need utf8 validation |
| 74 | + #if g_utf8_validate(info.ae.label, -1, None): |
| 75 | + # info.accel_assigned = True |
| 76 | + # g_warning("Extension menu label '%s' is not valid utf8.", info->ae.label) |
| 77 | + # return |
| 78 | + |
| 79 | + # check for pre-assigned accelerator - that is contains(begins?) _ |
| 80 | + ptr = info.ae.label.find('_') |
| 81 | + if ptr < 0: |
| 82 | + return |
| 83 | + |
| 84 | + accel_key = info.ae.label[ptr+1] |
| 85 | + #DEBUG("Accelerator preassigned: '%s'", accel_key) |
| 86 | + |
| 87 | + # create python dict for C glib hash table |
| 88 | + if info.path in table: |
| 89 | + map = table[info.path] |
| 90 | + else: |
| 91 | + map = None |
| 92 | + |
| 93 | + #DEBUG("path '%s', map '%s' -> '%s'", info->path, map, new_map) |
| 94 | + |
| 95 | + table[info.path] = map + accel_key |
| 96 | + |
| 97 | + info.accel_assigned = True |
| 98 | + |
| 99 | + def assign_accel (self, info, table): |
| 100 | + |
| 101 | + if info.accel_assigned: |
| 102 | + return |
| 103 | + |
| 104 | + if info.path in table: |
| 105 | + map = table[info.path] |
| 106 | + else: |
| 107 | + map = "" |
| 108 | + |
| 109 | + #DEBUG("map '%s', path %s", map, info->path) |
| 110 | + |
| 111 | + found_ptr = "" |
| 112 | + found_buf = "" |
| 113 | + for ptr in info.ae.label: |
| 114 | + if not ptr.isalpha(): |
| 115 | + continue |
| 116 | + ptr = ptr.lower() |
| 117 | + found_buf = ptr |
| 118 | + if map.find(ptr) < 0: |
| 119 | + found_ptr = ptr |
| 120 | + break |
| 121 | + |
| 122 | + if found_ptr == "": |
| 123 | + info.accel_assigned = True |
| 124 | + return |
| 125 | + |
| 126 | + start = info.ae.label[0:found_ptr] |
| 127 | + new_label = start + '_' + found_ptr |
| 128 | + |
| 129 | + info.ae.label = new_label |
| 130 | + |
| 131 | + new_map = map + found_buf |
| 132 | + |
| 133 | + table[info.path] = new_map |
| 134 | + |
| 135 | + info.accel_assigned = True |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +class PythonPerWindowMenuAdditions(gobject.GObject): |
| 140 | + |
| 141 | + def __init__ (self, window, menu_additions): |
| 142 | + |
| 143 | + #pdb.set_trace() |
| 144 | + |
| 145 | + super(PythonPerWindowMenuAdditions,self).__init__() |
| 146 | + |
| 147 | + self.menu_additions = menu_additions |
| 148 | + |
| 149 | + self.window = window |
| 150 | + self.merge_id = None |
| 151 | + |
| 152 | + |
| 153 | + def add_menu_item (self, ui_manager, group, menuitm): |
| 154 | + |
| 155 | + # although it says add actions we are actually adding one |
| 156 | + |
| 157 | + # this is a version of the gnc_menu_additions_menu_setup_one function |
| 158 | + # in gnc-plugin-menu-additions.c |
| 159 | + |
| 160 | + #DEBUG( "Adding %s/%s [%s] as [%s]", ext_info->path, ext_info->ae.label, |
| 161 | + # ext_info->ae.name, ext_info->typeStr ); |
| 162 | + |
| 163 | + #print "group",group |
| 164 | + #print "menuitem",menuitm |
| 165 | + #print "menuitem",menuitm.ae.as_tuple() |
| 166 | + #pdb.set_trace() |
| 167 | + |
| 168 | + group.add_actions([menuitm.ae.as_tuple()],user_data=self.window) |
| 169 | + |
| 170 | + # so this implements the xml |
| 171 | + # with path giving the menu position |
| 172 | + # <menuitem name="Python Reports" action="pythonreportsAction"/> |
| 173 | + #print " preui",ui_manager.get_ui() |
| 174 | + #print "add_ui",self.merge_id,menuitm.path,menuitm.name,menuitm.action,menuitm.type |
| 175 | + ui_manager.add_ui(self.merge_id,menuitm.path,menuitm.name,menuitm.action,menuitm.type,False) |
| 176 | + |
| 177 | + #print "postui",ui_manager.get_ui() |
| 178 | + ui_manager.ensure_update() |
| 179 | + |
| 180 | + |
| 181 | + def add_to_window (self, window, window_type): |
| 182 | + |
| 183 | + print >> sys.stderr, "called add_to_window" |
| 184 | + #pdb.set_trace() |
| 185 | + |
| 186 | + ui_manager = self.window.get_uimanager() |
| 187 | + group = gtk.ActionGroup(self.menu_additions.group_name) |
| 188 | + |
| 189 | + window.set_translation_domain(group, "gnucash"); |
| 190 | + |
| 191 | + self.merge_id = ui_manager.new_merge_id() |
| 192 | + |
| 193 | + ui_manager.insert_action_group(group,0) |
| 194 | + |
| 195 | + # sort list |
| 196 | + # do accelerators |
| 197 | + # maybe we can do the above earlier |
| 198 | + |
| 199 | + # then add |
| 200 | + for menuitm in self.menu_additions.menu_list: |
| 201 | + self.add_menu_item(ui_manager,group,menuitm) |
| 202 | + |
| 203 | + ui_manager.ensure_update() |
| 204 | + |
| 205 | + # remember this function stores the group and merge_id to remove later |
| 206 | + # when the GncPlugin remove_from_window function is called |
| 207 | + # ah - this does not update any gtk stuff just gnc stuff handling the menu additions |
| 208 | + #gnc_main_window_manual_merge_actions("gnc-plugin-menu-additions-actions",group,self.merge_id) |
| 209 | + self.window.manual_merge_actions(self.menu_additions.group_name,group,self.merge_id) |
| 210 | + |
| 211 | + |
| 212 | + def remove_from_window (self, window, window_type): |
| 213 | + |
| 214 | + print >> sys.stderr, "called remove_from_window" |
| 215 | + pdb.set_trace() |
| 216 | + print >> sys.stderr, "called remove_from_window" |
| 217 | + |
| 218 | + # weird - I think both this code is done and the super class |
| 219 | + # because the subclass actions are done first in C |
| 220 | + |
| 221 | + # if we call manual_merge_actions I dont think we need the following |
| 222 | + |
| 223 | + group = window.get_action_group(self.menu_additions.group_name) |
| 224 | + |
| 225 | + if group != None: |
| 226 | + window.ui_merge.remove_action_group(group) |
| 227 | + |
| 228 | + # what I dont understand is the original C code does not attempt |
| 229 | + # to remove the UI for gcn-plugin-menu-additions - but does for |
| 230 | + # any other plugin |
| 231 | + window.ui_merge.remove_ui(self.merge_id) |
| 232 | + |
| 233 | + |
| 234 | + # another miss - in remove window the subclass actions are done first |
| 235 | + # then the superclass |
| 236 | + super(GncPluginPythonMenuAdditions,self).remove_from_window(window, window_type) |
| 237 | + |
0 commit comments