src/window.cpp
changeset 8925 e0d37ce1eba8
parent 8924 7063881d180a
child 8940 afe7bc9fcc6e
equal deleted inserted replaced
8924:7063881d180a 8925:e0d37ce1eba8
    23 #include "table/sprites.h"
    23 #include "table/sprites.h"
    24 
    24 
    25 static Point _drag_delta; ///< delta between mouse cursor and upper left corner of dragged window
    25 static Point _drag_delta; ///< delta between mouse cursor and upper left corner of dragged window
    26 static Window *_mouseover_last_w = NULL; ///< Window of the last MOUSEOVER event
    26 static Window *_mouseover_last_w = NULL; ///< Window of the last MOUSEOVER event
    27 
    27 
    28 static Window _windows[MAX_NUMBER_OF_WINDOWS];
       
    29 
       
    30 /**
    28 /**
    31  * List of windows opened at the screen.
    29  * List of windows opened at the screen.
    32  * Uppermost window is at  _z_windows[_last_z_window - 1],
    30  * Uppermost window is at  _z_windows[_last_z_window - 1],
    33  * bottom window is at _z_windows[0]
    31  * bottom window is at _z_windows[0]
    34  */
    32  */
    35 Window *_z_windows[lengthof(_windows)];
    33 Window *_z_windows[MAX_NUMBER_OF_WINDOWS];
    36 Window **_last_z_window; ///< always points to the next free space in the z-array
    34 Window **_last_z_window; ///< always points to the next free space in the z-array
    37 
    35 
    38 Point _cursorpos_drag_start;
    36 Point _cursorpos_drag_start;
    39 
    37 
    40 int _scrollbar_start_pos;
    38 int _scrollbar_start_pos;
   431 	 * by moving all windows after it one to the left */
   429 	 * by moving all windows after it one to the left */
   432 	Window **wz = FindWindowZPosition(w);
   430 	Window **wz = FindWindowZPosition(w);
   433 	if (wz == NULL) return;
   431 	if (wz == NULL) return;
   434 	memmove(wz, wz + 1, (byte*)_last_z_window - (byte*)wz);
   432 	memmove(wz, wz + 1, (byte*)_last_z_window - (byte*)wz);
   435 	_last_z_window--;
   433 	_last_z_window--;
       
   434 
       
   435 	delete w;
   436 }
   436 }
   437 
   437 
   438 /**
   438 /**
   439  * Find a window by its class and window number
   439  * Find a window by its class and window number
   440  * @param cls Window class
   440  * @param cls Window class
   651 		w->widget_count = index - 1;
   651 		w->widget_count = index - 1;
   652 	} else {
   652 	} else {
   653 		w->widget = NULL;
   653 		w->widget = NULL;
   654 		w->widget_count = 0;
   654 		w->widget_count = 0;
   655 	}
   655 	}
   656 }
       
   657 
       
   658 static Window *FindFreeWindow()
       
   659 {
       
   660 	Window *w;
       
   661 
       
   662 	for (w = _windows; w < endof(_windows); w++) {
       
   663 		Window* const *wz;
       
   664 		bool window_in_use = false;
       
   665 
       
   666 		FOR_ALL_WINDOWS(wz) {
       
   667 			if (*wz == w) {
       
   668 				window_in_use = true;
       
   669 				break;
       
   670 			}
       
   671 		}
       
   672 
       
   673 		if (!window_in_use) return w;
       
   674 	}
       
   675 
       
   676 	assert(_last_z_window == endof(_z_windows));
       
   677 	return NULL;
       
   678 }
   656 }
   679 
   657 
   680 /** Open a new window.
   658 /** Open a new window.
   681  * This function is called from AllocateWindow() or AllocateWindowDesc()
   659  * This function is called from AllocateWindow() or AllocateWindowDesc()
   682  * See descriptions for those functions for usage
   660  * See descriptions for those functions for usage
   695  * @param data the data to be given during the WE_CREATE message
   673  * @param data the data to be given during the WE_CREATE message
   696  * @return Window pointer of the newly created window */
   674  * @return Window pointer of the newly created window */
   697 static Window *LocalAllocateWindow(int x, int y, int min_width, int min_height, int def_width, int def_height,
   675 static Window *LocalAllocateWindow(int x, int y, int min_width, int min_height, int def_width, int def_height,
   698 				WindowProc *proc, WindowClass cls, const Widget *widget, int window_number, void *data)
   676 				WindowProc *proc, WindowClass cls, const Widget *widget, int window_number, void *data)
   699 {
   677 {
   700 	Window *w = FindFreeWindow();
   678 	Window *w;
   701 
   679 
   702 	/* We have run out of windows, close one and use that as the place for our new one */
   680 	/* We have run out of windows, close one and use that as the place for our new one */
   703 	if (w == NULL) {
   681 	if (_last_z_window == endof(_z_windows)) {
   704 		w = FindDeletableWindow();
   682 		w = FindDeletableWindow();
   705 		if (w == NULL) w = ForceFindDeletableWindow();
   683 		if (w == NULL) w = ForceFindDeletableWindow();
   706 		DeleteWindow(w);
   684 		DeleteWindow(w);
   707 	}
   685 	}
   708 
   686 
       
   687 	w = new Window;
       
   688 
   709 	/* Set up window properties */
   689 	/* Set up window properties */
   710 	memset(w, 0, sizeof(*w));
       
   711 	w->window_class = cls;
   690 	w->window_class = cls;
   712 	w->flags4 = WF_WHITE_BORDER_MASK; // just opened windows have a white border
   691 	w->flags4 = WF_WHITE_BORDER_MASK; // just opened windows have a white border
   713 	w->caption_color = 0xFF;
   692 	w->caption_color = 0xFF;
   714 	w->left = x;
   693 	w->left = x;
   715 	w->top = y;
   694 	w->top = y;
  1055  */
  1034  */
  1056 void InitWindowSystem()
  1035 void InitWindowSystem()
  1057 {
  1036 {
  1058 	IConsoleClose();
  1037 	IConsoleClose();
  1059 
  1038 
  1060 	memset(&_windows, 0, sizeof(_windows));
       
  1061 	_last_z_window = _z_windows;
  1039 	_last_z_window = _z_windows;
  1062 	InitViewports();
  1040 	InitViewports();
  1063 	_no_scroll = 0;
  1041 	_no_scroll = 0;
  1064 }
  1042 }
  1065 
  1043