Need assistance with syntax.

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.

Need assistance with syntax.

Postby whittyusername69 » Tue Feb 05, 2019 2:16 am

I am doing some work with cefsimple and am looking to configure it such that I can send some basic layout arguments to it through the command line as I use it.

Unfortunately, this language is Greek to me. I normally I'd figure it out for myself- but this strikes me as if it's just a matter of syntax. Would someone mind translating this into it's workable form?

I've figured out how to specify the window's starting height/width and position, as well as it's default color (the page I will be using is black, as such I want the un-loaded window to start out black upon opening).

Now I'd like to figure out how to have these facets be specified through command line arguments. Something like this-

Code: Select all
   

    browser_settings.background_color = command_line->GetSwitchValue("background-color");

    window_info.width = command_line->GetSwitchValue("window-size")[0];
    window_info.height = command_line->GetSwitchValue("window-size")[1];
    window_info.x = command_line->GetSwitchValue("window-position")[0];
    window_info.y = command_line->GetSwitchValue("window-position")[1];

// Create the first browser window.
    CefBrowserHost::CreateBrowser(window_info, handler, url, browser_settings,
                                  NULL);



Please note- I have pretty much no idea what I'm doing here- apologies for clogging the forum.
whittyusername69
Techie
 
Posts: 13
Joined: Sat Jan 12, 2019 9:34 pm

Re: Need assistance with syntax.

Postby ndesktop » Tue Feb 05, 2019 9:21 am

For checking some argument in command line:
Code: Select all
CefRefPtr<CefCommandLine> command_line = CefCommandLine::GetGlobalCommandLine();
bool am_i_evil = command_line->HasSwitch("am_i_evil"); // cefclient --am_i_evil

int integer_evil_level = -1; // no value
const std::string& string_how_evil_am_i = command_line->GetSwitchValue("evil_level");
if(!how_evil_am_i.empty()) {
    integer_evil_level = atoi(string_how_evil_am_i.c_str());
    if(integer_evil_level == 0 && string_how_evil_am_i != "0") { // atoi can return 0 if the input string is "0"
        integer_evil_level = -1;
    }
}


In your example:
Code: Select all
#include <sstream>  // for parsing string
#include <iostream>

  CefRefPtr<CefCommandLine> command_line = CefCommandLine::GetGlobalCommandLine();

  if (command_line->HasSwitch(switches::kBackgroundColor)) {
       browser_settings.background_color = command_line->GetSwitchValue("background-color");
           ParseColor(command_line->GetSwitchValue(switches::kBackgroundColor));
      // ParseColor is in cef/tests/cefclient/browser/main_context_impl.cc;
      // you can implement whatever you like in terms of parsing, as
      // red=R,green=G,blue=B
      // 20,64,223
      // #fa,#cd,#09 etc
  }

  if (command_line->HasSwitch("window-size")) {
      const std::string& sSize = command_line->GetSwitchValue("window-size");
      if(!sSize.empty()) {
         // parse size; we expect x,y
         int x = CW_USEDEFAULT, y = CW_USEDEFAULT;

         std::vector<std::string> size_parts;
         std::istringstream ss(sSize);
         std::string s;
         while(std::getline(ss, s, ',')) {
           size_parts.push_back(s);
         }
         if(size_parts.size() == 2) {
             x = atoi(size_parts[0]);
             y = atoi(size_parts[1]);
             if((x1 != 0 || size_parts[0] == "0") && (x2 != 0 || size_parts[1] == "0")) {
                 // conversions succeeded
                 window_info.width = x;
                 window_info.height = y;
             }
         }

         // ... same for x,y window-position
      }
  }
ndesktop
Master
 
Posts: 756
Joined: Thu Dec 03, 2015 10:10 am

Re: Need assistance with syntax.

Postby whittyusername69 » Tue Feb 05, 2019 2:50 pm

Many thank yous! However, I'm getting some errors.

With the color portion of your code:

Code: Select all
/Users/lucasyoung/Desktop/cef_binary_3.3626.1881.g628f810_macosx64/tests/cefsimple/simple_app.cc:106:30: error:
      use of undeclared identifier 'switches'
        if (command_line->HasSwitch(switches::kBackgroundColor)) {
                                    ^
/Users/lucasyoung/Desktop/cef_binary_3.3626.1881.g628f810_macosx64/tests/cefsimple/simple_app.cc:107:41: error:
      assigning to 'cef_color_t' (aka 'unsigned int') from incompatible type
      'CefString' (aka 'CefStringBase<CefStringTraitsUTF16>')
  ...= command_line->GetSwitchValue("background-color");
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/lucasyoung/Desktop/cef_binary_3.3626.1881.g628f810_macosx64/tests/cefsimple/simple_app.cc:108:49: error:
      use of undeclared identifier 'switches'


And with the size portion:

Code: Select all
/Users/lucasyoung/Desktop/cef_binary_3.3626.1881.g628f810_macosx64/tests/cefsimple/simple_app.cc:108:15: error:
      use of undeclared identifier 'CW_USEDEFAULT'
             int x = CW_USEDEFAULT, y = CW_USEDEFAULT;
                     ^
/Users/lucasyoung/Desktop/cef_binary_3.3626.1881.g628f810_macosx64/tests/cefsimple/simple_app.cc:108:34: error:
      use of undeclared identifier 'CW_USEDEFAULT'
             int x = CW_USEDEFAULT, y = CW_USEDEFAULT;
                                        ^
/Users/lucasyoung/Desktop/cef_binary_3.3626.1881.g628f810_macosx64/tests/cefsimple/simple_app.cc:117:15: error:
      no matching function for call to 'atoi'
                 x = atoi(size_parts[0]);
                     ^~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/stdlib.h:135:6: note:
      candidate function not viable: no known conversion from
      'std::__1::__vector_base<std::__1::basic_string<char>,
      std::__1::allocator<std::__1::basic_string<char> > >::value_type' (aka
      'std::__1::basic_string<char>') to 'const char *' for 1st argument
int      atoi(const char *);
         ^
/Users/lucasyoung/Desktop/cef_binary_3.3626.1881.g628f810_macosx64/tests/cefsimple/simple_app.cc:118:15: error:
      no matching function for call to 'atoi'
                 y = atoi(size_parts[1]);
                     ^~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/stdlib.h:135:6: note:
      candidate function not viable: no known conversion from
      'std::__1::__vector_base<std::__1::basic_string<char>,
      std::__1::allocator<std::__1::basic_string<char> > >::value_type' (aka
      'std::__1::basic_string<char>') to 'const char *' for 1st argument
int      atoi(const char *);
         ^
/Users/lucasyoung/Desktop/cef_binary_3.3626.1881.g628f810_macosx64/tests/cefsimple/simple_app.cc:119:15: error:
      use of undeclared identifier 'x1'
                 if((x1 != 0 || size_parts[0] == "0") && (x2 != 0 || siz...
                     ^
/Users/lucasyoung/Desktop/cef_binary_3.3626.1881.g628f810_macosx64/tests/cefsimple/simple_app.cc:119:52: error:
      use of undeclared identifier 'x2'
                 if((x1 != 0 || size_parts[0] == "0") && (x2 != 0 || siz...


Pardon my ignorance.
whittyusername69
Techie
 
Posts: 13
Joined: Sat Jan 12, 2019 9:34 pm

Re: Need assistance with syntax.

Postby ndesktop » Tue Feb 05, 2019 3:26 pm

For switches you need to include cef_switches.h (or define a new file on your own and include it into your project). cefclient source code is the example to follow.

CW_USEDEFAULT are constants from Windows (I assumed - wrongly - this platform). You can replace them with some invalid constants, say 0xFFFFFFFE, but you need to handle the errors yourself.
(These Windows constants for size/position means "let the system position your window").

For atoi add at your source file
#include <stdlib.h>
ndesktop
Master
 
Posts: 756
Joined: Thu Dec 03, 2015 10:10 am


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 91 guests