FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Another noob questions.

 
Post new topic   Reply to topic     Forum Index -> gtkD
View previous topic :: View next topic  
Author Message
Grez



Joined: 16 Apr 2007
Posts: 2

PostPosted: Mon Apr 16, 2007 6:07 am    Post subject: Another noob questions. Reply with quote

Hello. Im new to D and gtk(D). Seems that this is cool thing, but it lack of documentation .

I have few questions:

1. How to get to know what mouse button was clicked on Button?
2. How to add and use thing(widget) that in msWindows terminology called List ?
Back to top
View user's profile Send private message
kaarna



Joined: 03 Apr 2006
Posts: 92
Location: Finland

PostPosted: Tue Apr 17, 2007 9:57 am    Post subject: Reply with quote

I'll try to answer the first question. And leave the second one unanswered because I know nothing of windows, and I don't know what that list would be.

1. How to get to know what mouse button was clicked on Button?

Hmm. I think one method would be to overload a button. There might be other ways of doing it, but this was the one that came into my mind.

Normally a button only emits the clicked signal when it's clicked with the first mouse button. So the button doesn't actually know about other mousebuttons regarding clicking. That's why we must overload Button, and make a new class MyButton which will have the abilities to be clickable with other mousebuttons.

But, when you overload it like we'll do in the following example, you basically lose all functionality that it used to have (for some reason, I don't know why). But that functionality was simple so we'll just have to do all the stuff that happens when a button is clicked.

Hope I didn't make too many mistakes with it. And propably these buttons wouldn't work on all circumstances like a normal button would, but the basic functionality is there.

This might look confusing at first, but it's really simple:

Code:

//A gtkD example about how to handle mouse events on a button.
//Public domain. You can use it for anything.

//Phobos imports:

import std.stdio;

//gtkD imports:

private import gtk.GtkD;
private import gtkc.gtktypes;

private import gtk.MainWindow;
private import gtk.Button;
private import gtk.HButtonBox;
private import gtk.Widget;


class MyButton : public Button
{
public:
   this( char[] label )
   {
      super( label );
      
      addOnButtonPress( &onButtonPress );
      addOnButtonRelease( &onButtonRelease );
      addOnMotionNotify( &onMotionNotify );
      addOnEnterNotify( &onEnterNotify );
      addOnLeaveNotify( &onLeaveNotify );
   }
   
   uint lastClickedMouseButton;
   
protected:

   bool m_isPointerInside = false;
   bool m_buttonPressed1 = false;
   bool m_buttonPressed2 = false;
   bool m_buttonPressed3 = false;
   
   int onEnterNotify( GdkEventCrossing* event, Widget widget )
   {
      m_isPointerInside = true;
      if( m_buttonPressed1 == true || m_buttonPressed2 == true || m_buttonPressed3 == true )//the button is pressed, so we set style to active
         setState( GtkStateType.ACTIVE );//which means the button will be drawn differently.
      else setState( GtkStateType.PRELIGHT );//button is not pressed, just set to prelight.
      
      return true;
   }
   
   int onLeaveNotify( GdkEventCrossing* event, Widget widget )
   {
      m_isPointerInside = false;
      setState( GtkStateType.NORMAL );//draw the button normally.
      return true;
   }

   int onButtonPress( GdkEventButton* event, Widget widget )
   {
      switch( event.button )
      {
         default:
         break;
         case 1:
            writefln( "Mouse button 1 pressed at x: ", event.x, " y: ", event.y );
            setState( GtkStateType.ACTIVE );//draw the button as active.
            m_buttonPressed1 = true;//we set this to true, so that we know
            //when the user is pressing the button.
         break;
         case 2:
            writefln( "Mouse button 2 pressed at x: ", event.x, " y: ", event.y );
            setState( GtkStateType.ACTIVE );
            m_buttonPressed2 = true;
         break;
         case 3:
            writefln( "Mouse button 3 pressed at x: ", event.x, " y: ", event.y );
            setState( GtkStateType.ACTIVE );
            m_buttonPressed3 = true;
         break;
      }
      
      return true; //block this message from going to other widgets, we have handled it.
   }
   
   int onButtonRelease(GdkEventButton* event, Widget widget)
   {
      switch( event.button )
      {
         default:
         break;
         case 1:
            writefln( "Mouse button 1 released at x: ", event.x, " y: ", event.y );
            
            //do these anyway, even if there's no click:
            m_buttonPressed1 = false;//return to normal waiting condition.
            if( m_buttonPressed1 == false && m_buttonPressed2 == false && m_buttonPressed3 == false )
            {//we only return to normal state if none of the buttons is doing a click.
               setState( GtkStateType.NORMAL );//this will draw the button normally.
            }
            
            if( m_isPointerInside == true )//If the pointer is inside, then we click.
            {
               lastClickedMouseButton = 1;
               grabFocus();//This will grab the keyboard focus, so that this was clicked last.
               clicked();//emit the clicked signal. This calls the addOnClicked signal handlers.
            }
         break;
         case 2:
            writefln( "Mouse button 2 released at x: ", event.x, " y: ", event.y );
            
            m_buttonPressed2 = false;//return to normal waiting condition.
            if( m_buttonPressed1 == false && m_buttonPressed2 == false && m_buttonPressed3 == false )
            {//we only return to normal state if none of the buttons is doing a click.
               setState( GtkStateType.NORMAL );//this will draw the button normally.
            }
            
            if( m_isPointerInside == true )//If the pointer is inside, then we click.
            {
               lastClickedMouseButton = 2;
               grabFocus();//This will grab the keyboard focus, so that this was clicked last.
               clicked();//emit the clicked signal. This calls the addOnClicked signal handlers.
            }
         break;
         case 3:
            writefln( "Mouse button 3 released at x: ", event.x, " y: ", event.y );
            
            m_buttonPressed3 = false;//return to normal waiting condition.
            if( m_buttonPressed1 == false && m_buttonPressed2 == false && m_buttonPressed3 == false )
            {//we only return to normal state if none of the buttons is doing a click.
               setState( GtkStateType.NORMAL );//this will draw the button normally.
            }
            
            if( m_isPointerInside == true )//If the pointer is inside, then we click.
            {
               lastClickedMouseButton = 3;
               grabFocus();//This will grab the keyboard focus, so that this was clicked last.
               clicked();//emit the clicked signal. This calls the addOnClicked signal handlers.
            }
         break;
      }
      
      return true; //block this message from going to other widgets, we have handled it.
   }
   
   int onMotionNotify(GdkEventMotion* event, Widget widget)
   {
      if( event.state & GdkModifierType.BUTTON1_MASK )
      {
         //writefln( "Mouse motion when button 1 pressed at x: ", event.x, " y: ", event.y );
      }
      else if( event.state & GdkModifierType.BUTTON2_MASK )
      {
         //writefln( "Mouse motion when button 2 pressed at x: ", event.x, " y: ", event.y );
      }
      else if( event.state & GdkModifierType.BUTTON3_MASK )
      {
         //writefln( "Mouse motion when button 3 pressed at x: ", event.x, " y: ", event.y );
      }
      
      return true; //block this message from going to other widgets, we have handled it.
   }

}

class MyApp : public MainWindow
{
public:

   this()
   {
      super( "A small test to show how to handle mouse in gtkD and buttons." );
      
      m_testHButtonBox = new HButtonBox();
      
      m_overloadedButton = new MyButton( "Overloaded button" );
      m_compareToNormalButton = new Button( "Normal button" );
      
      m_testHButtonBox.packStart( m_overloadedButton, false, false, 0 );
      m_testHButtonBox.packStart( m_compareToNormalButton, false, false, 0 );
      
      m_overloadedButton.addOnClicked( &onOverloadedClicked );
      m_compareToNormalButton.addOnClicked( &onNormalClicked );
      
      add( m_testHButtonBox );
      
      showAll();
      
   }
   
   void onOverloadedClicked( Button button )
   {
      uint but_num = (cast(MyButton)button).lastClickedMouseButton;
      
      writefln("An overloaded button was clicked with mouse button number ", but_num, "." );
   }
   
   void onNormalClicked( Button button )
   {
      writefln("A normal button was clicked.");
   }

protected:
   
   HButtonBox m_testHButtonBox;//ButtonBox keeps makes it's buttons
   //look similar.
   //Use HBox instead, if you want to put some other widget's there too.
   
   MyButton m_overloadedButton;
   Button m_compareToNormalButton;
   
}
   
void main(char[][] args)
{
   GtkD.init(args);
   new MyApp();
   GtkD.main();
}




And here's a compd script to make it compile. Put this inside .sh script and make it executable from e.g. Nautilus. And modify it according to where you've put all the files. There's propably a better way to make it compile than compd, but I don't know about them so:

Code:

compd \
   demos/gtk/button_mouse_test.d \
   -o button_mouse_test \
   -c \
   -I src \
   -I demos \
   -c \
   -L .. \
   -l phobos \
   -L . -l gtkd \
   -l dl
Back to top
View user's profile Send private message AIM Address MSN Messenger
Grez



Joined: 16 Apr 2007
Posts: 2

PostPosted: Wed Apr 18, 2007 3:00 pm    Post subject: Reply with quote

Thanks a lot.
Just seems strange that this functionality not added by default to gtkD.

About question 2 - I've found what i need: In "GtkD tests" test program present component called ListView. This is exactly what I need.
I will try to search in gtkD test sources for this component usage.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> gtkD All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group