2015-09-25 21:37:19 +02:00
|
|
|
// File: fmenubar.cpp
|
|
|
|
// Provides: class FMenuBar
|
2015-08-09 23:47:08 +02:00
|
|
|
|
|
|
|
#include "fmenubar.h"
|
2015-09-28 04:31:29 +02:00
|
|
|
#include "fmessagebox.h"
|
2015-08-09 23:47:08 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// class FMenuBar
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
// constructor and destructor
|
|
|
|
//----------------------------------------------------------------------
|
2015-09-22 04:18:20 +02:00
|
|
|
FMenuBar::FMenuBar(FWidget* parent)
|
|
|
|
: FWindow(parent)
|
|
|
|
, mouse_down(false)
|
|
|
|
, x(-1)
|
2015-08-09 23:47:08 +02:00
|
|
|
{
|
2015-09-22 04:18:20 +02:00
|
|
|
init();
|
2015-08-09 23:47:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
FMenuBar::~FMenuBar()
|
|
|
|
{
|
|
|
|
if ( vmenubar != 0 )
|
|
|
|
{
|
|
|
|
if ( vmenubar->changes != 0 )
|
|
|
|
delete[] vmenubar->changes;
|
|
|
|
if ( vmenubar->text != 0 )
|
|
|
|
delete[] vmenubar->text;
|
|
|
|
delete vmenubar;
|
|
|
|
}
|
|
|
|
vmenubar = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// private methods of FMenuBar
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FMenuBar::init()
|
|
|
|
{
|
|
|
|
xmin = ymin = 1;
|
|
|
|
xpos = 1;
|
|
|
|
ypos = 1;
|
|
|
|
createArea (vmenubar);
|
2015-09-22 22:12:31 +02:00
|
|
|
vmenubar->visible = true;
|
2015-08-09 23:47:08 +02:00
|
|
|
// initialize geometry values
|
|
|
|
setGeometry (1, 1, getColumnNumber(), 1, false);
|
|
|
|
getRootWidget()->setTopPadding(1, true);
|
|
|
|
setMenuBar(this);
|
2015-09-15 23:07:24 +02:00
|
|
|
foregroundColor = wc.menu_active_fg;
|
|
|
|
backgroundColor = wc.menu_active_bg;
|
2015-08-09 23:47:08 +02:00
|
|
|
window_object = true;
|
|
|
|
ignore_padding = true;
|
|
|
|
unsetFocusable();
|
|
|
|
}
|
|
|
|
|
2015-09-28 04:31:29 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FMenuBar::menu_dimension()
|
|
|
|
{
|
|
|
|
int item_X = 1;
|
|
|
|
int item_Y = 1;
|
|
|
|
std::vector<FMenuItem*>::const_iterator begin, end, iter;
|
|
|
|
iter = itemlist.begin();
|
|
|
|
end = itemlist.end();
|
|
|
|
|
|
|
|
// find the max item width
|
|
|
|
while ( iter != end )
|
|
|
|
{
|
|
|
|
uInt len = (*iter)->getTextLength();
|
|
|
|
int item_width = int(len + 2);
|
|
|
|
|
|
|
|
// set item geometry
|
|
|
|
(*iter)->setGeometry (item_X, item_Y, item_width, 1, false);
|
|
|
|
|
|
|
|
// set menu position
|
|
|
|
if ( (*iter)->hasMenu() )
|
|
|
|
(*iter)->getMenu()->setPos (item_X, item_Y, false);
|
|
|
|
|
|
|
|
item_X += item_width;
|
|
|
|
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 20:05:39 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
bool FMenuBar::isMenu (FMenuItem* mi) const
|
|
|
|
{
|
|
|
|
return mi->hasMenu();
|
|
|
|
}
|
|
|
|
|
2015-08-09 23:47:08 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
int FMenuBar::getHotkeyPos (wchar_t*& src, wchar_t*& dest, uInt length)
|
|
|
|
{
|
|
|
|
// find hotkey position in string
|
|
|
|
// + generate a new string without the '&'-sign
|
|
|
|
int hotkeypos = -1;
|
|
|
|
wchar_t* txt = src;
|
|
|
|
|
|
|
|
for (uInt i=0; i < length; i++)
|
|
|
|
{
|
|
|
|
if ( (i < length) && (txt[i] == L'&') && (hotkeypos == -1) )
|
|
|
|
{
|
|
|
|
hotkeypos = int(i);
|
|
|
|
i++;
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
*dest++ = *src++;
|
|
|
|
}
|
|
|
|
return hotkeypos;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FMenuBar::draw()
|
|
|
|
{
|
|
|
|
xmin = ymin = 1;
|
|
|
|
height = 1;
|
|
|
|
xpos = 1;
|
2015-09-28 04:31:29 +02:00
|
|
|
|
|
|
|
menu_dimension();
|
|
|
|
|
2015-08-09 23:47:08 +02:00
|
|
|
drawItems();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FMenuBar::drawItems()
|
|
|
|
{
|
2015-09-20 05:44:50 +02:00
|
|
|
bool is_Active;
|
|
|
|
bool is_Selected;
|
|
|
|
bool is_NoUnderline;
|
2015-08-09 23:47:08 +02:00
|
|
|
std::vector<FMenuItem*>::const_iterator iter, end;
|
|
|
|
int screenWidth;
|
|
|
|
|
|
|
|
x = 1;
|
|
|
|
screenWidth = getColumnNumber();
|
|
|
|
width = screenWidth;
|
|
|
|
ypos = 1;
|
|
|
|
|
|
|
|
if ( itemlist.empty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
setUpdateVTerm(false);
|
|
|
|
gotoxy (1,1);
|
|
|
|
|
|
|
|
iter = itemlist.begin();
|
|
|
|
end = itemlist.end();
|
|
|
|
|
|
|
|
while ( iter != end )
|
|
|
|
{
|
|
|
|
wchar_t* src;
|
|
|
|
wchar_t* dest;
|
|
|
|
wchar_t* item_text;
|
|
|
|
FString txt;
|
|
|
|
uInt txt_length;
|
|
|
|
int hotkeypos, to_char;
|
2015-08-16 20:05:39 +02:00
|
|
|
|
2015-09-28 04:31:29 +02:00
|
|
|
|
2015-09-20 05:44:50 +02:00
|
|
|
is_Active = (*iter)->isActivated();
|
|
|
|
is_Selected = (*iter)->isSelected();
|
|
|
|
is_NoUnderline = (((*iter)->getFlags() & NO_UNDERLINE) != 0);
|
2015-08-09 23:47:08 +02:00
|
|
|
|
2015-09-20 05:44:50 +02:00
|
|
|
if ( is_Active )
|
2015-08-09 23:47:08 +02:00
|
|
|
{
|
2015-09-20 05:44:50 +02:00
|
|
|
if ( is_Selected )
|
2015-08-09 23:47:08 +02:00
|
|
|
{
|
|
|
|
foregroundColor = wc.menu_active_focus_fg;
|
|
|
|
backgroundColor = wc.menu_active_focus_bg;
|
|
|
|
if ( isMonochron() )
|
|
|
|
setReverse(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foregroundColor = wc.menu_active_fg;
|
|
|
|
backgroundColor = wc.menu_active_bg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foregroundColor = wc.menu_inactive_fg;
|
|
|
|
backgroundColor = wc.menu_inactive_bg;
|
|
|
|
}
|
|
|
|
setColor (foregroundColor, backgroundColor);
|
|
|
|
x++;
|
|
|
|
print (vmenubar, ' ');
|
|
|
|
|
|
|
|
txt = (*iter)->getText();
|
2015-09-20 05:44:50 +02:00
|
|
|
txt_length = uInt(txt.getLength());
|
2015-08-09 23:47:08 +02:00
|
|
|
item_text = new wchar_t[txt_length+1];
|
|
|
|
src = const_cast<wchar_t*>(txt.wc_str());
|
|
|
|
dest = const_cast<wchar_t*>(item_text);
|
|
|
|
|
|
|
|
if ( x-1 <= screenWidth )
|
|
|
|
to_char = int(txt_length);
|
|
|
|
else
|
2015-09-20 05:44:50 +02:00
|
|
|
to_char = int(txt_length) - (screenWidth-x-1);
|
2015-08-09 23:47:08 +02:00
|
|
|
|
|
|
|
hotkeypos = getHotkeyPos (src, dest, txt_length);
|
|
|
|
|
|
|
|
if ( hotkeypos != -1 )
|
|
|
|
{
|
|
|
|
txt_length--;
|
|
|
|
to_char--;
|
|
|
|
}
|
2015-09-20 05:44:50 +02:00
|
|
|
x += int(txt_length);
|
2015-08-09 23:47:08 +02:00
|
|
|
|
|
|
|
for (int z=0; z < to_char; z++)
|
|
|
|
{
|
|
|
|
if ( ! iswprint(wint_t(item_text[z])) )
|
|
|
|
item_text[z] = L' ';
|
2015-09-20 05:44:50 +02:00
|
|
|
if ( (z == hotkeypos) && is_Active && ! is_Selected )
|
2015-08-09 23:47:08 +02:00
|
|
|
{
|
|
|
|
setColor (wc.menu_hotkey_fg, wc.menu_hotkey_bg);
|
2015-09-20 05:44:50 +02:00
|
|
|
if ( ! is_NoUnderline )
|
2015-08-09 23:47:08 +02:00
|
|
|
setUnderline();
|
|
|
|
print (vmenubar, item_text[z]);
|
2015-09-20 05:44:50 +02:00
|
|
|
if ( ! is_NoUnderline )
|
2015-08-09 23:47:08 +02:00
|
|
|
unsetUnderline();
|
|
|
|
setColor (foregroundColor, backgroundColor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
print (vmenubar, item_text[z]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( x > screenWidth )
|
|
|
|
{
|
|
|
|
print ( vmenubar,
|
2015-09-20 05:44:50 +02:00
|
|
|
txt.left(uInt(int(txt_length)+screenWidth-x-1)) );
|
2015-08-09 23:47:08 +02:00
|
|
|
print ( vmenubar, ".." );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x++;
|
|
|
|
print (vmenubar, ' ');
|
|
|
|
}
|
|
|
|
|
2015-09-20 05:44:50 +02:00
|
|
|
if ( is_Active && is_Selected )
|
2015-08-09 23:47:08 +02:00
|
|
|
setReverse(false);
|
|
|
|
delete[] item_text;
|
|
|
|
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
for (; x <= screenWidth; x++)
|
|
|
|
print (vmenubar, ' ');
|
|
|
|
|
|
|
|
setUpdateVTerm(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FMenuBar::adjustSize()
|
|
|
|
{
|
|
|
|
xmin = ymin = 1;
|
|
|
|
height = 1;
|
|
|
|
xpos = 1;
|
|
|
|
width = getColumnNumber();
|
|
|
|
ypos = 1;
|
|
|
|
FWidget::adjustSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// public methods of FMenuBar
|
|
|
|
//----------------------------------------------------------------------
|
2015-09-20 05:44:50 +02:00
|
|
|
void FMenuBar::onMouseDown (FMouseEvent* ev)
|
2015-08-09 23:47:08 +02:00
|
|
|
{
|
2015-09-20 05:44:50 +02:00
|
|
|
if ( ev->getButton() != LeftButton )
|
2015-08-09 23:47:08 +02:00
|
|
|
{
|
|
|
|
mouse_down = false;
|
|
|
|
if ( ! itemlist.empty() )
|
|
|
|
{
|
|
|
|
std::vector<FMenuItem*>::const_iterator iter, end;
|
|
|
|
iter = itemlist.begin();
|
|
|
|
end = itemlist.end();
|
|
|
|
|
|
|
|
while ( iter != end )
|
|
|
|
{
|
|
|
|
(*iter)->unsetSelected();
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
2015-09-22 04:18:20 +02:00
|
|
|
redraw();
|
2015-08-09 23:47:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( mouse_down )
|
|
|
|
return;
|
|
|
|
mouse_down = true;
|
|
|
|
|
|
|
|
if ( ! itemlist.empty() )
|
|
|
|
{
|
|
|
|
std::vector<FMenuItem*>::const_iterator iter, end;
|
|
|
|
int X = 1;
|
|
|
|
|
|
|
|
iter = itemlist.begin();
|
|
|
|
end = itemlist.end();
|
2015-09-28 04:31:29 +02:00
|
|
|
//FMessageBox::info (this, "Info", FString().sprintf("local(%d,%d) global(%d,%d)", ev->getX(),ev->getY(),ev->getGlobalX(), ev->getGlobalY())); // + #include
|
2015-08-09 23:47:08 +02:00
|
|
|
while ( iter != end )
|
|
|
|
{
|
|
|
|
int x1, x2, mouse_x, mouse_y, txt_length;
|
|
|
|
|
|
|
|
x1 = X;
|
2015-09-28 04:31:29 +02:00
|
|
|
txt_length = int((*iter)->getTextLength());
|
|
|
|
|
|
|
|
x2 = x1 + txt_length + 1;
|
|
|
|
mouse_x = ev->getGlobalX();
|
|
|
|
mouse_y = ev->getGlobalY();
|
2015-08-09 23:47:08 +02:00
|
|
|
|
|
|
|
if ( mouse_x >= x1
|
|
|
|
&& mouse_x <= x2
|
|
|
|
&& mouse_y == 1
|
|
|
|
&& ! (*iter)->isSelected() )
|
|
|
|
{
|
|
|
|
(*iter)->setSelected();
|
2015-09-22 04:18:20 +02:00
|
|
|
redraw();
|
2015-08-09 23:47:08 +02:00
|
|
|
}
|
2015-08-16 20:05:39 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
(*iter)->unsetSelected();
|
2015-09-22 04:18:20 +02:00
|
|
|
redraw();
|
2015-08-16 20:05:39 +02:00
|
|
|
}
|
|
|
|
X = x2 + 1;
|
2015-08-09 23:47:08 +02:00
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2015-09-20 05:44:50 +02:00
|
|
|
void FMenuBar::onMouseUp (FMouseEvent* ev)
|
2015-08-09 23:47:08 +02:00
|
|
|
{
|
2015-09-20 05:44:50 +02:00
|
|
|
if ( ev->getButton() != LeftButton )
|
2015-08-09 23:47:08 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if ( mouse_down )
|
|
|
|
{
|
|
|
|
mouse_down = false;
|
|
|
|
if ( ! itemlist.empty() )
|
|
|
|
{
|
|
|
|
std::vector<FMenuItem*>::const_iterator iter, end;
|
|
|
|
int X = 1;
|
|
|
|
|
|
|
|
iter = itemlist.begin();
|
|
|
|
end = itemlist.end();
|
|
|
|
|
|
|
|
while ( iter != end )
|
|
|
|
{
|
2015-09-28 04:31:29 +02:00
|
|
|
int x1, x2, txt_length;
|
|
|
|
|
|
|
|
x1 = X;
|
|
|
|
txt_length = int((*iter)->getTextLength());
|
|
|
|
x2 = x1 + txt_length + 1;
|
2015-08-09 23:47:08 +02:00
|
|
|
|
|
|
|
if ( (*iter)->isSelected() )
|
|
|
|
{
|
2015-09-28 04:31:29 +02:00
|
|
|
int mouse_x = ev->getGlobalX();
|
|
|
|
int mouse_y = ev->getGlobalY();
|
2015-08-09 23:47:08 +02:00
|
|
|
if ( mouse_x < x1 || mouse_x > x2 || mouse_y != 1 )
|
|
|
|
(*iter)->unsetSelected();
|
2015-08-22 18:53:52 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
(*iter)->processClicked();
|
|
|
|
}
|
2015-09-22 04:18:20 +02:00
|
|
|
redraw();
|
2015-08-09 23:47:08 +02:00
|
|
|
}
|
2015-08-16 20:05:39 +02:00
|
|
|
X = x2 + 1;
|
2015-08-09 23:47:08 +02:00
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2015-09-20 05:44:50 +02:00
|
|
|
void FMenuBar::onMouseMove (FMouseEvent* ev)
|
2015-08-09 23:47:08 +02:00
|
|
|
{
|
2015-09-20 05:44:50 +02:00
|
|
|
if ( ev->getButton() != LeftButton )
|
2015-08-09 23:47:08 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if ( mouse_down && ! itemlist.empty() )
|
|
|
|
{
|
|
|
|
std::vector<FMenuItem*>::const_iterator iter, end;
|
|
|
|
bool focus_changed = false;
|
|
|
|
int X=1;
|
|
|
|
|
|
|
|
iter = itemlist.begin();
|
|
|
|
end = itemlist.end();
|
|
|
|
|
|
|
|
while ( iter != end )
|
|
|
|
{
|
2015-09-28 04:31:29 +02:00
|
|
|
int x1, x2, txt_length;
|
|
|
|
|
|
|
|
x1 = X;
|
|
|
|
txt_length = int((*iter)->getTextLength());
|
|
|
|
x2 = x1 + txt_length + 1;
|
|
|
|
|
|
|
|
int mouse_x = ev->getGlobalX();
|
|
|
|
int mouse_y = ev->getGlobalY();
|
2015-08-09 23:47:08 +02:00
|
|
|
if ( mouse_x >= x1
|
|
|
|
&& mouse_x <= x2
|
|
|
|
&& mouse_y == 1 )
|
|
|
|
{
|
|
|
|
if ( ! (*iter)->isSelected() )
|
|
|
|
{
|
|
|
|
(*iter)->setSelected();
|
|
|
|
focus_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( (*iter)->isSelected() )
|
|
|
|
{
|
|
|
|
(*iter)->unsetSelected();
|
|
|
|
focus_changed = true;
|
|
|
|
}
|
|
|
|
}
|
2015-08-16 20:05:39 +02:00
|
|
|
X = x2 + 1;
|
2015-08-09 23:47:08 +02:00
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
if ( focus_changed )
|
2015-09-22 04:18:20 +02:00
|
|
|
redraw();
|
2015-08-09 23:47:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FMenuBar::hide()
|
|
|
|
{
|
|
|
|
int fg, bg, screenWidth;
|
|
|
|
char* blank;
|
|
|
|
|
2015-09-22 22:12:31 +02:00
|
|
|
FWindow::hide();
|
2015-08-09 23:47:08 +02:00
|
|
|
|
|
|
|
fg = wc.term_fg;
|
|
|
|
bg = wc.term_bg;
|
|
|
|
setColor (fg, bg);
|
|
|
|
|
|
|
|
screenWidth = getColumnNumber();
|
|
|
|
blank = new char[screenWidth+1];
|
|
|
|
memset(blank, ' ', uLong(screenWidth));
|
|
|
|
blank[screenWidth] = '\0';
|
|
|
|
|
|
|
|
gotoxy (1, 1);
|
|
|
|
print (vmenubar, blank);
|
|
|
|
delete[] blank;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FMenuBar::setGeometry (int xx, int yy, int ww, int hh, bool adjust)
|
|
|
|
{
|
|
|
|
int old_width = width;
|
|
|
|
int old_height = height;
|
|
|
|
FWidget::setGeometry (xx, yy, ww, hh, adjust);
|
|
|
|
if ( vmenubar && (width != old_width || height != old_height) )
|
|
|
|
resizeArea (vmenubar);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2015-08-16 20:05:39 +02:00
|
|
|
void FMenuBar::cb_item_activated (FWidget* widget, void*)
|
2015-08-09 23:47:08 +02:00
|
|
|
{
|
2015-08-16 20:05:39 +02:00
|
|
|
FMenuItem* menuitem = static_cast<FMenuItem*>(widget);
|
2015-08-09 23:47:08 +02:00
|
|
|
|
2015-08-16 20:05:39 +02:00
|
|
|
if ( menuitem->hasMenu() )
|
|
|
|
{
|
|
|
|
beep();
|
2015-08-09 23:47:08 +02:00
|
|
|
}
|
|
|
|
}
|