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

  1. /* -*- Mode: C++; tab-width: 8; 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.  
  20. /*
  21.  * JS event class.
  22.  */
  23. #include "lm.h"
  24. #include "lo_ele.h"
  25. #include "pa_tags.h"
  26. #include "xp.h"
  27. #include "prtypes.h"
  28.  
  29. typedef enum JSBarType {
  30.     JSBAR_MENU, JSBAR_TOOL, JSBAR_LOCATION, JSBAR_PERSONAL, JSBAR_STATUS,
  31.     JSBAR_SCROLL
  32. } JSBarType;
  33.  
  34. const char *barnames[] = {
  35.     "menubar", "toolbar", "locationbar", "personalbar", "statusbar",
  36.     "scrollbars"
  37. };
  38.  
  39. typedef struct JSBar {
  40.     MochaDecoder    *decoder;
  41.     JSBarType       bartype;
  42. } JSBar;
  43.  
  44. enum bar_tinyid {
  45.     BAR_VISIBLE        = -1
  46. };
  47.  
  48. static JSPropertySpec bar_props[] = {
  49.     {"visible"        , BAR_VISIBLE,        JSPROP_ENUMERATE},
  50.     {0}
  51. };
  52.  
  53. extern JSClass lm_bar_class;
  54.  
  55. PR_STATIC_CALLBACK(JSBool)
  56. bar_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
  57. {
  58.     JSBar * bar;
  59.     MWContext *context;
  60.     Chrome *chrome;
  61.  
  62.     if (!JSVAL_IS_INT(id))
  63.     return JS_TRUE;
  64.  
  65.     if (JSVAL_TO_INT(id) != BAR_VISIBLE)
  66.     return JS_TRUE;
  67.  
  68.     bar = JS_GetInstancePrivate(cx, obj, &lm_bar_class, NULL);
  69.     if (!bar)
  70.     return JS_TRUE;
  71.     context = bar->decoder->window_context;
  72.     if (!context)
  73.     return JS_TRUE;
  74.  
  75.     chrome = XP_NEW_ZAP(Chrome);
  76.     if (!chrome) {
  77.     JS_ReportOutOfMemory(cx);
  78.     return JS_FALSE;
  79.     }
  80.     ET_PostQueryChrome(context, chrome);
  81.  
  82.     switch (bar->bartype) {
  83.       case JSBAR_MENU:
  84.     *vp = BOOLEAN_TO_JSVAL(chrome->show_menu);
  85.     break;
  86.       case JSBAR_TOOL:
  87.     *vp = BOOLEAN_TO_JSVAL(chrome->show_button_bar);
  88.     break;
  89.       case JSBAR_LOCATION:
  90.     *vp = BOOLEAN_TO_JSVAL(chrome->show_url_bar);
  91.     break;
  92.       case JSBAR_PERSONAL:
  93.     *vp = BOOLEAN_TO_JSVAL(chrome->show_directory_buttons);
  94.     break;
  95.       case JSBAR_STATUS:
  96.     *vp = BOOLEAN_TO_JSVAL(chrome->show_bottom_status_bar);
  97.     break;
  98.       case JSBAR_SCROLL:
  99.     *vp = BOOLEAN_TO_JSVAL(chrome->show_scrollbar);
  100.     break;
  101.     }
  102.     XP_FREE(chrome);
  103.     return JS_TRUE;
  104. }
  105.  
  106. PR_STATIC_CALLBACK(JSBool)
  107. bar_setProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
  108. {
  109.     JSBar * bar;
  110.     MWContext *context;
  111.     JSBool show;
  112.     Chrome *chrome;
  113.  
  114.     if (!lm_CanAccessTarget(cx, JSTARGET_UNIVERSAL_BROWSER_WRITE))
  115.     return JS_TRUE;
  116.     
  117.     if (!JSVAL_IS_INT(id))
  118.     return JS_TRUE;
  119.  
  120.     if (JSVAL_TO_INT(id) != BAR_VISIBLE)
  121.     return JS_TRUE;
  122.  
  123.     bar = JS_GetInstancePrivate(cx, obj, &lm_bar_class, NULL);
  124.     if (!bar)
  125.     return JS_TRUE;
  126.     context = bar->decoder->window_context;
  127.     if (!context)
  128.     return JS_TRUE;
  129.  
  130.     if (!JS_ValueToBoolean(cx, *vp, &show))
  131.         return JS_FALSE;
  132.  
  133.     chrome = XP_NEW_ZAP(Chrome);
  134.     if (!chrome) {
  135.     JS_ReportOutOfMemory(cx);
  136.     return JS_FALSE;
  137.     }
  138.     ET_PostQueryChrome(context, chrome);
  139.  
  140.     switch (bar->bartype) {
  141.       case JSBAR_MENU:
  142.     chrome->show_menu = show;
  143.     if (show)
  144.         chrome->disable_commands = FALSE;
  145.     break;
  146.       case JSBAR_TOOL:
  147.     chrome->show_button_bar = show;
  148.     break;
  149.       case JSBAR_LOCATION:
  150.     chrome->show_url_bar = show;
  151.     break;
  152.       case JSBAR_PERSONAL:
  153.     chrome->show_directory_buttons = show;
  154.     break;
  155.       case JSBAR_STATUS:
  156.     chrome->show_bottom_status_bar = show;
  157.     break;
  158.       case JSBAR_SCROLL:
  159.     chrome->show_scrollbar = show;
  160.     break;
  161.     }
  162.  
  163.     ET_PostUpdateChrome(context, chrome);
  164.     XP_FREE(chrome);
  165.     return JS_TRUE;
  166. }
  167.  
  168. PR_STATIC_CALLBACK(void)
  169. bar_finalize(JSContext *cx, JSObject *obj)
  170. {
  171.     JSBar *bar;
  172.  
  173.     bar = JS_GetPrivate(cx, obj);
  174.     if (!bar)
  175.     return;
  176.     DROP_BACK_COUNT(bar->decoder);
  177.     JS_free(cx, bar);
  178. }
  179.  
  180. JSClass lm_bar_class = {
  181.     "Bar", JSCLASS_HAS_PRIVATE,
  182.     JS_PropertyStub,  JS_PropertyStub,    bar_getProperty,     bar_setProperty,
  183.     JS_EnumerateStub, JS_ResolveStub,     JS_ConvertStub,      bar_finalize
  184. };
  185.  
  186. PR_STATIC_CALLBACK(JSBool)
  187. Bar(JSContext *cx, JSObject *obj, uint argc, jsval *argv, jsval *rval)
  188. {
  189.     return JS_TRUE;
  190. }
  191.  
  192. static JSBool
  193. define_bar(JSContext *cx, MochaDecoder *decoder, JSBarType type)
  194. {
  195.     JSBar *bar;
  196.     JSObject *obj;
  197.  
  198.     if (!(bar = JS_malloc(cx, sizeof(JSBar))))
  199.     return JS_FALSE;
  200.  
  201.     obj = JS_DefineObject(cx, decoder->window_object, barnames[type],
  202.               &lm_bar_class, decoder->bar_prototype,
  203.               JSPROP_ENUMERATE|JSPROP_READONLY);
  204.     if (!obj || !JS_SetPrivate(cx, obj, bar)) {
  205.     JS_free(cx, bar);
  206.     return JS_FALSE;
  207.     }
  208.  
  209.     bar->decoder = HOLD_BACK_COUNT(decoder);
  210.     bar->bartype = type;
  211.     return JS_TRUE;
  212. }
  213.  
  214. JSBool
  215. lm_DefineBarClasses(MochaDecoder *decoder)
  216. {
  217.     JSContext *cx;
  218.     JSObject *prototype;
  219.  
  220.     cx = decoder->js_context;
  221.     prototype = JS_InitClass(cx, decoder->window_object, NULL, &lm_bar_class,
  222.                  Bar, 0, bar_props, NULL, NULL, NULL);
  223.     if (!prototype)
  224.     return JS_FALSE;
  225.     decoder->bar_prototype = prototype;
  226.     return JS_TRUE;
  227. }
  228.  
  229. JSBool
  230. lm_ResolveBar(JSContext *cx, MochaDecoder *decoder, const char *name)
  231. {
  232. #define FROB0(BAR, EXTRA) {                                                   \
  233.     if (XP_STRCMP(name, barnames[BAR]) == 0) {                                \
  234.     if (!define_bar(cx, decoder, BAR))                                    \
  235.         return JS_FALSE;                                                  \
  236.     EXTRA;                                                                \
  237.     return JS_TRUE;                                                       \
  238.     }                                                                         \
  239. }
  240. #define FROB(BAR)           FROB0(BAR, (void)0)
  241. #define FROB2(BAR, EXTRA)   FROB0(BAR, EXTRA)
  242.  
  243.     FROB(JSBAR_MENU);
  244.     FROB(JSBAR_TOOL);
  245.     FROB(JSBAR_LOCATION);
  246.     FROB2(JSBAR_PERSONAL,
  247.     if (!JS_AliasProperty(cx, decoder->window_object,
  248.                   barnames[JSBAR_PERSONAL],
  249.                   "directories")) {
  250.         return JS_FALSE;
  251.     })
  252.     FROB(JSBAR_STATUS);
  253.     FROB(JSBAR_SCROLL);
  254.  
  255.     return JS_TRUE;
  256.  
  257. #undef FROB
  258. }
  259.