home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libimg / src / dummy_nc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.4 KB  |  210 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /* This is a dummy Net Context which the Image Library uses for network
  20.    operations in lieu of an MWContext.  It will be replaced by a true
  21.    Net Context when the Network Library is modularized. */
  22.  
  23. #include "dummy_nc.h"
  24. #include "il_strm.h"
  25. #include "if.h"
  26.  
  27. typedef struct dum_TitleObsClosure {
  28.     MWContext *context;
  29.     XP_ObserverList obs_list;
  30. } dum_TitleObsClosure;
  31.  
  32. extern void
  33. lo_view_title( MWContext *context, char *title_str );
  34.  
  35. extern PRBool
  36. il_load_image(OPAQUE_CONTEXT *cx, char *image_url, NET_ReloadMethod cache_reload_policy);
  37.  
  38.  
  39. IL_NetContext *
  40. IL_NewDummyNetContext(MWContext *context, NET_ReloadMethod cache_reload_policy)
  41. {
  42.     IL_NetContext *net_cx;
  43.  
  44.     net_cx = XP_NEW_ZAP(IL_NetContext);
  45.     if (!net_cx)
  46.         return NULL;
  47.  
  48.     net_cx->cx = context;
  49.     net_cx->cache_reload_policy = cache_reload_policy;
  50.  
  51.     return net_cx;
  52. }
  53.  
  54. void
  55. IL_DestroyDummyNetContext(IL_NetContext *net_cx)
  56. {
  57.     XP_FREE(net_cx);
  58. }
  59.  
  60. IL_NetContext *
  61. IL_CloneDummyNetContext(IL_NetContext *net_cx)
  62. {
  63.     return IL_NewDummyNetContext((MWContext *)net_cx->cx,
  64.                                  net_cx->cache_reload_policy);
  65. }
  66.  
  67. #include "shist.h"              /* For use with IL_AddReferer */
  68. #include "structs.h"            /* For use with IL_AddReferer */
  69.  
  70. void
  71. IL_AddReferer(IL_NetContext *net_cx, URL_Struct *urls) 
  72. {
  73.     MWContext *cx = net_cx->cx;
  74.     History_entry *he = SHIST_GetCurrent (&cx->hist);
  75.  
  76.     if (urls->referer)
  77.         XP_FREE (urls->referer);
  78.     if (he && he->address)
  79.         urls->referer = XP_STRDUP (he->origin_url 
  80.                                    ? he->origin_url 
  81.                                    : he->address);
  82.     else
  83.         urls->referer = 0;
  84. }
  85.  
  86. void
  87. dum_TitleObserver(XP_Observable observerable, XP_ObservableMsg msg,
  88.                   void *message_data, void *closure){
  89.  
  90.     IL_MessageData *data = (IL_MessageData*)message_data;
  91.     MWContext *context; 
  92.     dum_TitleObsClosure *title_obs_closure = (dum_TitleObsClosure *)closure;    
  93.     context = (MWContext *)title_obs_closure->context;
  94.  
  95.     switch(msg){
  96.         case IL_DESCRIPTION:
  97.             lo_view_title(context, data->description);
  98.             break;
  99.  
  100.         case IL_IMAGE_DESTROYED:
  101.             /* Remove ourself from the observer callback list. */
  102.             XP_RemoveObserver(title_obs_closure->obs_list, dum_TitleObserver,
  103.                               title_obs_closure);
  104.             XP_FREE(title_obs_closure);
  105.             break;
  106.  
  107.         default:        
  108.             break;
  109.     }
  110.     return;
  111. }
  112.  
  113.  
  114. XP_ObserverList
  115. dum_NewTitleObserverList(MWContext *context)
  116. {
  117.     XP_ObserverList title_obs_list; /* List of observer callbacks. */
  118.     dum_TitleObsClosure *title_obs_closure; /* Closure data to be passed back
  119.                                               to lo_ImageObserver. */
  120.     NS_Error status;
  121.  
  122.     /* Create an XP_ObserverList for the title */
  123.     status = XP_NewObserverList(NULL, &title_obs_list);
  124.     if (status < 0) {        
  125.         return NULL;
  126.     }
  127.  
  128.     /* Closure data for the image observer. */
  129.     title_obs_closure = XP_NEW_ZAP(dum_TitleObsClosure);
  130.     if (!title_obs_closure) {
  131.         XP_DisposeObserverList(title_obs_list);
  132.         return NULL;
  133.     }
  134.     title_obs_closure->context = context;
  135.     title_obs_closure->obs_list = title_obs_list;
  136.  
  137.     /* Add the layout image observer to the observer list. */
  138.     status = XP_AddObserver(title_obs_list, dum_TitleObserver, title_obs_closure);
  139.     if (status < 0) {      
  140.         XP_DisposeObserverList(title_obs_list);
  141.         XP_RemoveObserver(title_obs_closure->obs_list, dum_TitleObserver,
  142.             title_obs_closure );
  143.         XP_FREE(title_obs_closure);
  144.         return NULL;
  145.     }
  146.  
  147.     return title_obs_list;
  148. }
  149.  
  150.  
  151.  
  152. /* This function only handles view_images. Note that the title observer
  153. created is only used to set the image dimensions and type in the 
  154. title bar. */
  155.  
  156. PRBool
  157. il_load_image(MWContext *cx, char *image_url, NET_ReloadMethod cache_reload_policy)
  158. {
  159.     PRBool ret_val = PR_TRUE;
  160.     XP_ObserverList obs_list;
  161.     IL_NetContext *net_cx;
  162.     IL_IRGB *trans_pixel;
  163.     IL_ImageReq *image_req;
  164.  
  165.  
  166.     /* Create a new observer list for the image's title.  The destruction of this
  167.        list is managed by the image library once we call IL_GetImage. */
  168.     obs_list = dum_NewTitleObserverList(cx);
  169.     if (!obs_list)
  170.         return PR_FALSE;    
  171.  
  172.     net_cx = IL_NewDummyNetContext(cx, cache_reload_policy);
  173.     if (!net_cx) {
  174.         XP_DisposeObserverList(obs_list);
  175.         return PR_FALSE;
  176.     }
  177.  
  178.     /* Determine whether to request a mask if this is a transparent image.
  179.        In the case of a document backdrop, we ask the image library to fill
  180.        in the transparent area with a solid color.  For all other transparent
  181.        images, we force the creation of a mask by passing in NULL. */
  182.     if (cx->type == MWContextPostScript) {
  183.         trans_pixel = XP_NEW_ZAP(IL_IRGB);
  184.         if (trans_pixel)
  185.             trans_pixel->red = trans_pixel->green = trans_pixel->blue = 0xff;
  186.     }
  187.     else if (cx->type == MWContextPrint) {
  188.         trans_pixel = cx->transparent_pixel;
  189.     }
  190.     else {
  191.         trans_pixel = NULL;
  192.     }
  193.  
  194.     /* Fetch the image. */
  195.     image_req = IL_GetImage(image_url, cx->img_cx, obs_list, trans_pixel, 0, 0, 0, net_cx);
  196.     if (!image_req) {
  197.         ret_val = PR_FALSE;
  198.     }
  199.  
  200.     /* Destroy the transparent pixel if this is a PostScript context. */
  201.     if ((cx->type == MWContextPostScript) && trans_pixel)
  202.         XP_FREE(trans_pixel);
  203.  
  204.     /* The Image Library clones the dummy Net Context, so it safe to destroy
  205.        it. */
  206.     IL_DestroyDummyNetContext(net_cx);
  207.  
  208.     return ret_val;
  209. }
  210.