126 lines
3.5 KiB
C++
126 lines
3.5 KiB
C++
#include "SidebarToolbox.h"
|
|
#include "Window.h"
|
|
|
|
|
|
void SidebarToolbox::buttonToggled(GtkWidget *button, SidebarToolbox *toolbox) {
|
|
if(!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
|
|
return;
|
|
|
|
toolbox->activateTool(toolbox->buttonsRev[button]);
|
|
|
|
toolbox->window->update();
|
|
}
|
|
|
|
void SidebarToolbox::sizeAllocate(GtkWidget *widget, GtkAllocation *allocation, SidebarToolbox *toolbox) {
|
|
toolbox->updateRows(false);
|
|
}
|
|
|
|
|
|
void SidebarToolbox::updateRows(bool changed) {
|
|
GtkRequisition requisition;
|
|
|
|
gtk_widget_size_request(buttons[tools.front()], &requisition);
|
|
|
|
int cols = MAX(1, widget->allocation.width/requisition.width);
|
|
int rows = (tools.size()+cols-1)/cols;
|
|
|
|
if(changed || this->cols != cols || this->rows != rows) {
|
|
this->cols = cols;
|
|
this->rows = rows;
|
|
|
|
for(std::map<Tool*, GtkWidget*>::iterator button = buttons.begin(); button != buttons.end(); button++) {
|
|
if(gtk_widget_get_parent(button->second))
|
|
gtk_container_remove(GTK_CONTAINER(widget), button->second);
|
|
}
|
|
|
|
gtk_table_resize(GTK_TABLE(widget), MAX(1, rows), MAX(1, cols));
|
|
|
|
int iCol = 0, iRow = 0;
|
|
for(std::list<Tool*>::iterator tool = tools.begin(); tool != tools.end(); tool++) {
|
|
gtk_table_attach(GTK_TABLE(widget), buttons[*tool], iCol, iCol+1, iRow, iRow+1, GTK_FILL, GTK_FILL, 0, 0);
|
|
|
|
iCol++;
|
|
if(iCol >= cols) {
|
|
iRow++;
|
|
iCol = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void SidebarToolbox::activateTool(Tool *tool) {
|
|
if(activeTool == tool)
|
|
return;
|
|
|
|
if(activeTool)
|
|
activeTool->deactivate();
|
|
|
|
activeTool = tool;
|
|
tool->activate();
|
|
}
|
|
|
|
SidebarToolbox::SidebarToolbox(Window *window) : toolSelector(&window->getEditManager()) {
|
|
this->window = window;
|
|
|
|
widget = gtk_table_new(1, 1, TRUE);
|
|
g_object_ref_sink(G_OBJECT(widget));
|
|
|
|
g_signal_connect(G_OBJECT(widget), "size-allocate", G_CALLBACK(sizeAllocate), this);
|
|
|
|
addTool(&toolSelector);
|
|
activeTool = NULL;
|
|
activateTool(&toolSelector);
|
|
|
|
gtk_widget_show(widget);
|
|
}
|
|
|
|
SidebarToolbox::~SidebarToolbox() {
|
|
for(std::map<Tool*, GtkWidget*>::iterator button = buttons.begin(); button != buttons.end(); button++) {
|
|
g_object_unref(G_OBJECT(button->second));
|
|
}
|
|
|
|
g_object_unref(G_OBJECT(widget));
|
|
}
|
|
|
|
void SidebarToolbox::addTool(Tool *tool) {
|
|
GtkWidget *button;
|
|
if(tools.empty())
|
|
button = gtk_radio_button_new(NULL);
|
|
else
|
|
button = gtk_radio_button_new_from_widget(GTK_RADIO_BUTTON(buttons[tools.front()]));
|
|
g_object_ref_sink(G_OBJECT(button));
|
|
gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(button), FALSE);
|
|
gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
|
|
gtk_button_set_image(GTK_BUTTON(button), tool->getImage());
|
|
|
|
gtk_widget_show(button);
|
|
g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(buttonToggled), this);
|
|
|
|
tools.push_back(tool);
|
|
buttons.insert(std::pair<Tool*, GtkWidget*>(tool, button));
|
|
buttonsRev.insert(std::pair<GtkWidget*, Tool*>(button, tool));
|
|
|
|
updateRows(true);
|
|
update();
|
|
}
|
|
|
|
void SidebarToolbox::removeTool(Tool *tool) {
|
|
if(tool == activeTool) {
|
|
activateTool(&toolSelector);
|
|
}
|
|
|
|
tools.remove(tool);
|
|
|
|
buttonsRev.erase(buttons[tool]);
|
|
g_object_unref(G_OBJECT(buttons[tool]));
|
|
buttons.erase(tool);
|
|
|
|
updateRows(true);
|
|
}
|
|
|
|
void SidebarToolbox::update() {
|
|
for(std::list<Tool*>::iterator tool = tools.begin(); tool != tools.end(); tool++) {
|
|
gtk_widget_set_tooltip_text(buttons[*tool], (*tool)->getName());
|
|
gtk_widget_set_sensitive(buttons[*tool], (*tool)->isSensitive());
|
|
}
|
|
}
|