# Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Rickard Lindberg, Roger Lindberg
#
# This file is part of Timeline.
#
# Timeline is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Timeline is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Timeline. If not, see <http://www.gnu.org/licenses/>.
import os
import wx
from timelinelib.config.paths import ICONS_DIR
# Border, in pixels, between controls in a window (should always be used when
# border is needed)
BORDER = 5
unlock_function = None
[docs]class WildcardHelper:
[docs] def __init__(self, name, extensions):
self.name = name
self.ext_data = {}
self.ext_names = []
self._extract_ext_info(extensions)
[docs] def wildcard_string(self):
return "%s (%s)|%s" % (
self.name,
", ".join(["*." + e for e in self.ext_names]),
";".join(["*." + e for e in self.ext_names]))
[docs] def get_path(self, dialog):
path = dialog.GetPath()
for ext_name in self.ext_names:
if path.endswith("." + ext_name):
return path
return "%s.%s" % (path, self.ext_names[0])
[docs] def get_extension_data(self, path):
split_path = path.split(".")
if len(split_path) > 1:
ext_name = split_path[-1]
return self.ext_data.get(ext_name, None)
return None
def _extract_ext_info(self, extensions):
for ext in extensions:
if isinstance(ext, tuple):
name, data = ext
self.ext_data[name] = data
self.ext_names.append(name)
else:
self.ext_names.append(ext)
def _set_focus_and_select(ctrl):
ctrl.SetFocus()
if hasattr(ctrl, "SelectAll"):
ctrl.SelectAll()
[docs]def display_error_message(message, parent=None, yesno=False):
"""Display an error message in a modal dialog box"""
if yesno:
style = wx.YES_NO | wx.ICON_ERROR
else:
style = wx.OK | wx.ICON_ERROR
dial = wx.MessageDialog(parent, message, _("Error"), style)
return dial.ShowModal()
[docs]def display_warning_message(message, parent=None):
dial = wx.MessageDialog(parent, message, _("Warning"), wx.OK | wx.ICON_WARNING)
dial.ShowModal()
[docs]def get_user_ack(question, parent=None):
return wx.MessageBox(question, _("Question"),
wx.YES_NO | wx.CENTRE | wx.NO_DEFAULT, parent) == wx.YES
def _ask_question(question, parent=None):
"""Ask a yes/no question and return the reply."""
return wx.MessageBox(question, _("Question"),
wx.YES_NO | wx.CENTRE | wx.NO_DEFAULT, parent)
[docs]def set_wait_cursor(parent):
parent.SetCursor(wx.StockCursor(wx.CURSOR_WAIT))
[docs]def set_default_cursor(parent):
parent.SetCursor(wx.Cursor(wx.CURSOR_DEFAULT))
[docs]def set_focus(parent, name):
for child in parent.GetChildren():
if child.GetName() == name:
child.SetFocus()
break
[docs]def register_unlock_function(function):
global unlock_function
unlock_function = function
[docs]def show_dialog(factory_function):
dialog = factory_function()
dialog.ShowModal()
dialog.Destroy()
[docs]def load_icon_bundle():
bundle = wx.IconBundle()
for size in ["16", "32", "48"]:
iconpath = os.path.join(ICONS_DIR, "%s.png" % size)
icon = wx.Icon(wx.Bitmap(wx.Image(iconpath)))
bundle.AddIcon(icon)
return bundle