Fedora Linux Support Community & Resources Center
  #16  
Old 16th July 2012, 04:11 AM
ah7013 Offline
Registered "Cashew" User
 
Join Date: May 2010
Location: Adelaide, Australia
Age: 20
Posts: 671
linuxfirefox
Re: Programming challenge: Create a GUI window

Quote:
Originally Posted by RupertPupkin View Post
Here's another Qt version, this time using the Qt Jambi bindings for Java. Yes, you can write Java apps with a native Qt GUI!
Sweet! I didn't know that Java had Qt bindings.
Reply With Quote
  #17  
Old 16th July 2012, 07:37 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,712
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

@ah7013: Nice perl/gtk version! It's funny that you told it to use Times New Roman -- which is a serif font -- but it still came out as sans serif. My guess is that GTK couldn't find a truetype Times font installed, so it used the default font instead, which is probably DejaVu Sans. Either way, it looks good!

I found an old C-based GUI toolkit that I thought had died in the late 90s: MGUI. You can download the Linux version (mglin412.tgz) here. I put the libmgui.a and libmgpp.a libraries from mgui/lib/x11 into /usr/local/lib, and I put the *.h header files from mgui/include/ in /usr/local/include. It's 32-bit only, so you'll need the glibc.i686, libxcb.i686, libXau.i686 and libX11.i686 packages installed if you're on 64-bit Fedora.

Here's the code (the ctype stuff is a hack to make a new program linked to the ancient libmgui.a static library work on modern Linux systems):
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <mgui.h>

__const unsigned short int *__ctype_b;
__const __int32_t *__ctype_tolower;
__const __int32_t *__ctype_toupper;

void ctSetup() {
   __ctype_b = *(__ctype_b_loc());
   __ctype_toupper = *(__ctype_toupper_loc());
   __ctype_tolower = *(__ctype_tolower_loc());
}

void MDQuitCB(MOBJECT shell, void *od, void *ad) {
   exit(0);
}

void MGUIMain(int argc, char **argv) {
   MOBJECT win = MCreateShell("MGUI Example Window", 0);
   MObjectSetForegroundRGB(win, 255, 255, 255);
   MObjectSetBackgroundRGB(win, 72, 61, 139);
   MObjectSetMinSize(win, 400, 150);
   MOBJECT msg = MCreateLabel(win, "Welcome!", HELV_LARGE);
   MObjectSetMinSize(msg, 400, 100);
   MObjectSetOffset(msg, 30, 80, 0, 0);
   MOBJECT ok = MCreatePButton(win, "OK", FIXED_SMALL);
   MObjectSetOffset(ok, 45, 15, 3, 3);
   MPButtonSetCallback(ok, MDQuitCB, NULL);
   MShellRealize(win);
   MShellSetWMCloseCallback(win, MDQuitCB, 0L);
   MMainLoop();
}
Save that in a file called mguiwindow.c, then compile it like this:
Code:
gcc -m32 -o mguiwindow mguiwindow.c -lmgui -lX11
Here's the screenshot:
Attached Thumbnails
Click image for larger version

Name:	mguiwindow.png
Views:	213
Size:	3.8 KB
ID:	23435  
__________________
OS: Fedora 20 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 5GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #18  
Old 17th July 2012, 06:44 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,712
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

Here's another C-based version, using the EZWGL toolkit that was mentioned in Linux Journal back in the 90s (when people were looking for free alternatives to Motif).

Here's the code:
Code:
#include <EZ.h>

void quit(EZ_Widget *widget, void *dat) {
   EZ_Shutdown();
   exit(0);
}
  
void main(int argc, char **argv) {
   EZ_Initialize(argc, argv, 0);
   EZ_Widget *win, *msg, *ok;
   win = EZ_CreateWidget(EZ_WIDGET_FRAME, NULL,
                         EZ_WM_WINDOW_NAME, "EZWGL Example Window",
                         EZ_GEOMETRY,       500,400,400,150,
                         EZ_ORIENTATION,    EZ_VERTICAL,
                         EZ_BACKGROUND,     "#483D8B",
                         0);
   msg = EZ_CreateWidget(EZ_WIDGET_LABEL, win,
                         EZ_LABEL_STRING, "Welcome!",
                         EZ_HEIGHT,       80,
                         EZ_FOREGROUND,   "white",
                         EZ_FONT_NAME,    "-*-helvetica-medium-r-normal-*-34-*-*-*-*-*-*-*",
                         0);
   ok = EZ_CreateWidget(EZ_WIDGET_NORMAL_BUTTON, win,
                        EZ_LABEL_STRING, "OK",
                        EZ_BACKGROUND,   "gray74",
                        0);
   EZ_AddWidgetCallBack(ok, EZ_CALLBACK, quit, NULL, 0);
   EZ_DisplayWidget(win);
   EZ_EventMainLoop();
}
Save that in a file called ezwglwindow.c and compile like this:
Code:
gcc -o ezwglwindow ezwglwindow.c -lEZ -lX11 -lXext -lm -lpng12 -ljpeg -ltiff
Here's the screenshot:
Attached Thumbnails
Click image for larger version

Name:	ezwglwindow.png
Views:	180
Size:	3.8 KB
ID:	23443  
__________________
OS: Fedora 20 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 5GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #19  
Old 17th July 2012, 11:42 PM
Gareth Jones Offline
Official Gnome 3 Sales Rep. (and Adminstrator)
 
Join Date: Jul 2011
Location: Birmingham, UK
Age: 32
Posts: 2,771
linuxfirefox
Re: Programming challenge: Create a GUI window

I've been meaning to catch up with Gtkmm 3.x for a while now, so I took this as an excuse to start.

Here is Gtkmm-3.4 (for C++):
Code:
// gtkmm-example.cc
// Gtkmm Window Example.
#include <gtkmm.h>

int main (int argc, char *argv [])
{
	// Name the application and parse the command-line for GTK+ standard
	// options.
	Glib::set_application_name ("Gtkmm Window Example");
	Glib::RefPtr <Gtk::Application> app =
		Gtk::Application::create (argc, argv, "gtkmm.window.example");

	// Create the label in a white size-32 font.
	Gtk::Label label ("<span font='32'>Welcome!</span>");
	label.set_use_markup ();
	label.override_color (Gdk::RGBA ("White"));
	label.set_halign (Gtk::ALIGN_CENTER);
	label.set_valign (Gtk::ALIGN_CENTER);

	// Create the okay button.
	Gtk::Button button ("OK");
	button.set_halign (Gtk::ALIGN_CENTER);
	button.set_valign (Gtk::ALIGN_CENTER);

	// Add the label and button to a single-columned grid.
	Gtk::Grid grid;
	grid.set_orientation (Gtk::ORIENTATION_VERTICAL);
	grid.set_halign (Gtk::ALIGN_CENTER);
	grid.set_valign (Gtk::ALIGN_CENTER);
	grid.set_row_spacing (12);
	grid.add (label);
	grid.add (button);

	// Create the top-level window and add the widgets.
	Gtk::Window window;
	window.resize (400, 150);
	window.set_border_width (12);
	window.override_background_color (Gdk::RGBA ("DarkSlateBlue"));
	window.add (grid);

	// Clicking the button should close the window.
	button.signal_clicked ().
		connect (sigc::mem_fun (window, &Gtk::Window::hide));

	// Show all widgets when the window is shown.
	window.show_all ();

	// Show the window and exit when it closes.
	return app->run (window);
}
Compile with:
Code:
g++ -O2 -o gtkmm-example $(pkg-config gtkmm-3.0 --cflags --libs) gtkmm-example.cc
Attached Thumbnails
Click image for larger version

Name:	Screenshot from 2012-07-17 22:31:52.png
Views:	191
Size:	12.4 KB
ID:	23453  
Reply With Quote
  #20  
Old 18th July 2012, 05:17 AM
ah7013 Offline
Registered "Cashew" User
 
Join Date: May 2010
Location: Adelaide, Australia
Age: 20
Posts: 671
linuxfirefox
Re: Programming challenge: Create a GUI window

Here is one with Perl+Qt. This one requires the Perl-Qt package and also the perl-X11-Protocol package (which is used as part of the program to move the window to the center of the screen instead of using Qt::DesktopWidget to do that which was causing problems).
Code:
#!/usr/bin/perl
use QtCore4;
use QtGui4;
use X11::Protocol;

sub putcenter {
    my $xproto = X11::Protocol->new();
    return (($xproto->{'width_in_pixels'} - shift(@_)) / 2, ($xproto->{'height_in_pixels'} - shift(@_)) / 2);
}

my $a = Qt::Application;
my $backcolor = "#483D8B";
my $fontcolor = "#FFFFFF";
my @xdimensions = (400, 150);
$a->setStyleSheet("QMainWindow { background : $backcolor; }");
my $mw = Qt::MainWindow();
$mw->setWindowTitle("Perl/Qt4 Window Example");
$mw->setGeometry(putcenter(@xdimensions), @xdimensions);
my $msg = Qt::Label("Welcome!", $mw);
my $font = Qt::Font;
$font->setPointSize(32);
$msg->setFont($font);
$msg->setStyleSheet("QLabel { color : $fontcolor; }");
$msg->setGeometry(90, 20, 210, 50);
my $ok = Qt::PushButton("OK", $mw);
$ok->setGeometry(170, 105, 60, 30);
$a->connect($ok, SIGNAL('clicked()'), $a, SLOT('quit()'));
$mw->show();
exit $a->exec;
And the screenshot
Attached Thumbnails
Click image for larger version

Name:	snapshot1.png
Views:	164
Size:	12.1 KB
ID:	23455  

Last edited by ah7013; 18th July 2012 at 05:21 AM.
Reply With Quote
  #21  
Old 18th July 2012, 02:07 PM
ah7013 Offline
Registered "Cashew" User
 
Join Date: May 2010
Location: Adelaide, Australia
Age: 20
Posts: 671
linuxfirefox
Re: Programming challenge: Create a GUI window

Quote:
Originally Posted by RupertPupkin View Post
@ah7013: Nice perl/gtk version! It's funny that you told it to use Times New Roman -- which is a serif font -- but it still came out as sans serif. My guess is that GTK couldn't find a truetype Times font installed, so it used the default font instead, which is probably DejaVu Sans. Either way, it looks good!
Thanks!

---------- Post added 19th July 2012 at 12:07 AM ---------- Previous post was 18th July 2012 at 03:22 PM ----------

Quote:
Originally Posted by ah7013 View Post
Here is one with Perl+Qt. This one requires the Perl-Qt package and also the perl-X11-Protocol package (which is used as part of the program to move the window to the center of the screen instead of using Qt:esktopWidget to do that which was causing problems).
Here's version "0.2"

Just wanted to make the code a bit easier to understand in the putcenter() sub. Also moved the color setting code into its own sub.
Code:
#!/usr/bin/perl
use QtCore4;
use QtGui4;
use X11::Protocol;

my $VERSION = 0.2;

sub setcolor {
    my ($object, $colortype, $color) = @_;
    my $objectclass = ref($object);
    $objectclass =~ s/^\s+//;
    $objectclass =~ s/^Qt::/Q/g;
    $object->setStyleSheet("$objectclass { $colortype : $color; }");
}

sub putcenter {
    my ($winx, $winy) = @_;
    my $xproto = X11::Protocol->new();
    my $xpos = ($xproto->{'width_in_pixels'} - $winx) / 2;
    my $ypos = ($xproto->{'height_in_pixels'} - $winy) / 2;
    return ($xpos, $ypos);
}

my $a = Qt::Application;
my $backcolor = "#483D8B";
my $fontcolor = "#FFFFFF";
my @xdimensions = (400, 150);
my $mw = Qt::MainWindow();
setcolor($mw, "background", $backcolor);
my $title = "Perl/Qt4 Window Example $VERSION";
$mw->setWindowTitle($title);
$mw->setGeometry(putcenter(@xdimensions), @xdimensions);
my $msg = Qt::Label("Welcome!", $mw);
my $font = Qt::Font;
$font->setPointSize(32);
$msg->setFont($font);
setcolor($msg, "color", $fontcolor);
$msg->setGeometry(90, 20, 210, 50);
my $ok = Qt::PushButton("OK", $mw);
$ok->setGeometry(170, 105, 60, 30);
$a->connect($ok, SIGNAL('clicked()'), $a, SLOT('quit()'));
$mw->show();
exit $a->exec;
The result still looks exactly the same so a screenshot of this is in my post above.
Reply With Quote
  #22  
Old 18th July 2012, 04:52 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,712
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

@ah7013: Another Perl-based example! Nice. You might be running out of GUI toolkits with Perl bindings.

So far we have 23 examples.

Here's a summary of what we have right now, first organized by toolkit with the corresponding language bindings:
  1. Qt: Python, C++, Java, Perl
  2. GTK+: Haskell, Python, Perl, C++
  3. Tk: Tcl, Perl, Python, Ruby
  4. Swing: Java
  5. XForms: C
  6. GNUstep: Objective-C
  7. Motif: C
  8. wxWidgets: Perl
  9. Icon: Icon
  10. Prima: Perl
  11. FOX: C++
  12. FLTK: C++
  13. MGUI: C
  14. EZWGL: C

Here's the list organized by programming language with the corresponding toolkit:
  1. Perl: Qt, GTK+, Tk, wxWidgets, Prima
  2. C: XForms, Motif, MGUI, EZWGL
  3. C++: Qt, GTK+, FOX, FLTK
  4. Python: Qt, GTK+, Tk
  5. Java: Swing, Qt
  6. Tcl: Tk
  7. Ruby: Tk
  8. Objective-C: GNUstep
  9. Haskell: GTK+
  10. Icon: Icon

So we have Qt with its native C++ bindings (thanks to Pitfall) but still no GTK+ with its native C bindings. And thanks to ah7013, Perl is in the lead with the most toolkits so far. Let's keep going!
__________________
OS: Fedora 20 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 5GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N

Last edited by RupertPupkin; 19th July 2012 at 07:44 AM. Reason: Corrected spelling of Pitfall's name.
Reply With Quote
  #23  
Old 18th July 2012, 08:47 PM
Gareth Jones Offline
Official Gnome 3 Sales Rep. (and Adminstrator)
 
Join Date: Jul 2011
Location: Birmingham, UK
Age: 32
Posts: 2,771
linuxfirefox
Re: Programming challenge: Create a GUI window

Here's a couple of new GNOME languages that haven't been posted: Vala and Genie.

Vala is a C#-inspired language which compiles to C/GObject instead of byte-code (https://live.gnome.org/Vala). I can't promise that I've written the best Vala code here, as I don't really know it that well, and pretty much translated my C++/Gtkmm version line-by-line...
Code:
// vala-example.vala
// Vala GTK+ Window Example.
int main (string [] args)
{
	// Name the application and parse the command-line for GTK+ standard
	// options.
	GLib.Environment.set_application_name ("Vala GTK+ Window Example");
	Gtk.init (ref args);

	// Create colours.
	var text_colour = Gdk.RGBA ();
	text_colour.parse ("White");
	var background_colour = Gdk.RGBA ();
	background_colour.parse ("DarkSlateBlue");

	// Create the label in a white size-32 font.
	var label = new Gtk.Label ("<span font='32'>Welcome!</span>");
	label.use_markup = true;
	label.override_color (Gtk.StateFlags.NORMAL, text_colour);
	label.halign = Gtk.Align.CENTER;
	label.valign = Gtk.Align.CENTER;

	// Create the okay button.
	var button = new Gtk.Button.with_label ("OK");
	button.halign = Gtk.Align.CENTER;
	button.valign = Gtk.Align.CENTER;

	// Add the label and button to a single-columned grid.
	var grid = new Gtk.Grid ();
	grid.orientation = Gtk.Orientation.VERTICAL;
	grid.halign = Gtk.Align.CENTER;
	grid.valign = Gtk.Align.CENTER;
	grid.row_spacing = 12;
	grid.add (label);
	grid.add (button);

	// Create the top-level window and add the widgets.
	var window = new Gtk.Window ();
	window.resize (400, 150);
	window.border_width = 12;
	window.override_background_color (Gtk.StateFlags.NORMAL,
	                                  background_colour);
	window.add (grid);

	// Connect signals.
	button.clicked.connect (Gtk.main_quit);
	window.destroy.connect (Gtk.main_quit);

	// Show window.
	window.show_all ();
	Gtk.main ();
	return 0;
}
Compile with:
Code:
valac --pkg gtk+-3.0 vala-example.vala
The screen-shot looks unsurprisingly similar to the Gtkmm one I already posted...

Quote:
Originally Posted by RupertPupkin View Post
but still no GTK+ with its native C bindings.
You can glimpse something of the horror of programming a modern toolkit in C by looking at Vala's intermediate C output (valac -C --pkg gtk+-3.0 vala-example.vala). Obviously that's machine generated, and thus contains more cruft than a human programmer would write, but you get the idea...
Attached Thumbnails
Click image for larger version

Name:	Screenshot from 2012-07-18 19:37:34.png
Views:	176
Size:	12.9 KB
ID:	23461  
Reply With Quote
  #24  
Old 18th July 2012, 08:53 PM
Gareth Jones Offline
Official Gnome 3 Sales Rep. (and Adminstrator)
 
Join Date: Jul 2011
Location: Birmingham, UK
Age: 32
Posts: 2,771
linuxfirefox
Re: Programming challenge: Create a GUI window

And here's Genie. Genie is an alternative syntax for Vala, based on Python and several other languages (https://live.gnome.org/Genie). The most immediately obvious difference being that it uses indentation instead of braces to nest syntax. It uses the same compiler etc. as Vala.

This isn't the best demonstration of the different syntax however, as apart from removing "main ... { ... }" and adding "init ..." there are no changes with respect to simple object and function usage...
Code:
// genie-example.gs
// Genie GTK+ Window Example.
init
	// Name the application and parse the command-line for GTK+ standard
	// options.
	GLib.Environment.set_application_name ("Genie GTK+ Window Example");
	Gtk.init (ref args);

	// Create colours.
	var text_colour = Gdk.RGBA ();
	text_colour.parse ("White");
	var background_colour = Gdk.RGBA ();
	background_colour.parse ("DarkSlateBlue");

	// Create the label in a white size-32 font.
	var label = new Gtk.Label ("<span font='32'>Welcome!</span>");
	label.use_markup = true;
	label.override_color (Gtk.StateFlags.NORMAL, text_colour);
	label.halign = Gtk.Align.CENTER;
	label.valign = Gtk.Align.CENTER;

	// Create the okay button.
	var button = new Gtk.Button.with_label ("OK");
	button.halign = Gtk.Align.CENTER;
	button.valign = Gtk.Align.CENTER;

	// Add the label and button to a single-columned grid.
	var grid = new Gtk.Grid ();
	grid.orientation = Gtk.Orientation.VERTICAL;
	grid.halign = Gtk.Align.CENTER;
	grid.valign = Gtk.Align.CENTER;
	grid.row_spacing = 12;
	grid.add (label);
	grid.add (button);

	// Create the top-level window and add the widgets.
	var window = new Gtk.Window ();
	window.resize (400, 150);
	window.border_width = 12;
	window.override_background_color (Gtk.StateFlags.NORMAL,
	                                  background_colour);
	window.add (grid);

	// Connect signals.
	button.clicked.connect (Gtk.main_quit);
	window.destroy.connect (Gtk.main_quit);

	// Show window.
	window.show_all ();
	Gtk.main ();
Compile:
Code:
valac --pkg gtk+-3.0 genie-example.gs
No surprises in the screen-shot...
Attached Thumbnails
Click image for larger version

Name:	Screenshot from 2012-07-18 19:37:21.png
Views:	160
Size:	12.6 KB
ID:	23462  
Reply With Quote
  #25  
Old 18th July 2012, 09:29 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,712
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

@GarethJones: Cool, nice to see some new languages represented.

Here's another Python example, this time using Swing, which can be accessed via Jython (the Python bindings for Java). Jython is in the Fedora repos (yum install jython).

Here's the code, which is run as a script:
Code:
#!/usr/bin/env jython
import sys
from javax.swing import *
from java.awt import *

def cb_quit(event) :
    sys.exit()

win = JFrame("Jython Window Example", layout=BorderLayout())
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
msg = JLabel("Welcome!", JLabel.CENTER)
msg.setFont(Font(Font.SANS_SERIF, Font.PLAIN, 32))
msg.setOpaque(1)
msg.setBackground(Color(72,61,139))
msg.setForeground(Color.WHITE);
win.add(msg, BorderLayout.CENTER);
bottom = JPanel()
bottom.setBackground(Color(72,61,139));
ok = JButton("OK", actionPerformed = cb_quit)
bottom.add(ok);
win.add(bottom, BorderLayout.SOUTH)
win.getContentPane().setPreferredSize(Dimension(400,150));
win.pack();
win.setVisible(1)
Here's the screenshot (which of course looks just like the Java example):
Attached Thumbnails
Click image for larger version

Name:	jythonwindow.png
Views:	146
Size:	4.1 KB
ID:	23526  
__________________
OS: Fedora 20 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 5GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N

Last edited by RupertPupkin; 26th July 2012 at 03:31 AM. Reason: Removed hack to get around JFrame.setSize() not working properly
Reply With Quote
  #26  
Old 19th July 2012, 12:24 AM
Gareth Jones Offline
Official Gnome 3 Sales Rep. (and Adminstrator)
 
Join Date: Jul 2011
Location: Birmingham, UK
Age: 32
Posts: 2,771
linuxfirefox
Re: Programming challenge: Create a GUI window

Quote:
Originally Posted by Gareth Jones View Post
You can glimpse something of the horror of programming a modern toolkit in C by looking at Vala's intermediate C output [...]
Well I decided to give it a try anyway. Here is hand-written C/GTK+ native. For such a simple example it turns out to be not that much more complicated than Vala or C++, just more verbose (with lots of macros and explicit casting to achieve object-oriented programming in C).

Note that the code shown here makes no attempt at "safe" memory management as provided by Glib::RefPointer in Gtkmm, or Vala objects which map to GObject objects. In this case all we're doing is exiting, so the memory is freed anyway, but in general there would be explicit "g_object_unref()" calls on all the pointed-to objects when they are no longer needed. This is where higher-level languages like C++ or Vala, with built-in object-oriented features ("resource-acquisition-is-initialization" and exception safety), would really show their advantages in more complex programs! As well as using namespaces instead of very long identifiers, and not needing potentially unsafe macros and casts throughout, of course...

Code:
// gtk-example.c
// C GTK+ Window Example.
#include <stdlib.h>
#include <gtk/gtk.h>

int main (int argc, char *argv [])
{
	// Name the application and parse the command-line for GTK+ standard
	// options.
	g_set_application_name ("C GTK+ Window Example");
	gtk_init (&argc, &argv);

	// Create colours.
	GdkRGBA text_colour, background_colour;
	gdk_rgba_parse (&text_colour, "White");
	gdk_rgba_parse (&background_colour, "DarkSlateBlue");

	// Create the label in a white size-32 font.
	GtkWidget *label = gtk_label_new ("<span font='32'>Welcome!</span>");
	gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
	gtk_widget_override_color (label, GTK_STATE_NORMAL, &text_colour);
	gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
	gtk_widget_set_valign (label, GTK_ALIGN_CENTER);

	// Create the okay button.
	GtkWidget *button = gtk_button_new_with_label ("OK");
	gtk_widget_set_halign (button, GTK_ALIGN_CENTER);
	gtk_widget_set_valign (button, GTK_ALIGN_CENTER);

	// Add the label and button to a single-columned grid.
	GtkWidget *grid = gtk_grid_new ();
	gtk_orientable_set_orientation (GTK_ORIENTABLE (grid),
	                                GTK_ORIENTATION_VERTICAL);
	gtk_widget_set_halign (grid, GTK_ALIGN_CENTER);
	gtk_widget_set_valign (grid, GTK_ALIGN_CENTER);
	gtk_grid_set_row_spacing (GTK_GRID (grid), 12);
	gtk_container_add (GTK_CONTAINER (grid), label);
	gtk_container_add (GTK_CONTAINER (grid), button);

	// Create the top-level window and add the widgets.
	GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_resize (GTK_WINDOW (window), 400, 150);
	gtk_container_set_border_width (GTK_CONTAINER (window), 12);
	gtk_widget_override_background_color (window,
	                                      GTK_STATE_FLAG_NORMAL,
	                                      &background_colour);
	gtk_container_add (GTK_CONTAINER (window), grid);

	// Connect signals.
	g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
	g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

	// Show window.
	gtk_widget_show_all (window);
	gtk_main ();
	return EXIT_SUCCESS;
}
Compile:
Code:
gcc -O2 $(pkg-config gtk+-3.0 --cflags --libs) gtk-example.c
No surprises in the screen-shot.

Does anyone know why the font in mine looks so much nicer than anyone else's? As far as I know I'm using the default font, and I can't believe Infinality makes that much difference...
Attached Thumbnails
Click image for larger version

Name:	Screenshot from 2012-07-18 23:08:42.png
Views:	161
Size:	12.8 KB
ID:	23464  
Reply With Quote
  #27  
Old 19th July 2012, 01:52 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,712
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

@GarethJones: Your native C version of the example in GTK+ isn't that complex, as you said. I remember looking ages ago in Havoc Pennington's GTK+ book and noticing how much code it took to do fairly simple stuff, and your example seems pretty similar. For C-based toolkits it looks about halfway between XForms and Motif in terms of lines of code. I'm pretty sure it's not using DejaVu Sans as the default font, like most of the other examples are; that curly lowercase L is a dead giveaway. I'd like to know what font that is.

Here's another Ruby example, this time using Swing, which can be accessed via JRuby (Ruby bindings for Java). JRuby is in the Fedora repos (yum install ruby jruby).

Here's the code, which is run as a script:
Code:
#!/usr/bin/env jruby
include Java
Font = java.awt.Font

win = javax.swing.JFrame.new("JRuby Window Example")
win.default_close_operation = javax.swing.JFrame::EXIT_ON_CLOSE
win.set_layout(java.awt.BorderLayout.new())
msg = javax.swing.JLabel.new("Welcome!", javax.swing.JLabel::CENTER)
msg.set_font(Font.new(Font::SANS_SERIF, Font::PLAIN, 32))
msg.set_background(java.awt.Color.new(72,61,139))
msg.set_foreground(java.awt.Color::WHITE)
msg.set_opaque(true)
win.add(msg, java.awt.BorderLayout::CENTER)
bottom = javax.swing.JPanel.new()
bottom.set_background(java.awt.Color.new(72,61,139))
ok = javax.swing.JButton.new("OK")
ok.add_action_listener do |evt|
   java.lang.System.exit(0)
end
bottom.add(ok)
win.add(bottom, java.awt.BorderLayout::SOUTH)
win.getContentPane().setPreferredSize(java.awt.Dimension.new(400,150))
win.pack
win.show
Here's the screenshot:
Attached Thumbnails
Click image for larger version

Name:	jrubywindow.png
Views:	138
Size:	4.2 KB
ID:	23528  
__________________
OS: Fedora 20 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 5GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N

Last edited by RupertPupkin; 26th July 2012 at 03:44 AM. Reason: Removed hack to get around JFrame.setSize() not working properly
Reply With Quote
  #28  
Old 19th July 2012, 04:12 AM
Gareth Jones Offline
Official Gnome 3 Sales Rep. (and Adminstrator)
 
Join Date: Jul 2011
Location: Birmingham, UK
Age: 32
Posts: 2,771
linuxfirefox
Re: Programming challenge: Create a GUI window

Quote:
Originally Posted by RupertPupkin View Post
I'm pretty sure it's not using DejaVu Sans as the default font, like most of the other examples are; that curly lowercase L is a dead giveaway. I'd like to know what font that is.
Apparently it's Cantarell (http://en.wikipedia.org/wiki/Cantarell_%28font%29), which is the GNOME 3 default. I didn't know that GNOME had changed its default font; apparently it's yet another source of controversy... I get the impression Infinality fixes everything except the limited Unicode coverage though. I guess no one else in this thread is using G3!
Reply With Quote
  #29  
Old 19th July 2012, 07:34 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,712
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

Quote:
Originally Posted by Gareth Jones View Post
Apparently it's Cantarell (http://en.wikipedia.org/wiki/Cantarell_%28font%29), which is the GNOME 3 default.
Never heard of it, but I'm surprised that GNOME went with a font lacking Unicode support. Good thing you're using GNOME 3!

Here's another Java example, this time using the Eclipse SWT, which comes with the Eclipse SDK. For this you'll need the two ${ECLIPSE_HOME}/plugins/org.eclipse.swt*.jar files in your classpath (e.g. in ${JAVA_HOME}/jre/lib/ext) if you're compiling outside of Eclipse.

Here's the code:
Code:
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.events.*;

public class swtwindow {
 public static void main(String[] args) {
   Display display = new Display();
   Shell shell = new Shell(display);
   Color bg = new Color(display, 72, 61, 139);
   shell.setText("SWT Example Window");
   shell.setSize(400, 150);
   shell.setBackground(bg);
   GridLayout grid = new GridLayout();
   grid.marginHeight = 10;
   shell.setLayout(grid);
   Label msg = new Label(shell, SWT.CENTER);
   msg.setText("Welcome!");
   msg.setFont(new Font(display, "Sans", 32, SWT.NORMAL));
   msg.setBackground(bg);
   msg.setForeground(new Color(display, 255, 255, 255));
   msg.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
   Button ok = new Button(shell, SWT.PUSH);
   ok.setText("OK");
   ok.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
   ok.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
         System.exit(0);
      }
   });
   shell.open();
   while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) display.sleep();
   }
   display.dispose();
 }
}
Save that in a file called swtwindow.java and compile and run it like this:
Code:
javac swtwindow.java
java swtwindow
Here's the screenshot (which shows better antialiasing than the Swing version):
Attached Thumbnails
Click image for larger version

Name:	swtwindow.png
Views:	152
Size:	7.7 KB
ID:	23468  
__________________
OS: Fedora 20 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 5GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #30  
Old 19th July 2012, 02:50 PM
ah7013 Offline
Registered "Cashew" User
 
Join Date: May 2010
Location: Adelaide, Australia
Age: 20
Posts: 671
linuxfirefox
Re: Programming challenge: Create a GUI window

Quote:
Originally Posted by RupertPupkin View Post
@ah7013: Another Perl-based example! Nice. You might be running out of GUI toolkits with Perl bindings.
Thanks again. Yup I think I'm defenitly running out of GUI toolkits with Perl bindings so this next one is in Ruby with Qt. This one is different from my other examples because it has inline C code to get the screen resolution which is used for centering the window. I'm sure there is a Ruby Gem out there specifically to get the screen resolution but because i'm less familiar with Ruby than Perl I have no idea what it would be called so I wrote one inline. Anyway this one requires rubygems, rubygem-RubyInline, gcc, ruby-qt and libX11-devel packages to be installed. Here's the code:
Code:
#!/usr/bin/ruby
require 'Qt4'
require 'rubygems'
require 'inline'

class << self
    inline do |builder|
	builder.c '
	#include <stdlib.h>
	#include <X11/Xlib.h>
        VALUE getResolution() {
            Display* dsp = XOpenDisplay(NULL);
            Screen* scr = DefaultScreenOfDisplay(dsp);
	    int x = scr->width;
            int y = scr->height;
	    char* buffer = malloc(10);
	    sprintf(buffer, "%d %d", x, y);
	    return rb_str_new2(buffer);
        }'
    end
end

def setcolor(object, colortype, color)
    objectclass = object.class.to_s().sub(/^Qt::/, "Q")
    object.setStyleSheet("#{objectclass} { #{colortype} : #{color} }")
end

def putcenter(winx, winy)
    resval = getResolution.split(" ")
    xpos = (resval[0].to_i() - winx) / 2
    ypos = (resval[1].to_i() - winy) / 2
    return Array.new(xpos, ypos)
end

a = Qt::Application.new(ARGV)
backcolor = "#483D8B"
fontcolor = "#FFFFFF"
mw = Qt::MainWindow.new()
setcolor(mw, "background", backcolor)
title = "Ruby/Qt4 Window Example"
mw.setWindowTitle(title)
cval = putcenter(400, 150)
mw.move(cval[0], cval[1])
mw.resize(400, 150)
msg = Qt::Label.new("Welcome!", mw)
font = Qt::Font.new()
font.setPointSize(32)
msg.setFont(font)
setcolor(msg, "color", fontcolor)
msg.setGeometry(90, 20, 210, 50)
ok = Qt::PushButton.new("OK", mw)
ok.setGeometry(165, 105, 60, 30)
a.connect(ok, SIGNAL('clicked()'), a, SLOT('quit()'))
mw.show()
a.exec
And the screenshot:
Attached Thumbnails
Click image for larger version

Name:	snapshot2.png
Views:	150
Size:	12.4 KB
ID:	23469  

Last edited by ah7013; 19th July 2012 at 02:59 PM.
Reply With Quote
Reply

Tags
challenge, create, gui, programming, window

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you create your own programming language hmm? Mepoe Programming & Packaging 7 23rd April 2012 01:16 AM
How does KDE 3.7 create popup window on detection of USB drives. Brian1 Using Fedora 0 23rd November 2007 12:12 AM
Which Window Manager are you using for programming ? michaeleekk Programming & Packaging 11 29th July 2007 09:45 AM
gtk programming ~ how to link to another window after button is clicked? yan612 Programming & Packaging 2 3rd October 2006 10:10 AM
Automatic daily login script *Programming Challenge* SickFreak Programming & Packaging 4 23rd November 2005 11:45 PM


Current GMT-time: 09:55 (Sunday, 23-11-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Maroantsetra - Biu Photos - Bettendorf Photos on Instagram