Back to index

lightning-sunbird  0.9+nobinonly
nsWelcomeDlg.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, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Samir Gehani <sgehani@netscape.com>
00025  *   Brian Ryner <bryner@brianryner.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "nsWelcomeDlg.h"
00042 #include "nsXInstaller.h"
00043 #include <sys/stat.h>
00044 
00045 const int nsWelcomeDlg::kMsgCount;
00046 
00047 nsWelcomeDlg::nsWelcomeDlg()
00048   : mTitle(NULL), mWelcomeMsg(NULL), mPixmap(NULL), mBox(NULL)
00049 {
00050   memset(mMsgs, 0, sizeof(mMsgs));
00051 }
00052 
00053 nsWelcomeDlg::~nsWelcomeDlg()
00054 {
00055   XI_IF_FREE(mPixmap);
00056   for (int i = 0; i < kMsgCount; ++i)
00057     XI_IF_FREE(mMsgs[i]);
00058 
00059   XI_IF_FREE(mWelcomeMsg);
00060   XI_IF_FREE(mTitle);
00061 }
00062 
00063 void 
00064 nsWelcomeDlg::Back(GtkWidget *aWidget, gpointer aData)
00065 {
00066   DUMP("Back");
00067   if (aData != gCtx->wdlg) return;
00068 }
00069 
00070 void
00071 nsWelcomeDlg::Next(GtkWidget *aWidget, gpointer aData)
00072 {
00073   DUMP("Next");
00074   if (aData != gCtx->wdlg) return;
00075 
00076   // hide this notebook page
00077   gCtx->wdlg->Hide(nsXInstallerDlg::FORWARD_MOVE);
00078 
00079   // disconnect this dlg's nav btn signal handlers
00080   gtk_signal_disconnect(GTK_OBJECT(gCtx->back), gCtx->backID);
00081   gtk_signal_disconnect(GTK_OBJECT(gCtx->next), gCtx->nextID);
00082 
00083   // show the next dlg
00084   gCtx->ldlg->Show(nsXInstallerDlg::FORWARD_MOVE);
00085 }
00086 
00087 int
00088 nsWelcomeDlg::Parse(nsINIParser *aParser)
00089 {
00090   int bufsize = 0;
00091   char *showDlg = NULL;
00092   char msgName[] = "Message0";
00093   char *tmp;
00094 
00095   /* optional keys */
00096   bufsize = 5;
00097   aParser->GetStringAlloc(DLG_WELCOME, SHOW_DLG, &showDlg, &bufsize);
00098   if (bufsize != 0 && showDlg) {
00099     if (strncmp(showDlg, "TRUE", 4) == 0)
00100       mShowDlg = nsXInstallerDlg::SHOW_DIALOG;
00101     else if (strncmp(showDlg, "FALSE", 5) == 0)
00102       mShowDlg = nsXInstallerDlg::SKIP_DIALOG;
00103   }
00104 
00105   bufsize = 0;
00106   aParser->GetStringAlloc(DLG_WELCOME, TITLE, &mTitle, &bufsize);
00107   if (bufsize == 0)
00108     XI_IF_FREE(mTitle); 
00109 
00110   bufsize = 0;
00111   aParser->GetStringAlloc(DLG_WELCOME, WATERMARK, &mPixmap, &bufsize);
00112   if (bufsize == 0)
00113     XI_IF_FREE(mPixmap);
00114 
00115   bufsize = 0;
00116   aParser->GetStringAlloc(DLG_WELCOME, MSGWELCOME, &tmp, &bufsize);
00117   if (bufsize == 0) {
00118     XI_IF_FREE(tmp);
00119   } else {
00120     mWelcomeMsg = g_strdup_printf(tmp, gCtx->opt->mProductName);
00121   }
00122 
00123   for (int i = 0; i < kMsgCount; ++i) {
00124     bufsize = 0;
00125     msgName[7] = (char)(i + 48);  // ASCII 48 == 0
00126     aParser->GetStringAlloc(DLG_WELCOME, msgName, &tmp, &bufsize);
00127     if (bufsize == 0) {
00128       XI_IF_FREE(tmp);
00129     } else {
00130       mMsgs[i] = g_strdup_printf(tmp, gCtx->opt->mProductName);
00131     }
00132   }
00133 
00134   return OK;
00135 }
00136 
00137 int
00138 nsWelcomeDlg::Show(int aDirection)
00139 {
00140   int err = 0;
00141   int msgLength = 0;
00142   char *msgText, *textPos;
00143 
00144   XI_VERIFY(gCtx);
00145   XI_VERIFY(gCtx->notebook);
00146 
00147   if (mWidgetsInit == FALSE) {
00148     // static widget init
00149 
00150     // Note our page number in the wizard.
00151     mPageNum = gtk_notebook_get_current_page(GTK_NOTEBOOK(gCtx->notebook));
00152 
00153     // Set up the window title
00154     gtk_window_set_title(GTK_WINDOW(gCtx->window), mTitle);
00155 
00156     mBox = gtk_hbox_new(FALSE, 0);
00157     gtk_notebook_append_page(GTK_NOTEBOOK(gCtx->notebook), mBox, NULL);
00158 
00159     GtkWidget *wmbox = gtk_event_box_new();
00160     if (mPixmap) {
00161       GdkPixbuf *pb = gdk_pixbuf_new_from_file(mPixmap, NULL);
00162       if (pb) {
00163         GdkPixmap *pm = NULL;
00164         gdk_pixbuf_render_pixmap_and_mask(pb, &pm, NULL, 0);
00165         if (pm) {
00166           GtkStyle *newStyle = gtk_style_copy(gtk_widget_get_style(wmbox));
00167           newStyle->bg_pixmap[GTK_STATE_NORMAL] = pm;
00168           gtk_widget_set_style(wmbox, newStyle);
00169 
00170           // newStyle now owns the pixmap, so we don't unref it.
00171           g_object_unref(newStyle);
00172 
00173           // Make the watermark box the width of the pixmap.
00174           gint width, height;
00175           gdk_drawable_get_size(pm, &width, &height);
00176           gtk_widget_set_size_request(wmbox, width, -1);
00177         }
00178 
00179         // We're done with rendering the pixbuf, so we can destroy it.
00180         gdk_pixbuf_unref(pb);
00181       }
00182     }
00183 
00184     gtk_box_pack_start(GTK_BOX(mBox), wmbox, FALSE, FALSE, 0);
00185 
00186     wmbox = gtk_event_box_new();
00187     gtk_box_pack_start_defaults(GTK_BOX(mBox), wmbox);
00188 
00189     GtkWidget *inner_hbox = gtk_hbox_new(FALSE, 0);
00190     gtk_container_add(GTK_CONTAINER(wmbox), inner_hbox);
00191 
00192     GtkWidget *spacer = gtk_alignment_new(0, 0, 0, 0);
00193     gtk_widget_set_size_request(spacer, 5, -1);
00194     gtk_box_pack_start(GTK_BOX(inner_hbox), spacer, FALSE, FALSE, 0);
00195 
00196     GtkWidget *vbox = gtk_vbox_new(FALSE, 8);
00197     gtk_box_pack_start(GTK_BOX(inner_hbox), vbox, FALSE, FALSE, 0);
00198 
00199     for (int i = 0; i < kMsgCount - 1; ++i)
00200       msgLength += strlen(mMsgs[i]);
00201 
00202 #define kSpanBegin "<span weight=\"bold\" size=\"larger\">"
00203 #define kSpanEnd   "</span>\n\n"
00204 
00205     msgLength += strlen(kSpanBegin) + strlen(mWelcomeMsg) + strlen(kSpanEnd);
00206     msgText = (char*) malloc(msgLength + (kMsgCount - 1) * 2 + 1);
00207 
00208     textPos = msgText;
00209 
00210     textPos += sprintf(textPos, kSpanBegin "%s" kSpanEnd, mWelcomeMsg);
00211 
00212     for (int i = 0; i < kMsgCount - 1; ++i)
00213       textPos += sprintf(textPos, "%s\n\n", mMsgs[i]);
00214 
00215     GtkWidget *msg = gtk_label_new(NULL);
00216     gtk_label_set_markup(GTK_LABEL(msg), msgText);
00217     gtk_label_set_line_wrap(GTK_LABEL(msg), TRUE);
00218     gtk_misc_set_alignment(GTK_MISC(msg), 0.0, 0.5);
00219 
00220     GdkColor black_col = { 0, 0, 0, 0 };
00221     GdkColor white_col = { 0, 65535, 65535, 65535 };
00222 
00223     gtk_widget_modify_text(msg, GTK_STATE_NORMAL, &black_col);
00224     gtk_widget_modify_bg(wmbox, GTK_STATE_NORMAL, &white_col);
00225 
00226     gtk_box_pack_start(GTK_BOX(vbox), msg, FALSE, FALSE, 0);
00227     gtk_container_child_set(GTK_CONTAINER(vbox), msg, "padding", 5, NULL);
00228 
00229     free(msgText);
00230 
00231     // Now add 100px of padding, and then the last mesage.
00232     spacer = gtk_alignment_new(0.0, 0.0, 0.0, 0.0);
00233     gtk_widget_set_size_request(spacer, -1, 110);
00234     gtk_box_pack_start(GTK_BOX(vbox), spacer, FALSE, FALSE, 0);
00235 
00236     msgText = (char*) malloc(strlen(mMsgs[kMsgCount - 1]) + 2);
00237     sprintf(msgText, "%s\n", mMsgs[kMsgCount - 1]);
00238 
00239     msg = gtk_label_new(msgText);
00240     gtk_widget_modify_text(msg, GTK_STATE_NORMAL, &black_col);
00241     gtk_misc_set_alignment(GTK_MISC(msg), 0.0, 0.5);
00242     gtk_box_pack_start(GTK_BOX(vbox), msg, FALSE, FALSE, 0);
00243     gtk_container_child_set(GTK_CONTAINER(vbox), msg, "padding", 5, NULL);
00244 
00245     free(msgText);
00246 
00247     mWidgetsInit = TRUE;
00248   } else {
00249     gtk_notebook_set_page(GTK_NOTEBOOK(gCtx->notebook), mPageNum);
00250   }
00251 
00252   // No header image for the welcome dialog
00253   gtk_widget_set_usize(gCtx->header, 0, 0);
00254 
00255   gtk_widget_show_all(mBox);
00256 
00257   // signal connect the buttons
00258   gCtx->backID = gtk_signal_connect(GTK_OBJECT(gCtx->back), "clicked",
00259                                     GTK_SIGNAL_FUNC(nsWelcomeDlg::Back),
00260                                     gCtx->wdlg);
00261   gCtx->nextID = gtk_signal_connect(GTK_OBJECT(gCtx->next), "clicked",
00262                                     GTK_SIGNAL_FUNC(nsWelcomeDlg::Next),
00263                                     gCtx->wdlg);
00264 
00265   gtk_widget_set_sensitive(gCtx->back, FALSE);
00266 
00267   GTK_WIDGET_SET_FLAGS(gCtx->next, GTK_CAN_DEFAULT);
00268   gtk_widget_grab_default(gCtx->next);
00269   gtk_widget_grab_focus(gCtx->next);
00270 
00271   return err;
00272 }
00273 
00274 int 
00275 nsWelcomeDlg::Hide(int aDirection)
00276 {
00277   // hide all this dlg's widgets
00278   gtk_widget_hide(mBox);
00279 
00280   return OK;
00281 }