|
| 1 | +/* ______ ___ ___ |
| 2 | + * /\ _ \ /\_ \ /\_ \ |
| 3 | + * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 | + * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 | + * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 | + * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 | + * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 | + * /\____/ |
| 9 | + * \_/__/ |
| 10 | + * |
| 11 | + * System drag&drop example. |
| 12 | + * |
| 13 | + * This example shows how an Allegro window can receive pictures |
| 14 | + * (and text) from other applications. |
| 15 | + * |
| 16 | + * See readme.txt for copyright information. |
| 17 | + */ |
| 18 | +#define ALLEGRO_UNSTABLE |
| 19 | +#include <allegro5/allegro.h> |
| 20 | +#include <allegro5/allegro_font.h> |
| 21 | +#include <allegro5/allegro_ttf.h> |
| 22 | +#include <allegro5/allegro_color.h> |
| 23 | +#include <allegro5/allegro_image.h> |
| 24 | +#include <allegro5/allegro_primitives.h> |
| 25 | + |
| 26 | +#include "common.c" |
| 27 | + |
| 28 | +// We only accept up to 25 lines of text or files at a time. |
| 29 | +typedef struct Cell { |
| 30 | + char *rows[25]; |
| 31 | + ALLEGRO_BITMAP *bitmap; |
| 32 | +} Cell; |
| 33 | + |
| 34 | +int main(int argc, char **argv) |
| 35 | +{ |
| 36 | + ALLEGRO_DISPLAY *display; |
| 37 | + ALLEGRO_TIMER *timer; |
| 38 | + ALLEGRO_EVENT_QUEUE *queue; |
| 39 | + ALLEGRO_FONT *font; |
| 40 | + bool done = false; |
| 41 | + bool redraw = true; |
| 42 | + int xdiv = 3; |
| 43 | + int ydiv = 4; |
| 44 | + Cell grid[xdiv * ydiv]; |
| 45 | + int drop_x = -1; |
| 46 | + int drop_y = -1; |
| 47 | + |
| 48 | + (void)argc; |
| 49 | + (void)argv; |
| 50 | + |
| 51 | + // Initialize everything to NULL |
| 52 | + for (int i = 0; i < xdiv * ydiv; i++) { |
| 53 | + grid[i].rows[0] = strdup("drop file or text here"); |
| 54 | + for (int r = 1; r < 25; r++) grid[i].rows[r] = NULL; |
| 55 | + grid[i].bitmap = NULL; |
| 56 | + } |
| 57 | + |
| 58 | + if (!al_init()) { |
| 59 | + abort_example("Failed to init Allegro.\n"); |
| 60 | + } |
| 61 | + al_init_image_addon(); |
| 62 | + al_init_primitives_addon(); |
| 63 | + al_init_font_addon(); |
| 64 | + al_init_ttf_addon(); |
| 65 | + init_platform_specific(); |
| 66 | + ALLEGRO_MONITOR_INFO info; |
| 67 | + al_get_monitor_info(0, &info); |
| 68 | + // Make the window half as wide as the desktop and 4:3 aspect |
| 69 | + int w = (info.x2 - info.x1) / 2; |
| 70 | + int h = w * 3 / 4; |
| 71 | + |
| 72 | + // Turn on drag/drop support |
| 73 | + al_set_new_display_flags(ALLEGRO_DRAG_AND_DROP); |
| 74 | + display = al_create_display(w, h); |
| 75 | + if (!display) { |
| 76 | + abort_example("Error creating display.\n"); |
| 77 | + } |
| 78 | + |
| 79 | + if (!al_install_keyboard()) { |
| 80 | + abort_example("Error installing keyboard.\n"); |
| 81 | + } |
| 82 | + |
| 83 | + font = al_load_font("data/DejaVuSans.ttf", 20, 0); |
| 84 | + if (!font) { |
| 85 | + abort_example("Could not load font.\n"); |
| 86 | + } |
| 87 | + |
| 88 | + timer = al_create_timer(1 / 60.0); |
| 89 | + |
| 90 | + queue = al_create_event_queue(); |
| 91 | + al_register_event_source(queue, al_get_keyboard_event_source()); |
| 92 | + al_register_event_source(queue, al_get_timer_event_source(timer)); |
| 93 | + // drag&drop events will come from the display, if enabled |
| 94 | + al_register_event_source(queue, al_get_display_event_source(display)); |
| 95 | + |
| 96 | + al_start_timer(timer); |
| 97 | + |
| 98 | + while (!done) { |
| 99 | + ALLEGRO_EVENT event; |
| 100 | + |
| 101 | + int fh = al_get_font_line_height(font); |
| 102 | + |
| 103 | + if (redraw && al_is_event_queue_empty(queue)) { |
| 104 | + // draw a grid with the dropped text rows or files |
| 105 | + ALLEGRO_COLOR c1 = al_color_name("gainsboro"); |
| 106 | + ALLEGRO_COLOR c2 = al_color_name("orange"); |
| 107 | + al_clear_to_color(al_map_rgb_f(0, 0, 0)); |
| 108 | + for (int y = 0; y < ydiv; y++) { |
| 109 | + for (int x = 0; x < xdiv; x++) { |
| 110 | + int gx = x * w / xdiv; |
| 111 | + int gy = y * h / ydiv; |
| 112 | + if (grid[x + y * xdiv].bitmap) { |
| 113 | + ALLEGRO_BITMAP *bmp = grid[x + y * xdiv].bitmap; |
| 114 | + float bw = al_get_bitmap_width(bmp); |
| 115 | + float bh = al_get_bitmap_height(bmp); |
| 116 | + float s = w / xdiv / bw; |
| 117 | + if (s > (h / ydiv - fh) / bh) s = (h / ydiv - fh) / bh; |
| 118 | + al_draw_scaled_bitmap(bmp, 0, 0, bw, bh, gx, gy + fh, bw * s, bh * s, 0); |
| 119 | + } |
| 120 | + for (int r = 0; r < 25; r++) { |
| 121 | + if (!grid[x + y * xdiv].rows[r]) break; |
| 122 | + al_draw_textf(font, c1, gx, gy + r * fh, |
| 123 | + 0, "%s", grid[x + y * xdiv].rows[r]); |
| 124 | + } |
| 125 | + if (drop_x >= gx && drop_x < gx + w / xdiv && |
| 126 | + drop_y >= gy && drop_y < gy + h / ydiv) { |
| 127 | + al_draw_rectangle(gx, gy, gx + w / xdiv, gy + h / ydiv, c2, 2); |
| 128 | + } |
| 129 | + else { |
| 130 | + al_draw_rectangle(gx, gy, gx + w / xdiv, gy + h / ydiv, c1, 0); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + al_flip_display(); |
| 135 | + redraw = false; |
| 136 | + } |
| 137 | + |
| 138 | + al_wait_for_event(queue, &event); |
| 139 | + switch (event.type) { |
| 140 | + case ALLEGRO_EVENT_KEY_DOWN: |
| 141 | + if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { |
| 142 | + done = true; |
| 143 | + } |
| 144 | + break; |
| 145 | + |
| 146 | + case ALLEGRO_EVENT_DISPLAY_CLOSE: |
| 147 | + done = true; |
| 148 | + break; |
| 149 | + |
| 150 | + case ALLEGRO_EVENT_TIMER: |
| 151 | + redraw = true; |
| 152 | + break; |
| 153 | + |
| 154 | + case ALLEGRO_EVENT_DROP: |
| 155 | + drop_x = event.drop.x; |
| 156 | + drop_y = event.drop.y; |
| 157 | + int col = drop_x * xdiv / w; |
| 158 | + int row = drop_y * ydiv / h; |
| 159 | + int i = col + row * xdiv; |
| 160 | + if (event.drop.text) { |
| 161 | + if (event.drop.row == 0) { |
| 162 | + // clear the previous contents of the cell |
| 163 | + for (int r = 0; r < 25; r++) { |
| 164 | + if (grid[i].rows[r]) { |
| 165 | + al_free(grid[i].rows[r]); |
| 166 | + grid[i].rows[r] = NULL; |
| 167 | + } |
| 168 | + } |
| 169 | + // insert the first row/file |
| 170 | + grid[i].rows[0] = event.drop.text; |
| 171 | + if (event.drop.is_file) { |
| 172 | + // if a file, try and load it as a bitmap to display |
| 173 | + if (grid[i].bitmap) al_destroy_bitmap(grid[i].bitmap); |
| 174 | + grid[i].bitmap = al_load_bitmap(grid[i].rows[0]); |
| 175 | + } |
| 176 | + } |
| 177 | + else { |
| 178 | + if (event.drop.row < 25) { |
| 179 | + grid[i].rows[event.drop.row] = event.drop.text; |
| 180 | + } |
| 181 | + else { |
| 182 | + al_free(event.drop.text); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + if (event.drop.is_complete) { |
| 187 | + // stop highlighting a cell when the drop is completed |
| 188 | + drop_x = -1; |
| 189 | + drop_y = -1; |
| 190 | + } |
| 191 | + break; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + al_destroy_font(font); |
| 196 | + |
| 197 | + return 0; |
| 198 | +} |
| 199 | + |
| 200 | +/* vim: set sts=3 sw=3 et: */ |
0 commit comments