You are on page 1of 31

X-Window Lab

INDEX

S No.
1. 2. 3. 4. 5. 6. 7.

Topics
Introduction of X-Window The X Server Program to create 200*200 size windows Using X-lib of server to write a program to create text box of given size Create menu bar and menu item To implement various drag and drop based GUI components in Visual Basic To implement a multiple windows application

Page No.
2 4

Date

Remarks

Faculty Sign.

10
11 14 18 21

Marudhar Engg. College

Page 1

X-Window Lab

X Window Overview
The X Window System was originally developed in the early 1980s, and encompassed from the beginning many of the windowing capabilities that we now take for granted. While in a number of ways X was (and still is) command-line oriented, the capability of moving away from the command line was inherent from the very beginning in the architecture of the system. The advent of the desktop graphical user interface (GUI) didnt require a major redesign of the X Window System. The X server still communicates with the X client via the X protocol over a network, and a window manager is still being used to manage the client application windows. The basics havent changed, just the frills. In 1993, MIT turned control of X over to the X Consortium to continue development. In 1997, the X Consortium transferred control of X over to another consortium, the Open Software Foundation (OSF), which in turn became The Open Group. Ownership and development of X remains with The Open Group today.

X Architecture Overview
The X Window System is based on the client/server model. The system is divided into two parts: The X display server: Provides display capabilities, manages the display hardware, and manages user input. X client applications: Application programs that perform specific tasks. Client/server technology as used by X may seem backwards at first glance. Generally, people think of servers as remote machines like file servers, news servers, or mail servers. We might choose to run a client on a remote machine for any number of reasons. Generally, however, the remote machine offers some feature unavailable on our local machine: a bigger or faster processor, a different architecture better suited to a particular task, application software not available locally, or maybe files server capabilities. X allows we to take advantage of these remote features and to see the results on our local display. One of the most significant features of X is that it was specifically designed to work across a

Marudhar Engg. College

Page 2

X-Window Lab

network. The client and the server communicate via the X Protocol, which can run locally or over a TCP/IP or DECnet (for historical reasons) network in a way that is transparent to the user. Our local Linux system will run an X display server and some local clients; other clients may be run across the network. Its all the same to we sitting at our display. In a sense, the server acts as intermediary between client application programs and the local display hardware and input devices (generally a keyboard and mouse or other pointing device). When we enter input using the keyboard or mouse, the server intercepts the input as a sequence of events and notifies the relevant client application. Likewise, clients make requests that are communicated to the hardware display by the server. For example, a client may request that a window be moved or that text be displayed in the window. X was designed to make the software independent of the hardware. Special graphics terminals, known as X terminals, were developed to run X. Nowadays, depending on the type of system were running Linux on, our X display is likely to be a standard PC or Macintosh monitor. As a windowing system, X allows us to work with multiple programs, each running in a separate window.

Marudhar Engg. College

Page 3

X-Window Lab

Figure: The software architecture of Xt Intrinsics(xlib)-based applications

The X Server
The X server is the program that manages the hardware interface to the display. The server accepts user input from devices such as the keyboard and the pointer and sends that input to the relevant client applications; the server also keeps track of client output and updates the display to reflect that output. Each physical display (which may be multiple screens) has only one server program running. User input and several other types of information pass from the server to a client in the form of events. An event is a packet of information that tells the client something it needs to act on, such as keyboard input. Actions such as moving the pointer or pressing a key cause input events to occur. The server intercepts each event and passes it on to the client. When a client program receives a meaningful event, it processes the event and then responds with a request to the server for some sort of action affecting the display. For example, the client may request that a window be resized to particular dimensions. The server responds to the client request updating the appropriate window on the display. However, its important to know that the window manager interprets this data. The window manager is responsible for the look and feel of the display and decides how the client window should be displayed.

The X client-server model and network transparency


X is based on a client-server model. An X server program runs on a computer with a graphical display and communicates with various client programs. The server accepts requests for graphical output (windows) and sends back user input (keyboard, mouse).

Marudhar Engg. College

Page 4

X-Window Lab

In X Window, the server runs on the user's computer, while the clients may run on a different machine. This is the reverse of the common configuration of clientserver systems, where the client runs on the user's computer and the server runs on a remote computer. This reversal often confuses new X users. The X Window terminology takes the perspective of the program, rather than the end-user or the hardware: the remote programs connect to the X server display running on the local machine, and thus act as clients; the local X display accepts incoming traffic, and thus acts as a server.

In this example, the X server takes input from a keyboard and mouse and displays to a screen. A web browser and a terminal emulator run on the user's workstation, and a system updater runs on a remote server but is controlled from the user's machine. Note that the remote application runs just as it would locally. The communication protocol between server and client runs networktransparently: the client and server may run on the same machine or on different ones, possibly with different architectures and operating systems. A client and server can communicate securely over the Internet by tunneling the connection over an encrypted connection.

The X Protocol, Xlib, and Toolkits

Marudhar Engg. College

Page 5

X-Window Lab

At the lowest level, the server and the client programs communicate using the X Protocol. In general, however, only programmers writing X server software need to use the X Protocol directly. For writing X clients, programmers use libraries, which are archives of commonly used functions. As an X user, we dont need to be concerned about libraries and toolkits; however, well see the terms and should know what they mean. Xlib is the lowest-level library for writing X client applications. Xlib defines an interface to the X Protocol, with a one-to-one correspondence between X protocol requests and Xlib library functions. Writing a program in Xlib is time-consuming and laborious; rather than having to go through that process, programmers use toolkits written on top of Xlib. A toolkit is a set of programming libraries and widgets for programming in X. A widget is best described as a graphical component, such as a menu, command button, dialog box, or scrollbar. Programmers use toolkits not only because its easier than writing from scratch,but also so their applications have a uniform look and feel with other applications.

Building an X Application in MS Visual Studio


To create and build our X application (i.e. to create an executable) in MS Visual Studio 6.0, we can use the following procedure: 1 Create an empty MS Visual C project: a) On the File menu of MS Visual Studio C++, click New and then click the Projects tab. b) In the main window of this tab, select either Win32 Application (for a GUI application) or Win32 Console Application (for a console application). c) Specify a location for the project. We can browse for a location or type one directly into the field. d) Specify a project name and click OK. e) In the resulting dialog box, choose to create an empty project and click Finish. Scan the resulting new project information to make sure it is correct and click OK. 2 Customize the project to work with the LabF XDK:

Marudhar Engg. College

Page 6

X-Window Lab

a) On the Project menu, click Settings and then click the C/C++ tab. In the upper left of the tab, from the Settings For drop-down list, select All Configurations. (All of the subsequent settings described in this procedure, regardless of the tab on which they are located, should be for All Configurations). b) Also on the C/C++ tab, from the Category drop-down list, select Code Generation. From the Use run-time library drop-down list, select Multithreaded DLL. c) Still on the C/C++ tab, from the Category drop-down list, select Preprocessor. In the Additional include directories field, type the LabF XDK include path (the location at which we have installed the LabF XDK include files). d) Click the Link tab of the Settings dialog box. From the Category drop-down list, select Input. In the Additional library path field, type the LabF XDK lib path (the location at which we have installed the LabF XDK lib files). e) When finished, click OK to save the settings and close the dialog box. 3 On the Project menu, click Add To Project and then Files. Add all source files to our project. 4 On the Project menu, click Settings and then click the Link tab. From the Category drop-down list, select Input. In the Object/library modules field, type the names of the necessary libraries for our X client. When finished, click OK to save the settings and close the dialog box. 5 On the Build menu, click Build ProjectName (where ProjectName is the name for our project that MS Visual Studio automatically inserts into the menu item). 6 On the Build menu, click Execute ProjectName (where ProjectName is the name of our project that MS Visual Studio automatically inserts into the menu item). NOTE :- To compile any of the examples on a UNIX system without using imake, use the following command line:

cc -O -o filename filename.c -lXm -lXt -lX11

Marudhar Engg. College

Page 7

X-Window Lab

A Simple X Toolkit Application


Some application code that uses the X Toolkit will go a long way to illustrate the basic concepts introduced above. Figure shows the actual window created by a minimal hello, world Toolkit application xhello, and Example shows the code for it. xhello simply displays the string hello in a window.

Figure :. xhello: appearance on screen Most window managers add a decorated border above or surrounding the window; this is not part of the application. Likewise, the placement of the widget on the screen depends on the window manager. Since no coordinates are specified as resources for the widget, most window managers will require the user to place it interactively. Some window managers can be configured to place new windows at an arbitrary default location. The Code Below Example shows the code for xhello.c. Example: xhello.c: a minimal "hello, world" application
/ * xhello.c - simple program to put up a banner on the display */ /* * Header file required for all Motif programs is <Xm/Xm.h>. * In other Xt-based widget sets: * #include <X11/Intrinsic.h> * #include <X11/StringDefs.h> */ #include <Xm/Xm.h> /* Standard Motif definitions */ /* * Public header file for widgets actually used in this file. */ #include <Xm/Label.h> /* Motif Label Widget */ main(argc, argv) int argc; char **argv; { XtAppContext app_context;

Marudhar Engg. College

Page 8

X-Window Lab

Widget topLevel, hello; /* Register the default language procedure */ XtSetLanguageProc(NULL, (XtLanguageProc)NULL, NULL); /* Initialize the Xt Intrinsics */ topLevel = XtVaAppInitialize( &app_context, /* Application context */ "XHello", /* Application class */ NULL, 0, /* command line option list */ &argc, argv, /* command line args */ NULL, /* for missing app-defaults file */ NULL); /* terminate varargs list */ /* Create a widget */ hello = XtVaCreateManagedWidget( "hello", /* arbitrary widget name */ xmLabelWidgetClass, /* widget class from Label.h */ topLevel, /* parent widget */ NULL); /* terminate varargs list */ /* * Create windows for widgets and map them. */ XtRealizeWidget(topLevel); /* * Loop for events. */ XtAppMainLoop(app_context);

Marudhar Engg. College

Page 9

X-Window Lab

***************************************************************** ***

Program to create 200*200 size windows


***************************************************************** *** #include <gtk/gtk.h> int main( int argc, char *argv[] ){ GtkWidget *window; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_widget_show (window); gtk_main (); return 0; } Output:

Marudhar Engg. College

Page 10

X-Window Lab

***************************************************************** ****

Using X-lib of server to write a program to create text box of given size
***************************************************************** **** #include <stdio.h> #include <stdlib.h> #include <gtk/gtk.h> static void enter_callback( GtkWidget *widget, GtkWidget *entry ) { const gchar *entry_text; entry_text = gtk_entry_get_text (GTK_ENTRY (entry)); printf ("Entry contents: %s\n", entry_text); } static void entry_toggle_editable( GtkWidget *checkbutton, GtkWidget *entry ) { gtk_editable_set_editable (GTK_EDITABLE (entry), GTK_TOGGLE_BUTTON (checkbutton)->active); } static void entry_toggle_visibility( GtkWidget *checkbutton, GtkWidget *entry ) { gtk_entry_set_visibility (GTK_ENTRY (entry), GTK_TOGGLE_BUTTON (checkbutton)>active);

Marudhar Engg. College

Page 11

X-Window Lab

} int main( int argc, char *argv[] ) { GtkWidget *window; GtkWidget *vbox, *hbox; GtkWidget *entry; GtkWidget *button; GtkWidget *check; gint tmp_pos; gtk_init (&argc, &argv); /* create a new window */ window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_widget_set_size_request (GTK_WIDGET (window), 200, 100); gtk_window_set_title (GTK_WINDOW (window), "GTK Entry"); g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL); g_signal_connect_swapped (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_widget_destroy), G_OBJECT (window)); vbox = gtk_vbox_new (FALSE, 0); gtk_container_add (GTK_CONTAINER (window), vbox); gtk_widget_show (vbox); entry = gtk_entry_new (); gtk_entry_set_max_length (GTK_ENTRY (entry), 50); g_signal_connect (G_OBJECT (entry), "activate", G_CALLBACK (enter_callback), (gpointer) entry); gtk_entry_set_text (GTK_ENTRY (entry), "hello"); tmp_pos = GTK_ENTRY (entry)->text_length; gtk_editable_insert_text (GTK_EDITABLE (entry), " world", -1, &tmp_pos); gtk_editable_select_region (GTK_EDITABLE (entry), 0, GTK_ENTRY (entry)->text_length); gtk_box_pack_start (GTK_BOX (vbox), entry, TRUE, TRUE, 0); gtk_widget_show (entry); hbox = gtk_hbox_new (FALSE, 0); gtk_container_add (GTK_CONTAINER (vbox), hbox); gtk_widget_show (hbox); check = gtk_check_button_new_with_label ("Editable"); gtk_box_pack_start (GTK_BOX (hbox), check, TRUE, TRUE, 0); g_signal_connect (G_OBJECT (check), "toggled", Marudhar Engg. College Page 12

X-Window Lab

G_CALLBACK (entry_toggle_editable), (gpointer) entry); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), TRUE); gtk_widget_show (check); check = gtk_check_button_new_with_label ("Visible"); gtk_box_pack_start (GTK_BOX (hbox), check, TRUE, TRUE, 0); g_signal_connect (G_OBJECT (check), "toggled", G_CALLBACK (entry_toggle_visibility), (gpointer) entry); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), TRUE); gtk_widget_show (check); button = gtk_button_new_from_stock (GTK_STOCK_CLOSE); g_signal_connect_swapped (G_OBJECT (button), "clicked", G_CALLBACK (gtk_widget_destroy), G_OBJECT (window)); gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0); GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT); gtk_widget_grab_default (button); gtk_widget_show (button); gtk_widget_show (window); gtk_main(); return 0; } Output:

Marudhar Engg. College

Page 13

X-Window Lab

***************************************************************** ***

Create menu bar and menu item


***************************************************************** *** #include <gtk/gtk.h> #include <glib.h> /*-- This function allows the program to exit properly when the window is closed --*/ gint destroyapp (GtkWidget *widget, gpointer gdata) { g_print ("Quitting...\n"); gtk_main_quit(); return (FALSE); } /*-- This function allows the program to exit properly when the window is closed --*/ gint ClosingAppWindow (GtkWidget *widget, gpointer gdata) { g_print ("Quitting...\n"); gtk_main_quit(); Marudhar Engg. College Page 14

X-Window Lab

return (FALSE); } int main (int argc, char *argv[]) { /*-- Declare the GTK Widgets used in the program --*/ GtkWidget *window; GtkWidget *menuFile; GtkWidget *menuEdit; GtkWidget *menuHelp; GtkWidget *menubar; GtkWidget *menu; GtkWidget *menuitem; GtkWidget *vbox; GtkWidget *text; gchar *buffer = "Go to the file menu and select Exit to close the application."; /*-- Initialize GTK --*/ gtk_init (&argc, &argv); /*-- Create the new window --*/ window = gtk_window_new(GTK_WINDOW_TOPLEVEL); /*-- Create the vbox --*/ vbox = gtk_vbox_new(FALSE, 0); /*-- Create a text area --*/ text = gtk_text_new(NULL, NULL); /*-- Set text area to be editable --*/ gtk_text_set_editable(GTK_TEXT (text), TRUE); /*-- Connect the window to the destroyapp function --*/ gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(destroyapp), NULL); /*-- Create the menu bar --*/ menubar = gtk_menu_bar_new(); /*-- Add the menubar to the vbox --*/ gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0); gtk_widget_show(menubar); /*-- Add the text area to the window --*/ gtk_container_add(GTK_CONTAINER(vbox), text); /*-- Add the vbox to the main window --*/ gtk_container_add(GTK_CONTAINER(window), vbox); Marudhar Engg. College Page 15

X-Window Lab

/*---------------- Create File menu items ------------------*/ menuFile = gtk_menu_item_new_with_label ("File"); gtk_menu_bar_append (GTK_MENU_BAR(menubar), menuFile); gtk_widget_show(menuFile); /*-- Create File submenu --*/ menu = gtk_menu_new(); gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuFile), menu); /*-- Create New menu item under File submenu --*/ menuitem = gtk_menu_item_new_with_label ("New"); gtk_menu_append(GTK_MENU(menu), menuitem); gtk_widget_show (menuitem); /*-- Create Open menu item under File submenu --*/ menuitem = gtk_menu_item_new_with_label ("Open"); gtk_menu_append(GTK_MENU(menu), menuitem); gtk_widget_show (menuitem); /*-- Create Exit menu item under File submenu --*/ menuitem = gtk_menu_item_new_with_label ("Exit"); gtk_menu_append(GTK_MENU(menu), menuitem); gtk_signal_connect(GTK_OBJECT (menuitem), "activate", GTK_SIGNAL_FUNC (ClosingAppWindow), NULL); gtk_widget_show (menuitem); /*---------------- End File menu declarations ----------------*/ /*---------------- Create Edit menu items --------------------*/ menuEdit = gtk_menu_item_new_with_label ("Edit"); gtk_menu_bar_append (GTK_MENU_BAR(menubar), menuEdit); gtk_widget_show(menuEdit); /*-- Create File submenu --*/ menu = gtk_menu_new(); gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuEdit), menu); /*-- Create Undo menu item under Edit submenu --*/ menuitem = gtk_menu_item_new_with_label ("Undo"); gtk_menu_append(GTK_MENU(menu), menuitem); gtk_widget_show (menuitem); /*-- Create Copy menu item under File submenu --*/ menuitem = gtk_menu_item_new_with_label ("Copy"); gtk_menu_append(GTK_MENU(menu), menuitem); gtk_widget_show (menuitem);

Marudhar Engg. College

Page 16

X-Window Lab

/*-- Create Cut menu item under File submenu --*/ menuitem = gtk_menu_item_new_with_label ("Cut"); gtk_menu_append(GTK_MENU(menu), menuitem); gtk_widget_show (menuitem); /*---------------- End Edit menu declarations ----------------*/ /*---------------- Start Help menu declarations ----------------*/ menuHelp = gtk_menu_item_new_with_label ("Help"); gtk_menu_bar_append (GTK_MENU_BAR(menubar), menuHelp); gtk_widget_show(menuHelp); /*-- Create Help submenu --*/ menu = gtk_menu_new(); gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuHelp), menu); /*-- Create About menu item under Help submenu --*/ menuitem = gtk_menu_item_new_with_label ("About"); gtk_menu_append(GTK_MENU(menu), menuitem); gtk_widget_show (menuitem); /*---------------- End Help menu declarations ----------------*/ /*-- Add some text to the window --*/ gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL, buffer, strlen(buffer)); /*-- Set window border to zero so that text area takes up the whole window --*/ gtk_container_border_width (GTK_CONTAINER (window), 0); /*-- Set the window to be 640 x 480 pixels --*/ gtk_window_set_default_size (GTK_WINDOW(window), 640, 200); /*-- Set the window title --*/ gtk_window_set_title(GTK_WINDOW (window), "Text Area"); /*-- Display the widgets --*/ gtk_widget_show(text); gtk_widget_show(menuitem); gtk_widget_show(menuFile); gtk_widget_show(menuEdit); gtk_widget_show(menuHelp); gtk_widget_show(vbox); gtk_widget_show(window); /*-- Start the GTK event loop --*/ gtk_main(); /*-- Return 0 if exit is successful --*/ return 0; } Marudhar Engg. College Page 17

X-Window Lab

output:-

***************************************************************** ****

To implement various drag and drop based GUI components in Visual Basic
***************************************************************** **** Option Explicit Dim Deposit As Single Dim Interest As Single Marudhar Engg. College Page 18

X-Window Lab

Dim Months As Single Dim Final As Single Const vbKeyDecPt = 46 Private Sub cmdCalculate_Click() Dim IntRate As Single Dim IntNew As Single Dim Fcn As Single, FcnD As Single 'Read the four text boxes Deposit = Val(txtDeposit.Text) Interest = Val(txtInterest.Text) IntRate = Interest / 1200 Months = Val(txtMonths.Text) Final = Val(txtFinal.Text) 'Determine which box is blank 'Compute that missing value and put in text box If Trim(txtDeposit.Text) = "" Then 'Deposit missing Deposit = Final / (((1 + IntRate) ^ Months - 1) / IntRate) txtDeposit.Text = Format(Deposit, "#####0.00") ElseIf Trim(txtInterest.Text) = "" Then 'Interest missing - requires iterative solution IntNew = (Final / (0.5 * Months * Deposit) - 1) / Months Do IntRate = IntNew Fcn = (1 + IntRate) ^ Months - Final * IntRate / Deposit - 1 FcnD = Months * (1 + IntRate) ^ (Months - 1) - Final / Deposit IntNew = IntRate - Fcn / FcnD Loop Until Abs(IntNew - IntRate) < 0.00001 / 12 Interest = IntNew * 1200 txtInterest.Text = Format(Interest, "##0.00") ElseIf Trim(txtMonths.Text) = "" Then 'Months missing Months = Log(Final * IntRate / Deposit + 1) / Log(1 + IntRate) txtMonths.Text = Format(Months, "###.0") ElseIf Trim(txtFinal.Text) = "" Then 'Final value missing Final = Deposit * ((1 + IntRate) ^ Months - 1) / IntRate txtFinal.Text = Format(Final, "#####0.00") End If End Sub Private Sub cmdClear_Click() 'Blank out text boxes txtDeposit.Text = "" txtInterest.Text = "" txtMonths.Text = "" txtFinal.Text = "" End Sub Marudhar Engg. College Page 19

X-Window Lab

Private Sub cmdExit_Click() End End Sub Private Sub txtDeposit_KeyPress(KeyAscii As Integer) 'Only allow number keys, decimal point, or backspace If (KeyAscii >= vbKey0 And KeyAscii <= vbKey9) Or KeyAscii = vbKeyDecPt Or KeyAscii = vbKeyBack Then Exit Sub Else KeyAscii = 0 Beep End If End Sub Private Sub txtFinal_KeyPress(KeyAscii As Integer) 'Only allow number keys, decimal point, or backspace If (KeyAscii >= vbKey0 And KeyAscii <= vbKey9) Or KeyAscii = vbKeyDecPt Or KeyAscii = vbKeyBack Then Exit Sub Else KeyAscii = 0 Beep End If End Sub Private Sub txtInterest_KeyPress(KeyAscii As Integer) 'Only allow number keys, decimal point, or backspace If (KeyAscii >= vbKey0 And KeyAscii <= vbKey9) Or KeyAscii = vbKeyDecPt Or KeyAscii = vbKeyBack Then Exit Sub Else KeyAscii = 0 Beep End If End Sub Private Sub txtMonths_KeyPress(KeyAscii As Integer) If (KeyAscii >= vbKey0 And KeyAscii <= vbKey9) Or KeyAscii = 46 Or KeyAscii = vbKeyBack Then Exit Sub Else KeyAscii = 0 Beep End If End Sub

Marudhar Engg. College

Page 20

X-Window Lab

OUTPUT :-

***************************************************************** ****

To implement a multiple windows application

Marudhar Engg. College

Page 21

X-Window Lab

***************************************************************** **** To implement multiple window application we are using a simple notepad application. Which performs traditional wordprocessing function like text file creation, modification, deletion, saving text file and priting it plus many more usual function like find and window arrangement(casecad, horizontally, vertically).

CODING :Option Explicit Public DocColl As New Collection Public CollKeyIndex As Integer Private Sub MDIForm_Load() Me.Height = Screen.Height * 0.75 Me.Width = Screen.Width * 0.75 Me.Move (Screen.Height - Me.Height) / 2, (Screen.Width - Me.Width) / 2 AddForm End Sub

Marudhar Engg. College

Page 22

X-Window Lab

Public Sub DisplayLineNumber() Dim LineNum As Integer With frmMain.ActiveForm.txtDocument If .SelStart > 0 Then LineNum = .GetLineFromChar(.SelStart) End If frmMain.StatusBar1.Panels("Text").Text = "Line: " & CStr(LineNum + 1) End With End Sub Private Function AddForm() As frmDocument Set AddForm = New frmDocument AddForm.Show vbModeless End Function Private Sub MDIForm_Terminate() Set DocColl = Nothing End Sub Private Sub mnuEditColor_Click() CommonDialog1.Flags = cdlCCFullOpen CommonDialog1.ShowColor frmMain.ActiveForm.txtDocument.SelColor = CommonDialog1.Color End Sub Private Sub mnuEditCopy_Click() CopyTextToClipboard SetupCSMenus End Sub Private Sub mnuEditCut_Click() CopyTextToClipboard DeleteSelectedText SetupCSMenus End Sub Private Sub mnuEditDelete_Click() DeleteSelectedText SetupCSMenus End Sub Private Sub mnuEditFind_Click() frmFind.Show vbModal End Sub Private Sub mnuEditFont_Click() CommonDialog1.Flags = cdlCFBoth Or cdlCFEffects CommonDialog1.ShowFont With frmMain.ActiveForm.txtDocument .SelFontName = CommonDialog1.FontName .SelFontSize = CommonDialog1.FontSize .SelBold = CommonDialog1.FontBold .SelItalic = CommonDialog1.FontItalic .SelStrikeThru = CommonDialog1.FontStrikethru .SelUnderline = CommonDialog1.FontUnderline .SelColor = CommonDialog1.Color End With End Sub Private Sub mnuEditPaste_Click() Dim Text As String Dim ClipboardText As String Dim SelStart As Long

Marudhar Engg. College

Page 23

X-Window Lab

If Clipboard.GetFormat(vbCFText) Then If frmMain.ActiveForm.txtDocument.SelLength > 0 Then DeleteSelectedText End If Text = frmMain.ActiveForm.txtDocument.Text SelStart = frmMain.ActiveForm.txtDocument.SelStart ClipboardText = Clipboard.GetText frmMain.ActiveForm.txtDocument.Text = Left(Text, SelStart) & ClipboardText & Right(Text, Len(Text) - SelStart) frmMain.ActiveForm.txtDocument.SelStart = SelStart Else SetupCSMenus End If End Sub Private Sub mnuEditSelectAll_Click() frmMain.ActiveForm.txtDocument.SelStart = 0 frmMain.ActiveForm.txtDocument.SelLength = Len(frmMain.ActiveForm.txtDocument.Text) SetupCSMenus End Sub Private Sub mnuEditTimeDate_Click() Dim Text As String Dim SelStart As Long If frmMain.ActiveForm.txtDocument.SelLength > 0 Then DeleteSelectedText End If Text = frmMain.ActiveForm.txtDocument.Text SelStart = frmMain.ActiveForm.txtDocument.SelStart frmMain.ActiveForm.txtDocument.Text = Left(Text, SelStart) & Format(Now, "Short Time") & " " & Format(Now, "Short Date") & Right(Text, Len(Text) - SelStart) frmMain.ActiveForm.txtDocument.SelStart = SelStart SetupCSMenus End Sub Private Sub mnuEditWordWrap_Click() mnuEditWordWrap.Checked = Not mnuEditWordWrap.Checked End Sub Private Sub mnuFileClose_Click() Unload frmMain.ActiveForm End Sub Private Sub mnuFileExit_Click() End End Sub Private Sub mnuFileNew_Click() AddForm End Sub Public Sub SetupCSMenus() Dim Found As Boolean Dim I As Integer For I = 0 To Forms.Count - 1 If Forms(I).Name = "frmDocument" Then If Forms(I).objDocument.Closing = False Then Found = True Exit For End If

Marudhar Engg. College

Page 24

X-Window Lab

End If Next I mnuFileClose.Enabled = Found mnuFileSave.Enabled = Found mnuFileSaveAs.Enabled = Found mnuFilePrint.Enabled = Found Toolbar1.Buttons("Save").Enabled = Found ' enable / disable Select All, TimeDate, WordWrap and Find _ edit menus according to whether or not there is an active document mnuEditSelectAll.Enabled = Found mnuEditTimeDate.Enabled = Found mnuEditWordWrap.Enabled = Found mnuEditFind.Enabled = Found If Found = False Then mnuEditCopy.Enabled = False mnuEditCut.Enabled = False mnuEditDelete.Enabled = False mnuEditPaste.Enabled = False mnuEditWordWrap.Checked = False Else If frmMain.ActiveForm.txtDocument.SelLength > 0 Then mnuEditCopy.Enabled = True mnuEditCut.Enabled = True mnuEditDelete.Enabled = True Else mnuEditCopy.Enabled = False mnuEditCut.Enabled = False mnuEditDelete.Enabled = False End If If Clipboard.GetFormat(vbCFText) Then mnuEditPaste.Enabled = True Else mnuEditPaste.Enabled = False End If End If End Sub Private Sub CopyTextToClipboard() Clipboard.SetText frmMain.ActiveForm.txtDocument.SelText End Sub Private Sub DeleteSelectedText() Dim Text As String Dim SelStart As Long Dim SelLength As Long Text = frmMain.ActiveForm.txtDocument.Text SelStart = frmMain.ActiveForm.txtDocument.SelStart SelLength = frmMain.ActiveForm.txtDocument.SelLength frmMain.ActiveForm.txtDocument.Text = Left(Text, SelStart) & Right(Text, Len(Text) - (SelStart + SelLength)) frmMain.ActiveForm.txtDocument.SelStart = SelStart End Sub Private Sub mnuFileOpen_Click() Dim Cancel As Boolean On Error GoTo ErrorHandler Cancel = False

Marudhar Engg. College

Page 25

X-Window Lab

CommonDialog1.Filter = "Rich Text files (*.rtf)|*.rtf|" & _ "Text files (*.txt)|*.txt|All files (*.*)|*.*" CommonDialog1.CancelError = True CommonDialog1.Flags = cdlOFNHideReadOnly Or _ cdlOFNFileMustExist CommonDialog1.ShowOpen If Not Cancel Then If UCase(Right(CommonDialog1.FileName, 3)) = "RTF" Then frmMain.ActiveForm.txtDocument.LoadFile CommonDialog1.FileName, rtfRTF Else frmMain.ActiveForm.txtDocument.LoadFile CommonDialog1.FileName, rtfText End If frmMain.ActiveForm.objDocument.DocName = CommonDialog1.FileName frmMain.ActiveForm.objDocument.Changed = False End If Exit Sub ErrorHandler: If Err.Number = cdlCancel Then Cancel = True Resume Next End If End End Sub Private Sub mnuFileSave_Click() With frmMain.ActiveForm If .objDocument.DocName = "Untitled" Then mnuFileSaveAs_Click Else If UCase(Right(.objDocument.DocName, 3)) = "RTF" Then .txtDocument.SaveFile .objDocument.DocName, rtfRTF Else .txtDocument.SaveFile .objDocument.DocName, rtfText End If .objDocument.Changed = False End If End With End Sub Private Sub mnuFileSaveAs_Click() Dim Cancel As Boolean On Error GoTo ErrorHandler Cancel = False CommonDialog1.DefaultExt = ".rtf" CommonDialog1.Filter = "Rich Text files (*.rtf)|*.rtf|" & _ "Text files (*.txt)|*.txt|All files (*.*)|*.*" CommonDialog1.CancelError = True CommonDialog1.Flags = cdlOFNHideReadOnly Or _ cdlOFNOverwritePrompt CommonDialog1.ShowSave If Not Cancel Then With frmMain.ActiveForm If UCase(Right(CommonDialog1.FileName, 3)) = "RTF" Then .txtDocument.SaveFile CommonDialog1.FileName, rtfRTF Else .txtDocument.SaveFile CommonDialog1.FileName, rtfText

Marudhar Engg. College

Page 26

X-Window Lab

End If .objDocument.DocName = CommonDialog1.FileName .objDocument.Changed = False End With End If Exit Sub ErrorHandler: If Err.Number = cdlCancel Then Cancel = True Resume Next End If End Sub Private Sub mnuHelpAbout_Click() frmAbout.Show vbModal End Sub Private Sub mnuToolsOptions_Click() frmOptions.Show vbModal End Sub Private Sub mnuWindowArrangeIcons_Click() frmMain.Arrange vbArrangeIcons End Sub Private Sub mnuWindowCascade_Click() frmMain.Arrange vbCascade End Sub Private Sub mnuWindowTileHorizontally_Click() frmMain.Arrange vbTileHorizontal End Sub Private Sub mnuWindowTileVertically_Click() frmMain.Arrange vbTileVertical End Sub Private Sub Toolbar1_ButtonClick(ByVal TheButton As Button) Select Case TheButton.Key Case "New" mnuFileNew_Click Case "Open" mnuFileOpen_Click Case "Save" mnuFileSave_Click End Select End Sub

Find Form:-

Marudhar Engg. College

Page 27

X-Window Lab

CODING :Option Explicit Private Sub cmdCancel_Click() Unload Me End Sub Private Sub cmdFind_Click() Dim DocumentObject As clsDocument Dim FindResults As Integer For Each DocumentObject In frmMain.DocColl FindResults = InStr(DocumentObject.DocumentForm.txtDocument.Text, txtFind.Text) If FindResults > 0 Then DocumentObject.DocumentForm.ZOrder DocumentObject.DocumentForm.txtDocument.SelStart = FindResults - 1 DocumentObject.DocumentForm.txtDocument.SelLength = Len(txtFind.Text) Exit For End If Next DocumentObject Unload Me End Sub

Document Form:-

Marudhar Engg. College

Page 28

X-Window Lab

CODING:Option Explicit Public objDocument As clsDocument Dim CollKey As Integer Private Sub Form_Activate() frmMain.SetupCSMenus frmMain.DisplayLineNumber End Sub Private Sub Form_Load() Set objDocument = New clsDocument frmMain.DocColl.Add objDocument, CStr(frmMain.CollKeyIndex) CollKey = frmMain.CollKeyIndex frmMain.CollKeyIndex = frmMain.CollKeyIndex + 1 Set objDocument.DocumentForm = Me objDocument.DocName = Me.Caption End Sub Private Sub Form_Resize() Me.txtDocument.Top = 0 Me.txtDocument.Left = 0 Me.txtDocument.Width = Me.ScaleWidth Me.txtDocument.Height = Me.ScaleHeight End Sub

Marudhar Engg. College

Page 29

X-Window Lab

Private Sub Form_Unload(Cancel As Integer) If objDocument.Changed Then Select Case MsgBox("The text in the " & objDocument.DocName & _ " file has changed." & Chr(13) & Chr(13) & _ "Do you want to save the changes ?", _ vbExclamation Or vbYesNoCancel, frmMain.Caption) Cancel = True End Select End If If Cancel = False Then objDocument.Closing = True End If frmMain.SetupCSMenus If Cancel = False Then frmMain.DocColl.Remove CStr(CollKey) Set objDocument = Nothing frmMain.StatusBar1.Panels("Text").Text = "" End If End Sub Private Sub txtDocument_KeyPress(KeyAscii As Integer) objDocument.Changed = True End Sub Private Sub txtDocument_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) frmMain.SetupCSMenus End Sub Private Sub txtDocument_SelChange() frmMain.DisplayLineNumber End Sub

Help Form:-

CODING :Option Explicit Private Sub cmdOK_Click() Unload Me End Sub Private Sub Form_Load() Me.Move (Screen.Height - Me.Height) / 2, (Screen.Width - Me.Width) / 2

Marudhar Engg. College

Page 30

X-Window Lab

End Sub

Marudhar Engg. College

Page 31

You might also like