Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <X11/Xlib.h>
- #include <cairo.h>
- #include <cairo-xlib.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_BUTTONS 10
- #define MAX_TEXT_LENGTH 100
- #define MAX_NAME_LENGTH 50
- #define MAX_ID_LENGTH 50
- #define MAX_TITLE_LENGTH 100
- #define MAX_DATA_CUSTOM_LENGTH 100
- typedef struct {
- int x, y;
- int width, height;
- char text[MAX_TEXT_LENGTH];
- int text_size;
- double text_red, text_green, text_blue;
- double bg_red, bg_green, bg_blue;
- int disabled;
- char name[MAX_NAME_LENGTH];
- char id[MAX_ID_LENGTH];
- char title[MAX_TITLE_LENGTH];
- char data_custom[MAX_DATA_CUSTOM_LENGTH];
- } Button;
- typedef struct {
- cairo_t *cr;
- Display *display;
- Window win;
- Button buttons[MAX_BUTTONS];
- int button_count;
- } ButtonContext;
- // func to draw a button
- void draw_button(ButtonContext *ctx, Button *button) {
- cairo_set_source_rgb(ctx->cr, button->bg_red, button->bg_green, button->bg_blue);
- cairo_rectangle(ctx->cr, button->x, button->y, button->width, button->height);
- cairo_fill(ctx->cr);
- cairo_set_source_rgb(ctx->cr, button->text_red, button->text_green, button->text_blue);
- cairo_set_font_size(ctx->cr, button->text_size);
- cairo_move_to(ctx->cr, button->x + 10, button->y + button->height / 2);
- cairo_show_text(ctx->cr, button->text);
- }
- // func to redraw all buttons
- void redraw(ButtonContext *ctx) {
- cairo_set_source_rgb(ctx->cr, 1, 1, 1); // clear bg
- cairo_paint(ctx->cr);
- for (int i = 0; i < ctx->button_count; ++i) {
- draw_button(ctx, &ctx->buttons[i]);
- }
- cairo_surface_flush(cairo_get_target(ctx->cr));
- }
- // func to handle button clicks
- void handle_button_click(Button *button) {
- if (button->disabled) {
- printf("Button '%s' is disabled.\n", button->text);
- } else {
- printf("Button '%s' clicked!\n", button->text);
- }
- }
- // func to check if a click is within a button
- int is_click_within_button(int click_x, int click_y, Button *button) {
- return click_x >= button->x && click_x <= (button->x + button->width) &&
- click_y >= button->y && click_y <= (button->y + button->height);
- }
- int main() {
- Display *display = XOpenDisplay(NULL);
- if (!display) {
- fprintf(stderr, "failed to open display\n");
- return 1;
- }
- int screen = DefaultScreen(display);
- Window win = XCreateSimpleWindow(display, RootWindow(display, screen), 10, 10, 800, 600, 1,
- BlackPixel(display, screen), WhitePixel(display, screen));
- XSelectInput(display, win, ExposureMask | ButtonPressMask);
- XMapWindow(display, win);
- cairo_surface_t *surface = cairo_xlib_surface_create(display, win, DefaultVisual(display, 0), 800, 600);
- cairo_t *cr = cairo_create(surface);
- ButtonContext ctx = {cr, display, win};
- ctx.button_count = 2;
- // init first button
- strcpy(ctx.buttons[0].text, "First button");
- ctx.buttons[0].x = 50;
- ctx.buttons[0].y = 50;
- ctx.buttons[0].width = 100;
- ctx.buttons[0].height = 50;
- ctx.buttons[0].text_size = 20;
- ctx.buttons[0].text_red = 1.0;
- ctx.buttons[0].text_green = 1.0;
- ctx.buttons[0].text_blue = 1.0;
- ctx.buttons[0].bg_red = 0.0;
- ctx.buttons[0].bg_green = 0.5;
- ctx.buttons[0].bg_blue = 0.0;
- // Initialize second button
- strcpy(ctx.buttons[1].text, "Button 2");
- ctx.buttons[1].x = 200;
- ctx.buttons[1].y = 50;
- ctx.buttons[1].width = 100;
- ctx.buttons[1].height = 50;
- ctx.buttons[1].text_size = 20;
- ctx.buttons[1].text_red = 1.0;
- ctx.buttons[1].text_green = 1.0;
- ctx.buttons[1].text_blue = 1.0;
- ctx.buttons[1].bg_red = 0.0;
- ctx.buttons[1].bg_green = 0.0;
- ctx.buttons[1].bg_blue = 0.5;
- XEvent event;
- while (1) {
- XNextEvent(display, &event);
- if (event.type == Expose) {
- redraw(&ctx);
- } else if (event.type == ButtonPress) {
- // check click against each button
- for (int i = 0; i < ctx.button_count; ++i) {
- if (is_click_within_button(event.xbutton.x, event.xbutton.y, &ctx.buttons[i])) {
- handle_button_click(&ctx.buttons[i]);
- break; // stop after handling the click on the first matching button
- }
- }
- }
- }
- cairo_destroy(cr);
- cairo_surface_destroy(surface);
- XCloseDisplay(display);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement