Автор Тема: GtkBuilder и Custom widgets  (Прочетена 1906 пъти)

mazzzterZ

  • Напреднали
  • *****
  • Публикации: 37
  • Distribution: Ubuntu
  • Window Manager: Gnome
    • Профил
GtkBuilder и Custom widgets
« -: Dec 23, 2009, 14:21 »
Здравейте !

Трябва ми чрез GtkBuilder да зареда форма направена на Glade-3. Във формата съм сложил контрол, който е наследник на контрол от gtkmm-to(в този пример е насмедник на Gtk::Button).
Програмата се стартира без никакви проблеми, и ако съм я стартирам без да съм сложил моя контрол всичко тръгва, но ако е там ми хвърля изключение:
Код:
BuilderError: Invalid object type `MyWidget'
Никаде из гугле не можах да намеря изчерпателна документация или пълен работещ пример. Иначе ето ми файловете:

main.cpp
Код:
#include "mywidget.h"
#include <iostream>

int main (int argc, char **argv)
{
  Gtk::Main kit(argc, argv);

  //Load the Glade file and instiate its widgets:
  Glib::RefPtr<Gtk::Builder> refBuilder = Gtk::Builder::create();
#ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
    refBuilder->add_from_file("basic.ui");
  }
  catch(const Glib::FileError& ex)
  {
    std::cerr << "FileError: " << ex.what() << std::endl;
    return 1;
  }
  catch(const Gtk::BuilderError& ex)
  {
    std::cerr << "BuilderError: " << ex.what() << std::endl;
    return 1;
  }
#else
  std::auto_ptr<Glib::Error> error;

  if (!refBuilder->add_from_file("basic.ui", error))
  {
    std::cerr << error->what() << std::endl;
    return 1;
  }
#endif /* !GLIBMM_EXCEPTIONS_ENABLED */

  //Get the GtkBuilder-instantiated dialog::
  Gtk::Window *window;
  refBuilder->get_widget("Window", window);

  //Start:
  kit.run(*window);

  return 0;
}

mywidget.h
Код:
#include <gtkmm.h>
#include <iostream>


class MyWidget : public Gtk::Button
{
public:
  MyWidget();
  virtual ~MyWidget();

protected:

  //Overrides:
  virtual void on_size_request(Gtk::Requisition* requisition);
  virtual void on_size_allocate(Gtk::Allocation& allocation);
  virtual void on_map();
  virtual void on_unmap();
  virtual void on_realize();
  virtual void on_unrealize();
  virtual bool on_expose_event(GdkEventExpose* event);

  Glib::RefPtr<Gdk::Window> m_refGdkWindow;

  int m_scale;
};

mywidget.cpp
Код:
#include "mywidget.h"

extern "C" {
GType my_widget_get_type (void) {
return MyWidget::get_type();
}
GtkButton* my_widget_new (void) {
return manage(new MyWidget())->gobj();
}
}

MyWidget::MyWidget() :
  //The GType name will actually be gtkmm__CustomObject_mywidget
  Glib::ObjectBase("mywidget"),
  Gtk::Button(),
  m_scale(1000)
{
  set_flags(Gtk::NO_WINDOW);

  //This shows the GType name, which must be used in the RC file.
  std::cout << "GType name: " << G_OBJECT_TYPE_NAME(gobj()) <<
std::endl;

  //This show that the GType still derives from GtkWidget:
  //std::cout << "Gtype is a GtkWidget?:" << GTK_IS_WIDGET(gobj()) << std::endl;

  //Install a style so that an aspect of this widget may be themed via an RC
  //file:
  gtk_widget_class_install_style_property(GTK_WIDGET_CLASS(
              G_OBJECT_GET_CLASS(gobj())),
      g_param_spec_int("example_scale",
        "Scale of Example Drawing",
        "The scale to use when drawing. This is just a silly example.",
        G_MININT,
        G_MAXINT,
        0,
        G_PARAM_READABLE) );

  gtk_rc_parse("custom_gtkrc");
}

MyWidget::~MyWidget()
{
}

void MyWidget::on_size_request(Gtk::Requisition* requisition)
{
  //Initialize the output parameter:
  *requisition = Gtk::Requisition();

  //Discover the total amount of minimum space needed by this widget.

  //Let's make this simple example widget always need 50 by 50:
  requisition->height = 50;
  requisition->width = 50;
}

void MyWidget::on_size_allocate(Gtk::Allocation& allocation)
{
  //Do something with the space that we have actually been given:
  //(We will not be given heights or widths less than we have requested, though
  //we might get more)

  //Use the offered allocation for this container:
  set_allocation(allocation);

  if(m_refGdkWindow)
  {
    m_refGdkWindow->move_resize( allocation.get_x(), allocation.get_y(),
            allocation.get_width(), allocation.get_height() );
  }
}

void MyWidget::on_map()
{
  //Call base class:
  Gtk::Widget::on_map();
}

void MyWidget::on_unmap()
{
  //Call base class:
  Gtk::Widget::on_unmap();
}

void MyWidget::on_realize()
{
  //Call base class:
  Gtk::Widget::on_realize();

  ensure_style();

  //Get the themed style from the RC file:
  get_style_property("example_scale", m_scale);
  std::cout << "m_scale (example_scale from the theme/rc-file) is: "
      << m_scale << std::endl;

  if(!m_refGdkWindow)
  {
    //Create the GdkWindow:

    GdkWindowAttr attributes = {0};

    Gtk::Allocation allocation = get_allocation();

    //Set initial position and size of the Gdk::Window:
    attributes.x = allocation.get_x();
    attributes.y = allocation.get_y();
    attributes.width = allocation.get_width();
    attributes.height = allocation.get_height();

    attributes.event_mask = get_events () | Gdk::EXPOSURE_MASK;
    attributes.window_type = GDK_WINDOW_CHILD;
    attributes.wclass = GDK_INPUT_OUTPUT;

    m_refGdkWindow = Gdk::Window::create(get_window() /* parent */,
&attributes,
            GDK_WA_X | GDK_WA_Y);
    unset_flags(Gtk::NO_WINDOW);
    set_window(m_refGdkWindow);

    //set colors
    modify_bg(Gtk::STATE_NORMAL , Gdk::Color("red"));
    modify_fg(Gtk::STATE_NORMAL , Gdk::Color("blue"));

    //make the widget receive expose events
    m_refGdkWindow->set_user_data(gobj());
  }
}

void MyWidget::on_unrealize()
{
  m_refGdkWindow.clear();

  //Call base class:
  Gtk::Widget::on_unrealize();
}

bool MyWidget::on_expose_event(GdkEventExpose* event)
{
  if(m_refGdkWindow)
  {
    double scale_x = (double)get_allocation().get_width() / m_scale;
    double scale_y = (double)get_allocation().get_height() / m_scale;

    Cairo::RefPtr<Cairo::Context> cr =
m_refGdkWindow->create_cairo_context();
    if(event)
    {
      // clip to the area that needs to be re-exposed so we don't draw any
      // more than we need to.
      cr->rectangle(event->area.x, event->area.y, event->area.width, event->area.height);
      cr->clip();
    }

    // paint the background
    Gdk::Cairo::set_source_color(cr,
get_style()->get_bg(Gtk::STATE_NORMAL));
    cr->paint();

    // draw the foreground
    Gdk::Cairo::set_source_color(cr,
get_style()->get_fg(Gtk::STATE_NORMAL));
    cr->move_to(155.*scale_x, 165.*scale_y);
    cr->line_to(155.*scale_x, 838.*scale_y);
    cr->line_to(265.*scale_x, 900.*scale_y);
    cr->line_to(849.*scale_x, 564.*scale_y);
    cr->line_to(849.*scale_x, 438.*scale_y);
    cr->line_to(265.*scale_x, 100.*scale_y);
    cr->line_to(155.*scale_x, 165.*scale_y);
    cr->move_to(265.*scale_x, 100.*scale_y);
    cr->line_to(265.*scale_x, 652.*scale_y);
    cr->line_to(526.*scale_x, 502.*scale_y);
    cr->move_to(369.*scale_x, 411.*scale_y);
    cr->line_to(633.*scale_x, 564.*scale_y);
    cr->move_to(369.*scale_x, 286.*scale_y);
    cr->line_to(369.*scale_x, 592.*scale_y);
    cr->move_to(369.*scale_x, 286.*scale_y);
    cr->line_to(849.*scale_x, 564.*scale_y);
    cr->move_to(633.*scale_x, 564.*scale_y);
    cr->line_to(155.*scale_x, 838.*scale_y);
    cr->stroke();
  }
  return true;
}

catalog.xml
Код:
<?xml version="1.0" encoding="UTF-8"?>
<glade-catalog name="mywidget" domain="glade-3" depends="gtk+" library="mywidge">
<init-function>cncwidgets_catalog_init</init-function>
<!-- Widget definitions -->
<glade-widget-classes>

<glade-widget-class  title="My widget" name="MyWidget" generic-name="mywidget" >
</glade-widget-class>

</glade-widget-classes>

<!-- Catalog definitions -->
<glade-widget-group name="CNCWidgets" title="CNC Interface Widgets">
<glade-widget-class-ref  name="MyWidget"/>
</glade-widget-group>


</glade-catalog>

basic.ui
Код:
<?xml version="1.0"?>
<interface>
  <!-- interface-requires gtk+ 2.12 -->
  <!-- interface-requires mywidget 0.0 -->
  <!-- interface-naming-policy toplevel-contextual -->
  <object class="GtkWindow" id="Window">
    <child>
      <object class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="label" translatable="yes">This is a test !!!</property>
          </object>
          <packing>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="MyWidget" id="mywidget1">
            <property name="label" translatable="yes">button</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

Забравих да спомена, че работната ми среда е KDevelop 3.5.2 и че от mywidget.cpp и mywidget.h правя библиотека(shared - *.so), както е показано във FAQ-ана KDevelop:
http://www.kdevelop.org/mediawiki/index.php/FAQ#How_to_create_a_.so_library.3F
Ако имате някакви идеи моля пишете.
Активен

Подобни теми
Заглавие Започната от Отговора Прегледи Последна публикация
Компилиране на custom kernel
Настройка на програми
eNcLaVe 5 2655 Последна публикация Feb 20, 2005, 16:36
от RedIce
Custom .iso
Идеи и мнения
alabal 5 2990 Последна публикация Mar 05, 2005, 23:33
от alabal
Custom Linux repository
Хардуерни и софтуерни проблеми
rcbandit 1 1962 Последна публикация Aug 12, 2011, 22:28
от AMD
KDE 4.7 проблеми с няколко widgets
Десктопи
sianbg 1 3395 Последна публикация Dec 14, 2011, 08:44
от Warstomp
KDE 4.7 проблеми с няколко widgets
Десктопи
linuxgaming 0 15288 Последна публикация Mar 03, 2020, 06:13
от linuxgaming