source: projects/synaptic/trunk/gtk/rguserdialog.cc @ 280

Revision 280, 7.0 KB checked in by yasumichi, 15 years ago (diff)

first import

Line 
1/* rguserdialog.cc
2 *
3 * Copyright (c) 2000, 2001 Conectiva S/A
4 *               2003 Michael Vogt
5 *
6 * Author: Alfredo K. Kojima <kojima@conectiva.com.br>
7 *         Michael Vogt <mvo@debian.org>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 */
24
25#include <apt-pkg/error.h>
26#include <apt-pkg/fileutl.h>
27
28#include <assert.h>
29#include "i18n.h"
30#include "rguserdialog.h"
31#include "rgmisc.h"
32
33static void actionResponse(GtkDialog *dialog, gint id, gpointer user_data)
34{
35   GtkResponseType *res = (GtkResponseType *) user_data;
36   *res = (GtkResponseType) id;
37}
38
39bool RGUserDialog::showErrors()
40{
41   GtkWidget *dia;
42   string err,warn;
43   
44   if (_error->empty())
45      return false;
46
47   while (!_error->empty()) {
48      string message;
49      bool iserror = _error->PopMessage(message);
50
51      // Ignore some stupid error messages.
52      if (message == "Tried to dequeue a fetching object")
53         continue;
54
55      if (!message.empty()) {
56         if (iserror)
57            err = err + "E: " + message+ "\n";
58         else
59            warn = warn + "W: " + message+ "\n";
60      }
61   }
62   string msg;
63   GtkMessageType msg_type = GTK_MESSAGE_ERROR;
64   if(err.empty()) {
65      msg_type = GTK_MESSAGE_WARNING;
66      msg = warn;
67   } else
68      msg = err + "\n"+ warn;
69   dia = gtk_message_dialog_new(GTK_WINDOW(_parentWindow),
70                                GTK_DIALOG_DESTROY_WITH_PARENT,
71                                msg_type, GTK_BUTTONS_OK,
72                                _("The following problems were found "
73                                  "on your system:"));
74   // if we have no parent, don't skip the taskbar hint
75   int id = _config->FindI("Volatile::ParentWindowId", -1);
76   if(_config->FindB("Volatile::HideMainwindow",false) && id < 0) {
77      gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dia), FALSE);
78      gtk_window_set_urgency_hint(GTK_WINDOW(dia), TRUE);
79   }
80   gtk_widget_set_size_request(dia,500,300);
81   gtk_container_set_border_width(GTK_CONTAINER(dia), 5);
82   GtkWidget *scroll = gtk_scrolled_window_new(NULL,NULL);
83   GtkWidget *textview = gtk_text_view_new();
84   GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
85   gtk_text_buffer_set_text(GTK_TEXT_BUFFER(buffer),msg.c_str(), -1);
86   gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_WORD);
87   gtk_text_view_set_left_margin(GTK_TEXT_VIEW(textview), 3);
88   gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview), FALSE);
89   gtk_container_add(GTK_CONTAINER(scroll), textview);
90   gtk_widget_show_all(scroll);
91   gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dia)->vbox), scroll);
92   gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dia)->vbox), 12);
93   gtk_dialog_run(GTK_DIALOG(dia));
94   gtk_widget_destroy(dia);
95
96   return true;
97}
98
99
100bool RGUserDialog::message(const char *msg,
101                           RUserDialog::DialogType dialog,
102                           RUserDialog::ButtonsType buttons, bool defres)
103{
104   GtkWidget *dia;
105   GtkResponseType res;
106   GtkMessageType gtkmessage;
107   GtkButtonsType gtkbuttons;
108
109   switch (dialog) {
110      case RUserDialog::DialogInfo:
111         gtkmessage = GTK_MESSAGE_INFO;
112         gtkbuttons = GTK_BUTTONS_OK;
113         break;
114      case RUserDialog::DialogWarning:
115         gtkmessage = GTK_MESSAGE_WARNING;
116         gtkbuttons = GTK_BUTTONS_OK;
117         break;
118      case RUserDialog::DialogError:
119         gtkmessage = GTK_MESSAGE_ERROR;
120         gtkbuttons = GTK_BUTTONS_OK;
121         break;
122      case RUserDialog::DialogQuestion:
123         gtkmessage = GTK_MESSAGE_QUESTION;
124         gtkbuttons = GTK_BUTTONS_YES_NO;
125         break;
126   }
127
128   switch (buttons) {
129      case RUserDialog::ButtonsDefault:
130         break;
131      case RUserDialog::ButtonsOk:
132         gtkbuttons = GTK_BUTTONS_OK;
133         break;
134      case RUserDialog::ButtonsOkCancel:
135         gtkbuttons = GTK_BUTTONS_OK_CANCEL;
136         break;
137      case RUserDialog::ButtonsYesNo:
138         gtkbuttons = GTK_BUTTONS_YES_NO;
139         break;
140   }
141
142   dia = gtk_message_dialog_new(GTK_WINDOW(_parentWindow),
143                                GTK_DIALOG_DESTROY_WITH_PARENT,
144                                gtkmessage, gtkbuttons, "%s", utf8(msg));
145
146   if (defres) {
147      switch (buttons) {
148         case RUserDialog::ButtonsOkCancel:
149            gtk_dialog_set_default_response(GTK_DIALOG(dia), GTK_RESPONSE_OK);
150            break;
151         case RUserDialog::ButtonsYesNo:
152            gtk_dialog_set_default_response(GTK_DIALOG(dia), GTK_RESPONSE_YES);
153            break;
154      }
155   } else {
156      switch (buttons) {
157         case RUserDialog::ButtonsOkCancel:
158            gtk_dialog_set_default_response(GTK_DIALOG(dia),
159                                            GTK_RESPONSE_CANCEL);
160            break;
161         case RUserDialog::ButtonsYesNo:
162            gtk_dialog_set_default_response(GTK_DIALOG(dia), GTK_RESPONSE_NO);
163            break;
164      }
165   }
166
167   g_signal_connect(GTK_OBJECT(dia), "response",
168                    G_CALLBACK(actionResponse), (gpointer) & res);
169   gtk_dialog_run(GTK_DIALOG(dia));
170   gtk_widget_destroy(dia);
171   return (res == GTK_RESPONSE_OK) || (res == GTK_RESPONSE_YES);
172}
173
174// RGGladeUserDialog
175RGGladeUserDialog::RGGladeUserDialog(RGWindow *parent, const char *name)
176{
177   init(name);
178   _parentWindow = parent->window();
179}
180
181bool RGGladeUserDialog::init(const char *name)
182{
183   gchar *filename = NULL;
184   gchar *main_widget = NULL;
185
186   //cout << "RGGladeUserDialog::RGGladeUserDialog()" << endl;
187
188   filename = g_strdup_printf("dialog_%s.glade", name);
189   main_widget = g_strdup_printf("dialog_%s", name);
190   if (FileExists(filename)) {
191      gladeXML = glade_xml_new(filename, main_widget, NULL);
192   } else {
193      g_free(filename);
194      filename = g_strdup_printf(SYNAPTIC_GLADEDIR "dialog_%s.glade", name);
195      gladeXML = glade_xml_new(filename, main_widget, NULL);
196   }
197   assert(gladeXML);
198   _dialog = glade_xml_get_widget(gladeXML, main_widget);
199   assert(_dialog);
200
201   gtk_window_set_position(GTK_WINDOW(_dialog),
202                           GTK_WIN_POS_CENTER_ON_PARENT);
203   gtk_window_set_skip_taskbar_hint(GTK_WINDOW(_dialog), TRUE);
204   gtk_window_set_transient_for(GTK_WINDOW(_dialog), 
205                                GTK_WINDOW(_parentWindow));
206
207   g_free(filename);
208   g_free(main_widget);
209}
210
211int RGGladeUserDialog::run(const char *name,bool return_gtk_response)
212{
213   if(name != NULL)
214      init(name);
215
216   res = (GtkResponseType) gtk_dialog_run(GTK_DIALOG(_dialog));
217   gtk_widget_hide(_dialog);
218
219   if(return_gtk_response)
220      return res;
221   else
222      return (res == GTK_RESPONSE_OK) || (res == GTK_RESPONSE_YES);
223}
224
225
226// vim:sts=4:sw=4
Note: See TracBrowser for help on using the repository browser.