// ( m4.C ) // コ−ルバック関数に複数の引数を渡す方法 // #include #include #include extern class Push ; struct A { char str[20] ; Push* btn ; }; class Push { /* 注:ここは A cb 定義でいいと思う */ friend void push_cb( Widget,caddr_t,caddr_t ) ; Widget motif ; A cb ; // A* cb [ポインタを使う場合] public: Push( Widget,int,int,char* ) ; ~Push() { XtDestroyWidget( motif ) ; } // delete cb 追加 void pushCB( char* ) ; } ; Push::Push( Widget wid,int x,int y,char* cmd ) { Arg args[2] ; // cb = new A, cb->str, cb->btn strcpy( cb.str,cmd ) ; cb.btn = this ; XtSetArg( args[0],XmNx,x ) ; XtSetArg( args[1],XmNy,y ) ; motif = XmCreatePushButton( wid,"PB",args,2 ) ; XtManageChild( motif ) ; XtAddCallback( motif,XmNactivateCallback,(XtCallbackProc)push_cb,(caddr_t)&cb ) ; } // (caddr_t)cb void Push::pushCB( char* str ) { cout << "Push :" << str << endl ; cout.flush() ; } void push_cb( Widget ww,caddr_t myself,caddr_t event ) { A *a = (A*)myself ; Push* push = (Push*)(a->btn) ; if ( push ) push->pushCB( a->str ) ; } static String fallback[] = { "*BB.x : 0","*BB.y : 0", "*BB.width: 250","*BB.height: 200", NULL } ; void main( int argc,char **argv ) { XtAppContext appCon ; Widget top,bb ; top = XtAppInitialize( &appCon,NULL,NULL,0,(Cardinal*)&argc,argv, fallback,NULL,0 ) ; bb = XmCreateBulletinBoard( top,"BB",NULL,0 ) ; XtManageChild( bb ) ; Push a( bb, 50,50,"hello katou" ) ; Push b( bb,150,50,"hello haruo" ) ; XtRealizeWidget( top ) ; XtAppMainLoop( appCon ) ; }