window_lnx.cpp.svn-base

来自「非结构化路识别」· SVN-BASE 代码 · 共 914 行 · 第 1/2 页

SVN-BASE
914
字号
    XtVaSetValues( window->frame, XmNx, x, XmNy, y, 0 );

    __END__;
}


static CvTrackbar*
icvFindTrackbarByName( const CvWindow* window, const char* name )
{
    CvTrackbar* trackbar = window->toolbar.first;

    for( ; trackbar != 0 && strcmp( trackbar->name, name ) != 0; trackbar = trackbar->next )
        ;

    return trackbar;
}


HIGHGUI_IMPL int
cvCreateTrackbar( const char* trackbar_name, const char* window_name,
                  int* val, int count, CvTrackbarCallback on_notify )
{
    int result = 0;

    CV_FUNCNAME( "cvCreateTrackbar" );

    __BEGIN__;
    
    /*char slider_name[32];*/
    CvWindow* window = 0;
    CvTrackbar* trackbar = 0;

    if( !window_name || !trackbar_name )
        CV_ERROR( CV_StsNullPtr, "NULL window or trackbar name" );

    if( count <= 0 )
        CV_ERROR( CV_StsOutOfRange, "Bad trackbar maximal value" );

    window = icvFindWindowByName(window_name);
    if( !window )
        EXIT;

    trackbar = icvFindTrackbarByName(window,trackbar_name);
    if( !trackbar )
    {
        int height = 0, theight = 50;
        int len = strlen(trackbar_name);
        Arg args[16];
        int n = 0;
        XmString xmname = XmStringCreateLocalized( (String)trackbar_name );
        CvTrackbar* t;
        int tcount = 0;
        
        trackbar = (CvTrackbar*)cvAlloc(sizeof(CvTrackbar) + len + 1);
        memset( trackbar, 0, sizeof(*trackbar));
        trackbar->signature = HG_TRACKBAR_SIGNATURE;
        trackbar->name = (char*)(trackbar+1);
        memcpy( trackbar->name, trackbar_name, len + 1 );
        trackbar->parent = window;
        trackbar->next = window->toolbar.first;
        window->toolbar.first = trackbar;
        XtSetArg( args[n], XmNorientation, XmHORIZONTAL ); n++;
        XtSetArg(args[n], XmNtitleString, xmname ); n++;
        XtSetArg(args[n], XmNshowValue, True); n++;
        
        trackbar->widget = XmCreateScale( window->trackbars, (char*)trackbar_name, args, n );
        XmStringFree( xmname );
        
        for( t = window->toolbar.first; t != 0; t = t->next )
            tcount++;
            
        
        XtAddCallback( trackbar->widget, XmNdragCallback,
                       icvTrackbarJumpProc, (XtPointer)trackbar );
        XtAddCallback( trackbar->widget, XmNvalueChangedCallback,
                       icvTrackbarJumpProc, (XtPointer)trackbar );
                       
        XtVaSetValues( window->trackbars, XmNpaneMinimum,
                       theight*tcount, 0 );
        XtVaGetValues( window->frame, XmNheight, &height, 0 );
        XtVaSetValues( window->frame, XmNheight, height + theight, 0 );
        XtManageChild( trackbar->widget );
    }
    
    XtVaSetValues( trackbar->widget, XmNmaximum, count, 0 );

    trackbar->data = val;
    trackbar->pos = 0;
        
    if( val )
    {
        int value = *val;
        if( value < 0 )
            value = 0;
        if( value > count )
            value = count;
        XtVaSetValues( trackbar->widget, XmNvalue, value, 0 );
        trackbar->pos = value;
    }
        
    trackbar->maxval = count;
    trackbar->notify = on_notify;

    result = 1;

    __END__;

    return result;
}


HIGHGUI_IMPL void
cvSetMouseCallback( const char* window_name, CvMouseCallback on_mouse )
{
    CV_FUNCNAME( "cvSetMouseCallback" );

    __BEGIN__;
    
    CvWindow* window = 0;

    if( !window_name )
        CV_ERROR( CV_StsNullPtr, "NULL window name" );

    window = icvFindWindowByName(window_name);
    if( !window )
        EXIT;

    window->on_mouse = on_mouse;

    __END__;
}


HIGHGUI_IMPL int cvGetTrackbarPos( const char* trackbar_name, const char* window_name )
{
    int pos = -1;
    
    CV_FUNCNAME( "cvGetTrackbarPos" );

    __BEGIN__;

    CvWindow* window;
    CvTrackbar* trackbar = 0;

    if( trackbar_name == 0 || window_name == 0 )
        CV_ERROR( CV_StsNullPtr, "NULL trackbar or window name" );

    window = icvFindWindowByName( window_name );
    if( window )
        trackbar = icvFindTrackbarByName( window, trackbar_name );

    if( trackbar )
        pos = trackbar->pos;

    __END__;

    return pos;
}


HIGHGUI_IMPL void cvSetTrackbarPos( const char* trackbar_name, const char* window_name, int pos )
{
    CV_FUNCNAME( "cvSetTrackbarPos" );

    __BEGIN__;

    CvWindow* window;
    CvTrackbar* trackbar = 0;

    if( trackbar_name == 0 || window_name == 0 )
        CV_ERROR( CV_StsNullPtr, "NULL trackbar or window name" );

    window = icvFindWindowByName( window_name );
    if( window )
        trackbar = icvFindTrackbarByName( window, trackbar_name );

    if( trackbar )
    {
        if( pos < 0 )
            pos = 0;

        if( pos > trackbar->maxval )
            pos = trackbar->maxval;
    }
    XtVaSetValues( trackbar->widget, XmNvalue, pos, 0 );

    __END__;
}


HIGHGUI_IMPL void* cvGetWindowHandle( const char* window_name )
{
    void* widget = 0;
    
    CV_FUNCNAME( "cvGetWindowHandle" );

    __BEGIN__;

    CvWindow* window;

    if( window_name == 0 )
        CV_ERROR( CV_StsNullPtr, "NULL window name" );

    window = icvFindWindowByName( window_name );
    if( window )
        widget = (void*)window->area;

    __END__;

    return widget;
}
    

HIGHGUI_IMPL const char* cvGetWindowName( void* window_handle )
{
    const char* window_name = "";
    
    CV_FUNCNAME( "cvGetWindowName" );

    __BEGIN__;

    CvWindow* window;

    if( window_handle == 0 )
        CV_ERROR( CV_StsNullPtr, "NULL window" );

    window = icvWindowByWidget( (Widget)window_handle );
    if( window )
        window_name = window->name;

    __END__;

    return window_name;
}


/* draw image to frame */
static void icvPutImage( CvWindow* window )
{
    int width = 10, height = 10;
    Assert( window != 0 );
    
    if( window->image == 0 )
        return;

    XtVaGetValues( window->area, XtNwidth, &width, XtNheight, &height, 0 );
   
    if( width <= 0 || height <= 0 )
        return;

    if( window->flags )
    {
        width = MIN(width, window->image->width);
        height = MIN(height, window->image->height);
    }
    
    if( window->dst_image &&
        (window->dst_image->width  < width  ||
         window->dst_image->height < height) )
    {
        /*cvFree( (void**)&window->dst_image->data );*/
        
        XShmDetach( XtDisplay(window->area), window->xshmseg );
        XDestroyImage( window->dst_image );
        shmdt( window->xshmseg->shmaddr );
        shmctl( window->xshmseg->shmid, IPC_RMID, NULL );        
        window->dst_image = 0;
    }

    if( !window->dst_image )
    {
        int new_width = (width + 7) & -8;
        int new_height = (height + 7) & -8;

        /*int step = (new_width * (window->image->depth / 8) + 3) & ~3;
        char* data = (char*)cvAlloc( step * new_height );
        window->dst_image = XCreateImage( XtDisplay(window->area),
                                          CopyFromParent,
                                          window->image->depth,
                                          ZPixmap, 0, data, new_width, new_height,
                                          8, step );
        window->dst_image->bits_per_pixel = window->dst_image->depth;*/

        Display* display = XtDisplay(window->frame);
        Visual* visual = DefaultVisualOfScreen(DefaultScreenOfDisplay(display));
        int depth = DefaultDepth(display, 0);
        
        window->dst_image = XShmCreateImage( display, visual, depth, ZPixmap, NULL, window->xshmseg,
                                             new_width, new_height );
        
        window->xshmseg->shmid = shmget(IPC_PRIVATE,
                                        window->dst_image->height*window->dst_image->bytes_per_line,
                                        IPC_CREAT|0777);
        assert( window->xshmseg->shmid != -1);
        window->xshmseg->shmaddr = window->dst_image->data= (char*)shmat(window->xshmseg->shmid, 0, 0) ;
        window->xshmseg->readOnly = False;
        XShmAttach( display, window->xshmseg );
    }
    
    {
        CvMat src;
        CvMat dst;
        int type = ((window->image->depth/8) - 1)*8 + CV_8U;
        
        OPENCV_CALL( cvInitMatHeader( &dst, height, width, type, window->dst_image->data,
                                      window->dst_image->bytes_per_line ));

        if( window->flags )
        {
            OPENCV_CALL( cvInitMatHeader( &src, height, width,
                                          type, window->image->data, window->image->bytes_per_line ));
            OPENCV_CALL( cvCopy( &src, &dst, 0 ));
        }
        else
        {
            OPENCV_CALL( cvInitMatHeader( &src, window->image->height, window->image->width,
                                          type, window->image->data, window->image->bytes_per_line ));
            OPENCV_CALL( cvResize( &src, &dst, CV_INTER_NN ));
        }
    }

    {
    Display* display = XtDisplay(window->area);
    GC gc = XCreateGC( display, XtWindow(window->area), 0, 0 );
    XShmPutImage( display, XtWindow(window->area), gc,
                  window->dst_image, 0, 0, 0, 0,
                  width, height, False );
    XFlush( display );
    XFreeGC( display, gc );
    }
}                                                   
                                                    

static void icvDrawingAreaCallback( Widget widget, XtPointer client_data, XtPointer call_data)
{
    XmDrawingAreaCallbackStruct *cbs = (XmDrawingAreaCallbackStruct *) call_data;
    XEvent *event = cbs->event;
    CvWindow* window = (CvWindow*)client_data;
    
    if (cbs->reason == XmCR_INPUT)
    {
        if( (event->xany.type == ButtonPress || event->xany.type == ButtonRelease) &&
            window->on_mouse && window->image &&
            widget == window->area )
        {
            int event_type = (event->xany.type == ButtonPress ?
                            CV_EVENT_LBUTTONDOWN : CV_EVENT_LBUTTONUP) +
                            (event->xbutton.button == Button1 ? 0 :
                             event->xbutton.button == Button2 ? 1 : 2);
            int flags = (event->xbutton.state & Button1Mask ? CV_EVENT_FLAG_LBUTTON : 0) |
                        (event->xbutton.state & Button2Mask ? CV_EVENT_FLAG_RBUTTON : 0) |
                        (event->xbutton.state & Button3Mask ? CV_EVENT_FLAG_MBUTTON : 0) |
                        (event->xbutton.state & ShiftMask ? CV_EVENT_FLAG_SHIFTKEY : 0) |
                        (event->xbutton.state & ControlMask ? CV_EVENT_FLAG_CTRLKEY : 0) |
                        (event->xbutton.state & (Mod1Mask|Mod2Mask) ? CV_EVENT_FLAG_ALTKEY : 0);
            int x = event->xbutton.x;
            int y = event->xbutton.y;
            int width = 0, height = 0;
            
            XtVaGetValues( window->area, XmNwidth, &width, XmNheight, &height, 0 );
            
            x = cvRound(((double)x)*window->image->width/MAX(width,1));
            y = cvRound(((double)y)*window->image->height/MAX(height,1));
            
            if( x >= window->image->width )
                x = window->image->width - 1;
            
            if( y >= window->image->height )
                y = window->image->height - 1;
            
            if( window->on_mouse )
                window->on_mouse( event_type, x, y, flags );
        }
        if( event->xany.type == KeyPress )
        {
            KeySym key;
            XLookupString( &event->xkey, 0, 0, &key, 0 );
            if( !IsModifierKey(key) )
            {
                last_key = key;
                if( timer )
                {
                    XtRemoveTimeOut( timer );
                    timer = 0;
                }
            }
        }
    }
    
    if( (cbs->reason == XmCR_EXPOSE || cbs->reason == XmCR_RESIZE) && window->image )
    {
        icvPutImage( window );
    }
    
    XmProcessTraversal( window->area, XmTRAVERSE_CURRENT );
}



static void icvCloseWindow( Widget w, XEvent* event, String* params, Cardinal* num_params )
{
    CvWindow* window = icvWindowByWidget( w );
    if( window )
        icvDeleteWindow( window );
}


static void icvAlarm( XtPointer, XtIntervalId* )
{
    timer = 0;
}


static void icvTrackbarJumpProc( Widget w, XtPointer client_data, XtPointer call_data )
{
    XmScaleCallbackStruct *cbs = (XmScaleCallbackStruct *)call_data;
    
    Assert( client_data != 0 );
    Assert( call_data != 0 );

    CvTrackbar* trackbar = (CvTrackbar*)client_data;
    int value = cbs->value;
    
    if( trackbar->data )
        *trackbar->data = value;

    if( trackbar->notify )
        trackbar->notify( value );
    
    XmProcessTraversal( trackbar->parent->area, XmTRAVERSE_CURRENT );
}


int cvWaitKey( int delay )
{
    int exit_flag = 0;
    
    last_key = -1;
    if( delay > 0 )
        timer = XtAppAddTimeOut( appContext, delay, icvAlarm, &timer );
    
    do
    {
        XtAppProcessEvent( appContext, XtIMAll );
    }
    while( last_key < 0 && (delay <= 0 || timer != 0)
           && (exit_flag = XtAppGetExitFlag( appContext )) == 0
           && hg_windows != 0 );
    
    if( timer )
        XtRemoveTimeOut( timer );
    
    return last_key;
}

#endif /* WIN32 */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?