Robot Framework Integrated Development Environment (RIDE)
popupmenu.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import wx
17 from wx import Colour
18 
19 class PopupCreator():
20 
21  def __init__(self):
22  self._external_hooks_external_hooks = []
23 
24  def add_hook(self, hook):
25  self._external_hooks_external_hooks.append(hook)
26 
27  def remove_hook(self, hook):
28  self._external_hooks_external_hooks.remove(hook)
29 
30  def _get_all_actions(self, fixed_menu_items, data):
31  menu_items = fixed_menu_items
32  external_items = self._get_external_menu_items_get_external_menu_items(data)
33  if external_items:
34  menu_items.add_separator()
35  for item in external_items:
36  menu_items.add_menu_item(item)
37  return menu_items
38 
39  def _get_external_menu_items(self, data):
40  menu_items = []
41  for hook in self._external_hooks_external_hooks:
42  menu_items.extend(hook(data))
43  return menu_items
44 
45  def show(self, parent, fixed_menu_items, data):
46  PopupMenu(parent, self._get_all_actions_get_all_actions(fixed_menu_items, data))
47 
48 
49 class PopupMenu(wx.Menu):
50 
51  def __init__(self, parent, menu_items):
52  wx.Menu.__init__(self)
53  """
54  parent.SetBackgroundColour(Colour(200, 222, 40))
55  parent.SetOwnBackgroundColour(Colour(200, 222, 40))
56  parent.SetForegroundColour(Colour(7, 0, 70))
57  parent.SetOwnForegroundColour(Colour(7, 0, 70))
58  """
59  for item in menu_items:
60  if item.is_separator():
61  self.AppendSeparator()
62  else:
63  self._add_item_add_item(item)
64  parent.PopupMenu(self)
65  self.Destroy()
66 
67  def _add_item(self, item):
68  id_ = wx.NewIdRef()
69  self.Append(id_, item.name)
70  self.Bind(wx.EVT_MENU, item.callable, id=id_)
71 
72 
74 
75  def __init__(self, parent=None, menu_names=[]):
76  self._items_items = []
77  for item in menu_names:
78  self.add_menu_itemadd_menu_item(PopupMenuItem(item, parent=parent))
79 
80  def __iter__(self):
81  return iter(self._items_items)
82 
83  def add_menu_item(self, item):
84  self._items_items.append(item)
85 
86  def add_separator(self):
87  self.add_menu_itemadd_menu_item(PopupMenuItem('---'))
88 
89 
90 class PopupMenuItem():
91 
92  def __init__(self, name, callable=None, parent=None):
93  self.namename = name
94  self.callablecallable = self._get_callable_get_callable(name, callable, parent)
95 
96  def _get_callable(self, name, callable, parent):
97  if callable:
98  return callable
99  if name == '---':
100  return None
101  handler_name = ''.join(x for x in name.split('\t')[0].title() if not x.isspace())
102  return getattr(parent, 'On'+handler_name)
103 
104  def is_separator(self):
105  return self.namename == '---'
106 
107 
def _get_all_actions(self, fixed_menu_items, data)
Definition: popupmenu.py:30
def show(self, parent, fixed_menu_items, data)
Definition: popupmenu.py:45
def _get_callable(self, name, callable, parent)
Definition: popupmenu.py:96
def __init__(self, name, callable=None, parent=None)
Definition: popupmenu.py:92
def __init__(self, parent=None, menu_names=[])
Definition: popupmenu.py:75
def __init__(self, parent, menu_items)
Definition: popupmenu.py:51