Debian Codegraph @

addons/native_dialog/gtk_msgbox.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * GTK native dialog implementation.
12 *
13 * See LICENSE.txt for copyright information.
14 */
15
16 #include <gtk/gtk.h>
17
18 #include "allegro5/allegro.h"
19 #include "allegro5/allegro_native_dialog.h"
20 #include "allegro5/internal/aintern_native_dialog.h"
21 #include "gtk_dialog.h"
22 #include "gtk_xgtk.h"
23
24
25 typedef struct {
26 ALLEGRO_DISPLAY *display;
27 ALLEGRO_NATIVE_DIALOG *dialog;
28 } Msg;
29
30
31 /* Note: the message box code cannot assume that Allegro is installed. */
32
33 static void msgbox_destroy(GtkWidget *w, gpointer data)
34 {
35 ALLEGRO_NATIVE_DIALOG *nd = data;
36 (void)w;
37
38 ASSERT(nd->async_queue);
39 g_async_queue_push(nd->async_queue, ACK_CLOSED);
40 }
41
42 static void msgbox_response(GtkDialog *dialog, gint response_id,
43 gpointer user_data)
44 {
45 ALLEGRO_NATIVE_DIALOG *nd = (void *)user_data;
46 (void)dialog;
47 switch (response_id) {
48 case GTK_RESPONSE_DELETE_EVENT:
49 nd->mb_pressed_button = 0;
50 break;
51 case GTK_RESPONSE_YES:
52 case GTK_RESPONSE_OK:
53 nd->mb_pressed_button = 1;
54 break;
55 case GTK_RESPONSE_NO:
56 case GTK_RESPONSE_CANCEL:
57 nd->mb_pressed_button = 2;
58 break;
59 default:
60 nd->mb_pressed_button = response_id;
61 }
62 }
63
64 /* [gtk thread] */
65 static gboolean create_native_message_box(gpointer data)
66 {
67 Msg *msg = data;
68 ALLEGRO_DISPLAY *display = msg->display;
69 ALLEGRO_NATIVE_DIALOG *fd = msg->dialog;
70 GtkWidget *window;
71
72 /* Create a new file selection widget */
73 GtkMessageType type = GTK_MESSAGE_INFO;
74 GtkButtonsType buttons = GTK_BUTTONS_OK;
75 if (fd->flags & ALLEGRO_MESSAGEBOX_YES_NO) type = GTK_MESSAGE_QUESTION;
76 if (fd->flags & ALLEGRO_MESSAGEBOX_QUESTION) type = GTK_MESSAGE_QUESTION;
77 if (fd->flags & ALLEGRO_MESSAGEBOX_WARN) type = GTK_MESSAGE_WARNING;
78 if (fd->flags & ALLEGRO_MESSAGEBOX_ERROR) type = GTK_MESSAGE_ERROR;
79 if (fd->flags & ALLEGRO_MESSAGEBOX_YES_NO) buttons = GTK_BUTTONS_YES_NO;
80 if (fd->flags & ALLEGRO_MESSAGEBOX_OK_CANCEL) buttons = GTK_BUTTONS_OK_CANCEL;
81 if (fd->mb_buttons) buttons = GTK_BUTTONS_NONE;
82
83 window = gtk_message_dialog_new(NULL, 0, type, buttons, "%s",
84 al_cstr(fd->mb_heading));
85 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(window), "%s",
86 al_cstr(fd->mb_text));
87
88 _al_gtk_make_transient(display, window);
89
90 if (fd->mb_buttons) {
91 int i = 1;
92 int pos = 0;
93 while (1) {
94 int next = al_ustr_find_chr(fd->mb_buttons, pos, '|');
95 int pos2 = next;
96 if (next == -1)
97 pos2 = al_ustr_size(fd->mb_buttons);
98 ALLEGRO_USTR_INFO info;
99 const ALLEGRO_USTR *button_text;
100 button_text = al_ref_ustr(&info, fd->mb_buttons, pos, pos2);
101 pos = pos2 + 1;
102 char buffer[256];
103 al_ustr_to_buffer(button_text, buffer, sizeof buffer);
104 gtk_dialog_add_button(GTK_DIALOG(window), buffer, i++);
105 if (next == -1)
106 break;
107 }
108 }
109
110 gtk_window_set_title(GTK_WINDOW(window), al_cstr(fd->title));
111
112 g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(msgbox_destroy), fd);
113
114 g_signal_connect(G_OBJECT(window), "response", G_CALLBACK(msgbox_response), fd);
115 g_signal_connect_swapped(G_OBJECT(window), "response", G_CALLBACK(gtk_widget_destroy), window);
116
117 gtk_widget_show(window);
118 return FALSE;
119 }
120
121 /* [user thread] */
122 int _al_show_native_message_box(ALLEGRO_DISPLAY *display,
123 ALLEGRO_NATIVE_DIALOG *fd)
124 {
125 Msg msg;
126
127 if (!_al_gtk_ensure_thread())
128 return 0; /* "cancelled" */
129
130 fd->async_queue = g_async_queue_new();
131
132 msg.display = display;
133 msg.dialog = fd;
134 g_timeout_add(0, create_native_message_box, &msg);
135
136 /* Wait for a signal that the window is closed. */
137 while (g_async_queue_pop(fd->async_queue) != ACK_CLOSED)
138 ;
139 g_async_queue_unref(fd->async_queue);
140 fd->async_queue = NULL;
141 return fd->mb_pressed_button;
142 }
143
144 /* vim: set sts=3 sw=3 et: */