Article Index

Because this example is part of a rebuild, the include files don't match with the proposed nasm include file directory. Adjust as needed.

;name: icon.asm
;
;description: Show the application icon
;
;source: http://zetcode.com/gui/gtk2/firstprograms/
;
;build:

bits 64

%include "icon.inc"

global _start

section .bss
    window:	resq  1
    icon:	resq  1
    pixbuf:	resq  1
    gerror:	resq  1	;pointer to GError structure
    
section	.rodata
    signal:
    .destroy:	db  "destroy",0
    title:	db  "Icon",0
    logo:	db  "resources/logo.png",0
    stringmask:	db  "%s",10,0
    
section .data
    
section .text

_start:
    ;initialize without arguments from commandline
    call    gtk_init
    ;create a new window
    xor	    rdi,rdi			;GTK_WINDOW_TOPLEVEL=0
    call    gtk_window_new		;rax = pointer to window
    mov	    qword[window],rax
    ;set the window title
    mov	    rsi,title			;set window title
    mov	    rdi,qword[window]
    call    gtk_window_set_title
    ;set the size of the window
    mov	    rdx,150			;height of window
    mov	    rsi,230			;width of window
    mov	    rdi,qword[window]
    call    gtk_window_set_default_size
    ;set the position of the window
    mov	    rsi,GTK_WIN_POS_CENTER	;center window on screen
    mov	    rdi,qword[window]
    call    gtk_window_set_position
    ;create a pixbuffer
    call    create_pixbuf
    mov	    qword[icon],rax		;save pointer to our icon
    ;rax has either a pointer to the pixbuffer or zero
    mov	    rsi,qword[icon]		;get the icon handle
    mov	    rdi,qword[window]
    call    gtk_window_set_icon		
    ;show the window
    mov	    rdi,qword[window]		;rdi = pointer to window
    call    gtk_widget_show
    ;connect the destroy signal to gtk_main_quit
    mov	    rdi,qword[window]
    mov	    rsi,signal.destroy
    mov	    rdx,gtk_main_quit
    xor     rcx, rcx                    ;pointer to the data to pass
    ;don't forget the next two instructions or the application
    ;will be "unexpectedly finished"
    xor     r9d, r9d                    ;combination of GConnectFlags 
    xor     r8d, r8d                    ;a GClosureNotify for data
    call    g_signal_connect_data
    ;run main window routine
    call    gtk_main
    ;exit program
    xor	    rdi,rdi
    call    exit

create_pixbuf:
    mov	    rsi,gerror
    mov	    rdi,logo			;set pointer to logo name
    call    gdk_pixbuf_new_from_file
    and	    rax,rax
    jnz	    .done
    ;rsi is a error handle, get pointer to error string
    mov	    rax,gerror			    ;load address to gerror structure address
    mov	    rax,[rax]			    ;load gerror address pointer in rax
    mov	    rdx,[rax+GERROR_STRUC.message]  ;load address of error message in rsi
    mov	    rsi,stringmask		    ;load mask to display the error
    mov	    rax,stderr			    ;load address to stderr structure address
    mov	    rdi,[rax]			    ;load stderr structure address
    xor	    rax,rax			    ;no floating point arguments
    call    fprintf			    ;print error message
    ;get pointer to error handle
    mov	    rdi,gerror
    call    g_error_free		    ;release error memory space
.done:
    ret