1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 2 -*- */ |
2 | /* GTK - The GIMP Toolkit |
3 | * Copyright (C) 2000 Red Hat, Inc. |
4 | * |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Library General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2 of the License, or (at your option) any later version. |
9 | * |
10 | * This library is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Library General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU Library General Public |
16 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. |
17 | */ |
18 | |
19 | /* |
20 | * Modified by the GTK+ Team and others 1997-2003. See the AUTHORS |
21 | * file for a list of people on the GTK+ Team. See the ChangeLog |
22 | * files for a list of changes. These files are distributed with |
23 | * GTK+ at ftp://ftp.gtk.org/pub/gtk/. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | |
28 | #include "gtkmessagedialog.h" |
29 | |
30 | #include "gtkbox.h" |
31 | #include "gtkbuildable.h" |
32 | #include "gtkdialogprivate.h" |
33 | #include "gtkintl.h" |
34 | #include "gtklabel.h" |
35 | #include "gtkprivate.h" |
36 | #include "gtktypebuiltins.h" |
37 | |
38 | #include <string.h> |
39 | |
40 | /** |
41 | * GtkMessageDialog: |
42 | * |
43 | * `GtkMessageDialog` presents a dialog with some message text. |
44 | * |
45 | *  |
46 | * |
47 | * It’s simply a convenience widget; you could construct the equivalent of |
48 | * `GtkMessageDialog` from `GtkDialog` without too much effort, but |
49 | * `GtkMessageDialog` saves typing. |
50 | * |
51 | * The easiest way to do a modal message dialog is to use the %GTK_DIALOG_MODAL |
52 | * flag, which will call [method@Gtk.Window.set_modal] internally. The dialog will |
53 | * prevent interaction with the parent window until it's hidden or destroyed. |
54 | * You can use the [signal@Gtk.Dialog::response] signal to know when the user |
55 | * dismissed the dialog. |
56 | * |
57 | * An example for using a modal dialog: |
58 | * ```c |
59 | * GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL; |
60 | * dialog = gtk_message_dialog_new (parent_window, |
61 | * flags, |
62 | * GTK_MESSAGE_ERROR, |
63 | * GTK_BUTTONS_CLOSE, |
64 | * "Error reading “%s”: %s", |
65 | * filename, |
66 | * g_strerror (errno)); |
67 | * // Destroy the dialog when the user responds to it |
68 | * // (e.g. clicks a button) |
69 | * |
70 | * g_signal_connect (dialog, "response", |
71 | * G_CALLBACK (gtk_window_destroy), |
72 | * NULL); |
73 | * ``` |
74 | * |
75 | * You might do a non-modal `GtkMessageDialog` simply by omitting the |
76 | * %GTK_DIALOG_MODAL flag: |
77 | * |
78 | * ```c |
79 | * GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT; |
80 | * dialog = gtk_message_dialog_new (parent_window, |
81 | * flags, |
82 | * GTK_MESSAGE_ERROR, |
83 | * GTK_BUTTONS_CLOSE, |
84 | * "Error reading “%s”: %s", |
85 | * filename, |
86 | * g_strerror (errno)); |
87 | * |
88 | * // Destroy the dialog when the user responds to it |
89 | * // (e.g. clicks a button) |
90 | * g_signal_connect (dialog, "response", |
91 | * G_CALLBACK (gtk_window_destroy), |
92 | * NULL); |
93 | * ``` |
94 | * |
95 | * # GtkMessageDialog as GtkBuildable |
96 | * |
97 | * The `GtkMessageDialog` implementation of the `GtkBuildable` interface exposes |
98 | * the message area as an internal child with the name “message_area”. |
99 | */ |
100 | |
101 | typedef struct |
102 | { |
103 | GtkWidget *label; |
104 | GtkWidget *message_area; /* vbox for the primary and secondary labels, and any extra content from the caller */ |
105 | GtkWidget *secondary_label; |
106 | |
107 | guint has_primary_markup : 1; |
108 | guint has_secondary_text : 1; |
109 | guint message_type : 3; |
110 | } GtkMessageDialogPrivate; |
111 | |
112 | struct _GtkMessageDialogClass |
113 | { |
114 | GtkDialogClass parent_class; |
115 | }; |
116 | |
117 | enum { |
118 | PROP_0, |
119 | PROP_MESSAGE_TYPE, |
120 | PROP_BUTTONS, |
121 | PROP_TEXT, |
122 | PROP_USE_MARKUP, |
123 | PROP_SECONDARY_TEXT, |
124 | PROP_SECONDARY_USE_MARKUP, |
125 | PROP_IMAGE, |
126 | PROP_MESSAGE_AREA |
127 | }; |
128 | |
129 | G_DEFINE_TYPE_WITH_PRIVATE (GtkMessageDialog, gtk_message_dialog, GTK_TYPE_DIALOG) |
130 | |
131 | static void |
132 | setup_type (GtkMessageDialog *dialog, |
133 | GtkMessageType type) |
134 | { |
135 | GtkMessageDialogPrivate *priv = gtk_message_dialog_get_instance_private (self: dialog); |
136 | |
137 | if (priv->message_type == type) |
138 | return; |
139 | |
140 | priv->message_type = type; |
141 | |
142 | g_object_notify (G_OBJECT (dialog), property_name: "message-type" ); |
143 | } |
144 | |
145 | static void |
146 | gtk_message_dialog_add_buttons (GtkMessageDialog *message_dialog, |
147 | GtkButtonsType buttons) |
148 | { |
149 | GtkDialog* dialog = GTK_DIALOG (message_dialog); |
150 | |
151 | switch (buttons) |
152 | { |
153 | case GTK_BUTTONS_NONE: |
154 | /* nothing */ |
155 | break; |
156 | |
157 | case GTK_BUTTONS_OK: |
158 | gtk_dialog_add_button (dialog, _("_OK" ), response_id: GTK_RESPONSE_OK); |
159 | break; |
160 | |
161 | case GTK_BUTTONS_CLOSE: |
162 | gtk_dialog_add_button (dialog, _("_Close" ), response_id: GTK_RESPONSE_CLOSE); |
163 | break; |
164 | |
165 | case GTK_BUTTONS_CANCEL: |
166 | gtk_dialog_add_button (dialog, _("_Cancel" ), response_id: GTK_RESPONSE_CANCEL); |
167 | break; |
168 | |
169 | case GTK_BUTTONS_YES_NO: |
170 | gtk_dialog_add_button (dialog, _("_No" ), response_id: GTK_RESPONSE_NO); |
171 | gtk_dialog_add_button (dialog, _("_Yes" ), response_id: GTK_RESPONSE_YES); |
172 | break; |
173 | |
174 | case GTK_BUTTONS_OK_CANCEL: |
175 | gtk_dialog_add_button (dialog, _("_Cancel" ), response_id: GTK_RESPONSE_CANCEL); |
176 | gtk_dialog_add_button (dialog, _("_OK" ), response_id: GTK_RESPONSE_OK); |
177 | break; |
178 | |
179 | default: |
180 | g_warning ("Unknown GtkButtonsType" ); |
181 | break; |
182 | } |
183 | |
184 | g_object_notify (G_OBJECT (message_dialog), property_name: "buttons" ); |
185 | } |
186 | |
187 | static void |
188 | gtk_message_dialog_set_property (GObject *object, |
189 | guint prop_id, |
190 | const GValue *value, |
191 | GParamSpec *pspec) |
192 | { |
193 | GtkMessageDialog *dialog = GTK_MESSAGE_DIALOG (object); |
194 | GtkMessageDialogPrivate *priv = gtk_message_dialog_get_instance_private (self: dialog); |
195 | |
196 | switch (prop_id) |
197 | { |
198 | case PROP_MESSAGE_TYPE: |
199 | setup_type (dialog, type: g_value_get_enum (value)); |
200 | break; |
201 | case PROP_BUTTONS: |
202 | gtk_message_dialog_add_buttons (message_dialog: dialog, buttons: g_value_get_enum (value)); |
203 | break; |
204 | case PROP_TEXT: |
205 | if (priv->has_primary_markup) |
206 | gtk_label_set_markup (GTK_LABEL (priv->label), str: g_value_get_string (value)); |
207 | else |
208 | gtk_label_set_text (GTK_LABEL (priv->label), str: g_value_get_string (value)); |
209 | break; |
210 | case PROP_USE_MARKUP: |
211 | if (priv->has_primary_markup != g_value_get_boolean (value)) |
212 | { |
213 | priv->has_primary_markup = g_value_get_boolean (value); |
214 | gtk_label_set_use_markup (GTK_LABEL (priv->label), setting: priv->has_primary_markup); |
215 | g_object_notify_by_pspec (object, pspec); |
216 | } |
217 | break; |
218 | case PROP_SECONDARY_TEXT: |
219 | { |
220 | const char *txt = g_value_get_string (value); |
221 | |
222 | if (gtk_label_get_use_markup (GTK_LABEL (priv->secondary_label))) |
223 | gtk_label_set_markup (GTK_LABEL (priv->secondary_label), str: txt); |
224 | else |
225 | gtk_label_set_text (GTK_LABEL (priv->secondary_label), str: txt); |
226 | |
227 | if (txt) |
228 | { |
229 | priv->has_secondary_text = TRUE; |
230 | gtk_widget_add_css_class (widget: priv->label, css_class: "title" ); |
231 | gtk_widget_show (widget: priv->secondary_label); |
232 | } |
233 | else |
234 | { |
235 | priv->has_secondary_text = FALSE; |
236 | gtk_widget_remove_css_class (widget: priv->label, css_class: "title" ); |
237 | gtk_widget_hide (widget: priv->secondary_label); |
238 | } |
239 | } |
240 | break; |
241 | case PROP_SECONDARY_USE_MARKUP: |
242 | if (gtk_label_get_use_markup (GTK_LABEL (priv->secondary_label)) != g_value_get_boolean (value)) |
243 | { |
244 | gtk_label_set_use_markup (GTK_LABEL (priv->secondary_label), setting: g_value_get_boolean (value)); |
245 | g_object_notify_by_pspec (object, pspec); |
246 | } |
247 | break; |
248 | |
249 | default: |
250 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
251 | break; |
252 | } |
253 | } |
254 | |
255 | static void |
256 | gtk_message_dialog_get_property (GObject *object, |
257 | guint prop_id, |
258 | GValue *value, |
259 | GParamSpec *pspec) |
260 | { |
261 | GtkMessageDialog *dialog = GTK_MESSAGE_DIALOG (object); |
262 | GtkMessageDialogPrivate *priv = gtk_message_dialog_get_instance_private (self: dialog); |
263 | |
264 | switch (prop_id) |
265 | { |
266 | case PROP_MESSAGE_TYPE: |
267 | g_value_set_enum (value, v_enum: (GtkMessageType) priv->message_type); |
268 | break; |
269 | case PROP_TEXT: |
270 | g_value_set_string (value, v_string: gtk_label_get_label (GTK_LABEL (priv->label))); |
271 | break; |
272 | case PROP_USE_MARKUP: |
273 | g_value_set_boolean (value, v_boolean: priv->has_primary_markup); |
274 | break; |
275 | case PROP_SECONDARY_TEXT: |
276 | if (priv->has_secondary_text) |
277 | g_value_set_string (value, |
278 | v_string: gtk_label_get_label (GTK_LABEL (priv->secondary_label))); |
279 | else |
280 | g_value_set_string (value, NULL); |
281 | break; |
282 | case PROP_SECONDARY_USE_MARKUP: |
283 | if (priv->has_secondary_text) |
284 | g_value_set_boolean (value, |
285 | v_boolean: gtk_label_get_use_markup (GTK_LABEL (priv->secondary_label))); |
286 | else |
287 | g_value_set_boolean (value, FALSE); |
288 | break; |
289 | case PROP_MESSAGE_AREA: |
290 | g_value_set_object (value, v_object: priv->message_area); |
291 | break; |
292 | default: |
293 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
294 | break; |
295 | } |
296 | } |
297 | |
298 | static void |
299 | update_title (GObject *dialog, |
300 | GParamSpec *pspec, |
301 | GtkWidget *label) |
302 | { |
303 | const char *title; |
304 | |
305 | title = gtk_window_get_title (GTK_WINDOW (dialog)); |
306 | gtk_label_set_label (GTK_LABEL (label), str: title); |
307 | gtk_widget_set_visible (widget: label, visible: title && title[0]); |
308 | } |
309 | |
310 | static void |
311 | gtk_message_dialog_constructed (GObject *object) |
312 | { |
313 | GtkMessageDialog *dialog = GTK_MESSAGE_DIALOG (object); |
314 | gboolean ; |
315 | |
316 | G_OBJECT_CLASS (gtk_message_dialog_parent_class)->constructed (object); |
317 | |
318 | g_object_get (object: gtk_widget_get_settings (GTK_WIDGET (dialog)), |
319 | first_property_name: "gtk-dialogs-use-header" , &use_header, |
320 | NULL); |
321 | |
322 | if (use_header) |
323 | { |
324 | GtkWidget *box; |
325 | GtkWidget *label; |
326 | |
327 | box = gtk_box_new (orientation: GTK_ORIENTATION_HORIZONTAL, spacing: 0); |
328 | gtk_widget_show (widget: box); |
329 | gtk_widget_set_size_request (widget: box, width: -1, height: 16); |
330 | label = gtk_label_new (str: "" ); |
331 | gtk_widget_hide (widget: label); |
332 | gtk_widget_set_margin_top (widget: label, margin: 6); |
333 | gtk_widget_set_margin_bottom (widget: label, margin: 6); |
334 | gtk_widget_set_halign (widget: label, align: GTK_ALIGN_CENTER); |
335 | gtk_widget_set_hexpand (widget: label, TRUE); |
336 | gtk_widget_add_css_class (widget: label, css_class: "title" ); |
337 | gtk_box_append (GTK_BOX (box), child: label); |
338 | g_signal_connect_object (instance: dialog, detailed_signal: "notify::title" , G_CALLBACK (update_title), gobject: label, connect_flags: 0); |
339 | |
340 | gtk_window_set_titlebar (GTK_WINDOW (dialog), titlebar: box); |
341 | } |
342 | } |
343 | |
344 | static void |
345 | gtk_message_dialog_class_init (GtkMessageDialogClass *class) |
346 | { |
347 | GtkWidgetClass *widget_class; |
348 | GObjectClass *gobject_class; |
349 | |
350 | widget_class = GTK_WIDGET_CLASS (class); |
351 | gobject_class = G_OBJECT_CLASS (class); |
352 | |
353 | gobject_class->constructed = gtk_message_dialog_constructed; |
354 | gobject_class->set_property = gtk_message_dialog_set_property; |
355 | gobject_class->get_property = gtk_message_dialog_get_property; |
356 | |
357 | /** |
358 | * GtkMessageDialog:message-type: |
359 | * |
360 | * The type of the message. |
361 | */ |
362 | g_object_class_install_property (oclass: gobject_class, |
363 | property_id: PROP_MESSAGE_TYPE, |
364 | pspec: g_param_spec_enum (name: "message-type" , |
365 | P_("Message Type" ), |
366 | P_("The type of message" ), |
367 | enum_type: GTK_TYPE_MESSAGE_TYPE, |
368 | default_value: GTK_MESSAGE_INFO, |
369 | GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_EXPLICIT_NOTIFY)); |
370 | g_object_class_install_property (oclass: gobject_class, |
371 | property_id: PROP_BUTTONS, |
372 | pspec: g_param_spec_enum (name: "buttons" , |
373 | P_("Message Buttons" ), |
374 | P_("The buttons shown in the message dialog" ), |
375 | enum_type: GTK_TYPE_BUTTONS_TYPE, |
376 | default_value: GTK_BUTTONS_NONE, |
377 | GTK_PARAM_WRITABLE|G_PARAM_CONSTRUCT_ONLY)); |
378 | /** |
379 | * GtkMessageDialog:text: |
380 | * |
381 | * The primary text of the message dialog. |
382 | * |
383 | * If the dialog has a secondary text, this will appear as the title. |
384 | */ |
385 | g_object_class_install_property (oclass: gobject_class, |
386 | property_id: PROP_TEXT, |
387 | pspec: g_param_spec_string (name: "text" , |
388 | P_("Text" ), |
389 | P_("The primary text of the message dialog" ), |
390 | default_value: "" , |
391 | GTK_PARAM_READWRITE)); |
392 | /** |
393 | * GtkMessageDialog:use-markup: |
394 | * |
395 | * %TRUE if the primary text of the dialog includes Pango markup. |
396 | * |
397 | * See [func@Pango.parse_markup]. |
398 | */ |
399 | g_object_class_install_property (oclass: gobject_class, |
400 | property_id: PROP_USE_MARKUP, |
401 | pspec: g_param_spec_boolean (name: "use-markup" , |
402 | P_("Use Markup" ), |
403 | P_("The primary text of the title includes Pango markup." ), |
404 | FALSE, |
405 | GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY)); |
406 | /** |
407 | * GtkMessageDialog:secondary-text: |
408 | * |
409 | * The secondary text of the message dialog. |
410 | */ |
411 | g_object_class_install_property (oclass: gobject_class, |
412 | property_id: PROP_SECONDARY_TEXT, |
413 | pspec: g_param_spec_string (name: "secondary-text" , |
414 | P_("Secondary Text" ), |
415 | P_("The secondary text of the message dialog" ), |
416 | NULL, |
417 | GTK_PARAM_READWRITE)); |
418 | /** |
419 | * GtkMessageDialog:secondary-use-markup: |
420 | * |
421 | * %TRUE if the secondary text of the dialog includes Pango markup. |
422 | * |
423 | * See [func@Pango.parse_markup]. |
424 | */ |
425 | g_object_class_install_property (oclass: gobject_class, |
426 | property_id: PROP_SECONDARY_USE_MARKUP, |
427 | pspec: g_param_spec_boolean (name: "secondary-use-markup" , |
428 | P_("Use Markup in secondary" ), |
429 | P_("The secondary text includes Pango markup." ), |
430 | FALSE, |
431 | GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY)); |
432 | /** |
433 | * GtkMessageDialog:message-area: |
434 | * |
435 | * The `GtkBox` that corresponds to the message area of this dialog. |
436 | * |
437 | * See [method@Gtk.MessageDialog.get_message_area] for a detailed |
438 | * description of this area. |
439 | */ |
440 | g_object_class_install_property (oclass: gobject_class, |
441 | property_id: PROP_MESSAGE_AREA, |
442 | pspec: g_param_spec_object (name: "message-area" , |
443 | P_("Message area" ), |
444 | P_("GtkBox that holds the dialog’s primary and secondary labels" ), |
445 | GTK_TYPE_WIDGET, |
446 | GTK_PARAM_READABLE)); |
447 | |
448 | gtk_widget_class_set_template_from_resource (widget_class, resource_name: "/org/gtk/libgtk/ui/gtkmessagedialog.ui" ); |
449 | gtk_widget_class_bind_template_child_private (widget_class, GtkMessageDialog, label); |
450 | gtk_widget_class_bind_template_child_private (widget_class, GtkMessageDialog, secondary_label); |
451 | gtk_widget_class_bind_template_child_internal_private (widget_class, GtkMessageDialog, message_area); |
452 | } |
453 | |
454 | static void |
455 | gtk_message_dialog_init (GtkMessageDialog *dialog) |
456 | { |
457 | GtkMessageDialogPrivate *priv = gtk_message_dialog_get_instance_private (self: dialog); |
458 | GtkWidget *action_area; |
459 | GtkSettings *settings; |
460 | gboolean use_caret; |
461 | |
462 | priv->has_primary_markup = FALSE; |
463 | priv->has_secondary_text = FALSE; |
464 | priv->message_type = GTK_MESSAGE_OTHER; |
465 | |
466 | gtk_widget_add_css_class (GTK_WIDGET (dialog), css_class: "message" ); |
467 | |
468 | gtk_widget_init_template (GTK_WIDGET (dialog)); |
469 | action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog)); |
470 | gtk_widget_set_halign (widget: action_area, align: GTK_ALIGN_FILL); |
471 | gtk_box_set_homogeneous (GTK_BOX (action_area), TRUE); |
472 | |
473 | settings = gtk_widget_get_settings (GTK_WIDGET (dialog)); |
474 | g_object_get (object: settings, first_property_name: "gtk-keynav-use-caret" , &use_caret, NULL); |
475 | gtk_label_set_selectable (GTK_LABEL (priv->label), setting: use_caret); |
476 | gtk_label_set_selectable (GTK_LABEL (priv->secondary_label), setting: use_caret); |
477 | } |
478 | |
479 | /** |
480 | * gtk_message_dialog_new: |
481 | * @parent: (nullable): transient parent |
482 | * @flags: flags |
483 | * @type: type of message |
484 | * @buttons: set of buttons to use |
485 | * @message_format: (nullable): printf()-style format string |
486 | * @...: arguments for @message_format |
487 | * |
488 | * Creates a new message dialog. |
489 | * |
490 | * This is a simple dialog with some text the user may want to see. |
491 | * When the user clicks a button a “response” signal is emitted with |
492 | * response IDs from [enum@Gtk.ResponseType]. See [class@Gtk.Dialog] |
493 | * for more details. |
494 | * |
495 | * Returns: (transfer none): a new `GtkMessageDialog` |
496 | */ |
497 | GtkWidget* |
498 | gtk_message_dialog_new (GtkWindow *parent, |
499 | GtkDialogFlags flags, |
500 | GtkMessageType type, |
501 | GtkButtonsType buttons, |
502 | const char *message_format, |
503 | ...) |
504 | { |
505 | GtkWidget *widget; |
506 | GtkDialog *dialog; |
507 | char * msg = NULL; |
508 | va_list args; |
509 | |
510 | g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL); |
511 | |
512 | widget = g_object_new (GTK_TYPE_MESSAGE_DIALOG, |
513 | first_property_name: "use-header-bar" , FALSE, |
514 | "message-type" , type, |
515 | "buttons" , buttons, |
516 | NULL); |
517 | dialog = GTK_DIALOG (widget); |
518 | |
519 | if (message_format) |
520 | { |
521 | GtkMessageDialogPrivate *priv = gtk_message_dialog_get_instance_private (self: (GtkMessageDialog*)dialog); |
522 | va_start (args, message_format); |
523 | msg = g_strdup_vprintf (format: message_format, args); |
524 | va_end (args); |
525 | |
526 | gtk_label_set_text (GTK_LABEL (priv->label), str: msg); |
527 | |
528 | g_free (mem: msg); |
529 | } |
530 | |
531 | if (parent != NULL) |
532 | gtk_window_set_transient_for (GTK_WINDOW (widget), GTK_WINDOW (parent)); |
533 | |
534 | if (flags & GTK_DIALOG_MODAL) |
535 | gtk_window_set_modal (GTK_WINDOW (dialog), TRUE); |
536 | |
537 | if (flags & GTK_DIALOG_DESTROY_WITH_PARENT) |
538 | gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE); |
539 | |
540 | return widget; |
541 | } |
542 | |
543 | /** |
544 | * gtk_message_dialog_new_with_markup: |
545 | * @parent: (nullable): transient parent |
546 | * @flags: flags |
547 | * @type: type of message |
548 | * @buttons: set of buttons to use |
549 | * @message_format: (nullable): printf()-style format string |
550 | * @...: arguments for @message_format |
551 | * |
552 | * Creates a new message dialog. |
553 | * |
554 | * This is a simple dialog with some text that is marked up with |
555 | * Pango markup. When the user clicks a button a “response” signal |
556 | * is emitted with response IDs from [enum@Gtk.ResponseType]. See |
557 | * [class@Gtk.Dialog] for more details. |
558 | * |
559 | * Special XML characters in the printf() arguments passed to this |
560 | * function will automatically be escaped as necessary. |
561 | * (See g_markup_printf_escaped() for how this is implemented.) |
562 | * Usually this is what you want, but if you have an existing |
563 | * Pango markup string that you want to use literally as the |
564 | * label, then you need to use [method@Gtk.MessageDialog.set_markup] |
565 | * instead, since you can’t pass the markup string either |
566 | * as the format (it might contain “%” characters) or as a string |
567 | * argument. |
568 | * |
569 | * ```c |
570 | * GtkWidget *dialog; |
571 | * GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT; |
572 | * dialog = gtk_message_dialog_new (parent_window, |
573 | * flags, |
574 | * GTK_MESSAGE_ERROR, |
575 | * GTK_BUTTONS_CLOSE, |
576 | * NULL); |
577 | * gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (dialog), |
578 | * markup); |
579 | * ``` |
580 | * |
581 | * Returns: a new `GtkMessageDialog` |
582 | **/ |
583 | GtkWidget* |
584 | gtk_message_dialog_new_with_markup (GtkWindow *parent, |
585 | GtkDialogFlags flags, |
586 | GtkMessageType type, |
587 | GtkButtonsType buttons, |
588 | const char *message_format, |
589 | ...) |
590 | { |
591 | GtkWidget *widget; |
592 | va_list args; |
593 | char *msg = NULL; |
594 | |
595 | g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL); |
596 | |
597 | widget = gtk_message_dialog_new (parent, flags, type, buttons, NULL); |
598 | |
599 | if (message_format) |
600 | { |
601 | va_start (args, message_format); |
602 | msg = g_markup_vprintf_escaped (format: message_format, args); |
603 | va_end (args); |
604 | |
605 | gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (widget), str: msg); |
606 | |
607 | g_free (mem: msg); |
608 | } |
609 | |
610 | return widget; |
611 | } |
612 | |
613 | /** |
614 | * gtk_message_dialog_set_markup: |
615 | * @message_dialog: a `GtkMessageDialog` |
616 | * @str: string with Pango markup |
617 | * |
618 | * Sets the text of the message dialog. |
619 | */ |
620 | void |
621 | gtk_message_dialog_set_markup (GtkMessageDialog *message_dialog, |
622 | const char *str) |
623 | { |
624 | GtkMessageDialogPrivate *priv = gtk_message_dialog_get_instance_private (self: message_dialog); |
625 | |
626 | g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog)); |
627 | |
628 | priv->has_primary_markup = TRUE; |
629 | gtk_label_set_markup (GTK_LABEL (priv->label), str); |
630 | } |
631 | |
632 | /** |
633 | * gtk_message_dialog_format_secondary_text: |
634 | * @message_dialog: a `GtkMessageDialog` |
635 | * @message_format: (nullable): printf()-style format string |
636 | * @...: arguments for @message_format |
637 | * |
638 | * Sets the secondary text of the message dialog. |
639 | */ |
640 | void |
641 | gtk_message_dialog_format_secondary_text (GtkMessageDialog *message_dialog, |
642 | const char *message_format, |
643 | ...) |
644 | { |
645 | GtkMessageDialogPrivate *priv = gtk_message_dialog_get_instance_private (self: message_dialog); |
646 | va_list args; |
647 | char *msg = NULL; |
648 | |
649 | g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog)); |
650 | |
651 | if (message_format) |
652 | { |
653 | priv->has_secondary_text = TRUE; |
654 | gtk_widget_add_css_class (widget: priv->label, css_class: "title" ); |
655 | |
656 | va_start (args, message_format); |
657 | msg = g_strdup_vprintf (format: message_format, args); |
658 | va_end (args); |
659 | |
660 | gtk_widget_show (widget: priv->secondary_label); |
661 | gtk_label_set_text (GTK_LABEL (priv->secondary_label), str: msg); |
662 | |
663 | g_free (mem: msg); |
664 | } |
665 | else |
666 | { |
667 | priv->has_secondary_text = FALSE; |
668 | gtk_widget_remove_css_class (widget: priv->label, css_class: "title" ); |
669 | gtk_widget_hide (widget: priv->secondary_label); |
670 | } |
671 | } |
672 | |
673 | /** |
674 | * gtk_message_dialog_format_secondary_markup: |
675 | * @message_dialog: a `GtkMessageDialog` |
676 | * @message_format: printf()-style string with Pango markup |
677 | * @...: arguments for @message_format |
678 | * |
679 | * Sets the secondary text of the message dialog. |
680 | * |
681 | * The @message_format is assumed to contain Pango markup. |
682 | * |
683 | * Due to an oversight, this function does not escape special |
684 | * XML characters like [ctor@Gtk.MessageDialog.new_with_markup] |
685 | * does. Thus, if the arguments may contain special XML characters, |
686 | * you should use g_markup_printf_escaped() to escape it. |
687 | * |
688 | * ```c |
689 | * char *msg; |
690 | * |
691 | * msg = g_markup_printf_escaped (message_format, ...); |
692 | * gtk_message_dialog_format_secondary_markup (message_dialog, |
693 | * "%s", msg); |
694 | * g_free (msg); |
695 | * ``` |
696 | */ |
697 | void |
698 | gtk_message_dialog_format_secondary_markup (GtkMessageDialog *message_dialog, |
699 | const char *message_format, |
700 | ...) |
701 | { |
702 | GtkMessageDialogPrivate *priv = gtk_message_dialog_get_instance_private (self: message_dialog); |
703 | va_list args; |
704 | char *msg = NULL; |
705 | |
706 | g_return_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog)); |
707 | |
708 | if (message_format) |
709 | { |
710 | priv->has_secondary_text = TRUE; |
711 | gtk_widget_add_css_class (widget: priv->label, css_class: "title" ); |
712 | |
713 | va_start (args, message_format); |
714 | msg = g_strdup_vprintf (format: message_format, args); |
715 | va_end (args); |
716 | |
717 | gtk_widget_show (widget: priv->secondary_label); |
718 | gtk_label_set_markup (GTK_LABEL (priv->secondary_label), str: msg); |
719 | |
720 | g_free (mem: msg); |
721 | } |
722 | else |
723 | { |
724 | priv->has_secondary_text = FALSE; |
725 | gtk_widget_remove_css_class (widget: priv->label, css_class: "title" ); |
726 | gtk_widget_hide (widget: priv->secondary_label); |
727 | } |
728 | } |
729 | |
730 | /** |
731 | * gtk_message_dialog_get_message_area: |
732 | * @message_dialog: a `GtkMessageDialog` |
733 | * |
734 | * Returns the message area of the dialog. |
735 | * |
736 | * This is the box where the dialog’s primary and secondary labels |
737 | * are packed. You can add your own extra content to that box and it |
738 | * will appear below those labels. See [method@Gtk.Dialog.get_content_area] |
739 | * for the corresponding function in the parent [class@Gtk.Dialog]. |
740 | * |
741 | * Returns: (transfer none): A `GtkBox` corresponding to the |
742 | * “message area” in the @message_dialog |
743 | */ |
744 | GtkWidget * |
745 | gtk_message_dialog_get_message_area (GtkMessageDialog *message_dialog) |
746 | { |
747 | GtkMessageDialogPrivate *priv = gtk_message_dialog_get_instance_private (self: message_dialog); |
748 | |
749 | g_return_val_if_fail (GTK_IS_MESSAGE_DIALOG (message_dialog), NULL); |
750 | |
751 | return priv->message_area; |
752 | } |
753 | |