]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add Table widget
authorMikko Rasa <tdb@tdb.fi>
Mon, 28 Apr 2008 13:51:58 +0000 (13:51 +0000)
committerMikko Rasa <tdb@tdb.fi>
Mon, 28 Apr 2008 13:51:58 +0000 (13:51 +0000)
Bugfixes in List

source/list.cpp
source/panel.cpp
source/table.cpp [new file with mode: 0644]
source/table.h [new file with mode: 0644]

index 722e296edf9cae096b19e2c736f6075d439dc686..8c0d789188ab917221336d98f09efbc66bb561fe 100644 (file)
@@ -71,6 +71,8 @@ void List::clear()
 {
        items.clear();
        sel_index=-1;
+
+       recalculate_parameters();
 }
 
 void List::set_selected_index(int i)
@@ -261,7 +263,8 @@ void List::recalculate_parameters()
 
 void List::slider_value_changed(double value)
 {
-       first=items.size()-n_visible-static_cast<unsigned>(value);
+       if(items.size()>n_visible)
+               first=items.size()-n_visible-static_cast<unsigned>(value);
 }
 
 
index f17439a5eca810b8510d3537fc750a53c2b5868a..4d0d5d1359ef71cb0ea770c43bb6011daa839ad5 100644 (file)
@@ -15,6 +15,7 @@ Distributed under the LGPL
 #include "list.h"
 #include "panel.h"
 #include "part.h"
+#include "table.h"
 #include "toggle.h"
 #include "vslider.h"
 
@@ -233,6 +234,7 @@ Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
        add("label",     &Loader::child<Label>);
        add("list",      &Loader::child<List>);
        add("panel",     &Loader::panel);
+       add("table",     &Loader::child<Table>);
        add("toggle",    &Loader::child<Toggle>);
        add("vslider",   &Loader::child<VSlider>);
 }
diff --git a/source/table.cpp b/source/table.cpp
new file mode 100644 (file)
index 0000000..7f334a0
--- /dev/null
@@ -0,0 +1,145 @@
+/* $Id$
+
+This file is part of libmspgltk
+Copyright © 2008  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include <msp/gl/matrix.h>
+#include <msp/gl/transform.h>
+#include "part.h"
+#include "style.h"
+#include "table.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GLtk {
+
+Table::Table(const Resources &r):
+       Widget(r),
+       rows(1),
+       columns(1),
+       data(1),
+       col_w(1)
+{
+       update_style();
+}
+
+void Table::set_rows(unsigned r)
+{
+       rows=r;
+       data.resize(rows*columns);
+}
+
+void Table::set_columns(unsigned c)
+{
+       vector<string> new_data(rows*c);
+       for(unsigned i=0; i<rows; ++i)
+               for(unsigned j=0; j<min(c, columns); ++j)
+                       new_data[i*c+j].swap(data[i*columns+j]);
+       data.swap(new_data);
+       columns=c;
+
+       col_w.resize(columns);
+}
+
+void Table::set_column_width(unsigned c, unsigned w)
+{
+       if(c>=columns)
+               throw InvalidParameterValue("Column index out of bounds");
+       col_w[c]=w;
+}
+
+void Table::set_cell_text(unsigned r, unsigned c, const string &t)
+{
+       if(r>=rows || c>=columns)
+               throw InvalidParameterValue("Cell coordinates out of bounds");
+
+       data[r*columns+c]=t;
+}
+
+void Table::render_special(const Part &part) const
+{
+       if(part.get_name()=="cells")
+       {
+               const GL::Font *const font=style->get_font();
+               const float font_size=font->get_default_size();
+
+               unsigned free_width=geom.w;
+               for(unsigned i=0; i<columns; ++i)
+                       free_width-=col_w[i];
+
+               Geometry cgeom;
+               cgeom.h=geom.h/rows;
+               cgeom.y=geom.h-cgeom.h;
+
+               for(unsigned i=0; i<rows; ++i)
+               {
+                       cgeom.x=0;
+                       for(unsigned j=0; j<columns; ++j)
+                       {
+                               if(col_w[j])
+                                       cgeom.w=col_w[j];
+                               else
+                                       cgeom.w=free_width/columns;
+
+                               const string &text=data[i*columns+j];
+                               Geometry rgeom;
+                               rgeom.x=cgeom.x;
+                               rgeom.y=cgeom.y;
+                               rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
+                               rgeom.h=static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
+
+                               part.get_alignment().apply(rgeom, cgeom, part.get_margin());
+
+                               GL::push_matrix();
+                               GL::translate(rgeom.x, rgeom.y, 0);
+                               GL::scale_uniform(font_size);
+                               font->draw_string(text);
+                               GL::pop_matrix();
+
+                               cgeom.x+=cgeom.w;
+                       }
+
+                       cgeom.y-=cgeom.h;
+               }
+       }
+}
+
+void Table::on_style_change()
+{
+}
+
+
+Table::Loader::Loader(Table &t):
+       Widget::Loader(t)
+{
+       add("cell_text",    &Loader::cell_text);
+       add("column_width", &Loader::column_width);
+       add("columns",      &Loader::columns);
+       add("rows",         &Loader::rows);
+}
+
+void Table::Loader::cell_text(unsigned r, unsigned c, const string &t)
+{
+       static_cast<Table &>(wdg).set_cell_text(r, c, t);
+}
+
+void Table::Loader::column_width(unsigned c, unsigned w)
+{
+       static_cast<Table &>(wdg).set_column_width(c, w);
+}
+
+void Table::Loader::columns(unsigned c)
+{
+       static_cast<Table &>(wdg).set_columns(c);
+}
+
+void Table::Loader::rows(unsigned r)
+{
+       static_cast<Table &>(wdg).set_rows(r);
+}
+
+} // namespace GLtk
+} // namespace Msp
diff --git a/source/table.h b/source/table.h
new file mode 100644 (file)
index 0000000..190973e
--- /dev/null
@@ -0,0 +1,58 @@
+/* $Id$
+
+This file is part of libmspgltk
+Copyright © 2008  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#ifndef MSP_GLTK_TABLE_H_
+#define MSP_GLTK_TABLE_H_
+
+#include <string>
+#include <vector>
+#include "widget.h"
+
+namespace Msp {
+namespace GLtk {
+
+class Table: public Widget
+{
+public:
+       class Loader: public Widget::Loader
+       {
+       public:
+               Loader(Table &);
+       private:
+               void cell_text(unsigned, unsigned, const std::string &);
+               void column_width(unsigned, unsigned);
+               void columns(unsigned);
+               void rows(unsigned);
+       };
+
+private:
+       unsigned rows;
+       unsigned columns;
+       std::vector<std::string> data;
+       std::vector<unsigned> col_w;
+
+public:
+       Table(const Resources &);
+
+       void set_rows(unsigned);
+       void set_columns(unsigned);
+       unsigned get_rows() const { return rows; }
+       unsigned get_columns() const { return columns; }
+
+       void set_column_width(unsigned, unsigned);
+       void set_cell_text(unsigned, unsigned, const std::string &);
+private:
+       virtual const char *get_class() const { return "table"; }
+
+       virtual void render_special(const Part &) const;
+       virtual void on_style_change();
+};
+
+} // namespace GLtk
+} // namespace Msp
+
+#endif