Commit 668da7e9 authored by Davis King's avatar Davis King

Cleaned up some more gui code. I fixed a memory leak in the native_font object,

made the get_native_font() function return a shared_ptr_thread_safe object instead
of a raw pointer, and also made the letter object noncopyable (as it should have been).
I also moved some code from the widgets header file to the cpp file.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402574
parent d87afaa2
......@@ -657,7 +657,7 @@ namespace dlib
// ----------------------------------------------------------------------------------------
const font* get_native_font (
const shared_ptr_thread_safe<font> get_native_font (
)
{
return nativefont::native_font::get_font();
......
......@@ -120,6 +120,10 @@ namespace dlib
}
private:
// restricted functions
letter(letter&); // copy constructor
letter& operator=(letter&); // assignment operator
point* points;
unsigned short w;
unsigned short count;
......@@ -477,7 +481,7 @@ namespace dlib
// ----------------------------------------------------------------------------------------
const font* get_native_font ();
const shared_ptr_thread_safe<font> get_native_font ();
// ----------------------------------------------------------------------------------------
......
......@@ -140,9 +140,9 @@ namespace dlib
private:
// restricted functions
letter(letter&); // copy constructor
letter& operator=(letter&); // assignment operator
// restricted functions
letter(letter&); // copy constructor
letter& operator=(letter&); // assignment operator
};
inline void swap (
......
......@@ -7,6 +7,7 @@
#include "../gui_widgets.h"
#include "../unicode.h"
#include "../smart_pointers_thread_safe.h"
#include <map>
......@@ -528,21 +529,32 @@ namespace nativefont
ascender_ = 0;
get_letter((int)('x'));
}
virtual ~native_font() {}
std::map<int,dlib::letter *> letters;
typedef std::map<int,dlib::letter *> letters_map_type;
letters_map_type letters;
font_renderer::font_renderer fl;
public:
virtual ~native_font()
{
// delete all the letter objects we have in our letters map
letters_map_type::iterator i;
for (i = letters.begin(); i != letters.end(); ++i)
{
delete i->second;
}
}
virtual bool has_character (
dlib::unichar ch
)const{
return (*this)[ch].width() > 0;
}
static const font* get_font (
static const dlib::shared_ptr_thread_safe<font>& get_font (
)
{
static native_font f;
return &f;
static dlib::shared_ptr_thread_safe<font> f(new native_font);
return f;
}
virtual const dlib::letter& operator[] (dlib::unichar ch) const{
......
......@@ -2953,6 +2953,236 @@ namespace dlib
template class list_box<std::wstring>;
template class list_box<dlib::ustring>;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// function message_box()
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
namespace message_box_helper
{
void box_win::
initialize (
)
{
msg.set_pos(20,20);
msg.set_text(message);
rectangle msg_rect = msg.get_rect();
btn_ok.set_name("OK");
btn_ok.set_size(60,btn_ok.height());
if (msg_rect.width() >= 60)
btn_ok.set_pos(msg_rect.width()/2+msg_rect.left()-btn_ok.width()/2,msg_rect.bottom()+15);
else
btn_ok.set_pos(20,msg_rect.bottom()+15);
btn_ok.set_click_handler(*this,&box_win::on_click);
rectangle size = btn_ok.get_rect() + msg_rect;
set_size(size.right()+20,size.bottom()+20);
show();
set_title(title);
}
// ------------------------------------------------------------------------------------
box_win::
box_win (
const std::string& title_,
const std::string& message_
) :
drawable_window(false),
title(convert_mbstring_to_wstring(title_)),
message(convert_mbstring_to_wstring(message_)),
msg(*this),
btn_ok(*this)
{
initialize();
}
// ------------------------------------------------------------------------------------
box_win::
box_win (
const std::wstring& title_,
const std::wstring& message_
) :
drawable_window(false),
title(title_),
message(message_),
msg(*this),
btn_ok(*this)
{
initialize();
}
// ------------------------------------------------------------------------------------
box_win::
box_win (
const dlib::ustring& title_,
const dlib::ustring& message_
) :
drawable_window(false),
title(convert_utf32_to_wstring(title_)),
message(convert_utf32_to_wstring(message_)),
msg(*this),
btn_ok(*this)
{
initialize();
}
// ------------------------------------------------------------------------------------
box_win::
~box_win (
)
{
close_window();
}
// ------------------------------------------------------------------------------------
void box_win::
deleter_thread (
void* param
)
{
// The point of this extra member function pointer stuff is to allow the user
// to end the program from within the callback. So we want to destroy the
// window *before* we call their callback.
box_win& w = *reinterpret_cast<box_win*>(param);
w.close_window();
member_function_pointer<>::kernel_1a event_handler(w.event_handler);
delete &w;
if (event_handler.is_set())
event_handler();
}
// ------------------------------------------------------------------------------------
void box_win::
on_click (
)
{
hide();
create_new_thread(&deleter_thread,this);
}
// ------------------------------------------------------------------------------------
base_window::on_close_return_code box_win::
on_window_close (
)
{
// The point of this extra member function pointer stuff is to allow the user
// to end the program within the callback. So we want to destroy the
// window *before* we call their callback.
member_function_pointer<>::kernel_1a event_handler_copy(event_handler);
delete this;
if (event_handler_copy.is_set())
event_handler_copy();
return CLOSE_WINDOW;
}
// ------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------
void blocking_box_win::
initialize (
)
{
msg.set_pos(20,20);
msg.set_text(message);
rectangle msg_rect = msg.get_rect();
btn_ok.set_name("OK");
btn_ok.set_size(60,btn_ok.height());
if (msg_rect.width() >= 60)
btn_ok.set_pos(msg_rect.width()/2+msg_rect.left()-btn_ok.width()/2,msg_rect.bottom()+15);
else
btn_ok.set_pos(20,msg_rect.bottom()+15);
btn_ok.set_click_handler(*this,&blocking_box_win::on_click);
rectangle size = btn_ok.get_rect() + msg_rect;
set_size(size.right()+20,size.bottom()+20);
set_title(title);
show();
}
// ------------------------------------------------------------------------------------
blocking_box_win::
blocking_box_win (
const std::string& title_,
const std::string& message_
) :
drawable_window(false),
title(convert_mbstring_to_wstring(title_)),
message(convert_mbstring_to_wstring(message_)),
msg(*this),
btn_ok(*this)
{
initialize();
}
// ------------------------------------------------------------------------------------
blocking_box_win::
blocking_box_win (
const std::wstring& title_,
const std::wstring& message_
) :
drawable_window(false),
title(title_),
message(message_),
msg(*this),
btn_ok(*this)
{
initialize();
}
// ------------------------------------------------------------------------------------
blocking_box_win::
blocking_box_win (
const dlib::ustring& title_,
const dlib::ustring& message_
) :
drawable_window(false),
title(convert_utf32_to_wstring(title_)),
message(convert_utf32_to_wstring(message_)),
msg(*this),
btn_ok(*this)
{
initialize();
}
// ------------------------------------------------------------------------------------
blocking_box_win::
~blocking_box_win (
)
{
close_window();
}
// ------------------------------------------------------------------------------------
void blocking_box_win::
on_click (
)
{
close_window();
}
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_WIDGETs_CPP_
......
......@@ -1134,69 +1134,25 @@ namespace dlib
class box_win : public drawable_window
{
void initialize (
)
{
msg.set_pos(20,20);
msg.set_text(message);
rectangle msg_rect = msg.get_rect();
btn_ok.set_name("OK");
btn_ok.set_size(60,btn_ok.height());
if (msg_rect.width() >= 60)
btn_ok.set_pos(msg_rect.width()/2+msg_rect.left()-btn_ok.width()/2,msg_rect.bottom()+15);
else
btn_ok.set_pos(20,msg_rect.bottom()+15);
btn_ok.set_click_handler(*this,&box_win::on_click);
rectangle size = btn_ok.get_rect() + msg_rect;
set_size(size.right()+20,size.bottom()+20);
show();
set_title(title);
}
);
public:
box_win (
const std::string& title_,
const std::string& message_
) :
drawable_window(false),
title(convert_mbstring_to_wstring(title_)),
message(convert_mbstring_to_wstring(message_)),
msg(*this),
btn_ok(*this)
{
initialize();
}
);
box_win (
const std::wstring& title_,
const std::wstring& message_
) :
drawable_window(false),
title(title_),
message(message_),
msg(*this),
btn_ok(*this)
{
initialize();
}
);
box_win (
const dlib::ustring& title_,
const dlib::ustring& message_
) :
drawable_window(false),
title(convert_utf32_to_wstring(title_)),
message(convert_utf32_to_wstring(message_)),
msg(*this),
btn_ok(*this)
{
initialize();
}
);
~box_win (
)
{
close_window();
}
);
template <
typename T
......@@ -1214,38 +1170,13 @@ namespace dlib
static void deleter_thread (
void* param
)
{
// The point of this extra member function pointer stuff is to allow the user
// to end the program from within the callback. So we want to destroy the
// window *before* we call their callback.
box_win& w = *reinterpret_cast<box_win*>(param);
w.close_window();
member_function_pointer<>::kernel_1a event_handler(w.event_handler);
delete &w;
if (event_handler.is_set())
event_handler();
}
);
void on_click (
)
{
hide();
create_new_thread(&deleter_thread,this);
}
);
on_close_return_code on_window_close (
)
{
// The point of this extra member function pointer stuff is to allow the user
// to end the program within the callback. So we want to destroy the
// window *before* we call their callback.
member_function_pointer<>::kernel_1a event_handler_copy(event_handler);
delete this;
if (event_handler_copy.is_set())
event_handler_copy();
return CLOSE_WINDOW;
}
);
const std::wstring title;
const std::wstring message;
......@@ -1258,79 +1189,31 @@ namespace dlib
class blocking_box_win : public drawable_window
{
void initialize (
)
{
msg.set_pos(20,20);
msg.set_text(message);
rectangle msg_rect = msg.get_rect();
btn_ok.set_name("OK");
btn_ok.set_size(60,btn_ok.height());
if (msg_rect.width() >= 60)
btn_ok.set_pos(msg_rect.width()/2+msg_rect.left()-btn_ok.width()/2,msg_rect.bottom()+15);
else
btn_ok.set_pos(20,msg_rect.bottom()+15);
btn_ok.set_click_handler(*this,&blocking_box_win::on_click);
);
rectangle size = btn_ok.get_rect() + msg_rect;
set_size(size.right()+20,size.bottom()+20);
set_title(title);
show();
}
public:
blocking_box_win (
const std::string& title_,
const std::string& message_
) :
drawable_window(false),
title(convert_mbstring_to_wstring(title_)),
message(convert_mbstring_to_wstring(message_)),
msg(*this),
btn_ok(*this)
{
initialize();
}
);
blocking_box_win (
const std::wstring& title_,
const std::wstring& message_
) :
drawable_window(false),
title(title_),
message(message_),
msg(*this),
btn_ok(*this)
{
initialize();
}
);
blocking_box_win (
const dlib::ustring& title_,
const dlib::ustring& message_
) :
drawable_window(false),
title(convert_utf32_to_wstring(title_)),
message(convert_utf32_to_wstring(message_)),
msg(*this),
btn_ok(*this)
{
initialize();
}
);
~blocking_box_win (
)
{
close_window();
}
);
private:
void on_click (
)
{
close_window();
}
);
const std::wstring title;
const std::wstring message;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment