How to change the label of File Dialog button?

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

How to change the label of File Dialog button?

Postby rkcef » Tue Jun 21, 2022 3:47 am

Hi,

I'm opening the file dialog by calling

Code: Select all
browser->GetHost()->RunFileDialog(FILE_DIALOG_OPEN_FOLDER, ...)


The button to accept the selected folder shows the label "Hochladen" (that's german for "upload").

How can I change this label?
Attachments
fileopen_dialog.jpg
File Dialog Screenshot
fileopen_dialog.jpg (104.22 KiB) Viewed 2887 times
rkcef
Techie
 
Posts: 27
Joined: Fri Feb 26, 2021 9:11 am

Re: How to change the label of File Dialog button?

Postby magreenblatt » Tue Jun 21, 2022 8:14 am

In CEF version 102 and newer you can handle IDS_SELECT_UPLOAD_FOLDER_DIALOG_UPLOAD_BUTTON via CefResourceBundleHandler::GetLocalizedString.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: How to change the label of File Dialog button?

Postby rkcef » Wed Jun 22, 2022 4:06 am

magreenblatt wrote:IDS_SELECT_UPLOAD_FOLDER_DIALOG_UPLOAD_BUTTON via CefResourceBundleHandler::GetLocalizedString.


Thanks for the hint.

The class "CefResourceBundleHandler" doesn't seem to be implemented. Perhaps because "CefSettings.pack_loading_disabled" is not set to "true" by default and therefore it isn't needed?

But I don't understand what this class is supposed to do and how I can implement the function "GetLocalizedString" myself. Are there any more information about this?

Is there another way to change a label?
rkcef
Techie
 
Posts: 27
Joined: Fri Feb 26, 2021 9:11 am

Re: How to change the label of File Dialog button?

Postby magreenblatt » Wed Jun 22, 2022 4:25 am

You need to implement CefResourceBundleHandler, CefApp::GetResourceBundleHandler and pass the CefApp instance to CefExecuteProcess and CefInitialize.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: How to change the label of File Dialog button?

Postby rkcef » Wed Jun 22, 2022 4:59 am

magreenblatt wrote:You need to implement CefResourceBundleHandler, CefApp::GetResourceBundleHandler


I'm sorry I still struggle with this :oops:

Just for clarification, maybe I misunderstood you:

In the "cefclient" main C++ file (cefclient_win.cc) there's a variable called "app", that is defined as "CefRefPtr<CefApp> app".
This variable is either an instance of "ClientAppBrowser", "ClientAppRenderer" or "ClientAppOther".
All these three classes inherit from the class "ClientApp" which again inherits from the class "CefApp".

Because of this hierarchy it makes sense to implement "CefApp::GetResourceBundleHandler" in the class "ClientApp", which I tried to do.

The method "CefApp::GetResourceBundleHandler" returns a value of the type "CefRefPtr<CefResourceBundleHandler>".
The class "CefResourceBundleHandler" is a so-called "abstract" class, because all of it's members are "virtual".
This means this class can't be instantiated directly. But I couldn't find another existing class that implements "CefResourceBundleHandler" to use as the return value.

If "CefResourceBundleHandler" is not imlemented, what should be the valid return value of "CefApp::GetResourceBundleHandler", to be able to call "CefResourceBundleHandler::GetLocalizedString" and change the label of the button?

I hoped this makes my current confusion a bit clearer.
rkcef
Techie
 
Posts: 27
Joined: Fri Feb 26, 2021 9:11 am

Re: How to change the label of File Dialog button?

Postby magreenblatt » Wed Jun 22, 2022 5:07 am

You need to implement CefResourceBundleHandler. For example, by having ClientApp implement that interface.
Code: Select all
class ClientApp : public CefApp, public CefResourceBundleHandler {
  ...
  CefRefPtr<CefResourceBundleHandler> GetResourceBundleHandler() override {
    return this;
  }

  // Implement CefResourceBundleHandler methods:
  bool GetLocalizedString(int string_id, CefString& string) override;
  ...
};
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: How to change the label of File Dialog button?

Postby rkcef » Wed Jun 22, 2022 5:21 am

As I wrote in my previous comment I don't know what an implementation should look like.

Are there any examples?
rkcef
Techie
 
Posts: 27
Joined: Fri Feb 26, 2021 9:11 am

Re: How to change the label of File Dialog button?

Postby magreenblatt » Wed Jun 22, 2022 5:38 am

See the method documentation. A simple implementation would be:
Code: Select all
if (string_id == IDS_SELECT_UPLOAD_FOLDER_DIALOG_UPLOAD_BUTTON) {
  string  = “someLabel”;
  return true;
}
return false;
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: How to change the label of File Dialog button?

Postby rkcef » Wed Jun 22, 2022 7:47 am

magreenblatt wrote:See the method documentation. A simple implementation would be:
Code: Select all
if (string_id == IDS_SELECT_UPLOAD_FOLDER_DIALOG_UPLOAD_BUTTON) {
  string  = “someLabel”;
  return true;
}
return false;


Thank your very much. It's working now! :)

I'll try to summerize my solution for anyone who needs it:

This is based on the "cefclient" example project.

As mentioned by magreenblatt, you need to implement the abstract class CefResourceBundleHandler and also the method CefApp::GetResourceBundleHandler. As I mentioned in my previous comments, the class CefApp is inherited by the class ClientApp, so it makes sense to implement the method in ClientApp.

1. Implement abstract class CefResourceBundleHandler:

You can either implement it directly in ClientApp (viewtopic.php?p=52089#p52089) or you can create a separate class. This is what I did. I've created a new class called CefResourceBundleHandlerImpl:

File: cef_resource_bundle_handler_impl.h
Code: Select all
#ifndef COMMON_CEF_RESOURCE_BUNDLE_HANDLER_IMPL_H
#define COMMON_CEF_RESOURCE_BUNDLE_HANDLER_IMPL_H
#pragma once

#include "include/cef_resource_bundle_handler.h"
#include "include/cef_pack_strings.h"

namespace common
{
  class CefResourceBundleHandlerImpl : public CefResourceBundleHandler
  {
    public:
      CefResourceBundleHandlerImpl(void);

      // Methods from CefResourceBundleHandler

      bool GetLocalizedString(
        int string_id,
        CefString& string) override;

      bool GetDataResource(
        int resource_id,
        void*& data,
        size_t& data_size) override;

      bool GetDataResourceForScale(
        int resource_id,
        ScaleFactor scale_factor,
        void*& data,
        size_t& data_size) override;

    private:
      IMPLEMENT_REFCOUNTING(CefResourceBundleHandlerImpl);

      ~CefResourceBundleHandlerImpl(void);
  };

} // namespace common

#endif // COMMON_CEF_RESOURCE_BUNDLE_HANDLER_IMPL_H


File: cef_resource_bundle_handler_impl.cpp
Code: Select all
#include "cef_resource_bundle_handler_impl.h"

namespace common
{
  CefResourceBundleHandlerImpl::CefResourceBundleHandlerImpl(void)
  {
    LOG(INFO) << "CefResourceBundleHandlerImpl constructed";
  }

  CefResourceBundleHandlerImpl::~CefResourceBundleHandlerImpl(void)
  {
    LOG(INFO) << "CefResourceBundleHandlerImpl destructed";
  }

  bool CefResourceBundleHandlerImpl::GetLocalizedString(
    int string_id,
    CefString& string)
  {
    switch (string_id)
    {
      case IDS_SELECT_UPLOAD_FOLDER_DIALOG_UPLOAD_BUTTON:
      {
        string = "NEW BUTTON LABEL";
        return true;
        break;
      }
      default:
      {
        break;
      }
    }

    return false;
  }

  bool CefResourceBundleHandlerImpl::GetDataResource(
    int resource_id,
    void*& data,
    size_t& data_size)
  {
      return false;
  }

  bool CefResourceBundleHandlerImpl::GetDataResourceForScale(
    int resource_id,
    ScaleFactor scale_factor,
    void*& data,
    size_t& data_size)
  {
      return false;
  }

} // namespace common


Place these two files somewhere in your project directory.


2. Implement method CefApp::GetResourceBundleHandler:

In the class ClientApp we need to add the method GetResourceBundleHandler:

File: client_app.h
Code: Select all
...

#include "/path/to/new/class/cef_resource_bundle_handler_impl.h"

...

class ClientApp : public CefApp
{
  public:
    ...
    ~ClientApp();
    CefRefPtr<CefResourceBundleHandler> GetResourceBundleHandler() override;
    ...
  private:
    ...
    CefRefPtr<CefResourceBundleHandler> cefResourceBundleHandlerImpl;
    ...
}


File: client_app.cc
Code: Select all
...

ClientApp::ClientApp()
{
  this->cefResourceBundleHandlerImpl = new common::CefResourceBundleHandlerImpl();
}

ClientApp::~ClientApp()
{
  this->cefResourceBundleHandlerImpl = nullptr;
}

...

CefRefPtr<CefResourceBundleHandler> ClientApp::GetResourceBundleHandler()
{
  return this->cefResourceBundleHandlerImpl;
}


That's it. The button in the file dialog window should now show the new label.
rkcef
Techie
 
Posts: 27
Joined: Fri Feb 26, 2021 9:11 am


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 48 guests