home *** CD-ROM | disk | FTP | other *** search
- # $Header: P:/source/ppee/macros/mouse.pev 1.96 11 Jun 1990 13:00:14 JB $
-
- ##############################################################################
- #
- # Sage Software - POLYTRON Division
- # 1700 NW 167th Place
- # Beaverton, OR 97006
- #
- # Copyright 1990, Sage Software, Inc.
- #
- # Permission is hereby granted for licensed users of Sage Professional
- # Editor and PolyAwk to copy and modify this source code for their own
- # personal use. These derivative works may be distributed only to other
- # licensed Sage Professional Editor and PolyAwk users. All other usage
- # is prohibited without express written permission from Sage Software.
- #
- ##############################################################################
-
- #### $Workfile: mouse.pel $: mouse support
-
- global mouse_selection_type = NORMAL_SELECTION
-
- global LEFT_BUTTON = 1
- global RIGHT_BUTTON = 2
- global BOTH_BUTTONS = 3
- global MIDDLE_BUTTON = 4
-
- global LEFT_PRESS = 0xf000
- global RIGHT_PRESS = 0xf100
- global MIDDLE_PRESS = 0xf200
- global LEFT_CLICK = 0xf600
- global RIGHT_CLICK = 0xf700
- global MIDDLE_CLICK = 0xf800
-
- ## augment scan codes in keys.pel
- global SCANCODE_LEFT_PRESS = 0xf0
- global SCANCODE_RIGHT_PRESS = 0xf1
- global SCANCODE_LEFT_CLICK = 0xf6
- global SCANCODE_RIGHT_CLICK = 0xf7
-
- ## assign mouse button events to the usual functions...
-
- function assign_mouse_buttons(){ #PUBLIC #VOID
- assign_key( "#61440", "left_press" )
- assign_key( "#61696", "right_press" )
- assign_key( "#61952", "middle_press" )
-
- assign_key( "#62208", "left_release" )
- assign_key( "#62464", "right_release" )
- assign_key( "#62720", "middle_release" )
-
- assign_key( "#62976", "left_click" )
- assign_key( "#63232", "right_click" )
- assign_key( "#63488", "middle_click" )
-
- assign_key( "#63744", "left_double_click" )
- assign_key( "#64000", "right_double_click" )
- assign_key( "#64256", "middle_double_click" )
- }
-
-
- ## the mouse's position falls into one of the following categories,
- # accurately computed by mouse_position():
- #
- global MOUSE_IN_BACKGROUND = 0
- global MOUSE_IN_TEXT = 0x10
- global MOUSE_IN_NUMBERS = 0x20
- global MOUSE_N = 0x01
- global MOUSE_E = 0x02
- global MOUSE_S = 0x04
- global MOUSE_W = 0x08
- global MOUSE_IGNORE = 0x40
-
- # composites:
-
- global MOUSE_LEFT = 0x28 # WEST or IN_NUMBERS
- global MOUSE_LEFTR = 0x2A # WEST or IN_NUMBERS or right
- #global MOUSE_LR = 0x2A # left or right
- global MOUSE_IN_BORDER = 0x2F # anywhere in border
- #global MOUSE_NE = 0x03 # NE corner
- global MOUSE_SE = 0x06
- global MOUSE_NW = 0x09
- global MOUSE_SW = 0x0C
-
- ## The following are set by mouse_position and used by its clients:
-
- local x, y, mw, mouse_pos
- local x0, y0, x1, y1 # these are computed only if MOUSE_IN_BORDER
- local mouse_prev_window
- local mouse_prev_buffer
-
- local highlighted_scroll = 0; # if set, raise anchor and drop line
- # selection for every vertical movement
-
-
- ## compute qualitatively the current mouse position.
- # ensures that if a window contains the mouse it becomes current
- #
- global function mouse_position( include_system_win ){
-
- x = mouse_event_x
- y = mouse_event_y
- mouse_prev_window = current_window
- mouse_prev_buffer = current_buffer
-
- mw = window_containing( x, y )
-
- if( !mw )
- return mouse_pos = MOUSE_IN_BACKGROUND
-
- current_window = mw
-
- if ( current_window != mw ) {
- # EVENT_NEW_CURNT_WINDOW may have changed the current window
- mouse_pos = MOUSE_IGNORE
-
- } else if( !include_system_win && and( window_flags, WINDOW_SYSTEM )){
- assign_current_window( mouse_prev_window )
- bury_window( mw )
- mouse_pos = MOUSE_IN_BACKGROUND
-
- } else if( window_border_contains( x, y, mw )){
- x0 = window_x0
- y0 = window_y0
- x1 = x0 + window_width - 1
- y1 = y0 + window_height - 1
-
- mouse_pos = 0
-
- if( x == x0 )
- mouse_pos += MOUSE_W
- else if( x == x1 )
- mouse_pos += MOUSE_E
-
- if( y == y0 )
- mouse_pos += MOUSE_N
- else if( y == y1 )
- mouse_pos += MOUSE_S
-
- if( mouse_pos == 0 \
- && linenumber_format \
- && x < window_text_x0 ) \
- mouse_pos += MOUSE_IN_NUMBERS
-
- if( !mouse_pos )
- play( "o6g" )
- } else
- mouse_pos = MOUSE_IN_TEXT
-
- return mouse_pos
- }
-
- ## perform mouse scroll bar action if scroll bars are enabled and the mouse
- # is within a scroll bar
-
- function mouse_in_scrollbar(){
-
- if(( and( mouse_pos, MOUSE_E ) \
- && and( window_flags, WINDOW_SB_RIGHT )) \
- || ( and( mouse_pos, MOUSE_W ) \
- && and( window_flags, WINDOW_SB_LEFT ))){
-
- if( y == y0 || y == y1 )
- return 0
-
- if( y == y0 + 1 ) # up arrow
- dynamic_scroll_vertical( -1 )
-
- else if( y == y0 + 2 ) # up diamond
- dynamic_page_up()
-
- else if( y == y1 - 2 ) # down diamond
- dynamic_page_down()
-
- else if( y == y1 - 1 ) # down arrow
- dynamic_scroll_vertical( +1 )
-
- else # elevator
- dynamic_elevator()
-
- return 1
- }
-
- if( and( mouse_pos, MOUSE_S ) \
- && and( window_flags, WINDOW_SB_BELOW )){
-
- if( x == x0 || x == x1 )
- return 0
-
- if( x == x0 + 1 ) # left arrow
- dynamic_scroll_horizontal( -1 )
-
- else if( x == x0 + 2 ) # left diamond
- dynamic_scroll_horizontal( -8 )
-
- else if( x == x1 - 2 ) # right diamond
- dynamic_scroll_horizontal( +8 )
-
- else if( x == x1 - 1 ) # right arrow
- dynamic_scroll_horizontal( +1 )
-
- else # elevator
- dynamic_flat_elevator()
-
- return 1
- }
-
- return 0
- }
-
- ### principal mouse actions
-
- ## left button
-
- local CTRL_KEY = 4
- local ALT_KEY = 8
- local SHIFT_KEYS = 3
-
- function left_press( include_system_win ){
- local where = mouse_position( include_system_win )
- local seltype
-
- if( where )
- if( and( where, MOUSE_IN_BORDER ))
- left_press_border( where )
- else if ( and( where, MOUSE_IGNORE ))
- return;
- else {
- if( and( keyboard_flags, CTRL_KEY ))
- move_selection()
-
- else if( and( keyboard_flags, SHIFT_KEYS ))
- copy_selection()
-
- else {
- seltype = and( keyboard_flags, ALT_KEY ) \
- ? COLUMN_SELECTION \
- : mouse_selection_type
- dynamic_selection( seltype, LEFT_BUTTON )
- }
- }
- else if (current_key != LEFT_CLICK)
- mouse_create_window()
- }
-
- function left_release(){
- }
-
- function left_click(){
- left_press();
- }
-
- function left_double_click(){
- left_press()
- }
-
-
- ## right button
-
- function right_press( include_system_win ){
- local where = mouse_position( include_system_win )
-
- if( where )
- if( and( where, MOUSE_IN_BORDER ))
- right_press_border( where )
- else
- dynamic_selection( COLUMN_SELECTION, RIGHT_BUTTON )
- else
- beep()
- }
-
- function right_release(){
- }
-
- function right_click(){
- right_press()
- }
-
- function right_double_click(){
- right_press()
- }
-
-
- ## middle button
-
- function middle_press( include_system_win ){
- local where = mouse_position( include_system_win )
- local seltype
-
- if( where )
- if( !and( where, MOUSE_IN_BORDER )){
- seltype = and( keyboard_flags, 3 ) \
- ? COLUMN_SELECTION \
- : mouse_selection_type
- dynamic_selection( seltype, LEFT_BUTTON )
- } else if( and( where, MOUSE_LEFT )){ # left margin
- dynamic_selection( LINE_SELECTION, LEFT_BUTTON )
- }
- }
-
- function middle_release(){
- playback()
- }
-
- function middle_click(){
- beep()
- }
-
- function middle_double_click(){
- beep()
- }
-
-
- ### mouse button helper routines
-
- local function left_press_border( where ){
-
- if( where == MOUSE_NW ){ # upper left corner
- smaller_window()
- mouse_display_x = window_x0
- mouse_display_y = window_y0
-
- } else if( and( where, MOUSE_N )){ # title bar
- dynamic_window_move()
-
- } else if( mouse_in_scrollbar()){ # any scroll-bar
-
- # (appropriate action performed by mouse_in_scrollbar())
-
- } else if( where == MOUSE_SE ){ # lower right corner
- dynamic_window_resize()
-
- } else if( where == MOUSE_SW ){ # lower left corner
- play( "o6g" )
-
- } else if( and( where, MOUSE_LEFTR )){ # left or right margin
- dynamic_selection( LINE_SELECTION, LEFT_BUTTON )
-
- } else { # can't (?) happen
- play( "o6g" )
- }
- }
-
- local function right_press_border( where ){
-
- if( where == MOUSE_NW ){ # upper left corner
- larger_window()
- mouse_display_x = window_x0
- mouse_display_y = window_y0
-
- } else if( and( where, MOUSE_N )){ # elsewhere in title
- while( and( mouse_buttons, RIGHT_BUTTON )){
- if( mouse_window_deleted())
- return
- }
- beep()
-
- } else if( and( where, MOUSE_LEFT )){ # left margin
- split_window_horizontal( y - y0 )
- # mouse_display_y --
-
- } else if( and( where, MOUSE_S )){ # bottom margin
- split_window_vertical( x - x0 )
-
- } else {
- play( "o6g" )
- }
- }
-
- ### mouse library routines
-
- local function dynamic_window_move(){
- local xl = window_width, yl = window_height
- local nx0, ny0
-
- if( !mw || mw != current_window )
- return
-
- while( and( mouse_buttons, LEFT_BUTTON )){
- if( mouse_window_deleted())
- return
- nx0 = x0 + mouse_display_x - x
- ny0 = y0 + mouse_display_y - y
- if( nx0 != window_x0 || ny0 != window_y0 ){
- move_window( nx0, ny0, mw )
- display_update()
- }
- }
- }
-
-
- local function dynamic_window_resize(){
- local xl = window_width, yl = window_height
- local nxl, nyl
-
- if( mw && mw == current_window ){
- while( and( mouse_buttons, LEFT_BUTTON )){
- nxl = xl + mouse_display_x - x
- nyl = yl + mouse_display_y - y
- if(( nxl != window_width || nyl != window_height ) \
- && ( nyl >= WINDOW_MIN_HEIGHT && nxl >= WINDOW_MIN_WIDTH )){
- frame_window( x0, y0, nxl, nyl, mw )
- display_update()
- }
- }
- }
- }
-
-
- global function dynamic_reposition(){
- local first, margin, x0, y0
-
- if( collapsed())
- return
-
- first = window_first
- margin = window_margin
- x0 = window_text_x0
- y0 = window_text_y0
-
- raise_scroll_highlight()
- goto_pos( y - y0 + window_first, \
- x - x0 + window_margin + 1 ) # x was mouse_display_x
- drop_scroll_highlight()
- }
-
- global function dynamic_selection( type, button, suppress_marking ){
- local first, margin, x0, y0
- local xx, yy, oldx, oldy
-
- if( collapsed())
- return
-
- first = window_first
- margin = window_margin
- x0 = window_text_x0
- y0 = window_text_y0
-
- raise_scroll_highlight()
- goto_pos( y - y0 + window_first, \
- x - x0 + window_margin + 1 ) # x was mouse_display_x
- drop_scroll_highlight()
-
- oldx = current_column
- oldy = current_line
-
- raise_anchor()
- if ( !suppress_marking ) {
- drop_anchor( type, create_mark( TEMP_MARK ), 0 )
- }
- display_update()
-
- while( and( mouse_buttons, button )){
- xx = highlighted_scroll ? 1 : mouse_display_x - x0 + window_margin + 1
- yy = mouse_display_y - y0 + window_first
-
- if( xx != current_column || yy != current_line ){
- raise_scroll_highlight()
- goto_pos( yy, xx )
- drop_scroll_highlight()
- display_update()
- }
- }
-
- if( oldx == current_column \
- && oldy == current_line \
- && type != LINE_SELECTION )
- raise_anchor()
- }
-
-
- local function dynamic_page_up(){
- local smp = scroll_or_pan()
-
- raise_scroll_highlight()
- page_up( smp )
- drop_scroll_highlight()
- pause()
-
- while( and( mouse_buttons, LEFT_BUTTON )){
- raise_scroll_highlight()
- page_up( smp )
- drop_scroll_highlight()
- display_update()
- }
- }
-
-
- local function dynamic_page_down(){
- local smp = scroll_or_pan()
-
- raise_scroll_highlight()
- page_down( smp )
- drop_scroll_highlight()
- pause()
-
- while( and( mouse_buttons, LEFT_BUTTON )){
- raise_scroll_highlight()
- page_down( smp )
- drop_scroll_highlight()
- display_update()
- }
- }
-
-
- local function dynamic_scroll_horizontal( dir ){
- local smp = scroll_or_pan()
-
- scroll_horizontal( dir, smp )
- pause()
-
- while( and( mouse_buttons, LEFT_BUTTON )){
- scroll_horizontal( dir, smp )
- display_update()
- }
- }
-
-
- local function dynamic_scroll_vertical( direction ){
- local smp = scroll_or_pan()
-
- raise_scroll_highlight()
- scroll_vertical( direction, smp )
- drop_scroll_highlight()
- pause()
-
- while( and( mouse_buttons, LEFT_BUTTON )){
- raise_scroll_highlight()
- scroll_vertical( direction, smp )
- drop_scroll_highlight()
- display_update()
- }
- }
-
-
- #
- # delete the current window if left and right mouse buttons
- # are depressed, and only if it is a regular window
- #
- local function mouse_window_deleted(){
- if( (and( mouse_buttons, BOTH_BUTTONS ) == BOTH_BUTTONS )) {
- delete_tiled_window()
- while( and( mouse_buttons, BOTH_BUTTONS ))
- continue
- return 1
- }
- return 0
- }
-
-
- function mouse_create_window(){
- local x, y, xl, yl
-
- if ( !and( mouse_buttons, LEFT_BUTTON ))
- return;
-
- x = mouse_event_x
- y = mouse_event_y
-
- message( "SLIDE to opposite corner, then RELEASE" )
-
- while( and( mouse_buttons, LEFT_BUTTON ))
- ;
-
- xl = mouse_display_x - x + 1
- yl = mouse_display_y - y + 1
-
- # normalize so that x0,y0 is the upper left corner:
-
- if( xl < 0 ){
- xl = 2 - xl
- x -= xl - 1
- }
- if( yl < 0 ){
- yl = 2 - yl
- y -= yl - 1
- }
-
- if( xl >= WINDOW_MIN_WIDTH && yl >= WINDOW_MIN_HEIGHT ){
- # create the window:
-
- current_window = create_window( x, y, xl, yl )
- attach_window_buffer( current_window, current_buffer )
- color_current_window() # maybe change its color
-
- message( "" )
- } else {
- message( "window would be too small" )
- }
- }
-
- function elevator_position(){
- local pos = ( y1 - y0 - 3 ) * buffer_offset / buffer_size
- # message( " my = %d, y0 = %d, y1 = %d, off = %d, size = %d => %d", \
- # mouse_display_y, y0, y1, buffer_offset, buffer_size, pos )
- return pos
- }
-
- function dynamic_elevator(){
- local yy0 = ( y0 + 3 )
- local yy1 = ( y1 - 3 )
- local whole = yy1 - yy0
- local opart = -1
- local part = y - yy0
- local off
-
- if( whole > 0 ){
- do {
- if( part != opart ){
- opart = part
- off = buffer_size * part / whole
- raise_scroll_highlight()
- goto_buffer_offset( off )
- goto_bol()
- drop_scroll_highlight()
- skip_whitespace()
- display_update()
- }
- part = mouse_display_y - yy0
- } while( and( mouse_buttons, LEFT_BUTTON ))
- } else
- beep() # "can't" happen
- }
-
-
- function dynamic_flat_elevator(){
- local smp = scroll_or_pan()
- local xx0 = ( x0 + 3 )
- local xx1 = ( x1 - 3 )
- local whole = xx1 - xx0
- local part = x - xx0
- local delta
-
- if( whole > 0 ){
- do {
- delta = ( window_width * part / whole ) \
- - window_margin
- if( delta ){
- scroll_horizontal( delta, smp )
- display_update()
- }
- part = mouse_display_x - xx0
- } while( and( mouse_buttons, LEFT_BUTTON ))
- } else
- beep() # "can't" happen
- }
-
-
- local function collapsed(){
- if( and( window_flags, WINDOW_ZOOM ) == WINDOW_COLLAPSED ){
- beep()
- return 1
- } else
- return 0
- }
-
-
- # when this function is called, mouse_position() has been called, all
- # related variables have been set, and current_window and current_buffer
- # have been updated to the destination versions.
-
- local function move_or_copy_selection( copy ){
- local destination_window = current_window
- local source_window = mouse_prev_window
- local type
- local x0,y0
-
- if( collapsed())
- return
-
-
- y0 = y - window_text_y0 + window_first
- x0 = x - window_text_x0 + window_margin + 1
-
- # select source window (also selects source buffer)
-
- assign_current_window( source_window )
-
- # save source region, if any
-
- if(( type = region_type())){
- save_position()
- swap_marks()
- save_position()
- raise_anchor()
- }
-
- # locate new position in destination window (may change buffer)
- # and mark it.
-
- assign_current_window( destination_window )
-
- goto_pos( y0, x0 )
-
- create_mark( TEMP_MARK )
-
- # if there was a marked region, copy or move it to the new pos.
-
- if( type ){
- # restore source selection (removed by dynamic_reposition())
-
- assign_current_window( source_window )
-
- restore_position( 1 )
- drop_anchor( type )
- restore_position( 1 )
-
- # copy or delete to scrap
-
- if( copy ){
- copy_to_scrap()
- } else
- delete_to_scrap()
- raise_anchor()
-
- # insert it in the new location:
-
- assign_current_window( destination_window )
- goto_mark( TEMP_MARK )
- insert_scrap()
- }
- }
-
- local function move_selection(){
- move_or_copy_selection( 0 )
- }
-
- local function copy_selection(){
- move_or_copy_selection( 1 )
- }
-
-
- # local function peep(){
- # play( ">>l16a" )
- #}
-
- local function pause(){
- display_update()
- play( "_" )
- }
-
-
- local function raise_scroll_highlight(){
- if (highlighted_scroll)
- raise_anchor();
- }
-
- local function drop_scroll_highlight(){
- if (highlighted_scroll)
- drop_anchor( LINE_SELECTION );
- }
-
-
- global function setHighlightedScrolling( mode ){
- highlighted_scroll = 0+mode;
- }
-