Back to index

lightning-sunbird  0.9+nobinonly
showOSAlert.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Don Bragg <dbragg@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include  <stdio.h>
00040 #include  <string.h>
00041 #include "nscore.h"
00042 
00043 //defines and includes for previous installation cleanup process
00044 #if defined (XP_WIN)
00045 #include <windows.h>
00046 #elif defined (XP_MAC)
00047 #include <Dialogs.h>
00048 #include <TextUtils.h>
00049 #elif defined (MOZ_WIDGET_GTK)
00050 #include <gtk/gtk.h>
00051 #elif defined (XP_OS2)
00052 #define INCL_DOS
00053 #define INCL_WIN
00054 #include <os2.h>
00055 #endif
00056 
00057 extern "C" void ShowOSAlert(const char* aMessage);
00058 
00059 #if defined (MOZ_WIDGET_GTK)
00060 
00061 static int sbAlertDone = 0;
00062 
00071 void
00072 ns_gtk_alert_OK_callback(GtkWidget *aWidget, gpointer aData)
00073 {
00074     GtkWidget *alertDlg = (GtkWidget *) aData;
00075 
00076     if (!alertDlg)
00077         return;
00078 
00079     gtk_widget_destroy(alertDlg);
00080     sbAlertDone = 1;
00081 }
00082 
00100 int
00101 NS_gtk_alert(const char *aMessage, const char *aTitle, const char *aOKBtnText)
00102 {
00103 #ifdef DEBUG_dbragg
00104     printf ("\n*** Now inside NS_gtk_alert *** \n");
00105 #endif 
00106 
00107     GtkWidget *alertDlg = NULL;
00108     GtkWidget *okBtn = NULL;
00109     GtkWidget *msgLabel = NULL;
00110     GtkWidget *packerLbl = NULL;
00111     GtkWidget *packerBtn = NULL;
00112     const char *okBtnText = aOKBtnText;
00113     const char *title = aTitle;
00114 
00115     if (!aMessage)
00116         return -1;
00117     if (!aOKBtnText)
00118         okBtnText = "OK";
00119     if (!aTitle)
00120         title = " ";
00121 
00122 #ifdef DEBUG_dbragg
00123     printf("title is: %s\n", title);
00124 #endif
00125 
00126     alertDlg = gtk_dialog_new();
00127     msgLabel = gtk_label_new(aMessage);
00128     if (msgLabel)
00129       gtk_label_set_line_wrap(GTK_LABEL(msgLabel), TRUE);
00130     okBtn = gtk_button_new_with_label(okBtnText);
00131     packerLbl = gtk_packer_new();
00132     packerBtn = gtk_packer_new();
00133     sbAlertDone = 0;
00134 
00135     if (alertDlg && msgLabel && okBtn && packerBtn && packerLbl)
00136     {
00137         // pack widgets in dialog 
00138         gtk_packer_set_default_border_width(GTK_PACKER(packerLbl), 20);
00139         gtk_packer_add_defaults(GTK_PACKER(packerLbl), msgLabel, 
00140             GTK_SIDE_BOTTOM, GTK_ANCHOR_CENTER, GTK_FILL_X);
00141         gtk_packer_set_default_border_width(GTK_PACKER(packerBtn), 0);
00142         gtk_packer_add_defaults(GTK_PACKER(packerBtn), okBtn, 
00143             GTK_SIDE_BOTTOM, GTK_ANCHOR_CENTER, GTK_FILL_Y);
00144         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(alertDlg)->vbox), 
00145             packerLbl);
00146         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(alertDlg)->action_area),
00147             packerBtn);
00148 
00149         gtk_window_set_modal(GTK_WINDOW(alertDlg), TRUE);
00150         gtk_window_set_title(GTK_WINDOW(alertDlg), title);
00151         gtk_window_set_position(GTK_WINDOW(alertDlg), GTK_WIN_POS_CENTER);
00152 
00153         // register callback for OK button
00154         gtk_signal_connect(GTK_OBJECT(okBtn), "clicked", 
00155             GTK_SIGNAL_FUNC(ns_gtk_alert_OK_callback), alertDlg);
00156         GTK_WIDGET_SET_FLAGS(okBtn, GTK_CAN_DEFAULT);
00157         gtk_widget_grab_default(okBtn);
00158 
00159         // show dialog
00160         gtk_widget_show(msgLabel);
00161         gtk_widget_show(packerLbl);
00162         gtk_widget_show(okBtn);
00163         gtk_widget_show(packerBtn);
00164         gtk_widget_show(alertDlg);
00165     }
00166     else
00167     {
00168         return -2;
00169     }
00170 
00171     while (!sbAlertDone)
00172     {
00173         while (gtk_events_pending())
00174             gtk_main_iteration();
00175     }
00176 
00177     return 0;
00178 }
00179 #endif //MOZ_WIDGET_GTK
00180 
00181 
00182 // The maximum allowed length of aMessage is 255 characters!
00183 void ShowOSAlert(const char* aMessage)
00184 {
00185 #ifdef DEBUG_dbragg
00186 printf("\n****Inside ShowOSAlert ***\n"); 
00187 #endif 
00188 
00189     const PRInt32 max_len = 255;
00190     char message_copy[max_len+1] = { 0 };
00191     PRInt32 input_len = strlen(aMessage);
00192     PRInt32 copy_len = (input_len > max_len) ? max_len : input_len;
00193     strncpy(message_copy, aMessage, copy_len);
00194     message_copy[copy_len] = 0;
00195 
00196 #if defined (XP_WIN)
00197     MessageBox(NULL, message_copy, NULL, MB_OK | MB_ICONERROR | MB_SETFOREGROUND );
00198 #elif (XP_MAC)
00199     short buttonClicked;
00200     StandardAlert(kAlertStopAlert, c2pstr(message_copy), nil, nil, &buttonClicked);
00201 #elif defined (MOZ_WIDGET_GTK)
00202     NS_gtk_alert(message_copy, NULL, "OK");
00203 #elif defined (XP_OS2)
00204     /* Set our app to be a PM app before attempting Win calls */
00205     PPIB ppib;
00206     PTIB ptib;
00207     DosGetInfoBlocks(&ptib, &ppib);
00208     ppib->pib_ultype = 3;
00209     HAB hab = WinInitialize(0);
00210     HMQ hmq = WinCreateMsgQueue(hmq,0);
00211     WinMessageBox( HWND_DESKTOP, HWND_DESKTOP, message_copy, "", 0, MB_OK);
00212     WinDestroyMsgQueue(hmq);
00213     WinTerminate(hab);
00214 #endif
00215     // It can't hurt to display the message on the console in any case,
00216     // even if we have already tried to display it in a GUI window.
00217     fprintf(stdout, "%s\n", aMessage);
00218 }