Skip to content

Commit

Permalink
Apply Guichan's changes from e5271f1ad79923ffa1810e2bc62435117d49ce9f…
Browse files Browse the repository at this point in the history
… (Jan 29th 2008)

Tabs width now adjust to their labels and some optimizations.
  • Loading branch information
Jarod42 committed Aug 10, 2024
1 parent 4e19580 commit 813e863
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 72 deletions.
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
* Continue rebasing from e5271f1ad79923ffa1810e2bc62435117d49ce9f
* Continue rebasing from 2bc3319195c94fd4ba3d87e0a9f095bca211646e
* Add a focus listener interface.
* Make focus apply synchronously.
* Graphics and input objects for DirectX.
Expand Down
4 changes: 2 additions & 2 deletions include/guisan/rectangle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ namespace gcn
/**
* Checks if a point is inside the rectangle
*
* @param x The point x coordinate of the point.
* @param y The point y coordinate of the point.
* @param x The x coordinate of the point.
* @param y The y coordinate of the point.
* @return True if the point is inside the rectangle.
* @since 0.1.0
*/
Expand Down
8 changes: 1 addition & 7 deletions include/guisan/widgets/tab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ namespace gcn
*/
const std::string& getCaption() const;


// Inherited from Widget

virtual void draw(Graphics *graphics);
Expand All @@ -140,7 +139,7 @@ namespace gcn
virtual void mouseEntered(MouseEvent& mouseEvent);

virtual void mouseExited(MouseEvent& mouseEvent);

protected:
/**
* Holds the label of the tab.
Expand All @@ -152,11 +151,6 @@ namespace gcn
*/
TabbedArea* mTabbedArea;

/**
* Holds the caption of the tab.
*/
std::string mCaption;

/**
* True if the tab has the mouse, false otherwise.
*/
Expand Down
14 changes: 7 additions & 7 deletions include/guisan/widgets/tabbedarea.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace gcn
{
class Container;
class Tab;

/**
* An implementation of a tabbed area where a user can display a widget by
* selecting a tab.
Expand All @@ -82,6 +82,8 @@ namespace gcn
public KeyListener,
public MouseListener
{
friend class Tab;

public:

/**
Expand All @@ -107,7 +109,7 @@ namespace gcn
* Adds a tab to the tabbed area.
*
* @param tab The tab widget for the tab.
* @param widget The widget to view when the tab is selected.
* @param widget The widget to view when the tab is selected.
* @see removeTab, removeTabWithIndex
*/
virtual void addTab(Tab* tab, Widget* widget);
Expand All @@ -119,7 +121,7 @@ namespace gcn
* @see addTab
*/
virtual void removeTabWithIndex(unsigned int index);

/**
* Removes a tab from the tabbed area.
*
Expand All @@ -136,7 +138,7 @@ namespace gcn
* @see setSelectedTab
*/
virtual bool isTabSelected(unsigned int index) const;

/**
* Checks if a tab is selected or not.
*
Expand Down Expand Up @@ -194,11 +196,9 @@ namespace gcn

void setDimension(const Rectangle& dimension);


// Inherited from ActionListener

void action(const ActionEvent& actionEvent);


// Inherited from DeathListener

Expand All @@ -222,7 +222,7 @@ namespace gcn
void adjustSize();

/**
* Adjusts the positions ot the tabs.
* Adjusts the positions of the tabs.
*/
void adjustTabPositions();

Expand Down
30 changes: 17 additions & 13 deletions src/widgets/tab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,20 @@ namespace gcn

addMouseListener(this);
}

Tab::~Tab()
{
delete mLabel;
}

void Tab::adjustSize()
{
setHeight(mLabel->getHeight() + 8);
setSize(mLabel->getWidth() + 8, mLabel->getHeight() + 8);

if (mTabbedArea != NULL)
{
mTabbedArea->adjustTabPositions();
}
}

void Tab::setTabbedArea(TabbedArea* tabbedArea)
Expand All @@ -100,24 +105,23 @@ namespace gcn

void Tab::setCaption(const std::string& caption)
{
mCaption = caption;
mLabel->setCaption(caption);
mLabel->adjustSize();
adjustSize();
}

const std::string& Tab::getCaption() const
{
return mCaption;
return mLabel->getCaption();
}

void Tab::draw(Graphics *graphics)
{
Color faceColor = getBaseColor();
Color highlightColor, shadowColor;
int alpha = getBaseColor().a;
highlightColor = faceColor + 0x303030;
const Color& faceColor = getBaseColor();
const int alpha = getBaseColor().a;
Color highlightColor = faceColor + 0x303030;
highlightColor.a = alpha;
shadowColor = faceColor - 0x303030;
Color shadowColor = faceColor - 0x303030;
shadowColor.a = alpha;

Color borderColor;
Expand Down Expand Up @@ -165,7 +169,7 @@ namespace gcn
}
graphics->popClipArea();
}

void Tab::mouseEntered(MouseEvent& mouseEvent)
{
mHasMouse = true;
Expand Down
67 changes: 25 additions & 42 deletions src/widgets/tabbedarea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace gcn
setFocusable(true);
addKeyListener(this);
addMouseListener(this);

mTabContainer = new Container();
mTabContainer->setOpaque(false);
mWidgetContainer = new Container();
Expand All @@ -92,7 +92,7 @@ namespace gcn
{
remove(mTabContainer);
remove(mWidgetContainer);

delete mTabContainer;
delete mWidgetContainer;

Expand All @@ -104,24 +104,22 @@ namespace gcn
}

void TabbedArea::addTab(const std::string& caption, Widget* widget)
{
{
Tab* tab = new Tab();
tab->setSize(70, 20);
tab->setCaption(caption);
mTabsToDelete.push_back(tab);

addTab(tab, widget);
}

void TabbedArea::addTab(Tab* tab, Widget* widget)
{
{
tab->setTabbedArea(this);
tab->addActionListener(this);
tab->addActionListener(this);

mTabContainer->add(tab);
mTabContainer->add(tab);
mTabs.push_back(std::pair<Tab*, Widget*>(tab, widget));


if (mSelectedTab == NULL)
{
setSelectedTab(tab);
Expand All @@ -144,11 +142,11 @@ namespace gcn
void TabbedArea::removeTab(Tab* tab)
{
int tabIndexToBeSelected = - 1;

if (tab == mSelectedTab)
{
int index = getSelectedTabIndex();

if (index == (int)mTabs.size() - 1
&& mTabs.size() >= 2)
{
Expand All @@ -167,15 +165,15 @@ namespace gcn

std::vector<std::pair<Tab*, Widget*> >::iterator iter;
for (iter = mTabs.begin(); iter != mTabs.end(); iter++)
{
{
if (iter->first == tab)
{
mTabContainer->remove(tab);
mTabs.erase(iter);
break;
}
}

std::vector<Tab*>::iterator iter2;
for (iter2 = mTabsToDelete.begin(); iter2 != mTabsToDelete.end(); iter2++)
{
Expand All @@ -196,7 +194,7 @@ namespace gcn
{
setSelectedTab(tabIndexToBeSelected);
}

adjustSize();
adjustTabPositions();
}
Expand Down Expand Up @@ -269,12 +267,11 @@ namespace gcn

void TabbedArea::draw(Graphics *graphics)
{
Color faceColor = getBaseColor();
Color highlightColor, shadowColor;
const Color& faceColor = getBaseColor();
int alpha = getBaseColor().a;
highlightColor = faceColor + 0x303030;
Color highlightColor = faceColor + 0x303030;
highlightColor.a = alpha;
shadowColor = faceColor - 0x303030;
Color shadowColor = faceColor - 0x303030;
shadowColor.a = alpha;

// Draw a border.
Expand Down Expand Up @@ -327,12 +324,10 @@ namespace gcn
}
}

mTabContainer->setWidth(getWidth() - 2);
mTabContainer->setHeight(maxTabHeight);
mTabContainer->setSize(getWidth() - 2, maxTabHeight);

mWidgetContainer->setPosition(1, maxTabHeight + 1);
mWidgetContainer->setWidth(getWidth() - 2);
mWidgetContainer->setHeight(getHeight() - maxTabHeight - 2);
mWidgetContainer->setSize(getWidth() - 2, getHeight() - maxTabHeight - 2);
}

void TabbedArea::adjustTabPositions()
Expand All @@ -348,33 +343,22 @@ namespace gcn
}

int x = 0;
for (i = 0; i < mTabs.size(); i++)
for (i = 0; i < mTabs.size(); i++)
{
Tab* tab = mTabs[i].first;

tab->setX(x);
tab->setPosition(x, maxTabHeight - tab->getHeight());

if (tab->getHeight() < maxTabHeight)
{
tab->setY(maxTabHeight
- tab->getHeight());
}
else
{
tab->setY(0);
}

x += tab->getWidth();
}
}

void TabbedArea::setWidth(int width)
{
Widget::setWidth(width);
adjustSize();
}


void TabbedArea::setHeight(int height)
{
Widget::setHeight(height);
Expand All @@ -386,7 +370,7 @@ namespace gcn
setWidth(width);
setHeight(height);
}

void TabbedArea::setDimension(const Rectangle& dimension)
{
setX(dimension.x);
Expand All @@ -401,12 +385,12 @@ namespace gcn
{
return;
}

if (keyEvent.getKey().getValue() == Key::LEFT)
{
int index = getSelectedTabIndex();
index--;

if (index < 0)
{
return;
Expand All @@ -415,7 +399,7 @@ namespace gcn
{
setSelectedTab(mTabs[index].first);
}

keyEvent.consume();
}
else if (keyEvent.getKey().getValue() == Key::RIGHT)
Expand All @@ -431,11 +415,10 @@ namespace gcn
{
setSelectedTab(mTabs[index].first);
}

keyEvent.consume();
}
}


void TabbedArea::mousePressed(MouseEvent& mouseEvent)
{
Expand All @@ -444,7 +427,7 @@ namespace gcn
{
return;
}

if (mouseEvent.getButton() == MouseEvent::LEFT)
{
Widget* widget = mTabContainer->getWidgetAt(mouseEvent.getX(), mouseEvent.getY());
Expand Down

0 comments on commit 813e863

Please sign in to comment.