Window creation: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(Using Template:Libheader, instead of Template:Library)
Line 5: Line 5:
'''Compiler:''' [[gcc]] 4.0.3
'''Compiler:''' [[gcc]] 4.0.3


{{library|SDL}}
{{libheader|SDL}}
'''Compile Command:''' gcc `sdl-config --cflags` `sdl-config --libs` SDL_Window.c -o window
'''Compile Command:''' gcc `sdl-config --cflags` `sdl-config --libs` SDL_Window.c -o window
/*
/*
Line 30: Line 30:
}
}


{{library|Gtk}}
{{libheader|Gtk}}
'''Compile command:''' gcc `gtk-config --cflags` `gtk-config --libs` -o window window.c
'''Compile command:''' gcc `gtk-config --cflags` `gtk-config --libs` -o window window.c


Line 50: Line 50:
}
}


{{library|Gtk2}}
{{libheader|Gtk2}}
'''Compile command:''' gcc -Wall -pedantic `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0` -o window window.c
'''Compile command:''' gcc -Wall -pedantic `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0` -o window window.c


Line 70: Line 70:


=={{header|C++}}==
=={{header|C++}}==
{{library|Qt}} 4
{{libheader|Qt}} 4


'''Compiler command:''' qmake -pro; qmake
'''Compiler command:''' qmake -pro; qmake
Line 85: Line 85:
}
}


{{library|Gtk+}}
{{libheader|Gtk+}}


'''Compiler command:''' g++ filename.cc -o test `pkg-config --cflags --libs gtkmm-2.4`
'''Compiler command:''' g++ filename.cc -o test `pkg-config --cflags --libs gtkmm-2.4`
Line 131: Line 131:


=={{header|D}}==
=={{header|D}}==
{{library|FLTK4d}}
{{libheader|FLTK4d}}
module Window;
module Window;
Line 142: Line 142:
}
}


{{library|Derelict}}
{{libheader|Derelict}}
{{library|SDL}}
{{libheader|SDL}}
import derelict.sdl.sdl;
import derelict.sdl.sdl;
Line 177: Line 177:
}
}


{{library|QD}}
{{libheader|QD}}
QD is a simple and easy-to-use wrapper around SDL.
QD is a simple and easy-to-use wrapper around SDL.
import qd;
import qd;
Line 233: Line 233:
=={{header|Java}}==
=={{header|Java}}==


'''Library:''' [[Swing]]
{{libheader|Swing}}


[[Category:Swing]]
import javax.swing.JFrame;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import javax.swing.WindowConstants;
Line 267: Line 266:
'''Interpreter:''' [[Perl]] 5.8.8
'''Interpreter:''' [[Perl]] 5.8.8


{{library|Tk}}
{{libheader|Tk}}
use Tk;
use Tk;
Line 273: Line 272:
MainLoop;
MainLoop;


{{library|SDL}}
{{libheader|SDL}}
use SDL::App;
use SDL::App;
use SDL::Event;
use SDL::Event;
Line 282: Line 281:
});
});


{{library|Gtk}}
{{libheader|Gtk}}
use Gtk '-init';
use Gtk '-init';
Line 292: Line 291:
Gtk->main;
Gtk->main;


{{library|Gtk2}}
{{libheader|Gtk2}}
use Gtk2 '-init';
use Gtk2 '-init';
Line 305: Line 304:
'''Interpreter:''' [[Python]] 2.4, 2.5
'''Interpreter:''' [[Python]] 2.4, 2.5


{{library|Tkinter}}
{{libheader|Tkinter}}
import Tkinter
import Tkinter
Line 311: Line 310:
w.mainloop()
w.mainloop()


{{library|wxPython}}
{{libheader|wxPython}}
from wxPython.wx import *
from wxPython.wx import *
Line 324: Line 323:
app.MainLoop()
app.MainLoop()


{{library|Pythonwin}}
{{libheader|Pythonwin}}
import win32ui
import win32ui
from pywin.mfc.dialog import Dialog
from pywin.mfc.dialog import Dialog
Line 331: Line 330:
d.CreateWindow()
d.CreateWindow()


{{library|PyGTK}}
{{libheader|PyGTK}}
import gtk
import gtk
Line 349: Line 348:
'''Interpreter:''' [[Ruby]] 1.8.5
'''Interpreter:''' [[Ruby]] 1.8.5


{{library|Tk}}
{{libheader|Tk}}
require 'tk'
require 'tk'
Line 355: Line 354:
window::mainloop()
window::mainloop()


{{library|GTK+}}
{{libheader|GTK+}}
require 'gtk2'
require 'gtk2'
Line 373: Line 372:


=={{header|Toka}}==
=={{header|Toka}}==
{{library|SDL}}
{{libheader|SDL}}


Toka does not inherently know about graphical environments, but can interact with them using external libraries. This example makes use of the [[SDL]] library bindings included with Toka.
Toka does not inherently know about graphical environments, but can interact with them using external libraries. This example makes use of the [[SDL]] library bindings included with Toka.

Revision as of 06:28, 5 February 2008

Task
Window creation
You are encouraged to solve this task according to the task description, using any language you may know.

C

Standard: ANSI C (AKA C89):

Compiler: gcc 4.0.3

Library: SDL

Compile Command: gcc `sdl-config --cflags` `sdl-config --libs` SDL_Window.c -o window

/*
 *   Opens an 800x600 16bit color window. 
 *   Done here with ANSI C.
 */

#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"

int main()
{
  SDL_Surface *screen;
  
  if (SDL_Init(SDL_INIT_VIDEO) != 0) {
    fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
    return 1;
  }
  atexit(SDL_Quit);
  screen = SDL_SetVideoMode( 800, 600, 16, SDL_SWSURFACE | SDL_HWPALETTE );
  
  return 0;
}
Library: Gtk

Compile command: gcc `gtk-config --cflags` `gtk-config --libs` -o window window.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_signal_connect(GTK_OBJECT(window), "destroy",
    GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
  gtk_widget_show(window);
  gtk_main();

  return 0;
}
Library: Gtk2

Compile command: gcc -Wall -pedantic `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0` -o window window.c

#include <gtk/gtk.h>

int
main(int argc, char *argv[])
{
  GtkWidget *window;

  gtk_init(&argc, &argv);
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
  gtk_widget_show(window);
  gtk_main();

  return 0;
}

C++

Library: Qt

4

Compiler command: qmake -pro; qmake

#include <QApplication>
#include <QMainWindow>

int main(int argc, char *argv[])
{
 QApplication app(argc, argv);
 QMainWindow window;
 window.show();
 return app.exec();
}
Library: Gtk+

Compiler command: g++ filename.cc -o test `pkg-config --cflags --libs gtkmm-2.4`

#include <iostream>
#include <gtkmm.h>

int
main( int argc, char* argv[] )
{
 try
 {
  Gtk::Main m( argc, argv ) ;
  Gtk::Window win ;
  m.run( win ) ;
 }
 
 catch( std::exception const & exc )
 {
  std::cout << exc.what() << std::endl ;
  exit( -1 ) ;
 }
 
 exit( 0 ) ;
}

C#

Compiler: Visual C# 2003

Using: System.Windows.Forms.dll

 using System;
 using System.Windows.Forms;
 
 public class Window
 {
   [STAThread]
   static void Main()
   {
     Form newForm = new Form();
     newForm.Text = "It's a new Window!";
     newForm.Show();
   }
 }

D

Library: FLTK4d
module Window;

import fltk4d.all;

void main() {
    auto window = new Window(300, 300, "A window");
    window.show;
    FLTK.run;
}
Library: Derelict
Library: SDL
import derelict.sdl.sdl;

int main(char[][] args)
{
    DerelictSDL.load();

    SDL_Event event;
    auto done = false;

    SDL_Init(SDL_INIT_VIDEO);
    scope(exit) SDL_Quit();

    SDL_SetVideoMode(1024, 768, 0, SDL_OPENGL);
    SDL_WM_SetCaption("My first Window", "SDL test");
	 
    while (!done)
    {
        if (SDL_PollEvent(&event) == 1)
        {
            switch (event.type)
	     {
                case SDL_QUIT:
	              done = true;
		          break;
		 default:
		      break;
	     }
	 }		
    }

   return 0;
}
Library: QD

QD is a simple and easy-to-use wrapper around SDL.

import qd;

void main() {
  screen(640, 480);
  while (true) events();
}


E

Java AWT/Swing

Implementation: E-on-Java

when (currentVat.morphInto("awt")) -> {
    def w := <swing:makeJFrame>("Window")
    w.setContentPane(<swing:makeJLabel>("Contents"))
    w.pack()
    w.show()
}

Groovy

 import groovy.swing.SwingBuilder

 new SwingBuilder().frame(title:'My Window', size:[200,100]).show()

IDL

With some example values filled in:

Id = WIDGET_BASE(TITLE='Window Title',xsize=200,ysize=100)
WIDGET_CONTROL, /REALIZE, id

J

MINWDW=: 0 : 0
pc minwdw;
pas 162 85;pcenter;
rem form end;
)

minwdw_run=: 3 : 0
wd MINWDW
wd 'pshow;'
)

minwdw_close=: 3 : 0
wd'pclose'
)

minwdw_run ''

Java

Library: Swing
   import javax.swing.JFrame;
   import javax.swing.WindowConstants;
   public class SimpleWindow {
   	public static void main(String[] args) {
   
   		JFrame window = new JFrame("This is a title!");
   		
   		window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   		window.setSize(800, 600);
   		window.setVisible(true);
   	}
   }

JavaScript

   window.open("webpage.html", "windowname", "width=800,height=600");

mIRC Scripting Language

Interpreter: mIRC

Switches: C = Center Window p = Picture Window d = Desktop Window

alias CreateMyWindow {
 .window -Cp +d @WindowName 600 480
}

Perl

Interpreter: Perl 5.8.8

Library: Tk
 use Tk;
 
 MainWindow->new();
 MainLoop;
Library: SDL
 use SDL::App;
 use SDL::Event;
 
 $app = SDL::App->new;
 $app->loop({
   SDL_QUIT() => sub { exit 0; },
 });
Library: Gtk
 use Gtk '-init';
 
 $window = Gtk::Window->new;
 $window->signal_connect(
   'destroy' => sub { Gtk->main_quit; }
 );
 $window->show_all;
 Gtk->main;
Library: Gtk2
 use Gtk2 '-init';
 
 $window = Gtk2::Window->new;
 $window->signal_connect(
   'destroy' => sub { Gtk2->main_quit; }
 );
 $window->show_all;
 Gtk2->main;

Python

Interpreter: Python 2.4, 2.5

Library: Tkinter
 import Tkinter
 
 w = Tkinter.Tk()
 w.mainloop()
Library: wxPython
 from wxPython.wx import *
 
 class MyApp(wxApp):
   def OnInit(self):
     frame = wxFrame(NULL, -1, "Hello from wxPython")
     frame.Show(true)
     self.SetTopWindow(frame)
     return true
 
 app = MyApp(0)
 app.MainLoop()
Library: Pythonwin
 import win32ui
 from pywin.mfc.dialog import Dialog
 
 d = Dialog(win32ui.IDD_SIMPLE_INPUT)
 d.CreateWindow()
Library: PyGTK
 import gtk
 
 window = gtk.Window()
 window.show()
 gtk.main()

RapidQ

  create form as qform
     center
     width=500
     height=400
  end create
  form.showModal

Ruby

Interpreter: Ruby 1.8.5

Library: Tk
require 'tk'

window = TkRoot::new()
window::mainloop()
Library: GTK+
require 'gtk2'

window = Gtk::Window.new.show
Gtk.main

Tcl

Loading the "Tk" package is all that is required to get an initial window:

  package require Tk

If you need an additional window:

  toplevel .top

If you are using the increasingly popular tclkit under MS Windows, all you have to do is associate the tcklkit with the extension ".tcl" and then create an empty file with the name (e.g.) nothing.tcl. Douple-clicking that will "open a window" (an empty one).

Toka

Library: SDL

Toka does not inherently know about graphical environments, but can interact with them using external libraries. This example makes use of the SDL library bindings included with Toka.

needs sdl
800 600 sdl_setup

Visual Basic .NET

   Dim newForm as new Form
   newForm.Text = "It's a new window"
  
       newForm.Show()