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

CloseableTabControl

 
Post new topic   Reply to topic     Forum Index -> DFL
View previous topic :: View next topic  
Author Message
soywiz



Joined: 21 Oct 2005
Posts: 6

PostPosted: Sun Jun 17, 2007 11:39 am    Post subject: CloseableTabControl Reply with quote

I made an extension for the default TabControl to allow closing tabs with mouse middle button.

I release it to public domain so, if you like it, you can use it in any form, modify it etc.

Sorry about my bad english, I'm spanish.

Code:

(Fixed a problem: 19/06/2007)
Code:
class CloseableTabEventArgs : EventArgs {
   this(CloseableTabControl ctb, int tabIndex, TabPage tabPage) {
      _ctb = ctb;
      _tabIndex = tabIndex;
      _tabPage = tabPage;
   }

   int tabIndex() { return _tabIndex; }
   TabPage tabPage() { return _tabPage; }

   void allow(bool allowClose = true) {
      _ctb.allowClose = allowClose;
   }

   public CloseableTabControl _ctb;
   private int _tabIndex;
   private TabPage _tabPage;
}

class CloseableTabControl : TabControl {
   Event!(CloseableTabControl, CloseableTabEventArgs) beforeCloseTab;
   Event!(CloseableTabControl, CloseableTabEventArgs) afterCloseTab;

   bool allowClose;

   void onMouseDown(MouseEventArgs mea) {
      scope (exit) super.onMouseDown(mea);

      // Check that the button pressed was the middle one
      if (mea.button != MouseButtons.MIDDLE) return;

      // Performs a binary search to determine which tab is affected
      int max = tabCount, min = 0, cur, mcyc = 10;
      bool found = false;

      while (min != max + 1 && mcyc-- > 0) {
         cur = (max + min) / 2;
         Rect cs = getTabRect(cur);
         if (cs.contains(mea.x, mea.y)) { found = true; break; }
         if (mea.x < cs.x) max = cur - 1; else min = cur + 1;
      }

      // Checks that found a tab
      if (!found) return;

      allowClose = true;

      TabPage tab = tabPages[cur];

      beforeCloseTab(this, new CloseableTabEventArgs(this, cur, tab));

      if (!allowClose) return;

      if (cur == selectedIndex) selectedIndex = ((selectedIndex > 0) ? selectedIndex : tabCount) - 1;
      tabPages.removeAt(cur);

      afterCloseTab(this, new CloseableTabEventArgs(this, -1, tab));

      onSelectedIndexChanged(EventArgs.empty);
   };
}

/*
   beforeCloseTab ~= delegate void(CloseableTabControl c, CloseableTabEventArgs ctea) {
      ctea.allow = (msgBox("Close '" ~ ctea.tabPage.text ~ "' tab?", "Warning", MsgBoxButtons.OK_CANCEL, MsgBoxIcon.ASTERISK) == DialogResult.OK);
   };

   afterCloseTab ~= delegate void(CloseableTabControl ctc, CloseableTabEventArgs ctea) {
      ctc.selectedTab.focus();
      msgBox("Tab Cloed");
   };
*/


Best regards
Back to top
View user's profile Send private message
Chris Miller



Joined: 27 Mar 2004
Posts: 514
Location: The Internet

PostPosted: Fri Jul 06, 2007 11:07 am    Post subject: Re: CloseableTabControl Reply with quote

It doesn't seem to work right; sometimes it prompts to close the wrong tab or even causes an access violation.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> DFL 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