finalcut/src/fmenuitem.cpp

238 lines
5.3 KiB
C++
Raw Normal View History

// File: fmenuitem.cpp
// Provides: class FMenuItem
2015-09-15 23:07:24 +02:00
#include "fmenu.h"
2015-08-16 20:05:39 +02:00
#include "fmenubar.h"
#include "fmenuitem.h"
#include "fmenulist.h"
//----------------------------------------------------------------------
// class FMenuItem
//----------------------------------------------------------------------
// constructor and destructor
//----------------------------------------------------------------------
2015-09-22 04:18:20 +02:00
FMenuItem::FMenuItem (FWidget* parent)
: FWidget(parent)
, text()
, active(true)
, selected(false)
, separator(false)
, checked(false)
, text_length(0)
2015-09-22 04:18:20 +02:00
, hotkey(0)
//, accel_key(0)
, menu(0)
, super_menu(0)
{
init (parent);
}
//----------------------------------------------------------------------
2015-09-22 04:18:20 +02:00
FMenuItem::FMenuItem (FString& txt, FWidget* parent)
: FWidget(parent)
, text(txt)
, active(true)
, selected(false)
, separator(false)
, checked(false)
, text_length(0)
2015-09-22 04:18:20 +02:00
, hotkey(0)
//, accel_key(0)
, menu(0)
, super_menu(0)
{
init (parent);
}
//----------------------------------------------------------------------
2015-09-22 04:18:20 +02:00
FMenuItem::FMenuItem (const std::string& txt, FWidget* parent)
: FWidget(parent)
, text(txt)
, active(true)
, selected(false)
, separator(false)
, checked(false)
, text_length(0)
2015-09-22 04:18:20 +02:00
, hotkey(0)
//, accel_key(0)
, menu(0)
, super_menu(0)
{
init (parent);
}
//----------------------------------------------------------------------
2015-09-22 04:18:20 +02:00
FMenuItem::FMenuItem (const char* txt, FWidget* parent)
: FWidget(parent)
, text(txt)
, active(true)
, selected(false)
, separator(false)
, checked(false)
, text_length(0)
2015-09-22 04:18:20 +02:00
, hotkey(0)
//, accel_key(0)
, menu(0)
, super_menu(0)
{
init (parent);
}
//----------------------------------------------------------------------
FMenuItem::~FMenuItem() // destructor
2015-09-22 04:18:20 +02:00
{ }
// private methods of FMenuItem
//----------------------------------------------------------------------
void FMenuItem::init (FWidget* parent)
{
text_length = text.getLength();
2015-09-22 04:18:20 +02:00
hotkey = getHotkey();
if ( hotkey )
text_length--;
setGeometry (1,1,text_length+2,1);
2015-09-15 23:07:24 +02:00
if ( parent )
{
2015-09-15 23:07:24 +02:00
if ( isMenuBar(parent) ) // Parent is menubar
{
setSuperMenu( dynamic_cast<FMenuList*>(parent) );
superMenu()->insert(this);
2015-09-15 23:07:24 +02:00
//addAccelerator (item->getKey(), item);
2015-09-15 23:07:24 +02:00
this->addCallback
(
"activate",
_METHOD_CALLBACK (superMenu(), &FMenu::cb_menuitem_activated)
2015-09-15 23:07:24 +02:00
);
}
else if ( isMenu(parent) ) // Parent is menu
{
setSuperMenu( dynamic_cast<FMenuList*>(parent) );
superMenu()->insert(this);
2015-09-15 23:07:24 +02:00
//addAccelerator (item->getKey(), item);
2015-09-15 23:07:24 +02:00
this->addCallback
(
"activate",
_METHOD_CALLBACK (superMenu(), &FMenu::cb_menuitem_activated)
2015-09-15 23:07:24 +02:00
);
}
}
}
2015-08-16 20:05:39 +02:00
//----------------------------------------------------------------------
uChar FMenuItem::getHotkey()
{
uInt length;
if ( text.isEmpty() )
return 0;
length = text.getLength();
for (uInt i=0; i < length; i++)
if ( (i+1 < length) && (text[i] == '&') )
return uChar(text[++i]);
return 0;
}
//----------------------------------------------------------------------
bool FMenuItem::isMenuBar (FWidget* w) const
{
2015-09-22 04:18:20 +02:00
return bool ( strcmp ( w->getClassName()
, const_cast<char*>("FMenuBar") ) == 0 );
}
2015-09-15 23:07:24 +02:00
//----------------------------------------------------------------------
bool FMenuItem::isMenu (FWidget* w) const
{
2015-09-22 04:18:20 +02:00
return bool ( strcmp ( w->getClassName()
, const_cast<char*>("FMenu") ) == 0 );
2015-09-15 23:07:24 +02:00
}
2015-08-16 20:05:39 +02:00
//----------------------------------------------------------------------
FMenuList* FMenuItem::superMenu() const
{
return super_menu;
}
//----------------------------------------------------------------------
void FMenuItem::setSuperMenu (FMenuList* smenu)
{
super_menu = smenu;
}
//----------------------------------------------------------------------
void FMenuItem::processActivate()
{
2015-08-16 20:05:39 +02:00
emitCallback("activate");
}
2015-08-22 18:53:52 +02:00
//----------------------------------------------------------------------
void FMenuItem::processClicked()
{
emitCallback("clicked");
}
// public methods of FMenuItem
//----------------------------------------------------------------------
2015-09-20 05:44:50 +02:00
void FMenuItem::onAccel (FAccelEvent* ev)
{
if ( isSelected() )
{
unsetSelected();
FWidget* w = reinterpret_cast<FWidget*>(superMenu());
if ( isMenuBar(w) )
w->redraw();
2015-09-20 05:44:50 +02:00
ev->accept();
}
}
//----------------------------------------------------------------------
2015-08-16 20:05:39 +02:00
void FMenuItem::setSelected()
{
2015-08-22 18:53:52 +02:00
if ( isActivated() )
{
2015-09-22 04:18:20 +02:00
selected = true;
2015-08-22 18:53:52 +02:00
processActivate();
}
}
//----------------------------------------------------------------------
2015-08-16 20:05:39 +02:00
inline void FMenuItem::setText (FString& txt)
{
2015-09-22 04:18:20 +02:00
text = txt;
text_length = text.getLength();
2015-09-22 04:18:20 +02:00
hotkey = getHotkey();
if ( hotkey )
text_length--;
setWidth(text_length);
}
//----------------------------------------------------------------------
2015-08-16 20:05:39 +02:00
inline void FMenuItem::setText (const std::string& txt)
{
2015-09-22 04:18:20 +02:00
text = txt;
text_length = text.getLength();
2015-09-22 04:18:20 +02:00
hotkey = getHotkey();
if ( hotkey )
text_length--;
setWidth(text_length);
2015-08-16 20:05:39 +02:00
}
//----------------------------------------------------------------------
inline void FMenuItem::setText (const char* txt)
{
2015-09-22 04:18:20 +02:00
text = txt;
text_length = text.getLength();
2015-09-22 04:18:20 +02:00
hotkey = getHotkey();
if ( hotkey )
text_length--;
setWidth(text_length);
}