finalcut/src/fmenuitem.cpp

383 lines
8.4 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--;
2015-10-04 19:01:34 +02:00
setGeometry (1,1,int(text_length+2),1, false);
2015-09-15 23:07:24 +02:00
if ( parent )
{
2015-10-01 05:08:05 +02:00
FMenuList* sm_list = dynamic_cast<FMenuList*>(parent);
if ( sm_list )
{
setSuperMenu(sm_list);
sm_list->insert(this);
}
2015-10-01 04:44:26 +02:00
2015-09-15 23:07:24 +02:00
if ( isMenuBar(parent) ) // Parent is menubar
{
2015-09-30 22:39:02 +02:00
FMenuBar* menubar_ptr = dynamic_cast<FMenuBar*>(parent);
if ( menubar_ptr )
menubar_ptr->menu_dimension();
2015-09-15 23:07:24 +02:00
//addAccelerator (item->getKey(), item);
2015-09-15 23:07:24 +02:00
this->addCallback
(
"activate",
2015-09-28 04:31:29 +02:00
_METHOD_CALLBACK (superMenu(), &FMenuBar::cb_item_activated)
2015-09-15 23:07:24 +02:00
);
2015-09-30 22:39:02 +02:00
this->addCallback
(
"deactivate",
_METHOD_CALLBACK (superMenu(), &FMenuBar::cb_item_deactivated)
);
2015-09-15 23:07:24 +02:00
}
else if ( isMenu(parent) ) // Parent is menu
{
2015-09-30 22:39:02 +02:00
FMenu* super_menu_ptr = dynamic_cast<FMenu*>(parent);
if ( super_menu_ptr )
super_menu_ptr->menu_dimension();
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-09-30 22:39:02 +02:00
this->addCallback
(
"deactivate",
_METHOD_CALLBACK (superMenu(), &FMenu::cb_menuitem_deactivated)
);
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++)
2015-09-30 22:39:02 +02:00
{
try
{
if ( (i+1 < length) && (text[i] == '&') )
return uChar(text[++i]);
}
catch (const std::out_of_range&)
{
return 0;;
}
}
2015-08-16 20:05:39 +02:00
return 0;
}
2015-09-28 04:31:29 +02:00
//----------------------------------------------------------------------
bool FMenuItem::isMenuBar (FMenuList* ml) const
{
2015-09-30 22:39:02 +02:00
FWidget* menubar_ptr = dynamic_cast<FWidget*>(ml);
if ( menubar_ptr )
return isMenuBar(menubar_ptr);
else
return false;
2015-09-28 04:31:29 +02:00
}
//----------------------------------------------------------------------
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-28 04:31:29 +02:00
//----------------------------------------------------------------------
bool FMenuItem::isMenu (FMenuList* ml) const
{
2015-09-30 22:39:02 +02:00
FWidget* super_menu_ptr = dynamic_cast<FWidget*>(ml);
if ( super_menu_ptr )
return isMenu(super_menu_ptr);
else
return false;
2015-09-28 04:31:29 +02:00
}
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-09-30 22:39:02 +02:00
//----------------------------------------------------------------------
void FMenuItem::processDeactivate()
{
emitCallback("deactivate");
}
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());
2015-09-30 22:39:02 +02:00
if ( w && isMenuBar(w) )
w->redraw();
2015-09-20 05:44:50 +02:00
ev->accept();
}
}
2015-09-28 04:31:29 +02:00
//----------------------------------------------------------------------
void FMenuItem::onMouseDown (FMouseEvent* ev)
{
const FPoint& p1 = ev->getPos();
const FPoint& g = ev->getGlobalPos();
FPoint p2(p1);
int b = ev->getButton();
p2 = p1 + getPos() - FPoint(1,1);
ev = new FMouseEvent (MouseMove_Event, p2, g, b);
2015-09-30 22:39:02 +02:00
if ( super_menu )
{
if ( isMenu(super_menu) )
2015-10-01 04:44:26 +02:00
{
FMenu* sm = dynamic_cast<FMenu*>(super_menu);
if ( sm )
sm->onMouseDown(ev);
}
2015-10-01 03:48:58 +02:00
2015-09-30 22:39:02 +02:00
if ( isMenuBar(super_menu) )
2015-10-01 04:44:26 +02:00
{
FMenuBar* mb = dynamic_cast<FMenuBar*>(super_menu);
if ( mb )
mb->onMouseDown(ev);
}
2015-09-30 22:39:02 +02:00
}
2015-09-28 04:31:29 +02:00
delete ev;
}
//----------------------------------------------------------------------
void FMenuItem::onMouseUp (FMouseEvent* ev)
{
const FPoint& p1 = ev->getPos();
const FPoint& g = ev->getGlobalPos();
FPoint p2(p1);
int b = ev->getButton();
p2 = p1 + getPos() - FPoint(1,1);
ev = new FMouseEvent (MouseMove_Event, p2, g, b);
2015-09-30 22:39:02 +02:00
if ( super_menu )
{
if ( isMenu(super_menu) )
2015-10-01 04:44:26 +02:00
{
FMenu* sm = dynamic_cast<FMenu*>(super_menu);
if ( sm )
sm->onMouseUp(ev);
}
2015-10-01 03:48:58 +02:00
2015-09-30 22:39:02 +02:00
if ( isMenuBar(super_menu) )
2015-10-01 04:44:26 +02:00
{
FMenuBar* mb = dynamic_cast<FMenuBar*>(super_menu);
if ( mb )
mb->onMouseUp(ev);
}
2015-09-30 22:39:02 +02:00
}
2015-09-28 04:31:29 +02:00
delete ev;
}
//----------------------------------------------------------------------
void FMenuItem::onMouseMove (FMouseEvent* ev)
{
const FPoint& p1 = ev->getPos();
const FPoint& g = ev->getGlobalPos();
FPoint p2(p1);
int b = ev->getButton();
p2 = p1 + getPos() - FPoint(1,1);
ev = new FMouseEvent (MouseMove_Event, p2, g, b);
2015-09-30 22:39:02 +02:00
if ( super_menu )
{
if ( isMenu(super_menu) )
2015-10-01 04:44:26 +02:00
{
FMenu* sm = dynamic_cast<FMenu*>(super_menu);
if ( sm )
sm->onMouseMove(ev);
}
2015-10-01 03:48:58 +02:00
2015-09-30 22:39:02 +02:00
if ( isMenuBar(super_menu) )
2015-10-01 04:44:26 +02:00
{
FMenuBar* mb = dynamic_cast<FMenuBar*>(super_menu);
if ( mb )
mb->onMouseMove(ev);
}
2015-09-30 22:39:02 +02:00
}
2015-09-28 04:31:29 +02:00
delete ev;
}
//----------------------------------------------------------------------
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-09-30 22:39:02 +02:00
void FMenuItem::unsetSelected()
{
selected = false;
processDeactivate();
}
//----------------------------------------------------------------------
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--;
2015-10-04 19:01:34 +02:00
setWidth(int(text_length));
}
//----------------------------------------------------------------------
2015-10-04 19:01:34 +02:00
void FMenuItem::setText (const std::string& txt)
{
2015-10-04 19:01:34 +02:00
FString s = FString(txt);
setText (s);
2015-08-16 20:05:39 +02:00
}
//----------------------------------------------------------------------
2015-10-04 19:01:34 +02:00
void FMenuItem::setText (const char* txt)
2015-08-16 20:05:39 +02:00
{
2015-10-04 19:01:34 +02:00
FString s = FString(txt);
setText (s);
}