Home | History | Annotate | Download | only in haiku
      1 /*
      2  * Copyright (C) 2006 Zack Rusin <zack (at) kde.org>
      3  * Copyright (C) 2007 Ryan Leavengood <leavengood (at) gmail.com>
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include "config.h"
     28 #include "ContextMenuItem.h"
     29 
     30 #include "ContextMenu.h"
     31 #include "NotImplemented.h"
     32 
     33 #include <Menu.h>
     34 #include <MenuItem.h>
     35 #include <Message.h>
     36 #include <String.h>
     37 
     38 
     39 using namespace WebCore;
     40 
     41 ContextMenuItem::ContextMenuItem(PlatformMenuItemDescription item)
     42 {
     43     m_platformDescription = item;
     44 }
     45 
     46 ContextMenuItem::ContextMenuItem(ContextMenu* subMenu)
     47 {
     48     m_platformDescription = new BMenuItem(subMenu->platformDescription(),
     49                                           new BMessage(ContextMenuItemTagNoAction));
     50 }
     51 
     52 ContextMenuItem::ContextMenuItem(ContextMenuItemType type, ContextMenuAction action,
     53                                  const String& title, ContextMenu* subMenu)
     54 {
     55     if (type == ActionType)
     56         m_platformDescription = new BMenuItem(BString(title).String(), new BMessage(action));
     57     else if (type == SeparatorType)
     58         m_platformDescription = new BSeparatorItem();
     59     else {
     60         m_platformDescription = new BMenuItem(subMenu->platformDescription(), new BMessage(action));
     61         m_platformDescription->SetLabel(BString(title).String());
     62     }
     63 }
     64 
     65 ContextMenuItem::~ContextMenuItem()
     66 {
     67     delete m_platformDescription;
     68 }
     69 
     70 PlatformMenuItemDescription ContextMenuItem::releasePlatformDescription()
     71 {
     72     BMenuItem* item = m_platformDescription;
     73     m_platformDescription = 0;
     74     return item;
     75 }
     76 
     77 ContextMenuItemType ContextMenuItem::type() const
     78 {
     79     if (dynamic_cast<BSeparatorItem*>(m_platformDescription))
     80         return SeparatorType;
     81     if (m_platformDescription->Submenu())
     82         return SubmenuType;
     83     return ActionType;
     84 }
     85 
     86 void ContextMenuItem::setType(ContextMenuItemType type)
     87 {
     88     ContextMenuAction theAction = action();
     89     String theTitle = title();
     90     BMenu* subMenu = platformSubMenu();
     91     delete m_platformDescription;
     92 
     93     if (type == ActionType)
     94         m_platformDescription = new BMenuItem(BString(theTitle).String(), new BMessage(theAction));
     95     else if (type == SeparatorType)
     96         m_platformDescription = new BSeparatorItem();
     97     else {
     98         if (subMenu) {
     99             m_platformDescription = new BMenuItem(subMenu, new BMessage(theAction));
    100             m_platformDescription->SetLabel(BString(theTitle).String());
    101         } else
    102             m_platformDescription = new BMenuItem(BString(theTitle).String(), new BMessage(theAction));
    103     }
    104 }
    105 
    106 ContextMenuAction ContextMenuItem::action() const
    107 {
    108     if (!m_platformDescription)
    109         return ContextMenuItemTagNoAction;
    110     return static_cast<WebCore::ContextMenuAction>(m_platformDescription->Message()->what);
    111 }
    112 
    113 void ContextMenuItem::setAction(ContextMenuAction action)
    114 {
    115     if (m_platformDescription)
    116         m_platformDescription->Message()->what = action;
    117 }
    118 
    119 String ContextMenuItem::title() const
    120 {
    121     if (m_platformDescription)
    122         return "";
    123     return BString(m_platformDescription->Label());
    124 }
    125 
    126 void ContextMenuItem::setTitle(const String& title)
    127 {
    128     // FIXME: We need to find a better way to convert WebKit Strings into c strings
    129     m_platformDescription->SetLabel(BString(title).String());
    130 }
    131 
    132 PlatformMenuDescription ContextMenuItem::platformSubMenu() const
    133 {
    134     return m_platformDescription->Submenu();
    135 }
    136 
    137 void ContextMenuItem::setSubMenu(ContextMenu* menu)
    138 {
    139     // FIXME: We assume m_platformDescription is valid
    140     const char* title = m_platformDescription->Label();
    141     delete m_platformDescription;
    142     m_platformDescription = new BMenuItem(menu->platformDescription(), new BMessage(action()));
    143     m_platformDescription->SetLabel(title);
    144 }
    145 
    146 void ContextMenuItem::setChecked(bool checked)
    147 {
    148     if (m_platformDescription)
    149         m_platformDescription->SetMarked(checked);
    150 }
    151 
    152 void ContextMenuItem::setEnabled(bool enable)
    153 {
    154     if (m_platformDescription)
    155         m_platformDescription->SetEnabled(enable);
    156 }
    157 
    158 bool ContextMenuItem::enabled() const
    159 {
    160     if (!m_platformDescription)
    161         return true;
    162     return m_platformDescription->IsEnabled();
    163 }
    164 
    165