Buchempfehlung
Visual Basic 6 Kochbuch
Visual Basic 6 Kochbuch
Viele praktische Tipps zum Programmieren mit Visual Basic 6, die sich oft auch auf FB übertragen lassen. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

Code-Beispiel

Code-Beispiele » GTK, WxWidgets etc.

Scribble (GTK / GtkBuilder)

Lizenz:Erster Autor:Letzte Bearbeitung:
GPLMitgliedTJF 04.05.2011
Fenster von Scribble
Vergrößern
Fenster von Scribble

Dieses Beispiel betrifft ein einfaches Zeichenprogramm mit grafischer Benutzer-Schnittstelle (Oberfläche) unter Verwendung des Gimp Tool Kits (= GTK). Verwendet wird ein GTK-Zeichenbereich (GtkDrawingArea). Der Code kann für verschiedene windows-Versionen und LINUX kompiliert werden.

Das Beispiel ist dem Externer Link!GTK Tutorial entnommen und wird unter Verwendung von Externer Link!Glade3, GtkBuilder und GTK+tobac in FreeBasic realisiert.

Zunächst wird mit dem Programm Externer Link!Glade3 die grafische Oberfläche erstellt und als GUI-XML-Datei gespeichert. Diese Datei wird anschließend im Programm GTK+tobac eingelesen und dort wird der FreeBasic Quelltext als Rahmen erzeugt. Die notwendigen Informationen hierzu liest GTK+tobac aus der GUI-XML-Datei, welche ebenfalls mit GTK+tobac für die Einbindung in den Quelltext vorbereitet wird.

Dieser Rahmen-Quelltext wird schließlich durch wenige Zeilen mit der gewünschten Funktionalität ergänzt und abschließend mit fbc kompiliert.

FreeBasic unterstützt in den offiziellen Versionen bisher (bis 0.21.1 einschließlich) die Verwendung von libglade, welche jedoch veraltet ist und nicht mehr weiterentwickelt wird. In diesem Beispiel wird die aktuelle Alternative GtkBuilder zur Handhabung der GUI-XML-Daten verwendet. Dies setzt die Installation einer GTK+ Version >= 2.16 voraus (Externer Link!Windows Download). Auch ist zur Kompilierung der aktualisierte FB header GTK-2.18.6_TJF.bi oder GTK-2.22.0_TJF.bi notwendig, welcher im Paket GTK+tobac enthalten ist.

' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'< main program generated by utility                 GTK+tobac2 V2.2.0 >
'< Hauptprogramm erzeugt von                                           >
'< Generated at / Generierung am                     2010-09-06, 01:24 >
' ---------------------------------------------------------------------
'< Program info:                                                       >
CONST PROG_NAME = "Scribble" '                                         >
CONST PROG_DESC = "Zeichenprogramm" '                                  >
CONST PROG_VERS = "0.0" '                                              >
CONST PROG_YEAR = "2010" '                                             >
CONST PROG_AUTH = "TJF" '                                              >
CONST PROG_MAIL = "Thomas.Freiherr@gmx.net" '                          >
CONST PROG_WEBS = "members.aon.at/tjf" '                               >
'<                                                                     >
'< Description / Beschreibung:                                         >
'< Beispiel DrawArea, einfaches Zeichenprogramm                        >
'<                                                                     >
'< License / Lizenz:                                                   >
'<                                                                     >
'< Redistribution and use in source and binary forms, with or          >
'< without modification, are permitted provided that the following     >
'< conditions are met:                                                 >
'<                                                                     >
'< * Redistributions of source code must retain the above              >
'< copyright notice, this list of conditions and the following         >
'< disclaimer.                                                         >
'<                                                                     >
'< * Redistributions in binary form must reproduce the above           >
'< copyright notice, this list of conditions and the following         >
'< disclaimer in the documentation and/or other materials provided     >
'< with the distribution.                                              >
'<                                                                     >
'< * Neither the name of the author nor the names of its               >
'< contributors may be used to endorse or promote products derived     >
'< from this software without specific prior written permission.       >
'<                                                                     >
'< THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS                  >
'< AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED                 >
'< WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES   >
'< OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE             >
'< ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER               >
'< OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,     >
'< SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT    >
'< LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF    >
'< USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED     >
'< AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT         >
'< LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN   >
'< ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE     >
'< POSSIBILITY OF SUCH DAMAGE.                                         >
'<                                                                     >
' ---------------------------------------------------------------------
'< Please prefer GNU GENERAL PUBLIC LICENSE to support open software.  >
'< For more information please visit:               http://www.fsf.org >
'<                                                                     >
'< Bitte bevorzugen Sie die GNU GENERAL PUBLIC LICENSE und             >
'< unterstuetzen Sie mit Ihrem Programm die freie Software             >
'< Mehr Informationen finden Sie unter:             http://www.fsf.org >
' ---------------------------------------------------------------------
'<  GTK+tobac:             general init / Allgemeine Initialisierungen >
    #INCLUDE "gtk/GTK-2.18.6_TJF.bi" '   GTK+library / GTK+ Bibliothek >
    gtk_init(@__FB_ARGC__, @__FB_ARGV__) '     start GKT / GTK starten >
'<  GTK+tobac:                                   end block / Blockende >
' vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

/' Backing pixmap for drawing area '/
DIM SHARED AS GdkPixmap PTR PIXMAP = NULL
DECLARE SUB draw_brush(BYVAL widget AS GtkWidget PTR, _
                       BYVAL x AS gdouble, _
                       BYVAL y AS gdouble)

' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'< _tobac.bas modul generated by utility             GTK+tobac2 V2.2.0 >
'< Modul _tobac.bas erzeugt von                                        >
'< Generated at / Generierung am                     2010-09-06, 02:45 >
' vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

IF gtk_check_version(2, 16, 0) THEN
  ?"Fehler/Error (GTK-Version): Version zu alt"
  END -1
END IF

DIM AS STRING GUISTR
DIM SHARED AS GtkBuilder PTR XML
DIM SHARED AS GObject PTR _
  WinScribble, Draw1, ButQuit

XML = gtk_builder_new()

SCOPE
  DIM AS GError PTR meld

  ' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  '< _gui.bas modul generated by utility               GTK+tobac2 V2.2.0 >
  '< Modul _gui.bas erzeugt von                                          >
  '< Generated at / Generierung am                     2010-09-06, 02:45 >
  ' vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

  GUIstr =_
  "<?xml version=""1.0""?>"+_
  "<interface>"+_
  "<requires lib=""gtk+"" version=""2.16""/>"+_
  "<!-- interface-naming-policy project-wide -->"+_
  "<object class=""GtkWindow"" id=""WinScribble"">"+_
  "<signal name=""destroy"" handler=""gtk_main_quit""/>"+_
  "<child>"+_
  "<object class=""GtkVBox"" id=""vbox1"">"+_
  "<property name=""visible"">True</property>"+_
  "<child>"+_
  "<object class=""GtkDrawingArea"" id=""Draw1"">"+_
  "<property name=""width_request"">200</property>"+_
  "<property name=""height_request"">200</property>"+_
  "<property name=""visible"">True</property>"+_
  "<property name=""events"">GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_STRUCTURE_MASK</property>"+_
  "<signal name=""button_press_event"" handler=""on_Draw1_button_press_event""/>"+_
  "<signal name=""motion_notify_event"" handler=""on_Draw1_motion_notify_event""/>"+_
  "<signal name=""expose_event"" handler=""on_Draw1_expose_event""/>"+_
  "<signal name=""configure_event"" handler=""on_Draw1_configure_event""/>"+_
  "</object>"+_
  "<packing>"+_
  "<property name=""position"">0</property>"+_
  "</packing>"+_
  "</child>"+_
  "<child>"+_
  "<object class=""GtkButton"" id=""ButQuit"">"+_
  "<property name=""label"" translatable=""yes"">Quit</property>"+_
  "<property name=""visible"">True</property>"+_
  "<property name=""can_focus"">True</property>"+_
  "<property name=""receives_default"">True</property>"+_
  "<signal name=""clicked"" handler=""gtk_main_quit""/>"+_
  "</object>"+_
  "<packing>"+_
  "<property name=""expand"">False</property>"+_
  "<property name=""fill"">False</property>"+_
  "<property name=""position"">1</property>"+_
  "</packing>"+_
  "</child>"+_
  "</object>"+_
  "</child>"+_
  "</object>"+_
  "</interface>"
  IF 0 = gtk_builder_add_from_string(XML, SADD(GUISTR), LEN(GUISTR), @meld) THEN
    WITH *meld
      ?"Fehler/Error (GTK-Builder):"
      ?*.message
    END WITH
    END -1
  END IF
END SCOPE

WinScribble = gtk_builder_get_object(XML, "WinScribble")
Draw1 = gtk_builder_get_object(XML, "Draw1")
ButQuit = gtk_builder_get_object(XML, "ButQuit")

' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'< callback modul generated by utility               GTK+tobac2 V2.2.0 >
'< callback-Modul erzeugt von                                          >
'< Generated at / Generierung am                     2010-09-06, 01:24 >
' vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

FUNCTION on_Draw1_button_press_event CDECL ALIAS "on_Draw1_button_press_event" _
         (BYVAL widget AS GtkWidget PTR, _
          BYVAL event AS GdkEventButton PTR) AS gboolean EXPORT
  IF event->button = 1 THEN IF PIXMAP THEN draw_brush(widget, event->x, event->y)
  RETURN TRUE
END FUNCTION

' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'< callback modul generated by utility               GTK+tobac2 V2.2.0 >
'< callback-Modul erzeugt von                                          >
'< Generated at / Generierung am                     2010-09-06, 01:24 >
' vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

FUNCTION on_Draw1_motion_notify_event CDECL ALIAS "on_Draw1_motion_notify_event" _
         (BYVAL widget AS GtkWidget PTR, _
          BYVAL event AS GdkEventMotion PTR) AS gboolean EXPORT
  DIM AS INTEGER x, y
  DIM AS GdkModifierType state

  IF event->is_hint THEN
    gdk_window_get_pointer(event->window, @x, @y, @state)
  ELSE
    x = event->x
    y = event->y
    state = event->state
  END IF

  IF state AND GDK_BUTTON1_MASK THEN IF PIXMAP THEN draw_brush(widget, x, y)
  RETURN TRUE
END FUNCTION

' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'< callback modul generated by utility               GTK+tobac2 V2.2.0 >
'< callback-Modul erzeugt von                                          >
'< Generated at / Generierung am                     2010-09-06, 01:24 >
' vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

FUNCTION on_Draw1_expose_event CDECL ALIAS "on_Draw1_expose_event" _
         (BYVAL widget AS GtkWidget PTR, _
          BYVAL event AS GdkEventExpose PTR) AS gboolean EXPORT
  gdk_draw_drawable(widget->window, _
                    widget->style->fg_gc(gtk_widget_get_state(widget)), _
                    PIXMAP, _
                    event->area.x, event->area.y, _
                    event->area.x, event->area.y, _
                    event->area.width, event->area.height)
  RETURN FALSE
END FUNCTION

' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'< callback modul generated by utility               GTK+tobac2 V2.2.0 >
'< callback-Modul erzeugt von                                          >
'< Generated at / Generierung am                     2010-09-06, 01:24 >
' vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

FUNCTION on_Draw1_configure_event CDECL ALIAS "on_Draw1_configure_event" _
        (BYVAL widget AS GtkWidget PTR, _
         BYVAL event AS GdkEventConfigure PTR) AS gboolean EXPORT
  IF PIXMAP THEN g_object_unref(pixmap)

  PIXMAP = gdk_pixmap_new(widget->window, _
                          widget->allocation.width, _
                          widget->allocation.height, _
                          -1)
  gdk_draw_rectangle(PIXMAP, _
                     widget->style->white_gc, _
                     TRUE, _
                     0, 0, _
                     widget->allocation.width, _
                     widget->allocation.height)
  RETURN TRUE
END FUNCTION


SUB draw_brush(BYVAL widget AS GtkWidget PTR, _
               BYVAL x AS gdouble, _
               BYVAL y AS gdouble)
  DIM AS GdkRectangle update_rect

  update_rect.x = x - 5
  update_rect.y = y - 5
  update_rect.width = 10
  update_rect.height = 10
  gdk_draw_rectangle(PIXMAP, _
                     widget->style->black_gc, _
                     TRUE, _
                     update_rect.x, update_rect.y, _
                     update_rect.width, update_rect.height)
  gtk_widget_queue_draw_area(widget, _
                             update_rect.x, update_rect.y, _
                             update_rect.width, update_rect.height)
END SUB

' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'<  GTK+tobac:   run GTK main, then end / GTK Hauptschleife, dann Ende >
    gtk_builder_connect_signals(XML, 0) '           callbacks anbinden >
    gtk_widget_show_all(GTK_WIDGET(WinScribble)) 'ptfenster darstellen >
    gtk_main() '                             main loop / Hauptschleife >
    g_object_unref(XML) '               dereference / Referenz abbauen >
'<  GTK+tobac:                                   end block / Blockende >
' vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

' you may unref files here / ggf. Dateien hier schliessen

END 0 ' finish with return code 0 / Ende mit Returncode 0

English

This example is about a simple drawing application with grafical user interface (= GUI), using the gimp tool kit (= GTK). It uses a GtkDrawingArea. The source is to compile at different windows versions and LINUX.

The example is from the Externer Link!GTK tutorial. It's realized in FreeBasic by using Externer Link!Glade3, GtkBuilder and GTK+tobac.

First, the GUI is designed with Externer Link!Glade3 and saved as a GUI-XML-file. This file gets loaded by GTK+tobac and a FB source code frame gets sketched. GTK+tobac reads the needed information form the GUI-XML-file. And it generates some code to include the GUI-XML-file into the source.

At last, the individual features (just a few lines of code) are inserted into the code frame and the source gets compiled by fbc.

The official FB releases only support libglade yet (up to version 0.21.1). Libglade is deprecated. This example uses the current alternative GtkBuilder to handle the GUI-XML-data. It needs a GTK installation >= 2.16 (, see Externer Link!Windows Download). Also, an up-to-date FB header GTK-2.18.6_TJF.bi or GTK-2.22.0_TJF.bi is needed, included in the package GTK+tobac.


Zusätzliche Informationen und Funktionen
  • Das Code-Beispiel wurde am 06.09.2010 von MitgliedTJF angelegt.
  • Die aktuellste Version wurde am 04.05.2011 von MitgliedTJF gespeichert.
  Bearbeiten Bearbeiten  

  Versionen Versionen