Hello world/Graphical: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added PicoLisp)
Line 88: Line 88:
function = 0
function = 0
End Function</lang>
End Function</lang>

{{works with|PureBasic}}
<lang PureBasic>MessageRequester("Info", "Goodbye, World!")</lang>


=={{header|C}}==
=={{header|C}}==

Revision as of 15:47, 24 February 2010

Task
Hello world/Graphical
You are encouraged to solve this task according to the task description, using any language you may know.

In this User Output task, the goal is to display the string "Goodbye, World!" on a GUI object (alert box, plain window, text area, etc.).

See also: User Output - text

ActionScript

<lang actionscript>trace("Goodbye, World!");</lang>

Ada

Library: GTK version GtkAda
Library: GtkAda

<lang ada>with Gdk.Event; use Gdk.Event; with Gtk.Label; use Gtk.Label; with Gtk.Window; use Gtk.Window; with Gtk.Widget; use Gtk.Widget;

with Gtk.Handlers; with Gtk.Main;

procedure Windowed_Goodbye_World is

  Window : Gtk_Window;
  Label  : Gtk_Label;
  package Handlers is new Gtk.Handlers.Callback (Gtk_Widget_Record);
  package Return_Handlers is
     new Gtk.Handlers.Return_Callback (Gtk_Widget_Record, Boolean);
  function Delete_Event (Widget : access Gtk_Widget_Record'Class)
     return Boolean is
  begin
     return False;
  end Delete_Event;
  procedure Destroy (Widget : access Gtk_Widget_Record'Class) is
  begin
    Gtk.Main.Main_Quit;
  end Destroy;

begin

  Gtk.Main.Init;
  Gtk.Window.Gtk_New (Window);
  Gtk_New (Label, "Goodbye, World!");
  Add (Window, Label);
  Return_Handlers.Connect
  (  Window,
     "delete_event",
     Return_Handlers.To_Marshaller (Delete_Event'Access)
  );
  Handlers.Connect
  (  Window,
     "destroy",
     Handlers.To_Marshaller (Destroy'Access)
  );
  Show_All (Label);
  Show (Window);
  Gtk.Main.Main;

end Windowed_Goodbye_World;</lang>

AppleScript

<lang applescript>display dialog "Goodbye, World!" buttons {"Bye"}</lang>

AutoHotkey

<lang autohotkey>MsgBox, Goodbye`, World!</lang> <lang autohotkey>ToolTip, Goodbye`, World!</lang> <lang autohotkey>Gui, Add, Text, x4 y4, To be announced: Gui, Add, Edit, xp+90 yp-3, Goodbye, World! Gui, Add, Button, xp+98 yp-1, OK Gui, Show, w226 h22 , Rosetta Code Return</lang> <lang autohotkey>SplashTextOn, 100, 100, Rosetta Code, Goodbye, World!</lang>

BASIC

Works with: FreeBASIC

<lang basic>' Demonstrate a simple Windows application using FreeBasic

  1. include once "windows.bi"

Declare Function WinMain(ByVal hInst As HINSTANCE, _

     ByVal hPrev As HINSTANCE, _
     ByVal szCmdLine as String, _
     ByVal iCmdShow As Integer) As Integer

End WinMain( GetModuleHandle( null ), null, Command( ), SW_NORMAL )

Function WinMain (ByVal hInst As HINSTANCE, _

                 ByVal hPrev As HINSTANCE, _
                 ByVal szCmdLine As String, _
                 ByVal iCmdShow As Integer) As Integer
   MessageBox(NULL, "Goodbye World", "Goodbye World", MB_ICONINFORMATION)
   function = 0

End Function</lang>

Works with: PureBasic

<lang PureBasic>MessageRequester("Info", "Goodbye, World!")</lang>

C

Library: GTK

<lang c>#include <gtk/gtk.h>

int main (int argc, char **argv) {

 GtkWidget *window;
 gtk_init(&argc, &argv);
 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 gtk_window_set_title (GTK_WINDOW (window), "Goodbye, World");
 g_signal_connect (G_OBJECT (window), "delete-event", gtk_main_quit, NULL);
 gtk_widget_show_all (window);
 gtk_main();
 return 0;

}</lang>

Library: Win32

Where hWnd is a valid window handle corresponding to a control in the application <lang c>#include "windows.h" void SayGoodbyeWorld(HWND hWnd) {

 SetWindowText(hWwnd, _T("Goodbye, World!"));

}</lang>

C#

Winforms

<lang csharp>using System; using System.Windows.Forms;

class Program {

   static void Main(string[] args) {
       Application.EnableVisualStyles(); //Optional.
       MessageBox.Show("Hello World!");
   }

}</lang>

Gtk

<lang csharp>using Gtk; using GtkSharp;

public class GoodbyeWorld {

 public static void Main(string[] args) {
   Gtk.Window window = new Gtk.Window();
   window.Title = "Goodbye, World";
   window.DeleteEvent += delegate { Application.Quit(); };
   window.ShowAll();
   Application.Run();
 }

}</lang>

C++

Works with: GCC version 3.3.5
Library: GTK

<lang cpp>#include <gtkmm.h> int main(int argc, char *argv[]) {

  Gtk::Main app(argc, argv);
  Gtk::MessageDialog msg("Goodbye, World!");
  msg.run();

}</lang>

Library: Win32

All Win32 APIs work in C++ the same way as they do in C. See the C example.

Library: MFC

Where pWnd is a pointer to a CWnd object corresponding to a valid window in the application. <lang cpp>#include "afx.h" void ShowGoodbyeWorld(CWnd* pWnd) {

   pWnd->SetWindowText(_T("Goodbye, World!"));

}</lang>

Clean

Library: Object I/O

<lang clean>import StdEnv, StdIO

Start :: *World -> *World Start world = startIO NDI Void (snd o openDialog undef hello) [] world where

   hello = Dialog "" (TextControl "Goodbye, World!" []) 
                                    [WindowClose (noLS closeProcess)]</lang>

Common Lisp

This can be done using the extension package ltk that provides an interface to the Tk library.

Library: Tk

<lang lisp>(use-package :ltk)

(defun show-message (text)

 "Show message in a label on a Tk window"
 (with-ltk ()
     (let* ((label (make-instance 'label :text text))
            (button (make-instance 'button :text "Done"
                                   :command (lambda () 
                                              (ltk::break-mainloop)
                                              (ltk::update)))))
             (pack label :side :top :expand t :fill :both)
             (pack button :side :right)
             (mainloop))))

(show-message "Goodbye World")</lang>

D

Library: gtkD

<lang D>import gtk.MainWindow; import gtk.Label; import gtk.Main;

class GoodbyeWorld : MainWindow {

       this()
       {
               super("GtkD");
               add(new Label("Goodbye World"));
               showAll();
       }

}

void main(string[] args) {

       Main.init(args);
       new GoodbyeWorld();
       Main.run();

}</lang>

E

Library: SWT

This is a complete application. If it were part of a larger application, the portions related to interp would be removed.

<lang e>def <widget> := <swt:widgets.*> def SWT := <swt:makeSWT>

def frame := <widget:makeShell>(currentDisplay)

 frame.setText("Rosetta Code")
 frame.setBounds(30, 30, 230, 60)
 frame.addDisposeListener(def _ { to widgetDisposed(event) {
   interp.continueAtTop()
 }})

def label := <widget:makeLabel>(frame, SWT.getLEFT())

 label.setText("Goodbye, World!")
 swtGrid`$frame: $label`

frame.open()

interp.blockAtTop()</lang>

eC

MessageBox:

<lang ec>import "ecere" MessageBox goodBye { contents = "Goodbye, World!" };</lang>

Label:

<lang ec>import "ecere" Label label { text = "Goodbye, World!", hasClose = true, opacity = 1, size = { 320, 200 } };</lang>

Titled Form + Surface Output:

<lang ec>import "ecere"

class GoodByeForm : Window {

  text = "Goodbye, World!";
  size = { 320, 200 };
  hasClose = true;
  void OnRedraw(Surface surface)
  {
     surface.WriteTextf(10, 10, "Goodbye, World!");
  }

}

GoodByeForm form {};</lang>

F#

Just display the text in a message box. <lang fsharp>#light open System open System.Windows.Forms [<EntryPoint>] let main _ =

   MessageBox.Show("Hello World!") |> ignore
   0</lang>

Factor

To be pasted in the listener :

   USE: ui ui.gadgets.labels
   [ "Goodbye World" <label> "Rosetta Window" open-window ] with-ui

Icon

<lang icon>link graphics procedure main()

  WOpen("size=80,20") | stop("No window")
  WWrites("goodbye world")
  WDone()

end</lang>

J

<lang j>wdinfo'Goodbye, World!'</lang>

Java

Library: Swing

<lang java>import javax.swing.*; public class OutputSwing {

   public static void main(String[] args) throws Exception {
       JOptionPane.showMessageDialog (null, "Goodbye, World!");//alert box
       JFrame window = new JFrame("Goodbye, World!");//text on title bar
   	JTextArea text = new JTextArea();
   	text.setText("Goodbye, World!");//text in editable area
   	JButton button = new JButton("Goodbye, World!");//text on button
   	
   	//so the button and text area don't overlap
   	window.setLayout(new FlowLayout());
   	window.add(button);//put the button on first
   	window.add(text);//then the text area
   	
   	window.pack();//resize the window so it's as big as it needs to be
   	
   	window.setVisible(true);//show it
   	//stop the program when the window is closed
   	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

}</lang>

JavaScript

Works with: Firefox version 2.0

This pops up a small dialog, so it might be termed GUI display. <lang javascript> alert("Goodbye, World!");</lang>

Works with: UCB Logo

Among the turtle commands are some commands for drawing text in the graphical area. Details and capabilities differ among Logo implementations. <lang logo>LABEL [Hello, World!] SETLABELHEIGHT 2 * last LABELSIZE LABEL [Goodbye, World!]</lang>

Lua

<lang lua>require "iuplua"

dlg = iup.dialog{iup.label{title="Goodbye, World!"}; title="test"} dlg:show()

if (not iup.MainLoopLevel or iup.MainLoopLevel()==0) then

 iup.MainLoop()

end</lang>

MAXScript

<lang maxscript>messageBox "Goodbye world"</lang>

Modula-3

Library: Trestle

<lang modula3>MODULE GUIHello EXPORTS Main;

IMPORT TextVBT, Trestle;

<*FATAL ANY*>

VAR v := TextVBT.New("Goodbye, World!");

BEGIN

 Trestle.Install(v);
 Trestle.AwaitDelete(v);

END GUIHello.</lang> This code requires an m3makefile.

import ("ui")
implementation ("GUIHello")
program ("Hello")

This tells the compiler to link with the UI library, the file name of the implementation code, and to output a program named "Hello".

Objective-C

To show a modal alert: <lang objc>NSAlert *alert = [[[NSAlert alloc] init] autorelease]; [alert setMessageText:@"Goodbye, World!"]; [alert runModal];</lang>

OCaml

Library: GTK

<lang ocaml>let delete_event evt = false

let destroy () = GMain.Main.quit ()

let main () =

 let window = GWindow.window in
 let _ = window#set_title "Goodbye, World" in
 let _ = window#event#connect#delete ~callback:delete_event in
 let _ = window#connect#destroy ~callback:destroy in
 let _ = window#show () in
 GMain.Main.main ()

let _ = main () ;;</lang>

Library: OCaml-Xlib
Library: Tk
ocaml -I +labltk labltk.cma

Just output as a label in a window: <lang ocaml>let () =

 let main_widget = Tk.openTk () in
 let lbl = Label.create ~text:"Goodbye, World" main_widget in
 Tk.pack [lbl];
 Tk.mainLoop();;</lang>

Output as text on a button that exits the current application: <lang ocaml>let () =

 let action () = exit 0 in
 let main_widget = Tk.openTk () in
 let bouton_press =
   Button.create main_widget ~text:"Goodbye, World" ~command:action in
 Tk.pack [bouton_press];
 Tk.mainLoop();;</lang>

Oz

<lang oz>declare

 [QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']}
 Window = {QTk.build td(label(text:"Goodbye, World!"))}

in

 {Window show}</lang>

Perl

Works with: Perl version 5.8.8
Library: Tk

Just output as a label in a window:

<lang perl>use Tk;

$main = MainWindow->new; $main->Label(-text => 'Goodbye, World')->pack; MainLoop();</lang>

Output as text on a button that exits the current application:

<lang perl>use Tk;

$main = MainWindow->new; $main->Button(

 -text => 'Goodbye, World',
 -command => \&exit,

)->pack; MainLoop();</lang>

Library: Gtk2

<lang perl>use Gtk2 '-init';

$window = Gtk2::Window->new; $window->set_title('Goodbye world'); $window->signal_connect(

 'destroy' => sub { Gtk2->main_quit; }

);

$label = Gtk2::Label->new('Goodbye, world'); $window->add($label);

$window->show_all; Gtk2->main;</lang>

PHP

Library: PHP-GTK

<lang php>if (!class_exists('gtk')) {

   die("Please load the php-gtk2 module in your php.ini\r\n");

}

$wnd = new GtkWindow(); $wnd->set_title('Goodbye world'); $wnd->connect_simple('destroy', array('gtk', 'main_quit'));

$lblHello = new GtkLabel("Goodbye, World!"); $wnd->add($lblHello);

$wnd->show_all(); Gtk::main();</lang>

PicoLisp

<lang PicoLisp>(call 'dialog "--msgbox" "Goodbye, World!" 5 20)</lang>

PostScript

In the geenral Postscript context, the show command will render the string that is topmost on the stack at the currentpoint in the previously setfont. Thus a minimal PostScript file that will print on a PostScript printer or previewer might look like this:

<lang postscript>%!PS % render in Helvetica, 12pt: /Helvetica findfont 12 scalefont setfont % somewhere in the lower left-hand corner: 50 dup moveto % render text (Goodbye World) show % wrap up page display: showpage</lang>

Python

Works with: Python version 2.5
Library: Tkinter

<lang python>import tkMessageBox

result = tkMessageBox.showinfo("Some Window Label", "Goodbye, World!")</lang>

Note: The result is a string of the button that was pressed.

Library: GTK

<lang python>import pygtk pygtk.require('2.0') import gtk

window = gtk.Window() window.set_title('Goodbye, World') window.connect('delete-event', gtk.main_quit) window.show_all() gtk.main()</lang>

RapidQ

<lang rapidq>MessageBox("Goodbye, World!", "RapidQ example", 0)</lang>

R

Library: GTK

Rather minimalist, but working... <lang R>library(RGtk2) # bindings to Gtk w <- gtkWindowNew() l <- gtkLabelNew("Goodbye, World!") w$add(l)</lang>

REBOL

<lang REBOL>alert "Goodbye, World!"</lang>

Ruby

Library: GTK

<lang ruby>require 'gtk2'

window = Gtk::Window.new window.title = 'Goodbye, World' window.signal_connect(:delete-event) { Gtk.main_quit } window.show_all

Gtk.main</lang>

Library: Ruby/Tk

<lang ruby>require 'tk' root = TkRoot.new("title" => "User Output") TkLabel.new(root, "text"=>"goodbye world").pack("side"=>'top') Tk.mainloop</lang>

Smalltalk

<lang smalltalk>MessageBox show: 'Goodbye, world.'</lang>

Tcl

Library: Tk

Just output as a label in a window: <lang tcl>pack [label .l -text "Goodbye, World"]</lang>

Output as text on a button that exits the current application: <lang tcl>pack [button .b -text "Goodbye, World" -command exit]</lang>

Or as a message box: <lang tcl>tk_messageBox -message "Goodbye, World"</lang>

TI-89 BASIC

<lang ti89b>Dialog

 Text "Goodbye, World!"

EndDlog</lang>

Vedit macro language

Displaying the message on status line. The message remains visible until the next keystroke, but macro execution continues. <lang vedit>Statline_Message("Goodbye, World!")</lang>

Displaying a dialog box with the message and default OK button: <lang vedit>Dialog_Input_1(1,"`Vedit example`,`Goodbye, World!`")</lang>

Visual Basic .NET

Works with: Visual Basic version 2005

<lang vbnet>Module GoodbyeWorld

   Sub Main()
       Messagebox.Show("Goodbye, World!")
   End Sub

End Module</lang>