home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libmocha / lm_screen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.2 KB  |  176 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.  * JS reflection of the Screen info top-level object.
  20.  *
  21.  * Tom Pixley, 3/1/97
  22.  */
  23. #include "lm.h"
  24.  
  25. typedef struct JSScreen {
  26.     MochaDecoder    *decoder;
  27. } JSScreen;
  28.  
  29. enum screen_tinyid {
  30.     SCREEN_WIDTH        = -1,
  31.     SCREEN_HEIGHT           = -2,
  32.     SCREEN_PIXEL_DEPTH        = -3,
  33.     SCREEN_COLOR_DEPTH        = -4,
  34.     SCREEN_AVAIL_WIDTH        = -5,
  35.     SCREEN_AVAIL_HEIGHT        = -6,
  36.     SCREEN_AVAIL_LEFT        = -7,
  37.     SCREEN_AVAIL_TOP        = -8
  38. };
  39.  
  40. static JSPropertySpec screen_props[] = {
  41.     {"width",        SCREEN_WIDTH,        JSPROP_ENUMERATE | JSPROP_READONLY},
  42.     {"height",        SCREEN_HEIGHT,        JSPROP_ENUMERATE | JSPROP_READONLY},
  43.     {"pixelDepth",      SCREEN_PIXEL_DEPTH,     JSPROP_ENUMERATE | JSPROP_READONLY},
  44.     {"colorDepth",      SCREEN_COLOR_DEPTH,     JSPROP_ENUMERATE | JSPROP_READONLY},
  45.     {"availWidth",      SCREEN_AVAIL_WIDTH,     JSPROP_ENUMERATE | JSPROP_READONLY},
  46.     {"availHeight",     SCREEN_AVAIL_HEIGHT,    JSPROP_ENUMERATE | JSPROP_READONLY},
  47.     {"availLeft",       SCREEN_AVAIL_LEFT,      JSPROP_ENUMERATE | JSPROP_READONLY},
  48.     {"availTop",        SCREEN_AVAIL_TOP,       JSPROP_ENUMERATE | JSPROP_READONLY},
  49.     {0}
  50. };
  51.  
  52. extern JSClass lm_screen_class;
  53.  
  54. PR_STATIC_CALLBACK(JSBool)
  55. screen_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
  56. {
  57.     MWContext *context;
  58.     JSScreen *screen;
  59.     jsint slot;
  60.     int32 width, height, left, top, pixel, pallette;
  61.  
  62.     if (!JSVAL_IS_INT(id))
  63.     return JS_TRUE;
  64.  
  65.     slot = JSVAL_TO_INT(id);
  66.  
  67.     screen = JS_GetInstancePrivate(cx, obj, &lm_screen_class, NULL);
  68.     if (!screen)
  69.     return JS_TRUE;
  70.     context = screen->decoder->window_context;
  71.     if (!context)
  72.     return JS_TRUE;
  73.  
  74.     switch (slot) {
  75.     case SCREEN_WIDTH:
  76.         ET_PostGetScreenSize(context, &width, &height);
  77.             *vp = INT_TO_JSVAL(width);
  78.         break;
  79.     case SCREEN_HEIGHT:
  80.         ET_PostGetScreenSize(context, &width, &height);
  81.             *vp = INT_TO_JSVAL(height);
  82.         break;
  83.     case SCREEN_PIXEL_DEPTH:
  84.         ET_PostGetColorDepth(context, &pixel, &pallette);
  85.             *vp = INT_TO_JSVAL(pixel);
  86.         break;
  87.     case SCREEN_COLOR_DEPTH:
  88.         ET_PostGetColorDepth(context, &pixel, &pallette);
  89.             *vp = INT_TO_JSVAL(pallette);
  90.         break;
  91.     case SCREEN_AVAIL_WIDTH:
  92.         ET_PostGetAvailScreenRect(context, &width, &height, &left, &top);
  93.             *vp = INT_TO_JSVAL(width);
  94.         break;
  95.     case SCREEN_AVAIL_HEIGHT:
  96.         ET_PostGetAvailScreenRect(context, &width, &height, &left, &top);
  97.             *vp = INT_TO_JSVAL(height);
  98.         break;
  99.     case SCREEN_AVAIL_LEFT:
  100.         ET_PostGetAvailScreenRect(context, &width, &height, &left, &top);
  101.             *vp = INT_TO_JSVAL(left);
  102.         break;
  103.     case SCREEN_AVAIL_TOP:
  104.         ET_PostGetAvailScreenRect(context, &width, &height, &left, &top);
  105.             *vp = INT_TO_JSVAL(top);
  106.         break;
  107.     default:
  108.         return JS_TRUE;
  109.     }
  110.     return JS_TRUE;
  111. }
  112.  
  113.  
  114. PR_STATIC_CALLBACK(void)
  115. screen_finalize(JSContext *cx, JSObject *obj)
  116. {
  117.     JSScreen *screen;
  118.  
  119.     screen = JS_GetPrivate(cx, obj);
  120.     if (!screen)
  121.     return;
  122.     DROP_BACK_COUNT(screen->decoder);
  123.     JS_free(cx, screen);
  124. }
  125.  
  126. JSClass lm_screen_class = {
  127.     "Screen", JSCLASS_HAS_PRIVATE,
  128.     JS_PropertyStub, JS_PropertyStub, screen_getProperty, screen_getProperty,
  129.     JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, screen_finalize
  130. };
  131.  
  132. PR_STATIC_CALLBACK(JSBool)
  133. Screen(JSContext *cx, JSObject *obj,
  134.           uint argc, jsval *argv, jsval *rval)
  135. {
  136.     return JS_TRUE;
  137. }
  138.  
  139. JSObject *
  140. lm_DefineScreen(MochaDecoder *decoder, JSObject *parent)
  141. {
  142.     JSObject *obj;
  143.     JSContext *cx;
  144.     JSScreen *screen;
  145.  
  146.     if (parent == decoder->window_object) {
  147.         obj = decoder->screen;
  148.         if (obj)
  149.             return obj;
  150.     }
  151.  
  152.     cx = decoder->js_context;
  153.     if (!(screen = JS_malloc(cx, sizeof(JSScreen))))
  154.     return NULL;
  155.     screen->decoder = NULL; /* in case of error below */
  156.  
  157.     obj = JS_InitClass(cx, decoder->window_object, NULL, &lm_screen_class,
  158.                        Screen, 0, screen_props, NULL, NULL, NULL);
  159.  
  160.     if (!obj || !JS_SetPrivate(cx, obj, screen)) {
  161.         JS_free(cx, screen);
  162.         return NULL;
  163.     }
  164.     
  165.     if (!JS_DefineProperty(cx, parent, "screen",
  166.                OBJECT_TO_JSVAL(obj), NULL, NULL,
  167.                            JSPROP_ENUMERATE | JSPROP_READONLY)) {
  168.         return NULL;
  169.     }
  170.  
  171.     screen->decoder = HOLD_BACK_COUNT(decoder);
  172.     if (parent == decoder->window_object)
  173.         decoder->screen = obj;
  174.     return obj;
  175. }
  176.