From: dao@rchland.vnet.ibm.com (Dan Dao)
Subject: Traversal problem
Date: Wed, 16 Dec 1992 21:10:20 GMT
Reply-To: dao@rchland.vnet.ibm.com
Disclaimer: This posting represents the poster's views, not necessarily those of IBM
Organization: IBM Rochester

-- 
I'm trying to create a new widget and having a problem with the
XmProcessTraversal 

What I have is a form widget which contains a text widget, scrolled window and 
its drawing area.
What I want is to be able to type in the drawing area and the key will appear
in the text widget.
I've included the code at the end of this note for you to see what I am trying to do.
It works for the first round, but fail on sub-sequence round.
What did I do wrong?

--------cmd.c--------------
#include <stdio.h>
#include <X11/IntrinsicP.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/CoreP.h>
#include <X11/Shell.h>
#include <X11/Xlib.h>
#include <Xm/Xm.h>
#include <Xm/DrawingA.h>
#include <Xm/ScrolledW.h>
#include <Xm/Form.h>
#include <Xm/Text.h>

#include "cmdP.h"
#include "cmd.h"

static XtResource cmd_resources[] =
{
  { XmNeditable, XmCEditable, XtRBoolean, sizeof( Boolean), XtOffset( CmdWidget, cmd.editable ), XtRString, "True" },
};

static void PageUp( );
static void PageDown( );
static void PageLeft( );
static void PageRight( );
static void BeginLine( );
static void EndLine( );
static void Up( );
static void Down( );
static void Left( );
static void Right( );
static void Slash( );
static void Backspace( );
static void Other( );

static XtActionsRec action_list[] =
{
  {"PageUp", PageUp},
  {"PageDown", PageDown},
  {"PageLeft", PageLeft},
  {"PageRight", PageRight},
  {"BeginLine", BeginLine},
  {"EndLine", EndLine},
  {"Up", Up},
  {"Down", Down},
  {"Left", Left},
  {"Right", Right},
  {"Slash", Slash},
  {"Backspace", Backspace},
  {"Other", Other},
};

static char default_translations[] =
" Ctrl<Key>osfPageUp : PageUp()\n\
  Ctrl<Key>osfPageDown : PageDown()\n\
      <Key>osfPageUp : PageLeft()\n\
      <Key>osfPageDown : PageRight()\n\
      <Key>osfBeginLine : BeginLine()\n\
      <Key>osfEndLine : EndLine()\n\
      <Key>osfUp : Up()\n\
      <Key>osfDown : Down()\n\
      <Key>osfLeft : Left()\n\
      <Key>osfRight : Right()\n\
      <Key>osfBackSpace : Backspace()\n\
      <Key>slash : Slash()\n\
      <Key> : Other()";

static void    Initialize( );
static void    Realize( );
static void    Redisplay( );
static void    WindowResize( );
static void    Destroy( );
static Boolean SetValues( );

CmdClassRec cmdClassRec =
{
  {
    /* CoreClassPart */
    (WidgetClass) &widgetClassRec,  /* superclass            */
    "cmd",                          /* class_name            */
    sizeof(CmdRec),                 /* widget_size           */
    NULL,                           /* class_initialize      */
    NULL,                           /* class_part_initialize */
    FALSE,                          /* class_inited          */
    Initialize,                     /* initialize            */
    NULL,                           /* initialize_hook       */
    Realize,                        /* realize               */
    action_list,                    /* actions               */
    XtNumber( action_list ),        /* num_actions           */
    cmd_resources,                  /* resources             */
    XtNumber( cmd_resources ),      /* num_resources         */
    NULLQUARK,                      /* xrm_class             */
    TRUE,                           /* compress_motion       */
    TRUE,                           /* compress_exposure     */
    TRUE,                           /* compress_enterleave   */
    TRUE,                           /* visible_interest      */
    Destroy,                        /* destroy               */
    WindowResize,                   /* resize                */
    Redisplay,                      /* expose                */
    SetValues,                      /* set_values            */
    NULL,                           /* set_values_hook       */
    XtInheritSetValuesAlmost,       /* set_values_almost     */
    NULL,                           /* get_values_hook       */
    NULL,                           /* accept_focus          */
    XtVersion,                      /* version               */
    NULL,                           /* callback private      */
    default_translations,           /* tm_table              */
    NULL,                           /* query_geometry        */
    NULL,                           /* display_accelerator   */
    NULL,                           /* extension             */
  },
  {
    /* Cmd ClassPart */
    0,                              /* ignore                */
  }
};

WidgetClass cmdWidgetClass = (WidgetClass) &cmdClassRec;
static void ExposeDrawArea( Widget w, CmdWidget cmd, XmDrawingAreaCallbackStruct * call_data )
{
}

static EnterHit( Widget w, CmdWidget cmd, XmAnyCallbackStruct * call_data )
{
  XmProcessTraversal( cmd->cmd.draw_area, XmTRAVERSE_CURRENT );
}

static void PageUp( CmdWidget w, XEvent * event, String * params, Cardinal num_params )
{
}

static void PageDown( CmdWidget w, XEvent * event, String * params, Cardinal num_params )
{
}

static void PageLeft( CmdWidget w, XEvent * event, String * params, Cardinal num_params )
{
}

static void PageRight( CmdWidget w, XEvent * event, String * params, Cardinal num_params )
{
}

static void BeginLine( CmdWidget w, XEvent * event, String * params, Cardinal num_params )
{
}

static void EndLine( CmdWidget w, XEvent * event, String * params, Cardinal num_params )
{
}

static void Up( CmdWidget w, XEvent * event, String * params, Cardinal num_params )
{
}

static void Down( CmdWidget w, XEvent * event, String * params, Cardinal num_params )
{
}

static void Left( CmdWidget w, XEvent * event, String * params, Cardinal num_params )
{
}

static void Right( CmdWidget w, XEvent * event, String * params, Cardinal num_params )
{
}

static void Slash( CmdWidget w, XEvent * event, \keyword{char * args[], int n )
{
}

static void Backspace( CmdWidget w, XEvent * event, char * args[], int n )
{
}

static void Other( CmdWidget w, XEvent * event, \keyword{char * args[], int n )
{
  XmProcessTraversal( w->cmd.entry_window, XmTRAVERSE_CURRENT );
}

static void ButtonPressed( CmdWidget w, CmdWidget cmd, XButtonEvent * event )
{
  XmProcessTraversal( cmd->cmd.draw_area, XmTRAVERSE_CURRENT );
}

static void ButtonMoved( CmdWidget w, CmdWidget cmd, XMotionEvent * event )
{
}

static void ButtonReleased( CmdWidget w, CmdWidget cmd, XButtonEvent * event )
{
}

static void Initialize( CmdWidget request, CmdWidget new )
{
  Arg args[20];
  int n;

  new->cmd.foundation = XtCreateManagedWidget( "cmdFoundation", xmFormWidgetClass, XtParent( new ), NULL, 0 );

  n = 0;
  XtSetArg( args[n], XmNleftAttachment, XmATTACH_FORM ); ++n;  
  XtSetArg( args[n], XmNrightAttachment, XmATTACH_FORM ); ++n;
  XtSetArg( args[n], XmNbottomAttachment, XmATTACH_NONE ); ++n;
  XtSetArg( args[n], XmNtopAttachment, XmATTACH_FORM ); ++n;
  XtSetArg( args[n], XmNmarginHeight, 2 ); ++n; 
  XtSetArg( args[n], XmNmarginWidth, 2 ); ++n; 
  XtSetArg( args[n], XmNhighlightThickness, 0 ); ++n; 
  XtSetArg( args[n], XmNhighlightOnEnter, FALSE ); ++n; 
  new->cmd.entry_window = XtCreateManagedWidget( "cmdEntryWindow", xmTextWidgetClass, new->cmd.foundation, args, n );
  XtAddCallback( new->cmd.entry_window, XmNactivateCallback, EnterHit, new );
  XmAddTabGroup( new->cmd.entry_window );

  n = 0;
  XtSetArg( args[n], XmNleftAttachment, XmATTACH_FORM ); ++n;  
  XtSetArg( args[n], XmNrightAttachment, XmATTACH_FORM ); ++n;
  XtSetArg( args[n], XmNbottomAttachment, XmATTACH_FORM ); ++n;
  XtSetArg( args[n], XmNtopAttachment, XmATTACH_WIDGET ); ++n;
  XtSetArg( args[n], XmNtopWidget, new->cmd.entry_window ); ++n;
  XtSetArg( args[n], XmNwidth, new->core.width ); ++n;
  XtSetArg( args[n], XmNheight, new->core.height ); ++n;
  XtSetArg( args[n], XmNscrollingPolicy, XmAUTOMATIC ); ++n;
  new->cmd.scrolled_window = XmCreateScrolledWindow( new->cmd.foundation, "cmdScrolledWindow", args, n );


  n = 0;
  XtSetArg( args[n], XmNmarginWidth, 0 ); ++n;
  XtSetArg( args[n], XmNmarginHeight, 0 ); ++n;
  XtSetArg( args[n], XmNwidth, 200 ); ++n;
  XtSetArg( args[n], XmNheight, 200 ); ++n;
  XtSetArg( args[n], XmNresizePolicy, XmRESIZE_GROW ); ++n;
  new->cmd.draw_area = XtCreateWidget( "cmdDrawArea", xmDrawingAreaWidgetClass, new->cmd.scrolled_window, args, n );
  XmAddTabGroup( new->cmd.draw_area );

  XtAddCallback( new->cmd.draw_area, XmNexposeCallback, ExposeDrawArea, new );
  XtAddEventHandler( new->cmd.draw_area, ButtonPressMask, FALSE, ButtonPressed, new );
  XtAddEventHandler( new->cmd.draw_area, ButtonMotionMask, FALSE, ButtonMoved, new );
  XtAddEventHandler( new->cmd.draw_area, ButtonReleaseMask, FALSE, ButtonReleased, new );

  n = 0;
  XtSetArg( args[n], XmNworkWindow, new->cmd.draw_area ); ++n;
  XtSetValues( new->cmd.scrolled_window, args, n );

  XtManageChild( new->cmd.draw_area );
  XtManageChild( new->cmd.scrolled_window );
}

static void Destroy( CmdWidget w )
{
}

static void Realize( CmdWidget w )
{
}

static void Redisplay( CmdWidget w, XEvent * event, Region region )
{
}

static void WindowResize( CmdWidget w )
{
}

static Boolean SetValues( CmdWidget current, CmdWidget request, CmdWidget new )
{
}

--------------------------
--------cmdP.h-------------
#ifndef _cmdP_h
#define _cmdP_h

typedef struct _CmdClassPart
{
  int foo;
} CmdClassPart;

typedef struct _CmdClassRec
{
  CoreClassPart core_class;
  CmdClassPart  cmd_class;
} CmdClassRec;

extern CmdClassRec cmdClassRec;

typedef struct _CmdPart
{
  Boolean editable;

  Widget foundation;
  Widget entry_window;
  Widget scrolled_window;
  Widget draw_area;
} CmdPart ;

typedef struct _CmdRec
{
  CorePart core;
  CmdPart  cmd;
} CmdRec;


#endif
--------------------------
--------cmd.h--------------
#ifndef _Cmd_h
#define _Cmd_h

extern WidgetClass cmdWidgetClass;

typedef struct _CmdClassRec * CmdWidgetClass;
typedef struct _CmdRec      * CmdWidget;

#endif 
--------------------------

--------useCmd.c------------
#include <stdio.h>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <Xm/Xm.h>
#include "cmd.h"

Widget AppShell;
Widget AppContext;
Display * MainDisplay;

int main( int argc, char * argv[] )
{
  Arg args[5];
  int n, i, j;
  XGCValues gcValues;
  Widget cmd;

  XtToolkitInitialize( );
  AppContext = XtCreateApplicationContext( );
  MainDisplay = XtOpenDisplay( AppContext, NULL, "", "", NULL, 0, &argc, argv );
  AppShell = XtAppCreateShell( NULL, 0, applicationShellWidgetClass, MainDisplay, NULL, 0 );

  n = 0;
  XtSetArg( args[n], XmNwidth, 200 ); ++n;
  XtSetArg( args[n], XmNheight, 100 ); ++n;
  cmd = XtCreateManagedWidget( "cmd", cmdWidgetClass, AppShell, args, n );

  XtRealizeWidget( AppShell );
  XtAppMainLoop( AppContext );
}
--------------------------
---
Dan Dao dao@rchland.vnet.ibm.com (507)253-8023
Newsgroups: comp.windows.x.motif