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 
  74     JLObject jtheme = getJScrollBarTheme(scrollbar);
  75     if (!jtheme) {
  76         return false;
  77     }
  78     JNIEnv* env = WebCore_GetJavaEnv();
  79 
  80     static jmethodID mid = env->GetMethodID(
  81         getJScrollBarThemeClass(),
  82         "createWidget",
  83         "(JIIIIII)Lcom/sun/webkit/graphics/Ref;");
  84     ASSERT(mid);
  85 
  86     RefPtr<RQRef> widgetRef = RQRef::create( env->CallObjectMethod(
  87         jtheme,
  88         mid,
  89         ptr_to_jlong(&scrollbar),
  90         (jint)scrollbar.width(),
  91         (jint)scrollbar.height(),
  92         (jint)scrollbar.orientation(),
  93         (jint)scrollbar.value(),
  94         (jint)scrollbar.visibleSize(),
  95         (jint)scrollbar.totalSize()));
  96     ASSERT(widgetRef.get());
  97     CheckAndClearException(env);
  98 
  99     // widgetRef will go into rq's inner refs vector.
 100     gc.platformContext()->rq().freeSpace(28)
 101         << (jint)com_sun_webkit_graphics_GraphicsDecoder_DRAWSCROLLBAR
 102         << RQRef::create(jtheme)
 103         << widgetRef
 104         << (jint)scrollbar.x()
 105         << (jint)scrollbar.y()
 106         << (jint)scrollbar.pressedPart()
 107         << (jint)scrollbar.hoveredPart();
 108 
 109     return false;
 110 }
 111 
 112 ScrollbarPart ScrollbarThemeJava::hitTest(Scrollbar& scrollbar, const IntPoint& pos)
 113 {
 114     if (!scrollbar.enabled()) {
 115         return (ScrollbarPart)0;
 116     }
 117 
 118     JLObject jtheme = getJScrollBarTheme(scrollbar);
 119     if (!jtheme) {
 120         return (ScrollbarPart)0;
 121     }
 122     JNIEnv* env = WebCore_GetJavaEnv();
 123 
 124     static jmethodID mid = env->GetMethodID(
 125         getJScrollBarThemeClass(),
 126         "hitTest",
 127         "(JII)I");
 128     ASSERT(mid);
 129 
 130     IntPoint p = scrollbar.convertFromContainingWindow(pos);
 131     int part = env->CallIntMethod(
 132         jtheme,
 133         mid,
 134         ptr_to_jlong(&scrollbar),
 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     if (!scrollbar.enabled()) {
 151         return 0;
 152     }
 153 
 154     JLObject jtheme = getJScrollBarTheme(scrollbar);
 155     if (!jtheme) {
 156         return 0;
 157     }
 158     JNIEnv* env = WebCore_GetJavaEnv();
 159 
 160     static jmethodID mid = env->GetMethodID(
 161         getJScrollBarThemeClass(),
 162         "getThumbPosition",
 163         "(J)I");
 164     ASSERT(mid);
 165 
 166     int pos = env->CallIntMethod(
 167         jtheme,
 168         mid,
 169         ptr_to_jlong(&scrollbar));
 170     CheckAndClearException(env);
 171 
 172     return pos;
 173 }
 174 
 175 int ScrollbarThemeJava::thumbLength(Scrollbar& scrollbar)
 176 {
 177     if (!scrollbar.enabled()) {
 178         return 0;
 179     }
 180     JLObject jtheme = getJScrollBarTheme(scrollbar);
 181     if (!jtheme) {
 182         return 0;
 183     }
 184     JNIEnv* env = WebCore_GetJavaEnv();
 185 
 186     static jmethodID mid = env->GetMethodID(
 187         getJScrollBarThemeClass(),
 188         "getThumbLength",
 189         "(J)I");
 190     ASSERT(mid);
 191 
 192     int len = env->CallIntMethod(
 193         jtheme,
 194         mid,
 195         ptr_to_jlong(&scrollbar));
 196     CheckAndClearException(env);
 197 
 198     return len;
 199 }
 200 
 201 int ScrollbarThemeJava::trackPosition(Scrollbar& scrollbar)
 202 {
 203     if (!scrollbar.enabled()) {
 204         return 0;
 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         "(J)I");
 216     ASSERT(mid);
 217 
 218     int pos = env->CallIntMethod(
 219         jtheme,
 220         mid,
 221         ptr_to_jlong(&scrollbar));
 222     CheckAndClearException(env);
 223 
 224     return pos;
 225 }
 226 
 227 int ScrollbarThemeJava::trackLength(Scrollbar& scrollbar)
 228 {
 229     if (!scrollbar.enabled()) {
 230         return 0;
 231     }
 232     JLObject jtheme = getJScrollBarTheme(scrollbar);
 233     if (!jtheme) {
 234         return 0;
 235     }
 236     JNIEnv* env = WebCore_GetJavaEnv();
 237 
 238     static jmethodID mid = env->GetMethodID(
 239         getJScrollBarThemeClass(),
 240         "getTrackLength",
 241         "(J)I");
 242     ASSERT(mid);
 243 
 244     int len = env->CallIntMethod(
 245         jtheme,
 246         mid,
 247         ptr_to_jlong(&scrollbar));
 248     CheckAndClearException(env);
 249 
 250     return len;
 251 }
 252 
 253 int ScrollbarThemeJava::scrollbarThickness(ScrollbarControlSize controlSize)
 254 {
 255     JNIEnv* env = WebCore_GetJavaEnv();
 256 
 257     static jmethodID mid = env->GetStaticMethodID(
 258         getJScrollBarThemeClass(),
 259         "getThickness",
 260         "()I");
 261     ASSERT(mid);
 262 
 263     int thickness = env->CallStaticIntMethod(
 264         getJScrollBarThemeClass(),
 265         mid);
 266     CheckAndClearException(env);
 267 
 268     return thickness;
 269 }
 270 
 271 } //namespace WebCore
 272