ViewManager::ViewManager(DwarfModel *dm, DwarfModelProxy *proxy,
                         QWidget *parent)
    : QTabWidget(parent)
    , m_model(dm)
    , m_proxy(proxy)
    , m_add_tab_button(new QToolButton(this))
{
    m_proxy->setSourceModel(m_model);
    setTabsClosable(true);
    setMovable(true);

    //reload_views();

    m_add_tab_button->setText(tr("Add "));
    m_add_tab_button->setIcon(QIcon(":img/ui-tab--plus.png"));
    m_add_tab_button->setPopupMode(QToolButton::InstantPopup);
    m_add_tab_button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    m_add_tab_button->setToolTip(tr("Add an existing view. New views can be copied or created from the [Windows->Docks->Grid Views] dock."));
    draw_add_tab_button();
    setCornerWidget(m_add_tab_button, Qt::TopLeftCorner);    

    connect(tabBar(), SIGNAL(tabMoved(int, int)), SLOT(write_views()));
    connect(tabBar(), SIGNAL(currentChanged(int)), SLOT(setCurrentIndex(int)), Qt::UniqueConnection);
    connect(this, SIGNAL(tabCloseRequested(int)), SLOT(remove_tab_for_gridview(int)));
    connect(m_model, SIGNAL(need_redraw()), SLOT(redraw_current_tab()));
    //draw_views();

    m_squad_warning = new QErrorMessage(this);    
}
static void on_mouse_button_up(struct glwin *win, int button, int x, int y)
{
	if (g_mouse_down[0] && button == 1) {
		struct quaternion next;
		quaternion_mul(&q_delta, &q_cur, &next);
		q_cur = next;
		q_delta.x = 0;
		q_delta.y = 0;
		q_delta.z = 0;
		q_delta.w = 1;
		need_redraw(g_L);
	}
	if (g_mouse_down[2] && button == 3) {
		g_offset[0] += g_offset_next[0];
		g_offset[1] += g_offset_next[1];
		g_offset[2] += g_offset_next[2];
		g_offset_next[0] = 0;
		g_offset_next[1] = 0;
		g_offset_next[2] = 0;
		need_redraw(g_L);
	}
	g_mouse_down[button -1] = false;
	on_mouse_move(win, x, y);
}
static int create_glwin(lua_State *L)
{
	int xid = lua_tointeger(L, -1);
	g_win = glwin_create_window("B2L 3D View", &cb, 512, 512);
	if (!g_win)
		exit(-1);

	glwin_set_type(g_win, GLWIN_UTILITY);
	glwin_set_transient_for(g_win, xid);

	glwin_show_window(g_win);

	g_ctx = glwin_create_context(g_win, 3, 3);
	if (!g_ctx)
		exit(-1);
	glwin_make_current(g_win, g_ctx);
	glb_glcore_init(3, 3);

	need_redraw(L);

	return 0;
}
static void on_resize(struct glwin *win)
{
	need_redraw(g_L);
}
static void on_expose(struct glwin *win)
{
	need_redraw(g_L);
}