Fixed setPos in FWidget

This commit is contained in:
Markus Gans 2015-09-28 04:31:29 +02:00
parent 69e30eae49
commit 15379c61fe
9 changed files with 190 additions and 63 deletions

View File

@ -13,7 +13,6 @@ FMenu::FMenu(FWidget* parent)
: FWindow(parent)
, item(0)
, super_menu(0)
, next_item_pos(1,1)
, maxItemWidth(0)
, current(0)
, mouse_down(false)
@ -26,7 +25,6 @@ FMenu::FMenu (FString& txt, FWidget* parent)
: FWindow(parent)
, item(0)
, super_menu(0)
, next_item_pos(1,1)
, maxItemWidth(0)
, current(0)
, mouse_down(false)
@ -40,7 +38,6 @@ FMenu::FMenu (const std::string& txt, FWidget* parent)
: FWindow(parent)
, item(0)
, super_menu(0)
, next_item_pos(1,1)
, maxItemWidth(0)
, current(0)
, mouse_down(false)
@ -54,7 +51,6 @@ FMenu::FMenu (const char* txt, FWidget* parent)
: FWindow(parent)
, item(0)
, super_menu(0)
, next_item_pos(1,1)
, maxItemWidth(0)
, current(0)
, mouse_down(false)
@ -122,6 +118,7 @@ void FMenu::init()
//----------------------------------------------------------------------
void FMenu::menu_dimension()
{
int item_X, item_Y;
std::vector<FMenuItem*>::const_iterator iter, end;
iter = itemlist.begin();
end = itemlist.end();
@ -130,18 +127,29 @@ void FMenu::menu_dimension()
// find the max item width
while ( iter != end )
{
FString item_text = (*iter)->getText();
uInt len = item_text.getLength();
uInt item_width = (*iter)->getTextLength() + 2;
if ( item_text.includes(L'&') ) // item has a hotkey '&'
len--;
if ( item_width > maxItemWidth )
maxItemWidth = item_width;
if ( len > maxItemWidth )
maxItemWidth = len;
++iter;
}
setGeometry (xpos, ypos, int(maxItemWidth + 4), int(count() + 2));
// set widget geometry
setGeometry (xpos, ypos, int(maxItemWidth + 2), int(count() + 2));
// set geometry of all items
iter = itemlist.begin();
item_X = 1;
item_Y = 1;
while ( iter != end )
{
(*iter)->setGeometry (item_X, item_Y, maxItemWidth, 1);
item_Y++;
++iter;
}
}
//----------------------------------------------------------------------
@ -350,7 +358,7 @@ void FMenu::drawItems()
if ( is_Selected )
{
for (uInt i=uInt(to_char); i <= maxItemWidth; i++)
for (uInt i=uInt(to_char); i < maxItemWidth-1; i++)
print (' ');
}

View File

@ -47,7 +47,6 @@ class FMenu : public FWindow, public FMenuList
private:
FMenuItem* item;
FMenuList* super_menu;
FPoint next_item_pos;
uInt maxItemWidth;
int current;
bool mouse_down;

View File

@ -2,7 +2,7 @@
// Provides: class FMenuBar
#include "fmenubar.h"
#include "fmessagebox.h"
//----------------------------------------------------------------------
// class FMenuBar
//----------------------------------------------------------------------
@ -12,7 +12,6 @@
FMenuBar::FMenuBar(FWidget* parent)
: FWindow(parent)
, mouse_down(false)
, next_item_pos(1,1)
, x(-1)
{
init();
@ -53,6 +52,34 @@ void FMenuBar::init()
unsetFocusable();
}
//----------------------------------------------------------------------
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;
}
}
//----------------------------------------------------------------------
bool FMenuBar::isMenu (FMenuItem* mi) const
{
@ -86,6 +113,9 @@ void FMenuBar::draw()
xmin = ymin = 1;
height = 1;
xpos = 1;
menu_dimension();
drawItems();
}
@ -121,6 +151,7 @@ void FMenuBar::drawItems()
uInt txt_length;
int hotkeypos, to_char;
is_Active = (*iter)->isActivated();
is_Selected = (*iter)->isSelected();
is_NoUnderline = (((*iter)->getFlags() & NO_UNDERLINE) != 0);
@ -255,18 +286,17 @@ void FMenuBar::onMouseDown (FMouseEvent* ev)
iter = itemlist.begin();
end = itemlist.end();
//FMessageBox::info (this, "Info", FString().sprintf("local(%d,%d) global(%d,%d)", ev->getX(),ev->getY(),ev->getGlobalX(), ev->getGlobalY())); // + #include
while ( iter != end )
{
int x1, x2, mouse_x, mouse_y, txt_length;
x1 = X;
txt_length = int((*iter)->getText().getLength());
if ( (*iter)->hasHotkey() )
txt_length--;
x2 = x1 + txt_length;
mouse_x = ev->getX();
mouse_y = ev->getY();
txt_length = int((*iter)->getTextLength());
x2 = x1 + txt_length + 1;
mouse_x = ev->getGlobalX();
mouse_y = ev->getGlobalY();
if ( mouse_x >= x1
&& mouse_x <= x2
@ -306,16 +336,16 @@ void FMenuBar::onMouseUp (FMouseEvent* ev)
while ( iter != end )
{
int x1 = X;
int txt_length = int((*iter)->getText().getLength());
if ( (*iter)->hasHotkey() )
txt_length--;
int x2 = x1 + txt_length;
int x1, x2, txt_length;
x1 = X;
txt_length = int((*iter)->getTextLength());
x2 = x1 + txt_length + 1;
if ( (*iter)->isSelected() )
{
int mouse_x = ev->getX();
int mouse_y = ev->getY();
int mouse_x = ev->getGlobalX();
int mouse_y = ev->getGlobalY();
if ( mouse_x < x1 || mouse_x > x2 || mouse_y != 1 )
(*iter)->unsetSelected();
else
@ -348,14 +378,14 @@ void FMenuBar::onMouseMove (FMouseEvent* ev)
while ( iter != end )
{
int x1 = X;
int txt_length = int((*iter)->getText().getLength());
if ( (*iter)->hasHotkey() )
txt_length--;
int x2 = x1 + txt_length;
int x1, x2, txt_length;
int mouse_x = ev->getX();
int mouse_y = ev->getY();
x1 = X;
txt_length = int((*iter)->getTextLength());
x2 = x1 + txt_length + 1;
int mouse_x = ev->getGlobalX();
int mouse_y = ev->getGlobalY();
if ( mouse_x >= x1
&& mouse_x <= x2
&& mouse_y == 1 )

View File

@ -30,8 +30,9 @@
#ifndef _FMENUBAR_H
#define _FMENUBAR_H
#include "fwindow.h"
#include "fmenulist.h"
#include "fmenu.h"
#include "fwindow.h"
//----------------------------------------------------------------------
@ -45,13 +46,13 @@ class FMenuBar : public FWindow, public FMenuList
{
private:
bool mouse_down;
FPoint next_item_pos;
int x;
private:
FMenuBar (const FMenuBar&);
FMenuBar& operator = (const FMenuBar&);
void init();
void menu_dimension();
bool isMenu (FMenuItem*) const;
int getHotkeyPos (wchar_t*&, wchar_t*&, uInt);
void draw();
@ -71,6 +72,9 @@ class FMenuBar : public FWindow, public FMenuList
using FWidget::setGeometry;
void setGeometry (int, int, int, int, bool = true);
void cb_item_activated (FWidget*, void*);
private:
friend class FMenuItem;
};
#pragma pack(pop)

View File

@ -92,7 +92,7 @@ void FMenuItem::init (FWidget* parent)
hotkey = getHotkey();
if ( hotkey )
text_length--;
setGeometry (1,1,text_length+2,1);
setGeometry (1,1,text_length+2,1, false);
if ( parent )
{
@ -100,13 +100,14 @@ void FMenuItem::init (FWidget* parent)
{
setSuperMenu( dynamic_cast<FMenuList*>(parent) );
superMenu()->insert(this);
dynamic_cast<FMenuBar*>(parent)->menu_dimension();
//addAccelerator (item->getKey(), item);
this->addCallback
(
"activate",
_METHOD_CALLBACK (superMenu(), &FMenu::cb_menuitem_activated)
_METHOD_CALLBACK (superMenu(), &FMenuBar::cb_item_activated)
);
}
else if ( isMenu(parent) ) // Parent is menu
@ -114,6 +115,7 @@ void FMenuItem::init (FWidget* parent)
setSuperMenu( dynamic_cast<FMenuList*>(parent) );
superMenu()->insert(this);
//addAccelerator (item->getKey(), item);
this->addCallback
@ -141,6 +143,12 @@ uChar FMenuItem::getHotkey()
return 0;
}
//----------------------------------------------------------------------
bool FMenuItem::isMenuBar (FMenuList* ml) const
{
return isMenuBar (dynamic_cast<FWidget*>(ml));
}
//----------------------------------------------------------------------
bool FMenuItem::isMenuBar (FWidget* w) const
{
@ -148,6 +156,12 @@ bool FMenuItem::isMenuBar (FWidget* w) const
, const_cast<char*>("FMenuBar") ) == 0 );
}
//----------------------------------------------------------------------
bool FMenuItem::isMenu (FMenuList* ml) const
{
return isMenu (dynamic_cast<FWidget*>(ml));
}
//----------------------------------------------------------------------
bool FMenuItem::isMenu (FWidget* w) const
{
@ -193,6 +207,63 @@ void FMenuItem::onAccel (FAccelEvent* ev)
}
}
//----------------------------------------------------------------------
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);
if ( isMenu(super_menu) )
dynamic_cast<FMenu*>(super_menu)->onMouseDown(ev);
if ( isMenuBar(super_menu) )
dynamic_cast<FMenuBar*>(super_menu)->onMouseDown(ev);
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);
if ( isMenu(super_menu) )
dynamic_cast<FMenu*>(super_menu)->onMouseUp(ev);
if ( isMenuBar(super_menu) )
dynamic_cast<FMenuBar*>(super_menu)->onMouseUp(ev);
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);
if ( isMenu(super_menu) )
dynamic_cast<FMenu*>(super_menu)->onMouseMove(ev);
if ( isMenuBar(super_menu) )
dynamic_cast<FMenuBar*>(super_menu)->onMouseMove(ev);
delete ev;
}
//----------------------------------------------------------------------
void FMenuItem::setSelected()
{

View File

@ -59,7 +59,9 @@ class FMenuItem : public FWidget
FMenuItem& operator = (const FMenuItem&);
void init (FWidget*);
uChar getHotkey();
bool isMenuBar (FMenuList*) const;
bool isMenuBar (FWidget*) const;
bool isMenu (FMenuList*) const;
bool isMenu (FWidget*) const;
FMenuList* superMenu() const;
void setSuperMenu (FMenuList*);
@ -74,6 +76,9 @@ class FMenuItem : public FWidget
virtual ~FMenuItem();
void onAccel (FAccelEvent*);
void onMouseDown (FMouseEvent*);
void onMouseUp (FMouseEvent*);
void onMouseMove (FMouseEvent*);
FString getText() const;
void setActive();
void unsetActive();
@ -88,8 +93,10 @@ class FMenuItem : public FWidget
void unsetChecked();
bool isChecked() const;
bool hasHotkey() const;
FMenu* getMenu() const;
void setMenu(FMenu*);
bool hasMenu() const;
uInt getTextLength() const;
void setText (FString&);
void setText (const std::string&);
void setText (const char*);
@ -154,6 +161,10 @@ inline bool FMenuItem::isChecked() const
inline bool FMenuItem::hasHotkey() const
{ return bool(hotkey != 0); }
//----------------------------------------------------------------------
inline FMenu* FMenuItem::getMenu() const
{ return menu; }
//----------------------------------------------------------------------
inline void FMenuItem::setMenu(FMenu* m)
{ menu = m; }
@ -162,5 +173,8 @@ inline void FMenuItem::setMenu(FMenu* m)
inline bool FMenuItem::hasMenu() const
{ return bool(menu != 0); }
//----------------------------------------------------------------------
inline uInt FMenuItem::getTextLength() const
{ return text_length; }
#endif // _FMENUITEM_H

View File

@ -16,12 +16,6 @@
#include "fwidget.h"
/*
- FMenuBar muß die x + y Positionen des nächsten
einzufügenden FMenuItem kennen
- FMenu muß die x + y Positionen des nächsten
einzufügenden FMenuItem kennen
FWindow FMenuBar

View File

@ -425,16 +425,19 @@ void FStatusBar::onMouseUp (FMouseEvent* ev)
while ( iter != end )
{
int x1 = X;
int kname_len = int(getKeyName((*iter)->getKey()).getLength());
int txt_length = int((*iter)->getText().getLength());
int x2 = x1 + kname_len + txt_length + 1;
int x1, x2, kname_len, txt_length;
x1 = X;
kname_len = int(getKeyName((*iter)->getKey()).getLength());
txt_length = int((*iter)->getText().getLength());
x2 = x1 + kname_len + txt_length + 1;
if ( (*iter)->hasMouseFocus() )
{
int mouse_x, mouse_y;
(*iter)->unsetMouseFocus();
int mouse_x = ev->getX();
int mouse_y = ev->getY();
mouse_x = ev->getX();
mouse_y = ev->getY();
if ( mouse_x >= x1 && mouse_x <= x2 && mouse_y == 1 )
(*iter)->setActive();
redraw();
@ -465,13 +468,15 @@ void FStatusBar::onMouseMove (FMouseEvent* ev)
while ( iter != end )
{
int x1 = X;
int kname_len = int(getKeyName((*iter)->getKey()).getLength());
int txt_length = int((*iter)->getText().getLength());
int x2 = x1 + kname_len + txt_length + 1;
int x1, x2, mouse_x, mouse_y, kname_len, txt_length;
int mouse_x = ev->getX();
int mouse_y = ev->getY();
x1 = X;
kname_len = int(getKeyName((*iter)->getKey()).getLength());
txt_length = int((*iter)->getText().getLength());
x2 = x1 + kname_len + txt_length + 1;
mouse_x = ev->getX();
mouse_y = ev->getY();
if ( mouse_x >= x1
&& mouse_x <= x2
&& mouse_y == 1 )

View File

@ -1492,10 +1492,11 @@ void FWidget::setPos (const FPoint& p, bool adjust)
//----------------------------------------------------------------------
void FWidget::setPos (int x, int y, bool adjust)
{
if ( xpos == x && widgetSize.getX() == x )
return;
if ( ypos == y && widgetSize.getY() == y )
if ( xpos == x && widgetSize.getX() == x
&& ypos == y && widgetSize.getY() == y )
{
return;
}
if ( ! isWindow() )
{
@ -1675,8 +1676,9 @@ void FWidget::setGeometry (int x, int y, int w, int h, bool adjust)
adjustWidgetSize.setRect(xpos, ypos, width, height);
adjustWidgetSizeShadow = adjustWidgetSize + shadow;
adjustWidgetSizeGlobal.setRect ( xpos + xmin - 1
, ypos + ymin - 1,
width, height );
, ypos + ymin - 1
, width
, height );
adjustWidgetSizeGlobalShadow = adjustWidgetSizeGlobal + shadow;
if ( adjust )