/
popupwindow.py
123 lines (92 loc) · 3.7 KB
/
popupwindow.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
# Copyright 2008-2015 Nokia Solutions and Networks
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import wx
from robotide.context import POPUP_BACKGROUND, IS_WINDOWS
from robotide import utils
from robotide.widgets import Dialog, VerticalSizer, HtmlWindow
class _PopupWindowBase(object):
def __init__(self, size, detachable=True, autohide=False):
self.panel = self._create_ui(size)
if autohide:
self._set_auto_hiding()
if detachable:
self._set_detachable()
self.SetSize(size)
def _create_ui(self, size):
panel = wx.Panel(self)
panel.SetBackgroundColour(POPUP_BACKGROUND)
szr = VerticalSizer()
self._details = HtmlWindow(self, size=size)
szr.add_expanding(self._details)
panel.SetSizer(szr)
panel.Fit()
return panel
def _set_detachable(self):
self._details.Bind(wx.EVT_LEFT_UP, self._detach)
def _detach(self, event):
self.hide()
dlg = HtmlDialog(self._detached_title, self._current_details)
dlg.SetPosition((wx.GetMouseState().x, wx.GetMouseState().y))
dlg.Show()
if IS_WINDOWS:
event.Skip()
def show_at(self, position):
if not self.IsShown():
self.SetPosition(position)
self.Show()
def hide(self, event=None):
self.Show(False)
@property
def screen_position(self):
return self.ScreenPosition
@property
def size(self):
return self.Size
def set_content(self, content, title=None):
color = ''.join(hex(item)[2:] for item in POPUP_BACKGROUND)
self._current_details = '<body bgcolor=#%s>%s</body>' % (color, content)
self._details.SetPage(self._current_details)
self._detached_title = title
class RidePopupWindow(wx.PopupWindow, _PopupWindowBase):
def __init__(self, parent, size):
wx.PopupWindow.__init__(self, parent)
self.SetSize(size)
def _set_auto_hiding(self):
# EVT_LEAVE is triggered on different components on different OSes.
component_to_hide = self.panel if IS_WINDOWS else self
component_to_hide.Bind(wx.EVT_LEAVE_WINDOW, self.hide)
class HtmlPopupWindow(RidePopupWindow):
def __init__(self, parent, size, detachable=True, autohide=False):
RidePopupWindow.__init__(self, parent, size)
_PopupWindowBase.__init__(self, size, detachable, autohide)
class MacRidePopupWindow(wx.Frame, _PopupWindowBase):
def __init__(self, parent, size, detachable=True, autohide=False):
wx.Frame.__init__(self, parent, style=wx.SIMPLE_BORDER)
_PopupWindowBase.__init__(self, size, detachable, autohide)
self.hide()
def _set_auto_hiding(self):
self._details.Bind(wx.EVT_MOTION, lambda evt: self.hide())
def OnKey(self, *params):
pass
class HtmlDialog(Dialog):
def __init__(self, title, content):
Dialog.__init__(self, title)
szr = VerticalSizer()
szr.add_expanding(HtmlWindow(self, text=content, size=self.Size))
self.SetSizer(szr)
def OnKey(self, event):
pass
if wx.PlatformInfo[0] == '__WXMAC__':
RidePopupWindow = HtmlPopupWindow = MacRidePopupWindow
del MacRidePopupWindow