Strumenti Utente

Strumenti Sito


Barra laterale

programmazione:gtk:messagebox

Creare messagebox in c e gtk2

Autore: Fabio Di Matteo
Ultima revisione: 30/01/2016 - 16:34


Ecco 3 funzioni che creano 3 message box una di info, una di conferma e l'altra d'errore: (come argomenti vogliono la finestra principale del programma che li genera, il testo principale, quello secondario e l'etichetta sul bordo della finestra della messagebox).

Utilizzo

info_message(MainWindow,"Titolo","Testo secondario del messaggio", "Titolo finestra");
err_message(MainWindow,"Titolo","Testo secondario del messaggio", "Titolo finestra");
 
//YesNo con controllo risposta.
gint r = info_YesNo(MainWindow,"Titolo","Testo secondario del messaggio", "Titolo finestra");
if (r==GTK_RESPONSE_YES)g_print("Hai risposto si.");

Prototipi

void info_message(GtkWidget *main_window,gchar* title, gchar *msg, gchar* winlabel);
gint info_YesNo(GtkWidget *main_window,gchar* title, gchar *msg, gchar* winlabel);
void err_message(GtkWidget *main_window,gchar* title, gchar *msg, gchar* winlabel);

Codice




message.c

/*
 *      message.c
 *      
 *      Copyright 2008 Fabio DM 
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
 
#include <gtk/gtk.h> 
 
void
info_message(GtkWidget *main_window,gchar* title, gchar *msg, gchar* winlabel)
{
	GtkWidget *dialog;
	title=g_strdup_printf("<span size=\"x-large\"><b>%s</b></span>", title);
 
  	dialog = gtk_message_dialog_new_with_markup (GTK_WINDOW (main_window),
				   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
				   GTK_MESSAGE_INFO,
				   GTK_BUTTONS_OK,
				   title);
	gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
					"%s",(gchar *)msg);
	gtk_window_set_title(GTK_WINDOW(dialog),winlabel);
  	gtk_dialog_run (GTK_DIALOG (dialog));
  	gtk_widget_destroy (dialog);
}
 
 
gint
info_YesNo(GtkWidget *main_window,gchar* title, gchar *msg, gchar* winlabel)
{
  GtkWidget *dialog;
  gint result;
  title=g_strdup_printf("<span size=\"x-large\"><b>%s</b></span>", title);
 
  	dialog = gtk_message_dialog_new_with_markup (GTK_WINDOW (main_window),
				   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
				   GTK_MESSAGE_WARNING,
				   GTK_BUTTONS_YES_NO,
				   title);
	gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
					"%s",(gchar *)msg);
	gtk_window_set_title(GTK_WINDOW(dialog),winlabel);
  	result = gtk_dialog_run (GTK_DIALOG (dialog));
	switch (result)
    {
      case GTK_RESPONSE_YES:
         result=GTK_RESPONSE_YES ;
		 break;
	  case GTK_RESPONSE_NO:
         result=GTK_RESPONSE_NO ;
		 break;	 
 
      default:
         result=GTK_RESPONSE_DELETE_EVENT;
         break;
    }
  	gtk_widget_destroy (dialog);
	return result;
 
}
 
void
err_message(GtkWidget *main_window,gchar* title, gchar *msg, gchar* winlabel)
{
	GtkWidget *dialog;
	title=g_strdup_printf("<span size=\"x-large\"><b>%s</b></span>", title);
 
	dialog = gtk_message_dialog_new_with_markup (GTK_WINDOW (main_window),
				   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
				   GTK_MESSAGE_ERROR,
				   GTK_BUTTONS_OK,title);
  gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
    												"%s",(gchar *)msg);
  gtk_window_set_title(GTK_WINDOW(dialog),winlabel);
  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

programmazione/gtk/messagebox.txt · Ultima modifica: 18/04/2018 - 15:48 (modifica esterna)