{"id":2277,"date":"2015-10-29T21:19:32","date_gmt":"2015-10-29T21:19:32","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2277"},"modified":"2020-03-27T17:52:48","modified_gmt":"2020-03-27T17:52:48","slug":"eventbox-image-button-general","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/gui-programming-in-c\/gtk\/button-eventbox-image\/eventbox-image-button-general","title":{"rendered":".EventBox Image Button General"},"content":{"rendered":"\n<p>Using an event box with an image in it is a nice and simple way to make a button that will do exactly what you want &#8211; display an image and&nbsp;trigger an event when pressed and released. &nbsp;Its advantage over GtbButton is that is just does that without adding a highlight effect and border. &nbsp;It&#8217;s perfectly simple.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Create The Button<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>GtkWidget *image_button_down;\nGtkWidget *event_box_button_down;\n\t\n\t\/\/----- EVENT BOX BASED IMAGE BUTTON -----\n\timage_button_down = gtk_image_new_from_file(\"\/home\/pi\/projects\/ui\/button_down_a.png\");\n\tevent_box_button_down = gtk_event_box_new();\n\tgtk_container_add(GTK_CONTAINER(event_box_button_down), image_button_down);\n\t\/\/gtk_widget_set_size_request(event_box_button_down, 68, 68);\t\t\t\/\/Will be image size by default, but you can force the widget to be either larger or smaller than it normally would be using this\n\tgtk_fixed_put(GTK_FIXED(fixed), event_box_button_down, 460, 270);\t\t\/\/x, y from top left\t\n\t\n\t\/\/----- SET EVENTS TO BE CALLED ----- \n\tg_signal_connect(G_OBJECT(event_box_button_down), \"button_press_event\", G_CALLBACK(event_button_press_event), NULL);\t\t\/\/(\"button_press_event\" must not be changed)\n\tg_signal_connect(G_OBJECT(event_box_button_down), \"button_release_event\", G_CALLBACK(event_button_release_event), NULL);\t\/\/Include if you want the release event (\"button_release_event\" must not be changed)\t<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Button Events<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/***********************************************\n\/\/***********************************************\n\/\/********** A BUTTON HAS BEEN PRESSED **********\n\/\/***********************************************\n\/\/***********************************************\ngboolean event_button_press_event (GtkWidget *widget, GdkEventButton *event, gpointer data)\n{\n\tif (widget == event_box_button_down)\n\t{\n\t\tgtk_image_set_from_file(GTK_IMAGE(image_button_down), \"\/home\/pi\/projects\/ui\/button_down_b.png\");\n\t}\n\n\t\/\/If you want the coordinates of the touch event:\n\tint ClickCoordX;\n\tint ClickCoordY;\n\tClickCoordX = event->x;\n\tClickCoordY = event->y;\n\n\n\treturn FALSE;\t\t\t\t\t\t\t\/\/Return false so event will be called again\n}\n\n\n\/\/************************************************\n\/\/************************************************\n\/\/********** A BUTTON HAS BEEN RELEASED **********\n\/\/************************************************\n\/\/************************************************\ngboolean event_button_release_event (GtkWidget *widget, GdkEventButton *event, gpointer data)\n{\n\tif (widget == event_box_button_down)\n\t{\n\t\tgtk_image_set_from_file(GTK_IMAGE(image_button_down), \"\/home\/pi\/projects\/ui\/button_down_a.png\");\n\n\t}\n\t\n\treturn FALSE;\t\t\t\t\t\t\t\/\/Return false so event will be called again\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Delete a button<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\t\tgtk_widget_destroy(imgButtonTimerUp);\n\t\t\timgButtonTimerUp = NULL;\n\t\t\tgtk_widget_destroy(ebButtonTimerUp);\n\t\t\tebButtonTimerUp = NULL;<\/code><\/pre>\n\n\n\n<p>When a widget is destroyed, it will break any references it holds to other objects. &nbsp;You can safely used destroy on a widget that hasn&#8217;t actaully been displayed yet. &nbsp;You can re-create and place the widget again later after it has previously been destroyed, you&#8217;re just removing its current usage.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Detecting A Touch Anywhere On The Screen<\/h4>\n\n\n\n<p>Place your background image in an eventbox&nbsp;just as above. &nbsp;Any event box buttons&nbsp;you create over the top of it will generate their own touch events when pressed, but if you touch anywhere else the background event box will generate the touch event.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using an event box with an image in it is a nice and simple way to make a button that will do exactly what you want &#8211; display an image and&nbsp;trigger an event when pressed and released. &nbsp;Its advantage over GtbButton is that is just does that without adding a highlight effect and border. &nbsp;It&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[107],"tags":[],"class_list":["post-2277","post","type-post","status-publish","format-standard","hentry","category-button-eventbox-image"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=2277"}],"version-history":[{"count":10,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2277\/revisions"}],"predecessor-version":[{"id":3164,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2277\/revisions\/3164"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}