User Interface Programming In X Windows and Microsoft Windows


User Interface Programming In X Windows and Microsoft Windows

The basics of getting a Microsoft Windows-based application and an X/Motif-based application started are very similar conceptually. Also, the libraries and core functions are similar in both environments. This section discusses the programming principles for developing user interfaces in the two environments.

Programming Principles

The basic structure of an X Windows-based application that uses Motif is quite similar to the structure of a Microsoft Windows-based application.

To initiate an X Windows-based interface

  1. Initialize the toolkit.

  2. Create widgets.

  3. Manage widgets.

  4. Set up callbacks.

  5. Display widgets.

  6. Enter the main program event handler.

The following code illustrates these steps:

 topWidget = XtVaAppInitialize(); frame = XtVaCreateManagedwidget("frame",xmFrameWidgetFrams, topWidget,,,); button = XmCreatePushButton(frame, "EXIT", NULL, 0); XtManageChild(button) XtAddCallback(button, XmNactivateCallback, myCallback, NULL); XtRealizeWidget(topWidget); XtAppMainLoop(); 

To initiate a Microsoft Windows-based application

  1. Initialize an instance and register the Window class.

  2. Set up callbacks.

  3. Create the window.

  4. Display the window.

  5. Enter the main program message loop.

The following code illustrates these steps:

 windowClassStruct.hInstance = thisApplicationInstance; windowClassStruct.lpfnWndProc = (WNDPROC)myCallback; RegisterClass(&windowClassStruct); myWindow = CreateWindow(); ShowWindow(myWindow); while(GetMessage(,,,)) {} 

To deal with input devices, X clients and Win32-based applications rely on events and messages from the outside. X and Win32 use a similar method for this, a message loop where a callback function or inline code executes based on the nature of the event or message.

The following code shows a simple Win32 message loop.

 while (GetMessage(&  msg  , NULL, 0, 0)) {     TranslateMessage(&  msg  );     DispatchMessage(&  msg  ); } 

The GetMessage() API returns an MSG structure (& msg ). Right now, only the message member of this structure ( UINT message; in the following listing) is of interest. Windows places the message identifier in this field. The developer can use this in the message loop to capture device events.

 Typedef struct tagMSG {     HWND  hwnd;     UINT  message;     WPARAM  wParam;     LPARAM  lParam;     DWORD  time;     POINT  py; } MSG, *PMSG; 

Libraries and Include Files

Despite differences in their underlying architectures, many of the graphical functions used in X Windows and Microsoft Windows perform similar tasks . These include the core libraries and the Motif and Win32 common dialog boxes.

Core Libraries

A number of functions exist to support the core API used in a graphical user interface. X Windows includes the libraries X and Xlib and the X Windows Intrinsics toolkit. The Win32 equivalent is Windows.h, which includes a great number of additional header files.

The Microsoft compiler includes Win32 USER32.LIB and GDI32.LIB import libraries, which can be roughly compared to X Library and X Toolkit Intrinsics because they provide nearly all of the basic window management and 2D Graphics APIs.

These Win32 libraries are called import libraries because they provide information to the linker. When a Win32-based program references the CreateWindow() function, User32.lib tells the linker that this function is in the User32.dll dynamic-link library. This information goes into the .exe file, which enables Windows to perform dynamic linking by using the User32.dll and Gdi32.dll dynamic-link libraries when the program is executed.

Motif and Win32 Common Dialog Boxes

Dialog box functionality is provided by Motif and by the Windows common dialog box library. If the code migrates from Motif, there is probably an equivalent Win32 common dialog box for each Motif function.

Win32 provides a set of functions to create commonly used windows. If Commdlg.h is included in a project, the project has access to the Win32 common dialog box functions. The Comdlg32.dll library stores templates for these dialog boxes, along with the code to drive them. By using these, a developer can save time and provide a consistent look and feel in the application being migrated .

For example, the Motif function XmCreateFileSelectionDialog() is very similar to the Win32 function GetOpenFileName() . The X/Motif code must include the Xm/FileSB.h header file. The Win32-based application must include Commdlg.h and must link to Comdlg32.lib. Calling the GetOpenFileName() API to displays the Open File dialog box, as shown in Figure 11.5.

click to expand
Figure 11.5: The GetOpenFileName common dialog box

Table 11.2 lists the available common dialog box functions.

Table 11.2: Common Dialog Box Functions

Function

Description

ChooseColor()

Creates a Color dialog box that enables the user to select a color .

ChooseFont()

Creates a Font dialog box that enables the user to choose attributes for a logical font.

CommDlgExtendedError()

Returns a common dialog box error code.

FindText()

Creates a system-defined modeless Find dialog box that lets the user specify a string to search for and specify options to use when searching for text in a document.

GetFileTitle()

Retrieves the name of the specified file.

GetOpenFileName()

Creates an Open dialog box that lets the user specify the drive, the directory, and the name of a file or set of files to open.

GetSaveFileName()

Creates a Save dialog box that lets the user specify the drive, the directory, and the name of a file to save.

PageSetupDlg()

Creates a Page Setup dialog box that enables the user to specify the attributes of a printed page.

PrintDlg()

Displays a Print dialog box.

PrintDlgEx()

Displays a Print property sheet that enables the user to specify the properties of a particular print job.

ReplaceText()

Creates a system-defined modeless dialog box that lets the user specify a string to search for and a replacement string, as well as options to control the find and replace operations.

In addition to Windows.h, the Windowsx.h library has roots in Windows version 3.1 and provides many useful macros that can be used in code migration. These macros are not used as often or in the same ways now, but they can be helpful. For example, using the SelectPen() and DeletePen() macros can be more intuitive than calling SelectObject() and DeleteObject() with all the required type specifications:

 #define SelectPen(hdc, hpen)((HPEN)SelectObject((hdc), (HGDIOBJ)(HPEN)(hpen))) #define DeletePen(hpen) DeleteObject((HGDIOBJ)(HPEN)(hpen)) 

Writing X Windows APIs for Win32

It might be easier to migrate by creating X Windows-like APIs in the Win32 environment. Creating these APIs can make the original X Windows code easier to migrate to native Win32 and perhaps make the code more portable. The following examples demonstrate this approach.

The following are examples of macro-based APIs:

 #define DefaultRootWindow(display) GetDesktopWindow() #define RootWindow(display,screen) GetDesktopWindow() #define DefaultScreen(display)   ((int)(0)) #define DefaultGC(display,screen)  GetDC(display) 

The following code demonstrates the use of DisplayWidth and DisplayHeight :

 #define DisplayWidth(d,s)  ((unsigned int)GetSystemMetrics(SM_CXSCREEN)) #define DisplayHeight(d,s) ((unsigned int)GetSystemMetrics(SM_CYSCREEN)) unsigned int maxWIDE; unsigned int maxHIGH; maxWIDE = DisplayWidth(x,x); maxHIGH = DisplayHeight(x,x); 

The following code demonstrates the use of XtMessage :

 //+ //  XtMessage(TCHAR *message) // //  This function requires the following on Win32: //    1. A global variable HANDLE stdOut; //    2. The following initialization code in WinProc() //  //         switch(message) { //         case WM_CREATE : //          AllocConsole(); //          stdOut = GetStdHandle(STD_OUTPUT_HANDLE); //           (. . .) // // //    3. The following cleanup code in WinProc() // //         switch(message) { //         case WM_CLOSE : //          FreeConsole(); //        (. . .) //- 

The following code demonstrates the use of XtWarning :

 void XtWarning(TCHAR *message) {   DWORD charsWritten;   WriteConsole(stdOut, message, _tcsclen(message) , &charsWritten, NULL); } 



UNIX Application Migration Guide
Unix Application Migration Guide (Patterns & Practices)
ISBN: 0735618380
EAN: 2147483647
Year: 2003
Pages: 134

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net