Back to index

lightning-sunbird  0.9+nobinonly
TestPhEmbed.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Christopher Blizzard. Portions created by Christopher Blizzard are Copyright (C) Christopher Blizzard.  All Rights Reserved.
00018  * Portions created by the Initial Developer are Copyright (C) 2001
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Christopher Blizzard <blizzard@mozilla.org>
00023  *   Brian Edmond <briane@qnx.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * 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 <stdlib.h>
00040 #include <string.h>
00041 #include <time.h>
00042 #include <errno.h>
00043 #include <libgen.h>
00044 
00045 #include <Pt.h>
00046 #include <photon/PtWebClient.h>
00047 #include <photon/PtProgress.h>
00048 #include "prtypes.h"
00049 #include "../src/PtMozilla.h"
00050 
00051 int window_count = 0;
00052 struct window_info
00053 {
00054        PtWidget_t *window, *back, *forward, *stop, *web, *reload, *url, *status, \
00055               *progress, *print, *sel, *save, *stream;
00056        char *statusMessage;
00057 };
00058 
00059 
00060 PtWidget_t *create_browser_window(unsigned window_flags);
00061 
00063 struct Auth_data
00064 {
00065        PtWidget_t *awin, *auser, *apass, *aok, *acan;
00066        PtModalCtrl_t mc;
00067        char *user, *pass;
00068 };
00069 
00070 // retrieve user input from authorization dialog
00071 int dlg_auth_cb( PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo )
00072 {
00073     struct Auth_data *auth = (struct Auth_data *) data;
00074        char *u, *p;
00075 
00076     // check which widget triggered the callback
00077     if (widget == auth->aok)
00078        {
00079               PtGetResource(auth->auser, Pt_ARG_TEXT_STRING, &u, 0);
00080               PtGetResource(auth->apass, Pt_ARG_TEXT_STRING, &p, 0);
00081               auth->user = strdup(u);
00082               auth->pass = strdup(p);
00083     }
00084     
00085        PtModalUnblock(&(auth->mc), (int *)1);
00086 
00087     return (Pt_CONTINUE);
00088 }
00089 
00090 int CreateAuthWindow(PtMozillaAuthenticateCb_t *acb, PtWidget_t *parent)
00091 {
00092        PhArea_t area = {{0, 0}, {310, 100}};
00093        PtArg_t args[5];
00094        struct Auth_data *auth = (struct Auth_data *)calloc (sizeof(struct Auth_data), 1);
00095        int response;
00096 
00097        // window
00098        PtSetArg(&args[0], Pt_ARG_DIM, &(area.size), 0);
00099        PtSetArg(&args[1], Pt_ARG_WINDOW_TITLE, "Authentication", 0);
00100        auth->awin = PtCreateWidget(PtWindow, parent, 2, args);
00101 
00102        // labels
00103        area.pos.x = 7;
00104        area.pos.y = 7;
00105        PtSetArg(&args[0], Pt_ARG_POS, &area.pos, 0);
00106        PtSetArg(&args[1], Pt_ARG_TEXT_STRING, "Username:", 0);
00107        PtCreateWidget(PtLabel, auth->awin, 2, args);
00108        area.pos.y += 30;
00109        PtSetArg(&args[0], Pt_ARG_POS, &area.pos, 0);
00110        PtSetArg(&args[1], Pt_ARG_TEXT_STRING, "Password:", 0);
00111        PtCreateWidget(PtLabel, auth->awin, 2, args);
00112 
00113        // fields
00114        area.pos.x = 90;
00115        area.pos.y = 5;
00116        area.size.w = 210;
00117        area.size.h = 20;
00118        PtSetArg(&args[0], Pt_ARG_AREA, &area, 0);
00119        auth->auser = PtCreateWidget(PtText, auth->awin, 1, args);
00120        area.pos.y += 30;
00121        PtSetArg(&args[0], Pt_ARG_AREA, &area, 0);
00122        auth->apass = PtCreateWidget(PtText, auth->awin, 1, args);
00123 
00124        // buttons
00125        area.pos.x = 155;
00126        area.pos.y = 68;
00127        area.size.w = 70;
00128        area.size.h = 21;
00129        PtSetArg(&args[0], Pt_ARG_AREA, &area, 0);
00130        PtSetArg(&args[1], Pt_ARG_TEXT_STRING, "Ok", 0);
00131        auth->aok = PtCreateWidget(PtButton, auth->awin, 2, args);
00132        PtAddCallback(auth->aok, Pt_CB_ACTIVATE, dlg_auth_cb, auth);
00133        area.pos.x += 75;
00134        PtSetArg(&args[0], Pt_ARG_AREA, &area, 0);
00135        PtSetArg(&args[1], Pt_ARG_TEXT_STRING, "Cancel", 0);
00136        auth->acan = PtCreateWidget(PtButton, auth->awin, 2, args);
00137        PtAddCallback(auth->acan, Pt_CB_ACTIVATE, dlg_auth_cb, auth);
00138 
00139        PtRealizeWidget(auth->awin);
00140 
00141        response = (int) PtModalBlock(&(auth->mc), 0);
00142        PtDestroyWidget(auth->awin);
00143        if (auth->user && auth->pass)
00144        {
00145               strcpy(acb->user, auth->user);
00146               strcpy(acb->pass, auth->pass);
00147               free(auth->user);
00148               free(auth->pass);
00149               free(auth);
00150               return (Pt_CONTINUE);
00151        }
00152 
00153        free(auth);
00154        return (Pt_END);
00155 }
00157 
00159 
00160 int window_close_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00161 {
00162        window_count--;
00163        if (window_count == 0)
00164               exit (0);
00165        return (Pt_CONTINUE);
00166 }
00167 
00168 int reload_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00169 {
00170        PtArg_t args[1];
00171        struct window_info *info;
00172        PtGetResource(PtFindDisjoint(widget), Pt_ARG_POINTER, &info, 0);
00173        PtSetArg(&args[0], Pt_ARG_MOZ_RELOAD, 0, 0);
00174        PtSetResources(info->web, 1, args);
00175        return (Pt_CONTINUE);
00176 }
00177 
00178 int print_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00179 {
00180     PtArg_t args[1];
00181        struct window_info *info;
00182        PpPrintContext_t *pc = NULL;
00183 
00184        pc = PpCreatePC();
00185 
00186        PtGetResource(PtFindDisjoint(widget), Pt_ARG_POINTER, &info, 0);
00187        if (PtPrintSelection(info->window, NULL, "Print", pc, Pt_PRINTSEL_DFLT_LOOK) != Pt_PRINTSEL_CANCEL)
00188        {
00189               PtSetArg(&args[0], Pt_ARG_MOZ_PRINT, pc, 0);
00190               PtSetResources(info->web, 1, args);
00191        }
00192        PpReleasePC(pc);
00193 
00194     return (Pt_CONTINUE);
00195 }
00196 
00197 int stop_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00198 {
00199        PtArg_t args[1];
00200        struct window_info *info;
00201        PtGetResource(PtFindDisjoint(widget), Pt_ARG_POINTER, &info, 0);
00202        PtSetArg(&args[0], Pt_ARG_MOZ_STOP, 0, 0);
00203        PtSetResources(info->web, 1, args);
00204        return (Pt_CONTINUE);
00205 }
00206 
00207 int back_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00208 {
00209        PtArg_t args[1];
00210        struct window_info *info;
00211        PtGetResource(PtFindDisjoint(widget), Pt_ARG_POINTER, &info, 0);
00212        PtSetArg(&args[0], Pt_ARG_MOZ_NAVIGATE_PAGE, Pt_WEB_DIRECTION_BACK, 0);
00213        PtSetResources(info->web, 1, args);
00214        return (Pt_CONTINUE);
00215 }
00216 
00217 int forward_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00218 {
00219        PtArg_t args[1];
00220        struct window_info *info;
00221        PtGetResource(PtFindDisjoint(widget), Pt_ARG_POINTER, &info, 0);
00222        PtSetArg(&args[0], Pt_ARG_MOZ_NAVIGATE_PAGE, Pt_WEB_DIRECTION_FWD, 0);
00223        PtSetResources(info->web, 1, args);
00224        return (Pt_CONTINUE);
00225 }
00226 
00227 int load_url_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00228 {
00229        PtTextCallback_t *tcb = (PtTextCallback_t *)cbinfo->cbdata;
00230        PtArg_t args[1];
00231        struct window_info *info;
00232        PtGetResource(PtFindDisjoint(widget), Pt_ARG_POINTER, &info, 0);
00233 
00234        PtSetArg(&args[0], Pt_ARG_MOZ_GET_URL, tcb->text, 0);
00235        PtSetResources(info->web, 1, args);
00236        return (Pt_CONTINUE);
00237 }
00238 
00239 int save_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00240 {
00241        struct window_info *info;
00242        PtFileSelectionInfo_t i;
00243        char *home = getenv("HOME");
00244 
00245        PtGetResource(PtFindDisjoint(widget), Pt_ARG_POINTER, &info, 0);
00246 
00247        memset (&i, 0, sizeof(PtFileSelectionInfo_t));
00248        PtFileSelection(info->window, NULL, "Save Page As", home, "*.html", \
00249               NULL, NULL, NULL, &i, Pt_FSR_NO_FCHECK);
00250 
00251        //MozSavePageAs(info->web, i.path, Pt_MOZ_SAVEAS_HTML);
00252        
00253        return (Pt_CONTINUE);
00254 }
00255 
00256 
00258 
00260 int moz_info_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00261 {
00262        PtMozillaInfoCb_t *info = (PtMozillaInfoCb_t *) cbinfo->cbdata;
00263        PtMozillaInfoCb_t *s = (PtMozillaInfoCb_t *) cbinfo->cbdata;
00264        struct window_info *i;
00265        PtGetResource(PtFindDisjoint(widget), Pt_ARG_POINTER, &i, 0);
00266 
00267        switch (s->type)
00268        {
00269               case Pt_MOZ_INFO_LINK:
00270               case Pt_MOZ_INFO_JSSTATUS:
00271                      if (i->status)
00272                             PtSetResource(i->status, Pt_ARG_TEXT_STRING, info->data, 0);
00273                      break;
00274               case Pt_MOZ_INFO_TITLE:
00275                      PtSetResource(i->window, Pt_ARG_WINDOW_TITLE, info->data, 0);
00276                      break;
00277               case Pt_MOZ_INFO_SSL:
00278                      printf("SSL status: ");
00279                      if (info->status & Pt_SSL_STATE_IS_INSECURE)
00280                             printf("INSECURE ");
00281                      if (info->status & Pt_SSL_STATE_IS_BROKEN)
00282                             printf("BROKEN ");
00283                      if (info->status & Pt_SSL_STATE_IS_SECURE)
00284                             printf("SECURE ");
00285                      if (info->status & Pt_SSL_STATE_SECURE_HIGH)
00286                             printf("HIGH ");
00287                      if (info->status & Pt_SSL_STATE_SECURE_MED)
00288                             printf("MED ");
00289                      if (info->status & Pt_SSL_STATE_SECURE_LOW)
00290                             printf("LOW ");
00291                      printf("\n");
00292                      break;
00293        }
00294 
00295        return (Pt_CONTINUE);
00296 }
00297 
00298 int moz_start_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00299 {
00300        struct window_info *i;
00301        PtGetResource(PtFindDisjoint(widget), Pt_ARG_POINTER, &i, 0);
00302        // could start an animation here
00303        if (i->status)
00304               PtSetResource(i->status, Pt_ARG_TEXT_STRING, "Starting to load page", 0);
00305        if (i->stop)
00306               PtSetResource(i->stop, Pt_ARG_FLAGS, 0, Pt_BLOCKED|Pt_GHOST);
00307 
00308        return (Pt_CONTINUE);
00309 }
00310 
00311 int moz_complete_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00312 {
00313        struct window_info *i;
00314 
00315        PtGetResource(PtFindDisjoint(widget), Pt_ARG_POINTER, &i, 0);
00316        if (i->status) 
00317               PtSetResource(i->status, Pt_ARG_TEXT_STRING, "Done", 0);
00318 
00319        // could stop an animation here
00320        if (i->stop)
00321               PtSetResource(i->stop, Pt_ARG_FLAGS, ~0, Pt_BLOCKED|Pt_GHOST);
00322 
00323        return (Pt_CONTINUE);
00324 }
00325 
00326 int moz_progress_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00327 {
00328        PtMozillaProgressCb_t *c = (PtMozillaProgressCb_t *) cbinfo->cbdata;
00329        int percent;
00330        char message[256]="";
00331        struct window_info *i;
00332 
00333        PtGetResource(PtFindDisjoint(widget), Pt_ARG_POINTER, &i, 0);
00334 
00335        if ((c->max <= 0) || (c->cur > c->max))
00336        {
00337               percent = 100;
00338        //if ( i->statusMessage != NULL ) 
00339               //sprintf(message, "%s (%d bytes loaded)", i->statusMessage, c->cur);
00340               sprintf(message, "%s (%d bytes loaded)", "Loading", c->cur);
00341        }
00342        else
00343        { 
00344               percent = (c->cur*100)/c->max;
00345        //if ( i->statusMessage != NULL ) 
00346               //sprintf(message, "%s (%d%% complete, %d bytes of %d loaded)", i->statusMessage, percent, c->cur, c->max);
00347               sprintf(message, "%s (%d%% complete, %d bytes of %d loaded)", "Transferring", percent, c->cur, c->max);
00348        }
00349 #if 0 
00350 printf ("Progress: %d\n",(int)percent);
00351 #endif
00352 
00353        if (i->progress) 
00354               PtSetResource(i->progress, Pt_ARG_GAUGE_VALUE, percent, 0);
00355        if (i->status)
00356               PtSetResource(i->status, Pt_ARG_TEXT_STRING, message, 0);
00357 
00358        return (Pt_CONTINUE);
00359 }
00360 
00361 int moz_url_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00362 {
00363        int *nflags = NULL;
00364        PtMozillaUrlCb_t *c = (PtMozillaUrlCb_t *) cbinfo->cbdata;
00365        struct window_info *i;
00366        PtGetResource(PtFindDisjoint(widget), Pt_ARG_POINTER, &i, 0);
00367 
00368        // display the url in the entry field
00369        if (i->url)
00370               PtSetResource(i->url, Pt_ARG_TEXT_STRING, c->url, 0);
00371 
00372        /* get the navigation possibilities */
00373        PtGetResource(i->web, Pt_ARG_MOZ_NAVIGATE_PAGE, &nflags, 0 );
00374 
00375        if ( nflags != NULL ) 
00376        {
00377               // disable or enable the forward and back buttons accordingly
00378               if (i->back)
00379               {
00380                      if (*nflags & (1 << Pt_WEB_DIRECTION_BACK))
00381                             PtSetResource(i->back, Pt_ARG_FLAGS, 0, Pt_BLOCKED|Pt_GHOST);
00382                      else
00383                             PtSetResource(i->back, Pt_ARG_FLAGS, ~0, Pt_BLOCKED|Pt_GHOST);
00384               }
00385               if (i->forward)
00386               {
00387                      if (*nflags & (1 << Pt_WEB_DIRECTION_FWD))
00388                             PtSetResource(i->forward, Pt_ARG_FLAGS, 0, Pt_BLOCKED|Pt_GHOST);
00389                      else
00390                             PtSetResource(i->forward, Pt_ARG_FLAGS, ~0, Pt_BLOCKED|Pt_GHOST);
00391               }
00392        }
00393 
00394        return (Pt_CONTINUE);
00395 }
00396 
00397 int moz_net_state_change_cb (PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00398 {
00399 #if 0
00400        PtMozillaNetStateCb_t *c = (PtMozillaNetStateCb_t *) cbinfo->cbdata;
00401        PtGetResource(PtFindDisjoint(widget), Pt_ARG_POINTER, &i, 0);
00402 
00403        i->statusMessage = NULL;
00404 
00405        if (c->flags & STATE_REQUEST) 
00406        {
00407               if (c->flags & STATE_REDIRECTING)
00408                      statusMessage = "Redirecting to site...";
00409               else if (c->flags & STATE_TRANSFERRING)
00410                      statusMessage = "Transferring data from site...";
00411               else if (c->flags & STATE_NEGOTIATING)
00412                      statusMessage = "Waiting for authorization...";
00413        }
00414 
00415        if (c->status == STATEFAILED_DNS)
00416               statusMessage = "Site not found.";
00417        else if (c->status == STATEFAILED_CONNECT)
00418               statusMessage = "Failed to connect to site.";
00419        else if (c->status == STATEFAILED_TIMEOUT)
00420               statusMessage = "Failed due to connection timeout.";
00421        else if (c->status == STATEFAILED_USERCANCELED)
00422               statusMessage = "User canceled connecting to site.";
00423 
00424        if (c->flags & STATE_WINDOW) 
00425        {
00426               if (c->flags & STATE_START)
00427                      statusMessage = "Loading site...";
00428               else if (c->flags & STATE_STOP)
00429                      statusMessage = "Done.";
00430        }
00431 
00432        if (statusMessage)
00433               PtSetResource(status, Pt_ARG_TEXT_STRING, statusMessage, 0);
00434 
00435 #endif
00436        return (Pt_CONTINUE);
00437 }
00438 
00439 int moz_new_window_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00440 {
00441        PtMozillaNewWindowCb_t *c = (PtMozillaNewWindowCb_t *) cbinfo->cbdata;
00442        struct window_info *i;
00443        PtWidget_t *win;
00444 
00445        win = create_browser_window(c->window_flags);
00446        if (win)
00447        {
00448               PtRealizeWidget(win);
00449               PtGetResource(win, Pt_ARG_POINTER, &i, 0);
00450               c->widget = i->web;
00451               return (Pt_CONTINUE);
00452        }
00453 
00454        return (Pt_END);
00455 }
00456 
00457 int moz_visibility_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00458 {
00459        PtMozillaVisibilityCb_t *v = (PtMozillaVisibilityCb_t *) cbinfo->cbdata;
00460        struct window_info *i;
00461 
00462        PtGetResource(PtFindDisjoint(widget), Pt_ARG_POINTER, &i, 0);
00463        if (v->show)
00464               PtRealizeWidget(i->window);
00465        else
00466               PtUnrealizeWidget(i->window);
00467 
00468        return (Pt_CONTINUE);
00469 }
00470 int moz_open_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00471 {
00472               return (Pt_CONTINUE);
00473 }
00474 
00475 int moz_auth_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00476 {
00477        PtMozillaAuthenticateCb_t *a = (PtMozillaAuthenticateCb_t *) cbinfo->cbdata;
00478 
00479        return (CreateAuthWindow(a, PtFindDisjoint(widget)));
00480 }
00481 
00482 int moz_new_area_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00483 {
00484        PtMozillaNewAreaCb_t *r = ( PtMozillaNewAreaCb_t * ) cbinfo->cbdata;
00485 
00486        if ( widget->flags & Pt_DESTROYED )
00487               return Pt_CONTINUE;
00488 
00489        printf("moz_new_area_cb called\n");
00490 
00491        return Pt_CONTINUE;
00492        }
00493 
00494 int moz_dialog_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00495 {
00496        PtMozillaDialogCb_t *d = (PtMozillaDialogCb_t *) cbinfo->cbdata;
00497 
00498        switch (d->type)
00499        {
00500               case Pt_MOZ_DIALOG_ALERT:
00501                      PtAskQuestion(NULL, d->title ? d->title : "JS Alert", (d->text) ? d->text : "Alert Message.", \
00502                             NULL, "OK", NULL, NULL, 1);
00503                      break;
00504               case Pt_MOZ_DIALOG_ALERT_CHECK:
00505                      printf("Alert Check\n");
00506                      printf("\tMessage: %s\n", d->checkbox_message);
00507                      break;
00508               case Pt_MOZ_DIALOG_CONFIRM:
00509                      if (PtAskQuestion(NULL, d->title ? d->title : "JS Confirm", (d->text) ? d->text : "Confirm Message.", \
00510                             NULL, "Yes", "No", NULL, 1) == 1)
00511                      {
00512                             return (Pt_CONTINUE);
00513                      }
00514                      else
00515                      {
00516                             return (Pt_END);
00517                      }
00518                      break;
00519               case Pt_MOZ_DIALOG_CONFIRM_CHECK:
00520                      printf("Confirm Check\n");
00521                      printf("\tMessage: %s\n", d->checkbox_message);
00522                      break;
00523        }
00524 
00525        return (Pt_END);
00526 }
00527 
00528 int moz_prompt_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00529 {
00530        PtMozillaPromptCb_t *p = (PtMozillaPromptCb_t *) cbinfo->cbdata;
00531        int answer;
00532        char *btns[] = { "&Ok", "&Cancel" };
00533        char text[128];
00534        int len = 127;
00535        struct window_info *i;
00536        PtGetResource(PtFindDisjoint(widget), Pt_ARG_POINTER, &i, 0);
00537 
00538        if (p->dflt_resp)
00539               strcpy(text, p->dflt_resp);
00540 
00541        answer = PtPrompt(i->window, NULL, p->title ? p->title : "User Prompt", NULL, (p->text) ? p->text : "Prompt Message.", \
00542                             NULL, 2, (char const **)btns, NULL, 1, 2, len, text, NULL, NULL, 0 );
00543 
00544        switch( answer ) 
00545        {
00546               case 1: // ok
00547                      strcpy(p->response, text);
00548                      return (Pt_CONTINUE);
00549                      break;
00550               case 2: // cancel
00551               default:
00552                      return (Pt_END);
00553                      break;
00554        }
00555 
00556        return (Pt_END);
00557 }
00558 
00559 int moz_destroy_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00560 {
00561        PtDestroyWidget(PtFindDisjoint(widget));
00562        return (Pt_CONTINUE);
00563 }
00564 
00565 int moz_context_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00566 {
00567        PtMozillaContextCb_t *c = (PtMozillaContextCb_t *) cbinfo->cbdata;
00568 
00569        printf("Context Callback:\n");
00570        if (c->flags & Pt_MOZ_CONTEXT_LINK)
00571               printf("\tLINK\n");
00572        if (c->flags & Pt_MOZ_CONTEXT_IMAGE)
00573               printf("\tIMAGE\n");
00574        if (c->flags & Pt_MOZ_CONTEXT_DOCUMENT)
00575               printf("\tDOCUMENT\n");
00576        if (c->flags & Pt_MOZ_CONTEXT_TEXT)
00577               printf("\tTEXT\n");
00578        if (c->flags & Pt_MOZ_CONTEXT_INPUT)
00579               printf("\tINPUT\n");
00580 
00581        return (Pt_CONTINUE);
00582 }
00583 
00584 int moz_print_status_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00585 {
00586        PtMozillaPrintStatusCb_t *c = (PtMozillaPrintStatusCb_t *) cbinfo->cbdata;
00587 
00588        switch (c->status)
00589        {
00590               case Pt_MOZ_PRINT_START:
00591                      printf("Printing: start\n");
00592                      break;
00593               case Pt_MOZ_PRINT_COMPLETE:
00594                      printf("Printing: complete\n");
00595                      break;
00596               case Pt_MOZ_PRINT_PROGRESS:
00597                      printf("Printing: progress %d-%d\n", c->cur, c->max);
00598                      break;
00599        }
00600 
00601        return (Pt_CONTINUE);
00602 }
00603 
00604 int moz_various_cb( PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo ) {
00605 
00606        printf("moz_various_cb called with reason %ld\n", cbinfo->reason);
00607        switch( cbinfo->reason ) {
00608 
00609               case Pt_CB_MOZ_WEB_DATA_REQ: {
00610                      PtMozillaDataRequestCb_t *cb = ( PtMozillaDataRequestCb_t * ) cbinfo->cbdata;
00611                      //PtWebReportClientData( swidget, cb->type, cb->length, cb->url );
00612                      }
00613                      break;
00614 
00615               case Pt_CB_MOZ_ERROR: {
00616                      PtWebErrorCallback_t *cb = ( PtWebErrorCallback_t * ) cbinfo->cbdata;
00617                      //PtWebReportError( swidget, cb->type, cb->reason, cb->url, cb->description );
00618                      }
00619               break;
00620               }
00621        return Pt_CONTINUE;
00622        }
00623 
00624 int moz_unknown_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00625 {
00626        PtWebUnknownWithNameCallback_t *cb = ( PtWebUnknownWithNameCallback_t * ) cbinfo->cbdata;
00627        PtWebClient2UnknownData_t resp;
00628        PtMozillaUnknownCtrl_t *moz_unknown_ctrl;
00629        int answer;
00630        char *btns[] = { "&Ok", "&Cancel" };
00631        char text[PATH_MAX+1], *env;
00632        struct window_info *i;
00633        struct timespec time;
00634 
00635        PtGetResource(PtFindDisjoint(widget), Pt_ARG_POINTER, &i, 0);
00636 
00637        printf("moz_unknown_cb called, content type %s, url %s, suggested filename %s\n", cb->content_type, cb->url, cb->suggested_filename);
00638 
00639        //
00640        // Make sure the PtMozilla widget has a moz_unknown_ctrl structure allocated
00641        //
00642        PtGetResource( widget, Pt_ARG_MOZ_UNKNOWN_CTRL, &moz_unknown_ctrl, 0 );
00643        if( !moz_unknown_ctrl ) {
00644               moz_unknown_ctrl = ( PtMozillaUnknownCtrl_t * ) calloc( 1, sizeof( *moz_unknown_ctrl ) );
00645               PtSetResource( widget, Pt_ARG_MOZ_UNKNOWN_CTRL, moz_unknown_ctrl, 0 );
00646        }
00647 
00648        //
00649        // Prompt the user.
00650        //
00651        env = getenv("HOME");
00652        sprintf(text, "%s/%s", env ? env:"", cb->suggested_filename);
00653 
00654        answer = PtPrompt(i->window, NULL, "Download", NULL,
00655                             "Confirm the location of the file to be downloaded", NULL, 2,
00656                             (char const **)btns, NULL, 1, 2, sizeof(text)-1, text, NULL,
00657                             NULL, 0 );
00658        switch( answer ) 
00659        {
00660               //
00661               // This is where the response from the UI determines whether the
00662               // download will be performed or not.
00663               //
00664               case 1: // ok
00665                      moz_unknown_ctrl->response = Pt_WEB_RESPONSE_OK;
00666                      free(moz_unknown_ctrl->filename);
00667                      moz_unknown_ctrl->filename = strdup(text);
00668                      clock_gettime( CLOCK_REALTIME, &time);
00669                      moz_unknown_ctrl->download_ticket = time.tv_sec;
00670                      break;
00671               case 2: // cancel
00672               default:
00673                      moz_unknown_ctrl->response = Pt_WEB_RESPONSE_CANCEL;
00674                      free(moz_unknown_ctrl->filename);
00675                      moz_unknown_ctrl->filename = strdup("");
00676                      moz_unknown_ctrl->download_ticket = 0;
00677                      break;
00678        }
00679        
00680 #if 0
00681        //
00682        // The only thing implemented for this resource right now is the ability
00683        // to cancel a current download. This code needs to be moved.
00684        //
00685        resp.response = moz_unknown_ctrl->response;
00686        resp.filename = moz_unknown_ctrl->filename;
00687        resp.download_ticket = moz_unknown_ctrl->download_ticket;
00688        PtSetResource( widget, Pt_ARG_MOZ_UNKNOWN_RESP, &resp, NULL );
00689 #endif
00690 
00691        return (Pt_CONTINUE);
00692 }
00693 
00694 int moz_download_cb(PtWidget_t *widget, void *data, PtCallbackInfo_t *cbinfo)
00695 {
00696        PtWebDownloadCallback_t *cb = ( PtWebDownloadCallback_t * ) cbinfo->cbdata;
00697 
00698        printf("moz_download_cb called, received %d of %d\n", cb->current, cb->total);
00699        //
00700        // Here we can report to the UI on the download's progress
00701        //
00702        //PtWebReportDownload( swidget, cb->url, cb->download_ticket, cb->type, cb->current, cb->total, cb->message );
00703 
00704        return (Pt_CONTINUE);
00705 }
00707 
00708 
00710 PtWidget_t *create_browser_window(unsigned window_flags)
00711 {
00712        int n = 0;
00713        PtArg_t args[10];
00714        PhDim_t       win_dim = {750, 700};
00715        PhDim_t       dim = {0, 0}, btn_dim = {70, 25};
00716        PhArea_t area = {{0, 0}, {70, 25}};
00717        struct window_info   *info = (struct window_info *) calloc (sizeof(struct window_info), 1);
00718        PtWidget_t *w, *container;
00719        int render = 0;
00720 
00721        // main window creation
00722        PtSetArg(&args[n++], Pt_ARG_TITLE, "PtMozilla Test\n", 0);
00723        PtSetArg(&args[n++], Pt_ARG_DIM, &win_dim, 0);
00724        PtSetArg(&args[n++], Pt_ARG_POINTER, info, 0);
00725 
00726        if (window_flags & Pt_MOZ_FLAG_WINDOWBORDERSON)
00727               render |= Ph_WM_RENDER_BORDER;
00728        if (window_flags & Pt_MOZ_FLAG_WINDOWCLOSEON)
00729               render |= Ph_WM_RENDER_CLOSE;
00730        if (window_flags & Pt_MOZ_FLAG_WINDOWRESIZEON)
00731               render |= Ph_WM_RENDER_RESIZE | Ph_WM_RENDER_MAX;
00732        if (window_flags & Pt_MOZ_FLAG_TITLEBARON)
00733               render |= Ph_WM_RENDER_TITLE;
00734 
00735        PtSetArg(&args[n++], Pt_ARG_WINDOW_RENDER_FLAGS, ~0, render);
00736        info->window = PtCreateWidget(PtWindow, NULL, n, args);
00737        PtAddCallback(info->window, Pt_CB_WINDOW_CLOSING, window_close_cb, NULL);
00738 
00739        if (window_flags & Pt_MOZ_FLAG_TOOLBARON)
00740        {
00741               // Button toolbar
00742               n = 0;
00743               area.size.w = win_dim.w;
00744               area.size.h = btn_dim.h + 4;
00745               PtSetArg(&args[n++], Pt_ARG_AREA, &area, 0);
00746               PtSetArg(&args[n++], Pt_ARG_ANCHOR_FLAGS, ~0, Pt_RIGHT_ANCHORED_RIGHT|Pt_LEFT_ANCHORED_LEFT|Pt_TOP_ANCHORED_TOP);
00747               container = PtCreateWidget(PtToolbar, info->window, n, args);
00748               
00749               // back button
00750               n = 0;
00751               PtSetArg(&args[n++], Pt_ARG_DIM, &btn_dim, 0);
00752               PtSetArg(&args[n++], Pt_ARG_TEXT_STRING, "Back", 0);
00753               info->back = PtCreateWidget(PtButton, container, n, args);
00754               PtAddCallback(info->back, Pt_CB_ACTIVATE, back_cb, NULL);
00755 
00756               // forward button
00757               n = 0;
00758               area.pos.x += 71;
00759               PtSetArg(&args[n++], Pt_ARG_DIM, &btn_dim, 0);
00760               PtSetArg(&args[n++], Pt_ARG_TEXT_STRING, "Forward", 0);
00761               info->forward = PtCreateWidget(PtButton, container, n, args);
00762               PtAddCallback(info->forward, Pt_CB_ACTIVATE, forward_cb, NULL);
00763 
00764               // reload button
00765               n = 0;
00766               area.pos.x += 71;
00767               PtSetArg(&args[n++], Pt_ARG_DIM, &btn_dim, 0);
00768               PtSetArg(&args[n++], Pt_ARG_TEXT_STRING, "Reload", 0);
00769               info->reload = PtCreateWidget(PtButton, container, n, args);
00770               PtAddCallback(info->reload, Pt_CB_ACTIVATE, reload_cb, NULL);
00771 
00772               // stop button
00773               n = 0;
00774               area.pos.x += 71;
00775               PtSetArg(&args[n++], Pt_ARG_DIM, &btn_dim, 0);
00776               PtSetArg(&args[n++], Pt_ARG_TEXT_STRING, "Stop", 0);
00777               info->stop = PtCreateWidget(PtButton, container, n, args);
00778               PtAddCallback(info->stop, Pt_CB_ACTIVATE, stop_cb, NULL);
00779 
00780               // print button
00781               n = 0;
00782               area.pos.x += 71;
00783               PtSetArg(&args[n++], Pt_ARG_DIM, &btn_dim, 0);
00784               PtSetArg(&args[n++], Pt_ARG_TEXT_STRING, "Print", 0);
00785               info->print = PtCreateWidget(PtButton, container, n, args);
00786               PtAddCallback(info->print, Pt_CB_ACTIVATE, print_cb, NULL);
00787 
00788               // save as button
00789               n = 0;
00790               area.pos.x += 71;
00791               PtSetArg(&args[n++], Pt_ARG_DIM, &btn_dim, 0);
00792               PtSetArg(&args[n++], Pt_ARG_TEXT_STRING, "Save", 0);
00793               info->save = PtCreateWidget(PtButton, container, n, args);
00794               PtAddCallback(info->save, Pt_CB_ACTIVATE, save_cb, NULL);
00795        
00796               PtExtentWidget (container);
00797               PtWidgetDim(container, &dim);      
00798               area.pos.y += dim.h + 1;
00799        }
00800 
00801        if (window_flags & Pt_MOZ_FLAG_LOCATIONBARON)
00802        {
00803               // Location toolbar
00804               n = 0;
00805               area.pos.x = 0;
00806               area.size.w = win_dim.w;
00807               area.size.h = btn_dim.h + 4;
00808               PtSetArg(&args[n++], Pt_ARG_AREA, &area, 0);
00809               PtSetArg(&args[n++], Pt_ARG_ANCHOR_FLAGS, ~0, Pt_RIGHT_ANCHORED_RIGHT|Pt_LEFT_ANCHORED_LEFT|Pt_TOP_ANCHORED_TOP);
00810               container = PtCreateWidget(PtToolbar, info->window, n, args);
00811               
00812               n = 0;
00813               PtSetArg(&args[n++], Pt_ARG_TEXT_STRING, "URL:", 0);
00814               w = PtCreateWidget(PtLabel, container, n, args);
00815 
00816               PtExtentWidget (w);
00817               PtWidgetDim(w, &dim);
00818 
00819               // url entry field
00820               n = 0;
00821               area.size.w = win_dim.w - 100;
00822               area.size.h = 22;
00823               PtSetArg(&args[n++], Pt_ARG_DIM, &area.size, 0);
00824               PtSetArg(&args[n++], Pt_ARG_ANCHOR_FLAGS, ~0, Pt_RIGHT_ANCHORED_RIGHT|Pt_LEFT_ANCHORED_LEFT|Pt_TOP_ANCHORED_TOP);
00825               info->url = PtCreateWidget(PtText, container, n, args);
00826               PtAddCallback(info->url, Pt_CB_ACTIVATE, load_url_cb, NULL);
00827 
00828               PtExtentWidget(container);
00829               PtWidgetDim(container, &dim);      
00830               area.pos.y += dim.h + 1;
00831        }
00832 
00833        // PtMozilla container and widget
00834        n = 0;
00835        area.pos.x = 0;
00836        area.size.w = win_dim.w;
00837        area.size.h = win_dim.h - area.pos.y - btn_dim.h - 5; 
00838        PtSetArg(&args[n++], Pt_ARG_AREA, &area, 0);
00839        PtSetArg(&args[n++], Pt_ARG_ANCHOR_FLAGS, ~0, Pt_RIGHT_ANCHORED_RIGHT | Pt_LEFT_ANCHORED_LEFT | Pt_TOP_ANCHORED_TOP | Pt_BOTTOM_ANCHORED_BOTTOM);
00840        PtSetArg(&args[n++], Pt_ARG_POINTER, info, 0);
00841        container = PtCreateWidget(PtContainer, info->window, n, args);
00842 
00843        n = 0;
00844        area.pos.x = 0;
00845        area.pos.y = 0;
00846        PtSetArg(&args[n++], Pt_ARG_AREA, &area, 0);
00847        PtSetArg(&args[n++], Pt_ARG_ANCHOR_FLAGS, ~0, Pt_RIGHT_ANCHORED_RIGHT | Pt_LEFT_ANCHORED_LEFT | Pt_TOP_ANCHORED_TOP | Pt_BOTTOM_ANCHORED_BOTTOM);
00848        info->web = PtCreateWidget(PtMozilla, container, n, args);
00849        if (!info->web)
00850        {
00851               printf("*** ERROR: PtMozilla widget could not be created.\n");
00852               exit(-1);
00853        }
00854 
00855        PtExtentWidget (container);
00856        PtWidgetArea(container, &area);    
00857        area.pos.y += area.size.h + 1;
00858 
00859        if (window_flags & Pt_MOZ_FLAG_STATUSBARON)
00860        {
00861               // Status toolbar
00862               n = 0;
00863               area.pos.x = 0;
00864               area.size.w = win_dim.w;
00865               area.size.h = btn_dim.h + 3;
00866               PtSetArg(&args[n++], Pt_ARG_AREA, &area, 0);
00867               PtSetArg(&args[n++], Pt_ARG_TOOLBAR_FLAGS, Pt_TOOLBAR_END_SEPARATOR|Pt_TOOLBAR_REVERSE_LAST_ITEM, Pt_TOOLBAR_END_SEPARATOR|Pt_TOOLBAR_REVERSE_LAST_ITEM|Pt_TOOLBAR_DRAGGABLE);
00868               PtSetArg(&args[n++], Pt_ARG_ANCHOR_FLAGS, ~0, Pt_RIGHT_ANCHORED_RIGHT|Pt_LEFT_ANCHORED_LEFT|Pt_TOP_ANCHORED_TOP);
00869               container = PtCreateWidget(PtToolbar, info->window, n, args);
00870 
00871               // status field
00872               n = 0;
00873               area.pos.x = 0;
00874               area.size.w = win_dim.w - 100;
00875               area.size.h = 22;
00876               PtSetArg(&args[n++], Pt_ARG_DIM, &area.size, 0);
00877               PtSetArg(&args[n++], Pt_ARG_FLAGS, Pt_HIGHLIGHTED, Pt_HIGHLIGHTED);
00878               PtSetArg(&args[n++], Pt_ARG_ANCHOR_FLAGS, ~0, Pt_RIGHT_ANCHORED_RIGHT|Pt_LEFT_ANCHORED_LEFT| \
00879                             Pt_TOP_ANCHORED_BOTTOM);
00880               info->status = PtCreateWidget(PtLabel, container, n, args);
00881        
00882               // progress bar
00883               n = 0;
00884               area.size.w = 75;
00885               PtSetArg(&args[n++], Pt_ARG_DIM, &area.size, 0);
00886               PtSetArg(&args[n++], Pt_ARG_ANCHOR_FLAGS, ~0, Pt_RIGHT_ANCHORED_RIGHT|Pt_LEFT_ANCHORED_RIGHT| \
00887                             Pt_TOP_ANCHORED_BOTTOM);
00888               PtSetArg( &args[n++], Pt_ARG_GAUGE_FLAGS, Pt_GAUGE_LIVE, Pt_GAUGE_LIVE );
00889               info->progress = PtCreateWidget(PtProgress, container, n, args);
00890        }
00891 
00892        PtAddCallback(info->web, Pt_CB_MOZ_PROGRESS,            moz_progress_cb, NULL);
00893     PtAddCallback(info->web, Pt_CB_MOZ_START,           moz_start_cb, NULL);
00894     PtAddCallback(info->web, Pt_CB_MOZ_COMPLETE,               moz_complete_cb, NULL);
00895     PtAddCallback(info->web, Pt_CB_MOZ_URL,                    moz_url_cb, NULL);
00896     PtAddCallback(info->web, Pt_CB_MOZ_OPEN,                   moz_open_cb, NULL);
00897     PtAddCallback(info->web, Pt_CB_MOZ_INFO,                   moz_info_cb, NULL);
00898     PtAddCallback(info->web, Pt_CB_MOZ_NET_STATE,       moz_net_state_change_cb, NULL);
00899     PtAddCallback(info->web, Pt_CB_MOZ_AUTHENTICATE,    moz_auth_cb, NULL);
00900     PtAddCallback(info->web, Pt_CB_MOZ_DIALOG,          moz_dialog_cb, NULL);
00901     PtAddCallback(info->web, Pt_CB_MOZ_PROMPT,          moz_prompt_cb, NULL);
00902     PtAddCallback(info->web, Pt_CB_MOZ_NEW_WINDOW,             moz_new_window_cb, NULL);
00903        PtAddCallback(info->web, Pt_CB_MOZ_NEW_AREA,      moz_new_area_cb, NULL);
00904     PtAddCallback(info->web, Pt_CB_MOZ_VISIBILITY,             moz_visibility_cb, NULL);
00905     PtAddCallback(info->web, Pt_CB_MOZ_DESTROY,         moz_destroy_cb, NULL);
00906     PtAddCallback(info->web, Pt_CB_MOZ_CONTEXT,         moz_context_cb, NULL);
00907     PtAddCallback(info->web, Pt_CB_MOZ_PRINT_STATUS,    moz_print_status_cb, NULL);
00908        PtAddCallback(info->web, Pt_CB_MOZ_WEB_DATA_REQ,  moz_various_cb, NULL);
00909        PtAddCallback(info->web, Pt_CB_MOZ_UNKNOWN,             moz_unknown_cb, NULL);
00910        PtAddCallback(info->web, Pt_CB_MOZ_ERROR,                             moz_various_cb, NULL);
00911        PtAddCallback(info->web, Pt_CB_MOZ_DOWNLOAD,     moz_download_cb, NULL);
00912        
00913        window_count++;
00914 
00915        return (info->window);
00916 }
00918 
00919 
00920 int
00921 main(int argc, char **argv)
00922 {
00923        unsigned window_flags = ~0;
00924        PtWidget_t *win;
00925        struct window_info *i;
00926        char *argv0 = strdup(argv[0]);
00927        PhChannelParms_t cparms;
00928 
00929        memset( &cparms, 0, sizeof( cparms ) );
00930        cparms.flags = Ph_NO_HOLD | Ph_DYNAMIC_BUFFER;
00931        PhAttach( NULL, &cparms );
00932        if ( PtInit( NULL ) == -1 ) 
00933        {
00934               fprintf(stderr,"Unable to Connect to Photon (%s)\n",strerror(errno));
00935               exit(EXIT_FAILURE);
00936        }
00937 
00938        /*
00939         * Set MOZILLA_FIVE_HOME if it is not already set.
00940         */
00941        if (!getenv("MOZILLA_FIVE_HOME"))
00942               setenv("MOZILLA_FIVE_HOME", dirname(argv0), 0);
00943        free(argv0);
00944        win = create_browser_window(window_flags);
00945        PtGetResource(win, Pt_ARG_POINTER, &i, 0);
00946 
00947        PtRealizeWidget(i->window);
00948        PtSetResource(i->web, Pt_ARG_MOZ_GET_URL, "www.qnx.com", 0);
00949 
00950        PtMainLoop();
00951 }