1 /*
   2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 #include "config.h"
   5 
   6 #include "Chrome.h"
   7 #include "ChromeClientJava.h"
   8 #include "Frame.h"
   9 #include "FrameView.h"
  10 #include "GraphicsContext.h"
  11 #include "HostWindow.h"
  12 #include "Page.h"
  13 #include "PlatformContextJava.h"
  14 #include "PlatformMouseEvent.h"
  15 #include "Scrollbar.h"
  16 #include "ScrollbarThemeJava.h"
  17 #include "ScrollView.h"
  18 #include "NotImplemented.h"
  19 
  20 #include "JavaEnv.h"
  21 #include "com_sun_webkit_graphics_ScrollBarTheme.h"
  22 #include "com_sun_webkit_graphics_GraphicsDecoder.h"
  23 
  24 namespace WebCore {
  25 
  26 
  27 ScrollbarTheme& ScrollbarTheme::nativeTheme()
  28 {
  29     static ScrollbarTheme *s_sharedInstance = new ScrollbarThemeJava();
  30     return *s_sharedInstance;
  31 }
  32 
  33 jclass getJScrollBarThemeClass()
  34 {
  35     static JGClass jScrollbarThemeClass(
  36         WebCore_GetJavaEnv()->FindClass("com/sun/webkit/graphics/ScrollBarTheme"));
  37     ASSERT(jScrollbarThemeClass);
  38 
  39     return jScrollbarThemeClass;
  40 }
  41 
  42 JLObject getJScrollBarTheme(Scrollbar& sb)  //XXX: ScrollbarThemeClient replaced by Scrollbar, double recheck
  43 {
  44     FrameView* fv = sb.root();
  45     if (!fv) {
  46         // the scrollbar has been detached
  47         return 0;
  48     }
  49     Page* page = fv->frame().page();
  50     JLObject jWebPage = ((ChromeClientJava*)&page->chrome().client())->platformPage();
  51 
  52     JNIEnv* env = WebCore_GetJavaEnv();
  53 
  54     static jmethodID mid  = env->GetMethodID(
  55         PG_GetWebPageClass(env),
  56         "getScrollBarTheme",
  57         "()Lcom/sun/webkit/graphics/ScrollBarTheme;");
  58     ASSERT(mid);
  59 
  60     JLObject jScrollbarTheme = env->CallObjectMethod(jWebPage, mid);
  61     ASSERT(jScrollbarTheme);
  62     CheckAndClearException(env);
  63 
  64     return jScrollbarTheme;
  65 }
  66 
  67 bool ScrollbarThemeJava::paint(Scrollbar& scrollbar, GraphicsContext& gc, const IntRect& damageRect)
  68 {
  69     // platformContext() returns 0 when printing
  70     if (gc.paintingDisabled() || !gc.platformContext()) {
  71         return true;
  72     }
  73     JLObject jtheme = getJScrollBarTheme(scrollbar);
  74     if (!jtheme) {
  75         return false;
  76     }
  77     JNIEnv* env = WebCore_GetJavaEnv();
  78 
  79     static jmethodID mid = env->GetMethodID(
  80         getJScrollBarThemeClass(),
  81         "createWidget",
  82         "(JIIIIII)Lcom/sun/webkit/graphics/Ref;");
  83     ASSERT(mid);
  84 
  85     RefPtr<RQRef> widgetRef = RQRef::create( env->CallObjectMethod(
  86         jtheme,
  87         mid,
  88         ptr_to_jlong(&scrollbar),
  89         (jint)scrollbar.width(),
  90         (jint)scrollbar.height(),
  91         (jint)scrollbar.orientation(),
  92         (jint)scrollbar.value(),
  93         (jint)scrollbar.visibleSize(),
  94         (jint)scrollbar.totalSize()));
  95     ASSERT(widgetRef.get());
  96     CheckAndClearException(env);
  97 
  98     // widgetRef will go into rq's inner refs vector.
  99     gc.platformContext()->rq().freeSpace(28)
 100         << (jint)com_sun_webkit_graphics_GraphicsDecoder_DRAWSCROLLBAR
 101         << RQRef::create(jtheme)
 102         << widgetRef
 103         << (jint)scrollbar.x()
 104         << (jint)scrollbar.y()
 105         << (jint)scrollbar.pressedPart()
 106         << (jint)scrollbar.hoveredPart();
 107 
 108     return false;
 109 }
 110 
 111 ScrollbarPart ScrollbarThemeJava::hitTest(Scrollbar& scrollbar, const IntPoint& pos)
 112 {
 113     JLObject jtheme = getJScrollBarTheme(scrollbar);
 114     if (!jtheme) {
 115         return (ScrollbarPart)0;
 116     }
 117     JNIEnv* env = WebCore_GetJavaEnv();
 118 
 119     static jmethodID mid = env->GetMethodID(
 120         getJScrollBarThemeClass(),
 121         "hitTest",
 122         "(IIIIIIII)I");
 123     ASSERT(mid);
 124 
 125     IntPoint p = scrollbar.convertFromContainingWindow(pos);
 126     int part = env->CallIntMethod(
 127         jtheme,
 128         mid,
 129         (jint)scrollbar.width(),
 130         (jint)scrollbar.height(),
 131         (jint)scrollbar.orientation(),
 132         (jint)scrollbar.value(),
 133         (jint)scrollbar.visibleSize(),
 134         (jint)scrollbar.totalSize(),
 135         (jint)p.x(),
 136         (jint)p.y());
 137     CheckAndClearException(env);
 138 
 139     return (ScrollbarPart)part;
 140 }
 141 
 142 void ScrollbarThemeJava::invalidatePart(Scrollbar& scrollbar, ScrollbarPart)
 143 {
 144     // FIXME: Do more precise invalidation.
 145     scrollbar.invalidate();
 146 }
 147 
 148 int ScrollbarThemeJava::thumbPosition(Scrollbar& scrollbar)
 149 {
 150     JLObject jtheme = getJScrollBarTheme(scrollbar);
 151     if (!jtheme) {
 152         return 0;
 153     }
 154     JNIEnv* env = WebCore_GetJavaEnv();
 155 
 156     static jmethodID mid = env->GetMethodID(
 157         getJScrollBarThemeClass(),
 158         "getThumbPosition",
 159         "(IIIIII)I");
 160     ASSERT(mid);
 161 
 162     int pos = env->CallIntMethod(
 163         jtheme,
 164         mid,
 165         (jint)scrollbar.width(),
 166         (jint)scrollbar.height(),
 167         (jint)scrollbar.orientation(),
 168         (jint)scrollbar.value(),
 169         (jint)scrollbar.visibleSize(),
 170         (jint)scrollbar.totalSize());
 171     CheckAndClearException(env);
 172 
 173     return pos;
 174 }
 175 
 176 int ScrollbarThemeJava::thumbLength(Scrollbar& scrollbar)
 177 {
 178     JLObject jtheme = getJScrollBarTheme(scrollbar);
 179     if (!jtheme) {
 180         return 0;
 181     }
 182     JNIEnv* env = WebCore_GetJavaEnv();
 183 
 184     static jmethodID mid = env->GetMethodID(
 185         getJScrollBarThemeClass(),
 186         "getThumbLength",
 187         "(IIIIII)I");
 188     ASSERT(mid);
 189 
 190     int len = env->CallIntMethod(
 191         jtheme,
 192         mid,
 193         (jint)scrollbar.width(),
 194         (jint)scrollbar.height(),
 195         (jint)scrollbar.orientation(),
 196         (jint)scrollbar.value(),
 197         (jint)scrollbar.visibleSize(),
 198         (jint)scrollbar.totalSize());
 199     CheckAndClearException(env);
 200 
 201     return len;
 202 }
 203 
 204 int ScrollbarThemeJava::trackPosition(Scrollbar& scrollbar)
 205 {
 206     JLObject jtheme = getJScrollBarTheme(scrollbar);
 207     if (!jtheme) {
 208         return 0;
 209     }
 210     JNIEnv* env = WebCore_GetJavaEnv();
 211 
 212     static jmethodID mid = env->GetMethodID(
 213         getJScrollBarThemeClass(),
 214         "getTrackPosition",
 215         "(III)I");
 216     ASSERT(mid);
 217 
 218     int pos = env->CallIntMethod(
 219         jtheme,
 220         mid,
 221         (jint)scrollbar.width(),
 222         (jint)scrollbar.height(),
 223         (jint)scrollbar.orientation());
 224     CheckAndClearException(env);
 225 
 226     return pos;
 227 }
 228 
 229 int ScrollbarThemeJava::trackLength(Scrollbar& scrollbar)
 230 {
 231     JLObject jtheme = getJScrollBarTheme(scrollbar);
 232     if (!jtheme) {
 233         return 0;
 234     }
 235     JNIEnv* env = WebCore_GetJavaEnv();
 236 
 237     static jmethodID mid = env->GetMethodID(
 238         getJScrollBarThemeClass(),
 239         "getTrackLength",
 240         "(III)I");
 241     ASSERT(mid);
 242 
 243     int len = env->CallIntMethod(
 244         jtheme,
 245         mid,
 246         (jint)scrollbar.width(),
 247         (jint)scrollbar.height(),
 248         (jint)scrollbar.orientation());
 249     CheckAndClearException(env);
 250 
 251     return len;
 252 }
 253 
 254 int ScrollbarThemeJava::scrollbarThickness(ScrollbarControlSize controlSize)
 255 {
 256     JNIEnv* env = WebCore_GetJavaEnv();
 257 
 258     static jmethodID mid = env->GetStaticMethodID(
 259         getJScrollBarThemeClass(),
 260         "getThickness",
 261         "()I");
 262     ASSERT(mid);
 263 
 264     int thickness = env->CallStaticIntMethod(
 265         getJScrollBarThemeClass(),
 266         mid);
 267     CheckAndClearException(env);
 268 
 269     return thickness;
 270 }
 271 
 272 } //namespace WebCore
 273