Advertisement
Guest User

Untitled

a guest
Sep 25th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.46 KB | None | 0 0
  1. #include <X11/Xlib.h>
  2. #include <cairo.h>
  3. #include <cairo-xlib.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. #define MAX_BUTTONS 10
  9. #define MAX_TEXT_LENGTH 100
  10. #define MAX_NAME_LENGTH 50
  11. #define MAX_ID_LENGTH 50
  12. #define MAX_TITLE_LENGTH 100
  13. #define MAX_DATA_CUSTOM_LENGTH 100
  14.  
  15. typedef struct {
  16.     int x, y;
  17.     int width, height;
  18.     char text[MAX_TEXT_LENGTH];
  19.     int text_size;
  20.     double text_red, text_green, text_blue;
  21.     double bg_red, bg_green, bg_blue;
  22.     int disabled;
  23.     char name[MAX_NAME_LENGTH];
  24.     char id[MAX_ID_LENGTH];
  25.     char title[MAX_TITLE_LENGTH];
  26.     char data_custom[MAX_DATA_CUSTOM_LENGTH];
  27. } Button;
  28.  
  29. typedef struct {
  30.     cairo_t *cr;
  31.     Display *display;
  32.     Window win;
  33.     Button buttons[MAX_BUTTONS];
  34.     int button_count;
  35. } ButtonContext;
  36.  
  37. // func to draw a button
  38. void draw_button(ButtonContext *ctx, Button *button) {
  39.     cairo_set_source_rgb(ctx->cr, button->bg_red, button->bg_green, button->bg_blue);
  40.     cairo_rectangle(ctx->cr, button->x, button->y, button->width, button->height);
  41.     cairo_fill(ctx->cr);
  42.  
  43.     cairo_set_source_rgb(ctx->cr, button->text_red, button->text_green, button->text_blue);
  44.     cairo_set_font_size(ctx->cr, button->text_size);
  45.     cairo_move_to(ctx->cr, button->x + 10, button->y + button->height / 2);
  46.     cairo_show_text(ctx->cr, button->text);
  47. }
  48.  
  49. // func to redraw all buttons
  50. void redraw(ButtonContext *ctx) {
  51.     cairo_set_source_rgb(ctx->cr, 1, 1, 1); // clear bg
  52.     cairo_paint(ctx->cr);
  53.  
  54.     for (int i = 0; i < ctx->button_count; ++i) {
  55.         draw_button(ctx, &ctx->buttons[i]);
  56.     }
  57.  
  58.     cairo_surface_flush(cairo_get_target(ctx->cr));
  59. }
  60.  
  61. // func to handle button clicks
  62. void handle_button_click(Button *button) {
  63.     if (button->disabled) {
  64.         printf("Button '%s' is disabled.\n", button->text);
  65.     } else {
  66.         printf("Button '%s' clicked!\n", button->text);
  67.     }
  68. }
  69.  
  70. // func to check if a click is within a button
  71. int is_click_within_button(int click_x, int click_y, Button *button) {
  72.     return click_x >= button->x && click_x <= (button->x + button->width) &&
  73.            click_y >= button->y && click_y <= (button->y + button->height);
  74. }
  75.  
  76. int main() {
  77.     Display *display = XOpenDisplay(NULL);
  78.     if (!display) {
  79.         fprintf(stderr, "failed to open display\n");
  80.         return 1;
  81.     }
  82.  
  83.     int screen = DefaultScreen(display);
  84.     Window win = XCreateSimpleWindow(display, RootWindow(display, screen), 10, 10, 800, 600, 1,
  85.                                      BlackPixel(display, screen), WhitePixel(display, screen));
  86.     XSelectInput(display, win, ExposureMask | ButtonPressMask);
  87.     XMapWindow(display, win);
  88.  
  89.     cairo_surface_t *surface = cairo_xlib_surface_create(display, win, DefaultVisual(display, 0), 800, 600);
  90.     cairo_t *cr = cairo_create(surface);
  91.  
  92.     ButtonContext ctx = {cr, display, win};
  93.     ctx.button_count = 2;
  94.  
  95.     // init first button
  96.     strcpy(ctx.buttons[0].text, "First button");
  97.     ctx.buttons[0].x = 50;
  98.     ctx.buttons[0].y = 50;
  99.     ctx.buttons[0].width = 100;
  100.     ctx.buttons[0].height = 50;
  101.     ctx.buttons[0].text_size = 20;
  102.     ctx.buttons[0].text_red = 1.0;
  103.     ctx.buttons[0].text_green = 1.0;
  104.     ctx.buttons[0].text_blue = 1.0;
  105.     ctx.buttons[0].bg_red = 0.0;
  106.     ctx.buttons[0].bg_green = 0.5;
  107.     ctx.buttons[0].bg_blue = 0.0;
  108.  
  109.     // Initialize second button
  110.     strcpy(ctx.buttons[1].text, "Button 2");
  111.     ctx.buttons[1].x = 200;
  112.     ctx.buttons[1].y = 50;
  113.     ctx.buttons[1].width = 100;
  114.     ctx.buttons[1].height = 50;
  115.     ctx.buttons[1].text_size = 20;
  116.     ctx.buttons[1].text_red = 1.0;
  117.     ctx.buttons[1].text_green = 1.0;
  118.     ctx.buttons[1].text_blue = 1.0;
  119.     ctx.buttons[1].bg_red = 0.0;
  120.     ctx.buttons[1].bg_green = 0.0;
  121.     ctx.buttons[1].bg_blue = 0.5;
  122.  
  123.     XEvent event;
  124.     while (1) {
  125.         XNextEvent(display, &event);
  126.         if (event.type == Expose) {
  127.             redraw(&ctx);
  128.         } else if (event.type == ButtonPress) {
  129.             // check click against each button
  130.             for (int i = 0; i < ctx.button_count; ++i) {
  131.                 if (is_click_within_button(event.xbutton.x, event.xbutton.y, &ctx.buttons[i])) {
  132.                     handle_button_click(&ctx.buttons[i]);
  133.                     break; // stop after handling the click on the first matching button
  134.                 }
  135.             }
  136.         }
  137.     }
  138.  
  139.     cairo_destroy(cr);
  140.     cairo_surface_destroy(surface);
  141.     XCloseDisplay(display);
  142.  
  143.     return 0;
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement