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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
|
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enth373ought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Enthought, Inc.
# Description: <Enthought pyface package component>
#------------------------------------------------------------------------------
""" An action manager item that represents an actual action. """
# Standard libary imports.
from inspect import getargspec
# Major package imports.
import wx
# Enthought library imports.
from enthought.traits.api import Any, Bool, HasTraits, Instance, Property, Str
# Local imports.
from action import Action
from action_event import ActionEvent
from action_manager_item import ActionManagerItem
# Map from Pyface action style to wx menu item/tool kind.
STYLE_TO_KIND_MAP = {
'push' : wx.ITEM_NORMAL,
'radio' : wx.ITEM_RADIO,
'toggle' : wx.ITEM_CHECK
}
class ActionItem(ActionManagerItem):
""" An action manager item that represents an actual action. """
#### 'ActionManagerItem' interface ########################################
# The item's unique identifier ('unique' in this case means unique within
# its group)
id = Property(Str)
#### 'ActionItem' interface ###############################################
# The action!
action = Instance(Action)
# The toolkit specific control created for this item.
control = Any
# The toolkit specific Id of the control created for this item.
#
# We have to keep the Id as well as the control because wx tool bar tools
# are created as 'wxObjectPtr's which do not have Ids, and the Id is
# required to manipulate the state of a tool via the tool bar 8^(
control_id = Any
###########################################################################
# 'ActionManagerItem' interface.
###########################################################################
def _get_id(self):
""" Return's the item's Id. """
return self.action.id
###########################################################################
# 'ActionItem' interface.
###########################################################################
def add_to_menu(self, parent, menu, controller):
""" Adds the item to a menu. """
if (controller is None) or controller.can_add_to_menu( self.action ):
widget = _MenuItem(parent, menu, self, controller)
# fixme: Martin, who uses this information?
if controller is None:
self.control = widget.control
self.control_id = widget.control_id
return
def add_to_toolbar(self, parent, tool_bar, image_cache, controller,
show_labels=True):
""" Adds the item to a tool bar. """
if (controller is None) or controller.can_add_to_toolbar(self.action):
widget = _Tool(
parent, tool_bar, image_cache, self, controller, show_labels
)
# fixme: Martin, who uses this information?
if controller is None:
self.control = widget.control
self.control_id = widget.control_id
return
def add_to_palette(self, tool_palette, image_cache, show_labels=True):
""" Adds the item to a tool palette. """
widget = _PaletteTool(tool_palette, image_cache, self, show_labels)
return
def destroy(self):
""" Called when the action is no longer required.
By default this method calls 'destroy' on the action itself.
"""
self.action.destroy()
return
class _MenuItem(HasTraits):
""" A menu item representation of an action item. """
#### '_MenuItem' interface ################################################
# Is the item checked?
checked = Bool(False)
# A controller object we delegate taking actions through (if any).
controller = Any
# Is the item enabled?
enabled = Bool(True)
# The radio group we are part of (None if the menu item is not part of such
# a group).
group = Any
###########################################################################
# 'object' interface.
###########################################################################
def __init__(self, parent, menu, item, controller):
""" Creates a new menu item for an action item. """
self.item = item
# Create an appropriate menu item depending on the style of the action.
#
# N.B. Don't try to use -1 as the Id for the menu item... wx does not
# ---- like it!
action = self.item.action
label = action.name
kind = STYLE_TO_KIND_MAP[action.style]
longtip = action.description
if len(action.accelerator) > 0:
label = label + '\t' + action.accelerator
self.control_id = wx.NewId()
self.control = wx.MenuItem(menu, self.control_id, label, longtip, kind)
menu.AppendItem(self.control)
# Set the initial enabled/disabled state of the action.
self.control.Enable(action.enabled)
# Set the initial checked state.
if action.style in ['radio', 'toggle']:
self.control.Check(action.checked)
# Wire it up...create an ugly flag since some platforms dont skip the
# event when we thought they would
self._skip_menu_event = False
wx.EVT_MENU(parent, self.control_id, self._on_menu)
# Listen for trait changes on the action (so that we can update its
# enabled/disabled/checked state etc).
action.on_trait_change(self._on_action_enabled_changed, 'enabled')
action.on_trait_change(self._on_action_checked_changed, 'checked')
if controller is not None:
self.controller = controller
controller.add_to_menu(self)
return
###########################################################################
# Private interface.
###########################################################################
#### Trait event handlers #################################################
def _enabled_changed(self):
""" Called when our 'enabled' trait is changed. """
self.control.Enable(self.enabled)
return
def _checked_changed(self):
""" Called when our 'checked' trait is changed. """
if self.item.action.style == 'radio':
# fixme: Not sure why this is even here, we had to guard it to
# make it work? Must take a look at svn blame!
if self.group is not None:
self.group.menu_checked( self )
self.control.Check(self.checked)
return
def _on_action_enabled_changed(self, action, trait_name, old, new):
""" Called when the enabled trait is changed on an action. """
self.control.Enable(action.enabled)
return
def _on_action_checked_changed(self, action, trait_name, old, new):
""" Called when the checked trait is changed on an action. """
if action.style == 'radio':
# If we're turning this one on, then we need to turn all the others
# off. But if we're turning this one off, don't worry about the
# others.
if new:
for item in self.item.parent.items:
if item is not self.item:
item.action.checked = False
# This will *not* emit a menu event because of this ugly flag
self._skip_menu_event = True
self.control.Check(action.checked)
self._skip_menu_event = False
return
#### wx event handlers ####################################################
def _on_menu(self, event):
""" Called when the menu item is clicked. """
# if the ugly flag is set, do not perform the menu event
if self._skip_menu_event:
return
action = self.item.action
action_event = ActionEvent()
is_checkable = action.style in ['radio', 'toggle']
# Perform the action!
if self.controller is not None:
if is_checkable:
# fixme: There is a difference here between having a controller
# and not in that in this case we do not set the checked state
# of the action! This is confusing if you start off without a
# controller and then set one as the action now behaves
# differently!
self.checked = self.control.IsChecked() == 1
# fixme: the 'perform' method without taking an event is
# deprecated!
args, varargs, varkw, dflts = getargspec(self.controller.perform)
# If the only arguments are 'self' and 'action' then this is the
# DEPRECATED interface.
if len(args) == 2:
self.controller.perform(action)
else:
self.controller.perform(action, action_event)
else:
if is_checkable:
action.checked = self.control.IsChecked() == 1
# fixme: the 'perform' method without taking an event is
# deprecated!
args, varargs, varkw, dflts = getargspec(action.perform)
# If the only argument is 'self' then this is the DEPRECATED
# interface.
if len(args) == 1:
action.perform()
else:
action.perform(action_event)
return
class _Tool(HasTraits):
""" A tool bar tool representation of an action item. """
#### '_Tool' interface ####################################################
# Is the item checked?
checked = Bool(False)
# A controller object we delegate taking actions through (if any).
controller = Any
# Is the item enabled?
enabled = Bool(True)
# The radio group we are part of (None if the tool is not part of such a
# group).
group = Any
###########################################################################
# 'object' interface.
###########################################################################
def __init__(self, parent, tool_bar, image_cache, item, controller,
show_labels):
""" Creates a new tool bar tool for an action item. """
self.item = item
self.tool_bar = tool_bar
# Create an appropriate tool depending on the style of the action.
action = self.item.action
label = action.name
# Tool bar tools never have '...' at the end!
if label.endswith('...'):
label = label[:-3]
# And they never contain shortcuts.
label = label.replace('&', '')
image = action.image.create_image(self.tool_bar.GetToolBitmapSize())
path = action.image.absolute_path
bmp = image_cache.get_bitmap(path)
kind = STYLE_TO_KIND_MAP[action.style]
tooltip = action.tooltip
longtip = action.description
if not show_labels:
label = ''
else:
self.tool_bar.SetSize((-1, 50))
self.control_id = wx.NewId()
self.control = tool_bar.AddLabelTool(
self.control_id, label, bmp, wx.NullBitmap, kind, tooltip, longtip
)
# Set the initial checked state.
tool_bar.ToggleTool(self.control_id, action.checked)
# Set the initial enabled/disabled state of the action.
tool_bar.EnableTool(self.control_id, action.enabled)
# Wire it up.
wx.EVT_TOOL(parent, self.control_id, self._on_tool)
# Listen for trait changes on the action (so that we can update its
# enabled/disabled/checked state etc).
action.on_trait_change(self._on_action_enabled_changed, 'enabled')
action.on_trait_change(self._on_action_checked_changed, 'checked')
if controller is not None:
self.controller = controller
controller.add_to_toolbar(self)
return
###########################################################################
# Private interface.
###########################################################################
#### Trait event handlers #################################################
def _enabled_changed(self):
""" Called when our 'enabled' trait is changed. """
self.tool_bar.EnableTool(self.control_id, self.enabled)
return
def _checked_changed(self):
""" Called when our 'checked' trait is changed. """
action = self.item.action
if action.style == 'radio':
self.group.toolbar_checked(self)
self.tool_bar.ToggleTool(self.control_id, self.checked)
return
def _on_action_enabled_changed(self, action, trait_name, old, new):
""" Called when the enabled trait is changed on an action. """
self.tool_bar.EnableTool(self.control_id, action.enabled)
return
def _on_action_checked_changed(self, action, trait_name, old, new):
""" Called when the checked trait is changed on an action. """
if action.style == 'radio':
# If we're turning this one on, then we need to turn all the others
# off. But if we're turning this one off, don't worry about the
# others.
if new:
for item in self.item.parent.items:
if item is not self.item:
item.action.checked = False
# This will *not* emit a tool event.
self.tool_bar.ToggleTool(self.control_id, new)
return
#### wx event handlers ####################################################
def _on_tool(self, event):
""" Called when the tool bar tool is clicked. """
action = self.item.action
action_event = ActionEvent()
is_checkable = (action.style == 'radio' or action.style == 'check')
# Perform the action!
if self.controller is not None:
# fixme: There is a difference here between having a controller
# and not in that in this case we do not set the checked state
# of the action! This is confusing if you start off without a
# controller and then set one as the action now behaves
# differently!
self.checked = self.tool_bar.GetToolState(self.control_id) == 1
# fixme: the 'perform' method without taking an event is
# deprecated!
args, varargs, varkw, dflts = getargspec(self.controller.perform)
# If the only argument is 'self' then this is the DEPRECATED
# interface.
if len(args) == 2:
self.controller.perform(action)
else:
self.controller.perform(action, action_event)
else:
action.checked = self.tool_bar.GetToolState(self.control_id) == 1
# fixme: the 'stop' method without taking a context is deprecated!
args, varargs, varkw, dflts = getargspec(action.perform)
# If the only argument is 'self' then this is the DEPRECATED
# interface.
if len(args) == 1:
action.perform()
else:
action.perform(action_event)
return
class _PaletteTool(HasTraits):
""" A tool palette representation of an action item. """
#### '_Tool' interface ####################################################
# The radio group we are part of (None if the tool is not part of such a
# group).
group = Any
def __init__(self, tool_palette, image_cache, item, show_labels):
""" Creates a new tool palette tool for an action item. """
self.item = item
self.tool_palette = tool_palette
action = self.item.action
label = action.name
# Tool palette tools never have '...' at the end.
if label.endswith('...'):
label = label[:-3]
# And they never contain shortcuts.
label = label.replace('&', '')
image = action.image.create_image()
path = action.image.absolute_path
bmp = image_cache.get_bitmap(path)
kind = action.style
tooltip = action.tooltip
longtip = action.description
if not show_labels:
label = ''
# Add the tool to the tool palette.
self.tool_id = tool_palette.add_tool(label, bmp, kind, tooltip,longtip)
tool_palette.toggle_tool(self.tool_id, action.checked)
tool_palette.enable_tool(self.tool_id, action.enabled)
tool_palette.on_tool_event(self.tool_id, self._on_tool)
# Listen to the trait changes on the aciton (so that we can update its
# enabled/disabled/checked state etc).
action.on_trait_change(self._on_action_enabled_changed, 'enabled')
action.on_trait_change(self._on_action_checked_changed, 'checked')
return
###########################################################################
# Private interface.
###########################################################################
#### Trait event handlers #################################################
def _on_action_enabled_changed(self, action, trait_name, old, new):
""" Called when the enabled trait is changed on an action. """
self.tool_palette.enable_tool(self.tool_id, action.enabled)
return
def _on_action_checked_changed(self, action, trait_name, old, new):
""" Called when the checked trait is changed on an action. """
if action.style == 'radio':
# If we're turning this one on, then we need to turn all the others
# off. But if we're turning this one off, don't worry about the
# others.
if new:
for item in self.item.parent.items:
if item is not self.item:
item.action.checked = False
# This will *not* emit a tool event.
self.tool_palette.toggle_tool(self.tool_id, new)
return
#### Tool palette event handlers ##########################################
def _on_tool(self, event):
""" Called when the tool palette button is clicked. """
action = self.item.action
is_checkable = (action.style == 'radio' or action.style == 'check')
# Perform the action!
action.checked = self.tool_palette.get_tool_state(self.tool_id) == 1
action.perform()
return
#### EOF ######################################################################
|