Using Motif with C++ X and Motif are written in C

Slides:



Advertisements
Similar presentations
Chapter 16 Graphical User Interfaces
Advertisements

Chapter 16 Graphical User Interfaces John Keyser’s Modifications of Slides by Bjarne Stroustrup
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Container Widgets Container widgets can be used to combine other widgets into composite panels. Examples: – XmMessageBox – XmForm – XmBulletinBoard – XmRowColumn.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Introduction To System Analysis and Design
1 Lab Session-1 CSIT221 Fall 2002 b Refresher slides b Practice Problem b Lab Exercise (Demo Required)
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Pre-Exam Class CSIT121 Fall 2000 Exam-II (Final Examination) TUESDAY DECEMBER 19th 8:30AM.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Chapter 13: Object-Oriented Programming
A Simple Motif Widget The basic steps involved in creating and displaying this or any other Motif widget are essentially the same. This is a shell widget.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
C++ fundamentals.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Unified Modeling Language
Programming Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Chapter 3 Introduction to Collections – Stacks Modified
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming 1.
OOD Case Study (For parallel treatment, see Chapter 2 of the text)
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
1 Kyung Hee University Diagram Editor : Implementation View Spring 2001.
FLTK Help Session By Richard Yu Gu CS 638 -Graphics Fall, 1999.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Data Structures and Java CS 105. L7: Java Slide 2 Data structure Data structure defined: A systematic way of organizing and accessing data Examples Dictionary:
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Computer Science 111 Fundamentals of Programming I Model/View/Controller and Data model design.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Building X Window GUIs with Motif The X Window System: Architecture Applications Sample code Review function (as time permits) … Introduce what it’s like.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Python Programming Graphical User Interfaces Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
© 2005 Prentice Hall9-1 Stumpf and Teague Object-Oriented Systems Analysis and Design with UML.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
Implementing the 8-puzzle Design (no corresponding text chapter)
Dale Roberts Introduction to Visual Programming Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Build-A-Button Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, October 8, 2003.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Creating a Multiple-Form Interface.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Object-Oriented Programming Chapter Chapter
QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
+ TIC-TAC-TOE GAME CAPSTONE PROJECT SEN Team Members Sno.NameITU ID 1Keerthi Alimity Venkata Ganugapati Sujitha Sanku Bavi Bharathan87550.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Dialog Widgets Dialog widgets present information and collect input from the user: Dialogs are windows that are often displayed for a relatively short.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Object-Oriented Programming: Polymorphism Chapter 10.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Java for android Development Nasrullah Khan. Using instanceof in Android Development the classes such as Button, TextView, and CheckBox, which represent.
Java Programming Language
Presentation transcript:

Using Motif with C++ X and Motif are written in C C++ is backward compatible with C, provided that the function prototypes are correctly given So it is straightforward to call X and Motif from C++ programs But all of our examples so far just look like C programs with strong typing (no class-oriented design)

Classes in C++ and Motif Motif is designed with a class hierarchy and inheritance in mind (see next slide) But the approaches to classes used by Motif and C++ are incompatible: C++ programmers often work with object-oriented libraries by creating new specialized subclasses of the library However, although Motif widgets are described as classes, there is no way to create a C++ class that is a subclass of a Motif widget

Motif and Xt Widget Classes Label PushButton . DrawnButton Primitive . Text RowColumn Core Constraint Manager Form BulletinBoard Composite . . . Shell . . .

Approaches to Mixing Motif and C++ Do not use OO features of C++ and just write applications in a style similar to C misses out on the benefit of OOP Wrap each Motif widget class in a C++ class public domain and commercial wrapper sets are available disadvantages: additional code, support, portability Create higher-level user interface components in C++ which use Motif widgets as data attributes use C++ classes to describe software architecture do not force Motif widgets to be C++ classes we will emphasize this approach

Example: A Tic-Tac-Toe Program Board Game canvas: Widget display: Display gc: GC board game state: CharArray count: Integer playerX: Player game game board startd endd summaryd Main Dialog shell: Widget app: XtAppContext dialog: Widget *

Notes on TTT Class Diagram Only the class attributes are shown All classes have singleton members except Dialog The role of Motif widgets is to be attributes of C++ classes: Main::shell, a shell widget Board::canvas, an XmDrawingArea widget Dialog::dialog, an XmDialogShell widget

Notes on TTT Class Diagram (cont'd) The Board class manages the game display: Detects clicks, displays marks Needs the game object to access internal state The Game class runs the game and keeps an internal representation Needs the board object to update the display The Dialog class manages the start, end, and summary dialogs Needs the game object to return results of dialog The Main class creates all of the above and manages the Xt connection

The Game Class Recall that Game also has a Board as an attribute as shown in the class diagram.

The Board Class Recall that Board also has a Game as an attribute as shown in the class diagram.

Board Class Methods drawGrid, drawX, and drawO are the subject of a lab exercise manage and unManage allow for exposing and hiding the drawing area asciiDisplay is for debugging purposes drawBoardCallback responds to any expose event generated for the drawing area boardClickCallback responds to any mouse input event generated for the drawing area

Callbacks Available for Drawing Areas XmNexposeCallback Triggered when part of the widget is exposed. We must provide a callback that will ``repaint'' the tic- tac-toe board when this is triggered XmNinputCallback Triggered when the widget receives a keyboard or mouse event. We must provide a callback that will determine the coordinates of the clicked square and ask the board to draw an appropriate mark there XmNresizeCallback Triggered when the widget is resized. (We will not act on resizings.)

BoardInfo Constructor At the time we construct a new board, we will also add the callbacks for it: BoardInfo::BoardInfo(Widget parent, Game g) { ... canvas = XtVaCreateManagedWidget ( "canvas", xmDrawingAreaWidgetClass, parent, XmNheight, 300, XmNwidth, 300, NULL ); ... XtAddCallback(canvas, XmNexposeCallback, &BoardInfo::drawBoardCallback, ...); XtAddCallback(canvas, XmNinputCallback, &BoardInfo::boardClickCallback, ...); } Since the callbacks are static class methods, they must be qualified by the class name and begun with ``&''.

Writing the Draw Board Callback When an expose event occurs for the drawing area, the action called for is straightforward: drawGrid(); for (Integer x = 0; x < 3; x++) // Draw the Xs & Os for (Integer y = 0; y < 3; y++) { if (game->getState(x,y) == 'X') drawX(x, y); else if (game->getState(x,y) == 'O') drawO(x, y); }

Writing the Draw Board Callback (cont'd) Q: So, will this work? void BoardInfo::drawBoardCallback(Widget, XtPointer, XtPointer) { drawGrid(); for (Integer x = 0; x < 3; x++) // Draw the Xs & Os for (Integer y = 0; y < 3; y++) { if (game->getState(x,y) == 'X') drawX(x, y); else if (game->getState(x,y) == 'O') drawO(x, y); } A: No, because: - callbacks are straight C functions and thus are static - static methods cannot access objects like game

Solution: Using Client Data Recall the XtAddCallback function prototype: void XtAddCallback ( Widget widget, //1 const String callbackName, //2 XtCallbackProc proc, //3 XtPointer clientData); //4 Recall the actual callback function prototype: void <funcname> (Widget w, // 1 XtPointer clientData, // 2 XtPointer callData); // 3

Using Client Data The clientdata argument is specified by the call to XtAddCallback. The system remembers it and passes it along to the callback when appropriate. XtPointer is a generic pointer type defined by Xt. It can be type cast to any pointer type. So use it to obtain a pointer to an object that knows about the game object, i.e., the board object.

Strategy When adding the callback to draw the grid, include the this pointer (a pointer to this board) as clientData Write the (static) callback to use the clientData to get the board object for which the callback was added The (static) callback does nothing but call a nonstatic method on the board object that does the work of drawing the grid

BoardInfo Constructor Again BoardInfo::BoardInfo(Widget parent, Game g) { ... canvas = XtVaCreateManagedWidget ( "canvas", xmDrawingAreaWidgetClass, parent, XmNheight, 300, XmNwidth, 300, NULL ); ... XtAddCallback(canvas, XmNexposeCallback, &BoardInfo::drawBoardCallback, (XtPointer) this); XtAddCallback(canvas, XmNinputCallback, &BoardInfo::boardClickCallback, (XtPointer) this); } Since the clientData argument is of type XtPointer, the this pointer, which is of type BoardInfo*, must be type cast

BoardInfo Class Declaration class BoardInfo { private: Game game; // the game state Widget canvas; // drawing area Display* display; // display device for app GC gc; // graphics context public: BoardInfo(Widget parent, Game g); void drawGrid(); // draw game grid void drawX ( Integer x, Integer y); // Draw an X at (x,y) void drawO ( Integer x, Integer y); // Draw an O at (x,y) void manage(); // expose drawing area void unManage(); // hide the drawing area void asciiDisplay(); // display for debugging static void drawBoardCallback(Widget, XtPointer clientData, XtPointer); void drawBoard(); // draw board when it is exposed static void boardClickCallback(Widget, XtPointer clientData, void boardClick(); // get board coords when clicked }

Draw Board Callback Methods void BoardInfo::drawBoardCallback(Widget, XtPointer clientData, XtPointer) { Board * b = (Board *)clientData; b->drawBoard(); } void BoardInfo::drawBoard() { drawGrid(); for (Integer x = 0; x < 3; x++) // Draw the Xs & Os for (Integer y = 0; y < 3; y++) { if (game->getState(x,y) == 'X') drawX(x, y); else if (game->getState(x,y) == 'O') drawO(x, y);

Macros to Handle C++ Callbacks Since it's a hassle to define two methods for every callback, we can define macros to simplify both the declaration and the implementation of callbacks. #define name2(a,b) a ## b #define DECL_CALLBACK(func) \ private: \ static void name2(func,Callback) (Widget, \ XtPointer, \ XtPointer); \ protected: \ virtual void func ( Widget, XtPointer)

Callback Declaration Macro Whenever the preprocessor sees name2(a,b), it plugs in the concatenation of a and b: For example, name2(drawBoard,Callback) becomes drawBoardCallback Whenever the preprocessor sees DECL_CALLBACK(func) it plugs in two method declarations. For example, DECL_CALLBACK(drawBoard) becomes: private: \ static void drawBoardCallback (Widget, \ XtPointer, \ XtPointer); \ protected: \ virtual void drawBoard ( Widget, XtPointer)

BoardInfo Class Declaration Again class BoardInfo { private: Game game; // the game state Widget canvas; // drawing area Display* display; // display device for app GC gc; // graphics context public: BoardInfo(Widget parent, Game g); void drawGrid(); // draw game grid void drawX ( Integer x, Integer y); // Draw an X at (x,y) void drawO ( Integer x, Integer y); // Draw an O at (x,y) void manage(); // expose drawing area void unManage(); // hide the drawing area void asciiDisplay(); // display for debugging DECL_CALLBACK(drawBoard); // draw board when it is exposed DECL_CALLBACK(boardClick); // get board coords when clicked }

Callback Implementation Macro We can also define a macro to simplify the implementation (definition) of callbacks: #define IMPL_CALLBACK(cls, func) \ void cls::name2(func,Callback) (Widget w, \ XtPointer clientData, \ XtPointer callData) \ { \ ((cls *)clientData)->func(w, callData); \ } \ \ void cls::func(Widget w, XtPointer callData) This macro will hide the details of the communication between the static and nonstatic class methods.

Simplified Implementation of Draw Board Callback IMPL_CALLBACK(BoardInfo, drawBoard) { drawGrid(); for (Integer x = 0; x < 3; x++) for (Integer y = 0; y < 3; y++) { if (game->getState(x,y) == 'X') drawX(x, y); else if (game->getState(x,y) == 'O') drawO(x, y); } The callback macros are in the file CallbackMacros.h in the Tic-Tac-Toe directory.

Board Click Callback When a mouse click is detected in the board's drawing area, what must be done? The (x,y) pixel coordinates of the click location must be determined From these coordinates the indices of the 3x3 character array (internal representation) must be calculated The game object must be told to process a move given these indices To retrieve the (x,y) pixel coordinates, we must make use of the drawing area widget's call data

Using Call Data Recall the callback function prototype: void <funcname> (Widget w, // 1 XtPointer clientData, // 2 XtPointer callData); // 3 The third argument is a pointer to a data structure holding information from the widget. At a minimum: typedef struct { int reason; // coded reason for callback XEvent *event; // event causing callback } XmAnyCallbackStruct This structure can be extended in widget-specific ways.

Using Call Data (cont'd) When call data is needed in a callback, first do: XmAnyCallbackStruct *cbs = (XmAnyCallbackStruct *) callData; Now we can get the general event causing the callback with: cbs->event From the general event we can get the specific button event: cbs->event->xbutton From the button event we can get the x and y pixel coordinates: cbs->event->xbutton.x cbs->event->xbutton.y

Board Click Callback Implementation // get board coordinates when clicked and process move IMPL_CALLBACK(BoardInfo, boardClick) { XmAnyCallbackStruct *cbs = (XmAnyCallbackStruct *) callData; Integer x = (Integer) floor(cbs->event->xbutton.x/100); Integer y = (Integer) floor(cbs->event->xbutton.y/100); game->processMove(x,y); } Since a tic-tac-toe square is 100 x 100 pixels, this will translate pixel coordinates to game board coordinates in a 3 x 3 character array. For example: (150,150) -> (1,1)