LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-15-2011, 11:12 AM   #1
muggabug
LQ Newbie
 
Registered: Aug 2011
Posts: 18

Rep: Reputation: Disabled
How to programme around Xaw3d asciiTextWidget vanishing character bug ?


Hi,

update: Am I doing anything seriously wrong ? With Autofill set to True, it doesn't make any difference if I set XtNwrap to never, word or line!
I have a question about Xaw3d widgets. I would like to have suggestions where I can find a good forum for Xaw3d questions. Or a hint to the question below, of course .

My problem is this. I am trying to set asciiTextWidget to Autofill. and use it as a scrolling text input widget (one line high). But it does not scroll right.

The code is at the bottom of this message. In it,I use the function "setIP" to switch back to the correct (wrapped) line after using the backspace key at the start of a line. This solves one problem.
The asciiTextWidget, by itself, overwrites the last character when it wraps a line. You can see this by compiling the code below. Write in the text box beyond the right border, and click "report". The last character on the first line is gone. If the end of a line is 'qwe', and the 'e' does not fit any more, with the resources I have set, e is put on the next line, and 'w' disappears. 'My' checkFill function needs a lot of code to try to solve this (Add a -DRInsert option for gcc to compile with this function). It does not work 100 % yet, because Xaw turns newlines into spaces when you use form-paragraph. I remove these, but this removes user inputted spaces as well

So my question is. How have real programmers done this ? I went through some old applications, but they do not use the widget in this way, or write their own text box widget (xfm).


=========================================================================
Code:
// g++ -x c++ thisfile -lXaw3d -lXt -lX11 -L/usr/X11/lib
// add -DRInsert for test code
#define DEBUG

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/keysymdef.h>


#ifdef RInsert
#include <X11/IntrinsicP.h>
#include <X11/Xaw3d/AsciiSrcP.h> 
#endif

#include <X11/Xaw/Form.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/TextSrc.h>
#include <X11/Xaw/MultiSink.h>
#include <X11/Xaw/Text.h>


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

struct QAStrings{
  string title;
  vector<string> hints;
};

Widget topshell;
XtAppContext XtC;
XawTextPosition lasttpos,lp=0;
char bufferchar[1];

#ifdef RInsert
inline Widget XawTextGetSink(Widget w){   
return ((TextWidget)w)->text.sink;
}
int flag=1;
#endif
int lineSize=-1;

void  QuitCB(Widget w, XtPointer client, XtPointer call){
  XtDestroyApplicationContext(XtC);  
  XtAppSetExitFlag(XtC);  
  return;
}

void setIP(Widget w,XEvent *event,String *ps, Cardinal *nps){
  /* this function makes the previous line appear in the box after using the 
   backspace key at the beginning of the next line */

  XawTextPosition op=XawTextTopPosition(w);
#ifdef DEBUG
  cout <<" op is: "<<op<<"   "<<flush;
#endif
  if (op<lp){
     XtCallActionProc(w,"scroll-one-line-down",NULL,NULL,0);
  }
  XtCallActionProc(w,"form-paragraph",NULL,NULL,0);
  XtCallActionProc(w,"redraw-display",NULL,NULL,0);
}


void WriteSB(Widget w, XtPointer client, XtPointer call){
  Widget *tmp=(Widget *)client;
  char *str=new char[200];
  Arg widgetArgs[2];
  XtSetArg(widgetArgs[0],XtNstring,&str);
  XtGetValues(*tmp,widgetArgs,1);
  string s(str);
  remove(s.begin(),s.end(),'\n');  
  remove(s.begin(),s.end(),' ');  
  cout <<s<<flush;
  return;
}  
XawTextPosition *cp=new XawTextPosition(120);

void checkFill(Widget w,XEvent *event,String *ps, Cardinal *nps){
  XRectangle *rect=new XRectangle;
  XawTextPosition tempp;
  int a=-1,b=-1,c=-1;
  XawTextPosition t1=0,t2=15,t3=-10;
#ifdef DEBUG
  cout << "lp is: "<< (tempp=XawTextTopPosition(w));
  cout << " Insert pos is: " << (t3=XawTextGetInsertionPoint(w))<<endl<<flush;
 #endif

#ifdef RInsert

  XawTextSinkGetCursorBounds(XawTextGetSink(w),rect);
#ifdef DEBUG
  cout << "Cursor: at  x:"<<rect->x<<endl<<flush;
 #endif

  XawTextSinkFindDistance(XawTextGetSink(w),lp,-1,(XawTextPosition)300,&a,&t3,&c);
  
  XawTextBlock block, block2;
  char *deletethis,*deletethis2;
  block.firstPos=0;
  block.length=1;
  deletethis=block.ptr=new char[1];
  block2.firstPos=0;
  block2.length=1;
  deletethis2=block2.ptr=new char[1];

  XawTextSourceRead(XawTextGetSource(w),t3-1,&block,1);

  if ( tempp>lp) {flag=0;               //just for testing   !!!!!
#ifdef DEBUG
  cout << "block has:"<<*block.ptr <<" , "<<*bufferchar<<endl<<flush;
  cout << " TotWidth  "<< a << ", resPos," << t3 <<". \n"<<flush;
   cout << "buffer contains "<<bufferchar<<endl<<flush;
   cout << "lineSize, a:"<<lineSize<<"><"<<a<<" (lp,tempp)"<<lp\
<<","<<tempp<<flush<<endl;
#endif

   block.length=1;
   block.firstPos=0;
   strncpy(block2.ptr,bufferchar,1);
    XawTextPosition cpos=XawTextGetInsertionPoint(w);
#ifdef DEBUG
    cout <<"I am at "<< cpos << " and was at " << t3<<endl<<flush;
    cout << "Inserting "<<block2.ptr[0]<<" at "<<t3<<endl<<flush;
#endif
  block2.firstPos=0;
  XawTextReplace(w,t3,t3,&block2); 
  XtCallActionProc(w,"form-paragraph",NULL,NULL,0);
  XtCallActionProc(w,"redraw-display",NULL,NULL,0);
  }else
 *bufferchar=(char)block.ptr[0];
#ifdef DEBUG
  cout << "buffered: "<<bufferchar <<endl<<flush;
  cout << block.firstPos<<endl;
#endif
  delete [] deletethis;
  delete [] deletethis2;
#endif
  lineSize=a;
  lp=tempp;
}


class mainWindow{
  Widget win, button,label, bbox,userinput;
  Arg asciiwidArgs[16];
  Arg asciiArgs[2];
  static String default_colour[];

public:

 XtActionsRec actions[4];
 static char translateText[];
 mainWindow(int argc, char **argv,vector<QAStrings>& vl);
 ~mainWindow(){};
 void  Run(){XtAppMainLoop(XtC);}
 inline void InsertFields(vector<QAStrings> &);
};

char mainWindow::translateText[]=\
{"<Key>BackSpace:delete-previous-character() setIP()\n\
<Key>Left:backward-character()\n\
<Key>Right:forward-character()\n\
<Key>Down:next-line()\n\
<Key>Up:previous-line()\n\
<Key>Delete:delete-previous-character()\n\
~Ctrl ~Alt <Key>: insert-char() checkFill()\n\
 "};
String mainWindow::default_colour[]={"*background: grey90",NULL};

mainWindow::mainWindow(int argc, char **argv,vector<QAStrings>  &vl){

  actions[0].string= new char(20);
  strcpy(actions[0].string,"setIP");
  actions[0].proc=setIP;
  actions[1].string= new char(20);
  strcpy(actions[1].string,"checkFill");
  actions[1].proc=checkFill;

  XtSetArg(asciiwidArgs[0], XtNwidth,200);
  XtSetArg(asciiwidArgs[1], XtNfromHoriz,NULL);     
  XtSetArg(asciiwidArgs[2], XtNfromVert,NULL);
  XtSetArg(asciiwidArgs[3], XtNeditType,XawtextEdit);
  XtSetArg(asciiwidArgs[4], XtNautoFill,True);
  XtSetArg(asciiwidArgs[5], XtNwrap, XawtextWrapLine);
  XtSetArg(asciiwidArgs[6], XtNstring,NULL);
  XtSetArg(asciiwidArgs[7], XtNtop,XawChainTop);
  XtSetArg(asciiwidArgs[8], XtNbottom,XawChainTop);
  XtSetArg(asciiwidArgs[9], XtNleft,XawChainLeft);
  XtSetArg(asciiwidArgs[10],XtNright,XawChainLeft);
  XtSetArg(asciiwidArgs[11],XtNbackground,0xFFFFFF);
  XtSetArg(asciiwidArgs[12],XtNborderColor,0xAAAAAA);
  XtSetArg(asciiwidArgs[13],XtNcallback,NULL);
  XtSetArg(asciiwidArgs[14],XtNuseStringInPlace,False);
  XtSetArg(asciiwidArgs[15],XtNlength,200);


topshell=XtVaAppInitialize(&XtC, "Testascii",NULL,0,&argc,argv,default_colour,NULL);

win=XtVaCreateManagedWidget("mainWin",formWidgetClass, topshell,\
      XtNwidth,300, XtNheight,300,NULL);
XtAppAddActions(XtC,actions,XtNumber(actions));


InsertFields(vl);

bbox=XtVaCreateManagedWidget("Buttons",boxWidgetClass, win,\
  XtNorientation, XtorientHorizontal,\
  XtNheight,40,\
  XtNfromHoriz,NULL,\
  XtNfromVert,label,\
  XtNtop,XawChainBottom,\
  XtNbottom,XawChainBottom,\
  XtNleft,XawChainRight,\
  XtNright,XawChainRight,\
  XtNborderWidth,0,NULL);

button=XtVaCreateManagedWidget("report",commandWidgetClass,bbox,\
  XtNwidth,100,XtNheight,30,NULL);
  XtAddCallback( button , XtNcallback,WriteSB,&userinput);
button=XtVaCreateManagedWidget("cancel",commandWidgetClass,bbox,\
  XtNwidth,100,XtNheight,30,NULL);
XtAddCallback( button , XtNcallback,QuitCB,0);


XtRealizeWidget(topshell);
}

inline void mainWindow::InsertFields(vector<QAStrings> &vl){
 typedef  vector<QAStrings>::const_iterator VCI;
 for ( VCI i=vl.begin();i!=vl.end();i++){
    label=XtVaCreateManagedWidget(i->title.c_str(),labelWidgetClass, win,\
      XtNheight,(i->hints.size()>1)?25:20,\
      XtNfromHoriz,NULL,\
      XtNfromVert,NULL,\
      XtNtop,XawChainTop,XtNbottom,XawChainTop,\
      XtNleft,XawChainLeft,XtNright,XawChainLeft,\
      XtNborderWidth,0,NULL);
  if(i->hints.size()==1){
   XtSetArg(asciiwidArgs[1], XtNfromHoriz,label);    
   XtSetArg(asciiwidArgs[6],XtNstring,i->hints[0].c_str());
   userinput=XtCreateManagedWidget(i->hints[0].c_str(), asciiTextWidgetClass, win,\
      asciiwidArgs,XtNumber(asciiwidArgs));
    
   XtOverrideTranslations(userinput,XtParseTranslationTable(translateText));
  }
  else;
  }
}


int GetDefaultData(vector<QAStrings> &input){
    input[0].title="Your text:";
    input[0].hints.push_back("Thisisastringasastart");
  return 0;
}



int main (int argc , char **argv)
{

  vector <QAStrings> namesList(2);
  
  GetDefaultData(namesList);

  mainWindow interface(argc,argv,namesList);
  interface.Run();
 return 0;
}

Last edited by muggabug; 08-16-2011 at 02:56 PM. Reason: it's worse
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
losing DSL, losing GRUB controller andreas_skw Linux - Newbie 4 06-14-2008 01:36 AM
autofill extension for firefox? mma8x Linux - Software 3 04-27-2007 01:02 PM
Gv installation and Xaw3d issues arobic Linux - Software 3 10-21-2005 06:01 AM
Konqueror Autofill confusedpenguin Linux - Newbie 1 04-22-2005 06:51 AM
FC2 install hangs on Xaw3d - how to deselect? billbest Fedora - Installation 0 06-06-2004 10:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration