Can anyone kindly translate this GTK program?

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
taijinantangXX
Posts: 13
Joined: Dec 19, 2014 5:29

Can anyone kindly translate this GTK program?

Postby taijinantangXX » Mar 23, 2015 5:25

Could anyone kindly translate this GTK sample program for me?
from C to FB
Thank you!

Code: Select all

#include <gtk-2.0/gtk/gtk.h>

#define NAME    "root"
#define PWD     "123456"

//structure of main window

struct login_widget{
        GtkWidget *window; //main window
        GtkWidget *label_tips; //tooltip
        GtkWidget *button_login; //Two buttons, one for login, one exit
        GtkWidget *button_exit;
        GtkWidget *entry_username; //two text boxes for username and password
        GtkWidget *entry_pwd;
        GtkWidget *label_username; //two labels for showing username and password text box tooltips
        GtkWidget *label_pwd;
        GtkWidget *vbox; //vertical box,contains three horizontal boxes
        GtkWidget *hbox_username; //include username tooltip and username text box. HBOX is similar
        GtkWidget *hbox_pwd;
        GtkWidget *hbox_button;
}wgt;

void gtk_win_destroy(GtkWidget *widget,gpointer data)
{
        gtk_main_quit();
}

//login verification , when login button clicked

void login(GtkWidget *widget,gpointer data)
{
        gchar *name;
        gchar *pwd;
        struct login_widget *wgt;

        wgt = (struct loging_widget *)data;

        //get input text

        name = gtk_entry_get_text(GTK_ENTRY(wgt->entry_username));
        pwd = gtk_entry_get_text(GTK_ENTRY(wgt->entry_pwd));

        if((strcmp(name,NAME) == 0) && (strcmp(pwd,PWD) == 0))
        {
                gtk_label_set_text(GTK_LABEL(wgt->label_tips),"Welcome to GTK+-2.0");
        }
        else
        {
                gtk_label_set_text(GTK_LABEL(wgt->label_tips),"Invalid user name or password.");
        }
}

void init_login_widget()
{
        wgt.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        wgt.label_tips = gtk_label_new("Welcome,please login first");
        wgt.button_login = gtk_button_new_with_label("login");
        wgt.button_exit = gtk_button_new_with_label("exit");
        wgt.entry_username = gtk_entry_new_with_max_length(10);
        wgt.entry_pwd = gtk_entry_new_with_max_length(10);
        wgt.label_username = gtk_label_new("User name :");
        wgt.label_pwd = gtk_label_new("Password :");
        wgt.vbox = gtk_vbox_new(FALSE,20);
        wgt.hbox_username = gtk_hbox_new(FALSE,20);
        wgt.hbox_pwd = gtk_hbox_new(FALSE,20);
        wgt.hbox_button = gtk_hbox_new(FALSE,20);

        //setup window

        gtk_window_set_title(GTK_WINDOW(wgt.window),"System login");
        gtk_window_set_resizable(GTK_WINDOW(wgt.window),FALSE);

        //setup vboxes and hboxes

        gtk_box_pack_start(GTK_BOX(wgt.hbox_username),wgt.label_username,TRUE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(wgt.hbox_username),wgt.entry_username,TRUE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(wgt.hbox_pwd),wgt.label_pwd,TRUE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(wgt.hbox_pwd),wgt.entry_pwd,TRUE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(wgt.hbox_button),wgt.button_login,TRUE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(wgt.hbox_button),wgt.button_exit,TRUE,FALSE,10);

        gtk_box_pack_start(GTK_BOX(wgt.vbox),wgt.label_tips,TRUE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(wgt.vbox),wgt.hbox_username,TRUE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(wgt.vbox),wgt.hbox_pwd,TRUE,FALSE,10);
        gtk_box_pack_start(GTK_BOX(wgt.vbox),wgt.hbox_button,TRUE,FALSE,10);

        //password invisible,show“#” when typing

        gtk_entry_set_visibility(GTK_ENTRY(wgt.entry_pwd),FALSE);
        gtk_entry_set_invisible_char(GTK_ENTRY(wgt.entry_pwd),'#');

        g_signal_connect(GTK_OBJECT(wgt.button_login),"clicked",GTK_SIGNAL_FUNC(login),&wgt);
        g_signal_connect(GTK_OBJECT(wgt.window),"destroy",GTK_SIGNAL_FUNC(gtk_win_destroy),NULL);
        g_signal_connect(GTK_OBJECT(wgt.button_exit),"clicked",GTK_SIGNAL_FUNC(gtk_win_destroy),NULL);

        gtk_container_add(GTK_CONTAINER(wgt.window),wgt.vbox);

        gtk_widget_show_all(wgt.window);
}

int main(int argc,char *argv[])
{
        gtk_init(&argc,&argv);

        init_login_widget();

        gtk_main();

        return 0;
}
TJF
Posts: 3481
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Can anyone kindly translate this GTK program?

Postby TJF » Mar 23, 2015 9:32

Hello taijinantangXX!

Old stuff here, GTK+-2 is outdated. And a lot of optimization potential: the window should be of type GtkDialog, no need for SHARED variable wgt, ... .

Anyway, here's an FB translation for 32 and 64 bit systems, adapted to be compiled against GTK+-3 as well

Code: Select all

'' old fashioned #INCLUDE, 32-bit only (shipped with fbc)
'#DEFINE __USE_GTK3__  ' optional to use GTK+-3 instead of GTK+-2
'#INCLUDE ONCE "gtk/gtk.bi"

'' modern #INCLUDEs, 32 and 64-bit compatible, see
'' http://www.freebasic.net/forum/viewtopic.php?p=190157#p190157
'#INCLUDE ONCE "Gir/Gtk-2.0.bi"
#INCLUDE ONCE "Gir/Gtk-3.0.bi"
#INCLUDE ONCE "Gir/_GObjectMacros-2.0.bi"

#DEFINE _NAME    "root"
#DEFINE _PWD     "123456"

'//structure of main window

TYPE login_widget
  AS GtkWidget PTR window '//main window
  AS GtkWidget PTR label_tips '//tooltip
  AS GtkWidget PTR button_login '//Two buttons, one for login, one exit
  AS GtkWidget PTR button_exit
  AS GtkWidget PTR entry_username '//two text boxes for username and password
  AS GtkWidget PTR entry_pwd
  AS GtkWidget PTR label_username '//two labels for showing username and password text box tooltips
  AS GtkWidget PTR label_pwd
  AS GtkWidget PTR vbox '//vertical box,contains three horizontal boxes
  AS GtkWidget PTR hbox_username '//include username tooltip and username text box. HBOX is similar
  AS GtkWidget PTR hbox_pwd
  AS GtkWidget PTR hbox_button
END TYPE
DIM SHARED AS login_widget wgt


SUB login(BYVAL Widget AS GtkWidget PTR, BYVAL Udata AS gpointer)
  WITH *CAST(login_widget PTR, Udata)
    VAR nam = gtk_entry_get_text(GTK_ENTRY(.entry_username)) _
      , pwd = gtk_entry_get_text(GTK_ENTRY(.entry_pwd)) _
      , vrs = "Welcome to GTK+-"

#if GTK_MAJOR_VERSION > 2
  vrs &= gtk_get_major_version() _
      & "." & gtk_get_minor_version() _
      & "." & gtk_get_micro_version()
#else
  vrs &= "2"
#endif

    IF *nam = _NAME ANDALSO *pwd = _PWD THEN
      gtk_label_set_text(GTK_LABEL(.label_tips), vrs)
    ELSE
      gtk_label_set_text(GTK_LABEL(.label_tips), "Invalid user name or password.")
    END IF
  END WITH
END SUB


SUB init_login_widget()
  WITH wgt
    .window = gtk_window_new(GTK_WINDOW_TOPLEVEL)
    .label_tips = gtk_label_new("Welcome, please login first")
    .button_login = gtk_button_new_with_label("login")
    .button_exit = gtk_button_new_with_label("exit")
    .entry_username = gtk_entry_new()
    .entry_pwd = gtk_entry_new()
    .label_username = gtk_label_new("User name :")
    .label_pwd = gtk_label_new("Password :")
    .vbox = gtk_vbox_new(FALSE, 20)
    .hbox_username = gtk_hbox_new(FALSE, 20)
    .hbox_pwd = gtk_hbox_new(FALSE, 20)
    .hbox_button = gtk_hbox_new(FALSE, 20)

    '//setup window

    gtk_window_set_title(GTK_WINDOW(.window), "System login")
    gtk_window_set_resizable(GTK_WINDOW(.window), FALSE)

    '//setup vboxes and hboxes

    gtk_box_pack_start(GTK_BOX(wgt.hbox_username), .label_username, TRUE, FALSE, 10)
    gtk_box_pack_start(GTK_BOX(wgt.hbox_username), .entry_username, TRUE, FALSE, 10)
    gtk_box_pack_start(GTK_BOX(wgt.hbox_pwd), .label_pwd, TRUE, FALSE, 10)
    gtk_box_pack_start(GTK_BOX(wgt.hbox_pwd), .entry_pwd, TRUE, FALSE, 10)
    gtk_box_pack_start(GTK_BOX(wgt.hbox_button), .button_login, TRUE, FALSE, 10)
    gtk_box_pack_start(GTK_BOX(wgt.hbox_button), .button_exit, TRUE, FALSE, 10)

    gtk_box_pack_start(GTK_BOX(wgt.vbox), .label_tips, TRUE, FALSE, 10)
    gtk_box_pack_start(GTK_BOX(wgt.vbox), .hbox_username, TRUE, FALSE, 10)
    gtk_box_pack_start(GTK_BOX(wgt.vbox), .hbox_pwd, TRUE, FALSE, 10)
    gtk_box_pack_start(GTK_BOX(wgt.vbox), .hbox_button, TRUE, FALSE, 10)

    '//password invisible,show“#” when typing

    gtk_entry_set_visibility(GTK_ENTRY(.entry_pwd), FALSE)
    gtk_entry_set_invisible_char(GTK_ENTRY(.entry_pwd), CAST(gunichar, ASC("#")))
    gtk_entry_set_max_length(GTK_ENTRY(.entry_username), 10)
    gtk_entry_set_max_length(GTK_ENTRY(.entry_pwd), 10)

    g_signal_connect(G_OBJECT(.button_login), "clicked", G_CALLBACK(@login), @wgt)
    g_signal_connect(G_OBJECT(.window), "destroy", G_CALLBACK(@gtk_main_quit), NULL)
    g_signal_connect(G_OBJECT(.button_exit), "clicked", G_CALLBACK(@gtk_main_quit), NULL)

    gtk_container_add(GTK_CONTAINER(wgt.window), wgt.vbox)

    gtk_widget_show_all(wgt.window)
  END WITH
END SUB

#IFNDEF __GTK_H__
  gtk_init(@__FB_ARGC__, @__FB_ARGV__)
#ELSE
  gtk_init(NULL, NULL) '' workarround for 64 bit and old fashioned style (http://www.freebasic.net/forum/viewtopic.php?p=201267#p201267)
#ENDIF

init_login_widget()

gtk_main()

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 3 guests