Home | History | Annotate | Download | only in android
      1 /*
      2  * Copyright 2006, The Android Open Source Project
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *  * Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  *  * Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #define LOG_TAG "WebCore"
     27 
     28 #include "config.h"
     29 #include "android_graphics.h"
     30 #include "Document.h"
     31 #include "IntRect.h"
     32 #include "Node.h"
     33 #include "RenderSkinButton.h"
     34 #include "RenderSkinNinePatch.h"
     35 #include "SkCanvas.h"
     36 #include "SkNinePatch.h"
     37 #include "SkRect.h"
     38 #include <utils/Asset.h>
     39 #include <utils/AssetManager.h>
     40 #include <utils/Debug.h>
     41 #include <utils/Log.h>
     42 #include <utils/ResourceTypes.h>
     43 #include <wtf/text/CString.h>
     44 
     45 extern android::AssetManager* globalAssetManager();
     46 
     47 static const char* gFiles[] = {
     48     "btn_default_disabled_holo.9.png",
     49     "btn_default_normal_holo.9.png",
     50     "btn_default_focused_holo.9.png",
     51     "btn_default_pressed_holo.9.png"
     52     };
     53 
     54 namespace WebCore {
     55 
     56 RenderSkinButton::RenderSkinButton(String drawableDirectory)
     57     : m_decoded(false)
     58     , m_decodingAttempted(false)
     59     , m_drawableDirectory(drawableDirectory)
     60 {
     61     // Ensure our enums properly line up with our arrays.
     62     android::CompileTimeAssert<(RenderSkinAndroid::kDisabled == 0)> a1;
     63     android::CompileTimeAssert<(RenderSkinAndroid::kNormal == 1)> a2;
     64     android::CompileTimeAssert<(RenderSkinAndroid::kFocused == 2)> a3;
     65     android::CompileTimeAssert<(RenderSkinAndroid::kPressed == 3)> a4;
     66 }
     67 
     68 void RenderSkinButton::decode()
     69 {
     70     m_decodingAttempted = true;
     71 
     72     android::AssetManager* am = globalAssetManager();
     73 
     74     for (size_t i = 0; i < 4; i++) {
     75         String path = m_drawableDirectory;
     76         path.append(String(gFiles[i]));
     77         if (!RenderSkinNinePatch::decodeAsset(am, path.utf8().data(), &m_buttons[i])) {
     78             m_decoded = false;
     79             LOGE("RenderSkinButton::decode: button assets failed to decode\n\tWebView buttons will not draw");
     80             return;
     81         }
     82     }
     83     m_decoded = true;
     84 }
     85 
     86 void RenderSkinButton::draw(SkCanvas* canvas, const IntRect& r,
     87                             RenderSkinAndroid::State newState)
     88 {
     89     if (!m_decodingAttempted)
     90         decode();
     91 
     92     // If we failed to decode, do nothing.  This way the browser still works,
     93     // and webkit will still draw the label and layout space for us.
     94     if (!m_decoded) {
     95         return;
     96     }
     97 
     98     // Ensure that the state is within the valid range of our array.
     99     SkASSERT(static_cast<unsigned>(newState) <
    100             static_cast<unsigned>(RenderSkinAndroid::kNumStates));
    101 
    102     RenderSkinNinePatch::DrawNinePatch(canvas, SkRect(r), m_buttons[newState]);
    103 }
    104 
    105 } //WebCore
    106