Skip to content

Commit

Permalink
Change Toolbar container type
Browse files Browse the repository at this point in the history
  • Loading branch information
bhennion committed Sep 16, 2023
1 parent ac377d4 commit 9dc83db
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 264 deletions.
4 changes: 2 additions & 2 deletions src/core/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,9 @@ auto MainWindow::getSelectedToolbar() const -> ToolbarData* { return this->selec

auto MainWindow::getToolbarWidgets() const -> const ToolbarWidgetArray& { return toolbarWidgets; }

auto MainWindow::getToolbarName(GtkToolbar* toolbar) const -> const char* {
auto MainWindow::getToolbarName(GtkWidget* toolbar) const -> const char* {
for (size_t i = 0; i < TOOLBAR_DEFINITIONS_LEN; i++) {
if (static_cast<void*>(this->toolbarWidgets[i].get()) == static_cast<void*>(toolbar)) {
if (this->toolbarWidgets[i].get() == toolbar) {
return TOOLBAR_DEFINITIONS[i].propName;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/gui/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class MainWindow: public GladeGui {
void updateColorscheme();

const ToolbarWidgetArray& getToolbarWidgets() const;
const char* getToolbarName(GtkToolbar* toolbar) const;
const char* getToolbarName(GtkWidget* toolbar) const;

Layout* getLayout() const;

Expand Down
4 changes: 2 additions & 2 deletions src/core/gui/ToolbarDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ typedef struct {
bool horizontal;
} ToolbarEntryDefintion;

const static ToolbarEntryDefintion TOOLBAR_DEFINITIONS[] = {
constexpr ToolbarEntryDefintion TOOLBAR_DEFINITIONS[] = {
{"tbTop1", "toolbarTop1", true}, {"tbTop2", "toolbarTop2", true},
{"tbLeft1", "toolbarLeft1", false}, {"tbLeft2", "toolbarLeft2", false},
{"tbRight1", "toolbarRight1", false}, {"tbRight2", "toolbarRight2", false},
Expand All @@ -38,7 +38,7 @@ const static ToolbarEntryDefintion TOOLBAR_DEFINITIONS[] = {
};


const static int TOOLBAR_DEFINITIONS_LEN = G_N_ELEMENTS(TOOLBAR_DEFINITIONS);
constexpr size_t TOOLBAR_DEFINITIONS_LEN = G_N_ELEMENTS(TOOLBAR_DEFINITIONS);

#define TBFloatFirst 8
#define TBFloatLast 11
76 changes: 39 additions & 37 deletions src/core/gui/dialog/toolbarCustomize/ToolbarAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ void ToolbarAdapter::toolitemDragDataGet(GtkWidget* widget, GdkDragContext* cont
/**
* A tool item was dragged to the toolbar
*/
auto ToolbarAdapter::toolbarDragMotionCb(GtkToolbar* toolbar, GdkDragContext* context, gint x, gint y, guint time,
auto ToolbarAdapter::toolbarDragMotionCb(GtkWidget* toolbar, GdkDragContext* context, gint x, gint y, guint time,
ToolbarAdapter* adapter) -> bool {
GdkAtom target = gtk_drag_dest_find_target(GTK_WIDGET(toolbar), context, nullptr);
GdkAtom target = gtk_drag_dest_find_target(toolbar, context, nullptr);
if (target != ToolbarDragDropHelper::atomToolItem) {
gdk_drag_status(context, static_cast<GdkDragAction>(0), time);
return false;
Expand All @@ -236,31 +236,31 @@ auto ToolbarAdapter::toolbarDragMotionCb(GtkToolbar* toolbar, GdkDragContext* co
if (d->type == TOOL_ITEM_ITEM) {
GtkWidget* iconWidget = d->item->getNewToolIcon();
GtkToolItem* it = gtk_tool_button_new(iconWidget, "");
gtk_toolbar_set_drop_highlight_item(toolbar, it, ipos);
// gtk_toolbar_set_drop_highlight_item(toolbar, it, ipos);
} else if (d->type == TOOL_ITEM_SEPARATOR) {
GtkToolItem* it = gtk_separator_tool_item_new();
gtk_toolbar_set_drop_highlight_item(toolbar, it, ipos);
// gtk_toolbar_set_drop_highlight_item(toolbar, it, ipos);
} else if (d->type == TOOL_ITEM_SPACER) {
GtkToolItem* it = gtk_separator_tool_item_new();
gtk_tool_item_set_expand(it, true);
gtk_toolbar_set_drop_highlight_item(toolbar, it, ipos);
// gtk_toolbar_set_drop_highlight_item(toolbar, it, ipos);
} else if (d->type == TOOL_ITEM_COLOR) {
GtkWidget* iconWidget = ColorIcon::newGtkImage(d->namedColor->getColor(), 16, true);
GtkToolItem* it = gtk_tool_button_new(iconWidget, "");
gtk_toolbar_set_drop_highlight_item(toolbar, it, ipos);
// gtk_toolbar_set_drop_highlight_item(toolbar, it, ipos);
} else {
g_warning("ToolbarAdapter::toolbarDragMotionCb Unhandled type %i", d->type);
}

return true;
}

void ToolbarAdapter::toolbarDragLeafeCb(GtkToolbar* toolbar, GdkDragContext* context, guint time,
void ToolbarAdapter::toolbarDragLeafeCb(GtkWidget* toolbar, GdkDragContext* context, guint time,
ToolbarAdapter* adapter) {
gtk_toolbar_set_drop_highlight_item(toolbar, nullptr, -1);
// gtk_toolbar_set_drop_highlight_item(toolbar, nullptr, -1);
}

void ToolbarAdapter::toolbarDragDataReceivedCb(GtkToolbar* toolbar, GdkDragContext* context, gint x, gint y,
void ToolbarAdapter::toolbarDragDataReceivedCb(GtkWidget* toolbar, GdkDragContext* context, gint x, gint y,
GtkSelectionData* data, guint info, guint time,
ToolbarAdapter* adapter) {
auto* d = reinterpret_cast<ToolItemDragDropData const*>(gtk_selection_data_get_data(data));
Expand All @@ -270,11 +270,11 @@ void ToolbarAdapter::toolbarDragDataReceivedCb(GtkToolbar* toolbar, GdkDragConte
gint pos = toolbarGetDropIndex(toolbar, x, y, horizontal);

if (d->type == TOOL_ITEM_ITEM) {
GtkToolItem* it = d->item->createToolItem(horizontal);
GtkWidget* it = d->item->createItem(horizontal);

gtk_widget_show_all(GTK_WIDGET(it));
gtk_toolbar_insert(toolbar, it, pos);
adapter->prepareToolItem(it);
gtk_widget_show_all(it);
// gtk_toolbar_insert(toolbar, it, pos);
// adapter->prepareToolItem(it);

ToolbarData* tb = adapter->window->getSelectedToolbar();
const char* name = adapter->window->getToolbarName(toolbar);
Expand All @@ -286,25 +286,25 @@ void ToolbarAdapter::toolbarDragDataReceivedCb(GtkToolbar* toolbar, GdkDragConte
} else if (d->type == TOOL_ITEM_COLOR) {
auto* item = new ColorToolItem(*(d->namedColor));

GtkToolItem* it = item->createToolItem(horizontal);

gtk_widget_show_all(GTK_WIDGET(it));
gtk_toolbar_insert(toolbar, it, pos);
adapter->prepareToolItem(it);

ToolbarData* tb = adapter->window->getSelectedToolbar();
const char* name = adapter->window->getToolbarName(toolbar);

string id = item->getId();
// GtkToolItem* it = item->createItem(horizontal);

int newId = tb->insertItem(name, id, pos);
ToolitemDragDrop::attachMetadataColor(GTK_WIDGET(it), newId, d->namedColor, item);
// gtk_widget_show_all(GTK_WIDGET(it));
// gtk_toolbar_insert(toolbar, it, pos);
// adapter->prepareToolItem(it);
//
// ToolbarData* tb = adapter->window->getSelectedToolbar();
// const char* name = adapter->window->getToolbarName(toolbar);
//
// string id = item->getId();
//
// int newId = tb->insertItem(name, id, pos);
// ToolitemDragDrop::attachMetadataColor(GTK_WIDGET(it), newId, d->namedColor, item);

adapter->window->getToolMenuHandler()->addColorToolItem(item);
} else if (d->type == TOOL_ITEM_SEPARATOR) {
GtkToolItem* it = gtk_separator_tool_item_new();
gtk_widget_show_all(GTK_WIDGET(it));
gtk_toolbar_insert(toolbar, it, pos);
// gtk_toolbar_insert(toolbar, it, pos);
adapter->prepareToolItem(it);

ToolbarData* tb = adapter->window->getSelectedToolbar();
Expand All @@ -317,7 +317,7 @@ void ToolbarAdapter::toolbarDragDataReceivedCb(GtkToolbar* toolbar, GdkDragConte
gtk_separator_tool_item_set_draw(GTK_SEPARATOR_TOOL_ITEM(it), false);
gtk_tool_item_set_expand(it, true);
gtk_widget_show_all(GTK_WIDGET(it));
gtk_toolbar_insert(toolbar, it, pos);
// gtk_toolbar_insert(toolbar, it, pos);
adapter->prepareToolItem(it);

ToolbarData* tb = adapter->window->getSelectedToolbar();
Expand All @@ -330,19 +330,21 @@ void ToolbarAdapter::toolbarDragDataReceivedCb(GtkToolbar* toolbar, GdkDragConte
}
}

auto ToolbarAdapter::toolbarGetDropIndex(GtkToolbar* toolbar, gint x, gint y, bool horizontal) -> gint {
auto ToolbarAdapter::toolbarGetDropIndex(GtkWidget* toolbar, gint x, gint y, bool horizontal) -> gint {
return 0;
/*
* gtk_toolbar_get_drop_index does not use the correct coordinate system for the y coordinate
* since the y coordinate is only relevant in vertical toolbars the transformation is NOT required for horizontal
* toolbars
*/
if (horizontal) {
return gtk_toolbar_get_drop_index(toolbar, x, y);
} else {
gint wx = 0;
gint wy = 0;
gtk_widget_translate_coordinates(GTK_WIDGET(toolbar), gtk_widget_get_toplevel(GTK_WIDGET(toolbar)), x, y, &wx,
&wy);
return gtk_toolbar_get_drop_index(toolbar, x, wy);
}
// if (horizontal) {
// return gtk_toolbar_get_drop_index(toolbar, x, y);
// } else {
// gint wx = 0;
// gint wy = 0;
// gtk_widget_translate_coordinates(GTK_WIDGET(toolbar), gtk_widget_get_toplevel(GTK_WIDGET(toolbar)), x, y,
// &wx,
// &wy);
// return gtk_toolbar_get_drop_index(toolbar, x, wy);
// }
}
8 changes: 4 additions & 4 deletions src/core/gui/dialog/toolbarCustomize/ToolbarAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ class ToolbarAdapter {
/**
* A tool item was dragged to the toolbar
*/
static bool toolbarDragMotionCb(GtkToolbar* toolbar, GdkDragContext* context, gint x, gint y, guint time,
static bool toolbarDragMotionCb(GtkWidget* toolbar, GdkDragContext* context, gint x, gint y, guint time,
ToolbarAdapter* adapter);
static void toolbarDragLeafeCb(GtkToolbar* toolbar, GdkDragContext* context, guint time, ToolbarAdapter* adapter);
static void toolbarDragDataReceivedCb(GtkToolbar* toolbar, GdkDragContext* context, gint x, gint y,
static void toolbarDragLeafeCb(GtkWidget* toolbar, GdkDragContext* context, guint time, ToolbarAdapter* adapter);
static void toolbarDragDataReceivedCb(GtkWidget* toolbar, GdkDragContext* context, gint x, gint y,
GtkSelectionData* data, guint info, guint time, ToolbarAdapter* adapter);

/**
Expand All @@ -70,7 +70,7 @@ class ToolbarAdapter {
* @param horizontal denotes whether the toolbar is horizontal
* @return gint position at which drop item should be inserted
*/
static gint toolbarGetDropIndex(GtkToolbar* toolbar, gint x, gint y, bool horizontal);
static gint toolbarGetDropIndex(GtkWidget* toolbar, gint x, gint y, bool horizontal);

private:
GtkWidget* w;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void ToolbarDragDropHandler::prepareToolbarsForDragAndDrop() {
this->toolbars.clear();

for (auto w: win->getToolbarWidgets()) {
this->toolbars.emplace_back(std::make_unique<ToolbarAdapter>(w.get(), win->getToolbarName(GTK_TOOLBAR(w.get())),
this->toolbars.emplace_back(std::make_unique<ToolbarAdapter>(w.get(), win->getToolbarName(w.get()),
control->getWindow()->getToolMenuHandler(), win));
}
}
Expand Down
13 changes: 0 additions & 13 deletions src/core/gui/toolbarMenubar/AbstractToolItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,6 @@ auto AbstractToolItem::getId() const -> std::string { return id; }

auto AbstractToolItem::getItem() const -> GtkWidget* { return this->item.get(); }

GtkToolItem* AbstractToolItem::createToolItem(bool horizontal) {
GtkWidget* item = createItem(horizontal);
if (GTK_IS_TOOL_ITEM(item)) {
return GTK_TOOL_ITEM(item);
}

// Wrap in a GtkToolItem
GtkToolItem* wrap = gtk_tool_item_new();
gtk_container_add(GTK_CONTAINER(wrap), item);
this->item.reset(GTK_WIDGET(wrap), xoj::util::adopt);
return wrap;
}

auto AbstractToolItem::isUsed() const -> bool { return used; }

void AbstractToolItem::setUsed(bool used) { this->used = used; }
2 changes: 0 additions & 2 deletions src/core/gui/toolbarMenubar/AbstractToolItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class AbstractToolItem {
public:
virtual GtkWidget* createItem(bool horizontal) = 0;

GtkToolItem* createToolItem(bool horizontal);

bool isUsed() const;
void setUsed(bool used);

Expand Down
20 changes: 1 addition & 19 deletions src/core/gui/toolbarMenubar/ColorSelectorToolItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,7 @@ auto ColorSelectorToolItem::createItem(bool) -> GtkWidget* {
}),
btn, GConnectFlags(0));

// Make a proxy for GtkToolbar
GtkToolItem* it = gtk_tool_item_new();
gtk_container_add(GTK_CONTAINER(it), btn);
/// Makes a proxy item for the toolbar's overflow menu
auto createProxy = [&]() {
constexpr int PROXY_ICON_SIZE = 16;
GtkWidget* proxy = gtk_menu_item_new();
auto* box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
gtk_container_add(GTK_CONTAINER(proxy), box);
gtk_box_append(GTK_BOX(box), ColorIcon::newGtkImage(Colors::gray, PROXY_ICON_SIZE, false));
gtk_box_append(GTK_BOX(box), gtk_label_new(getToolDisplayName().c_str()));
gtk_actionable_set_action_name(GTK_ACTIONABLE(proxy),
(std::string("win.") + Action_toString(Action::SELECT_FONT)).c_str());
xoj::util::gtk::fixActionableInitialSensitivity(GTK_ACTIONABLE(proxy));
return proxy;
};
gtk_tool_item_set_proxy_menu_item(it, "", createProxy());
this->item.reset(GTK_WIDGET(it), xoj::util::adopt);

this->item.reset(GTK_WIDGET(btn), xoj::util::adopt);
return this->item.get();
}

Expand Down
27 changes: 1 addition & 26 deletions src/core/gui/toolbarMenubar/ColorToolItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,7 @@ auto ColorToolItem::createItem(bool) -> GtkWidget* {
gtk_widget_set_tooltip_text(btn, this->namedColor.getName().c_str());
gtk_button_set_child(GTK_BUTTON(btn), getNewToolIcon());


// For the sake of deprecated GtkToolbar, wrap the button in a GtkToolItem
// Todo(gtk4): remove
GtkToolItem* it = gtk_tool_item_new();
gtk_container_add(GTK_CONTAINER(it), btn);
/// Makes a proxy item for the toolbar's overflow menu
auto createProxy = [this]() {
GtkWidget* proxy = gtk_check_menu_item_new();
gtk_check_menu_item_set_draw_as_radio(GTK_CHECK_MENU_ITEM(proxy), true);

auto* box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
gtk_container_add(GTK_CONTAINER(proxy), box);
gtk_box_append(GTK_BOX(box), getNewToolIcon());
gtk_box_append(GTK_BOX(box), gtk_label_new(getToolDisplayName().c_str()));

gtk_actionable_set_action_name(GTK_ACTIONABLE(proxy),
(std::string("win.") + Action_toString(Action::TOOL_COLOR)).c_str());
if (target) {
gtk_actionable_set_action_target_value(GTK_ACTIONABLE(proxy), target.get());
}
xoj::util::gtk::fixActionableInitialSensitivity(GTK_ACTIONABLE(proxy));
return proxy;
};
gtk_tool_item_set_proxy_menu_item(it, "", createProxy());
this->item.reset(GTK_WIDGET(it), xoj::util::adopt);

this->item.reset(GTK_WIDGET(btn), xoj::util::adopt);
return this->item.get();
}

Expand Down
17 changes: 1 addition & 16 deletions src/core/gui/toolbarMenubar/FontButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,7 @@ auto FontButton::createItem(bool horizontal) -> GtkWidget* {
}),
btn, GConnectFlags(0));

GtkToolItem* it = gtk_tool_item_new();
gtk_container_add(GTK_CONTAINER(it), btn);
/// Makes a proxy item for the toolbar's overflow menu
auto createProxy = [this]() {
GtkWidget* proxy = gtk_menu_item_new();
auto* box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
gtk_container_add(GTK_CONTAINER(proxy), box);
gtk_box_append(GTK_BOX(box), getNewToolIcon());
gtk_box_append(GTK_BOX(box), gtk_label_new(getToolDisplayName().c_str()));
gtk_actionable_set_action_name(GTK_ACTIONABLE(proxy),
(std::string("win.") + Action_toString(Action::SELECT_FONT)).c_str());
xoj::util::gtk::fixActionableInitialSensitivity(GTK_ACTIONABLE(proxy));
return proxy;
};
gtk_tool_item_set_proxy_menu_item(it, "", createProxy());
this->item.reset(GTK_WIDGET(it), xoj::util::adopt);
this->item.reset(GTK_WIDGET(btn), xoj::util::adopt);
return this->item.get();
}

Expand Down
51 changes: 4 additions & 47 deletions src/core/gui/toolbarMenubar/ToolButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,62 +45,19 @@ auto ToolButton::createItem(bool horizontal) -> GtkWidget* {
xoj::util::gtk::setToggleButtonUnreleasable(GTK_TOGGLE_BUTTON(btn));
}

// For the sake of deprecated GtkToolbar, wrap the button in a GtkToolItem
// Todo(gtk4): remove
GtkToolItem* it = gtk_tool_item_new();
if (popover) {
GtkMenuButton* menubutton = GTK_MENU_BUTTON(gtk_menu_button_new());
gtk_menu_button_set_popover(menubutton, popover.get());
gtk_menu_button_set_direction(menubutton, GTK_ARROW_DOWN);
gtk_menu_button_set_direction(menubutton, GTK_ARROW_DOWN); // TODO: fix directions

GtkBox* box = GTK_BOX(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0));
GtkBox* box = GTK_BOX(gtk_box_new(horizontal ? GTK_ORIENTATION_HORIZONTAL : GTK_ORIENTATION_VERTICAL, 0));
gtk_box_append(box, btn);
gtk_box_append(box, GTK_WIDGET(menubutton));

gtk_container_add(GTK_CONTAINER(it), GTK_WIDGET(box));

g_signal_connect_object(it, "toolbar-reconfigured", G_CALLBACK(+[](GtkToolItem* it, gpointer box) {
gtk_orientable_set_orientation(GTK_ORIENTABLE(box),
gtk_tool_item_get_orientation(it));
}),
box, GConnectFlags(0));
g_signal_connect_object(it, "toolbar-reconfigured", G_CALLBACK(+[](GtkToolItem* it, gpointer btn) {
const bool h = gtk_tool_item_get_orientation(it) == GTK_ORIENTATION_HORIZONTAL;
gtk_menu_button_set_direction(GTK_MENU_BUTTON(btn),
h ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT);
}),
menubutton, GConnectFlags(0));

this->item.reset(GTK_WIDGET(box), xoj::util::adopt);
} else {
gtk_container_add(GTK_CONTAINER(it), GTK_WIDGET(btn));
this->item.reset(btn, xoj::util::adopt);
}

/// Makes a proxy item for the toolbar's overflow menu
auto createProxy = [this]() {
GtkWidget* proxy;
if (toggle) {
proxy = gtk_check_menu_item_new();
if (target) {
gtk_check_menu_item_set_draw_as_radio(GTK_CHECK_MENU_ITEM(proxy), true);
}
} else {
proxy = gtk_menu_item_new();
}

auto* box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
gtk_container_add(GTK_CONTAINER(proxy), box);
gtk_box_append(GTK_BOX(box), getNewToolIcon());
gtk_box_append(GTK_BOX(box), gtk_label_new(getToolDisplayName().c_str()));

gtk_actionable_set_action_name(GTK_ACTIONABLE(proxy), (std::string("win.") + Action_toString(action)).c_str());
if (target) {
gtk_actionable_set_action_target_value(GTK_ACTIONABLE(proxy), target.get());
}
xoj::util::gtk::fixActionableInitialSensitivity(GTK_ACTIONABLE(proxy));
return proxy;
};
gtk_tool_item_set_proxy_menu_item(it, "", createProxy());
this->item.reset(GTK_WIDGET(it), xoj::util::adopt);
return this->item.get();
}

Expand Down
Loading

0 comments on commit 9dc83db

Please sign in to comment.