int cTemplateViewList::GetAverageFontWidth(void) {
    int defaultAverageFontWidth = 20;

    if (!listElement)
        return defaultAverageFontWidth;

    int numItems = GetNumericParameter(ptNumElements);
    int listHeight = GetNumericParameter(ptHeight);
    if (listHeight <= 0)
        return defaultAverageFontWidth;
    int itemHeight = (double)listHeight / (double)numItems;
    string fontFuncName = parameters->GetParameter(ptDeterminateFont);

    cTemplateFunction *fontFunc = listElement->GetFunction(fontFuncName);
    if (!fontFunc)
        return defaultAverageFontWidth;

    string fontNameToken = fontFunc->GetParameter(ptFont);
    string paramFontSize = fontFunc->GetParameter(ptFontSize);

    string fontName = "";
    if (IsToken(fontNameToken)) {
        if (!globals->GetFont(fontNameToken, fontName)) {
            if (!globals->GetFont("{vdrOsd}", fontName)) {
                return defaultAverageFontWidth;
            }
        }
    } else {
        //if no token, directly use input
        fontName = fontNameToken;
    }

    cNumericParameter pFontSize(paramFontSize);
    pFontSize.SetGlobals(globals);
    pFontSize.SetAreaSize(1000, itemHeight);
    pFontSize.SetVertical();
    int fontSize = pFontSize.Parse(paramFontSize);
    if (!pFontSize.Valid())
        return defaultAverageFontWidth;

    int averageFontWidth = fontManager->Width(fontName, fontSize, "x") + 3;
    return averageFontWidth;
}
int cTemplateViewList::GetMenuItemWidth(void) {
    return GetNumericParameter(ptMenuItemWidth);    
}
void SetWindowRectCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  int width = -1;
  int height = -1;
  int x = -1;
  int y = -1;

  std::string argument_error_message = "";
  if (!GetNumericParameter(command_parameters, "width", &width, &argument_error_message)) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, argument_error_message);
    return;
  }

  if (!GetNumericParameter(command_parameters, "height", &height, &argument_error_message)) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, argument_error_message);
    return;
  }

  if (!GetNumericParameter(command_parameters, "x", &x, &argument_error_message)) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, argument_error_message);
    return;
  }

  if (!GetNumericParameter(command_parameters, "y", &y, &argument_error_message)) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, argument_error_message);
    return;
  }

  int status_code = WD_SUCCESS;

  BrowserHandle browser_wrapper;
  status_code = executor.GetCurrentBrowser(&browser_wrapper);
  if (status_code != WD_SUCCESS) {
    response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Error retrieving current window");
    return;
  }

  // If the window is minimized, maximized, or full screen,
  // the window needs to be restored.
  browser_wrapper->Restore();

  HWND window_handle = browser_wrapper->GetTopLevelWindowHandle();
  if (x >= 0 && y >= 0) {
    BOOL set_window_pos_result = ::SetWindowPos(window_handle, NULL, x, y, 0, 0, SWP_NOSIZE);
    if (!set_window_pos_result) {
      response->SetErrorResponse(ERROR_UNKNOWN_ERROR,
                                "Unexpected error setting window size (SetWindowPos API failed)");
      return;
    }
  }

  if (width >= 0 && height >= 0) {
    BOOL set_window_size_result = ::SetWindowPos(window_handle, NULL, 0, 0, width, height, SWP_NOMOVE);
    if (!set_window_size_result) {
      response->SetErrorResponse(ERROR_UNKNOWN_ERROR,
                                 "Unexpected error setting window size (SetWindowPos API failed)");
      return;
    }
  }

  HWND browser_window_handle = browser_wrapper->GetTopLevelWindowHandle();
  RECT window_rect;
  ::GetWindowRect(browser_window_handle, &window_rect);
  Json::Value returned_rect;
  returned_rect["width"] = window_rect.right - window_rect.left;
  returned_rect["height"] = window_rect.bottom - window_rect.top;
  returned_rect["x"] = window_rect.left;
  returned_rect["y"] = window_rect.top;
  response->SetSuccessResponse(returned_rect);
}