From 37d5ef75306c45ca95550f4af40d9750c5b9cb88 Mon Sep 17 00:00:00 2001 From: Hannes Schulze Date: Mon, 6 May 2024 13:42:55 +0200 Subject: initial commit yup, i definitely wrote all of this in a single commit --- src/widgets/panel/panel-item-icon.c | 92 +++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/widgets/panel/panel-item-icon.c (limited to 'src/widgets/panel/panel-item-icon.c') diff --git a/src/widgets/panel/panel-item-icon.c b/src/widgets/panel/panel-item-icon.c new file mode 100644 index 0000000..327f027 --- /dev/null +++ b/src/widgets/panel/panel-item-icon.c @@ -0,0 +1,92 @@ +#include "widgets/panel/panel-item-icon.h" + +// +// FoobarPanelItemIcon: +// +// Simple item implementation displaying an icon. +// + +struct _FoobarPanelItemIcon +{ + FoobarPanelItem parent_instance; + GtkWidget* button; + FoobarPanelItemAction action; +}; + +static void foobar_panel_item_icon_class_init ( FoobarPanelItemIconClass* klass ); +static void foobar_panel_item_icon_init ( FoobarPanelItemIcon* self ); +static void foobar_panel_item_icon_handle_clicked( GtkButton* button, + gpointer userdata ); + +G_DEFINE_FINAL_TYPE( FoobarPanelItemIcon, foobar_panel_item_icon, FOOBAR_TYPE_PANEL_ITEM ) + +// --------------------------------------------------------------------------------------------------------------------- +// Panel Item Implementation +// --------------------------------------------------------------------------------------------------------------------- + +// +// Static initialization for icon panel items. +// +void foobar_panel_item_icon_class_init( FoobarPanelItemIconClass* klass ) +{ + (void)klass; +} + +// +// Instance initialization for icon panel items. +// +void foobar_panel_item_icon_init( FoobarPanelItemIcon* self ) +{ + self->button = gtk_button_new( ); + gtk_button_set_has_frame( GTK_BUTTON( self->button ), FALSE ); + gtk_widget_set_halign( self->button, GTK_ALIGN_CENTER ); + gtk_widget_set_valign( self->button, GTK_ALIGN_CENTER ); + g_signal_connect( + self->button, + "clicked", + G_CALLBACK( foobar_panel_item_icon_handle_clicked ), + self ); + + gtk_widget_add_css_class( GTK_WIDGET( self ), "icon" ); + foobar_panel_item_set_child( FOOBAR_PANEL_ITEM( self ), self->button ); +} + +// --------------------------------------------------------------------------------------------------------------------- +// Public API +// --------------------------------------------------------------------------------------------------------------------- + +// +// Create a new icon panel item with the given configuration. +// +FoobarPanelItem* foobar_panel_item_icon_new( FoobarPanelItemConfiguration const* config ) +{ + g_return_val_if_fail( config != NULL, NULL ); + g_return_val_if_fail( foobar_panel_item_configuration_get_kind( config ) == FOOBAR_PANEL_ITEM_KIND_ICON, NULL ); + + FoobarPanelItemIcon* self = g_object_new( FOOBAR_TYPE_PANEL_ITEM_ICON, NULL ); + self->action = foobar_panel_item_configuration_get_action( config ); + + gtk_widget_set_sensitive( self->button, self->action != FOOBAR_PANEL_ITEM_ACTION_NONE ); + gtk_button_set_icon_name( + GTK_BUTTON( self->button ), + foobar_panel_item_icon_configuration_get_icon_name( config ) ); + + return FOOBAR_PANEL_ITEM( self ); +} + +// --------------------------------------------------------------------------------------------------------------------- +// Signal Handlers +// --------------------------------------------------------------------------------------------------------------------- + +// +// Called when the button was clicked. +// +void foobar_panel_item_icon_handle_clicked( + GtkButton* button, + gpointer userdata ) +{ + (void)button; + FoobarPanelItemIcon* self = (FoobarPanelItemIcon*)userdata; + + foobar_panel_item_invoke_action( FOOBAR_PANEL_ITEM( self ), self->action ); +} \ No newline at end of file -- cgit v1.3.1