'''
Action Bar
==========

.. versionadded:: 1.8.0

.. image:: images/actionbar.png
    :align: right

The ActionBar widget is like Android's `ActionBar
<http://developer.android.com/guide/topics/ui/actionbar.html>`_, where items
are stacked horizontally. When the area becomes to small, widgets are moved
into the :class:`ActionOverflow` area.

An :class:`ActionBar` contains an :class:`ActionView` with various
:class:`ContextualActionViews <kivy.uix.actionbar.ContextualActionView>`.
An :class:`ActionView` will contain an :class:`ActionPrevious` having title,
app_icon and previous_icon properties. An :class:`ActionView` will contain
subclasses of :class:`ActionItems <ActionItem>`. Some predefined ones include
an :class:`ActionButton`, an :class:`ActionToggleButton`, an
:class:`ActionCheck`, an :class:`ActionSeparator` and an :class:`ActionGroup`.

An :class:`ActionGroup` is used to display :class:`ActionItems <ActionItem>`
in a group. An :class:`ActionView` will always display an :class:`ActionGroup`
after other :class:`ActionItems <ActionItem>`. An :class:`ActionView` contains
an :class:`ActionOverflow`, but this is only made visible when required i.e.
the available area is too small to fit all the widgets. A
:class:`ContextualActionView` is a subclass of an:class:`ActionView`.

.. versionchanged:: 1.10.1
    :class:`ActionGroup` core rewritten from :class:`Spinner` to pure
    :class:`DropDown`
'''

__all__ = ('ActionBarException', 'ActionItem', 'ActionButton',
           'ActionToggleButton', 'ActionCheck', 'ActionSeparator',
           'ActionDropDown', 'ActionGroup', 'ActionOverflow',
           'ActionView', 'ContextualActionView', 'ActionPrevious',
           'ActionBar')

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.dropdown import DropDown
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.checkbox import CheckBox
from kivy.uix.spinner import Spinner
from kivy.uix.label import Label
from kivy.config import Config
from kivy.properties import ObjectProperty, NumericProperty, BooleanProperty, \
    StringProperty, ListProperty, OptionProperty, AliasProperty
from kivy.metrics import sp
from kivy.lang import Builder
from functools import partial


window_icon = ''
if Config:
    window_icon = Config.get('kivy', 'window_icon')


class ActionBarException(Exception):
    '''
    ActionBarException class
    '''
    pass


class ActionItem(object):
    '''
    ActionItem class, an abstract class for all ActionBar widgets. To create a
    custom widget for an ActionBar, inherit from this class. See module
    documentation for more information.
    '''

    minimum_width = NumericProperty('90sp')
    '''
    Minimum Width required by an ActionItem.

    :attr:`minimum_width` is a :class:`~kivy.properties.NumericProperty` and
    defaults to '90sp'.
    '''

    def get_pack_width(self):
        return max(self.minimum_width, self.width)

    pack_width = AliasProperty(get_pack_width, bind=('minimum_width', 'width'))
    '''
    (read-only) The actual width to use when packing the item. Equal to the
    greater of minimum_width and width.

    :attr:`pack_width` is an :class:`~kivy.properties.AliasProperty`.
    '''

    important = BooleanProperty(False)
    '''
    Determines if an ActionItem is important or not. If an item is important
    and space is limited, this item will be displayed in preference to others.

    :attr:`important` is a :class:`~kivy.properties.BooleanProperty` and
    defaults to False.
    '''

    inside_group = BooleanProperty(False)
    '''
    (internal) Determines if an ActionItem is displayed inside an
    ActionGroup or not.

    :attr:`inside_group` is a :class:`~kivy.properties.BooleanProperty` and
    defaults to False.
    '''

    background_normal = StringProperty(
        'atlas://data/images/defaulttheme/action_item')
    '''
    Background image of the ActionItem used for the default graphical
    representation when the ActionItem is not pressed.

    :attr:`background_normal` is a :class:`~kivy.properties.StringProperty`
    and defaults to 'atlas://data/images/defaulttheme/action_item'.
    '''

    background_down = StringProperty(
        'atlas://data/images/defaulttheme/action_item_down')
    '''
    Background image of the ActionItem used for the default graphical
    representation when an ActionItem is pressed.

    :attr:`background_down` is a :class:`~kivy.properties.StringProperty`
    and defaults to 'atlas://data/images/defaulttheme/action_item_down'.
    '''

    mipmap = BooleanProperty(True)
    '''
    Defines whether the image/icon dispayed on top of the button uses a
    mipmap or not.

    :attr:`mipmap` is a :class:`~kivy.properties.BooleanProperty` and
    defaults to `True`.
    '''


class ActionButton(Button, ActionItem):
    '''
    ActionButton class, see module documentation for more information.

    The text color, width and size_hint_x are set manually via the Kv language
    file. It covers a lot of cases: with/without an icon, with/without a group
    and takes care of the padding between elements.

    You don't have much control over these properties, so if you want to
    customize it's appearance, we suggest you create you own button
    representation. You can do this by creating a class that subclasses an
    existing widget and an :class:`ActionItem`::

        class MyOwnActionButton(Button, ActionItem):
            pass

    You can then create your own style using the Kv language.
    '''

    icon = StringProperty(None, allownone=True)
    '''
    Source image to use when the Button is part of the ActionBar. If the
    Button is in a group, the text will be preferred.

    :attr:`icon` is a :class:`~kivy.properties.StringProperty` and defaults
    to None.
    '''


class ActionPrevious(BoxLayout, ActionItem):
    '''
    ActionPrevious class, see module documentation for more information.
    '''

    with_previous = BooleanProperty(True)
    '''
    Specifies whether clicking on ActionPrevious will load the previous
    screen or not. If True, the previous_icon will be shown otherwise it
    will not.

    :attr:`with_previous` is a :class:`~kivy.properties.BooleanProperty` and
    defaults to True.
    '''

    app_icon = StringProperty(window_icon)
    '''
    Application icon for the ActionView.

    :attr:`app_icon` is a :class:`~kivy.properties.StringProperty`
    and defaults to the window icon if set, otherwise
    'data/logo/kivy-icon-32.png'.
    '''

    app_icon_width = NumericProperty(0)
    '''
    Width of app_icon image.

    :attr:`app_icon_width` is a :class:`~kivy.properties.NumericProperty` and
    defaults to 0.
    '''

    app_icon_height = NumericProperty(0)
    '''
    Height of app_icon image.

 
   :attr:`app_icon_height` is a :class:`~kivy.properties.NumericProperty`
    and defaults to 0.
    '''

    color = ListProperty([1, 1, 1, 1])
    '''
    Text color, in the format (r, g, b, a)

    :attr:`color` is a :class:`~kivy.properties.ListProperty` and defaults
    to [1, 1, 1, 1].
    '''

    previous_image = StringProperty(
        'atlas://data/images/defaulttheme/previous_normal')
    '''
    Image for the 'previous' ActionButtons default graphica
l representation.

    :attr:`previous_image` is a :class:`~kivy.properties.StringProperty` and
    defaults to 'atlas://data/images/defaulttheme/previous_normal'.
    '''

    previous_image_width = NumericProperty(0)
    '''
    Width of previous_image image.

    :attr:`width` is a :class:`~kivy.properties.NumericProperty` and
    defaults to 0.
    '''

    previous_image_height = NumericProperty(0)
    '''
    Height of previous_image image.

    :attr:`app_icon_width` is a :class:`~kivy.properties.NumericProperty` and
    defaults to 0.
    '''

    title = StringProperty('')
    '''
    Title for ActionView.

    :attr:`title` is a :class:`~kivy.properties.StringProperty` and
    defaults to ''.
    '''

    markup = BooleanProperty(False)
    '''
    If True, the text will be rendered using the
    :class:`~kivy.core.text.markup.MarkupLabel`: you can change the style of
 