Commit 35ce0d4c by Jessica Hawkwell

Adding FlowLayout for #1

1 parent f7aa26b0
......@@ -17,6 +17,7 @@ nitrogen/nwt/Container.cpp
nitrogen/nwt/Dimension.cpp
nitrogen/nwt/EventListener.cpp
nitrogen/nwt/EventObject.cpp
nitrogen/nwt/FlowLayout.cpp
nitrogen/nwt/Font.cpp
nitrogen/nwt/FontMetrics.cpp
nitrogen/nwt/Graphics.cpp
......
/*
* Copyright (c) 2018, jlhawkwell
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* File: FlowLayout.cpp
* Author: jlhawkwell
*
* Created on March 24, 2018, 8:53 PM
*/
#include "FlowLayout.h"
#include "Insets.h"
namespace nitrogen {
namespace nwt {
FlowLayout::FlowLayout() {
align = LEFT;
flc = 0;
flr = 0;
flh = 0;
flv = 0;
}
FlowLayout::FlowLayout(unsigned int alignment) {
align = alignment;
flc = 0;
flr = 0;
flh = 0;
flv = 0;
}
FlowLayout::FlowLayout(unsigned int alignment, unsigned int hgap, unsigned int vgap) {
align = alignment;
flc = 0;
flr = 0;
flh = hgap;
flv = vgap;
}
void FlowLayout::addLayoutComponent(std::string* s, Component* c) {
}
void FlowLayout::addLayoutComponent(std::string& s, Component* c) {
}
void FlowLayout::remoteLayoutComponent(Component* c) {
}
Dimension* FlowLayout::minimumLayoutSize(Container* c) {
return c->getSize();
}
Dimension* FlowLayout::preferredLayoutSize(Container* c) {
return c->getSize();
}
void FlowLayout::layoutContainer(Container* parent) {
unsigned int count = parent->getComponentCount();
if (count) {
Dimension* d = parent->getSize();
Border* b = parent->getBorder();
Insets* i = new Insets(0, 0, 0, 0);
if (b) {
i = b->getBorderInsets(parent);
}
d->setSize(d->w - (i->getLeft() + i->getRight()), d->h - (i->getTop() + i->getBottom()));
unsigned int max_row_height = 0;
unsigned int aw = 0;
unsigned int ah = i->getTop();
Dimension* es;
Component* c;
unsigned int prev_index = 0;
if (d->w != prev_size->w) {
for (unsigned int a = 0; a < count; a++) {
c = parent->getComponent(a);
es = c->getPreferredSize();
if ((es->w + aw) > d->w) {
adjustAlignment(d->w - aw, parent, i->getLeft(), prev_index, a);
prev_index = a;
ah += max_row_height;
aw = 0;
max_row_height = 0;
}
c->setLocation(aw, ah);
c->setSize(es);
aw += es->w;
if (es->h > max_row_height) {
max_row_height = es->h;
}
}
prev_size = d;
}
}
}
void FlowLayout::adjustAlignment(unsigned int remainder, Container* parent,
unsigned int left_offset,
unsigned int begin, unsigned int end) {
if (align != LEFT) {
unsigned int tx;
switch (align) {
case RIGHT:
tx = remainder;
break;
case CENTER:
tx = remainder >> 1;
break;
}
Component* c;
for (int a = begin; a < end; a++) {
c = parent->getComponent(a);
c->setLocation(c->getX() + tx + left_offset, c->getY());
}
}
}
}
}
/*
* Copyright (c) 2018, jlhawkwell
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* File: FlowLayout.h
* Author: jlhawkwell
*
* Created on March 24, 2018, 8:53 PM
*/
#ifndef FLOWLAYOUT_H
#define FLOWLAYOUT_H
#include <ntk.h>
#include <nitrogen/nwt/Component.h>
#include <nitrogen/nwt/Container.h>
#include <nitrogen/nwt/LayoutManager.h>
namespace nitrogen {
namespace nwt {
class FlowLayout : public LayoutManager {
public:
static const unsigned int LEFT = 0;
static const unsigned int CENTER = 1;
static const unsigned int RIGHT = 2;
FlowLayout();
FlowLayout(unsigned int alignment);
FlowLayout(unsigned int alignment, unsigned int hgap, unsigned int vgap);
virtual void addLayoutComponent(std::string*, Component*) override;
virtual void addLayoutComponent(std::string&, Component*) override;
virtual void remoteLayoutComponent(Component*);
virtual Dimension* minimumLayoutSize(Container*) override;
virtual Dimension* preferredLayoutSize(Container*) override;
void layoutContainer(Container*) override;
protected:
unsigned int align;
unsigned int flc, flr;
unsigned int flh, flv;
Dimension* prev_size;
private:
private:
virtual void adjustAlignment(unsigned int remainder, Container* parent,
unsigned int left_offset,
unsigned int begin, unsigned int end);
};
}
}
#endif /* FLOWLAYOUT_H */
......@@ -57,6 +57,7 @@ namespace nitrogen {
virtual Dimension* preferredLayoutSize(Container*) override;
void layoutContainer(Container*) override;
protected:
unsigned int glr;
unsigned int glc;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!