Window creation: Difference between revisions
(→{{header|Common Lisp}}: add CLIM) |
(→TI-89 BASIC: new example) |
||
Line 529: | Line 529: | ||
<lang tcl>toplevel .top</lang> |
<lang tcl>toplevel .top</lang> |
||
If you are using the increasingly popular [http://www.equi4.com/tclkit.html tclkit] under MS Windows, all you have to do is associate the tclkit with the extension “<tt>.tcl</tt>” and then create an <i>empty</i> file with, e.g., with the name <tt>nothing.tcl</tt>. Double-clicking that will “open a window” (an empty one). |
If you are using the increasingly popular [http://www.equi4.com/tclkit.html tclkit] under MS Windows, all you have to do is associate the tclkit with the extension “<tt>.tcl</tt>” and then create an <i>empty</i> file with, e.g., with the name <tt>nothing.tcl</tt>. Double-clicking that will “open a window” (an empty one). |
||
=={{header|TI-89 BASIC}}== |
|||
<pre style="font-family:'TI Uni'">Dialog |
|||
Title "Rosetta Code" |
|||
Text "" |
|||
EndDlog</pre> |
|||
=={{header|Toka}}== |
=={{header|Toka}}== |
Revision as of 12:49, 14 August 2009
You are encouraged to solve this task according to the task description, using any language you may know.
Ada
<lang ada> with Gtk.Window; use Gtk.Window; with Gtk.Widget; use Gtk.Widget;
with Gtk.Handlers; with Gtk.Main;
procedure Windowed_Application is
Window : Gtk_Window;
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); Return_Handlers.Connect ( Window, "delete_event", Return_Handlers.To_Marshaller (Delete_Event'Access) ); Handlers.Connect ( Window, "destroy", Handlers.To_Marshaller (Destroy'Access) ); Show (Window);
Gtk.Main.Main;
end Windowed_Application; </lang>
AutoHotkey
<lang AutoHotkey> Gui, Add, Text,, Hello Gui, Show</lang>
C
Compile Command: gcc `sdl-config --cflags` `sdl-config --libs` SDL_Window.c -o window <lang c> /*
* 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; }</lang>
Compile command: gcc `gtk-config --cflags` `gtk-config --libs` -o window window.c
<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_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(gtk_main_quit), NULL); gtk_widget_show(window); gtk_main(); return 0; }</lang>
Compile command: gcc -Wall -pedantic `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0` -o window window.c
<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); g_signal_connect (window, "destroy", G_CALLBACK(gtk_main_quit), NULL); gtk_widget_show(window); gtk_main(); return 0; }</lang>
C++
Compiler command: qmake -pro; qmake
<lang cpp> #include <QApplication>
#include <QMainWindow> int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow window; window.show(); return app.exec(); }</lang>
Compiler command: g++ filename.cc -o test `pkg-config --cflags --libs gtkmm-2.4`
<lang cpp> #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 ) ; }</lang>
C#
Winforms
<lang csharp>using System; using System.Windows.Forms;
public class Window {
[STAThread] static void Main() { Form form = new Form(); form.Text = "Window"; form.Disposed += delegate { Application.Exit(); };
form.Show(); Application.Run(); }
}</lang>
Common Lisp
<lang lisp>(capi:display (make-instance 'capi:interface :title "A Window"))</lang>
Setting up the environment:
<lang lisp>(require :mcclim) (cl:defpackage #:rc-window
(:use #:clim-lisp #:clim))
(cl:in-package #:rc-window)</lang>
The actual definition and display:
<lang lisp>(define-application-frame rc-window ()
() (:layouts (:default)))
(run-frame-top-level (make-application-frame 'rc-window))</lang lisp>
Note: This creates a small, useless window ("frame"). Useful frames will have some panes defined inside them.
D
<lang d> module Window;
import fltk4d.all; void main() { auto window = new Window(300, 300, "A window"); window.show; FLTK.run; }</lang>
<lang d> 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; }</lang>
QD is a simple and easy-to-use wrapper around SDL. <lang d> import qd;
void main() { screen(640, 480); while (true) events(); }</lang>
E
Swing
when (currentVat.morphInto("awt")) -> { def w := <swing:makeJFrame>("Window") w.setContentPane(<swing:makeJLabel>("Contents")) w.pack() w.show() }
Emacs Lisp
<lang lisp> (make-frame) </lang>
F#
Everything is provided by the .NET runtime so this is almost identical to C_sharp.
#light open System open System.Drawing open System.Windows.Forms [<EntryPoint>][<STAThread>] let main args = let form = new Form(Text = "F# Window", Size = Size(320, 160)) Application.Run(form) 0
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
A minimalist modal dialog:
title=:'"Hamlet -- Act 3, Scene 1"' text=:'"To be, or not to be: that is the question:"' wd 'mb ',title,text
A free-standing window:
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
<lang java>import javax.swing.JFrame;
public class Main {
public static void main(String[] args) throws Exception { JFrame w = new JFrame("Title"); w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); w.setSize(800,600); w.setVisible(true); }
}</lang>
JavaScript
window.open("webpage.html", "windowname", "width=800,height=600");
mIRC Scripting Language
Switches: C = Center Window p = Picture Window d = Desktop Window
alias CreateMyWindow { .window -Cp +d @WindowName 600 480 }
Objective-C
It opens a 800×600 window, centered on the screen, with title "A Window".
<lang objc>#include <Foundation/Foundation.h>
- include <AppKit/AppKit.h>
@interface Win : NSWindow { } - (void)applicationDidFinishLaunching: (NSNotification *)notification; - (BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSNotification *)notification; @end
@implementation Win : NSWindow
-(id) init
{
[self initWithContentRect: NSMakeRect(0, 0, 800, 600) styleMask: (NSTitledWindowMask | NSClosableWindowMask) backing: NSBackingStoreBuffered defer: NO];
[self setTitle: @"A Window"]; [self center];
return self;
}
-(void) dealloc
{
[super dealloc];
}
- (void)applicationDidFinishLaunching: (NSNotification *)notification {
[self orderFront: self];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSNotification *)notification {
return YES;
} @end
int main() {
Win *mywin; NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init]; [NSApplication sharedApplication]; mywin = [[Win alloc] init]; [NSApp setDelegate: mywin]; [NSApp runModalForWindow: mywin]; return EXIT_SUCCESS;
}</lang>
Perl
<lang perl> use Tk;
MainWindow->new(); MainLoop;</lang>
<lang perl> use SDL::App;
use SDL::Event; $app = SDL::App->new; $app->loop({ SDL_QUIT() => sub { exit 0; }, });</lang>
<lang perl> use Gtk '-init';
$window = Gtk::Window->new; $window->signal_connect( 'destroy' => sub { Gtk->main_quit; } ); $window->show_all; Gtk->main;</lang>
<lang perl> use Gtk2 '-init';
$window = Gtk2::Window->new; $window->signal_connect( 'destroy' => sub { Gtk2->main_quit; } ); $window->show_all; Gtk2->main;</lang>
Python
<lang python> import Tkinter
w = Tkinter.Tk() w.mainloop()</lang>
<lang python> 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()</lang>
<lang python> import win32ui
from pywin.mfc.dialog import Dialog d = Dialog(win32ui.IDD_SIMPLE_INPUT) d.CreateWindow()</lang>
<lang python> import gtk
window = gtk.Window() window.show() gtk.main()</lang>
RapidQ
create form as qform center width=500 height=400 end create form.showModal
Ruby
<lang ruby> require 'tk'
window = TkRoot::new() window::mainloop()</lang>
<lang ruby> require 'gtk2'
window = Gtk::Window.new.show Gtk.main</lang>
Tcl
Loading the Tk package is all that is required to get an initial window: <lang tcl>package require Tk</lang> If you need an additional window: <lang tcl>toplevel .top</lang> If you are using the increasingly popular tclkit under MS Windows, all you have to do is associate the tclkit with the extension “.tcl” and then create an empty file with, e.g., with the name nothing.tcl. Double-clicking that will “open a window” (an empty one).
TI-89 BASIC
Dialog Title "Rosetta Code" Text "" EndDlog
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.
needs sdl 800 600 sdl_setup
Vedit macro language
Creates an empty window with ID 'A' near the upper left corner of document area, with height of 20 text lines and width of 80 characters. <lang vedit>Win_Create(A, 2, 5, 20, 80)</lang> Note: if you run this command while in Visual Mode, you should adjust your active window smaller so that the created window will not be hidden behind it (since the active window is always on top).
Visual Basic .NET
<lang vb> Dim newForm as new Form
newForm.Text = "It's a new window" newForm.Show()</lang>
- Less Than 20 Examples
- Programming Tasks
- GUI
- Ada
- GTK
- GtkAda
- AutoHotkey
- C
- SDL
- Gtk2
- C++
- Qt
- C sharp
- Common Lisp
- CAPI
- CLIM
- D
- FLTK4d
- Derelict
- QD
- E
- Emacs Lisp
- F Sharp
- Groovy
- IDL
- J
- Java
- Swing
- JavaScript
- MIRC Scripting Language
- Objective-C
- Perl
- Tk
- Python
- Tkinter
- WxPython
- Pythonwin
- PyGTK
- RapidQ
- Ruby
- Ruby/Tk
- Tcl
- TI-89 BASIC
- Toka
- Vedit macro language
- Visual Basic .NET