Change Window Title & Centre Window

Previous topic - Next topic

MrTAToad

This set of routines are for changing a windows title and for centring a window.  I couldn't use Kitty's code for changing the title, hence this code. 
You can also get a list of screen modes, although for Linux & Mac, the full but unknown list is returned.

Code (glbasic) Select
// --------------------------------- //
// Project: Spots
// Start: Sunday, October 26, 2008
// IDE Version: 6.051
@FUNCTION dummy:
ENDFUNCTION

TYPE tScreenResolutions
width%
height%
ENDTYPE

GLOBAL screenResolutions[] AS tScreenResolutions
INLINE
}
#ifndef win32
#undef main
#endif

typedef struct SDL_Rect {
short x, y;
unsigned short w, h;
} SDL_Rect;

typedef struct{
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char unused;
} SDL_Color;

typedef struct{
int ncolors;
    SDL_Color *colors;
} SDL_Palette;

typedef struct SDL_PixelFormat {
    SDL_Palette *palette;
    unsigned char  BitsPerPixel;
    unsigned char BytesPerPixel;
    unsigned char  Rloss, Gloss, Bloss, Aloss;
    unsigned char Rshift, Gshift, Bshift, Ashift;
    unsigned int  Rmask, Gmask, Bmask, Amask;
    unsigned int colorkey;
    unsigned char alpha;
    } SDL_PixelFormat;

#define SDL_SWSURFACE 0x00000000 /* Surface is in system memory */
#define SDL_HWSURFACE 0x00000001 /* Surface is in video memory */
#define SDL_ASYNCBLIT 0x00000004 /* Use asynchronous blits if possible */
/* Available for SDL_SetVideoMode() */
#define SDL_ANYFORMAT 0x10000000 /* Allow any video depth/pixel-format */
#define SDL_HWPALETTE 0x20000000 /* Surface has exclusive palette */
#define SDL_DOUBLEBUF 0x40000000 /* Set up double-buffered video mode */
#define SDL_FULLSCREEN 0x80000000 /* Surface is a full screen display */
#define SDL_OPENGL      0x00000002      /* Create an OpenGL rendering context */
#define SDL_OPENGLBLIT 0x0000000A /* Create an OpenGL rendering context and use it for blitting */
#define SDL_RESIZABLE 0x00000010 /* This video mode may be resized */
#define SDL_NOFRAME 0x00000020 /* No window caption or edge frame */
/* Used internally (read-only) */
#define SDL_HWACCEL 0x00000100 /* Blit uses hardware acceleration */
#define SDL_SRCCOLORKEY 0x00001000 /* Blit uses a source color key */
#define SDL_RLEACCELOK 0x00002000 /* Private flag */
#define SDL_RLEACCEL 0x00004000 /* Surface is RLE encoded */
#define SDL_SRCALPHA 0x00010000 /* Blit uses source alpha blending */
#define SDL_PREALLOC 0x01000000 /* Surface uses preallocated memory */

#ifdef WIN32
typedef struct __RECT {
long left;
long top;
long right;
long bottom;
} __RECT;

#define SM_CXSCREEN 0
#define SM_CYSCREEN 1
#define HWND_BOTTOM ((HWND)1)
#define HWND_NOTOPMOST ((HWND)(-2))
#define HWND_TOP ((HWND)0)
#define HWND_TOPMOST ((HWND)(-1))
#define HWND_DESKTOP (HWND)0
#define HWND_MESSAGE ((HWND)(-3))

#define SWP_DRAWFRAME 0x0020
#define SWP_FRAMECHANGED 0x0020
#define SWP_HIDEWINDOW 0x0080
#define SWP_NOACTIVATE 0x0010
#define SWP_NOCOPYBITS 0x0100
#define SWP_NOMOVE 0x0002
#define SWP_NOSIZE 0x0001
#define SWP_NOREDRAW 0x0008
#define SWP_NOZORDER 0x0004
#define SWP_SHOWWINDOW 0x0040
#define SWP_NOOWNERZORDER 0x0200
#define SWP_NOREPOSITION SWP_NOOWNERZORDER
#define SWP_NOSENDCHANGING 0x0400
#define SWP_DEFERERASE 0x2000
#define SWP_ASYNCWINDOWPOS 0x4000

extern "C" __stdcall int GetSystemMetrics(int);
extern "C" __stdcall int GetWindowRect(HWND hWnd,struct __RECT *lpRect);
extern "C" __stdcall int SetWindowTextA(HWND hWnd,const char *lpString);
extern "C" __stdcall HWND GetDesktopWindow(void);
extern "C" __stdcall int SetWindowPos(HWND hWnd,HWND hWndInsertAfter,int X,int Y,int cx,int cy,int uFlags);
extern "C" __stdcall int EnumDisplaySettingsA(const char*, unsigned int, void*);
#endif

#if defined(LINUX) || defined(MACOSX)
extern "C" __stdcall void SDL_WM_SetCaption(const char *title, const char *icon);
extern "C" __stdcall SDL_Rect **SDL_ListModes(SDL_PixelFormat *format, int flags);
#endif

class availScreenModes {
public:
int width;
int height;
};

DGArray<availScreenModes> modeRes;

bool isValidSize(int minX,int minY,int x,int y)
{
int loop;
bool foundX;
bool foundY;
bool found;
availScreenModes use;

foundX=false;
foundY=false;

if ((minX<=0) || (minX>0 && x>=minX)) foundX=true;
if ((minY<=0) || (minY>0 && y>=minY)) foundY=true;

if (foundX && foundY)
{
found=true;
if (BOUNDS(modeRes,0)>0)
{
for (loop=0; loop<BOUNDS(modeRes,0); loop++)
{
use=modeRes(loop);
if (use.width==x && use.height==y)
{
found=false;
break;
}
}
}

if (found==true)
{
use.width=x;
use.height=y;
DIMPUSH(modeRes,use);
return true;
}
}

return false;
}
namespace __GLBASIC__{
ENDINLINE

FUNCTION ChangeWindowText: t$
INLINE
#ifdef WIN32
::SetWindowTextA((HWND) GLBASIC_HWND(),t_Str.c_str());
#else
::SDL_WM_SetCaption(t_Str.c_str(), t_Str.c_str());
#endif
ENDINLINE
ENDFUNCTION

FUNCTION CentreWindow:
INLINE
#ifdef WIN32
struct __RECT desk;
struct __RECT window;

::GetWindowRect(::GetDesktopWindow(),&desk);
::GetWindowRect((HWND) GLBASIC_HWND(),&window);
::SetWindowPos((HWND) GLBASIC_HWND(),HWND_NOTOPMOST,(desk.right-(window.right-window.left)) / 2, (desk.bottom-(window.bottom-window.top)) / 2, 0, 0, SWP_NOSIZE);
#endif
ENDINLINE
ENDFUNCTION

FUNCTION ScreenModes:minX%,minY%,displayName$
LOCAL loop%
LOCAL count%
LOCAL scnRes AS tScreenResolutions

count%=0

INLINE
availScreenModes use;
bool foundX;
bool foundY;

DIM(modeRes,0);

#ifdef WIN32
struct DEVMODE {
          char dmDeviceName[32];
          short dmSpecVersion;
          short dmDriverVersion;
          short dmSize;
          short dmDriverExtra;
          long  dmFields;
          short dmOrientation;
          short dmPaperSize;
          short dmPaperLength;
          short dmPaperWidth;
          short dmScale;
          short dmCopies;
          short dmDefaultSource;
          short dmPrintQuality;
          short dmColor;
          short dmDuplex;
          short dmYResolution;
          short dmTTOption;
          short dmCollate;
          char dmFormName[32];
          short dmUnusedPadding;
          long dmBitsPerPel;
          long dmPelsWidth;
          long dmPelsHeight;
          long dmDisplayFlags;
          long dmDisplayFrequency;
    };
DEVMODE devmode;
int resLoop;

    devmode.dmSize = sizeof(DEVMODE);
 
    // if disp$ = "", pass NULL instead!
    resLoop=0;
    while (EnumDisplaySettingsA(LEN(displayName_Str) ? displayName_Str.c_str() : NULL, resLoop, &devmode))
    {
    isValidSize(minX,minY,devmode.dmPelsWidth,devmode.dmPelsHeight);
resLoop++;
    }
#else
struct SDL_Rect **modes;
int i;

modes=SDL_ListModes(NULL,SDL_SWSURFACE|SDL_HWSURFACE);
if (modes==NULL)
{
}
else
if (modes==(SDL_Rect **) -1)
{
// All the modes are availiable - but we're not told what they are!
// Stupid, eh ?
isValidSize(minX,minY,640,480);
isValidSize(minX,minY,800,600);
isValidSize(minX,minY,1024,768);
}
else
{
for (i=0; modes[i]; ++i)
{
isValidSize(minX,minY,modes[i]->w,modes[i]->h);
}
}
#endif

count=BOUNDS(modeRes,0);
ENDINLINE

DIM screenResolutions[0]
FOR loop%=0 TO count%-1
INLINE
use=modeRes(loop);
scnRes.width=use.width;
scnRes.height=use.height;
ENDINLINE

DIMPUSH screenResolutions[],scnRes
NEXT

SORTARRAY screenResolutions[],ADDRESSOF(resSort)
RETURN count%
ENDFUNCTION

FUNCTION resSort:a AS tScreenResolutions,b AS tScreenResolutions
IF b.width<a.width
RETURN 1
ELSE
IF b.width>a.width
RETURN -1
ELSE
IF b.height<a.height
RETURN 1
ELSE
IF b.height>a.height
RETURN -1
ENDIF
ENDIF
ENDIF
ENDIF

RETURN 0
ENDFUNCTION