From d389dce3ef4f1342a2f91926a9dcd02b452c6045 Mon Sep 17 00:00:00 2001 From: neoraider Date: Tue, 4 Dec 2007 21:35:01 +0000 Subject: zoomedit: Got SidebarToolbox working. --- SidebarToolbox.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 82 insertions(+), 12 deletions(-) (limited to 'SidebarToolbox.cpp') diff --git a/SidebarToolbox.cpp b/SidebarToolbox.cpp index ed5e5da..3094d2a 100644 --- a/SidebarToolbox.cpp +++ b/SidebarToolbox.cpp @@ -1,31 +1,101 @@ #include "SidebarToolbox.h" -SidebarToolbox::SidebarToolbox() -{ - widget = gtk_vbox_new(FALSE, 0); + +void SidebarToolbox::toolAction(GtkWidget *button, Tool *tool) { + tool->action(); +} + +void SidebarToolbox::sizeAllocate(GtkWidget *widget, GtkAllocation *allocation, SidebarToolbox *toolbox) { + toolbox->updateRows(false); +} + +SidebarToolbox::SidebarToolbox() { + + cols = 1; rows = 0; + + 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); + + gtk_widget_show(widget); } -SidebarToolbox::~SidebarToolbox() -{ +SidebarToolbox::~SidebarToolbox() { + for(std::map::iterator button = buttons.begin(); button != buttons.end(); button++) { + g_object_unref(G_OBJECT(button->second)); + } + g_object_unref(G_OBJECT(widget)); } -bool SidebarToolbox::addTool(Tool *tool) { - bool ret = tools.insert(tool).second; +void SidebarToolbox::addTool(Tool *tool) { + tools.push_back(tool); + + GtkWidget *button = gtk_button_new(); + g_object_ref_sink(G_OBJECT(button)); + gtk_button_set_image(GTK_BUTTON(button), tool->getImage()); + gtk_widget_show(button); + g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(toolAction), tool); + + buttons.insert(std::pair(tool, button)); + updateRows(true); update(); +} + +void SidebarToolbox::removeTool(Tool *tool) { + tools.remove(tool); - return ret; + g_object_unref(G_OBJECT(buttons[tool])); + buttons.erase(tool); + + updateRows(true); } -bool SidebarToolbox::removeTool(Tool *tool) { - bool ret = (tools.erase(tool) > 0); +void SidebarToolbox::updateRows(bool changed) { + int cols, rows; + GtkRequisition requisition = {0, 0}; - update(); + if(tools.empty()) { + cols = 1; rows = 0; + } + else { + gtk_widget_size_request(buttons[tools.front()], &requisition); + + cols = MAX(1, widget->allocation.width/requisition.width); + rows = (tools.size()+cols-1)/cols; + } - return ret; + if(this->cols != cols || this->rows != rows || changed) { + this->cols = cols; + this->rows = rows; + + gtk_widget_set_size_request(widget, -1, rows*requisition.height); + + for(std::map::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, cols), MAX(1, rows)); + + int iCol = 0, iRow = 0; + for(std::list::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::update() { + for(std::list::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()); + } } -- cgit v1.2.3