Fedora Linux Support Community & Resources Center
  #61  
Old 17th June 2013, 10:58 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,502
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

Here's an example with Lua and Qt, using the bindings in the lqt library. That library is not in the Fedora repos, so you'll have to compile it yourself from this zip file: https://github.com/mkottman/lqt/archive/master.zip. You'll need the qt-devel, qtwebkit-devel, and lua-devel packages installed. The lqt library wouldn't compile for me until I added the line "#include <QDebug>" below line 25 in the common/lqt_qt.hpp file. Then I just followed the instructions in the README.md file (mkdir build; cd build; cmake ..; make; cp lib/qt*so /usr/lib64/lua/5.1) to compile and install the library in F18.

Here's the code for the window example (save as lqtwindow.lua):
Code:
#!/usr/bin/lua
require 'qtcore'
require 'qtgui'
app = QApplication.new(select('#',...) + 1, {'lua', ...})
app:setStyleSheet("QMainWindow, QLabel {background-color: #483D8B; color: #FFFFFF}")
win = QMainWindow.new()
win:setWindowTitle("Lua/Qt Window Example")
win:resize(400, 150)
lbl = QLabel.new("Welcome!", win)
lbl:setGeometry(90, 25, 220, 50)
lbl:setFont(QFont.new("Sans", 32))
lbl:setAlignment({'AlignHCenter'})
btn = QPushButton.new("OK", win)
btn:setGeometry(175, 100, 50, 30)
btn:setStyle(QPlastiqueStyle.new())
btn:connect('2clicked()', function() win:close() end)
win:show()
app.exec()
Screenshot:
Attached Thumbnails
Click image for larger version

Name:	lqtwindow.png
Views:	273
Size:	7.6 KB
ID:	25139  
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #62  
Old 19th June 2013, 02:18 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,502
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

Here's an example in Ada with Qt, using the QtAda bindings, which you can download here: http://www.qtada.com/en/download.html
You'll need the following packages from the Fedora repos to compile QtAda 3.1.0: gcc-gnat, libgnat-devel, gprbuild, qt-devel. qtwebkit-devel
When you unpack the qtada-gpl-3.1.0.tar.gz tarball and cd into qtada-gpl-3.1.0, run the configure script like this:
Code:
./configure --with-gela=no
Then remove all references to the examples subdirectory in the generated Makefile (otherwise the compile will hang indefinitely after a few minutes).

Here's the code for the window (save as qtadawindow.adb):
Code:
with Qt_Ada.Application;
with Qt4.Core_Applications;
with Qt4.Labels.Constructors;
with Qt4.Push_Buttons.Constructors;
with Qt4.Fonts;
with Qt4.Objects;
with Qt4.Strings;
with Qt4.Main_Windows.Constructors;

procedure qtadawindow is
begin
   Qt_Ada.Application.Initialize;
   declare
      win : constant not null access Qt4.Main_Windows.Q_Main_Window'Class
          := Qt4.Main_Windows.Constructors.Create;
      lbl : constant not null Qt4.Labels.Q_Label_Access
          := Qt4.Labels.Constructors.Create(Qt4.Strings.From_Utf_16("Welcome!"), win);
      btn : constant not null access Qt4.Push_Buttons.Q_Push_Button'Class
          := Qt4.Push_Buttons.Constructors.Create(Qt4.Strings.From_Utf_16("OK"), win);
   begin
      win.Set_Window_Title(Qt4.Strings.From_Utf_16("QtAda Window Example"));
      win.Resize(400, 150);
      win.Set_Style_Sheet(Qt4.Strings.From_Utf_16
                         ("QMainWindow, QLabel {color: #FFFFFF; background-color: #483D8B}"));
      lbl.Set_Geometry(90, 25, 220, 50);
      lbl.Set_Font(Qt4.Fonts.Create(Qt4.Strings.From_Utf_16("Sans"), 32, Qt4.Fonts.Weight_Normal));
      btn.Set_Geometry(175, 100, 50, 30);
      Qt4.Objects.Connect(btn, Qt4.Signal("clicked()"), Qt4.Core_Applications.Instance,
                          Qt4.Slot("quit()"));
      win.Show;
      Qt_Ada.Application.Execute;
   end;
end qtadawindow;
Compile that code like this:
Code:
gnat compile -I/usr/local/include/qtada/core -I/usr/local/include/qtada/gui qtadawindow.adb
gnat bind -aO/usr/local/lib/qtada qtadawindow.ali
gnat link qtadawindow.ali -lQtAdaCore -lQtAdaGui
Screenshot:
Attached Thumbnails
Click image for larger version

Name:	qtadawindow.png
Views:	214
Size:	8.3 KB
ID:	25140  
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #63  
Old 19th June 2013, 02:26 AM
solo2101 Offline
Registered User
 
Join Date: Jan 2010
Location: behind that screen...
Posts: 905
linuxchrome
Re: Programming challenge: Create a GUI window

So... are this programming challenges linux aimed only?
Reply With Quote
  #64  
Old 19th June 2013, 02:55 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,502
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

Quote:
Originally Posted by solo2101 View Post
So... are this programming challenges linux aimed only?
The examples should run natively in Linux, but if they also run in Windows or some other OS then that's fine.

This came up in another challenge with an example in SAS that was run on Windows, and it was allowed since there is a version of SAS for Linux. The "native" part is important. Running a Windows-only example in Linux through Wine or something like VirtualBox with a Windows guest OS doesn't count.

For instance, here's an example in Matlab using its built-in GUI toolkit (I don't know what that toolkit is called - it's different from Swing, so I'll just call it Matlab ). It runs in both Windows and Linux versions of Matlab.

Save this as matlabwindow.m:
Code:
function matlabwindow()
   f = figure('Position',[360,500,400,150], 'Color',[0.282 0.239 0.545],...
              'MenuBar','none', 'NumberTitle','off', 'Visible','off',...
              'Name','Matlab Window Example');
   uicontrol('Style','pushbutton', 'String','OK',...
             'Position',[165,20,70,25], 'Callback',{@btnOK_Callback});
   uicontrol('Style','text', 'String','Welcome!',...
             'Position',[90,80,220,50], 'ForegroundColor','white',...
             'BackgroundColor',[0.282 0.239 0.545], 'FontSize',32);
   set(f,'Visible','on');
   function btnOK_Callback(~,~)
      close();
   end
end
Screenshots in Linux (student version of Matlab ) and in Windows 7:
Attached Thumbnails
Click image for larger version

Name:	matlabwindow-linux.png
Views:	238
Size:	6.8 KB
ID:	25141   Click image for larger version

Name:	matlabwindow-win7.png
Views:	224
Size:	5.0 KB
ID:	25142  
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #65  
Old 20th June 2013, 02:36 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,502
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

Here's an example in Ada with Tk, using the TASH bindings which you can get here: http://tcladashell.sourceforge.net/
To compile TASH you'll need the following packages from the Fedora repos: libgnat-devel, tcl-devel, tk-devel

Save this as adatkwindow.adb:
Code:
with Interfaces.C;
with Tcl.Ada;
with Tcl.Tk.Ada;

procedure adatkwindow is
   use type Interfaces.C.int;
begin
   declare
      Interp : Tcl.Tcl_Interp;
      lbl : Tcl.Tk.Ada.Label;
      btn : Tcl.Tk.Ada.Button;
   begin
      Interp := Tcl.Tcl_CreateInterp;
      if Tcl.Tcl_Init(Interp) = Tcl.TCL_ERROR or Tcl.Tk.Tk_Init(Interp) = Tcl.TCL_ERROR then
         return;
      end if;
      Tcl.Tk.Ada.Set_Context(Interp);
      Tcl.Ada.Tcl_Eval(Interp, "wm title . ""Ada/Tk Window Example""");
      Tcl.Ada.Tcl_Eval(Interp, "wm geometry . 400x150");
      Tcl.Ada.Tcl_Eval(Interp, ". configure -bg darkslateblue");
      lbl := Tcl.Tk.Ada.Create(".msg", "-text ""Welcome!"" -font {-size 32} -fg white -bg #483D8B");
      Tcl.Tk.Ada.Pack(lbl, "-pady 10");
      btn := Tcl.Tk.Ada.Create(".ok", "-text OK -command exit");
      Tcl.Tk.Ada.Pack(btn, "-pady 20");
      Tcl.Tk.Tk_MainLoop;
   end;
end adatkwindow;
Suppose you installed TASH under /usr/local/tash. Then you'd compile this example like this:
Code:
gnat compile -I/usr/local/tash/include adatkwindow.adb
gnat bind -aO/usr/local/tash/lib adatkwindow.ali
gnat link adatkwindow.ali -ltash -ltcl -ltk
Screenshot of running the adatkwindow executable:
Attached Thumbnails
Click image for larger version

Name:	adatkwindow.png
Views:	199
Size:	8.0 KB
ID:	25143  
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #66  
Old 21st June 2013, 11:45 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,502
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

Here's an example in Python with wxWidgets, using the wxPython bindings, which you can get from the Fedora repos (yum install wxPython).

Here's the code:
Code:
#!/usr/bin/python
import wx

def Quit(evt):
        win.Close()

app = wx.App()
win = wx.Frame(None, -1, "wxPython Window Example", pos=(0, 0), size=(400, 150))
pnl = wx.Panel(win)
pnl.SetBackgroundColour(wx.Colour(72, 61, 139))
lbl = wx.StaticText(pnl, -1, "Welcome!")
lbl.SetFont(wx.Font(32, wx.SWISS, wx.NORMAL, wx.NORMAL))
lbl.SetForegroundColour(wx.WHITE)
btn = wx.Button(pnl, -1, "OK")
win.Bind(wx.EVT_BUTTON, Quit, btn)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(lbl, 1, wx.CENTER|wx.ALL|wx.ALIGN_CENTRE, 15)
sizer.Add(btn, 0, wx.BOTTOM|wx.ALL|wx.ALIGN_CENTRE, 15)
pnl.SetSizer(sizer)
app.SetTopWindow(win)
win.CentreOnScreen(wx.BOTH)
win.Show(True)
app.MainLoop()
Screenshot:
Attached Thumbnails
Click image for larger version

Name:	wxpythonwindow.png
Views:	229
Size:	7.5 KB
ID:	25146  
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #67  
Old 23rd June 2013, 03:15 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,502
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

Quote:
Originally Posted by cyborg View Post
Seeing as I never got round to the Python GTK one
Well I went ahead and finished it for you . This requires the pygtk2 package from the Fedora repos.
Code:
#!/usr/bin/python
import pygtk
import gtk

class PyGTKwindow(gtk.Window):
	def __init__(self, parent=None):
		gtk.Window.__init__(self)
		self.connect("destroy", lambda *w: gtk.main_quit())
		self.set_title("PyGTK Window Example")
		self.set_default_size(400, 150)
		self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(18504, 15677, 35723))
		vbox = gtk.VBox(False, 20)
		self.add(vbox)
		lbl = gtk.Label()
		lbl.set_justify(gtk.JUSTIFY_CENTER)
		lbl.set_markup("<span foreground='white' size='32000'>Welcome!</span>")
		vbox.pack_start(lbl, True, True, 0)
		okBox = gtk.HBox(True, 0)
		vbox.pack_start(okBox, False, False, 10)
		btn = gtk.Button("OK")
		btn.connect_object("clicked", gtk.Widget.destroy, self)
		okBox.pack_start(btn, False, False, 0)
		self.show_all()

def main():
	PyGTKwindow()
	gtk.main()

main()
Screenshot:
Attached Thumbnails
Click image for larger version

Name:	pygtkwindow.png
Views:	208
Size:	8.6 KB
ID:	25151  
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #68  
Old 26th June 2013, 02:30 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,502
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

Here's an example in Scilab with Tk, which has built-in bindings in Scilab (yum install scilab).

Save this as scilabtkwindow.sce:
Code:
function scilabtkwindow()
   TCL_EvalStr('package require Tk');
   TCL_EvalStr('toplevel .w');
   TCL_EvalStr('wm title .w ""Scilab/Tk Window Example""');
   TCL_EvalStr('wm geometry .w 400x150+300+200');
   TCL_EvalStr('wm protocol .w WM_DELETE_WINDOW {destroy .w}');
   TCL_EvalStr('.w configure -bg darkslateblue');
   TCL_EvalStr('label .w.msg -text ""Welcome!"" -font {-size 32} -bg darkslateblue -fg white');
   TCL_EvalStr('pack .w.msg -pady 20');
   TCL_EvalStr('button .w.ok -text OK -command {destroy .w}');
   TCL_EvalStr('pack .w.ok -pady 10 -expand 1 -anchor s');
   while %t
      TCL_EvalStr('set h [winfo exists .w]');
      if TCL_GetVar("h")=='0' then break,end
      sleep(1);
   end
endfunction
scilabtkwindow();
clear scilabtkwindow;
quit
Run it like this:
Code:
scilab -nb -nw -f scilabtkwindow.sce
Screenshot:
Attached Thumbnails
Click image for larger version

Name:	scilabtkwindow.png
Views:	204
Size:	7.3 KB
ID:	25155  
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #69  
Old 27th June 2013, 02:36 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,502
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

Here's an example in C using the X Athena Widgets (Xaw), which is as low-level as you can go in X GUI programming without having to write your own widgets in raw Xlib. For this you'll need the following packages from the Fedora repos: libX11-devel, libXt-devel, libXaw-devel

Save this as xawwindow.c:
Code:
#include <X11/Shell.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/Command.h>
#include <stdlib.h>

void cb_quit(Widget w, XtPointer client_data, XtPointer call_data) {
   exit(0);
}

int main(int argc, char *argv[]) {
   XtAppContext app;
   Widget toplevel, box, lbl, space, btn;
   Display *display;
   Colormap cmap;
   XColor back, fore, spare;
   XFontStruct *font = NULL;
   Arg al[7];
   int ac;

   XtToolkitInitialize();
   app = XtCreateApplicationContext();
   toplevel = XtAppInitialize(&app, "", NULL, 0, &argc, argv, NULL, NULL, 0);
   XtVaSetValues(toplevel, XtNtitle, "Xaw Window Example", NULL);
   ac = 0;
   XtSetArg(al[ac], XtNwidth, 400); ac++;
   XtSetArg(al[ac], XtNheight, 150); ac++;
   XtSetValues(toplevel, al, ac);

   display = XtDisplay(toplevel);
   cmap = DefaultColormapOfScreen(XtScreen(toplevel));
   XAllocNamedColor(display, cmap, "dark slate blue", &back, &spare);
   XAllocNamedColor(display, cmap, "white", &fore, &spare);

   ac = 0;
   box = XtCreateManagedWidget("box", boxWidgetClass, toplevel, NULL ,0);
   XtManageChild(box);
   XtSetArg(al[ac], XtNbackground, back.pixel); ac++;
   XtSetArg(al[ac], XtNwidth, 400); ac++;
   XtSetArg(al[ac], XtNheight, 150); ac++;
   XtSetArg(al[ac], XtNborderWidth, 0); ac++;
   XtSetArg(al[ac], XtNhSpace, 0); ac++;
   XtSetArg(al[ac], XtNvSpace, 10); ac++;
   XtSetValues(box, al, ac);

   ac = 0;
   lbl = XtCreateManagedWidget("Welcome!", labelWidgetClass, box, NULL ,0);
   XtManageChild(lbl);
   font = XLoadQueryFont(display, "*-lucida-medium-r-normal-sans-34-*" );
   XtSetArg(al[ac], XtNwidth, 400); ac++;
   XtSetArg(al[ac], XtNheight, 90); ac++;
   XtSetArg(al[ac], XtNbackground, back.pixel); ac++;
   XtSetArg(al[ac], XtNforeground, fore.pixel); ac++;
   XtSetArg(al[ac], XtNfont, font); ac++;
   XtSetArg(al[ac], XtNborderWidth, 0); ac++;
   XtSetValues(lbl, al, ac);

   ac = 0;
   space = XtCreateManagedWidget("", labelWidgetClass, box, NULL ,0);
   XtManageChild(space);
   XtSetArg(al[ac], XtNwidth, 180); ac++;
   XtSetArg(al[ac], XtNheight, 25); ac++;
   XtSetArg(al[ac], XtNbackground, back.pixel); ac++;
   XtSetArg(al[ac], XtNborderWidth, 0); ac++;
   XtSetValues(space, al, ac);

   ac = 0;
   btn = XtCreateManagedWidget("OK", commandWidgetClass, box, NULL ,0);
   XtManageChild(btn);
   XtSetArg(al[ac], XtNwidth, 40); ac++;
   XtSetArg(al[ac], XtNheight, 25); ac++;
   XtSetValues(btn, al, ac);
   XtAddCallback(btn, XtNcallback, cb_quit, NULL);

   XtRealizeWidget(toplevel);
   XtAppMainLoop(app);
}
If that looks similar to the Motif example it's because it is. They're both built on top of Xt.

Compile it like this:
Code:
gcc -o xawwindow xawwindow.c -lX11 -lXt -lXaw
Note: You can give it a "3d" look by replacing "Xaw" by "Xaw3d" in the 3 include statements and in the gcc command (you'll need the Xaw3d-devel package if you want to do that). There's also a NeXT-ish look you can give it by using the neXtaw-devel package (this time replace "Xaw" by "neXtaw").

Screenshots of the plain Xaw version and the Xaw3d version:
Attached Thumbnails
Click image for larger version

Name:	xawwindow.png
Views:	221
Size:	3.8 KB
ID:	25160   Click image for larger version

Name:	xaw3dwindow.png
Views:	198
Size:	3.9 KB
ID:	25161  
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #70  
Old 27th June 2013, 03:09 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,502
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

Latest update: we now have 60 examples.

Here's a summary of what we have right now, first organized by programming language with the corresponding toolkit:
  1. C: XForms, Motif, MGUI, EZWGL, GTK+, Tk, XView, X Athena Widgets
  2. Python: Tk, Qt, GTK+, Swing, Kivy, wxWidgets
  3. Java: Swing, Qt, SWT, AWT, JavaFX
  4. Perl: Tk, wxWidgets, Prima, GTK+, Qt
  5. C++: Qt, FOX, FLTK, GTK+
  6. Ruby: Tk, Swing, Qt
  7. Tcl: Tk, GTK+, Qt
  8. R: Tk, Qt
  9. Guile: GTK+, Tk
  10. Haskell: GTK+, Tk
  11. JavaScript: Swing, jQuery
  12. Lua: Tk, Qt
  13. Matlab: Swing, Matlab GUI
  14. Ada: Qt, Tk
  15. Objective-C: GNUstep
  16. Icon: Icon
  17. Vala: GTK+
  18. Genie: GTK+
  19. OCaml: Tk
  20. Pure: Tk
  21. SNOBOL: Tk
  22. Scala: Swing
  23. BeanShell: Swing
  24. Groovy: Swing
  25. Clojure: Swing
  26. Scilab: Tk

Here's the list organized by toolkit with the corresponding language bindings:
  1. Tk: Tcl, Perl, Python, Ruby, R, OCaml, Pure, SNOBOL, C, Lua, Guile, Haskell, Ada, Scilab
  2. Swing: Java, Python, Ruby, Scala, BeanShell, Groovy, Clojure, JavaScript, Matlab
  3. Qt: Python, C++, Java, Perl, Ruby, Tcl, R, Lua, Ada
  4. GTK+: Haskell, Python, Perl, C++, Vala, Genie, C, Tcl, Guile
  5. wxWidgets: Perl, Python
  6. XForms: C
  7. GNUstep: Objective-C
  8. Motif: C
  9. Icon: Icon
  10. Prima: Perl
  11. FOX: C++
  12. FLTK: C++
  13. MGUI: C
  14. EZWGL: C
  15. SWT: Java
  16. AWT: Java
  17. XView: C
  18. JavaFX: Java
  19. jQuery: JavaScript
  20. Kivy: Python
  21. Matlab GUI: Matlab
  22. X Athena Widgets: C
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #71  
Old 28th June 2013, 01:07 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,502
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

Here's an example in Python with FLTK, using the pyFLTK bindings. I just downloaded the 1.3.0 source tarball and followed the instructions to build and install it. You'll need the fltk-devel and swig packages from the Fedora repos for that.

Here's the script, which is almost identical to the code of the C++ version:
Code:
#!/usr/bin/python
from fltk import *
import sys

def quit_cb(ptr, widget):
	sys.exit(0)

win = Fl_Window(400, 150, "pyFLTK Window Example")
win.color(0x483D8B00)
lbl = Fl_Box(110, 20, 180, 50, "Welcome!")
lbl.labelsize(32)
lbl.labelcolor(FL_WHITE)
btn = Fl_Button(180, 100, 40, 30, "OK")
btn.callback(quit_cb, win)
win.end()
win.show(sys.argv)
Fl.run()
Screenshot:
Attached Thumbnails
Click image for larger version

Name:	pyfltkwindow.png
Views:	213
Size:	7.0 KB
ID:	25164  
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #72  
Old 28th June 2013, 05:40 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,502
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

Here's an example in C++ with wxWidgets. For this you'll need to install the wxGTK-devel package from the fedora repos.

Save this as wxwindow.cpp:
Code:
#include <wx/wx.h>

class MyApp : public wxApp {
   public:
      virtual bool OnInit();
};

class mf : public wxFrame {
   public:
      mf(const wxString& title, const wxPoint& pos, const wxSize& size);
      void OnQuit(wxCommandEvent& event); 

   private:
      DECLARE_EVENT_TABLE()
};

enum {
   ID_Quit = wxID_EXIT,
};

BEGIN_EVENT_TABLE(mf, wxFrame) 
   EVT_BUTTON(ID_Quit,  mf::OnQuit) 
END_EVENT_TABLE() 

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit() {
   if (!wxApp::OnInit())
      return false;
   mf *win = new mf(_T("wxWidgets Window Example"), wxPoint(0,0), wxSize(400,150));
   SetTopWindow(win);
   win->CentreOnScreen(wxBOTH);
   win->Show(true);
   return true;
}

mf::mf(const wxString& title, const wxPoint& pos, const wxSize& size)
       : wxFrame((wxFrame *)NULL, -1, title, pos, size) {
   wxPanel *pnl = new wxPanel(this);
   pnl->SetBackgroundColour(_T("dark slate blue"));
   wxStaticText *lbl = new wxStaticText(pnl, -1, _T("Welcome!"));
   lbl->SetForegroundColour(_T("white"));
   lbl->SetFont(*wxFont::New(32, wxSWISS, wxNORMAL, wxNORMAL));
   wxButton *btn = new wxButton(pnl, wxID_EXIT, _T("OK"));
   wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
   sizer->Add(lbl, 1, wxCENTER|wxALL|wxALIGN_CENTRE, 15);
   sizer->Add(btn, 0, wxBOTTOM|wxALL|wxALIGN_CENTRE, 15);
   pnl->SetSizer(sizer); 
}

void mf::OnQuit(wxCommandEvent& WXUNUSED(event)) { 
   Close(true); 
}
Compile it like this:
Code:
g++ -o wxwindow wxwindow.cpp $(wx-config --cppflags --libs)
Screenshot:
Attached Thumbnails
Click image for larger version

Name:	wxwindow.png
Views:	252
Size:	8.5 KB
ID:	25165  
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #73  
Old 29th June 2013, 01:39 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,502
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

Here's an example of Tcl with FLTK, using the Tcl/FLTK bindings, which you can download here: http://sourceforge.net/projects/fltk...lFltk%201.0.x/
I used the TclFltk-1.0.155-14-amd64-linux-bin.rpm file, which installs under /usr/local, for some reason. I ran into 2 problems with it: it only works with Tcl/Tk 8.4.x, and it needs libtiff.so.4. I fixed the first issue by downloading the ancient F7 tcl-8.4.13-16.fc7.x86_64.rpm and tk-8.4.13-5.fc7.x86_64.rpm files and then using rpm2cpio and cpio -ivd to unpack those RPMs into a temp directory and copy the /usr/lib64/libtcl8.4.so and /usr/lib64/libtk8.4.so libraries into /usr/local/lib, as well as copying /usr/share/tcl8.4 to /usr/local/lib/tcl8.4 and copying /usr/lib64/tk8.4 and /usr/share/tk8.4 to /usr/local/lib/tk8.4. The bigger problem was libtiff.so.4, which has never existed in Fedora. Trying symlinks to libtiff.so.5 or libtiff.so.3 didn't work. So I had to download tiff-4.0.0beta4.tar.gz from here and compile it to say version 4, which meant changing every Makefile to have "LIBTIFF_VERSION_INFO = 4:1:0". Kind of a pain, but it worked by copying the resulting libtiff.so.4 to /usr/local/lib.

Anyway, here's the script:
Code:
#!/usr/local/bin/fltkwish
Toplevel root -label "Tcl/FLTK Window Example" -background {72,61,139} -w 400 -h 150 -relief flat
Label root.lbl -x 110 -y 20 -w 180 -h 50 -text "Welcome!" -relief flat \
               -background {72,61,139} -foreground white -fontsize 32
Button root.btn -x 180 -y 100 -w 40 -h 30 -text "OK" -command {exit}
Show root
Screenshot:
Attached Thumbnails
Click image for larger version

Name:	fltkwindow-tcl.png
Views:	239
Size:	6.4 KB
ID:	25170  
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #74  
Old 30th June 2013, 10:05 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,502
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

Here's an example in Ruby with the FOX toolkit, using the FXRuby bindings. It's not in the Fedora repos but it's easy to install by using rubygems. First use yum to install the fox-devel and ruby-devel packages, then do this:
Code:
gem install fxruby
Here is the script (almost identical to the C++ example):
Code:
#!/usr/bin/ruby
require 'fox16'
include Fox
app = FXApp.new("fxrubywindow")
win = FXMainWindow.new(app, "FXRuby Window Example", :width => 400, :height => 150)
win.backColor = FXRGB(72, 61, 139)
lbl = FXLabel.new(win, "Welcome!", nil, LAYOUT_CENTER_X, 0, 0, 0, 0, 0, 0, 20, 30)
lbl.backColor = FXRGB(72, 61, 139)
lbl.textColor = FXRGB(255, 255, 255)
lbl.font = FXFont.new(app, "Sans,320")
btn = FXButton.new(win, "OK", nil, app, FXApp::ID_QUIT, LAYOUT_CENTER_X|BUTTON_NORMAL,
                   0, 0, 0, 0, 15, 15, 5, 5)
app.create()
win.show(PLACEMENT_SCREEN)
app.run()
Screenshot:
Attached Thumbnails
Click image for larger version

Name:	fxrubywindow.png
Views:	210
Size:	7.9 KB
ID:	25182  
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #75  
Old 1st July 2013, 07:54 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 6,502
linuxfedorafirefox
Re: Programming challenge: Create a GUI window

Here's an example in KornShell (ksh) with Motif, using the Desktop KornShell (dtksh) bindings that come with CDE, the Common Desktop Environment. CDE was open-sourced last year, and I finally got it to compile in F18. A new release (2.2.0d-beta) came out about a month ago, and by following the instructions here as well as Fedora-specific instructions here it compiled, including dtksh. Yay!

Here's the script:
Code:
#!/usr/dt/bin/dtksh
function cb_exit {
   exit
}
XtInitialize TOPLEVEL DTWIN dtwin dtkshwindow "@"
XtSetValues $TOPLEVEL width:400 height:150 title:"Desktop KornShell Window Example"
XtCreateManagedWidget BB bb XmBulletinBoard $TOPLEVEL background:darkslateblue shadowThickness:0
XtCreateManagedWidget LBL "Welcome!" XmLabel $BB x:90 y:20 width:220 alignment:ALIGNMENT_CENTER \
   background:darkslateblue foreground:white fontList:"*-lucida-medium-r-normal-sans-34-*"
XtCreateManagedWidget BTN "OK" XmPushButton $BB x:175 y:110 width:50
XtAddCallback $BTN activateCallback 'cb_exit'
XtRealizeWidget $TOPLEVEL
XtMainLoop
The nice thing is that dtksh gives you full access to all the Motif widgets in a shell script. I think this makes ksh the only Linux/UNIX shell with a full-fledged GUI toolkit. Unfortunately Motif uses bitmap fonts by default. But if you use the motif-devel package from the Fedora repos to compile CDE, then you'll get access to anti-aliased fonts in dtksh by way of an X resources file. For example, put this in ~/.Xresources:
Code:
dtkshwindow*XmLabel.renderTable: rt1
dtkshwindow*rt1.fontType: FONT_IS_XFT
dtkshwindow*rt1.fontName: DejaVu Sans
dtkshwindow*rt1.fontSize: 32
dtkshwindow*XmPushButton.renderTable: rt2
dtkshwindow*rt2.fontType: FONT_IS_XFT
dtkshwindow*rt2.fontName: DejaVu Sans
dtkshwindow*rt2.fontSize: 10
Then run this command:
Code:
xrdb -load ~/.Xresources
You'll now have nice anti-aliased fonts for both the label and button in the above dtksh script. Using an X resources file seems to be the only way to do this right now; I couldn't find a way to do it within the script. And even using X resources I'm sure there's a cleaner way to do it than the clumsy way I did.

Below are screenshots of how it looks without and with the above resources. The xft-enabled version looks much better!
Attached Thumbnails
Click image for larger version

Name:	dtkshwindow.png
Views:	239
Size:	4.4 KB
ID:	25185   Click image for larger version

Name:	dtkshwindow-xft.png
Views:	228
Size:	7.9 KB
ID:	25186  
__________________
OS: Fedora 25 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
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 12:16 AM
How does KDE 3.7 create popup window on detection of USB drives. Brian1 Using Fedora 0 22nd November 2007 11:12 PM
Which Window Manager are you using for programming ? michaeleekk Programming & Packaging 11 29th July 2007 08:45 AM
gtk programming ~ how to link to another window after button is clicked? yan612 Programming & Packaging 2 3rd October 2006 09:10 AM
Automatic daily login script *Programming Challenge* SickFreak Programming & Packaging 4 23rd November 2005 10:45 PM


Current GMT-time: 08:54 (Thursday, 27-04-2017)

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