home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / System / Swatch / Development / swatch 1.7 / window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-05  |  11.1 KB  |  513 lines  |  [TEXT/KAHL]

  1. /**
  2.  
  3.     window.c
  4.     Copyright (c) 1990-1992, joe holt
  5.  
  6.  **/
  7.  
  8.  
  9. /**-----------------------------------------------------------------------------
  10.  **
  11.  **    Headers
  12.  **
  13.  **/
  14.  
  15. #ifndef __content__
  16. #include "content.h"
  17. #endif
  18. #ifndef __ctypes__
  19. #include "ctypes.h"
  20. #endif
  21. #ifndef __display__
  22. #include "display.h"
  23. #endif
  24. #ifndef __heap_list__
  25. #include "heap_list.h"
  26. #endif
  27. #ifndef __prefs__
  28. #include "prefs.h"
  29. #endif
  30. #ifndef __pstring__
  31. #include "pstring.h"
  32. #endif
  33. #ifndef __resources__
  34. #include "resources.h"
  35. #endif
  36. #ifndef __swatch__
  37. #include "swatch.h"
  38. #endif
  39. #ifndef __window__
  40. #include "window.h"
  41. #endif
  42.  
  43.  
  44. /**-----------------------------------------------------------------------------
  45.  **
  46.  ** Private Constants
  47.  **
  48.  **/
  49.  
  50. #define HORIZ_SCROLL_UNIT_DELTA        10
  51.  
  52. #define MIN_WINDOW_WIDTH    100
  53. #define MIN_WINDOW_HEIGHT    100
  54.  
  55.  
  56. /**-----------------------------------------------------------------------------
  57.  **
  58.  ** Private Variables
  59.  **
  60.  **/
  61.  
  62. static MBox_proc_t mbox_proc;
  63.  
  64.  
  65. /**-----------------------------------------------------------------------------
  66.  **
  67.  ** Private Functions
  68.  **
  69.  **/
  70.  
  71. static void draw_grow_icon( Boolean active_state, Boolean rgn_state );
  72.  
  73. static void size_controls( void );
  74.  
  75. static pascal void vert_scroll_proc( ControlHandle the_control, int16 part );
  76. static pascal void horiz_scroll_proc( ControlHandle the_control, int16 part );
  77.  
  78.  
  79. /*******************************************************************************
  80.  **
  81.  **    Public Variables
  82.  **
  83.  **/
  84.  
  85. ControlHandle horiz_scroll, vert_scroll;
  86. WindowPtr App_window;
  87. int16 MBox_right;
  88.  
  89.  
  90. /*******************************************************************************
  91.  **
  92.  **    Public Functions
  93.  **
  94.  **/
  95.  
  96. void Window_init( void )
  97. {
  98.     Rect bounds;
  99.  
  100.     bounds = Prefs.window_rect;
  101.     App_window = This_mac.hasColorQD ?
  102.             NewCWindow( nil, &bounds, (StringPtr) pstr(Window_title_STR_x), true,
  103.                     documentProc, (WindowPtr) -1, true, 0 ) :
  104.             NewWindow( nil, &bounds, (StringPtr) pstr(Window_title_STR_x), true,
  105.                     documentProc, (WindowPtr) -1, true, 0 );
  106.  
  107.     SetPort( App_window );
  108.     TextFont( geneva );
  109.     TextSize( 9 );
  110.  
  111.     Window_clip_with_controls();
  112.     SetRect( &bounds, 16384, 16384, 16384, 16384 );
  113.     vert_scroll = NewControl( App_window, &bounds, nil, true, 0, 0, 0, scrollBarProc, 0 );
  114.     horiz_scroll = NewControl( App_window, &bounds, nil, true, 0, 0, 0, scrollBarProc, 0 );
  115.     size_controls();
  116.  
  117.     Check_color_usage();
  118. }
  119.  
  120.  
  121. void Window_idle( void )
  122. {
  123.     int32 old_largest;
  124.     int16 update;
  125.  
  126.     Check_color_usage();
  127.     old_largest = Heap_list_largest_heap_size();
  128.     if ( Heap_list_update() ) {
  129.         Window_reset_vert_scroll();
  130.         update = UPDATE_COLUMNS | UPDATE_BOTTOM | FORCE_UPDATE;
  131.     }
  132.     else update = UPDATE_IDLE_COLUMNS;
  133.  
  134.     if ( Heap_list_largest_heap_size() != old_largest )    // largest has changed
  135.         Window_reset_horiz_scroll();
  136.  
  137.     if ( Swatch_in_foreground() ) {
  138.         Heap_info_handle_t h = nil;
  139.         while ( h = l_next( heaps, h ) ) {
  140.             Heap_info_t *hi = l_access( h );
  141.             if ( hi->has_selection ) hi->selection_phase = (hi->selection_phase + 1) & 7;
  142.             l_release( h );
  143.         }
  144.     }
  145.  
  146.     Display_lines( update, DISPLAY_ALL, DISPLAY_ALL );
  147. }
  148.  
  149.  
  150. Boolean Window_mousedown( EventRecord *the_event, int16 part )
  151. {
  152.     Boolean quit = false;
  153.     Rect old_global_window, new_global_window;
  154.  
  155.     old_global_window = App_window->portRect;
  156.     LocalToGlobal( (Point *)&old_global_window );
  157.     LocalToGlobal( (Point *)&old_global_window.bottom );
  158.     
  159.     switch ( part ) {
  160.     case inDrag: {
  161.         Rect drag_rect;
  162.         drag_rect = (**GrayRgn).rgnBBox;
  163.         DragWindow( App_window, the_event->where, &drag_rect );
  164.         break;
  165.         }
  166.  
  167.     case inContent: {
  168.         ControlHandle the_control;
  169.         int16 i, j;
  170.  
  171.         GlobalToLocal( &the_event->where );
  172.         Content_set_cursor( the_event );
  173.         i = FindControl( the_event->where, App_window, &the_control );
  174.         if ( the_control == vert_scroll ) {
  175.             if ( i == inThumb ) {
  176.                 i = GetCtlValue( the_control );
  177.                 TrackControl( the_control, the_event->where, nil );
  178.                 if ( GetCtlValue( the_control ) != i ) {
  179.                     Display_update_row_tops();
  180.                     Display_lines( UPDATE_EVERYTHING | FORCE_UPDATE,
  181.                             DISPLAY_ALL, DISPLAY_ALL );
  182.                 }
  183.             }
  184.             else
  185.                 TrackControl( the_control, the_event->where,
  186.                         (ProcPtr) vert_scroll_proc );
  187.         }
  188.         else if ( the_control == horiz_scroll ) {
  189.             if ( i == inThumb ) {
  190.                 i = GetCtlValue( the_control );
  191.                 TrackControl( the_control, the_event->where, nil );
  192.                 if ( ( j = GetCtlValue( the_control ) ) != i ) {
  193.                     Content_horiz_scrolled( j );
  194.                     Display_lines( UPDATE_HEAP | UPDATE_SELECTION,
  195.                             DISPLAY_ALL, DISPLAY_ALL );
  196.                 }
  197.             }
  198.             else
  199.                 TrackControl( the_control, the_event->where,
  200.                         (ProcPtr) horiz_scroll_proc );
  201.         }
  202.         else Content_mousedown( the_event );
  203.         break;
  204.     }
  205.  
  206.     case inGrow: {
  207.         uns32 new_size;
  208.         Rect size_rect;
  209.  
  210.         SetRect( &size_rect, MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT, 32767, 32767 );
  211.         new_size = GrowWindow( App_window, the_event->where, &size_rect );
  212.         if ( new_size ) {
  213.             draw_grow_icon( false, false );
  214.             HideControl( horiz_scroll );
  215.             HideControl( vert_scroll );
  216.             Window_erase_mbox();
  217.             SizeWindow( App_window, (int16) new_size, new_size >> 16, true );
  218.             Window_clip_with_controls();
  219.             size_controls();
  220.             Window_update_mbox();
  221.             draw_grow_icon( true, true );
  222.             Display_header( UPDATE_HEAP_SCALE );
  223.         }
  224.         break;
  225.     }
  226.  
  227.     case inGoAway:
  228.         if ( TrackGoAway( App_window, the_event->where ) )
  229.             quit = true;
  230.         break;
  231.  
  232.     default:
  233.         break;
  234.     }
  235.  
  236.     new_global_window = App_window->portRect;
  237.     LocalToGlobal( (Point *)&new_global_window );
  238.     LocalToGlobal( (Point *)&new_global_window.bottom );
  239.     if ( !EqualRect( &old_global_window, &new_global_window ) ) {
  240.         Prefs.window_rect = new_global_window;
  241.         Prefs.dirty = true;
  242.     }
  243.  
  244.     return quit;
  245. }
  246.  
  247.  
  248. void Window_update( EventRecord *the_event )
  249. {
  250.     BeginUpdate( App_window );
  251.     UpdtControl( App_window, App_window->visRgn );
  252.     Window_update_mbox();
  253.     draw_grow_icon( Swatch_in_foreground(), true );
  254.     Display_lines( UPDATE_COLUMNS_AND_BORDER | FORCE_UPDATE, DISPLAY_ALL, DISPLAY_ALL );
  255.     EndUpdate( App_window );
  256. }
  257.  
  258.  
  259. void Window_activate( EventRecord *the_event, Boolean active_state )
  260. {
  261.     int16 control_state;
  262.  
  263.     draw_grow_icon( active_state, true );
  264.     control_state = active_state ? 0 : 255;
  265.     HiliteControl( vert_scroll, control_state );
  266.     HiliteControl( horiz_scroll, control_state );
  267.     GlobalToLocal( &the_event->where );
  268.     Content_set_cursor( the_event );
  269. }
  270.  
  271.  
  272. void Window_close( void )
  273. {
  274.     CloseWindow( App_window );
  275. }
  276.  
  277.  
  278. ControlHandle Window_horiz_scroll( void )
  279. {
  280.     return horiz_scroll;
  281. }
  282.  
  283.  
  284. ControlHandle Window_vert_scroll( void )
  285. {
  286.     return vert_scroll;
  287. }
  288.  
  289.  
  290. Boolean Window_reset_horiz_scroll( void )
  291. {
  292.     return Content_reset_horiz( horiz_scroll );
  293. }
  294.  
  295.  
  296. void Window_reset_vert_scroll( void )
  297. {
  298.     int16 i;
  299.     int16 old_rows, old_value;
  300.     Rect r;
  301.  
  302.     old_rows = Content_rows();
  303.     old_value = GetCtlValue( vert_scroll );
  304.     Content_reset_vert();
  305.  
  306.     i = Heap_list_count() - Content_rows();
  307.     if ( i < 0 ) i = 0;
  308.     SetCtlMax( vert_scroll, i );
  309.  
  310.     if ( old_value && Content_rows() > old_rows ) {
  311.         old_value -= Content_rows() - old_rows;
  312.         SetCtlValue( vert_scroll, ( old_value < 0 ? 0 : old_value ) );
  313.         r = App_window->portRect;
  314.         r.top += CELL_HEIGHT;
  315.         r.right -= 15;
  316.         r.bottom -= 15;
  317.         InvalRect( &r );
  318.     }
  319. }
  320.  
  321.  
  322.  
  323. void Window_set_mbox( MBox_proc_t *handler )
  324. {
  325.     mbox_proc = handler;
  326. }
  327.  
  328.  
  329. void Window_erase_mbox( void )
  330. {
  331.     Rect r;
  332.  
  333.     r.left = 0;
  334.     r.right = MBox_right;
  335.     r.top = App_window->portRect.bottom - 15;
  336.     r.bottom = App_window->portRect.bottom;
  337.  
  338.     EraseRect( &r );
  339.     InvalRect( &r );
  340. }
  341.  
  342.  
  343. void Window_update_mbox( void )
  344. {
  345.     Rect r;
  346.     RgnHandle save_clipRgn;
  347.  
  348.     r.left = 0;
  349.     r.right = MBox_right;
  350.     r.top = App_window->portRect.bottom - 14;
  351.     r.bottom = App_window->portRect.bottom;
  352.     EraseRect( &r );
  353.  
  354.     save_clipRgn = NewRgn();
  355.     GetClip( save_clipRgn );
  356.     InsetRect( &r, 1, 1 );
  357.     ClipRect( &r );
  358.  
  359.     if ( mbox_proc ) mbox_proc( &r );
  360.  
  361.     SetClip( save_clipRgn );
  362.     DisposeRgn( save_clipRgn );
  363. }
  364.  
  365.  
  366. void Window_clip_with_controls( void )
  367. {
  368.     ClipRect( &App_window->portRect );
  369. }
  370.  
  371.  
  372. void Window_clip_without_controls( void )
  373. {
  374.     Rect r;
  375.  
  376.     r = App_window->portRect;
  377.     r.right -= 15;
  378.     r.bottom -= 15;
  379.     ClipRect( &r );
  380. }
  381.  
  382.  
  383. /**-----------------------------------------------------------------------------
  384.  **
  385.  ** Private Functions
  386.  **
  387.  **/
  388.  
  389. static void draw_grow_icon( Boolean active_state, Boolean rgn_state )
  390. {
  391.     Boolean save_hilited;
  392.     Rect r;
  393.  
  394.     r = App_window->portRect;
  395.  
  396.     Set_fore_color_or_pattern( HEADER_BORDER_COLOR );
  397.     MoveTo( MBox_right, BORDER_BOTTOM );
  398.     LineTo( MBox_right, r.bottom - 15 );
  399.     Set_fore_color_or_pattern( BLACK_COLOR );
  400.  
  401.     r.top += BORDER_BOTTOM;
  402.     ClipRect( &r );
  403.     save_hilited = ((WindowPeek) App_window)->hilited;
  404.     ((WindowPeek) App_window)->hilited = active_state;
  405.     DrawGrowIcon( App_window );
  406.     ((WindowPeek) App_window)->hilited = save_hilited;
  407.     r.top = r.bottom - 15;
  408.     r.left = r.right - 15;
  409.     if ( rgn_state )
  410.         ValidRect( &r );
  411.     else
  412.         InvalRect( &r );
  413.     Window_clip_with_controls();
  414. }
  415.  
  416.  
  417. static void size_controls( void )
  418. {
  419.     int16 top, left, bottom, right;
  420.  
  421.     Window_reset_horiz_scroll();
  422.     Window_reset_vert_scroll();
  423.  
  424.     left = App_window->portRect.right - 15;
  425.     right = App_window->portRect.right + 1;
  426.     top = BORDER_BOTTOM - 1;
  427.     bottom = App_window->portRect.bottom - 14;
  428.     HideControl( vert_scroll );
  429.     MoveControl( vert_scroll, left, top );
  430.     SizeControl( vert_scroll, right - left, bottom - top );
  431.     ShowControl( vert_scroll );
  432.  
  433.     left = MBox_right;
  434.     right = App_window->portRect.right - 14;
  435.     top = App_window->portRect.bottom - 15;
  436.     bottom = App_window->portRect.bottom + 1;
  437.     HideControl( horiz_scroll );
  438.     MoveControl( horiz_scroll, left, top );
  439.     SizeControl( horiz_scroll, right - left, bottom - top );
  440.     ShowControl( horiz_scroll );
  441. }
  442.  
  443.  
  444. static pascal void vert_scroll_proc( ControlHandle the_control, int16 part )
  445. {
  446.     Rect r;
  447.     int16 old, new;
  448.     RgnHandle aRgn;
  449.  
  450.     aRgn = NewRgn();
  451.     r = App_window->portRect;
  452.     r.right -= 15;
  453.     r.top = BORDER_BOTTOM;
  454.     r.bottom -= 15;
  455.     old = GetCtlValue( the_control );
  456.  
  457.     if ( part == inUpButton ) {
  458.         if ( old ) {
  459.             SetCtlValue( the_control, old - 1 );
  460.             ScrollRect( &r, 0, CELL_HEIGHT, aRgn );
  461.             Display_update_row_tops();
  462.             Display_lines( UPDATE_COLUMNS | FORCE_UPDATE, 0, 0 );
  463.         }
  464.     }
  465.  
  466.     else if ( part == inDownButton ) {
  467.         if ( old < Heap_list_count() - Content_rows() ) {
  468.             SetCtlValue( the_control, old + 1 );
  469.             ScrollRect( &r, 0, -CELL_HEIGHT, aRgn );
  470.             Display_update_row_tops();
  471.             Display_lines( UPDATE_COLUMNS | UPDATE_BOTTOM | FORCE_UPDATE,
  472.                     Content_rows() - 1,
  473.                     Content_rows() + Content_partial_row() - 1 );
  474.         }
  475.     }
  476.  
  477.     else if ( part == inPageUp ) {
  478.         new = old - ( Content_rows() - 1 );
  479.         if ( new < 0 )
  480.             new = 0;
  481.         if ( new != old ) {
  482.             SetCtlValue( the_control, new );
  483.             Display_update_row_tops();
  484.             Display_lines( UPDATE_COLUMNS | FORCE_UPDATE, DISPLAY_ALL, DISPLAY_ALL );
  485.         }
  486.     }
  487.  
  488.     else if ( part == inPageDown ) {
  489.         new = old + ( Content_rows() - 1 );
  490.         if ( new > Heap_list_count() - Content_rows() )
  491.             new = Heap_list_count() - Content_rows();
  492.         if ( new != old ) {
  493.             SetCtlValue( the_control, new );
  494.             Display_update_row_tops();
  495.             Display_lines( UPDATE_COLUMNS | UPDATE_BOTTOM | FORCE_UPDATE, DISPLAY_ALL,
  496.                     DISPLAY_ALL );
  497.         }
  498.     }
  499.  
  500.     DisposeRgn( aRgn );
  501.  
  502.     Set_fore_color_or_pattern( HEADER_BORDER_COLOR );
  503.     MoveTo( MBox_right, BORDER_BOTTOM );
  504.     LineTo( MBox_right, App_window->portRect.bottom - 15 );
  505.     Set_fore_color_or_pattern( BLACK_COLOR );
  506. }
  507.  
  508.  
  509. static pascal void horiz_scroll_proc( ControlHandle the_control, int16 part )
  510. {
  511.     Content_scroll_horiz( the_control, part, HORIZ_SCROLL_UNIT_DELTA );
  512. }
  513.