////////////////////////////////////////////////////////////////////////////////
// GLTableView.h
// Copyright Bjoern Ganster 2007
////////////////////////////////////////////////////////////////////////////////

#ifndef GLTableView__H
#define GLTableView__H

#include "GLWindow.h"
#include "BGCPString.h"
#include "GLScrollbar.h"

#include <vector>
using namespace std;

////////////////////////////////////////////////////////////////////////////////
// Abstract base class for TableView data sources

class GLTableViewDataSource {
public:
	virtual void getDimensions (size_t senderID, size_t& rowCount, size_t& colCount) = 0;

	// Get string for a cell
	virtual bool getString (size_t senderID, int row, int col, BGCPString& str) = 0;

	// Request context menu
	virtual void showContextMenu(VirtualClass* /*sender*/, int /*x*/, int /*y*/)
	{
	}
};

////////////////////////////////////////////////////////////////////////////////
// Base class for a table view

class GLTableView: public GLWindow {
public:
	Color textColor, selectionColor;
	BGCPString idString;

	// Constructor
	GLTableView (GLWindow* _parent = NULL, int _left = 0, int _top = 0, 
				int _width = 0, int _height = 0, int _ID = 0);

	// Destructor
	~GLTableView() OVERRIDE;

	// Set data source
	void setDataSource (GLTableViewDataSource* ds)
	{ dataSource = ds; }

	// Get/Set column/row count
	void setColWidth (size_t num, size_t newVal, size_t defaultWidth = 50);

	//void setRowHeight (size_t num, size_t newVal, size_t defaultHeight = 15);

	// Set a single height for all rows
	void setRowHeights (size_t num, size_t newVal);

	// Render
	void render(int x, int y, int w, int h) OVERRIDE;

	// Keyboard Events
	void keypressed (BGKey /*key*/, bool /*shiftHeld*/, bool /*ctrlHeld*/, bool /*down*/, bool /*repeat*/) OVERRIDE;

	// Button state changed over a window: returns child that was clicked
	GLWindow* buttonStateChanged (int /*button*/, bool down, int /*x*/, int /*y*/) OVERRIDE;

	void mouseMoved (int /*x*/, int y) OVERRIDE;

	int getSelectedRow() const;
	void setSelectedRow(int newVal);
	void clearSelection();

protected:
	vector <size_t> colStart, rowStart;
	GLTableViewDataSource* dataSource;
	GLScrollbar* vScrollbar;
	int selectedRow;
	size_t ID;
	int mInnerMargin;

	// Find selected row
	void findSelectedRow (int y);

	size_t getRowStart(size_t row);
};

////////////////////////////////////////////////////////////////////////////////
// GLStringTableView: specialized for rendering a table of strings

class GLStringTableView: public GLTableView, public GLTableViewDataSource {
public:
	// Constructor
	GLStringTableView (GLWindow* _parent = NULL, int _left = 0, int _top = 0, 
				int _width = 0, int _height = 0, int _ID = 0);

	void setString(int row, int col, const BGCPString& str);

	void getDimensions (size_t senderID, size_t& rowCount, size_t& colCount) OVERRIDE;

	// Get string for a cell
	bool getString (size_t senderID, int row, int col, BGCPString& str) OVERRIDE;

	// Request context menu
	void showContextMenu(VirtualClass* /*sender*/, int /*x*/, int /*y*/) OVERRIDE;
private:
	std::vector<std::vector<BGCPString>> mTableStrings;
};

#endif
