Mirror / src / statusBar.cpp

#include "statusBar.h"
#include "setup.h"
#include "monitor.h"

StatusBar::StatusBar(Setup *setup, Monitor monitor):
    setup(setup)
{
    xcb_window_t id = xcb_generate_id(setup->conn);
    int width = monitor.area.width;
    uint32_t values[] = {XCB_EVENT_MASK_EXPOSURE};
    xcb_create_window( setup->conn, XCB_COPY_FROM_PARENT, id, setup->root, 
                        monitor.area.x, monitor.area.y, width, setup->statusBarHeight, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT, 
                        setup->screen->root_visual, XCB_CW_EVENT_MASK, values );
    
    xcb_visualtype_t *visualType = nullptr;
    auto depthIter = xcb_screen_allowed_depths_iterator(setup->screen);
    for (; depthIter.rem; xcb_depth_next(&depthIter))
    {
        auto visualIter = xcb_depth_visuals_iterator(depthIter.data);
        for (; visualIter.rem; xcb_visualtype_next(&visualIter))
        {
            if (setup->screen->root_visual == visualIter.data->visual_id)
            {
                visualType = visualIter.data;
            }
        }
    }
    surface =  Cairo::XcbSurface::create(setup->conn, id, visualType, width, setup->statusBarHeight);
    context = Cairo::Context::create(surface);
    
    
    xcb_map_window(setup->conn, id);
    draw(); 

    xcb_flush(setup->conn);
}

StatusBar::~StatusBar()
{
}

void StatusBar::setText(std::string newText)
{
    text = newText;
    draw();
}


void StatusBar::draw()
{
    context->set_source_rgb(0, .5, 1);
    context->paint();
    context->move_to(5, 15);
    context->set_source_rgb(1, 1, 1);
    context->set_font_size(12);
    context->show_text(text);
	context->move_to(setup->monitors[0].area.width/2, 15);
	context->show_text(timeText);
	Cairo::TextExtents extents;
	context->get_text_extents("VimWM", extents);
	//std::cout << extents.width << std::endl;
	//std::cout << extents.x_advance << std::endl;
	context->move_to(setup->monitors[0].area.width - extents.width-5, 15);
	context->show_text("VimWM");
	xcb_flush(setup->conn);
}

void StatusBar::update()
{
	char buffer[80];
	time_t rawTime;
	struct tm *timeInfo;
	time(&rawTime);
	timeInfo = localtime(&rawTime);
	std::string oldTimeText = timeText;
	timeText = std::string(buffer, strftime(buffer, 80, "%l:%M", timeInfo));
	if (timeText != oldTimeText)
	{
		draw();
	}
}

void StatusBar::handleExposeEvent(EventPtr event)
{
    draw();
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.