How to resolve the algorithm Window creation step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Window creation step by step in the Nim programming language

Table of Contents

Problem Statement

Display a GUI window. The window need not have any contents, but should respond to requests to be closed.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Window creation step by step in the Nim programming language

Source code in the nim programming language

import gintro/[glib, gobject, gtk, gio]

proc activate(app: Application) =
  ## Activate the application.
  let window = newApplicationWindow(app)
  window.setTitle("Window for Rosetta")
  window.setSizeRequest(640, 480)
  window.showAll()

let app = newApplication(Application, "Rosetta.Window")
discard app.connect("activate", activate)
discard app.run()


import gdk2, glib2, gtk2

const
  Inside = "Mouse is over label"
  OutSide = "Mouse is not over label"

# Context transmitted to callback.
type Context = object
  label: PLabel
  overButton: bool


proc changeLabel(p: PWidget; event: gdk2.PEventCrossing; context: var Context) {.cdecl.} =
  context.label.set_text(if context.overButton: OutSide else: Inside)
  context.overButton = not context.overButton

proc thisDestroy(widget: PWidget, data: Pgpointer)  {.cdecl.} =
  main_quit()


var context: Context
nim_init()

let window = window_new(gtk2.WINDOW_TOPLEVEL)
let stackbox = vbox_new(true, 10)
let button1 = button_new("Move mouse over button")
let buttonstyle = copy(button1.get_style())
buttonstyle.bg[STATE_PRELIGHT] = TColor(pixel: 0, red: 255, green: 0, blue: 0)
button1.set_style(buttonstyle)
let button2 = button_new()
context = Context(label: label_new(Outside), overButton: false)
let button3 = button_new("Quit")

button2.add(context.label)
stackbox.pack_start(button1, true, true, 0)
stackbox.pack_start(button2, true, true, 0)
stackbox.pack_start(button3, true, true, 0)
window.set_border_width(5)
window.add(stackbox)

discard window.signal_connect("destroy", SIGNAL_FUNC(thisDestroy), nil)
discard button1.signal_connect("enter_notify_event", SIGNAL_FUNC(changeLabel), addr(context))
discard button1.signal_connect("leave_notify_event", SIGNAL_FUNC(changeLabel), addr(context))
discard button3.signal_connect("clicked", SIGNAL_FUNC(thisDestroy), nil)

window.show_all()
main()


import
  sdl, sdl_image, colors

var
  screen, greeting: PSurface
  r: TRect
  event: TEvent
  bgColor = colChocolate.int32

if init(INIT_VIDEO) != 0:
  quit "SDL failed to initialize!"

screen = SetVideoMode(640, 480, 16, SWSURFACE or ANYFORMAT)
if screen.isNil:
  quit($sdl.getError())

greeting = IMG_load("tux.png")
if greeting.isNil:
  echo "Failed to load tux.png"
else:
  ## convert the image to alpha and free the old one
  var s = greeting.displayFormatAlpha()
  swap(greeting, s)
  s.freeSurface()

r.x = 0
r.y = 0

block game_loop:
  while true:
    
    while pollEvent(addr event) > 0:
      case event.kind
      of QUITEV:
        break game_loop
      of KEYDOWN:
        if EvKeyboard(addr event).keysym.sym == K_ESCAPE:
          break game_loop
      else:
        discard
    
    discard fillRect(screen, nil, bgColor)
    discard blitSurface(greeting, nil, screen, addr r)
    discard flip(screen)

greeting.freeSurface()
screen.freeSurface()
sdl.Quit()


import x11/[xlib, xutil, x]

const
  WINDOW_WIDTH = 400
  WINDOW_HEIGHT = 300

type WindowData = tuple[display: PDisplay; window: Window]

proc createWindow: WindowData =
  let width: cuint = WINDOW_WIDTH
  let height: cuint = WINDOW_HEIGHT
  var sizeHints: XSizeHints

  let display = XOpenDisplay(nil)
  if display == nil:
    echo "Connection to X server failed."
    quit QuitFailure

  let screen = XDefaultScreen(display)
  var rootwin = XRootWindow(display, screen)
  let win = XCreateSimpleWindow(display, rootwin, 100, 10, width, height, 5,
                                XBlackPixel(display, screen), XWhitePixel(display, screen))
  sizeHints.flags = PSize or PMinSize or PMaxSize
  sizeHints.min_width = width.cint
  sizeHints.max_width = width.cint
  sizeHints.min_height = height.cint
  sizeHints.max_height = height.cint
  discard XSetStandardProperties(
          display, win, "Simple Window", "window", 0, nil, 0, addr(sizeHints))
  discard XSelectInput(display, win, ButtonPressMask or KeyPressMask or PointerMotionMask)
  discard XMapWindow(display, win)
  result = (display, win)

proc closeWindow(data: WindowData) =
  discard XDestroyWindow(data.display, data.window)
  discard XCloseDisplay(data.display)

proc processEvent(xev: var XEvent) =
  var key: KeySym
  case xev.theType.int
  of KeyPress:
    key = XLookupKeysym(cast[ptr XKeyEvent](addr(xev)), 0)
    if key.int != 0:
      echo "keyboard event ", key.int
    if key.int == 65307:    # <Esc>
      quit QuitSuccess
  of ButtonPressMask, PointerMotionMask:
    echo "Mouse event"
  else:
    discard

proc eventloop(data: WindowData) =
  var xev: XEvent
  discard XFlush(data.display)
  var numEvents = XPending(data.display).int
  while numEvents != 0:
    dec numEvents
    discard XNextEvent(data.display, addr(xev))
    processEvent(xev)

let windata = createWindow()
while true:
  eventloop(windata)
windata.closeWindow()


import glut

var win: int = 0

proc myOnKeyPress(c: int8, v1, v2: cint) {.cdecl.} =
   echo(c)
   if c == 27:
      glutDestroyWindow(win) 

glutInit()
win = glutCreateWindow("Goodbye, World!")
glutKeyboardFunc(TGlut1Char2IntCallback(myOnKeyPress))
glutMainLoop()


# test a Windows GUI application

import
  windows, shellapi, nb30, mmsystem, shfolder

#proc MessageBox(hWnd: int, lpText, lpCaption: CString, uType: uint): int
# {stdcall, import: "MessageBox", header: "<windows.h>"}

discard MessageBox(0, "Hello World!", "Nim GUI Application", 0)


import iup

# assumes you have the iup  .dll or .so installed

discard iup.open(nil,nil)


# now use a Dialog box to show a message
var lbl = label("Hello World")
setAttribute(lbl,"PADDING","10x10")

var contents = hbox(lbl, nil)
#SetAttribute(contents, "MARGIN", "5x5")

var dlg = dialog(contents)
#SetAttribute(dlg, "SIZE", "100x50")

discard dlg.show()

# a window via a quick message box, sitting on top of the main dialog window
discard Alarm("MyTitle","Hello World","Ok", "Not Ok", nil)

discard mainloop()
iup.close()


import wx

{.experimental.}

const
    TITLE       = "Rosetta Code - Window Creation Nim"
    WIDTH       = 300
    HEIGHT      = 300

let
    POSITION    = construct_wxPoint(100,100)
    SIZE        = construct_wxSize(WIDTH,HEIGHT)

let window = cnew construct_wxFrame(nil, wxID_ANY, TITLE, POSITION, SIZE)

window.show(true)

run_main_loop()


  

You may also check:How to resolve the algorithm Hello world/Text step by step in the VTL-2 programming language
You may also check:How to resolve the algorithm Super-d numbers step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Percolation/Mean run density step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Leap year step by step in the Joy programming language
You may also check:How to resolve the algorithm Call an object method step by step in the Julia programming language