0 Replies - 3074 Views - Last Post: 28 April 2014 - 08:38 PM

#1 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

NASM/GTK+ - Listing all widget ids from glade file

Post icon  Posted 28 April 2014 - 08:38 PM

I have a ui file created with Glade with well over 300 widgets. It is very tedious to copy the widget names from Glade, over to the Assembly source file and add to the data section. I needed a way to programmatically list all of the widget names in the glade file. It turns out, gtk_widget get_name does not do what you think it would do. It returns the class type of the widget, and this drove me crazy.
This is what I came up with.

Sample Glade file - test.ui
<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkWindow" id="win_main">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkGrid" id="grid1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="margin_left">5</property>
        <property name="margin_right">5</property>
        <property name="margin_top">5</property>
        <property name="margin_bottom">5</property>
        <property name="row_spacing">5</property>
        <property name="column_spacing">5</property>
        <property name="row_homogeneous">True</property>
        <property name="column_homogeneous">True</property>
        <child>
          <placeholder/>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <object class="GtkButton" id="btn_push_me">
            <property name="label" translatable="yes">Push Me</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">1</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="lbl_hello">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">Hello there!</property>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">0</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkLinkButton" id="lnk_homepage">
            <property name="label" translatable="yes">&lt;dream.in.code&gt;</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">True</property>
            <property name="has_tooltip">True</property>
            <property name="relief">none</property>
            <property name="uri">http://www.dreamincode.net/</property>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">2</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkImage" id="img_sample">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="stock">gtk-dialog-error</property>
          </object>
          <packing>
            <property name="left_attach">2</property>
            <property name="top_attach">1</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry" id="txt_hello">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="invisible_char">●</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">1</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>


list_glad_ids.asm
extern gtk_init_check
extern gtk_builder_new, gtk_builder_add_from_file, gtk_builder_get_objects 
extern g_object_unref, g_printf, g_slist_foreach, g_slist_free, g_slist_length
extern gtk_buildable_get_name

extern exit

NULL    equ 0
 
global main

section .data 
szUIFile        db  "test.ui", 0
fmtstr          db  "%s", 10, 0

section .text
main:
    sub     rsp, 8 * 3

    mov     [rsp + 8], rdi                  ; argc
    lea     rdi, [rsp + 8]
    mov     [rsp], rsi                      ; argv
    mov     rsi, rsp        
    call    gtk_init_check                  ; initialize GTK+ and terminate if FALSE
    add     rsp, 8 * 2 
    test    rax, rax
    jz      .Done
       
    call    gtk_builder_new                 ; create new empty builder object
    mov     r15, rax
         
    mov     rdx, NULL  
    mov     rsi, szUIFile
    mov     rdi, rax  
    call    gtk_builder_add_from_file       ; build the GTKBuilder UI definition
    
    mov     rdi, r15
    call    gtk_builder_get_objects         ; get all objects constructed by builder
    mov     r14, rax
    
    mov     rdx, 0
    mov     rsi, Show
    mov     rdi, rax
    call    g_slist_foreach                 ; loop through each node in GSList and call Show for each

    mov     rdi, r14
    call    g_slist_free                    ; free GSList
    
    mov     rdi, r15
    call    g_object_unref                  ; decrease object count and free mem
    
.Done:
    mov     rdi, 0
    call    exit
    
;~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Show:
    call    gtk_buildable_get_name          ; get ID (name) of widget in current node of GSList
    
    mov     rsi, rax
    mov     rdi, fmtstr
    xor     rax, rax
    call    g_printf                        ; display it
    ret



makefile
APP=list_glade_ids

all: $(APP)

$(APP): $(APP).o
	gcc -s -o $(APP) $(APP).o `pkg-config --cflags --libs gtk+-3.0` 

$(APP).o: $(APP).asm
	nasm -f elf64 $(APP).asm 



Plop all three files into a directory, run make and the output should be:
Attached Image

Is This A Good Question/Topic? 0
  • +

Page 1 of 1