Re: Finding the .glade file



Am Sat, 05 Apr 2008 16:23:15 +0100 schrieb Joe:

> Hi.
> 
> I am not sure exactly how to go about pointing my program at the glade
> file when the program is installed.
> 
> I most likely need environment variables but I have no idea how to do
> this.
> 
> I have written the application using the anjuta IDE, and the .glade
> seems to install into /usr/share/appname, however I cannot be certain
> this is the same for all distributions.

Hello,

I use that code to load my .glade files:

It enabled you to load from the prefix path, and some local path
or included in the binary. Very useful for development or 
distribution as binary package (e.g. on win32).

#ifdef GLADE_HEADER_COMPILE
#include "app_glade.h"
#else
#define APP_GLADE_FILE "app.glade"
#endif

Glib::RefPtr<Gnome::Glade::Xml> createGlade (const Glib::ustring &root, 
                                             const Glib::ustring &domain)
{
#ifdef GLADE_HEADER_COMPILE
  return Gnome::Glade::Xml::create_from_buffer(
         pogo_glade_data, pogo_glade_data_size,
         root, domain);
#else
  return Gnome::Glade::Xml::create(
         searchGladeFile (APP_GLADE_FILE), root, domain);
#endif
}

const std::string searchGladeFile (const std::string &glade_file)
{
  vector <string> name_vector;
  
  name_vector.push_back (glade_file);
  name_vector.push_back ("../" + glade_file);
  name_vector.push_back ("src/" + glade_file);
  // TODO: src dir variable...
  name_vector.push_back (string (PACKAGE_DATA_DIR) + "/glade/" + glade_file);
  
  return searchFile (name_vector);
} 

const std::string searchFile (std::vector <std::string> &name_vector)
{
  struct stat buf;

  for (unsigned int i = 0; i < name_vector.size (); i++)
  {
    string &try_name = name_vector[i];

    bool found = !(stat (try_name.c_str (), &buf));

    if (found)
    {
      return try_name;
    }
  }

  return "";
}

Your application needs to be based on autotools stuff to get that working.
Use this macro:

dnl Check for option to enable glade_h
AC_MSG_CHECKING(whether to enable glade header compile)
AC_ARG_ENABLE(glade_header_compile,
        [  --enable-glade_header_compile=[no/yes]        enables to compile the glade file into the executable (default=no)],,
                 enable_glade_header_compile=no)


if [ ! test "x$enable_glade_header_compile" != "xyes"]; then
  AC_DEFINE(GLADE_HEADER_COMPILE, 1, [Define to enable to compile the glade header])
  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no)
fi


And this in autogen.sh:

perl src/bin2hex.pl src/app.glade 1 app_glade_data > src/app_glade.h

I modified the bin2hex.pl from the original author to get it working for my
special use case. The modified code follows below:

# bin2hex.pl by Chami.com
# http://www.chami.com/tips/
#

# number of characters per line
$chars_per_line = 15;

# -------------------------------------

# language id
#
# 0 = Perl (default)
# 1 = C / C++
# 2 = Pascal / Delphi
#
$lang  = $ARGV[1];

$argc = $#ARGV + 1;

if ($argc == 3)
{
  $array_name = $ARGV[2];
}
else
{
  $array_name = "bin_data";
}

$rem_begin  = "begin binary data:";
$rem_end    = "end binary data.";

# initialize for Perl strings
# by default
$_var       = "# $rem_begin\n".
              "\$bin_data = # %d\n";
$_begin     = "\"";
$_end       = "\";\n";
$_break     = "\".\n\"";
$_format    = "\\x%02X";
$_separator = "";
$_comment   = "# $rem_end ";
$_size      = "# size = %d - size of binary data in bytes";


# C / C++
if(1 == $lang)
{
  $_var       = "/* $rem_begin */\n".
                "char " . $array_name . "[] = ".
                "/* %d */\n";
  $_begin     = "{";
  $_end       = "};\n";
  $_break     = "\n";
  $_format    = "0x%02X";
  $_separator = ",";
  $_comment   = "/* $rem_end */";
  $_size      = "unsigned int " . $array_name  . "_size = %d; /* size of binary data in bytes */";
}
elsif(2 == $lang)
{
  $_var       = "{ $rem_begin }\n".
                "const bin_data : ".
                "array [1..%d] of ".
                "byte = \n";
  $_begin     = "(";
  $_end       = ");\n";
  $_break     = "\n";
  $_format    = "\$%02X";
  $_separator = ",";
  $_comment   = "{ $rem_end }";
  $_size       = "{size = %d bytes }";
}

if(open(F, "<".$ARGV[0]))
{
  binmode(F);

  $s = '';
  $i = 0;
  $count = 0;
  $first = 1;
  $s .= $_begin;
  while(!eof(F))
  {
    if($i >= $chars_per_line)
    {
      $s .= $_break;
      $i = 0;
    }
    if(!$first)
    {
      $s .= $_separator;
    }
    $s .= sprintf(
            $_format, ord(getc(F)));
    ++$i;
    ++$count;
    $first = 0;
  }
  $s .= $_end;
  $s .= sprintf $_comment;
  $s .= "\n\n";
  $s .= sprintf $_size, $count;
  $s .= "\n\n";

  $s = "\n".sprintf($_var, $count).$s;

  print $s;

  close( F );
}
else
{
  print
    "bin2hex.pl by Chami.com\n".
    "\n".
    "usage:\n".
    "  perl bin2hex.pl <binary file>".
    " <language id> [<array_name>]\n".
    "\n".
    "  <binary file> : path to the ".
    "binary file\n".
    "  <language id> : 0 = Perl, ".
    "1 = C/C++/Java, ".
    "2 = Pascal/Delphi\n".
    "  <array_name>  : name of the array or 'bin_data' as fallback" .
    "\n";
}

So you're also able to include the .glade file into your binary. Very useful for
binary only distributions.

Have fun with it!

If there're some questions feel free to ask.

regards
Andreas


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]