Scripts/Splitter

From J Wiki
Jump to: navigation, search

Template:Tick

Splitter control for dynamically resizing boundary between two or more controls. It has a vertical and horizontal variety. It is capable of having multiple splitters in one window.

(!) The splitreal variant will resize the controls in real time, while you are dragging.

Constructor boxed parameters:
   [0] splitter control ID
   [1] left/top adjacent control ids (space delimited string or boxed list)
   [2] right/bottom adjacent control ids

Horizontal variety is signaled by the trailing h in the splitter id.

Information.png There is an inherent "feature" of J wd driver that it resets control positions when autoresizing, as mentioned in JForum:general/2003-October/014971.

It is very easy to use, as event hook-up is done automatically. For example, the following window is produced by the script below:

Split.png

Implementation

Below is the component code rendered in Literate style.

There are two variants: line dragging and real-time resize. Most of the code is the same, so only the differences are defined separately. Some definitions are only present in one variant.

Line Dragging

This verion will drag a line before resize, which happens on mouse release.

It is very easy to use, as event hook-up is done automatically. For example, the following window is produced by the script below:

Split.png

Implementation

Below is the component code rendered in Literate style.

There are two variants: line dragging and real-time resize. Most of the code is the same, so only the differences are defined separately. Some definitions are only present in one variant.

Line Dragging

This verion will drag a line before resize, which happens on mouse release. Download script:

NB. split.ijs - splitter control
«main»

Real-Time Resize

In real-time resize, the related controls are resized while the mouse is still dragging the splitter. Download script:

NB. splitreal.ijs - splitter control, real-time resize
«main»

Common

The main structure of the component. Download script:

NB. http://www.jsoftware.com/pipermail/general/2003-October/014971.html
NB. 07/14/2003 Oleg Kobchenko
NB. 07/15/2006 Oleg Kobchenko - j601, componentized
NB. 08/16/2006 Oleg Kobchenko - real-time resize
NB. 03/23/2007 Oleg Kobchenko - literate

«imports and locale declarations»

«globals»

«create and destroy»

«events»

«logic»

jgl2 locale is inserted for access to 2D routines. Download script:

require'gl2'

coclass 'psplit'
coinsert'jgl2'

Here we define utility verbs and global configurable settings. Download script:

wfopen=: ;:^:(0=L.)
crot=: [:  ,  _2 |.\  ]
irot=: [: ,./ _2 ]\"1 ]

FG=: 3#127
BG=: 210 200 190

Create parameters include control ID, controls before and controls after. Control events are assigned in the parent form. Download script:

create=: 3 : 0
  HZ=:'h'={: ID=: 0{::y
  LC=:  wfopen    1{::y
  RC=:  wfopen    2{::y
  nm=. >coname''
  pn=. >{:<;._2 wd'qp'
  (pn,'_',ID,'_paint__COCREATOR')   =: ('paint_',  nm,'_')~
  (pn,'_',ID,'_mbldown__COCREATOR') =: ('mbldown_',nm,'_')~
  (pn,'_',ID,'_mblup__COCREATOR')   =: ('mblup_',  nm,'_')~
  «move event»
  CAPT=: ''
)

destroy=: codestroy

For realsplit we track mouse move events. Download script:

(pn,'_',ID,'_mmove__COCREATOR')   =: ('mmove_',  nm,'_')~

and need a corresponding event handler. Download script:

mmove=: 3 : 0
  if. 0=#CAPT do. return. end.
  move (HZ{".sysdata__COCREATOR) - CAPT
)

Handlers of the events assigned in parent form. Download script:

paint=: 3 : 0
  glclear''
  glcursor HZ { IDC_SIZEWE,IDC_SIZENS
  glpen 0,0 [ glbrush glrgb BG
  glrect 0 0 , glqwh''
  glpen 1,0 [ glrgb FG
  wh=. <. , (|.^:HZ 0.25 0.75,:0.4 0.6) * glqwh''
  gllines (irot^:HZ 0 2  0 3 ,:1 2 1 3) { wh
)

mbldown=: 3 : 0
  glsel ID      NB. is it really necessary?
  «set capture»
  CAPT=: HZ{".sysdata__COCREATOR
)

mblup=: 3 : 0
  if. 0=#CAPT do. return. end.
  glcapture 0
  «release capture»
  CAPT=: ''
)

«mouse move»

where in line dragging split we show horizontal or vertical line Download script:

glcapture 5+HZ

moving the controls only on release Download script:

move (HZ{".sysdata__COCREATOR) - CAPT

and hidden capture for real-time resize, Download script:

glcapture 1

while nothing needs to be done on release.

The logic supports event processing. Download script:

move=: 3 : 0
     LC setp crot^:HZ 0 0, y, 0
 (< ID) setp crot^:HZ y,0  0  0
     RC setp crot^:HZ y,0,-y, 0
)

setp=: 4 : 0"0 1
  pos=. ".wd'qchildxywhx ',>x
  wd'setxywhx ',(>x),' ',":pos+y
)

Example

Usage is the same for both versions. Download script:

require '~user/split.ijs'   NB. or 'splitreal.ijs'

TEST=: 0 : 0
pc test;
xywh 6 6 44 61;cc ok button bottommove;cn "OK";
xywh 6 69 44 14;cc b1 button topmove bottommove;
xywh 6 86 44 14;cc b2 button topmove bottommove;
xywh 51 6 3 94;cc splitv isigraph bottommove;
xywh 55 6 111 28;cc b3 button rightmove;
xywh 169 6 31 28;cc b4 button leftmove rightmove;
xywh 54 35 146 3;cc splith isigraph rightmove;
xywh 55 39 145 62;cc cancel button rightmove bottommove;cn "Cancel";
pas 6 6;pcenter;
rem form end;
)

test_run=: 3 : 0
  wd TEST
  spv=: 'psplit' conew~ 'splitv';'ok b1 b2';'b3 splith cancel'
  sph=: 'psplit' conew~ 'splith';'b3 b4'   ;'cancel'
  wd 'pshow;'
)

test_close=: 3 : 0
  destroy__spv''
  destroy__sph''
  wd'pclose'
)

test_run''


Contributed by Oleg Kobchenko